Display a linear combination: Difference between revisions
Content added Content deleted
m (→{{header|Perl 6}}: forgot to remove a line) |
(→{{header|Perl 6}}: more compact code. Arguably more obscure, but still probably OK) |
||
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' !! |
|||
join( |
|||
⚫ | |||
⚫ | |||
map { .key ~ '*' ~ .value }, |
|||
⚫ | |||
return '0' unless @terms; |
|||
(@coeff Z=> map { "e($_)" }, 1 .. *) |
|||
)\ |
|||
⚫ | |||
.subst('+-', '-', :g)\ |
|||
.subst(/<|w>1\*/, '', :g); |
|||
@terms; |
|||
$result ~~ s:g/'+-'/-/; |
|||
$result ~~ s:g/<|w>1\*//; |
|||
return $result; |
|||
} |
} |
||