Determinant and permanent: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 301:
return 0;
}</lang>
 
 
 
=={{header|Common Lisp}}==
Line 359 ⟶ 357:
((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)) determinant: 0, permanent: 6778800
</pre>
 
 
=={{header|D}}==
Line 664 ⟶ 661:
float d = det(m1);
</lang>
 
=={{header|Go}}==
===Implementation===
Line 1,421 ⟶ 1,419:
2 0 -1
det: 18 permanent: 10</pre>
 
=={{header|МК-61/52}}==
<pre>
П4 ИПE П2 КИП0 ИП0 П1 С/П ИП4 / КП2
L1 06 ИПE П3 ИП0 П1 Сx КП2 L1 17
ИП0 ИП2 + П1 П2 ИП3 - x#0 34 С/П
ПП 80 БП 21 КИП0 ИП4 С/П КИП2 - *
П4 ИП0 П3 x#0 35 Вx С/П КИП2 - <->
/ КП1 L3 45 ИП1 ИП0 + П3 ИПE П1
П2 КИП1 /-/ ПП 80 ИП3 + П3 ИП1 -
x=0 61 ИП0 П1 КИП3 КП2 L1 74 БП 12
ИП0 <-> ^ КИП3 * КИП1 + КП2 -> L0
82 -> П0 В/О
</pre>
 
This program calculates the determinant of the matrix of order <= 5. Prior to startup, ''РE'' entered ''13'', entered the order of the matrix ''Р0'', and the elements are introduced with the launch of the program after one of them, the last on the screen will be determinant. Permanent is calculated in this way.
 
=={{header|Maple}}==
Line 1,464 ⟶ 1,446:
permanent(a);
900</lang>
 
=={{header|МК-61/52}}==
<pre>
П4 ИПE П2 КИП0 ИП0 П1 С/П ИП4 / КП2
L1 06 ИПE П3 ИП0 П1 Сx КП2 L1 17
ИП0 ИП2 + П1 П2 ИП3 - x#0 34 С/П
ПП 80 БП 21 КИП0 ИП4 С/П КИП2 - *
П4 ИП0 П3 x#0 35 Вx С/П КИП2 - <->
/ КП1 L3 45 ИП1 ИП0 + П3 ИПE П1
П2 КИП1 /-/ ПП 80 ИП3 + П3 ИП1 -
x=0 61 ИП0 П1 КИП3 КП2 L1 74 БП 12
ИП0 <-> ^ КИП3 * КИП1 + КП2 -> L0
82 -> П0 В/О
</pre>
 
This program calculates the determinant of the matrix of order <= 5. Prior to startup, ''РE'' entered ''13'', entered the order of the matrix ''Р0'', and the elements are introduced with the launch of the program after one of them, the last on the screen will be determinant. Permanent is calculated in this way.
 
=={{header|Nim}}==
Line 1,650 ⟶ 1,648:
perm(M) = 900.
</pre>
 
=={{header|Perl 6}}==
{{works with|Rakudo|2015.12}}
Uses the permutations generator from the [[Permutations by swapping#Perl_6|Permutations by swapping]] task. This implementation is naive and brute-force (slow) but exact.
 
<lang perl6>sub insert ($x, @xs) { ([flat @xs[0 ..^ $_], $x, @xs[$_ .. *]] for 0 .. @xs) }
sub order ($sg, @xs) { $sg > 0 ?? @xs !! @xs.reverse }
 
multi σ_permutations ([]) { [] => 1 }
 
multi σ_permutations ([$x, *@xs]) {
σ_permutations(@xs).map({ |order($_.value, insert($x, $_.key)) }) Z=> |(1,-1) xx *
}
 
sub m_arith ( @a, $op ) {
note "Not a square matrix" and return
if [||] map { @a.elems cmp @a[$_].elems }, ^@a;
[+] map {
my $permutation = .key;
my $term = $op eq 'perm' ?? 1 !! .value;
for $permutation.kv -> $i, $j { $term *= @a[$i][$j] };
$term
}, σ_permutations [^@a];
}
 
########### Testing ###########
 
my @tests = (
[
[ 1, 2 ],
[ 3, 4 ]
],
[
[ 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 ]
]
);
 
sub dump (@matrix) {
say $_».fmt: "%3s" for @matrix;
say '';
}
 
for @tests -> @matrix {
say 'Matrix:';
@matrix.&dump;
say "Determinant:\t", @matrix.&m_arith: <det>;
say "Permanent: \t", @matrix.&m_arith: <perm>;
say '-' x 25;
}</lang>
 
'''Output'''
<pre>Matrix:
[ 1 2]
[ 3 4]
 
Determinant: -2
Permanent: 10
-------------------------
Matrix:
[ 1 2 3 4]
[ 4 5 6 7]
[ 7 8 9 10]
[ 10 11 12 13]
 
Determinant: 0
Permanent: 29556
-------------------------
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]
 
Determinant: 0
Permanent: 6778800
-------------------------</pre>
 
=={{header|Phix}}==
Line 1,980 ⟶ 1,891:
(matrix-ref M i σi))))
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|2015.12}}
Uses the permutations generator from the [[Permutations by swapping#Perl_6|Permutations by swapping]] task. This implementation is naive and brute-force (slow) but exact.
 
<lang perl6>sub insert ($x, @xs) { ([flat @xs[0 ..^ $_], $x, @xs[$_ .. *]] for 0 .. @xs) }
sub order ($sg, @xs) { $sg > 0 ?? @xs !! @xs.reverse }
 
multi σ_permutations ([]) { [] => 1 }
 
multi σ_permutations ([$x, *@xs]) {
σ_permutations(@xs).map({ |order($_.value, insert($x, $_.key)) }) Z=> |(1,-1) xx *
}
 
sub m_arith ( @a, $op ) {
note "Not a square matrix" and return
if [||] map { @a.elems cmp @a[$_].elems }, ^@a;
[+] map {
my $permutation = .key;
my $term = $op eq 'perm' ?? 1 !! .value;
for $permutation.kv -> $i, $j { $term *= @a[$i][$j] };
$term
}, σ_permutations [^@a];
}
 
########### Testing ###########
 
my @tests = (
[
[ 1, 2 ],
[ 3, 4 ]
],
[
[ 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 ]
]
);
 
sub dump (@matrix) {
say $_».fmt: "%3s" for @matrix;
say '';
}
 
for @tests -> @matrix {
say 'Matrix:';
@matrix.&dump;
say "Determinant:\t", @matrix.&m_arith: <det>;
say "Permanent: \t", @matrix.&m_arith: <perm>;
say '-' x 25;
}</lang>
 
'''Output'''
<pre>Matrix:
[ 1 2]
[ 3 4]
 
Determinant: -2
Permanent: 10
-------------------------
Matrix:
[ 1 2 3 4]
[ 4 5 6 7]
[ 7 8 9 10]
[ 10 11 12 13]
 
Determinant: 0
Permanent: 29556
-------------------------
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]
 
Determinant: 0
Permanent: 6778800
-------------------------</pre>
 
=={{header|REXX}}==
Line 2,250 ⟶ 2,249:
permanent: 6778800
</pre>
 
=={{header|Scala}}==
<lang scala>
Line 2,667:
18 18 10
13 error 13 </pre>
 
=={{header|zkl}}==
<lang zkl>var [const] GSL=Import("zklGSL"); // libGSL (GNU Scientific Library)
10,327

edits