LU decomposition: Difference between revisions
m
→{{header|Raku}}: bit more idiomatic
m (→{{header|Phix}}: added syntax colouring, made p2js compatible) |
SqrtNegInf (talk | contribs) m (→{{header|Raku}}: bit more idiomatic) |
||
Line 3,963:
=={{header|Raku}}==
(formerly Perl 6)
Translation of Ruby.▼
▲Translation of Ruby.
<lang perl6>for ( [1, 3, 5], # Test Matrices
[2, 4, 7],
Line 3,977 ⟶ 3,976:
-> @test {
say-it 'A Matrix', @test;
say-it(
}
sub lu (@a) {
die unless @a.&is-square;
my $n =
my @P = pivotize @a;
my @Aʼ = mmult @P, @a;
my @L = matrix-ident $n;
my @U = matrix-zero $n;
for ^$n X ^$n -> ($i,$j) {
else
▲ @U[$i][$j] = @Aʼ[$i][$j] - [+] map { @U[$_][$j] * @L[$i][$_] }, ^$i
}
}
sub pivotize (@m) {
my $size =
my @id = matrix-ident $size;
for ^$size -> $i {
my $max = @m[$i
my $row = $i;
for $i ..^ $size -> $j {
if @m[$j
$max = @m[$j
$row = $j;
}
}
@id[$row, $i] = @id[$i, $row] if $row != $i
}
@id
}
sub is-square (@m) { so @m == all @m
sub matrix-zero ($n, $m = $n) { map { [ flat 0 xx $n ] }, ^$m }
Line 4,028 ⟶ 4,019:
my @p;
for ^@a X ^@b[0] -> ($r, $c) {
@p[$r
}
@p
Line 4,035 ⟶ 4,026:
sub rat-int ($num) {
return $num unless $num ~~ Rat;
return $num.narrow if $num.narrow
$num.nude.join: '/';
}
|