Jump to content

Display a linear combination: Difference between revisions

m
syntax highlighting fixup automation
No edit summary
m (syntax highlighting fixup automation)
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 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">
<lang C>
 
#include<stdlib.h>
Line 194:
return 0;
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 223:
=={{header|C sharp|C#}}==
{{trans|D}}
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Text;
Line 279:
}
}
}</langsyntaxhighlight>
{{out}}
<pre> [1, 2, 3] -> e(1) + 2*e(2) + 3*e(3)
Line 294:
=={{header|C++}}==
{{trans|D}}
<langsyntaxhighlight lang="cpp">#include <iomanip>
#include <iostream>
#include <sstream>
Line 374:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre> [1, 2, 3] -> e(1) + 2*e(2) + 3*e(3)
Line 389:
=={{header|D}}==
{{trans|Kotlin}}
<langsyntaxhighlight Dlang="d">import std.array;
import std.conv;
import std.format;
Line 441:
writefln("%-15s -> %s", arr, linearCombo(c));
}
}</langsyntaxhighlight>
{{out}}
<pre>[1, 2, 3] -> e(1) + 2*e(2) + 3*e(3)
Line 455:
 
=={{header|EchoLisp}}==
<langsyntaxhighlight lang="scheme">
;; 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>
</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:
=={{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]
]
Enum.each(coeffs, &Linear_combination.display(&1))</langsyntaxhighlight>
 
{{out}}
Line 543:
=={{header|F_Sharp|F#}}==
===The function===
<langsyntaxhighlight lang="fsharp">
// Display a linear combination. Nigel Galloway: March 28th., 2018
let fN g =
Line 559:
|_ -> 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:
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: formatting kernel match math pair-rocket regexp sequences ;
 
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</langsyntaxhighlight>
{{out}}
<pre>
Line 670:
=={{header|FreeBASIC}}==
{{trans|Ring}}
<langsyntaxhighlight 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}}
Line 697:
Print cadena
Next n
Sleep</langsyntaxhighlight>
{{out}}
<pre>
Line 705:
=={{header|Go}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 763:
fmt.Printf("%-15s -> %s\n", t, linearCombo(c))
}
}</langsyntaxhighlight>
 
{{out}}
Line 781:
=={{header|Groovy}}==
{{trans|Java}}
<langsyntaxhighlight lang="groovy">class LinearCombination {
private static String linearCombo(int[] c) {
StringBuilder sb = new StringBuilder()
Line 824:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>[1, 2, 3] -> e(1) + 2*e(2) + 3*e(3)
Line 838:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Text.Printf (printf)
 
linearForm :: [Int] -> String
Line 853:
'+':s -> s
"" -> "0"
s -> s</langsyntaxhighlight>
 
Testing
 
<langsyntaxhighlight lang="haskell">coeffs :: [[Int]]
coeffs = [ [1, 2, 3]
, [0, 1, 2, 3]
Line 867:
, [-1, -1, -1]
, [-1, -2, 0, -3]
, [-1] ]</langsyntaxhighlight>
 
<pre>λ> mapM_ (print . linearForm) coeffs
Line 885:
Implementation:
 
<langsyntaxhighlight Jlang="j">fourbanger=:3 :0
e=. ('e(',')',~])@":&.> 1+i.#y
firstpos=. 0< {.y-.0
Line 898:
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:
-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:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>[1, 2, 3] -> e(1) + 2*e(2) + 3*e(3)
Line 981:
 
=={{header|jq}}==
<langsyntaxhighlight lang="jq">def linearCombo:
reduce to_entries[] as {key: $k,value: $v} ("";
if $v == 0 then .
Line 1,010:
[-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,-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, -1, -1], [-1, -2, 0, -3], [-1]]
@printf("%20s -> %s\n", c, linearcombination(c))
end</langsyntaxhighlight>
 
{{out}}
Line 1,045:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.1.2
 
fun linearCombo(c: IntArray): String {
Line 1,080:
println("${c.contentToString().padEnd(15)} -> ${linearCombo(c)}")
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,097:
 
=={{header|Lambdatalk}}==
<langsyntaxhighlight lang="scheme">
 
{def linearcomb
Line 1,127:
{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:
end
 
main()</langsyntaxhighlight>
{{out}}
<pre> [1, 2, 3] -> e(1) + 2*e(2) + 3*e(3)
Line 1,211:
 
=={{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:
 
=={{header|Modula-2}}==
<langsyntaxhighlight lang="modula2">MODULE Linear;
FROM FormatString IMPORT FormatString;
FROM Terminal IMPORT WriteString,WriteLn,ReadChar;
Line 1,292:
 
ReadChar
END Linear.</langsyntaxhighlight>
 
=={{header|Nim}}==
{{trans|Kotlin}}
<langsyntaxhighlight Nimlang="nim">import strformat
 
proc linearCombo(c: openArray[int]): string =
Line 1,324:
 
for c in Combos:
echo fmt"{($c)[1..^1]:15} → {linearCombo(c)}"</langsyntaxhighlight>
 
{{out}}
Line 1,339:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use strict;
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 ]</langsyntaxhighlight>
{{out}}
<pre>e(1) + 2*e(2) + 3*e(3)
Line 1,369:
=={{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:
<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:
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">; Process and output values.
Procedure WriteLinear(Array c.i(1))
Define buf$,
Line 1,527:
Data.i -1
_V10:
EndDataSection</langsyntaxhighlight>
{{out}}
<pre>
Line 1,543:
 
=={{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:
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:
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">#lang racket/base
(require racket/match racket/string)
 
Line 1,599:
(-1 -2 0 -3)
(-1)))
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,615:
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" perl6line>sub linear-combination(@coeff) {
(@coeff Z=> map { "e($_)" }, 1 .. *)
.grep(+*.key)
Line 1,636:
[-1, -2, 0, -3],
[-1 ]
;</langsyntaxhighlight>
{{out}}
<pre>e(1) + 2*e(2) + 3*e(3)
Line 1,650:
 
=={{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:
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:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Display a linear combination
 
Line 1,722:
see str + nl
next
</syntaxhighlight>
</lang>
Output:
<pre>
Line 1,739:
=={{header|Ruby}}==
{{trans|D}}
<langsyntaxhighlight lang="ruby">def linearCombo(c)
sb = ""
c.each_with_index { |n, i|
Line 1,793:
end
 
main()</langsyntaxhighlight>
{{out}}
<pre>[1, 2, 3] -> e(1) + 2*e(2) + 3*e(3)
Line 1,807:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">
use std::fmt::{Display, Formatter, Result};
use std::process::exit;
Line 1,899:
println!("{}", linear_combination(&coefficients));
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,906:
 
=={{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:
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:
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:
This solution strives for legibility rather than golf.
 
<langsyntaxhighlight Tcllang="tcl">proc lincom {factors} {
set exp 0
set res ""
Line 2,020:
} {
puts [format "%10s -> %-10s" $test [lincom $test]]
}</langsyntaxhighlight>
 
{{out}}
Line 2,036:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<langsyntaxhighlight lang="vbnet">Imports System.Text
 
Module Module1
Line 2,090:
End Sub
 
End Module</langsyntaxhighlight>
{{out}}
<pre> [1, 2, 3] -> e(1) + 2*e(2) + 3*e(3)
Line 2,105:
=={{header|Vlang}}==
{{trans|Go}}
<langsyntaxhighlight lang="vlang">import strings
 
fn linear_combo(c []int) string {
Line 2,161:
println("${c:-15} -> ${linear_combo(c)}")
}
}</langsyntaxhighlight>
 
{{out}}
Line 2,180:
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "/fmt" for Fmt
 
var linearCombo = Fn.new { |c|
Line 2,213:
for (c in combos) {
Fmt.print("$-15s -> $s", c.toString, linearCombo.call(c))
}</langsyntaxhighlight>
 
{{out}}
Line 2,231:
=={{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>
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.