LU decomposition: Difference between revisions

Content added Content deleted
m (→‎{{header|Perl 6}}: minor tweaks, remove some superstitious parentheses)
m (→‎{{header|Perl 6}}: Named matrices more appropriately)
Line 1,868: Line 1,868:
)
)
-> @test {
-> @test {
say-it 'Original Matrix', @test;
say-it 'A Matrix', @test;
say-it( $_[0], @($_[1]) ) for 'P Matrix', 'A Matrix', 'L Matrix', 'U Matrix' Z, lu @test;
say-it( $_[0], @($_[1]) ) for 'P Matrix', 'AP Matrix', 'L Matrix', 'U Matrix' Z, lu @test;
}
}


Line 1,876: Line 1,876:
my $n = +@a;
my $n = +@a;
my @P = pivotize @a;
my @P = pivotize @a;
my @A = mmult @P, @a;
my @AP = mmult @P, @a;
my @L = matrix-ident $n;
my @L = matrix-ident $n;
my @U = matrix-zero $n;
my @U = matrix-zero $n;
Line 1,882: Line 1,882:
for ^$n -> $j {
for ^$n -> $j {
if $j >= $i {
if $j >= $i {
@U[$i][$j] = @A[$i][$j] - [+] map { @U[$_][$j] * @L[$i][$_] }, ^$i
@U[$i][$j] = @AP[$i][$j] - [+] map { @U[$_][$j] * @L[$i][$_] }, ^$i
} else {
} else {
@L[$i][$j] = (@A[$i][$j] - [+] map { @U[$_][$j] * @L[$i][$_] }, ^$j) / @U[$j][$j];
@L[$i][$j] = (@AP[$i][$j] - [+] map { @U[$_][$j] * @L[$i][$_] }, ^$j) / @U[$j][$j];
}
}
}
}


}
}
return @P, @A, @L, @U;
return @P, @AP, @L, @U;
}
}


Line 1,936: Line 1,936:
}</lang>
}</lang>
{{out}}
{{out}}
<pre>Original Matrix
<pre>A Matrix
1 3 5
1 3 5
2 4 7
2 4 7
Line 1,946: Line 1,946:
0 0 1
0 0 1


A Matrix
AP Matrix
2 4 7
2 4 7
1 3 5
1 3 5
Line 1,961: Line 1,961:
0 0 -2
0 0 -2


Original Matrix
A Matrix
11 9 24 2
11 9 24 2
1 5 2 6
1 5 2 6
Line 1,973: Line 1,973:
0 0 0 1
0 0 0 1


A Matrix
AP Matrix
11 9 24 2
11 9 24 2
3 17 18 1
3 17 18 1