This commit is contained in:
Ingy döt Net 2013-10-27 22:24:23 +00:00
parent 6f050a029e
commit 776bba907c
3887 changed files with 59894 additions and 7280 deletions

View file

@ -10,10 +10,10 @@ in {
foreach (const row; a)
assert(row.length == a[0].length);
} body {
auto r = iota(cast()a.length);
auto r = a.length.iota;
T tot = 0;
foreach (sigma; permutations(r.array()))
tot += r.map!(i => a[i][sigma[i]])().prod();
foreach (sigma; r.array.permutations)
tot += r.map!(i => a[i][sigma[i]]).prod;
return tot;
}
@ -23,13 +23,13 @@ in {
assert(row.length == a[0].length);
} body {
immutable n = a.length;
auto r = iota(n);
auto r = n.iota;
T tot = 0;
//foreach (sigma, sign; spermutations(n)) {
foreach (sigma_sign; spermutations(n)) {
//foreach (sigma, sign; n.spermutations) {
foreach (sigma_sign; n.spermutations) {
const sigma = sigma_sign[0];
immutable sign = sigma_sign[1];
tot += sign * r.map!(i => a[i][sigma[i]])().prod();
tot += sign * r.map!(i => a[i][sigma[i]]).prod;
}
return tot;
}
@ -37,21 +37,21 @@ in {
void main() {
import std.stdio;
foreach (const a; [[[1, 2],
[3, 4]],
foreach (/*immutable*/ const a; [[[1, 2],
[3, 4]],
[[1, 2, 3, 4],
[4, 5, 6, 7],
[7, 8, 9, 10],
[10, 11, 12, 13]],
[[1, 2, 3, 4],
[4, 5, 6, 7],
[7, 8, 9, 10],
[10, 11, 12, 13]],
[[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19],
[20, 21, 22, 23, 24]]]) {
[[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19],
[20, 21, 22, 23, 24]]]) {
writefln("[%([%(%2s, %)],\n %)]]", a);
writefln("Permanent: %s, determinant: %s\n",
permanent(a), determinant(a));
a.permanent, a.determinant);
}
}

View file

@ -0,0 +1,26 @@
require 'matrix'
class Matrix
# Add "permanent" method to Matrix class
def permanent
r = (0...row_count).to_a # [0,1] (first example), [0,1,2,3] (second example)
r.permutation.inject(0) do |sum, sigma|
sum += sigma.zip(r).inject(1){|prod, (row, col)| prod *= self[row, col] }
end
end
end
m1 = Matrix[[1,2],[3,4]] # testcases from Python version
m2 = Matrix[[1, 2, 3, 4], [4, 5, 6, 7], [7, 8, 9, 10], [10, 11, 12, 13]]
m3 = Matrix[[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19],
[20, 21, 22, 23, 24]]
[m1, m2, m3].each do |m|
puts "determinant:\t #{m.determinant}", "permanent:\t #{m.permanent}"
puts
end