Display a linear combination: Difference between revisions
Content added Content deleted
No edit summary |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 38: | Line 38: | ||
=={{header|11l}}== |
=={{header|11l}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<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))</ |
print(linear(x))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 59: | Line 59: | ||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
< |
<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;</ |
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}} |
||
< |
<syntaxhighlight lang="csharp">using System; |
||
using System.Collections.Generic; |
using System.Collections.Generic; |
||
using System.Text; |
using System.Text; |
||
Line 279: | Line 279: | ||
} |
} |
||
} |
} |
||
}</ |
}</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}} |
||
< |
<syntaxhighlight lang="cpp">#include <iomanip> |
||
#include <iostream> |
#include <iostream> |
||
#include <sstream> |
#include <sstream> |
||
Line 374: | Line 374: | ||
return 0; |
return 0; |
||
}</ |
}</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}} |
||
< |
<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)); |
||
} |
} |
||
}</ |
}</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}}== |
||
< |
<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}} |
||
< |
<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))</ |
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=== |
||
< |
<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=== |
||
< |
<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> |
||
< |
<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> |
||
< |
<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> |
||
< |
<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> |
||
< |
<syntaxhighlight lang="fsharp"> |
||
fN[0;0;0] |
fN[0;0;0] |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
0 |
0 |
||
</pre> |
</pre> |
||
< |
<syntaxhighlight lang="fsharp"> |
||
fN[0] |
fN[0] |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
0 |
0 |
||
</pre> |
</pre> |
||
< |
<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> |
||
< |
<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> |
||
< |
<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> |
||
< |
<syntaxhighlight lang="fsharp"> |
||
fN[1] |
fN[1] |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 634: | Line 634: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
< |
<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</ |
[ 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}} |
||
< |
<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</ |
Sleep</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 705: | Line 705: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
< |
<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)) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 781: | Line 781: | ||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<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: | ||
} |
} |
||
} |
} |
||
}</ |
}</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}}== |
||
< |
<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</ |
s -> s</syntaxhighlight> |
||
Testing |
Testing |
||
< |
<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] ]</ |
, [-1] ]</syntaxhighlight> |
||
<pre>λ> mapM_ (print . linearForm) coeffs |
<pre>λ> mapM_ (print . linearForm) coeffs |
||
Line 885: | Line 885: | ||
Implementation: |
Implementation: |
||
< |
<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. |
||
)</ |
)</syntaxhighlight> |
||
Example use: |
Example use: |
||
< |
<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)</ |
-e(1)</syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
< |
<syntaxhighlight lang="java">import java.util.Arrays; |
||
public class LinearCombination { |
public class LinearCombination { |
||
Line 967: | Line 967: | ||
} |
} |
||
} |
} |
||
}</ |
}</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}}== |
||
< |
<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}} |
||
< |
<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</ |
[-1] => -e0</syntaxhighlight> |
||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
< |
<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</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,045: | Line 1,045: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
< |
<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)}") |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,097: | Line 1,097: | ||
=={{header|Lambdatalk}}== |
=={{header|Lambdatalk}}== |
||
< |
<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#}} |
||
< |
<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()</ |
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}}== |
||
< |
<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]</ |
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}}== |
||
< |
<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.</ |
END Linear.</syntaxhighlight> |
||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
< |
<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)}"</ |
echo fmt"{($c)[1..^1]:15} → {linearCombo(c)}"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,339: | Line 1,339: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
< |
<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 ]</ |
[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}} |
||
<!--< |
<!--<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 -> %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 -> %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> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,422: | Line 1,422: | ||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
< |
<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</ |
EndDataSection</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,543: | Line 1,543: | ||
=={{header|Python}}== |
=={{header|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}}== |
||
< |
<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 |
<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 ] |
||
;</ |
;</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}}== |
||
< |
<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. */</ |
end /*j*/ /*stick a fork in it, we're all done. */</syntaxhighlight> |
||
{{out|output|text= when using the default inputs:}} |
{{out|output|text= when using the default inputs:}} |
||
<pre> |
<pre> |
||
Line 1,692: | Line 1,692: | ||
=={{header|Ring}}== |
=={{header|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}} |
||
< |
<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()</ |
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}}== |
||
< |
<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}}== |
||
< |
<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") |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
{{trans|Tcl}} |
{{trans|Tcl}} |
||
< |
<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)) |
||
}</ |
}</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. |
||
< |
<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]] |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,036: | Line 2,036: | ||
=={{header|Visual Basic .NET}}== |
=={{header|Visual Basic .NET}}== |
||
{{trans|C#}} |
{{trans|C#}} |
||
< |
<syntaxhighlight lang="vbnet">Imports System.Text |
||
Module Module1 |
Module Module1 |
||
Line 2,090: | Line 2,090: | ||
End Sub |
End Sub |
||
End Module</ |
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}} |
||
< |
<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)}") |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,180: | Line 2,180: | ||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
{{libheader|Wren-fmt}} |
{{libheader|Wren-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)) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,231: | Line 2,231: | ||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
{{trans|Raku}} |
{{trans|Raku}} |
||
< |
<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 |
||
}</ |
}</syntaxhighlight> |
||
< |
<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);</ |
.pump(Console.println,linearCombination);</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |