Display a linear combination: Difference between revisions

Content added Content deleted
Line 1,845: Line 1,845:
-e1-2*e2-3*e4
-e1-2*e2-3*e4
-e1
-e1
</pre>

=={{header|RPL}}==
RPL can handle both stack-based program flows and algebraic expressions, which is quite useful for tasks such as this one.
{{works with|Halcyon Calc|4.2.7}}
===Straightforward approach===
This version has the disadvantage of sometimes interchanging some terms when simplifying the expression by the COLCT function.
≪ → scalars
≪ '0' 1 scalars SIZE FOR j
scalars j GET
"e" j →STR + STR→ * +
NEXT
COLCT COLCT
≫ ≫
'COMB→' STO
===Full-compliant version===
The constant π is here simply used to facilitate the construction of the algebraic expression; it is then eliminated during the conversion into a string.
≪ → scalars
≪ "" 1 scalars SIZE FOR j
'π' scalars j GET "e" j →STR + STR→ * + →STR
OVER SIZE NOT OVER 3 3 SUB "+" AND 4 3 IFTE
OVER SIZE 1 - SUB +
NEXT
IF DUP "" == THEN DROP "0" END
'COMB→' STO

≪ { { 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 } }
{ } 1 3 PICK SIZE FOR j
OVER j GET COMB→ +
NEXT
SWAP DROP
≫ EVAL
{{out}}
<pre>
1: { "e1+2*e2+3*e3" "e2+2*e3+3*e4" "e1+3*e3+4*e4" "e1+2*e2" "0" "0" "e1+e2+e3" "-e1-e2-e3" "-e1-2*e2-3*e4" "-e1" }
</pre>
</pre>