Jump to content

LU decomposition: Difference between revisions

m
m (→‎{{header|Perl 6}}: Named matrices more appropriately)
m (→‎{{header|Perl 6}}: bikeshedding)
Line 1,858:
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 'A Matrix', @test;
say-it( $_[0], @($_[1]) ) for 'P Matrix', 'AP Matrix', 'L Matrix', 'U Matrix' Z, lu @test;
}
 
Line 1,876:
my $n = +@a;
my @P = pivotize @a;
my @AP = mmult @P, @a;
my @L = matrix-ident $n;
my @U = matrix-zero $n;
Line 1,882:
for ^$n -> $j {
if $j >= $i {
@U[$i][$j] = @AP[$i][$j] - [+] map { @U[$_][$j] * @L[$i][$_] }, ^$i
} else {
@L[$i][$j] = (@AP[$i][$j] - [+] map { @U[$_][$j] * @L[$i][$_] }, ^$j) / @U[$j][$j];
}
}
 
}
return @P, @AP, @L, @U;
}
 
Line 1,902:
$max = @m[$j][$i];
$row = $j;
};
}
if $row != $i {
@id[$row, $i] = @id[$i, $row];
}
}
Line 1,922:
@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;
Line 1,946:
0 0 1
 
AP Matrix
2 4 7
1 3 5
Line 1,973:
0 0 0 1
 
AP Matrix
11 9 24 2
3 17 18 1
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.