Display a linear combination: Difference between revisions

Applesoft BASIC
(Added various BASIC dialects (Chipmunk Basic, GW-BASIC, MSX Basic, QBasic and Yabasic))
(Applesoft BASIC)
 
(4 intermediate revisions by 2 users not shown)
Line 191:
</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}}==
 
Line 235 ⟶ 260:
 
=={{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}}
Line 276 ⟶ 325:
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}}===
Line 657 ⟶ 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}}==
Line 725 ⟶ 880:
[-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}}
Line 970 ⟶ 1,181:
[ dup linear-combo "%-14u -> %s\n" printf ] each</syntaxhighlight>
{{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 980 ⟶ 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}}
<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>
Igual que la entrada de Ring.
</pre>
 
=={{header|Go}}==
413

edits