Display a linear combination: Difference between revisions

Applesoft BASIC
(Display a linear combination en FreeBASIC)
(Applesoft BASIC)
 
(15 intermediate revisions by 11 users not shown)
Line 38:
=={{header|11l}}==
{{trans|Python}}
<langsyntaxhighlight 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))
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))</langsyntaxhighlight>
{{out}}
<pre>
Line 59:
 
=={{header|Ada}}==
<langsyntaxhighlight Adalang="ada">with Ada.Text_Io;
with Ada.Strings.Unbounded;
with Ada.Strings.Fixed;
Line 112:
Put_Line (Linear_Combination ((-1, -2, 0, -3)));
Put_Line (Linear_Combination ((1 => -1)));
end Display_Linear;</langsyntaxhighlight>
{{out}}
<pre>e(1) + 2*e(2) + 3*e(3)
Line 124:
-e(1) - 2*e(2) - 3*e(4)
-e(1)</pre>
 
=={{header|ALGOL 68}}==
Using implicit multiplication operators, as in the C and Mathematica samples.
<syntaxhighlight lang="algol68">
BEGIN # display a string representation of some linear combinations #
# returns a string representing the sum of the terms of a linear combination #
# whose coefficients are the elements of coeffs #
PROC linear combination = ( []INT coeffs )STRING:
BEGIN
[]INT cf = coeffs[ AT 1 ]; # ensure the lower bound is 1 #
STRING result := "";
BOOL first term := TRUE;
FOR i FROM LWB cf TO UPB cf DO
IF INT c = cf[ i ];
c /= 0
THEN # non-null element #
IF first term THEN
# first term - only add the operator if it is "-" #
IF c < 0 THEN result +:= "-" FI;
first term := FALSE
ELSE
# second or subsequent term - separate from the previous #
# and always add the operator #
result +:= " " + IF c < 0 THEN "-" ELSE "+" FI + " "
FI;
# add the coefficient, unless it is one #
IF ABS c /= 1 THEN
result +:= whole( ABS c, 0 )
FI;
# add the vector #
result +:= "e(" + whole( i, 0 ) + ")"
FI
OD;
IF result = "" THEN "0" ELSE result FI
END # linear combination # ;
 
# test cases #
[][]INT 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 FROM LWB tests TO UPB tests DO
print( ( linear combination( tests[ i ] ), newline ) )
OD
END
</syntaxhighlight>
{{out}}
<pre>
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)
</pre>
 
=={{header|APL}}==
{{works with|Dyalog APL}}
<syntaxhighlight lang="apl"> lincomb←{
fmtco←{
mul←(⍵≠1)/(⍕|⍵),'*'
mul,'e(',(⍕⍺),')'
}
 
co←⊃¨fmtco/¨(⍵≠0)/(⍳⍴,⍵),¨|⍵
sgn←'-+'[1+(×⍵)/×⍵]
lc←(('+'=⊃)↓⊢)∊sgn,¨co
0=⍴lc:'0' ⋄ lc
}</syntaxhighlight>
{{out}}
<pre> ↑lincomb¨(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)
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) </pre>
=={{header|Arturo}}==
 
<syntaxhighlight lang="arturo">linearCombination: function [coeffs][
combo: new []
loop.with:'i coeffs 'x [
case [x]
when? [=0] []
when? [=1] -> 'combo ++ ~"e(|i+1|)"
when? [= neg 1] -> 'combo ++ ~"-e(|i+1|)"
else -> 'combo ++ ~"|x|*e(|i+1|)"
]
join.with: " + " 'combo
replace 'combo {/\+ -/} "- "
(empty? combo)? -> "0" -> combo
]
 
loop @[
[1 2 3]
[0 1 2 3]
[1 0 3 4]
[1 2 0]
[0 0 0]
[0]
[1 1 1]
@[neg 1 neg 1 neg 1]
@[neg 1 neg 2 0 neg 3]
@[neg 1]
] => [print linearCombination &]
</syntaxhighlight>
 
{{out}}
 
<pre>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)</pre>
 
=={{header|BASIC}}==
==={{header|Applesoft BASIC}}===
{{trans|Chipmunk Basic}} The Spanish word "cadena" means "string" in English.
<syntaxhighlight lang="basic"> 100 READ UX,UY
110 DIM SCALARS(UX,UY)
120 FOR N = 1 TO UX
130 FOR M = 1 TO UY
140 READ SCALARS(N,M)
150 NEXT M,N
160 DATA10,4
170 DATA1,2,3,0,0,1,2,3
180 DATA1,0,3,4,1,2,0,0
190 DATA0,0,0,0,0,0,0,0
200 DATA1,1,1,0,-1,-1,-1,0
210 DATA-1,-2,0,-3,-1,0,0,0
220 FOR N = 1 TO UX
230 CADENA$ = ""
240 FOR M = 1 TO UY
250 SCALAR = SCALARS(N,M)
260 IF SCALAR THEN CADENA$ = CADENA$ + CHR$ (44 - SGN (SCALAR)) + MID$ ( STR$ ( ABS (SCALAR)) + "*",1,255 * ( ABS (SCALAR) < > 1)) + "E" + STR$ (M)
270 NEXT M
280 IF CADENA$ = "" THEN CADENA$ = "0"
290 IF LEFT$ (CADENA$,1) = "+" THEN CADENA$ = RIGHT$ (CADENA$, LEN (CADENA$) - 1)
300 PRINT CADENA$
310 NEXT N</syntaxhighlight>
==={{header|Chipmunk Basic}}===
{{trans|FreeBASIC}}
{{works with|Chipmunk Basic|3.6.4}}
<syntaxhighlight lang="vbnet">100 dim scalars(10,4)
110 scalars(1,1) = 1 : scalars(1,2) = 2 : scalars(1,3) = 3
120 scalars(2,1) = 0 : scalars(2,2) = 1 : scalars(2,3) = 2 : scalars(2,4) = 3
130 scalars(3,1) = 1 : scalars(3,2) = 0 : scalars(3,3) = 3 : scalars(3,4) = 4
140 scalars(4,1) = 1 : scalars(4,2) = 2 : scalars(4,3) = 0
150 scalars(5,1) = 0 : scalars(5,2) = 0 : scalars(5,3) = 0
160 scalars(6,1) = 0
170 scalars(7,1) = 1 : scalars(7,2) = 1 : scalars(7,3) = 1
180 scalars(8,1) = -1 : scalars(8,2) = -1 : scalars(8,3) = -1
190 scalars(9,1) = -1 : scalars(9,2) = -2 : scalars(9,3) = 0 : scalars(9,4) = -3
200 scalars(10,1) = -1
210 cls
220 for n = 1 to ubound(scalars)
230 cadena$ = ""
240 scalar = 0
250 for m = 1 to ubound(scalars,2)
260 scalar = scalars(n,m)
270 if scalar <> 0 then
280 if scalar = 1 then
290 cadena$ = cadena$+"+e"+str$(m)
300 else
310 if scalar = -1 then
320 cadena$ = cadena$+"-e"+str$(m)
330 else
340 if scalar > 0 then
350 cadena$ = cadena$+chr$(43)+str$(scalar)+"*e"+str$(m)
360 else
370 cadena$ = cadena$+str$(scalar)+"*e"+str$(m)
380 endif
390 endif
400 endif
410 endif
420 next m
430 if cadena$ = "" then cadena$ = "0"
440 if left$(cadena$,1) = "+" then cadena$ = right$(cadena$,len(cadena$)-1)
450 print cadena$
460 next n
470 end</syntaxhighlight>
 
==={{header|FreeBASIC}}===
{{trans|Ring}}
<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, -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</syntaxhighlight>
{{out}}
<pre>Same as Ring entry.</pre>
 
==={{header|GW-BASIC}}===
{{works with|PC-BASIC|any}}
{{works with|BASICA}}
{{works with|Chipmunk Basic}}
{{works with|QBasic}}
{{works with|MSX BASIC}}
<syntaxhighlight lang="qbasic">100 DIM SKLS(10, 4)
110 SKLS(1, 1) = 1: SKLS(1, 2) = 2: SKLS(1, 3) = 3
120 SKLS(2, 1) = 0: SKLS(2, 2) = 1: SKLS(2, 3) = 2: SKLS(2, 4) = 3
130 SKLS(3, 1) = 1: SKLS(3, 2) = 0: SKLS(3, 3) = 3: SKLS(3, 4) = 4
140 SKLS(4, 1) = 1: SKLS(4, 2) = 2: SKLS(4, 3) = 0
150 SKLS(5, 1) = 0: SKLS(5, 2) = 0: SKLS(5, 3) = 0
160 SKLS(6, 1) = 0
170 SKLS(7, 1) = 1: SKLS(7, 2) = 1: SKLS(7, 3) = 1
180 SKLS(8, 1) = -1: SKLS(8, 2) = -1: SKLS(8, 3) = -1
190 SKLS(9, 1) = -1: SKLS(9, 2) = -2: SKLS(9, 3) = 0: SKLS(9, 4) = -3
200 SKLS(10, 1) = -1
210 CLS
220 FOR N = 1 TO 10
230 CAD$ = ""
240 SCL = 0
250 FOR M = 1 TO 4
260 SCL = SKLS(N, M)
270 IF SCL <> 0 THEN IF SCL = 1 THEN CAD$ = CAD$ + "+e" + STR$(M) ELSE IF SCL = -1 THEN CAD$ = CAD$ + "-e" + STR$(M) ELSE IF SCL > 0 THEN CAD$ = CAD$ + CHR$(43) + STR$(SCL) + "*e" + STR$(M) ELSE CAD$ = CAD$ + STR$(SCL) + "*e" + STR$(M)
280 NEXT M
290 IF CAD$ = "" THEN CAD$ = "0"
300 IF LEFT$(CAD$, 1) = "+" THEN CAD$ = RIGHT$(CAD$, LEN(CAD$) - 1)
310 PRINT CAD$
320 NEXT N
330 END</syntaxhighlight>
 
==={{header|MSX Basic}}===
{{works with|MSX BASIC|any}}
The [[#GW-BASIC|GW-BASIC]] solution works without any changes.ht>
 
==={{header|QBasic}}===
{{trans|FreeBASIC}}
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
{{works with|QB64}}
<syntaxhighlight lang="qbasic">DIM scalars(1 TO 10, 1 TO 4)
scalars(1, 1) = 1: scalars(1, 2) = 2: scalars(1, 3) = 3
scalars(2, 1) = 0: scalars(2, 2) = 1: scalars(2, 3) = 2: scalars(2, 4) = 3
scalars(3, 1) = 1: scalars(3, 2) = 0: scalars(3, 3) = 3: scalars(3, 4) = 4
scalars(4, 1) = 1: scalars(4, 2) = 2: scalars(4, 3) = 0
scalars(5, 1) = 0: scalars(5, 2) = 0: scalars(5, 3) = 0
scalars(6, 1) = 0
scalars(7, 1) = 1: scalars(7, 2) = 1: scalars(7, 3) = 1
scalars(8, 1) = -1: scalars(8, 2) = -1: scalars(8, 3) = -1
scalars(9, 1) = -1: scalars(9, 2) = -2: scalars(9, 3) = 0: scalars(9, 4) = -3
scalars(10, 1) = -1
 
CLS
FOR n = 1 TO UBOUND(scalars)
cadena$ = ""
scalar = 0
FOR m = 1 TO UBOUND(scalars, 2)
scalar = scalars(n, m)
IF scalar <> 0 THEN
IF scalar = 1 THEN
cadena$ = cadena$ + "+e" + STR$(m)
ELSEIF scalar = -1 THEN
cadena$ = cadena$ + "-e" + STR$(m)
ELSE
IF scalar > 0 THEN
cadena$ = cadena$ + CHR$(43) + STR$(scalar) + "*e" + STR$(m)
ELSE
cadena$ = cadena$ + STR$(scalar) + "*e" + STR$(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
END</syntaxhighlight>
 
==={{header|Yabasic}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="vb">dim scalars(10,4)
scalars(1,1) = 1: scalars(1,2) = 2: scalars(1,3) = 3
scalars(2,1) = 0: scalars(2,2) = 1: scalars(2,3) = 2: scalars(2,4) = 3
scalars(3,1) = 1: scalars(3,2) = 0: scalars(3,3) = 3: scalars(3,4) = 4
scalars(4,1) = 1: scalars(4,2) = 2: scalars(4,3) = 0
scalars(5,1) = 0: scalars(5,2) = 0: scalars(5,3) = 0
scalars(6,1) = 0
scalars(7,1) = 1: scalars(7,2) = 1: scalars(7,3) = 1
scalars(8,1) = -1: scalars(8,2) = -1: scalars(8,3) = -1
scalars(9,1) = -1: scalars(9,2) = -2: scalars(9,3) = 0: scalars(9,4) = -3
scalars(10,1) = -1
 
for n = 1 to arraysize(scalars(),1)
cadena$ = ""
for m = 1 to arraysize(scalars(),2)
scalar = scalars(n, m)
if scalar <> 0 then
if scalar = 1 then
cadena$ = cadena$ + "+e" + str$(m)
else
if scalar = -1 then
cadena$ = cadena$ + "-e" + str$(m)
else
if scalar > 0 then
cadena$ = cadena$ + chr$(43) + str$(scalar) + "*e" + str$(m)
else
cadena$ = cadena$ + str$(scalar) + "*e" + str$(m)
fi
fi
fi
fi
next m
if cadena$ = "" cadena$ = "0"
if left$(cadena$, 1) = "+" cadena$ = right$(cadena$, len(cadena$)-1)
print cadena$
next n
end</syntaxhighlight>
 
=={{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.
<syntaxhighlight lang="c">
<lang C>
 
#include<stdlib.h>
Line 194 ⟶ 547:
return 0;
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 223 ⟶ 576:
=={{header|C sharp|C#}}==
{{trans|D}}
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Text;
Line 279 ⟶ 632:
}
}
}</langsyntaxhighlight>
{{out}}
<pre> [1, 2, 3] -> e(1) + 2*e(2) + 3*e(3)
Line 294 ⟶ 647:
=={{header|C++}}==
{{trans|D}}
<langsyntaxhighlight lang="cpp">#include <iomanip>
#include <iostream>
#include <sstream>
Line 374 ⟶ 727:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre> [1, 2, 3] -> e(1) + 2*e(2) + 3*e(3)
Line 386 ⟶ 739:
[-1, -2, 0, -3] -> -e(1) - 2*e(2) - 3*e(4)
[-1] -> -e(1)</pre>
 
=={{header|Cowgol}}==
<syntaxhighlight lang="cowgol">include "cowgol.coh";
 
sub abs(n: int32): (r: uint32) is
if n < 0
then r := (-n) as uint32;
else r := n as uint32;
end if;
end sub;
 
sub lincomb(scalar: [int32], size: intptr) is
var first: uint8 := 1;
var item: uint8 := 1;
 
sub print_sign() is
if first == 1 then
if [scalar] < 0 then print("-"); end if;
else
if [scalar] < 0
then print(" - ");
else print(" + ");
end if;
end if;
end sub;
 
sub print_term() is
if [scalar] == 0 then return; end if;
print_sign();
if abs([scalar]) > 1 then
print_i32(abs([scalar]));
print("*");
end if;
print("e(");
print_i8(item);
print(")");
first := 0;
end sub;
 
while size > 0 loop
print_term();
scalar := @next scalar;
size := size - 1;
item := item + 1;
end loop;
 
if first == 1 then
print("0");
end if;
print_nl();
end sub;
 
var a1: int32[] := {1, 2, 3}; lincomb(&a1[0], @sizeof a1);
var a2: int32[] := {0, 1, 2, 3}; lincomb(&a2[0], @sizeof a2);
var a3: int32[] := {1, 0, 3, 4}; lincomb(&a3[0], @sizeof a3);
var a4: int32[] := {1, 2, 0}; lincomb(&a4[0], @sizeof a4);
var a5: int32[] := {0, 0, 0}; lincomb(&a5[0], @sizeof a5);
var a6: int32[] := {0}; lincomb(&a6[0], @sizeof a6);
var a7: int32[] := {1, 1, 1}; lincomb(&a7[0], @sizeof a7);
var a8: int32[] := {-1, -1, -1}; lincomb(&a8[0], @sizeof a8);
var a9: int32[] := {-1, -2, 0, 3}; lincomb(&a9[0], @sizeof a9);
var a10: int32[] := {-1}; lincomb(&a10[0], @sizeof a10);</syntaxhighlight>
{{out}}
<pre>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)</pre>
 
=={{header|D}}==
{{trans|Kotlin}}
<langsyntaxhighlight Dlang="d">import std.array;
import std.conv;
import std.format;
Line 441 ⟶ 867:
writefln("%-15s -> %s", arr, linearCombo(c));
}
}</langsyntaxhighlight>
{{out}}
<pre>[1, 2, 3] -> e(1) + 2*e(2) + 3*e(3)
Line 453 ⟶ 879:
[-1, -2, 0, -3] -> -e(1) - 2*e(2) - 3*e(4)
[-1] -> -e(1)</pre>
 
=={{header|Draco}}==
<syntaxhighlight lang="draco">proc abs(int n) int: if n<0 then -n else n fi corp
 
proc write_term(word index; int scalar; bool first) void:
if first then
if scalar<0 then write("-") fi
else
write(if scalar<0 then " - " else " + " fi)
fi;
if abs(scalar)>1 then
write(abs(scalar), '*')
fi;
write("e(",index,")")
corp
 
proc lincomb([*]int terms) void:
bool first;
word index;
first := true;
 
for index from 0 upto dim(terms,1)-1 do
if terms[index] /= 0 then
write_term(index+1, terms[index], first);
first := false
fi
od;
 
writeln(if first then "0" else "" fi)
corp
 
proc main() void:
[3]int a1 = (1,2,3);
[4]int a2 = (0,1,2,3);
[4]int a3 = (1,0,3,4);
[3]int a4 = (1,2,0);
[3]int a5 = (0,0,0);
[1]int a6 = (0);
[3]int a7 = (1,1,1);
[3]int a8 = (-1,-1,-1);
[4]int a9 = (-1,-2,0,3);
[1]int a10 = (-1);
lincomb(a1); lincomb(a2); lincomb(a3); lincomb(a4);
lincomb(a5); lincomb(a6); lincomb(a7); lincomb(a8);
lincomb(a9); lincomb(a10)
corp</syntaxhighlight>
{{out}}
<pre>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)</pre>
=={{header|EasyLang}}==
{{trans|Ring}}
<syntaxhighlight>
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$ &= "+e" & m
elif scalar = -1
str$ &= "-e" & m
else
if scalar > 0
str$ &= strchar 43 & scalar & "*e" & m
else
str$ = scalar & "*e" & m
.
.
.
.
if str$ = ""
str$ = 0
.
if substr str$ 1 1 = "+"
str$ = substr str$ 2 (len str$ - 1)
.
print str$
.
</syntaxhighlight>
{{out}}
<pre>
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
-3*e4
-e1
</pre>
 
=={{header|EchoLisp}}==
<langsyntaxhighlight lang="scheme">
;; build an html string from list of coeffs
 
Line 488 ⟶ 1,015:
(for/string ((linear linears))
(format "%a -> <span style='color:blue'>%a</span> <br>" linear (linear->html linear)))))
</syntaxhighlight>
</lang>
{{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>
Line 494 ⟶ 1,021:
=={{header|Elixir}}==
{{works with|Elixir|1.3}}
<langsyntaxhighlight lang="elixir">defmodule Linear_combination do
def display(coeff) do
Enum.with_index(coeff)
Line 525 ⟶ 1,052:
[-1]
]
Enum.each(coeffs, &Linear_combination.display(&1))</langsyntaxhighlight>
 
{{out}}
Line 543 ⟶ 1,070:
=={{header|F_Sharp|F#}}==
===The function===
<langsyntaxhighlight lang="fsharp">
// Display a linear combination. Nigel Galloway: March 28th., 2018
let fN g =
Line 559 ⟶ 1,086:
|_ -> printfn "0"
fN 1 g
</syntaxhighlight>
</lang>
 
===The Task===
<langsyntaxhighlight lang="fsharp">
fN [1;2;3]
</syntaxhighlight>
</lang>
{{out}}
<pre>
e(1)+2e(2)+3e(3)
</pre>
<langsyntaxhighlight lang="fsharp">
fN [0;1;2;3]
</syntaxhighlight>
</lang>
{{out}}
<pre>
e(2)+2e(3)+3e(4)
</pre>
<langsyntaxhighlight lang="fsharp">
fN[1;0;3;4]
</syntaxhighlight>
</lang>
{{out}}
<pre>
e(1)+3e(3)+4e(4)
</pre>
<langsyntaxhighlight lang="fsharp">
fN[1;2;0]
</syntaxhighlight>
</lang>
{{out}}
<pre>
e(1)+2e(2)
</pre>
<langsyntaxhighlight lang="fsharp">
fN[0;0;0]
</syntaxhighlight>
</lang>
{{out}}
<pre>
0
</pre>
<langsyntaxhighlight lang="fsharp">
fN[0]
</syntaxhighlight>
</lang>
{{out}}
<pre>
0
</pre>
<langsyntaxhighlight lang="fsharp">
fN[1;1;1]
</syntaxhighlight>
</lang>
{{out}}
<pre>
e(1)+e(2)+e(3)
</pre>
<langsyntaxhighlight lang="fsharp">
fN[-1;-1;-1]
</syntaxhighlight>
</lang>
{{out}}
<pre>
-e(1)-e(2)-e(3)
</pre>
<langsyntaxhighlight lang="fsharp">
fN[-1;-2;0;-3]
</syntaxhighlight>
</lang>
{{out}}
<pre>
-e(1)-2e(2)-3e(4)
</pre>
<langsyntaxhighlight lang="fsharp">
fN[1]
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 634 ⟶ 1,161:
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: formatting kernel match math pair-rocket regexp sequences ;
 
MATCH-VARS: ?a ?b ;
Line 652 ⟶ 1,179:
{ { 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</langsyntaxhighlight>
{{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)
{ 0 1 2 3 } -> e(2) + 2*e(3) + 3*e(4)
{ 1 0 3 4 } -> e(1) + 3*e(3) + 4*e(4)
Line 664 ⟶ 1,190:
{ -1 -1 -1 } -> -e(1) - e(2) - e(3)
{ -1 -2 0 -3 } -> -e(1) - 2*e(2) - 3*e(4)
{ -1 } -> -e(1)</pre>
</pre>
 
 
=={{header|FreeBASIC}}==
{{trans|Ring}}
<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, -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</lang>
{{out}}
<pre>
Igual que la entrada de Ring.
</pre>
 
=={{header|Go}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 763 ⟶ 1,252:
fmt.Printf("%-15s -> %s\n", t, linearCombo(c))
}
}</langsyntaxhighlight>
 
{{out}}
Line 781 ⟶ 1,270:
=={{header|Groovy}}==
{{trans|Java}}
<langsyntaxhighlight lang="groovy">class LinearCombination {
private static String linearCombo(int[] c) {
StringBuilder sb = new StringBuilder()
Line 824 ⟶ 1,313:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>[1, 2, 3] -> e(1) + 2*e(2) + 3*e(3)
Line 838 ⟶ 1,327:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Text.Printf (printf)
 
linearForm :: [Int] -> String
Line 853 ⟶ 1,342:
'+':s -> s
"" -> "0"
s -> s</langsyntaxhighlight>
 
Testing
 
<langsyntaxhighlight lang="haskell">coeffs :: [[Int]]
coeffs = [ [1, 2, 3]
, [0, 1, 2, 3]
Line 867 ⟶ 1,356:
, [-1, -1, -1]
, [-1, -2, 0, -3]
, [-1] ]</langsyntaxhighlight>
 
<pre>λ> mapM_ (print . linearForm) coeffs
Line 885 ⟶ 1,374:
Implementation:
 
<langsyntaxhighlight Jlang="j">fourbanger=:3 :0
e=. ('e(',')',~])@":&.> 1+i.#y
firstpos=. 0< {.y-.0
Line 898 ⟶ 1,387:
case. do. pfx,(":|x),'*',y
end.
)</langsyntaxhighlight>
 
Example use:
 
<langsyntaxhighlight Jlang="j"> fourbanger 1 2 3
e(1)+2*e(2)+3*e(3)
fourbanger 0 1 2 3
Line 919 ⟶ 1,408:
-e(1)-2*e(2)-3*e(4)
fourbanger _1
-e(1)</langsyntaxhighlight>
 
=={{header|Java}}==
{{trans|Kotlin}}
<langsyntaxhighlight Javalang="java">import java.util.Arrays;
 
public class LinearCombination {
Line 967 ⟶ 1,456:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>[1, 2, 3] -> e(1) + 2*e(2) + 3*e(3)
Line 981 ⟶ 1,470:
 
=={{header|jq}}==
<langsyntaxhighlight lang="jq">def linearCombo:
reduce to_entries[] as {key: $k,value: $v} ("";
if $v == 0 then .
Line 1,010 ⟶ 1,499:
[-1]
| "\(lpad(15)) => \(linearCombo)"
</syntaxhighlight>
</lang>
{{out}}
<langsyntaxhighlight lang="sh"> [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
Line 1,021 ⟶ 1,510:
[-1,-1,-1] => -e0 - e1 - e2
[-1,-2,0,-3] => -e0 - 2*e1 - 3*e3
[-1] => -e0</langsyntaxhighlight>
=={{header|Julia}}==
<langsyntaxhighlight lang="julia"># v0.6
 
linearcombination(coef::Array) = join(collect("$c * e($i)" for (i, c) in enumerate(coef) if c != 0), " + ")
Line 1,030 ⟶ 1,519:
[-1, -1, -1], [-1, -2, 0, -3], [-1]]
@printf("%20s -> %s\n", c, linearcombination(c))
end</langsyntaxhighlight>
 
{{out}}
Line 1,045 ⟶ 1,534:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.1.2
 
fun linearCombo(c: IntArray): String {
Line 1,080 ⟶ 1,569:
println("${c.contentToString().padEnd(15)} -> ${linearCombo(c)}")
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,097 ⟶ 1,586:
 
=={{header|Lambdatalk}}==
<langsyntaxhighlight lang="scheme">
 
{def linearcomb
Line 1,127 ⟶ 1,616:
{linearcomb -1} -> - e(1)
 
</syntaxhighlight>
</lang>
 
=={{header|Lua}}==
{{trans|C#}}
<langsyntaxhighlight lang="lua">function t2s(t)
local s = "["
for i,v in pairs(t) do
Line 1,196 ⟶ 1,685:
end
 
main()</langsyntaxhighlight>
{{out}}
<pre> [1, 2, 3] -> e(1) + 2*e(2) + 3*e(3)
Line 1,211 ⟶ 1,700:
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="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]</langsyntaxhighlight>
{{out}}
<pre>e(1)+2e(2)+3e(3)
Line 1,226 ⟶ 1,715:
 
=={{header|Modula-2}}==
<langsyntaxhighlight lang="modula2">MODULE Linear;
FROM FormatString IMPORT FormatString;
FROM Terminal IMPORT WriteString,WriteLn,ReadChar;
Line 1,292 ⟶ 1,781:
 
ReadChar
END Linear.</langsyntaxhighlight>
 
=={{header|Nim}}==
{{trans|Kotlin}}
<langsyntaxhighlight Nimlang="nim">import strformat
 
proc linearCombo(c: openArray[int]): string =
Line 1,324 ⟶ 1,813:
 
for c in Combos:
echo fmt"{($c)[1..^1]:15} → {linearCombo(c)}"</langsyntaxhighlight>
 
{{out}}
Line 1,337 ⟶ 1,826:
[-1, -2, 0, -3] → -e(1) - 2*e(2) - 3*e(4)
[-1] → -e(1)</pre>
 
=={{header|OCaml}}==
<syntaxhighlight lang="ocaml">let fmt_linear_comb =
let rec head e = function
| 0 :: t -> head (succ e) t
| 1 :: t -> Printf.sprintf "e(%u)%s" e (tail (succ e) t)
| -1 :: t -> Printf.sprintf "-e(%u)%s" e (tail (succ e) t)
| a :: t -> Printf.sprintf "%d*e(%u)%s" a e (tail (succ e) t)
| _ -> "0"
and tail e = function
| 0 :: t -> tail (succ e) t
| 1 :: t -> Printf.sprintf " + e(%u)%s" e (tail (succ e) t)
| -1 :: t -> Printf.sprintf " - e(%u)%s" e (tail (succ e) t)
| a :: t when a < 0 -> Printf.sprintf " - %u*e(%u)%s" (-a) e (tail (succ e) t)
| a :: t -> Printf.sprintf " + %u*e(%u)%s" a e (tail (succ e) t)
| _ -> ""
in
head 1
 
let () =
List.iter (fun v -> print_endline (fmt_linear_comb v)) [
[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}}
<pre>
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)
</pre>
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 1,354 ⟶ 1,887:
 
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 ]</langsyntaxhighlight>
{{out}}
<pre>e(1) + 2*e(2) + 3*e(3)
Line 1,369 ⟶ 1,902:
=={{header|Phix}}==
{{trans|Tcl}}
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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>
Line 1,406 ⟶ 1,939:
<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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,422 ⟶ 1,955:
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">; Process and output values.
Procedure WriteLinear(Array c.i(1))
Define buf$,
Line 1,527 ⟶ 2,060:
Data.i -1
_V10:
EndDataSection</langsyntaxhighlight>
{{out}}
<pre>
Line 1,543 ⟶ 2,076:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">
def linear(x):
return ' + '.join(['{}e({})'.format('-' if v == -1 else '' if v == 1 else str(v) + '*', i + 1)
Line 1,550 ⟶ 2,083:
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]]))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,566 ⟶ 2,099:
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">#lang racket/base
(require racket/match racket/string)
 
Line 1,599 ⟶ 2,132:
(-1 -2 0 -3)
(-1)))
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,615 ⟶ 2,148:
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" perl6line>sub linear-combination(@coeff) {
(@coeff Z=> map { "e($_)" }, 1 .. *)
.grep(+*.key)
Line 1,636 ⟶ 2,169:
[-1, -2, 0, -3],
[-1 ]
;</langsyntaxhighlight>
{{out}}
<pre>e(1) + 2*e(2) + 3*e(3)
Line 1,650 ⟶ 2,183:
 
=={{header|REXX}}==
<langsyntaxhighlight 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*/
@.2 = ' 0, 1, 2, 3 ' /* " " " " " " " */
Line 1,676 ⟶ 2,209:
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. */</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 1,692 ⟶ 2,225:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Display a linear combination
 
Line 1,722 ⟶ 2,255:
see str + nl
next
</syntaxhighlight>
</lang>
Output:
<pre>
Line 1,735 ⟶ 2,268:
-e1-2*e2-3*e4
-e1
</pre>
 
=={{header|RPL}}==
RPL can handle both stack-based program flows and algebraic expressions, which is quite useful for tasks such as this one.
{{works with|Halcyon Calc|4.2.7}}
===Straightforward approach===
This version has the disadvantage of sometimes interchanging some terms when simplifying the expression by the COLCT function.
≪ → scalars
≪ '0' 1 scalars SIZE FOR j
scalars j GET
"e" j →STR + STR→ * +
NEXT
COLCT COLCT
≫ ≫
'COMB→' STO
===Full-compliant version===
The constant π is here simply used to facilitate the construction of the algebraic expression; it is then eliminated during the conversion into a string.
≪ → scalars
≪ "" 1 scalars SIZE FOR j
'π' scalars j GET "e" j →STR + STR→ * + →STR
OVER SIZE NOT OVER 3 3 SUB "+" AND 4 3 IFTE
OVER SIZE 1 - SUB +
NEXT
IF DUP "" == THEN DROP "0" END
'COMB→' STO
 
≪ { { 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 3 PICK SIZE FOR j
OVER j GET COMB→ +
NEXT
SWAP DROP
≫ EVAL
{{out}}
<pre>
1: { "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" }
</pre>
 
=={{header|Ruby}}==
{{trans|D}}
<langsyntaxhighlight lang="ruby">def linearCombo(c)
sb = ""
c.each_with_index { |n, i|
Line 1,793 ⟶ 2,363:
end
 
main()</langsyntaxhighlight>
{{out}}
<pre>[1, 2, 3] -> e(1) + 2*e(2) + 3*e(3)
Line 1,807 ⟶ 2,377:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">
use std::fmt::{Display, Formatter, Result};
use std::process::exit;
Line 1,899 ⟶ 2,469:
println!("{}", linear_combination(&coefficients));
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,906 ⟶ 2,476:
 
=={{header|Scala}}==
<langsyntaxhighlight Scalalang="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),
Line 1,928 ⟶ 2,498:
println(f"${c.mkString("[", ", ", "]")}%-15s -> ${linearCombo(c)}%s")
}
}</langsyntaxhighlight>
 
=={{header|Sidef}}==
{{trans|Tcl}}
<langsyntaxhighlight lang="ruby">func linear_combination(coeffs) {
var res = ""
for e,f in (coeffs.kv) {
Line 1,969 ⟶ 2,539:
tests.each { |t|
printf("%10s -> %-10s\n", t.join(' '), linear_combination(t))
}</langsyntaxhighlight>
{{out}}
<pre> 1 2 3 -> e(1)+2*e(2)+3*e(3)
Line 1,985 ⟶ 2,555:
This solution strives for legibility rather than golf.
 
<langsyntaxhighlight Tcllang="tcl">proc lincom {factors} {
set exp 0
set res ""
Line 2,020 ⟶ 2,590:
} {
puts [format "%10s -> %-10s" $test [lincom $test]]
}</langsyntaxhighlight>
 
{{out}}
Line 2,036 ⟶ 2,606:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<langsyntaxhighlight lang="vbnet">Imports System.Text
 
Module Module1
Line 2,090 ⟶ 2,660:
End Sub
 
End Module</langsyntaxhighlight>
{{out}}
<pre> [1, 2, 3] -> e(1) + 2*e(2) + 3*e(3)
Line 2,102 ⟶ 2,672:
[-1, -2, 0, -3] -> -e(1) - 2*e(2) - 3*e(4)
[-1] -> -e(1)</pre>
 
=={{header|V (Vlang)}}==
{{trans|Go}}
<syntaxhighlight lang="v (vlang)">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)}")
}
}</syntaxhighlight>
 
{{out}}
<pre>
[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)
</pre>
 
=={{header|Wren}}==
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./fmt" for Fmt
 
var linearCombo = Fn.new { |c|
Line 2,139 ⟶ 2,783:
for (c in combos) {
Fmt.print("$-15s -> $s", c.toString, linearCombo.call(c))
}</langsyntaxhighlight>
 
{{out}}
Line 2,157 ⟶ 2,801:
=={{header|zkl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="zkl">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
}</langsyntaxhighlight>
<langsyntaxhighlight 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)
.pump(Console.println,linearCombination);</langsyntaxhighlight>
{{out}}
<pre>
413

edits