Display a linear combination: Difference between revisions

Content added Content deleted
(added Arturo implementation)
Line 190: Line 190:
-e(1)
-e(1)
</pre>
</pre>

=={{header|Arturo}}==

<syntaxhighlight lang="arturo">linearCombination: function [coeffs][
combo: new []
loop.with:'i coeffs 'x [
case [x]
when? [=0] []
when? [=1] -> 'combo ++ ~"e(|i+1|)"
when? [= neg 1] -> 'combo ++ ~"-e(|i+1|)"
else -> 'combo ++ ~"|x|*e(|i+1|)"
]
join.with: " + " 'combo
replace 'combo {/\+ -/} "- "
(empty? combo)? -> "0" -> combo
]

loop @[
[1 2 3]
[0 1 2 3]
[1 0 3 4]
[1 2 0]
[0 0 0]
[0]
[1 1 1]
@[neg 1 neg 1 neg 1]
@[neg 1 neg 2 0 neg 3]
@[neg 1]
] => [print linearCombination &]
</syntaxhighlight>

{{out}}

<pre>e(1) + 2*e(2) + 3*e(3)
e(2) + 2*e(3) + 3*e(4)
e(1) + 3*e(3) + 4*e(4)
e(1) + 2*e(2)
0
0
e(1) + e(2) + e(3)
-e(1) - e(2) - e(3)
-e(1) - 2*e(2) - 3*e(4)
-e(1)</pre>


=={{header|C}}==
=={{header|C}}==