(phixonline)-->
with javascript_semantics
function linear_combination(sequence f)
string res = ""
for e=1 to length(f) do
integer fe = f[e]
if fe!=0 then
if fe=1 then
if length(res) then res &= "+" end if
elsif fe=-1 then
res &= "-"
elsif fe>0 and length(res) then
res &= sprintf("+%d*",fe)
else
res &= sprintf("%d*",fe)
end if
res &= sprintf("e(%d)",e)
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 i=1 to length(tests) do
sequence ti = tests[i]
printf(1,"%12s -> %s\n",{sprint(ti), linear_combination(ti)})
end for