LU decomposition: Difference between revisions
Content added Content deleted
(Added EchoLisp) |
Thundergnat (talk | contribs) (→{{header|Perl 6}}: Add Perl 6 example) |
||
Line 1,854: | Line 1,854: | ||
/* matrix([-204], [2100], [-4740], [2940]) */</lang> |
/* matrix([-204], [2100], [-4740], [2940]) */</lang> |
||
=={{header|Perl 6}}== |
|||
{{works with|Rakudo|2015-11-20}} |
|||
Translation of Ruby. |
|||
<lang perl6>for ( [1, 3, 5], # Test Matrices |
|||
[2, 4, 7], |
|||
[1, 1, 0] |
|||
), |
|||
( [11, 9, 24, 2], |
|||
[ 1, 5, 2, 6], |
|||
[ 3, 17, 18, 1], |
|||
[ 2, 5, 7, 1] |
|||
) |
|||
-> @test { |
|||
say-it 'Original Matrix', @test; |
|||
say-it( $_[0], @($_[1]) ) for 'P Matrix', 'A Matrix', 'L Matrix', 'U Matrix' Z, lu @test; |
|||
} |
|||
sub lu (@a) { |
|||
die unless @a.&is-square; |
|||
my $n = +@a; |
|||
my @L = matrix-ident $n; |
|||
my @U = matrix-zero $n; |
|||
my @P = pivotize @a; |
|||
my @A = mmult @P, @a; |
|||
for ^$n -> $i { |
|||
for ^$n -> $j { |
|||
if $j >= $i { |
|||
@U[$i][$j] = @A[$i][$j] - [+] map { @U[$_][$j] * @L[$i][$_] }, ^$i |
|||
} else { |
|||
@L[$i][$j] = (@A[$i][$j] - [+] map { @U[$_][$j] * @L[$i][$_] }, ^$j) / @U[$j][$j]; |
|||
} |
|||
} |
|||
} |
|||
return @P, @A, @L, @U; |
|||
} |
|||
sub pivotize (@m) { |
|||
my $size = +@m; |
|||
my @id = matrix-ident $size; |
|||
for ^$size -> $i { |
|||
my $max = @m[$i][$i]; |
|||
my $row = $i; |
|||
for $i ..^ $size -> $j { |
|||
if @m[$j][$i] > $max { |
|||
$max = @m[$j][$i]; |
|||
$row = $j; |
|||
}; |
|||
} |
|||
if $row != $i { |
|||
@id[$row, $i] = @id[$i, $row]; |
|||
} |
|||
} |
|||
@id |
|||
} |
|||
sub is-square (@m) { so @m == all @m[*] } |
|||
sub matrix-zero ($n, $m = $n) { map ( { [ flat 0 xx $n ] } ), ^$m } |
|||
sub matrix-ident ($n) { map ( { [ flat 0 xx $_, 1, 0 xx $n - 1 - $_ ] } ), ^$n } |
|||
sub mmult(@a,@b) { |
|||
my @p; |
|||
for ^@a X ^@b[0] -> ($r, $c) { |
|||
@p[$r][$c] += @a[$r][$_] * @b[$_][$c] for ^@b; |
|||
} |
|||
@p; |
|||
} |
|||
sub rat-int ($num is rw) { |
|||
return $num unless $num ~~ Rat; |
|||
return $num.narrow if $num.narrow.WHAT ~~ Int; |
|||
$num.nude.join: '/'; |
|||
} |
|||
sub say-it ($message, @array) { |
|||
say "\n$message"; |
|||
$_».&rat-int.fmt("%7s").say for @array; |
|||
}</lang> |
|||
{{out}} |
|||
<pre>Original Matrix |
|||
1 3 5 |
|||
2 4 7 |
|||
1 1 0 |
|||
P Matrix |
|||
0 1 0 |
|||
1 0 0 |
|||
0 0 1 |
|||
A Matrix |
|||
2 4 7 |
|||
1 3 5 |
|||
1 1 0 |
|||
L Matrix |
|||
1 0 0 |
|||
1/2 1 0 |
|||
1/2 -1 1 |
|||
U Matrix |
|||
2 4 7 |
|||
0 1 3/2 |
|||
0 0 -2 |
|||
Original Matrix |
|||
11 9 24 2 |
|||
1 5 2 6 |
|||
3 17 18 1 |
|||
2 5 7 1 |
|||
P Matrix |
|||
1 0 0 0 |
|||
0 0 1 0 |
|||
0 1 0 0 |
|||
0 0 0 1 |
|||
A Matrix |
|||
11 9 24 2 |
|||
3 17 18 1 |
|||
1 5 2 6 |
|||
2 5 7 1 |
|||
L Matrix |
|||
1 0 0 0 |
|||
3/11 1 0 0 |
|||
1/11 23/80 1 0 |
|||
2/11 37/160 1/278 1 |
|||
U Matrix |
|||
11 9 24 2 |
|||
0 160/11 126/11 5/11 |
|||
0 0 -139/40 91/16 |
|||
0 0 0 71/139 |
|||
</pre> |
|||
=={{header|PL/I}}== |
=={{header|PL/I}}== |