Display a linear combination: Difference between revisions
Content deleted Content added
change task description for better consistency between scalar indices and vector indices : indices begin at one, not zero |
J draft |
||
Line 22:
9) -1, -2, 0, -3
10) -1</pre>
=={{header|J}}==
Implementation:
<lang J>fourbanger=:3 :0
e=. ('e(',')',~])@":&.> 1+i.#y
firstpos=. 0< {.y-.0
if. */0=y do. '0' else. firstpos}.;y gluedto e end.
)
gluedto=:4 :0 each
pfx=. '+-' {~ x<0
select. |x
case. 0 do. ''
case. 1 do. pfx,y
case. do. pfx,(":|x),'*',y
end.
)</lang>
Example use:
<lang J> fourbanger 1 2 3
e(1)+2*e(2)+3*e(3)
fourbanger 0 1 2 3
e(2)+2*e(3)+3*e(4)
fourbanger 1 0 3 4
e(1)+3*e(3)+4*e(4)
fourbanger 0 0 0
0
fourbanger 0
0
fourbanger 1 1 1
e(1)+e(2)+e(3)
fourbanger _1 _1 _1
-e(1)-e(2)-e(3)
fourbanger _1 _2 0 _3
-e(1)-2*e(2)-3*e(4)
fourbanger _1
-e(1)</lang>
=={{header|Perl 6}}==
|