Display a linear combination: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
m (→‎{{header|Perl}}: made 'strict' compliant)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 153:
Vector for [-1] -> - e1
</pre>
 
=={{header|C sharp|C#}}==
{{trans|D}}
<lang csharp>using System;
using System.Collections.Generic;
using System.Text;
 
namespace DisplayLinearCombination {
class Program {
static string LinearCombo(List<int> c) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < c.Count; i++) {
int n = c[i];
if (n < 0) {
if (sb.Length == 0) {
sb.Append('-');
} else {
sb.Append(" - ");
}
} else if (n > 0) {
if (sb.Length != 0) {
sb.Append(" + ");
}
} else {
continue;
}
 
int av = Math.Abs(n);
if (av != 1) {
sb.AppendFormat("{0}*", av);
}
sb.AppendFormat("e({0})", i + 1);
}
if (sb.Length == 0) {
sb.Append('0');
}
return sb.ToString();
}
 
static void Main(string[] args) {
List<List<int>> combos = new List<List<int>>{
new List<int> { 1, 2, 3},
new List<int> { 0, 1, 2, 3},
new List<int> { 1, 0, 3, 4},
new List<int> { 1, 2, 0},
new List<int> { 0, 0, 0},
new List<int> { 0},
new List<int> { 1, 1, 1},
new List<int> { -1, -1, -1},
new List<int> { -1, -2, 0, -3},
new List<int> { -1},
};
 
foreach (List<int> c in combos) {
var arr = "[" + string.Join(", ", c) + "]";
Console.WriteLine("{0,15} -> {1}", arr, LinearCombo(c));
}
}
}
}</lang>
{{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|C++}}==
Line 236 ⟶ 307:
 
return 0;
}</lang>
{{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|C#}}==
{{trans|D}}
<lang csharp>using System;
using System.Collections.Generic;
using System.Text;
 
namespace DisplayLinearCombination {
class Program {
static string LinearCombo(List<int> c) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < c.Count; i++) {
int n = c[i];
if (n < 0) {
if (sb.Length == 0) {
sb.Append('-');
} else {
sb.Append(" - ");
}
} else if (n > 0) {
if (sb.Length != 0) {
sb.Append(" + ");
}
} else {
continue;
}
 
int av = Math.Abs(n);
if (av != 1) {
sb.AppendFormat("{0}*", av);
}
sb.AppendFormat("e({0})", i + 1);
}
if (sb.Length == 0) {
sb.Append('0');
}
return sb.ToString();
}
 
static void Main(string[] args) {
List<List<int>> combos = new List<List<int>>{
new List<int> { 1, 2, 3},
new List<int> { 0, 1, 2, 3},
new List<int> { 1, 0, 3, 4},
new List<int> { 1, 2, 0},
new List<int> { 0, 0, 0},
new List<int> { 0},
new List<int> { 1, 1, 1},
new List<int> { -1, -1, -1},
new List<int> { -1, -2, 0, -3},
new List<int> { -1},
};
 
foreach (List<int> c in combos) {
var arr = "[" + string.Join(", ", c) + "]";
Console.WriteLine("{0,15} -> {1}", arr, LinearCombo(c));
}
}
}
}</lang>
{{out}}
Line 935:
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 ]</lang>
{{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 6}}==
<lang perl6>sub linear-combination(@coeff) {
(@coeff Z=> map { "e($_)" }, 1 .. *)
.grep(+*.key)
.map({ .key ~ '*' ~ .value })
.join(' + ')
.subst('+ -', '- ', :g)
.subst(/<|w>1\*/, '', :g)
|| '0'
}
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 ]
;</lang>
{{out}}
<pre>e(1) + 2*e(2) + 3*e(3)
Line 1,102 ⟶ 1,067:
-e(1)-e(2)-e(3)
-e(1)-2*e(2)-3*e(4)
-e(1)</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
<lang perl6>sub linear-combination(@coeff) {
(@coeff Z=> map { "e($_)" }, 1 .. *)
.grep(+*.key)
.map({ .key ~ '*' ~ .value })
.join(' + ')
.subst('+ -', '- ', :g)
.subst(/<|w>1\*/, '', :g)
|| '0'
}
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 ]
;</lang>
{{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>
 
Line 1,215 ⟶ 1,216:
}
}</lang>
 
=={{header|Sidef}}==
{{trans|Tcl}}
10,327

edits