Display a linear combination: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: added whitespace, added a template for the output.)
Line 881: Line 881:
=={{header|REXX}}==
=={{header|REXX}}==
<lang rexx>/*REXX program displays a finite liner combination in an infinite vector basis. */
<lang rexx>/*REXX program displays a finite liner combination in an infinite vector basis. */
@.=.; @.1 = '1, 2, 3'
@.=.; @.1 = ' 1, 2, 3 '
@.2 = '0, 1, 2, 3'
@.2 = ' 0, 1, 2, 3 '
@.3 = '1, 0, 3, 4'
@.3 = ' 1, 0, 3, 4 '
@.4 = '1, 2, 0'
@.4 = ' 1, 2, 0 '
@.5 = '0, 0, 0'
@.5 = ' 0, 0, 0 '
@.6 = 0
@.6 = 0
@.7 = '1, 1, 1'
@.7 = ' 1, 1, 1 '
@.8 = '-1, -1, -1'
@.8 = ' -1, -1, -1 '
@.9 = '-1, -2, 0, -3'
@.9 = ' -1, -2, 0, -3 '
@.10 = -1
@.10 = -1
do j=1 while @.j\==.; n=0 /*process each vector; zero element cnt*/
do j=1 while @.j\==.; n= 0 /*process each vector; zero element cnt*/
y=space( translate(@.j, ,',') ) /*elide commas and superfluous blanks. */
y= space( translate(@.j, ,',') ) /*elide commas and superfluous blanks. */
$= /*nullify output (liner combination).*/
$= /*nullify output (liner combination).*/
do k=1 for words(y); #=word(y, k) /* ◄───── process each of the elements.*/
do k=1 for words(y); #= word(y, k) /* ◄───── process each of the elements.*/
if #=0 then iterate; a=abs(# / 1) /*if the value is zero, then ignore it.*/
if #=0 then iterate; a= abs(# / 1) /*if the value is zero, then ignore it.*/
s='+ '; if #<0 then s='- ' /*define the sign: plus(+) or minus(-)*/
s= '+ ' ; if #<0 then s= "- " /*define the sign: plus(+) or minus(-)*/
n=n+1; if n==1 then s=strip(s) /*if the 1st element used, remove plus.*/
n= n + 1; if n==1 then s= strip(s) /*if the 1st element used, remove plus.*/
if a\==1 then s=s || a'*' /*if multiplier is unity, then ignore #*/
if a\==1 then s= s || a'*' /*if multiplier is unity, then ignore #*/
$=$ s'e('k")" /*construct a liner combination element*/
$= $ s'e('k")" /*construct a liner combination element*/
end /*k*/
end /*k*/


$=strip( strip($), 'L', "+") /*strip leading plus sign (1st element)*/
$= strip( strip($), 'L', "+") /*strip leading plus sign (1st element)*/
if $=='' then $=0 /*handle special case of no elements. */
if $=='' then $= 0 /*handle special case of no elements. */
say right( space(@.j), 20) ' ──► ' strip($) /*align the output for presentation. */
say right( space(@.j), 20) ' ──► ' strip($) /*align the output for presentation. */
end /*j*/ /*stick a fork in it, we're all done. */</lang>
end /*j*/
{{out|output|text=&nbsp; when using the default inputs:}}
/*stick a fork in it, we're all done. */</lang>
'''output''' &nbsp; when using the default inputs:
<pre>
<pre>
1, 2, 3 ──► e(1) + 2*e(2) + 3*e(3)
1, 2, 3 ──► e(1) + 2*e(2) + 3*e(3)