Display a linear combination: Difference between revisions

Content added Content deleted
Line 27: Line 27:
=={{header|Perl 6}}==
=={{header|Perl 6}}==
<lang perl6>sub linear-combination(@coeff) {
<lang perl6>sub linear-combination(@coeff) {
none(@coeff) ?? '0' !!
(@coeff Z=> map { "e($_)" }, 1 .. *)\
join(
.grep(+*.key)\
'+',
.map({ .key ~ '*' ~ .value })\
.join('+')\
map { .key ~ '*' ~ .value },
.subst('+-', '-', :g)\
grep +*.key,
.subst(/<|w>1\*/, '', :g)
(@coeff Z=> map { "e($_)" }, 1 .. *)
)\
|| '0'
.subst('+-', '-', :g)\
.subst(/<|w>1\*/, '', :g);
}
}


say linear-combination(@$_) for
say linear-combination($_) for
[1, 2, 3],
[1, 2, 3],
[0, 1, 2, 3],
[0, 1, 2, 3],
Line 48: Line 46:
[-1, -1, -1],
[-1, -1, -1],
[-1, -2, 0, -3],
[-1, -2, 0, -3],
[ -1]
[-1 ]
;</lang>
;</lang>
{{out}}
{{out}}