Display a linear combination: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: added whitespace, added a template for the output.)
(Added Factor)
Line 378: Line 378:
</pre>
</pre>


=={{header|Factor}}==
<lang factor>USING: formatting kernel match math pair-rocket regexp sequences ;

MATCH-VARS: ?a ?b ;

: choose-term ( coeff i -- str )
1 + { } 2sequence {
{ 0 _ } => [ "" ]
{ 1 ?a } => [ ?a "e(%d)" sprintf ]
{ -1 ?a } => [ ?a "-e(%d)" sprintf ]
{ ?a ?b } => [ ?a ?b "%d*e(%d)" sprintf ]
} match-cond ;
: linear-combo ( seq -- str )
[ choose-term ] map-index harvest " + " join
R/ \+ -/ "- " re-replace dup empty? [ drop "0" ] when ;
{ { 1 2 3 } { 0 1 2 3 } { 1 0 3 4 } { 1 2 0 } { 0 0 0 } { 0 }
{ 1 1 1 } { -1 -1 -1 } { -1 -2 0 -3 } { -1 } }
[ dup linear-combo "%-14u -> %s\n" printf ] each</lang>
{{out}}
<pre>
{ 1 2 3 } -> e(1) + 2*e(2) + 3*e(3)
{ 0 1 2 3 } -> e(2) + 2*e(3) + 3*e(4)
{ 1 0 3 4 } -> e(1) + 3*e(3) + 4*e(4)
{ 1 2 0 } -> e(1) + 2*e(2)
{ 0 0 0 } -> 0
{ 0 } -> 0
{ 1 1 1 } -> e(1) + e(2) + e(3)
{ -1 -1 -1 } -> -e(1) - e(2) - e(3)
{ -1 -2 0 -3 } -> -e(1) - 2*e(2) - 3*e(4)
{ -1 } -> -e(1)
</pre>


=={{header|Go}}==
=={{header|Go}}==