Display a linear combination: Difference between revisions
m
syntax highlighting fixup automation
No edit summary |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 38:
=={{header|11l}}==
{{trans|Python}}
<
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))</
{{out}}
<pre>
Line 59:
=={{header|Ada}}==
<
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;</
{{out}}
<pre>e(1) + 2*e(2) + 3*e(3)
Line 127:
=={{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">
#include<stdlib.h>
Line 194:
return 0;
}
</syntaxhighlight>
{{out}}
<pre>
Line 223:
=={{header|C sharp|C#}}==
{{trans|D}}
<
using System.Collections.Generic;
using System.Text;
Line 279:
}
}
}</
{{out}}
<pre> [1, 2, 3] -> e(1) + 2*e(2) + 3*e(3)
Line 294:
=={{header|C++}}==
{{trans|D}}
<
#include <iostream>
#include <sstream>
Line 374:
return 0;
}</
{{out}}
<pre> [1, 2, 3] -> e(1) + 2*e(2) + 3*e(3)
Line 389:
=={{header|D}}==
{{trans|Kotlin}}
<
import std.conv;
import std.format;
Line 441:
writefln("%-15s -> %s", arr, linearCombo(c));
}
}</
{{out}}
<pre>[1, 2, 3] -> e(1) + 2*e(2) + 3*e(3)
Line 455:
=={{header|EchoLisp}}==
<
;; build an html string from list of coeffs
Line 488:
(for/string ((linear linears))
(format "%a -> <span style='color:blue'>%a</span> <br>" linear (linear->html linear)))))
</syntaxhighlight>
{{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:
=={{header|Elixir}}==
{{works with|Elixir|1.3}}
<
def display(coeff) do
Enum.with_index(coeff)
Line 525:
[-1]
]
Enum.each(coeffs, &Linear_combination.display(&1))</
{{out}}
Line 543:
=={{header|F_Sharp|F#}}==
===The function===
<
// Display a linear combination. Nigel Galloway: March 28th., 2018
let fN g =
Line 559:
|_ -> printfn "0"
fN 1 g
</syntaxhighlight>
===The Task===
<
fN [1;2;3]
</syntaxhighlight>
{{out}}
<pre>
e(1)+2e(2)+3e(3)
</pre>
<
fN [0;1;2;3]
</syntaxhighlight>
{{out}}
<pre>
e(2)+2e(3)+3e(4)
</pre>
<
fN[1;0;3;4]
</syntaxhighlight>
{{out}}
<pre>
e(1)+3e(3)+4e(4)
</pre>
<
fN[1;2;0]
</syntaxhighlight>
{{out}}
<pre>
e(1)+2e(2)
</pre>
<
fN[0;0;0]
</syntaxhighlight>
{{out}}
<pre>
0
</pre>
<
fN[0]
</syntaxhighlight>
{{out}}
<pre>
0
</pre>
<
fN[1;1;1]
</syntaxhighlight>
{{out}}
<pre>
e(1)+e(2)+e(3)
</pre>
<
fN[-1;-1;-1]
</syntaxhighlight>
{{out}}
<pre>
-e(1)-e(2)-e(3)
</pre>
<
fN[-1;-2;0;-3]
</syntaxhighlight>
{{out}}
<pre>
-e(1)-2e(2)-3e(4)
</pre>
<
fN[1]
</syntaxhighlight>
{{out}}
<pre>
Line 634:
=={{header|Factor}}==
<
MATCH-VARS: ?a ?b ;
Line 652:
{ { 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</
{{out}}
<pre>
Line 670:
=={{header|FreeBASIC}}==
{{trans|Ring}}
<
{1, 0, 3, 4}, {1, 2, 0}, {0, 0, 0}, {0}, {1, 1, 1}, {-1, -1, -1}, _
{-1, -2, 0, -3}, {-1}}
Line 697:
Print cadena
Next n
Sleep</
{{out}}
<pre>
Line 705:
=={{header|Go}}==
{{trans|Kotlin}}
<
import (
Line 763:
fmt.Printf("%-15s -> %s\n", t, linearCombo(c))
}
}</
{{out}}
Line 781:
=={{header|Groovy}}==
{{trans|Java}}
<
private static String linearCombo(int[] c) {
StringBuilder sb = new StringBuilder()
Line 824:
}
}
}</
{{out}}
<pre>[1, 2, 3] -> e(1) + 2*e(2) + 3*e(3)
Line 838:
=={{header|Haskell}}==
<
linearForm :: [Int] -> String
Line 853:
'+':s -> s
"" -> "0"
s -> s</
Testing
<
coeffs = [ [1, 2, 3]
, [0, 1, 2, 3]
Line 867:
, [-1, -1, -1]
, [-1, -2, 0, -3]
, [-1] ]</
<pre>λ> mapM_ (print . linearForm) coeffs
Line 885:
Implementation:
<
e=. ('e(',')',~])@":&.> 1+i.#y
firstpos=. 0< {.y-.0
Line 898:
case. do. pfx,(":|x),'*',y
end.
)</
Example use:
<
e(1)+2*e(2)+3*e(3)
fourbanger 0 1 2 3
Line 919:
-e(1)-2*e(2)-3*e(4)
fourbanger _1
-e(1)</
=={{header|Java}}==
{{trans|Kotlin}}
<
public class LinearCombination {
Line 967:
}
}
}</
{{out}}
<pre>[1, 2, 3] -> e(1) + 2*e(2) + 3*e(3)
Line 981:
=={{header|jq}}==
<
reduce to_entries[] as {key: $k,value: $v} ("";
if $v == 0 then .
Line 1,010:
[-1]
| "\(lpad(15)) => \(linearCombo)"
</syntaxhighlight>
{{out}}
<
[0,1,2,3] => e1 + 2*e2 + 3*e3
[1,0,3,4] => e0 + 3*e2 + 4*e3
Line 1,021:
[-1,-1,-1] => -e0 - e1 - e2
[-1,-2,0,-3] => -e0 - 2*e1 - 3*e3
[-1] => -e0</
=={{header|Julia}}==
<
linearcombination(coef::Array) = join(collect("$c * e($i)" for (i, c) in enumerate(coef) if c != 0), " + ")
Line 1,030:
[-1, -1, -1], [-1, -2, 0, -3], [-1]]
@printf("%20s -> %s\n", c, linearcombination(c))
end</
{{out}}
Line 1,045:
=={{header|Kotlin}}==
<
fun linearCombo(c: IntArray): String {
Line 1,080:
println("${c.contentToString().padEnd(15)} -> ${linearCombo(c)}")
}
}</
{{out}}
Line 1,097:
=={{header|Lambdatalk}}==
<
{def linearcomb
Line 1,127:
{linearcomb -1} -> - e(1)
</syntaxhighlight>
=={{header|Lua}}==
{{trans|C#}}
<
local s = "["
for i,v in pairs(t) do
Line 1,196:
end
main()</
{{out}}
<pre> [1, 2, 3] -> e(1) + 2*e(2) + 3*e(3)
Line 1,211:
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<
Column[TraditionalForm[Total[MapIndexed[#1 e[#2[[1]]] &, #]]] & /@ tests]</
{{out}}
<pre>e(1)+2e(2)+3e(3)
Line 1,226:
=={{header|Modula-2}}==
<
FROM FormatString IMPORT FormatString;
FROM Terminal IMPORT WriteString,WriteLn,ReadChar;
Line 1,292:
ReadChar
END Linear.</
=={{header|Nim}}==
{{trans|Kotlin}}
<
proc linearCombo(c: openArray[int]): string =
Line 1,324:
for c in Combos:
echo fmt"{($c)[1..^1]:15} → {linearCombo(c)}"</
{{out}}
Line 1,339:
=={{header|Perl}}==
<
use warnings;
use feature 'say';
Line 1,354:
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 ]</
{{out}}
<pre>e(1) + 2*e(2) + 3*e(3)
Line 1,369:
=={{header|Phix}}==
{{trans|Tcl}}
<!--<
<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:
<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>
<!--</
{{out}}
<pre>
Line 1,422:
=={{header|PureBasic}}==
<
Procedure WriteLinear(Array c.i(1))
Define buf$,
Line 1,527:
Data.i -1
_V10:
EndDataSection</
{{out}}
<pre>
Line 1,543:
=={{header|Python}}==
<
def linear(x):
return ' + '.join(['{}e({})'.format('-' if v == -1 else '' if v == 1 else str(v) + '*', i + 1)
Line 1,550:
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>
{{out}}
<pre>
Line 1,566:
=={{header|Racket}}==
<
(require racket/match racket/string)
Line 1,599:
(-1 -2 0 -3)
(-1)))
</syntaxhighlight>
{{out}}
Line 1,615:
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku"
(@coeff Z=> map { "e($_)" }, 1 .. *)
.grep(+*.key)
Line 1,636:
[-1, -2, 0, -3],
[-1 ]
;</
{{out}}
<pre>e(1) + 2*e(2) + 3*e(3)
Line 1,650:
=={{header|REXX}}==
<
@.= .; @.1 = ' 1, 2, 3 ' /*define a specific test case for build*/
@.2 = ' 0, 1, 2, 3 ' /* " " " " " " " */
Line 1,676:
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. */</
{{out|output|text= when using the default inputs:}}
<pre>
Line 1,692:
=={{header|Ring}}==
<
# Project : Display a linear combination
Line 1,722:
see str + nl
next
</syntaxhighlight>
Output:
<pre>
Line 1,739:
=={{header|Ruby}}==
{{trans|D}}
<
sb = ""
c.each_with_index { |n, i|
Line 1,793:
end
main()</
{{out}}
<pre>[1, 2, 3] -> e(1) + 2*e(2) + 3*e(3)
Line 1,807:
=={{header|Rust}}==
<
use std::fmt::{Display, Formatter, Result};
use std::process::exit;
Line 1,899:
println!("{}", linear_combination(&coefficients));
}
</syntaxhighlight>
{{out}}
<pre>
Line 1,906:
=={{header|Scala}}==
<
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:
println(f"${c.mkString("[", ", ", "]")}%-15s -> ${linearCombo(c)}%s")
}
}</
=={{header|Sidef}}==
{{trans|Tcl}}
<
var res = ""
for e,f in (coeffs.kv) {
Line 1,969:
tests.each { |t|
printf("%10s -> %-10s\n", t.join(' '), linear_combination(t))
}</
{{out}}
<pre> 1 2 3 -> e(1)+2*e(2)+3*e(3)
Line 1,985:
This solution strives for legibility rather than golf.
<
set exp 0
set res ""
Line 2,020:
} {
puts [format "%10s -> %-10s" $test [lincom $test]]
}</
{{out}}
Line 2,036:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<
Module Module1
Line 2,090:
End Sub
End Module</
{{out}}
<pre> [1, 2, 3] -> e(1) + 2*e(2) + 3*e(3)
Line 2,105:
=={{header|Vlang}}==
{{trans|Go}}
<
fn linear_combo(c []int) string {
Line 2,161:
println("${c:-15} -> ${linear_combo(c)}")
}
}</
{{out}}
Line 2,180:
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
<
var linearCombo = Fn.new { |c|
Line 2,213:
for (c in combos) {
Fmt.print("$-15s -> $s", c.toString, linearCombo.call(c))
}</
{{out}}
Line 2,231:
=={{header|zkl}}==
{{trans|Raku}}
<
[1..].zipWith(fcn(n,c){ if(c==0) "" else "%s*e(%s)".fmt(c,n) },coeffs)
.filter().concat("+").replace("+-","-").replace("1*","")
or 0
}</
<
T(-1,-2,0,-3),T(-1),T)
.pump(Console.println,linearCombination);</
{{out}}
<pre>
|