Determinant and permanent: Difference between revisions
→{{header|Raku}}: add a test with a Hilbert matrix
Thundergnat (talk | contribs) (→{{header|Raku}}: Fix up some internal links) |
Thundergnat (talk | contribs) (→{{header|Raku}}: add a test with a Hilbert matrix) |
||
Line 1,909:
note "Not a square matrix" and return
if [||] map { @a.elems cmp @a[$_].elems }, ^@a;
sum σ_permutations([
my $permutation = .key;
my $term = $op eq 'perm' ?? 1 !! .value;
for $permutation.kv -> $i, $j { $term *= @a[$i][$j] };
$term
}
sub hilbert-matrix (\h) {[(1..h).map(-> \n {[(n..^n+h).map: {(1/$_).FatRat}]})]}
sub rat-or-int ($num) {
return $num unless $num ~~ Rat|FatRat;
return $num.narrow if $num.narrow.WHAT ~~ Int;
$num.nude.join: '/';
}▼
sub say-it ($message, @array) {
@array.map: {$max max= max $_».&rat-or-int.comb(/\S+/)».chars};
$_».&rat-or-int.fmt(" %{$max}s").put for @array;
}
########### Testing ###########
my @tests = (
[
Line 1,930 ⟶ 1,945:
[ 10, 11, 12, 13 ]
],
hilbert-matrix 7
▲ [
[ 0, 1, 2, 3, 4 ],▼
[ 5, 6, 7, 8, 9 ],▼
);
▲ say '';
▲}
for @tests -> @matrix {
say-it 'Matrix:', @matrix;
say "Determinant:\t", rat-or-int @matrix.&
say "
say
▲ say '-' x 25;
}</lang>
'''Output'''
<pre>Matrix:
Determinant: -2
Permanent: 10
----------------------------------------
Matrix:
Determinant: 0
Permanent: 29556
----------------------------------------
Matrix:
1/4 1/5 1/6 1/7 1/8 1/9 1/10
1/5 1/6 1/7 1/8 1/9 1/10 1/11
1/7 1/8 1/9 1/10 1/11 1/12 1/13
Determinant: 1/2067909047925770649600000
Permanent: 29453515169174062608487/2067909047925770649600000
----------------------------------------</pre>
=={{header|REXX}}==
|