Determinant and permanent: Difference between revisions

Add C# implementation
m (→‎{{header|Wren}}: Minor tidy)
(Add C# implementation)
 
(One intermediate revision by one other user not shown)
Line 446:
return 0;
}</syntaxhighlight>
 
=={{header|C#}}==
{{trans|Go}}
<syntaxhighlight lang="C#">
using System;
using System.Collections.Generic;
using System.Linq; // This is required for LINQ extension methods
 
class Program
{
static IEnumerable<IEnumerable<int>> GetPermutations(IEnumerable<int> list, int length)
{
if (length == 1) return list.Select(t => new int[] { t });
 
return GetPermutations(list, length - 1)
.SelectMany(t => list.Where(e => !t.Contains(e)),
(t1, t2) => t1.Concat(new int[] { t2 }));
}
 
static double Determinant(double[][] m)
{
double d = 0;
var p = new List<int>();
for (int i = 0; i < m.Length; i++)
{
p.Add(i);
}
 
var permutations = GetPermutations(p, p.Count);
foreach (var perm in permutations)
{
double pr = 1;
int sign = Math.Sign(GetPermutationSign(perm.ToList()));
for (int i = 0; i < perm.Count(); i++)
{
pr *= m[i][perm.ElementAt(i)];
}
d += sign * pr;
}
 
return d;
}
 
static int GetPermutationSign(IList<int> perm)
{
int inversions = 0;
for (int i = 0; i < perm.Count; i++)
for (int j = i + 1; j < perm.Count; j++)
if (perm[i] > perm[j])
inversions++;
return inversions % 2 == 0 ? 1 : -1;
}
 
static double Permanent(double[][] m)
{
double d = 0;
var p = new List<int>();
for (int i = 0; i < m.Length; i++)
{
p.Add(i);
}
 
var permutations = GetPermutations(p, p.Count);
foreach (var perm in permutations)
{
double pr = 1;
for (int i = 0; i < perm.Count(); i++)
{
pr *= m[i][perm.ElementAt(i)];
}
d += pr;
}
 
return d;
}
 
static void Main(string[] args)
{
double[][] m2 = new double[][] {
new double[] { 1, 2 },
new double[] { 3, 4 }
};
 
double[][] m3 = new double[][] {
new double[] { 2, 9, 4 },
new double[] { 7, 5, 3 },
new double[] { 6, 1, 8 }
};
 
Console.WriteLine($"{Determinant(m2)}, {Permanent(m2)}");
Console.WriteLine($"{Determinant(m3)}, {Permanent(m3)}");
}
}
</syntaxhighlight>
{{out}}
<pre>
-2, 10
-360, 900
 
</pre>
 
=={{header|C++}}==
Line 2,745 ⟶ 2,845:
permanent=6778800
--------------------------------------------------</pre>
 
=={{header|RPL}}==
{{trans|Phix}}
{{works with|HP|48G}}
« → a x y
« a SIZE {-1 -1} ADD 0 CON
1 OVER SIZE 1 GET '''FOR''' k
1 OVER SIZE 1 GET '''FOR''' j
k j 2 →LIST
a k DUP x ≥ + j DUP y ≥ + 2 →LIST GET
PUT
'''NEXT NEXT'''
» » '<span style="color:blue">MINOR</span>' STO <span style="color:grey">@ ''( matrix x y → matrix )''</span>
« DUP SIZE 1 GET
'''IF''' DUP 1 == '''THEN''' GET
'''ELSE'''
0
1 ROT '''FOR''' k
OVER { 1 } k + GET
3 PICK 1 k <span style="color:blue">MINOR PRM</span> * +
'''NEXT'''
SWAP DROP
END
» '<span style="color:blue">PRM</span>' STO <span style="color:grey">@ ''( matrix → permanent )''</span>
 
[[ 1 2 ]
[ 3 4 ]] DET LASTARG <span style="color:blue">PRM</span>
[[2 9 4]
[7 5 3]
[6 1 8]] DET LASTARG <span style="color:blue">PRM</span>
{{out}}
<pre>
4: -2
3: 10
2: -360
1: 900
</pre>
 
=={{header|Ruby}}==
Line 2,785 ⟶ 2,923:
permanent: 6778800
</pre>
 
=={{header|Rust}}==
{{trans|Java}}
337

edits