Display a linear combination: Difference between revisions

From Rosetta Code
Content added Content deleted
No edit summary
m (syntax highlighting fixup automation)
Line 38: Line 38:
=={{header|11l}}==
=={{header|11l}}==
{{trans|Python}}
{{trans|Python}}
<lang 11l>F linear(x)
<syntaxhighlight lang="11l">F linear(x)
V a = enumerate(x).filter2((i, v) -> v != 0).map2((i, v) -> ‘#.e(#.)’.format(I v == -1 {‘-’} E I v == 1 {‘’} E String(v)‘*’, i + 1))
V a = enumerate(x).filter2((i, v) -> v != 0).map2((i, v) -> ‘#.e(#.)’.format(I v == -1 {‘-’} E I v == 1 {‘’} E String(v)‘*’, i + 1))
R (I !a.empty {a} E [String(‘0’)]).join(‘ + ’).replace(‘ + -’, ‘ - ’)
R (I !a.empty {a} E [String(‘0’)]).join(‘ + ’).replace(‘ + -’, ‘ - ’)


L(x) [[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]]
L(x) [[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]]
print(linear(x))</lang>
print(linear(x))</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 59: Line 59:


=={{header|Ada}}==
=={{header|Ada}}==
<lang Ada>with Ada.Text_Io;
<syntaxhighlight lang="ada">with Ada.Text_Io;
with Ada.Strings.Unbounded;
with Ada.Strings.Unbounded;
with Ada.Strings.Fixed;
with Ada.Strings.Fixed;
Line 112: Line 112:
Put_Line (Linear_Combination ((-1, -2, 0, -3)));
Put_Line (Linear_Combination ((-1, -2, 0, -3)));
Put_Line (Linear_Combination ((1 => -1)));
Put_Line (Linear_Combination ((1 => -1)));
end Display_Linear;</lang>
end Display_Linear;</syntaxhighlight>
{{out}}
{{out}}
<pre>e(1) + 2*e(2) + 3*e(3)
<pre>e(1) + 2*e(2) + 3*e(3)
Line 127: Line 127:
=={{header|C}}==
=={{header|C}}==
Accepts vector coefficients from the command line, prints usage syntax if invoked with no arguments. This implementation can handle floating point values but displays integer values as integers. All test case results shown with invocation. A multiplication sign is not shown between a coefficient and the unit vector when a vector is written out by hand ( i.e. human readable) and is thus not shown here as well.
Accepts vector coefficients from the command line, prints usage syntax if invoked with no arguments. This implementation can handle floating point values but displays integer values as integers. All test case results shown with invocation. A multiplication sign is not shown between a coefficient and the unit vector when a vector is written out by hand ( i.e. human readable) and is thus not shown here as well.
<syntaxhighlight lang="c">
<lang C>


#include<stdlib.h>
#include<stdlib.h>
Line 194: Line 194:
return 0;
return 0;
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 223: Line 223:
=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==
{{trans|D}}
{{trans|D}}
<lang csharp>using System;
<syntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Text;
using System.Text;
Line 279: Line 279:
}
}
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre> [1, 2, 3] -> e(1) + 2*e(2) + 3*e(3)
<pre> [1, 2, 3] -> e(1) + 2*e(2) + 3*e(3)
Line 294: Line 294:
=={{header|C++}}==
=={{header|C++}}==
{{trans|D}}
{{trans|D}}
<lang cpp>#include <iomanip>
<syntaxhighlight lang="cpp">#include <iomanip>
#include <iostream>
#include <iostream>
#include <sstream>
#include <sstream>
Line 374: Line 374:


return 0;
return 0;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre> [1, 2, 3] -> e(1) + 2*e(2) + 3*e(3)
<pre> [1, 2, 3] -> e(1) + 2*e(2) + 3*e(3)
Line 389: Line 389:
=={{header|D}}==
=={{header|D}}==
{{trans|Kotlin}}
{{trans|Kotlin}}
<lang D>import std.array;
<syntaxhighlight lang="d">import std.array;
import std.conv;
import std.conv;
import std.format;
import std.format;
Line 441: Line 441:
writefln("%-15s -> %s", arr, linearCombo(c));
writefln("%-15s -> %s", arr, linearCombo(c));
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>[1, 2, 3] -> e(1) + 2*e(2) + 3*e(3)
<pre>[1, 2, 3] -> e(1) + 2*e(2) + 3*e(3)
Line 455: Line 455:


=={{header|EchoLisp}}==
=={{header|EchoLisp}}==
<lang scheme>
<syntaxhighlight lang="scheme">
;; build an html string from list of coeffs
;; build an html string from list of coeffs


Line 488: Line 488:
(for/string ((linear linears))
(for/string ((linear linears))
(format "%a -> <span style='color:blue'>%a</span> <br>" linear (linear->html linear)))))
(format "%a -> <span style='color:blue'>%a</span> <br>" linear (linear->html linear)))))
</syntaxhighlight>
</lang>
{{out}}
{{out}}
(1 2 3) -> <span style='color:blue'> e<sub>1</sub> + 2*e<sub>2</sub> + 3*e<sub>3</sub> </span> <br>(0 1 2 3) -> <span style='color:blue'> e<sub>2</sub> + 2*e<sub>3</sub> + 3*e<sub>4</sub> </span> <br>(1 0 3 4) -> <span style='color:blue'> e<sub>1</sub> + 3*e<sub>3</sub> + 4*e<sub>4</sub> </span> <br>(1 2 0) -> <span style='color:blue'> e<sub>1</sub> + 2*e<sub>2</sub> </span> <br>(0 0 0) -> <span style='color:blue'>0</span> <br>(0) -> <span style='color:blue'>0</span> <br>(1 1 1) -> <span style='color:blue'> e<sub>1</sub> + e<sub>2</sub> + e<sub>3</sub> </span> <br>(-1 -1 -1) -> <span style='color:blue'>- e<sub>1</sub> - e<sub>2</sub> - e<sub>3</sub> </span> <br>(-1 -2 0 -3) -> <span style='color:blue'>- e<sub>1</sub> - 2*e<sub>2</sub> - 3*e<sub>4</sub> </span> <br>(-1) -> <span style='color:blue'>- e<sub>1</sub> </span> <br>
(1 2 3) -> <span style='color:blue'> e<sub>1</sub> + 2*e<sub>2</sub> + 3*e<sub>3</sub> </span> <br>(0 1 2 3) -> <span style='color:blue'> e<sub>2</sub> + 2*e<sub>3</sub> + 3*e<sub>4</sub> </span> <br>(1 0 3 4) -> <span style='color:blue'> e<sub>1</sub> + 3*e<sub>3</sub> + 4*e<sub>4</sub> </span> <br>(1 2 0) -> <span style='color:blue'> e<sub>1</sub> + 2*e<sub>2</sub> </span> <br>(0 0 0) -> <span style='color:blue'>0</span> <br>(0) -> <span style='color:blue'>0</span> <br>(1 1 1) -> <span style='color:blue'> e<sub>1</sub> + e<sub>2</sub> + e<sub>3</sub> </span> <br>(-1 -1 -1) -> <span style='color:blue'>- e<sub>1</sub> - e<sub>2</sub> - e<sub>3</sub> </span> <br>(-1 -2 0 -3) -> <span style='color:blue'>- e<sub>1</sub> - 2*e<sub>2</sub> - 3*e<sub>4</sub> </span> <br>(-1) -> <span style='color:blue'>- e<sub>1</sub> </span> <br>
Line 494: Line 494:
=={{header|Elixir}}==
=={{header|Elixir}}==
{{works with|Elixir|1.3}}
{{works with|Elixir|1.3}}
<lang elixir>defmodule Linear_combination do
<syntaxhighlight lang="elixir">defmodule Linear_combination do
def display(coeff) do
def display(coeff) do
Enum.with_index(coeff)
Enum.with_index(coeff)
Line 525: Line 525:
[-1]
[-1]
]
]
Enum.each(coeffs, &Linear_combination.display(&1))</lang>
Enum.each(coeffs, &Linear_combination.display(&1))</syntaxhighlight>


{{out}}
{{out}}
Line 543: Line 543:
=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
===The function===
===The function===
<lang fsharp>
<syntaxhighlight lang="fsharp">
// Display a linear combination. Nigel Galloway: March 28th., 2018
// Display a linear combination. Nigel Galloway: March 28th., 2018
let fN g =
let fN g =
Line 559: Line 559:
|_ -> printfn "0"
|_ -> printfn "0"
fN 1 g
fN 1 g
</syntaxhighlight>
</lang>


===The Task===
===The Task===
<lang fsharp>
<syntaxhighlight lang="fsharp">
fN [1;2;3]
fN [1;2;3]
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
e(1)+2e(2)+3e(3)
e(1)+2e(2)+3e(3)
</pre>
</pre>
<lang fsharp>
<syntaxhighlight lang="fsharp">
fN [0;1;2;3]
fN [0;1;2;3]
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
e(2)+2e(3)+3e(4)
e(2)+2e(3)+3e(4)
</pre>
</pre>
<lang fsharp>
<syntaxhighlight lang="fsharp">
fN[1;0;3;4]
fN[1;0;3;4]
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
e(1)+3e(3)+4e(4)
e(1)+3e(3)+4e(4)
</pre>
</pre>
<lang fsharp>
<syntaxhighlight lang="fsharp">
fN[1;2;0]
fN[1;2;0]
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
e(1)+2e(2)
e(1)+2e(2)
</pre>
</pre>
<lang fsharp>
<syntaxhighlight lang="fsharp">
fN[0;0;0]
fN[0;0;0]
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
0
0
</pre>
</pre>
<lang fsharp>
<syntaxhighlight lang="fsharp">
fN[0]
fN[0]
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
0
0
</pre>
</pre>
<lang fsharp>
<syntaxhighlight lang="fsharp">
fN[1;1;1]
fN[1;1;1]
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
e(1)+e(2)+e(3)
e(1)+e(2)+e(3)
</pre>
</pre>
<lang fsharp>
<syntaxhighlight lang="fsharp">
fN[-1;-1;-1]
fN[-1;-1;-1]
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
-e(1)-e(2)-e(3)
-e(1)-e(2)-e(3)
</pre>
</pre>
<lang fsharp>
<syntaxhighlight lang="fsharp">
fN[-1;-2;0;-3]
fN[-1;-2;0;-3]
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
-e(1)-2e(2)-3e(4)
-e(1)-2e(2)-3e(4)
</pre>
</pre>
<lang fsharp>
<syntaxhighlight lang="fsharp">
fN[1]
fN[1]
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 634: Line 634:


=={{header|Factor}}==
=={{header|Factor}}==
<lang factor>USING: formatting kernel match math pair-rocket regexp sequences ;
<syntaxhighlight lang="factor">USING: formatting kernel match math pair-rocket regexp sequences ;


MATCH-VARS: ?a ?b ;
MATCH-VARS: ?a ?b ;
Line 652: Line 652:
{ { 1 2 3 } { 0 1 2 3 } { 1 0 3 4 } { 1 2 0 } { 0 0 0 } { 0 }
{ { 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 1 1 } { -1 -1 -1 } { -1 -2 0 -3 } { -1 } }
[ dup linear-combo "%-14u -> %s\n" printf ] each</lang>
[ dup linear-combo "%-14u -> %s\n" printf ] each</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 670: Line 670:
=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
{{trans|Ring}}
{{trans|Ring}}
<lang freebasic>Dim scalars(1 To 10, 1 To 4) As Integer => {{1, 2, 3}, {0, 1, 2, 3}, _
<syntaxhighlight lang="freebasic">Dim scalars(1 To 10, 1 To 4) As Integer => {{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, 0, 3, 4}, {1, 2, 0}, {0, 0, 0}, {0}, {1, 1, 1}, {-1, -1, -1}, _
{-1, -2, 0, -3}, {-1}}
{-1, -2, 0, -3}, {-1}}
Line 697: Line 697:
Print cadena
Print cadena
Next n
Next n
Sleep</lang>
Sleep</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 705: Line 705:
=={{header|Go}}==
=={{header|Go}}==
{{trans|Kotlin}}
{{trans|Kotlin}}
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 763: Line 763:
fmt.Printf("%-15s -> %s\n", t, linearCombo(c))
fmt.Printf("%-15s -> %s\n", t, linearCombo(c))
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 781: Line 781:
=={{header|Groovy}}==
=={{header|Groovy}}==
{{trans|Java}}
{{trans|Java}}
<lang groovy>class LinearCombination {
<syntaxhighlight lang="groovy">class LinearCombination {
private static String linearCombo(int[] c) {
private static String linearCombo(int[] c) {
StringBuilder sb = new StringBuilder()
StringBuilder sb = new StringBuilder()
Line 824: Line 824:
}
}
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>[1, 2, 3] -> e(1) + 2*e(2) + 3*e(3)
<pre>[1, 2, 3] -> e(1) + 2*e(2) + 3*e(3)
Line 838: Line 838:


=={{header|Haskell}}==
=={{header|Haskell}}==
<lang haskell>import Text.Printf (printf)
<syntaxhighlight lang="haskell">import Text.Printf (printf)


linearForm :: [Int] -> String
linearForm :: [Int] -> String
Line 853: Line 853:
'+':s -> s
'+':s -> s
"" -> "0"
"" -> "0"
s -> s</lang>
s -> s</syntaxhighlight>


Testing
Testing


<lang haskell>coeffs :: [[Int]]
<syntaxhighlight lang="haskell">coeffs :: [[Int]]
coeffs = [ [1, 2, 3]
coeffs = [ [1, 2, 3]
, [0, 1, 2, 3]
, [0, 1, 2, 3]
Line 867: Line 867:
, [-1, -1, -1]
, [-1, -1, -1]
, [-1, -2, 0, -3]
, [-1, -2, 0, -3]
, [-1] ]</lang>
, [-1] ]</syntaxhighlight>


<pre>λ> mapM_ (print . linearForm) coeffs
<pre>λ> mapM_ (print . linearForm) coeffs
Line 885: Line 885:
Implementation:
Implementation:


<lang J>fourbanger=:3 :0
<syntaxhighlight lang="j">fourbanger=:3 :0
e=. ('e(',')',~])@":&.> 1+i.#y
e=. ('e(',')',~])@":&.> 1+i.#y
firstpos=. 0< {.y-.0
firstpos=. 0< {.y-.0
Line 898: Line 898:
case. do. pfx,(":|x),'*',y
case. do. pfx,(":|x),'*',y
end.
end.
)</lang>
)</syntaxhighlight>


Example use:
Example use:


<lang J> fourbanger 1 2 3
<syntaxhighlight lang="j"> fourbanger 1 2 3
e(1)+2*e(2)+3*e(3)
e(1)+2*e(2)+3*e(3)
fourbanger 0 1 2 3
fourbanger 0 1 2 3
Line 919: Line 919:
-e(1)-2*e(2)-3*e(4)
-e(1)-2*e(2)-3*e(4)
fourbanger _1
fourbanger _1
-e(1)</lang>
-e(1)</syntaxhighlight>


=={{header|Java}}==
=={{header|Java}}==
{{trans|Kotlin}}
{{trans|Kotlin}}
<lang Java>import java.util.Arrays;
<syntaxhighlight lang="java">import java.util.Arrays;


public class LinearCombination {
public class LinearCombination {
Line 967: Line 967:
}
}
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>[1, 2, 3] -> e(1) + 2*e(2) + 3*e(3)
<pre>[1, 2, 3] -> e(1) + 2*e(2) + 3*e(3)
Line 981: Line 981:


=={{header|jq}}==
=={{header|jq}}==
<lang jq>def linearCombo:
<syntaxhighlight lang="jq">def linearCombo:
reduce to_entries[] as {key: $k,value: $v} ("";
reduce to_entries[] as {key: $k,value: $v} ("";
if $v == 0 then .
if $v == 0 then .
Line 1,010: Line 1,010:
[-1]
[-1]
| "\(lpad(15)) => \(linearCombo)"
| "\(lpad(15)) => \(linearCombo)"
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<lang sh> [1,2,3] => e0 + 2*e1 + 3*e2
<syntaxhighlight lang="sh"> [1,2,3] => e0 + 2*e1 + 3*e2
[0,1,2,3] => e1 + 2*e2 + 3*e3
[0,1,2,3] => e1 + 2*e2 + 3*e3
[1,0,3,4] => e0 + 3*e2 + 4*e3
[1,0,3,4] => e0 + 3*e2 + 4*e3
Line 1,021: Line 1,021:
[-1,-1,-1] => -e0 - e1 - e2
[-1,-1,-1] => -e0 - e1 - e2
[-1,-2,0,-3] => -e0 - 2*e1 - 3*e3
[-1,-2,0,-3] => -e0 - 2*e1 - 3*e3
[-1] => -e0</lang>
[-1] => -e0</syntaxhighlight>
=={{header|Julia}}==
=={{header|Julia}}==
<lang julia># v0.6
<syntaxhighlight lang="julia"># v0.6


linearcombination(coef::Array) = join(collect("$c * e($i)" for (i, c) in enumerate(coef) if c != 0), " + ")
linearcombination(coef::Array) = join(collect("$c * e($i)" for (i, c) in enumerate(coef) if c != 0), " + ")
Line 1,030: Line 1,030:
[-1, -1, -1], [-1, -2, 0, -3], [-1]]
[-1, -1, -1], [-1, -2, 0, -3], [-1]]
@printf("%20s -> %s\n", c, linearcombination(c))
@printf("%20s -> %s\n", c, linearcombination(c))
end</lang>
end</syntaxhighlight>


{{out}}
{{out}}
Line 1,045: Line 1,045:


=={{header|Kotlin}}==
=={{header|Kotlin}}==
<lang scala>// version 1.1.2
<syntaxhighlight lang="scala">// version 1.1.2


fun linearCombo(c: IntArray): String {
fun linearCombo(c: IntArray): String {
Line 1,080: Line 1,080:
println("${c.contentToString().padEnd(15)} -> ${linearCombo(c)}")
println("${c.contentToString().padEnd(15)} -> ${linearCombo(c)}")
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,097: Line 1,097:


=={{header|Lambdatalk}}==
=={{header|Lambdatalk}}==
<lang scheme>
<syntaxhighlight lang="scheme">


{def linearcomb
{def linearcomb
Line 1,127: Line 1,127:
{linearcomb -1} -> - e(1)
{linearcomb -1} -> - e(1)


</syntaxhighlight>
</lang>


=={{header|Lua}}==
=={{header|Lua}}==
{{trans|C#}}
{{trans|C#}}
<lang lua>function t2s(t)
<syntaxhighlight lang="lua">function t2s(t)
local s = "["
local s = "["
for i,v in pairs(t) do
for i,v in pairs(t) do
Line 1,196: Line 1,196:
end
end


main()</lang>
main()</syntaxhighlight>
{{out}}
{{out}}
<pre> [1, 2, 3] -> e(1) + 2*e(2) + 3*e(3)
<pre> [1, 2, 3] -> e(1) + 2*e(2) + 3*e(3)
Line 1,211: Line 1,211:


=={{header|Mathematica}} / {{header|Wolfram Language}}==
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<lang Mathematica>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}};
<syntaxhighlight lang="mathematica">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}};
Column[TraditionalForm[Total[MapIndexed[#1 e[#2[[1]]] &, #]]] & /@ tests]</lang>
Column[TraditionalForm[Total[MapIndexed[#1 e[#2[[1]]] &, #]]] & /@ tests]</syntaxhighlight>
{{out}}
{{out}}
<pre>e(1)+2e(2)+3e(3)
<pre>e(1)+2e(2)+3e(3)
Line 1,226: Line 1,226:


=={{header|Modula-2}}==
=={{header|Modula-2}}==
<lang modula2>MODULE Linear;
<syntaxhighlight lang="modula2">MODULE Linear;
FROM FormatString IMPORT FormatString;
FROM FormatString IMPORT FormatString;
FROM Terminal IMPORT WriteString,WriteLn,ReadChar;
FROM Terminal IMPORT WriteString,WriteLn,ReadChar;
Line 1,292: Line 1,292:


ReadChar
ReadChar
END Linear.</lang>
END Linear.</syntaxhighlight>


=={{header|Nim}}==
=={{header|Nim}}==
{{trans|Kotlin}}
{{trans|Kotlin}}
<lang Nim>import strformat
<syntaxhighlight lang="nim">import strformat


proc linearCombo(c: openArray[int]): string =
proc linearCombo(c: openArray[int]): string =
Line 1,324: Line 1,324:


for c in Combos:
for c in Combos:
echo fmt"{($c)[1..^1]:15} → {linearCombo(c)}"</lang>
echo fmt"{($c)[1..^1]:15} → {linearCombo(c)}"</syntaxhighlight>


{{out}}
{{out}}
Line 1,339: Line 1,339:


=={{header|Perl}}==
=={{header|Perl}}==
<lang perl>use strict;
<syntaxhighlight lang="perl">use strict;
use warnings;
use warnings;
use feature 'say';
use feature 'say';
Line 1,354: Line 1,354:


say linear_combination($_) for
say linear_combination($_) for
[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 ]</lang>
[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 ]</syntaxhighlight>
{{out}}
{{out}}
<pre>e(1) + 2*e(2) + 3*e(3)
<pre>e(1) + 2*e(2) + 3*e(3)
Line 1,369: Line 1,369:
=={{header|Phix}}==
=={{header|Phix}}==
{{trans|Tcl}}
{{trans|Tcl}}
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">linear_combination</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">f</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">linear_combination</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">f</span><span style="color: #0000FF;">)</span>
Line 1,406: Line 1,406:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%12s -&gt; %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">sprint</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ti</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">linear_combination</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ti</span><span style="color: #0000FF;">)})</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%12s -&gt; %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">sprint</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ti</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">linear_combination</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ti</span><span style="color: #0000FF;">)})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 1,422: Line 1,422:


=={{header|PureBasic}}==
=={{header|PureBasic}}==
<lang PureBasic>; Process and output values.
<syntaxhighlight lang="purebasic">; Process and output values.
Procedure WriteLinear(Array c.i(1))
Procedure WriteLinear(Array c.i(1))
Define buf$,
Define buf$,
Line 1,527: Line 1,527:
Data.i -1
Data.i -1
_V10:
_V10:
EndDataSection</lang>
EndDataSection</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,543: Line 1,543:


=={{header|Python}}==
=={{header|Python}}==
<lang python>
<syntaxhighlight lang="python">
def linear(x):
def linear(x):
return ' + '.join(['{}e({})'.format('-' if v == -1 else '' if v == 1 else str(v) + '*', i + 1)
return ' + '.join(['{}e({})'.format('-' if v == -1 else '' if v == 1 else str(v) + '*', i + 1)
Line 1,550: Line 1,550:
list(map(lambda x: print(linear(x)), [[1, 2, 3], [0, 1, 2, 3], [1, 0, 3, 4], [1, 2, 0],
list(map(lambda x: print(linear(x)), [[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]]))
[0, 0, 0], [0], [1, 1, 1], [-1, -1, -1], [-1, -2, 0, 3], [-1]]))
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 1,566: Line 1,566:


=={{header|Racket}}==
=={{header|Racket}}==
<lang racket>#lang racket/base
<syntaxhighlight lang="racket">#lang racket/base
(require racket/match racket/string)
(require racket/match racket/string)


Line 1,599: Line 1,599:
(-1 -2 0 -3)
(-1 -2 0 -3)
(-1)))
(-1)))
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 1,615: Line 1,615:
=={{header|Raku}}==
=={{header|Raku}}==
(formerly Perl 6)
(formerly Perl 6)
<lang perl6>sub linear-combination(@coeff) {
<syntaxhighlight lang="raku" line>sub linear-combination(@coeff) {
(@coeff Z=> map { "e($_)" }, 1 .. *)
(@coeff Z=> map { "e($_)" }, 1 .. *)
.grep(+*.key)
.grep(+*.key)
Line 1,636: Line 1,636:
[-1, -2, 0, -3],
[-1, -2, 0, -3],
[-1 ]
[-1 ]
;</lang>
;</syntaxhighlight>
{{out}}
{{out}}
<pre>e(1) + 2*e(2) + 3*e(3)
<pre>e(1) + 2*e(2) + 3*e(3)
Line 1,650: Line 1,650:


=={{header|REXX}}==
=={{header|REXX}}==
<lang rexx>/*REXX program displays a finite liner combination in an infinite vector basis. */
<syntaxhighlight lang="rexx">/*REXX program displays a finite liner combination in an infinite vector basis. */
@.= .; @.1 = ' 1, 2, 3 ' /*define a specific test case for build*/
@.= .; @.1 = ' 1, 2, 3 ' /*define a specific test case for build*/
@.2 = ' 0, 1, 2, 3 ' /* " " " " " " " */
@.2 = ' 0, 1, 2, 3 ' /* " " " " " " " */
Line 1,676: Line 1,676:
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*/ /*stick a fork in it, we're all done. */</syntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
<pre>
Line 1,692: Line 1,692:


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang="ring">
# Project : Display a linear combination
# Project : Display a linear combination


Line 1,722: Line 1,722:
see str + nl
see str + nl
next
next
</syntaxhighlight>
</lang>
Output:
Output:
<pre>
<pre>
Line 1,739: Line 1,739:
=={{header|Ruby}}==
=={{header|Ruby}}==
{{trans|D}}
{{trans|D}}
<lang ruby>def linearCombo(c)
<syntaxhighlight lang="ruby">def linearCombo(c)
sb = ""
sb = ""
c.each_with_index { |n, i|
c.each_with_index { |n, i|
Line 1,793: Line 1,793:
end
end


main()</lang>
main()</syntaxhighlight>
{{out}}
{{out}}
<pre>[1, 2, 3] -> e(1) + 2*e(2) + 3*e(3)
<pre>[1, 2, 3] -> e(1) + 2*e(2) + 3*e(3)
Line 1,807: Line 1,807:


=={{header|Rust}}==
=={{header|Rust}}==
<lang rust>
<syntaxhighlight lang="rust">
use std::fmt::{Display, Formatter, Result};
use std::fmt::{Display, Formatter, Result};
use std::process::exit;
use std::process::exit;
Line 1,899: Line 1,899:
println!("{}", linear_combination(&coefficients));
println!("{}", linear_combination(&coefficients));
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 1,906: Line 1,906:


=={{header|Scala}}==
=={{header|Scala}}==
<lang Scala>object LinearCombination extends App {
<syntaxhighlight lang="scala">object LinearCombination extends App {
val combos = Seq(Seq(1, 2, 3), Seq(0, 1, 2, 3),
val combos = Seq(Seq(1, 2, 3), Seq(0, 1, 2, 3),
Seq(1, 0, 3, 4), Seq(1, 2, 0), Seq(0, 0, 0), Seq(0),
Seq(1, 0, 3, 4), Seq(1, 2, 0), Seq(0, 0, 0), Seq(0),
Line 1,928: Line 1,928:
println(f"${c.mkString("[", ", ", "]")}%-15s -> ${linearCombo(c)}%s")
println(f"${c.mkString("[", ", ", "]")}%-15s -> ${linearCombo(c)}%s")
}
}
}</lang>
}</syntaxhighlight>


=={{header|Sidef}}==
=={{header|Sidef}}==
{{trans|Tcl}}
{{trans|Tcl}}
<lang ruby>func linear_combination(coeffs) {
<syntaxhighlight lang="ruby">func linear_combination(coeffs) {
var res = ""
var res = ""
for e,f in (coeffs.kv) {
for e,f in (coeffs.kv) {
Line 1,969: Line 1,969:
tests.each { |t|
tests.each { |t|
printf("%10s -> %-10s\n", t.join(' '), linear_combination(t))
printf("%10s -> %-10s\n", t.join(' '), linear_combination(t))
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre> 1 2 3 -> e(1)+2*e(2)+3*e(3)
<pre> 1 2 3 -> e(1)+2*e(2)+3*e(3)
Line 1,985: Line 1,985:
This solution strives for legibility rather than golf.
This solution strives for legibility rather than golf.


<lang Tcl>proc lincom {factors} {
<syntaxhighlight lang="tcl">proc lincom {factors} {
set exp 0
set exp 0
set res ""
set res ""
Line 2,020: Line 2,020:
} {
} {
puts [format "%10s -> %-10s" $test [lincom $test]]
puts [format "%10s -> %-10s" $test [lincom $test]]
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 2,036: Line 2,036:
=={{header|Visual Basic .NET}}==
=={{header|Visual Basic .NET}}==
{{trans|C#}}
{{trans|C#}}
<lang vbnet>Imports System.Text
<syntaxhighlight lang="vbnet">Imports System.Text


Module Module1
Module Module1
Line 2,090: Line 2,090:
End Sub
End Sub


End Module</lang>
End Module</syntaxhighlight>
{{out}}
{{out}}
<pre> [1, 2, 3] -> e(1) + 2*e(2) + 3*e(3)
<pre> [1, 2, 3] -> e(1) + 2*e(2) + 3*e(3)
Line 2,105: Line 2,105:
=={{header|Vlang}}==
=={{header|Vlang}}==
{{trans|Go}}
{{trans|Go}}
<lang vlang>import strings
<syntaxhighlight lang="vlang">import strings


fn linear_combo(c []int) string {
fn linear_combo(c []int) string {
Line 2,161: Line 2,161:
println("${c:-15} -> ${linear_combo(c)}")
println("${c:-15} -> ${linear_combo(c)}")
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 2,180: Line 2,180:
{{trans|Kotlin}}
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
{{libheader|Wren-fmt}}
<lang ecmascript>import "/fmt" for Fmt
<syntaxhighlight lang="ecmascript">import "/fmt" for Fmt


var linearCombo = Fn.new { |c|
var linearCombo = Fn.new { |c|
Line 2,213: Line 2,213:
for (c in combos) {
for (c in combos) {
Fmt.print("$-15s -> $s", c.toString, linearCombo.call(c))
Fmt.print("$-15s -> $s", c.toString, linearCombo.call(c))
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 2,231: Line 2,231:
=={{header|zkl}}==
=={{header|zkl}}==
{{trans|Raku}}
{{trans|Raku}}
<lang zkl>fcn linearCombination(coeffs){
<syntaxhighlight lang="zkl">fcn linearCombination(coeffs){
[1..].zipWith(fcn(n,c){ if(c==0) "" else "%s*e(%s)".fmt(c,n) },coeffs)
[1..].zipWith(fcn(n,c){ if(c==0) "" else "%s*e(%s)".fmt(c,n) },coeffs)
.filter().concat("+").replace("+-","-").replace("1*","")
.filter().concat("+").replace("+-","-").replace("1*","")
or 0
or 0
}</lang>
}</syntaxhighlight>
<lang zkl>T(T(1,2,3),T(0,1,2,3),T(1,0,3,4),T(1,2,0),T(0,0,0),T(0),T(1,1,1),T(-1,-1,-1),
<syntaxhighlight lang="zkl">T(T(1,2,3),T(0,1,2,3),T(1,0,3,4),T(1,2,0),T(0,0,0),T(0),T(1,1,1),T(-1,-1,-1),
T(-1,-2,0,-3),T(-1),T)
T(-1,-2,0,-3),T(-1),T)
.pump(Console.println,linearCombination);</lang>
.pump(Console.println,linearCombination);</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>

Revision as of 23:59, 26 August 2022

Task
Display a linear combination
You are encouraged to solve this task according to the task description, using any language you may know.
Task

Display a finite linear combination in an infinite vector basis .

Write a function that, when given a finite list of scalars ,
creates a string representing the linear combination in an explicit format often used in mathematics, that is:

where



The output must comply to the following rules:

  •   don't show null terms, unless the whole combination is null.
e(1)     is fine,     e(1) + 0*e(3)     or     e(1) + 0     is wrong.
  •   don't show scalars when they are equal to one or minus one.
e(3)     is fine,     1*e(3)     is wrong.
  •   don't prefix by a minus sign if it follows a preceding term.   Instead you use subtraction.
e(4) - e(5)     is fine,     e(4) + -e(5)     is wrong.


Show here output for the following lists of scalars:

 1)    1,  2,  3
 2)    0,  1,  2,  3
 3)    1,  0,  3,  4
 4)    1,  2,  0
 5)    0,  0,  0
 6)    0
 7)    1,  1,  1
 8)   -1, -1, -1
 9)   -1, -2,  0, -3
10)   -1



11l

Translation of: Python
F linear(x)
   V a = enumerate(x).filter2((i, v) -> v != 0).map2((i, v) -> ‘#.e(#.)’.format(I v == -1 {‘-’} E I v == 1 {‘’} E String(v)‘*’, i + 1))
   R (I !a.empty {a} E [String(‘0’)]).join(‘ + ’).replace(‘ + -’, ‘ - ’)

L(x) [[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]]
   print(linear(x))
Output:
e(1) + 2*e(2) + 3*e(3)
e(2) + 2*e(3) + 3*e(4)
e(1) + 3*e(3) + 4*e(4)
e(1) + 2*e(2)
0
0
e(1) + e(2) + e(3)
-e(1) - e(2) - e(3)
-e(1) - 2*e(2) + 3*e(4)
-e(1)

Ada

with Ada.Text_Io;
with Ada.Strings.Unbounded;
with Ada.Strings.Fixed;

procedure Display_Linear is

   subtype Position is Positive;
   type Coefficient is new Integer;
   type Combination is array (Position range <>) of Coefficient;

   function Linear_Combination (Comb : Combination) return String is
      use Ada.Strings.Unbounded;
      use Ada.Strings;
      Accu : Unbounded_String;
   begin
      for Pos in Comb'Range loop
         case Comb (Pos) is
            when Coefficient'First .. -1 =>
               Append (Accu, (if Accu = "" then "-" else " - "));
            when 0 => null;
            when 1 .. Coefficient'Last =>
               Append (Accu, (if Accu /= "" then " + " else ""));
         end case;

         if Comb (Pos) /= 0 then
            declare
               Abs_Coeff   : constant Coefficient := abs Comb (Pos);
               Coeff_Image : constant String := Fixed.Trim (Coefficient'Image (Abs_Coeff), Left);
               Exp_Image   : constant String := Fixed.Trim (Position'Image (Pos), Left);
            begin
               if Abs_Coeff /= 1 then
                  Append (Accu, Coeff_Image & "*");
               end if;
               Append (Accu, "e(" & Exp_Image & ")");
            end;
         end if;
      end loop;

      return (if Accu = "" then "0" else To_String (Accu));
   end Linear_Combination;

   use Ada.Text_Io;
begin
   Put_Line (Linear_Combination ((1, 2, 3)));
   Put_Line (Linear_Combination ((0, 1, 2, 3)));
   Put_Line (Linear_Combination ((1, 0, 3, 4)));
   Put_Line (Linear_Combination ((1, 2, 0)));
   Put_Line (Linear_Combination ((0, 0, 0)));
   Put_Line (Linear_Combination ((1 => 0)));
   Put_Line (Linear_Combination ((1, 1, 1)));
   Put_Line (Linear_Combination ((-1, -1, -1)));
   Put_Line (Linear_Combination ((-1, -2, 0, -3)));
   Put_Line (Linear_Combination ((1 => -1)));
end Display_Linear;
Output:
e(1) + 2*e(2) + 3*e(3)
e(2) + 2*e(3) + 3*e(4)
e(1) + 3*e(3) + 4*e(4)
e(1) + 2*e(2)
0
0
e(1) + e(2) + e(3)
-e(1) - e(2) - e(3)
-e(1) - 2*e(2) - 3*e(4)
-e(1)

C

Accepts vector coefficients from the command line, prints usage syntax if invoked with no arguments. This implementation can handle floating point values but displays integer values as integers. All test case results shown with invocation. A multiplication sign is not shown between a coefficient and the unit vector when a vector is written out by hand ( i.e. human readable) and is thus not shown here as well.

#include<stdlib.h>
#include<stdio.h>
#include<math.h> /*Optional, but better if included as fabs, labs and abs functions are being used. */

int main(int argC, char* argV[])
{
	
	int i,zeroCount= 0,firstNonZero = -1;
	double* vector;
	
	if(argC == 1){
		printf("Usage : %s <Vector component coefficients seperated by single space>",argV[0]);
	}
	
	else{
		
		printf("Vector for [");
		for(i=1;i<argC;i++){
			printf("%s,",argV[i]);
		}
		printf("\b] -> ");
		
		
		vector = (double*)malloc((argC-1)*sizeof(double));
		
		for(i=1;i<=argC;i++){
			vector[i-1] = atof(argV[i]);
			if(vector[i-1]==0.0)
				zeroCount++;
			if(vector[i-1]!=0.0 && firstNonZero==-1)
				firstNonZero = i-1;
		}

		if(zeroCount == argC){
			printf("0");
		}
		
		else{
			for(i=0;i<argC;i++){
				if(i==firstNonZero && vector[i]==1)
					printf("e%d ",i+1);
				else if(i==firstNonZero && vector[i]==-1)
					printf("- e%d ",i+1);
				else if(i==firstNonZero && vector[i]<0 && fabs(vector[i])-abs(vector[i])>0.0)
					printf("- %lf e%d ",fabs(vector[i]),i+1);
				else if(i==firstNonZero && vector[i]<0 && fabs(vector[i])-abs(vector[i])==0.0)
					printf("- %ld e%d ",labs(vector[i]),i+1);
				else if(i==firstNonZero && vector[i]>0 && fabs(vector[i])-abs(vector[i])>0.0)
					printf("%lf e%d ",vector[i],i+1);
				else if(i==firstNonZero && vector[i]>0 && fabs(vector[i])-abs(vector[i])==0.0)
					printf("%ld e%d ",vector[i],i+1);
				else if(fabs(vector[i])==1.0 && i!=0)
					printf("%c e%d ",(vector[i]==-1)?'-':'+',i+1);
				else if(i!=0 && vector[i]!=0 && fabs(vector[i])-abs(vector[i])>0.0)
					printf("%c %lf e%d ",(vector[i]<0)?'-':'+',fabs(vector[i]),i+1);
				else if(i!=0 && vector[i]!=0 && fabs(vector[i])-abs(vector[i])==0.0)
					printf("%c %ld e%d ",(vector[i]<0)?'-':'+',labs(vector[i]),i+1);				
			}
		}
	}
	
	free(vector);
	
	return 0;
}
Output:
C:\rossetaCode>vectorDisplay.exe 1 2 3
Vector for [1,2,3] -> e1 + 2 e2 + 3 e3
C:\rossetaCode>vectorDisplay.exe 0 0 0
Vector for [0,0,0] -> 0
C:\rossetaCode>vectorDisplay.exe 0 1 2 3
Vector for [0,1,2,3] -> e2 + 2 e3 + 3 e4
C:\rossetaCode>vectorDisplay.exe 1 0 3 4
Vector for [1,0,3,4] -> e1 + 3 e3 + 4 e4
C:\rossetaCode>vectorDisplay.exe 1 2 0
Vector for [1,2,0] -> e1 + 2 e2
C:\rossetaCode>vectorDisplay.exe 0 0 0
Vector for [0,0,0] -> 0
C:\rossetaCode>vectorDisplay.exe 0
Vector for [0] -> 0
C:\rossetaCode>vectorDisplay.exe 1 1 1
Vector for [1,1,1] -> e1 + e2 + e3
C:\rossetaCode>vectorDisplay.exe -1 -1 -1
Vector for [-1,-1,-1] -> - e1 - e2 - e3
C:\rossetaCode>vectorDisplay.exe -1 -2 0 -3
Vector for [-1,-2,0,-3] -> - e1 - 2 e2 - 3 e4
C:\rossetaCode>vectorDisplay.exe -1
Vector for [-1] -> - e1

C#

Translation of: D
using System;
using System.Collections.Generic;
using System.Text;

namespace DisplayLinearCombination {
    class Program {
        static string LinearCombo(List<int> c) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < c.Count; i++) {
                int n = c[i];
                if (n < 0) {
                    if (sb.Length == 0) {
                        sb.Append('-');
                    } else {
                        sb.Append(" - ");
                    }
                } else if (n > 0) {
                    if (sb.Length != 0) {
                        sb.Append(" + ");
                    }
                } else {
                    continue;
                }

                int av = Math.Abs(n);
                if (av != 1) {
                    sb.AppendFormat("{0}*", av);
                }
                sb.AppendFormat("e({0})", i + 1);
            }
            if (sb.Length == 0) {
                sb.Append('0');
            }
            return sb.ToString();
        }

        static void Main(string[] args) {
            List<List<int>> combos = new List<List<int>>{
                new List<int> { 1, 2, 3},
                new List<int> { 0, 1, 2, 3},
                new List<int> { 1, 0, 3, 4},
                new List<int> { 1, 2, 0},
                new List<int> { 0, 0, 0},
                new List<int> { 0},
                new List<int> { 1, 1, 1},
                new List<int> { -1, -1, -1},
                new List<int> { -1, -2, 0, -3},
                new List<int> { -1},
            };

            foreach (List<int> c in combos) {
                var arr = "[" + string.Join(", ", c) + "]";
                Console.WriteLine("{0,15} -> {1}", arr, LinearCombo(c));
            }
        }
    }
}
Output:
      [1, 2, 3] -> e(1) + 2*e(2) + 3*e(3)
   [0, 1, 2, 3] -> e(2) + 2*e(3) + 3*e(4)
   [1, 0, 3, 4] -> e(1) + 3*e(3) + 4*e(4)
      [1, 2, 0] -> e(1) + 2*e(2)
      [0, 0, 0] -> 0
            [0] -> 0
      [1, 1, 1] -> e(1) + e(2) + e(3)
   [-1, -1, -1] -> -e(1) - e(2) - e(3)
[-1, -2, 0, -3] -> -e(1) - 2*e(2) - 3*e(4)
           [-1] -> -e(1)

C++

Translation of: D
#include <iomanip>
#include <iostream>
#include <sstream>
#include <vector>

template<typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) {
    auto it = v.cbegin();
    auto end = v.cend();

    os << '[';
    if (it != end) {
        os << *it;
        it = std::next(it);
    }
    while (it != end) {
        os << ", " << *it;
        it = std::next(it);
    }
    return os << ']';
}

std::ostream& operator<<(std::ostream& os, const std::string& s) {
    return os << s.c_str();
}

std::string linearCombo(const std::vector<int>& c) {
    std::stringstream ss;
    for (size_t i = 0; i < c.size(); i++) {
        int n = c[i];
        if (n < 0) {
            if (ss.tellp() == 0) {
                ss << '-';
            } else {
                ss << " - ";
            }
        } else if (n > 0) {
            if (ss.tellp() != 0) {
                ss << " + ";
            }
        } else {
            continue;
        }

        int av = abs(n);
        if (av != 1) {
            ss << av << '*';
        }
        ss << "e(" << i + 1 << ')';
    }
    if (ss.tellp() == 0) {
        return "0";
    }
    return ss.str();
}

int main() {
    using namespace std;

    vector<vector<int>> combos{
        {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 (auto& c : combos) {
        stringstream ss;
        ss << c;
        cout << setw(15) << ss.str() << " -> ";
        cout << linearCombo(c) << '\n';
    }

    return 0;
}
Output:
      [1, 2, 3] -> e(1) + 2*e(2) + 3*e(3)
   [0, 1, 2, 3] -> e(2) + 2*e(3) + 3*e(4)
   [1, 0, 3, 4] -> e(1) + 3*e(3) + 4*e(4)
      [1, 2, 0] -> e(1) + 2*e(2)
      [0, 0, 0] -> 0
            [0] -> 0
      [1, 1, 1] -> e(1) + e(2) + e(3)
   [-1, -1, -1] -> -e(1) - e(2) - e(3)
[-1, -2, 0, -3] -> -e(1) - 2*e(2) - 3*e(4)
           [-1] -> -e(1)

D

Translation of: Kotlin
import std.array;
import std.conv;
import std.format;
import std.math;
import std.stdio;

string linearCombo(int[] c) {
    auto sb = appender!string;
    foreach (i, n; c) {
        if (n==0) continue;
        string op;
        if (n < 0) {
            if (sb.data.empty) {
                op = "-";
            } else {
                op = " - ";
            }
        } else if (n > 0) {
            if (!sb.data.empty) {
                op = " + ";
            }
        }
        auto av = abs(n);
        string coeff;
        if (av != 1) {
            coeff = to!string(av) ~ "*";
        }
        sb.formattedWrite("%s%se(%d)", op, coeff, i+1);
    }
    if (sb.data.empty) {
        return "0";
    }
    return sb.data;
}

void main() {
    auto combos = [
        [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],
    ];
    foreach (c; combos) {
        auto arr = c.format!"%s";
        writefln("%-15s  ->  %s", arr, linearCombo(c));
    }
}
Output:
[1, 2, 3]        ->  e(1) + 2*e(2) + 3*e(3)
[0, 1, 2, 3]     ->  e(2) + 2*e(3) + 3*e(4)
[1, 0, 3, 4]     ->  e(1) + 3*e(3) + 4*e(4)
[1, 2, 0]        ->  e(1) + 2*e(2)
[0, 0, 0]        ->  0
[0]              ->  0
[1, 1, 1]        ->  e(1) + e(2) + e(3)
[-1, -1, -1]     ->  -e(1) - e(2) - e(3)
[-1, -2, 0, -3]  ->  -e(1) - 2*e(2) - 3*e(4)
[-1]             ->  -e(1)

EchoLisp

;; build an html string from list of coeffs

(define (linear->html coeffs)
    (define plus #f) 
    (or* 
    (for/fold (html "") ((a coeffs) (i (in-naturals 1)))
      (unless (zero? a)
 		(set! plus (if plus "+" "")))
      (string-append html
	 (cond 
	  ((= a 1)  (format "%a e<sub>%d</sub> " plus i))
	  ((= a -1) (format "- e<sub>%d</sub> " i))
	  ((> a 0)  (format "%a %d*e<sub>%d</sub> " plus a i))
	  ((< a 0)  (format "- %d*e<sub>%d</sub> " (abs a) i))
	  (else ""))))
     "0"))
	
(define linears '((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)))
   
(define (task linears)
    (html-print ;; send string to stdout
    (for/string ((linear linears))
      (format "%a -> <span style='color:blue'>%a</span> <br>" linear (linear->html linear)))))
Output:

(1 2 3) -> e1 + 2*e2 + 3*e3
(0 1 2 3) -> e2 + 2*e3 + 3*e4
(1 0 3 4) -> e1 + 3*e3 + 4*e4
(1 2 0) -> e1 + 2*e2
(0 0 0) -> 0
(0) -> 0
(1 1 1) -> e1 + e2 + e3
(-1 -1 -1) -> - e1 - e2 - e3
(-1 -2 0 -3) -> - e1 - 2*e2 - 3*e4
(-1) -> - e1

Elixir

Works with: Elixir version 1.3
defmodule Linear_combination do
  def display(coeff) do
    Enum.with_index(coeff)
    |> Enum.map_join(fn {n,i} ->
         {m,s} = if n<0, do: {-n,"-"}, else: {n,"+"}
         case {m,i} do
           {0,_} -> ""
           {1,i} -> "#{s}e(#{i+1})"
           {n,i} -> "#{s}#{n}*e(#{i+1})"
         end
       end)
    |> String.trim_leading("+")
    |> case do
         ""  -> IO.puts "0"
         str -> IO.puts str
       end
  end
end

coeffs = 
  [ [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]
  ]
Enum.each(coeffs, &Linear_combination.display(&1))
Output:
e(1)+2*e(2)+3*e(3)
e(2)+2*e(3)+3*e(4)
e(1)+3*e(3)+4*e(4)
e(1)+2*e(2)
0
0
e(1)+e(2)+e(3)
-e(1)-e(2)-e(3)
-e(1)-2*e(2)-3*e(4)
-e(1)

F#

The function

// Display a linear combination. Nigel Galloway: March 28th., 2018
let fN g =
  let rec fG n g=match g with
                 |0::g    ->                        fG (n+1) g
                 |1::g    -> printf "+e(%d)" n;     fG (n+1) g
                 |(-1)::g -> printf "-e(%d)" n;     fG (n+1) g
                 |i::g    -> printf "%+de(%d)" i n; fG (n+1) g
                 |_       -> printfn ""
  let rec fN n g=match g with
                 |0::g    ->                        fN (n+1) g
                 |1::g    -> printf "e(%d)" n;      fG (n+1) g
                 |(-1)::g -> printf "-e(%d)" n;     fG (n+1) g
                 |i::g    -> printf "%de(%d)" i n;  fG (n+1) g
                 |_       -> printfn "0"
  fN 1 g

The Task

fN [1;2;3]
Output:
e(1)+2e(2)+3e(3)
fN [0;1;2;3]
Output:
e(2)+2e(3)+3e(4)
fN[1;0;3;4]
Output:
e(1)+3e(3)+4e(4)
fN[1;2;0]
Output:
e(1)+2e(2)
fN[0;0;0]
Output:
0
fN[0]
Output:
0
fN[1;1;1]
Output:
e(1)+e(2)+e(3)
fN[-1;-1;-1]
Output:
-e(1)-e(2)-e(3)
fN[-1;-2;0;-3]
Output:
-e(1)-2e(2)-3e(4)
fN[1]
Output:
e(1)

Factor

USING: formatting kernel match math pair-rocket regexp sequences ;

MATCH-VARS: ?a ?b ;

: choose-term ( coeff i -- str )
    1 + { } 2sequence {
        {  0  _ } => [       ""                 ]
        {  1 ?a } => [ ?a    "e(%d)"    sprintf ]
        { -1 ?a } => [ ?a    "-e(%d)"   sprintf ]
        { ?a ?b } => [ ?a ?b "%d*e(%d)" sprintf ]
    } match-cond ;
    
: linear-combo ( seq -- str )
    [ choose-term ] map-index harvest " + " join
    R/ \+ -/ "- " re-replace [ "0" ] when-empty ;
    
{ { 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 } }
[ dup linear-combo "%-14u  ->  %s\n" printf ] each
Output:
{ 1 2 3 }       ->  e(1) + 2*e(2) + 3*e(3)
{ 0 1 2 3 }     ->  e(2) + 2*e(3) + 3*e(4)
{ 1 0 3 4 }     ->  e(1) + 3*e(3) + 4*e(4)
{ 1 2 0 }       ->  e(1) + 2*e(2)
{ 0 0 0 }       ->  0
{ 0 }           ->  0
{ 1 1 1 }       ->  e(1) + e(2) + e(3)
{ -1 -1 -1 }    ->  -e(1) - e(2) - e(3)
{ -1 -2 0 -3 }  ->  -e(1) - 2*e(2) - 3*e(4)
{ -1 }          ->  -e(1)


FreeBASIC

Translation of: Ring
Dim scalars(1 To 10, 1 To 4) As Integer => {{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 n As Integer = 1 To Ubound(scalars)
    Dim As String cadena = ""
    Dim As Integer scalar
    For m As Integer = 1 To Ubound(scalars,2)
        scalar = scalars(n, m)
        If scalar <> 0 Then
            If scalar = 1 Then
                cadena &= "+e" & m
            Elseif scalar = -1 Then
                cadena &= "-e" & m
            Else
                If scalar > 0 Then
                    cadena &= Chr(43) & scalar & "*e" & m
                Else
                    cadena &= scalar & "*e" & m
                End If
            End If
        End If   
    Next m
    If cadena = "" Then cadena = "0"
    If Left(cadena, 1) = "+" Then cadena = Right(cadena, Len(cadena)-1)
    Print cadena
Next n
Sleep
Output:
Igual que la entrada de Ring.

Go

Translation of: Kotlin
package main

import (
    "fmt"
    "strings"
)

func linearCombo(c []int) string {
    var sb strings.Builder
    for i, n := range c {
        if n == 0 {
            continue
        }
        var op string
        switch {
        case n < 0 && sb.Len() == 0:
            op = "-"
        case n < 0:
            op = " - "
        case n > 0 && sb.Len() == 0:
            op = ""
        default:
            op = " + "
        }
        av := n
        if av < 0 {
            av = -av
        }
        coeff := fmt.Sprintf("%d*", av)
        if av == 1 {
            coeff = ""
        }
        sb.WriteString(fmt.Sprintf("%s%se(%d)", op, coeff, i+1))
    }
    if sb.Len() == 0 {
        return "0"
    } else {
        return sb.String()
    }
}

func main() {
    combos := [][]int{
        {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 _, c := range combos {
        t := strings.Replace(fmt.Sprint(c), " ", ", ", -1)
        fmt.Printf("%-15s  ->  %s\n", t, linearCombo(c))
    }
}
Output:
[1, 2, 3]        ->  e(1) + 2*e(2) + 3*e(3)
[0, 1, 2, 3]     ->  e(2) + 2*e(3) + 3*e(4)
[1, 0, 3, 4]     ->  e(1) + 3*e(3) + 4*e(4)
[1, 2, 0]        ->  e(1) + 2*e(2)
[0, 0, 0]        ->  0
[0]              ->  0
[1, 1, 1]        ->  e(1) + e(2) + e(3)
[-1, -1, -1]     ->  -e(1) - e(2) - e(3)
[-1, -2, 0, -3]  ->  -e(1) - 2*e(2) - 3*e(4)
[-1]             ->  -e(1)

Groovy

Translation of: Java
class LinearCombination {
    private static String linearCombo(int[] c) {
        StringBuilder sb = new StringBuilder()
        for (int i = 0; i < c.length; ++i) {
            if (c[i] == 0) continue
            String op
            if (c[i] < 0 && sb.length() == 0) {
                op = "-"
            } else if (c[i] < 0) {
                op = " - "
            } else if (c[i] > 0 && sb.length() == 0) {
                op = ""
            } else {
                op = " + "
            }
            int av = Math.abs(c[i])
            String coeff = av == 1 ? "" : "" + av + "*"
            sb.append(op).append(coeff).append("e(").append(i + 1).append(')')
        }
        if (sb.length() == 0) {
            return "0"
        }
        return sb.toString()
    }

    static void main(String[] args) {
        int[][] combos = [
                [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 (int[] c : combos) {
            printf("%-15s  ->  %s\n", Arrays.toString(c), linearCombo(c))
        }
    }
}
Output:
[1, 2, 3]        ->  e(1) + 2*e(2) + 3*e(3)
[0, 1, 2, 3]     ->  e(2) + 2*e(3) + 3*e(4)
[1, 0, 3, 4]     ->  e(1) + 3*e(3) + 4*e(4)
[1, 2, 0]        ->  e(1) + 2*e(2)
[0, 0, 0]        ->  0
[0]              ->  0
[1, 1, 1]        ->  e(1) + e(2) + e(3)
[-1, -1, -1]     ->  -e(1) - e(2) - e(3)
[-1, -2, 0, -3]  ->  -e(1) - 2*e(2) - 3*e(4)
[-1]             ->  -e(1)

Haskell

import Text.Printf (printf)

linearForm :: [Int] -> String
linearForm = strip . concat . zipWith term [1..]
  where
    term :: Int -> Int -> String
    term i c = case c of
      0  -> mempty
      1  -> printf "+e(%d)" i
      -1 -> printf "-e(%d)" i
      c  -> printf "%+d*e(%d)" c i

    strip str = case str of
      '+':s -> s
      ""    -> "0"
      s     -> s

Testing

coeffs :: [[Int]]
coeffs = [ [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] ]
λ> mapM_ (print . linearForm) coeffs
"e(1)+2*e(2)+3*e(3)"
"e(2)+2*e(3)+3*e(4)"
"e(1)+3*e(3)+4*e(4)"
"e(1)+2*e(2)"
"0"
"0"
"e(1)+e(2)+e(3)"
"-e(1)-e(2)-e(3)"
"-e(1)-2*e(2)-3*e(4)"
"-e(1)"

J

Implementation:

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.
)

Example use:

   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)

Java

Translation of: Kotlin
import java.util.Arrays;

public class LinearCombination {
    private static String linearCombo(int[] c) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < c.length; ++i) {
            if (c[i] == 0) continue;
            String op;
            if (c[i] < 0 && sb.length() == 0) {
                op = "-";
            } else if (c[i] < 0) {
                op = " - ";
            } else if (c[i] > 0 && sb.length() == 0) {
                op = "";
            } else {
                op = " + ";
            }
            int av = Math.abs(c[i]);
            String coeff = av == 1 ? "" : "" + av + "*";
            sb.append(op).append(coeff).append("e(").append(i + 1).append(')');
        }
        if (sb.length() == 0) {
            return "0";
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        int[][] combos = new int[][]{
            new int[]{1, 2, 3},
            new int[]{0, 1, 2, 3},
            new int[]{1, 0, 3, 4},
            new int[]{1, 2, 0},
            new int[]{0, 0, 0},
            new int[]{0},
            new int[]{1, 1, 1},
            new int[]{-1, -1, -1},
            new int[]{-1, -2, 0, -3},
            new int[]{-1},
        };
        for (int[] c : combos) {
            System.out.printf("%-15s  ->  %s\n", Arrays.toString(c), linearCombo(c));
        }
    }
}
Output:
[1, 2, 3]        ->  e(1) + 2*e(2) + 3*e(3)
[0, 1, 2, 3]     ->  e(2) + 2*e(3) + 3*e(4)
[1, 0, 3, 4]     ->  e(1) + 3*e(3) + 4*e(4)
[1, 2, 0]        ->  e(1) + 2*e(2)
[0, 0, 0]        ->  0
[0]              ->  0
[1, 1, 1]        ->  e(1) + e(2) + e(3)
[-1, -1, -1]     ->  -e(1) - e(2) - e(3)
[-1, -2, 0, -3]  ->  -e(1) - 2*e(2) - 3*e(4)
[-1]             ->  -e(1)

jq

def linearCombo:
  reduce to_entries[] as {key: $k,value: $v} ("";
     if $v == 0 then .
     else
        (if $v < 0 and length==0 then   "-"
         elif $v < 0 then               " - "
         elif $v > 0 and length==0 then ""
         else                           " + "
         end) as $sign
        | ($v|fabs) as $av
        | (if ($av == 1) then "" else "\($av)*" end) as $coeff
        | .  + "\($sign)\($coeff)e\($k)"
     end)
  | if length==0 then "0" else . end ;

# The exercise
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;

[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]
| "\(lpad(15)) => \(linearCombo)"
Output:
        [1,2,3] => e0 + 2*e1 + 3*e2
      [0,1,2,3] => e1 + 2*e2 + 3*e3
      [1,0,3,4] => e0 + 3*e2 + 4*e3
        [1,2,0] => e0 + 2*e1
        [0,0,0] => 0
            [0] => 0
        [1,1,1] => e0 + e1 + e2
     [-1,-1,-1] => -e0 - e1 - e2
   [-1,-2,0,-3] => -e0 - 2*e1 - 3*e3
           [-1] => -e0

Julia

# v0.6

linearcombination(coef::Array) = join(collect("$c * e($i)" for (i, c) in enumerate(coef) if c != 0), " + ")

for c in [[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]]
    @printf("%20s -> %s\n", c, linearcombination(c))
end
Output:
           [1, 2, 3] -> 1 * e(1) + 2 * e(2) + 3 * e(3)
        [0, 1, 2, 3] -> 1 * e(2) + 2 * e(3) + 3 * e(4)
        [1, 0, 3, 4] -> 1 * e(1) + 3 * e(3) + 4 * e(4)
           [1, 2, 0] -> 1 * e(1) + 2 * e(2)
           [0, 0, 0] -> 
                 [0] -> 
           [1, 1, 1] -> 1 * e(1) + 1 * e(2) + 1 * e(3)
        [-1, -1, -1] -> -1 * e(1) + -1 * e(2) + -1 * e(3)
     [-1, -2, 0, -3] -> -1 * e(1) + -2 * e(2) + -3 * e(4)
                [-1] -> -1 * e(1)

Kotlin

// version 1.1.2

fun linearCombo(c: IntArray): String { 
    val sb = StringBuilder()
    for ((i, n) in c.withIndex()) {
        if (n == 0) continue
        val op = when {
            n < 0 && sb.isEmpty() -> "-"
            n < 0                 -> " - "
            n > 0 && sb.isEmpty() -> ""
            else                  -> " + "
        }
        val av = Math.abs(n)
        val coeff = if (av == 1) "" else "$av*"
        sb.append("$op${coeff}e(${i + 1})")
    }
    return if(sb.isEmpty()) "0" else sb.toString()
}

fun main(args: Array<String>) { 
    val combos = arrayOf(
        intArrayOf(1, 2, 3),
        intArrayOf(0, 1, 2, 3),
        intArrayOf(1, 0, 3, 4),
        intArrayOf(1, 2, 0),
        intArrayOf(0, 0, 0),
        intArrayOf(0),
        intArrayOf(1, 1, 1),
        intArrayOf(-1, -1, -1),
        intArrayOf(-1, -2, 0, -3),
        intArrayOf(-1)
    )
    for (c in combos) {
        println("${c.contentToString().padEnd(15)}  ->  ${linearCombo(c)}")
    }
}
Output:
[1, 2, 3]        ->  e(1) + 2*e(2) + 3*e(3)
[0, 1, 2, 3]     ->  e(2) + 2*e(3) + 3*e(4)
[1, 0, 3, 4]     ->  e(1) + 3*e(3) + 4*e(4)
[1, 2, 0]        ->  e(1) + 2*e(2)
[0, 0, 0]        ->  0
[0]              ->  0
[1, 1, 1]        ->  e(1) + e(2) + e(3)
[-1, -1, -1]     ->  -e(1) - e(2) - e(3)
[-1, -2, 0, -3]  ->  -e(1) - 2*e(2) - 3*e(4)
[-1]             ->  -e(1)

Lambdatalk

{def linearcomb
 {def linearcomb.r
  {lambda {:a :n :i}
   {if {= :i :n}
    then 
    else {let { {:e e({+ :i 1})}
                {:v {abs {A.get :i :a}}}
                {:s {if {< {A.get :i :a} 0} then - else +}}
              } {if {= :v 0} then  else
                {if {= :v 1} then :s :e else :s :v*:e}}}
         {linearcomb.r :a :n {+ :i 1}} }}}
 {lambda {:a}
  {S.replace _LAMB_[^\s]+ by 0 in
   {let { {:r {linearcomb.r {A.new :a} {S.length :a} 0}}
        } {if {W.equal? {S.first :r} +} then {S.rest :r} else :r} }}}}
-> linearcomb

{linearcomb 1 2 3}      -> e(1) + 2*e(2) + 3*e(3)
{linearcomb -1 -2 0 -3} -> - e(1) - 2*e(2) - 3*e(4) 
{linearcomb 0 1 2 3}    -> e(2) + 2*e(3) + 3*e(4) 
{linearcomb 1 0 3 4}    -> e(1) + 3*e(3) + 4*e(4)
{linearcomb 1 2 0}      -> e(1) + 2*e(2) 
{linearcomb 0 0 0}      -> 0
{linearcomb 0}          -> 0 
{linearcomb 1 1 1}      -> e(1) + e(2) + e(3)  
{linearcomb -1 -1 -1}   -> - e(1) - e(2) - e(3)
{linearcomb -1}         -> - e(1)

Lua

Translation of: C#
function t2s(t)
    local s = "["
    for i,v in pairs(t) do
        if i > 1 then
            s = s .. ", " .. v
        else
            s = s .. v
        end
    end
    return s .. "]"
end

function linearCombo(c)
    local sb = ""
    for i,n in pairs(c) do
        local skip = false

        if n < 0 then
            if sb:len() == 0 then
                sb = sb .. "-"
            else
                sb = sb .. " - "
            end
        elseif n > 0 then
            if sb:len() ~= 0 then
                sb = sb .. " + "
            end
        else
            skip = true
        end

        if not skip then
            local av = math.abs(n)
            if av ~= 1 then
                sb = sb .. av .. "*"
            end
            sb = sb .. "e(" .. i .. ")"
        end
    end
    if sb:len() == 0 then
        sb = "0"
    end
    return sb
end

function main()
    local combos = {
        {  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,c in pairs(combos) do
        local arr = t2s(c)
        print(string.format("%15s -> %s", arr, linearCombo(c)))
    end
end

main()
Output:
      [1, 2, 3] -> e(1) + 2*e(2) + 3*e(3)
   [0, 1, 2, 3] -> e(2) + 2*e(3) + 3*e(4)
   [1, 0, 3, 4] -> e(1) + 3*e(3) + 4*e(4)
      [1, 2, 0] -> e(1) + 2*e(2)
      [0, 0, 0] -> 0
            [0] -> 0
      [1, 1, 1] -> e(1) + e(2) + e(3)
   [-1, -1, -1] -> -e(1) - e(2) - e(3)
[-1, -2, 0, -3] -> -e(1) - 2*e(2) - 3*e(4)
           [-1] -> -e(1)


Mathematica / Wolfram Language

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}};
Column[TraditionalForm[Total[MapIndexed[#1 e[#2[[1]]] &, #]]] & /@ tests]
Output:
e(1)+2e(2)+3e(3)
e(2)+2e(3)+3e(4)
e(1)+3e(3)+4e(4)
e(1)+2e(2)
0
0
e(1)+e(2)+e(3)
-e(1)-e(2)-e(3)
-e(1)-2e(2)-3e(4)
-e(1)

Modula-2

MODULE Linear;
FROM FormatString IMPORT FormatString;
FROM Terminal IMPORT WriteString,WriteLn,ReadChar;

PROCEDURE WriteInt(n : INTEGER);
VAR buf : ARRAY[0..15] OF CHAR;
BEGIN
    FormatString("%i", buf, n);
    WriteString(buf)
END WriteInt;

PROCEDURE WriteLinear(c : ARRAY OF INTEGER);
VAR
    buf : ARRAY[0..15] OF CHAR;
    i,j : CARDINAL;
    b : BOOLEAN;
BEGIN
    b := TRUE;
    j := 0;

    FOR i:=0 TO HIGH(c) DO
        IF c[i]=0 THEN CONTINUE END;

        IF c[i]<0 THEN
            IF b THEN WriteString("-")
            ELSE      WriteString(" - ") END;
        ELSIF c[i]>0 THEN
            IF NOT b THEN WriteString(" + ") END;
        END;

        IF c[i] > 1 THEN
            WriteInt(c[i]);
            WriteString("*")
        ELSIF c[i] < -1 THEN
            WriteInt(-c[i]);
            WriteString("*")
        END;

        FormatString("e(%i)", buf, i+1);
        WriteString(buf);

        b := FALSE;
        INC(j)
    END;

    IF j=0 THEN WriteString("0") END;
    WriteLn
END WriteLinear;

TYPE
    Array1 = ARRAY[0..0] OF INTEGER;
    Array3 = ARRAY[0..2] OF INTEGER;
    Array4 = ARRAY[0..3] OF INTEGER;
BEGIN
    WriteLinear(Array3{1,2,3});
    WriteLinear(Array4{0,1,2,3});
    WriteLinear(Array4{1,0,3,4});
    WriteLinear(Array3{1,2,0});
    WriteLinear(Array3{0,0,0});
    WriteLinear(Array1{0});
    WriteLinear(Array3{1,1,1});
    WriteLinear(Array3{-1,-1,-1});
    WriteLinear(Array4{-1,-2,0,-3});
    WriteLinear(Array1{-1});

    ReadChar
END Linear.

Nim

Translation of: Kotlin
import strformat

proc linearCombo(c: openArray[int]): string =

  for i, n in c:
    if n == 0: continue
    let op = if n < 0:
               if result.len == 0: "-" else: " - "
             else:
               if n > 0 and result.len == 0: "" else: " + "
    let av = abs(n)
    let coeff = if av == 1: "" else: $av & '*'
    result &= fmt"{op}{coeff}e({i + 1})"
  if result.len == 0:
    result = "0"

const Combos = [@[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 c in Combos:
  echo fmt"{($c)[1..^1]:15}  →  {linearCombo(c)}"
Output:
[1, 2, 3]        →  e(1) + 2*e(2) + 3*e(3)
[0, 1, 2, 3]     →  e(2) + 2*e(3) + 3*e(4)
[1, 0, 3, 4]     →  e(1) + 3*e(3) + 4*e(4)
[1, 2, 0]        →  e(1) + 2*e(2)
[0, 0, 0]        →  0
[0]              →  0
[1, 1, 1]        →  e(1) + e(2) + e(3)
[-1, -1, -1]     →  -e(1) - e(2) - e(3)
[-1, -2, 0, -3]  →  -e(1) - 2*e(2) - 3*e(4)
[-1]             →  -e(1)

Perl

use strict;
use warnings;
use feature 'say';

sub linear_combination {
    my(@coef) = @$_;
    my $e = '';
    for my $c (1..+@coef) { $e .= "$coef[$c-1]*e($c) + " if $coef[$c-1] }
    $e =~ s/ \+ $//;
    $e =~ s/1\*//g;
    $e =~ s/\+ -/- /g;
    $e or 0;
}

say linear_combination($_) for
  [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 ]
Output:
e(1) + 2*e(2) + 3*e(3)
e(2) + 2*e(3) + 3*e(4)
e(1) + 3*e(3) + 4*e(4)
e(1) + 2*e(2)
0
0
e(1) + e(2) + e(3)
-e(1) - e(2) - e(3)
-e(1) - 2*e(2) - 3*e(4)
-e(1)

Phix

Translation of: Tcl
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
Output:
     {1,2,3} -> e(1)+2*e(2)+3*e(3)
   {0,1,2,3} -> e(2)+2*e(3)+3*e(4)
   {1,0,3,4} -> e(1)+3*e(3)+4*e(4)
     {1,2,0} -> e(1)+2*e(2)
     {0,0,0} -> 0
         {0} -> 0
     {1,1,1} -> e(1)+e(2)+e(3)
  {-1,-1,-1} -> -e(1)-e(2)-e(3)
{-1,-2,0,-3} -> -e(1)-2*e(2)-3*e(4)
        {-1} -> -e(1)

PureBasic

; Process and output values.
Procedure WriteLinear(Array c.i(1))
  Define buf$,
         i.i, j.i, b,i
  
  b = #True
  j = 0
  
  For i = 0 To ArraySize(c(), 1) 
    If c(i) = 0 : Continue : EndIf
    
    If c(i) < 0
      If b : Print("-") : Else : Print(" - ") : EndIf
    ElseIf c(i) > 0
      If Not b : Print(" + ") : EndIf
    EndIf
    
    If c(i) > 1
      Print(Str(c(i))+"*")
    ElseIf c(i) < -1
      Print(Str(-c(i))+"*")
    EndIf
    
    Print("e("+Str(i+1)+")")
    
    b = #False
    j+1
  Next
  
  If j = 0 : Print("0") : EndIf
  PrintN("")
EndProcedure


Macro VectorHdl(Adr_Start, Adr_Stop)
  ; 1. Output of the input values  
  Define buf$ = "[", *adr_ptr
  For *adr_ptr = Adr_Start To Adr_Stop - SizeOf(Integer) Step SizeOf(Integer)
    buf$ + Str(PeekI(*adr_ptr))
    If *adr_ptr >= Adr_Stop - SizeOf(Integer)
      buf$ + "]  ->  "
    Else
      buf$ + ", "
    EndIf    
  Next
  buf$ =  RSet(buf$, 25)
  Print(buf$)  
  
  ; 2. Reserve memory, pass and process values.
  Dim a.i((Adr_Stop - Adr_Start) / SizeOf(Integer) -1)
  CopyMemory(Adr_Start, @a(0), Adr_Stop - Adr_Start)
  WriteLinear(a())
EndMacro


If OpenConsole("")
  ; Pass memory addresses of the data.
  VectorHdl(?V1, ?_V1)
  VectorHdl(?V2, ?_V2)
  VectorHdl(?V3, ?_V3)
  VectorHdl(?V4, ?_V4)
  VectorHdl(?V5, ?_V5)
  VectorHdl(?V6, ?_V6)
  VectorHdl(?V7, ?_V7)
  VectorHdl(?V8, ?_V8)
  VectorHdl(?V9, ?_V9)
  VectorHdl(?V10, ?_V10)    
  
  Input()
EndIf

End 0
      
    
DataSection
  V1:
  Data.i 1,2,3
  _V1:
  V2:
  Data.i 0,1,2,3
  _V2:
  V3:
  Data.i 1,0,3,4
  _V3:
  V4:
  Data.i 1,2,0
  _V4:
  V5:
  Data.i 0,0,0
  _V5:
  V6:
  Data.i 0
  _V6:
  V7:
  Data.i 1,1,1
  _V7:
  V8:
  Data.i -1,-1,-1
  _V8:
  V9:
  Data.i -1,-2,0,-3
  _V9:
  V10:
  Data.i -1
  _V10:  
EndDataSection
Output:
          [1, 2, 3]  ->  e(1) + 2*e(2) + 3*e(3)
       [0, 1, 2, 3]  ->  e(2) + 2*e(3) + 3*e(4)
       [1, 0, 3, 4]  ->  e(1) + 3*e(3) + 4*e(4)
          [1, 2, 0]  ->  e(1) + 2*e(2)
          [0, 0, 0]  ->  0
                [0]  ->  0
          [1, 1, 1]  ->  e(1) + e(2) + e(3)
       [-1, -1, -1]  ->  -e(1) - e(2) - e(3)
    [-1, -2, 0, -3]  ->  -e(1) - 2*e(2) - 3*e(4)
               [-1]  ->  -e(1)

Python

def linear(x):
    return ' + '.join(['{}e({})'.format('-' if v == -1 else '' if v == 1 else str(v) + '*', i + 1)
        for i, v in enumerate(x) if v] or ['0']).replace(' + -', ' - ')

list(map(lambda x: print(linear(x)), [[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]]))
Output:
e(1) + 2*e(2) + 3*e(3)
e(2) + 2*e(3) + 3*e(4)
e(1) + 3*e(3) + 4*e(4)
e(1) + 2*e(2)
0
0
e(1) + e(2) + e(3)
-e(1) - e(2) - e(3)
-e(1) - 2*e(2) + 3*e(4)
-e(1)

Racket

#lang racket/base
(require racket/match racket/string)

(define (linear-combination->string es)
  (let inr ((es es) (i 1) (rv ""))
    (match* (es rv)
      [((list) "") "0"]
      [((list) rv) rv]
      [((list (? zero?) t ...) rv)
       (inr t (add1 i) rv)]
      [((list n t ...) rv)
       (define ±n
         (match* (n rv)
           ;; zero is handled above
           [(1 "") ""]
           [(1 _) "+"]
           [(-1 _) "-"]
           [((? positive? n) (not "")) (format "+~a*" n)]
           [(n _) (format "~a*" n)]))
       (inr t (add1 i) (string-append rv ±n "e("(number->string i)")"))])))

(for-each
 (compose displayln linear-combination->string)
 '((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)))
Output:
e(1)+2*e(2)+3*e(3)
e(2)+2*e(3)+3*e(4)
e(1)+3*e(3)+4*e(4)
e(1)+2*e(2)
0
0
e(1)+e(2)+e(3)
-e(1)-e(2)-e(3)
-e(1)-2*e(2)-3*e(4)
-e(1)

Raku

(formerly Perl 6)

sub linear-combination(@coeff) {
    (@coeff Z=> map { "e($_)" }, 1 .. *)
    .grep(+*.key)
    .map({ .key ~ '*' ~ .value })
    .join(' + ')
    .subst('+ -', '- ', :g)
    .subst(/<|w>1\*/, '', :g)
        || '0'
}
 
say linear-combination($_) for 
[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 ]
;
Output:
e(1) + 2*e(2) + 3*e(3)
e(2) + 2*e(3) + 3*e(4)
e(1) + 3*e(3) + 4*e(4)
e(1) + 2*e(2)
0
0
e(1) + e(2) + e(3)
-e(1) - e(2) - e(3)
-e(1) - 2*e(2) - 3*e(4)
-e(1)

REXX

/*REXX program displays a   finite liner combination   in an   infinite vector basis.   */
@.= .;           @.1  =    '  1,  2,  3     '    /*define a specific test case for build*/
                 @.2  =    '  0,  1,  2,  3 '    /*   "   "     "      "    "   "    "  */
                 @.3  =    '  1,  0,  3,  4 '    /*   "   "     "      "    "   "    "  */
                 @.4  =    '  1,  2,  0     '    /*   "   "     "      "    "   "    "  */
                 @.5  =    '  0,  0,  0     '    /*   "   "     "      "    "   "    "  */
                 @.6  =       0                  /*   "   "     "      "    "   "    "  */
                 @.7  =    '  1,  1,  1     '    /*   "   "     "      "    "   "    "  */
                 @.8  =    ' -1, -1, -1     '    /*   "   "     "      "    "   "    "  */
                 @.9  =    ' -1, -2,  0, -3 '    /*   "   "     "      "    "   "    "  */
                 @.10 =      -1                  /*   "   "     "      "    "   "    "  */
  do j=1  while  @.j\==.;        n= 0            /*process each vector; zero element cnt*/
  y= space( translate(@.j, ,',') )               /*elide commas and superfluous blanks. */
  $=                                             /*nullify  output  (liner combination).*/
       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 s= '- '                      /*define the sign:   minus (-).        */
               else s= '+ '                      /*   "    "    "     plus  (+).        */
       n= n + 1                                  /*bump the number of elements in vector*/
       if n==1  then s= strip(s)                 /*if the 1st element used, remove blank*/
       if a\==1    then s= s  ||  a'*'           /*if multiplier is unity, then ignore #*/
       $= $  s'e('k")"                           /*construct a liner combination element*/
       end   /*k*/
  $= strip( strip($), 'L', "+")                  /*strip leading plus sign (1st element)*/
  if $==''  then $= 0                            /*handle special case of no elements.  */
  say right( space(@.j), 20)  ' ──► '   strip($) /*align the output for presentation.   */
  end       /*j*/                                /*stick a fork in it,  we're all done. */
output   when using the default inputs:
             1, 2, 3  ──►  e(1) + 2*e(2) + 3*e(3)
          0, 1, 2, 3  ──►  e(2) + 2*e(3) + 3*e(4)
          1, 0, 3, 4  ──►  e(1) + 3*e(3) + 4*e(4)
             1, 2, 0  ──►  e(1) + 2*e(2)
             0, 0, 0  ──►  0
                   0  ──►  0
             1, 1, 1  ──►  e(1) + e(2) + e(3)
          -1, -1, -1  ──►  -e(1) - e(2) - e(3)
       -1, -2, 0, -3  ──►  -e(1) - 2*e(2) - 3*e(4)
                  -1  ──►  -e(1)

Ring

# Project : Display a linear combination

scalars = [[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 n=1 to len(scalars)
    str = ""
    for m=1 to len(scalars[n])
        scalar = scalars[n] [m]
        if scalar != "0"
           if scalar = 1
              str = str + "+e" + m
           elseif  scalar = -1
              str = str + "" + "-e" + m
           else
              if scalar > 0
                 str = str + char(43) + scalar + "*e" + m
              else
                 str = str + "" + scalar + "*e" + m
              ok
           ok
        ok   
    next
    if str = ""
       str = "0"
    ok
    if left(str, 1) = "+"
       str = right(str, len(str)-1)
    ok
    see str + nl
next

Output:

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

Ruby

Translation of: D
def linearCombo(c)
    sb = ""
    c.each_with_index { |n, i|
        if n == 0 then
            next
        end
        if n < 0 then
            if sb.length == 0 then
                op = "-"
            else
                op = " - "
            end
        elsif n > 0 then
            if sb.length > 0 then
                op = " + "
            else
                op = ""
            end
        else
            op = ""
        end
        av = n.abs()
        if av != 1 then
            coeff = "%d*" % [av]
        else
            coeff = ""
        end
        sb = sb + "%s%se(%d)" % [op, coeff, i + 1]
    }
    if sb.length == 0 then
        return "0"
    end
    return sb
end

def main
    combos = [
        [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 c in combos do
        print "%-15s  ->  %s\n" % [c, linearCombo(c)]
    end
end

main()
Output:
[1, 2, 3]        ->  e(1) + 2*e(2) + 3*e(3)
[0, 1, 2, 3]     ->  e(2) + 2*e(3) + 3*e(4)
[1, 0, 3, 4]     ->  e(1) + 3*e(3) + 4*e(4)
[1, 2, 0]        ->  e(1) + 2*e(2)
[0, 0, 0]        ->  0
[0]              ->  0
[1, 1, 1]        ->  e(1) + e(2) + e(3)
[-1, -1, -1]     ->  -e(1) - e(2) - e(3)
[-1, -2, 0, -3]  ->  -e(1) - 2*e(2) - 3*e(4)
[-1]             ->  -e(1)

Rust

use std::fmt::{Display, Formatter, Result};
use std::process::exit;

struct Coefficient(usize, f64);

impl Display for Coefficient {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
        let i = self.0;
        let c = self.1;

        if c == 0. {
            return Ok(());
        }

        write!(
            f,
            " {} {}e({})",
            if c < 0. {
                "-"
            } else if f.alternate() {
                " "
            } else {
                "+"
            },
            if (c.abs() - 1.).abs() < f64::EPSILON {
                "".to_string()
            } else {
                c.abs().to_string() + "*"
            },
            i + 1
        )
    }
}

fn usage() {
    println!("Usage: display-linear-combination a1 [a2 a3 ...]");
}

fn linear_combination(coefficients: &[f64]) -> String {
    let mut string = String::new();

    let mut iter = coefficients.iter().enumerate();

    // find first nonzero argument
    loop {
        match iter.next() {
            Some((_, &c)) if c == 0. => {
                continue;
            }
            Some((i, &c)) => {
                string.push_str(format!("{:#}", Coefficient(i, c)).as_str());
                break;
            }
            None => {
                string.push('0');
                return string;
            }
        }
    }

    // print subsequent arguments
    for (i, &c) in iter {
        string.push_str(format!("{}", Coefficient(i, c)).as_str());
    }

    string
}

fn main() {
    let mut coefficients = Vec::new();
    let mut args = std::env::args();

    args.next(); // drop first argument

    // parse arguments into floats
    for arg in args {
        let c = arg.parse::<f64>().unwrap_or_else(|e| {
            eprintln!("Failed to parse argument \"{}\": {}", arg, e);
            exit(-1);
        });
        coefficients.push(c);
    }

    // no arguments, print usage and exit
    if coefficients.is_empty() {
        usage();
        return;
    }

    println!("{}", linear_combination(&coefficients));
}
Output:
1 2 3 -> e(1) + 2*e(2) + 3*e(3)

Scala

object LinearCombination extends App {
    val combos = Seq(Seq(1, 2, 3), Seq(0, 1, 2, 3),
      Seq(1, 0, 3, 4), Seq(1, 2, 0), Seq(0, 0, 0), Seq(0),
      Seq(1, 1, 1), Seq(-1, -1, -1), Seq(-1, -2, 0, -3), Seq(-1))

  private def linearCombo(c: Seq[Int]): String = {
    val sb = new StringBuilder
    for {i <- c.indices
         term = c(i)
         if term != 0} {
      val av = math.abs(term)
      def op = if (term < 0 && sb.isEmpty) "-"
      else if (term < 0) " - "
      else if (term > 0 && sb.isEmpty) "" else " + "

      sb.append(op).append(if (av == 1) "" else s"$av*").append("e(").append(i + 1).append(')')
    }
    if (sb.isEmpty) "0" else sb.toString
  }
    for (c <- combos) {
      println(f"${c.mkString("[", ", ", "]")}%-15s  ->  ${linearCombo(c)}%s")
    }
}

Sidef

Translation of: Tcl
func linear_combination(coeffs) {
    var res = ""
    for e,f in (coeffs.kv) {
        given(f) {
            when (1) {
                res += "+e(#{e+1})"
            }
            when (-1) {
                res += "-e(#{e+1})"
            }
            case (.> 0) {
                res += "+#{f}*e(#{e+1})"
            }
            case (.< 0) {
                res += "#{f}*e(#{e+1})"
            }
        }
    }
    res -= /^\+/
    res || 0
}

var tests = [
    %n{1 2 3},
    %n{0 1 2 3},
    %n{1 0 3 4},
    %n{1 2 0},
    %n{0 0 0},
    %n{0},
    %n{1 1 1},
    %n{-1 -1 -1},
    %n{-1 -2 0 -3},
    %n{-1},
]

tests.each { |t|
    printf("%10s -> %-10s\n", t.join(' '), linear_combination(t))
}
Output:
     1 2 3 -> e(1)+2*e(2)+3*e(3)
   0 1 2 3 -> e(2)+2*e(3)+3*e(4)
   1 0 3 4 -> e(1)+3*e(3)+4*e(4)
     1 2 0 -> e(1)+2*e(2)
     0 0 0 -> 0         
         0 -> 0         
     1 1 1 -> e(1)+e(2)+e(3)
  -1 -1 -1 -> -e(1)-e(2)-e(3)
-1 -2 0 -3 -> -e(1)-2*e(2)-3*e(4)
        -1 -> -e(1)     

Tcl

This solution strives for legibility rather than golf.

proc lincom {factors} {
    set exp 0
    set res ""
    foreach f $factors {
        incr exp
        if {$f == 0} {
            continue
        } elseif {$f == 1} {
            append res "+e($exp)"
        } elseif {$f == -1} {
            append res "-e($exp)"
        } elseif {$f > 0} {
            append res "+$f*e($exp)"
        } else {
            append res "$f*e($exp)"
        }
    }
    if {$res eq ""} {set res 0}
    regsub {^\+} $res {} res
    return $res
}

foreach test {
    {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}
} {
    puts [format "%10s -> %-10s" $test [lincom $test]]
}
Output:
     1 2 3 -> e(1)+2*e(2)+3*e(3)
   0 1 2 3 -> e(2)+2*e(3)+3*e(4)
   1 0 3 4 -> e(1)+3*e(3)+4*e(4)
     1 2 0 -> e(1)+2*e(2)
     0 0 0 -> 0         
         0 -> 0         
     1 1 1 -> e(1)+e(2)+e(3)
  -1 -1 -1 -> -e(1)-e(2)-e(3)
-1 -2 0 -3 -> -e(1)-2*e(2)-3*e(4)
        -1 -> -e(1)     

Visual Basic .NET

Translation of: C#
Imports System.Text

Module Module1

    Function LinearCombo(c As List(Of Integer)) As String
        Dim sb As New StringBuilder
        For i = 0 To c.Count - 1
            Dim n = c(i)
            If n < 0 Then
                If sb.Length = 0 Then
                    sb.Append("-")
                Else
                    sb.Append(" - ")
                End If
            ElseIf n > 0 Then
                If sb.Length <> 0 Then
                    sb.Append(" + ")
                End If
            Else
                Continue For
            End If

            Dim av = Math.Abs(n)
            If av <> 1 Then
                sb.AppendFormat("{0}*", av)
            End If
            sb.AppendFormat("e({0})", i + 1)
        Next
        If sb.Length = 0 Then
            sb.Append("0")
        End If
        Return sb.ToString()
    End Function

    Sub Main()
        Dim combos = New List(Of List(Of Integer)) From {
            New List(Of Integer) From {1, 2, 3},
            New List(Of Integer) From {0, 1, 2, 3},
            New List(Of Integer) From {1, 0, 3, 4},
            New List(Of Integer) From {1, 2, 0},
            New List(Of Integer) From {0, 0, 0},
            New List(Of Integer) From {0},
            New List(Of Integer) From {1, 1, 1},
            New List(Of Integer) From {-1, -1, -1},
            New List(Of Integer) From {-1, -2, 0, -3},
            New List(Of Integer) From {-1}
        }

        For Each c In combos
            Dim arr = "[" + String.Join(", ", c) + "]"
            Console.WriteLine("{0,15} -> {1}", arr, LinearCombo(c))
        Next
    End Sub

End Module
Output:
      [1, 2, 3] -> e(1) + 2*e(2) + 3*e(3)
   [0, 1, 2, 3] -> e(2) + 2*e(3) + 3*e(4)
   [1, 0, 3, 4] -> e(1) + 3*e(3) + 4*e(4)
      [1, 2, 0] -> e(1) + 2*e(2)
      [0, 0, 0] -> 0
            [0] -> 0
      [1, 1, 1] -> e(1) + e(2) + e(3)
   [-1, -1, -1] -> -e(1) - e(2) - e(3)
[-1, -2, 0, -3] -> -e(1) - 2*e(2) - 3*e(4)
           [-1] -> -e(1)

Vlang

Translation of: Go
import strings

fn linear_combo(c []int) string {
    mut sb := strings.new_builder(128)
    for i, n in c {
        if n == 0 {
            continue
        }
        mut op := ''
        match true {
        	n < 0 && sb.len == 0 {
            	op = "-"
			}
        	n < 0{
            	op = " - "
			}
        	n > 0 && sb.len == 0 {
            	op = ""
			}
        	else{
            	op = " + "
			}
        }
        mut av := n
        if av < 0 {
            av = -av
        }
        mut coeff := "$av*"
        if av == 1 {
            coeff = ""
        }
        sb.write_string("$op${coeff}e(${i+1})")
    }
    if sb.len == 0 {
        return "0"
    } else {
        return sb.str()
    }
}
 
fn main() {
    combos := [
        [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 c in combos {
        println("${c:-15}  ->  ${linear_combo(c)}")
    }
}
Output:
[1, 2, 3]        ->  e(1) + 2*e(2) + 3*e(3)
[0, 1, 2, 3]     ->  e(2) + 2*e(3) + 3*e(4)
[1, 0, 3, 4]     ->  e(1) + 3*e(3) + 4*e(4)
[1, 2, 0]        ->  e(1) + 2*e(2)
[0, 0, 0]        ->  0
[0]              ->  0
[1, 1, 1]        ->  e(1) + e(2) + e(3)
[-1, -1, -1]     ->  -e(1) - e(2) - e(3)
[-1, -2, 0, -3]  ->  -e(1) - 2*e(2) - 3*e(4)
[-1]             ->  -e(1)

Wren

Translation of: Kotlin
Library: Wren-fmt
import "/fmt" for Fmt

var linearCombo = Fn.new { |c|
    var sb = ""
    var i = 0
    for (n in c) {
        if (n != 0) {
            var op = (n < 0 && sb == "") ? "-"   :
                     (n < 0)             ? " - " :
                     (n > 0 && sb == "") ? ""    : " + "
            var av = n.abs
            var coeff = (av == 1) ? "" : "%(av)*"
            sb = sb + "%(op)%(coeff)e(%(i + 1))"
        }
        i = i + 1
    }
    return (sb == "") ? "0" : sb
}

var combos = [
    [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 (c in combos) {
    Fmt.print("$-15s  ->  $s", c.toString, linearCombo.call(c))
}
Output:
[1, 2, 3]        ->  e(1) + 2*e(2) + 3*e(3)
[0, 1, 2, 3]     ->  e(2) + 2*e(3) + 3*e(4)
[1, 0, 3, 4]     ->  e(1) + 3*e(3) + 4*e(4)
[1, 2, 0]        ->  e(1) + 2*e(2)
[0, 0, 0]        ->  0
[0]              ->  0
[1, 1, 1]        ->  e(1) + e(2) + e(3)
[-1, -1, -1]     ->  -e(1) - e(2) - e(3)
[-1, -2, 0, -3]  ->  -e(1) - 2*e(2) - 3*e(4)
[-1]             ->  -e(1)

zkl

Translation of: Raku
fcn linearCombination(coeffs){
   [1..].zipWith(fcn(n,c){ if(c==0) "" else "%s*e(%s)".fmt(c,n) },coeffs)
      .filter().concat("+").replace("+-","-").replace("1*","")
   or 0
}
T(T(1,2,3),T(0,1,2,3),T(1,0,3,4),T(1,2,0),T(0,0,0),T(0),T(1,1,1),T(-1,-1,-1),
  T(-1,-2,0,-3),T(-1),T)
.pump(Console.println,linearCombination);
Output:
e(1)+2*e(2)+3*e(3)
e(2)+2*e(3)+3*e(4)
e(1)+3*e(3)+4*e(4)
e(1)+2*e(2)
0
0
e(1)+e(2)+e(3)
-e(1)-e(2)-e(3)
-e(1)-2*e(2)-3*e(4)
-e(1)
0