24 lines
706 B
Text
24 lines
706 B
Text
with javascript_semantics
|
|
function linear_combination(sequence f)
|
|
string res = "", fmt
|
|
for i,e in f do
|
|
if e!=0 then
|
|
integer l = length(res)
|
|
if abs(e)=1 then
|
|
res &= iff(e<0?"-":iff(l?"+":""))
|
|
else
|
|
fmt = iff(e>0 and l?"+%d*":"%d*")
|
|
res &= sprintf(fmt,e)
|
|
end if
|
|
res &= sprintf("e(%d)",i)
|
|
end if
|
|
end for
|
|
if res="" then res = "0" end if
|
|
return res
|
|
end function
|
|
|
|
constant tests = {{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}}
|
|
for t in tests do
|
|
printf(1,"%12v -> %s\n",{t, linear_combination(t)})
|
|
end for
|