Cramer's rule: Difference between revisions
m (→{{header|Sidef}}: generic algorithm for matrix determinant) |
m (→{{header|Perl 6}}: generic algorithm for matrix determinant) |
||
Line 27: | Line 27: | ||
=={{header|Perl 6}}== |
=={{header|Perl 6}}== |
||
<lang perl6>sub det(@ |
<lang perl6>sub det(@matrix) { |
||
my @a = @matrix.map: { [|$_] }; |
|||
my $sign = +1; |
|||
my $pivot = 1; |
|||
for ^@a -> $k { |
|||
my @r = ($k+1 .. @a.end); |
|||
my $previous-pivot = $pivot; |
|||
if 0 == ($pivot = @a[$k][$k]) { |
|||
(my $s = @r.first: { @a[$_][$k] != 0 }) // return 0; |
|||
(@a[$s],@a[$k]) = (@a[$k], @a[$s]); |
|||
my $pivot = @a[$k][$k]; |
|||
$sign = -$sign; |
|||
} |
|||
for @r X @r -> ($i, $j) { |
|||
((@a[$i][$j] *= $pivot) -= @a[$i][$k]*@a[$k][$j]) /= $previous-pivot; |
|||
} |
|||
} |
} |
||
$sign * $pivot |
|||
} |
} |
||
Revision as of 14:51, 28 January 2016
In linear algebra, Cramer's rule is an explicit formula for the solution of a system of linear equations with as many equations as unknowns, valid whenever the system has a unique solution. It expresses the solution in terms of the determinants of the (square) coefficient matrix and of matrices obtained from it by replacing one column by the vector of right hand sides of the equations.
Given
which in matrix format is
Then the values of and can be found as follows:
- Task
Given the following system of equations:
solve for , and , using Cramer's rule.
Perl 6
<lang perl6>sub det(@matrix) {
my @a = @matrix.map: { [|$_] }; my $sign = +1; my $pivot = 1; for ^@a -> $k { my @r = ($k+1 .. @a.end); my $previous-pivot = $pivot; if 0 == ($pivot = @a[$k][$k]) { (my $s = @r.first: { @a[$_][$k] != 0 }) // return 0; (@a[$s],@a[$k]) = (@a[$k], @a[$s]); my $pivot = @a[$k][$k]; $sign = -$sign; } for @r X @r -> ($i, $j) { ((@a[$i][$j] *= $pivot) -= @a[$i][$k]*@a[$k][$j]) /= $previous-pivot; } } $sign * $pivot
}
sub cramers_rule(@A, @terms) {
gather for ^@A -> $i { my @Ai = @A.map: { [|$_] }; for ^@terms -> $j { @Ai[$j][$i] = @terms[$j]; } take det(@Ai); } »/» det(@A);
}
my @matrix = (
[2, -3, 1], [1, -2, -2], [3, -4, 1],
);
my @free_terms = (4, -6, 5); my ($x, $y, $z) = |cramers_rule(@matrix, @free_terms);
say "x = $x"; say "y = $y"; say "z = $z";</lang>
- Output:
x = 2 y = 1 z = 3
Sidef
<lang ruby>func det(a) {
a = a.map{.map{_}} var sign = +1 var pivot = 1 a.range.each { |k| var r = (k+1 .. a.end) var previous_pivot = pivot if ((pivot = a[k][k]) == 0) { a.swap(r.first_by {|i| a[i][k] != 0 } \\ (return 0), k) pivot = a[k][k] sign = -sign } r ~X r -> each { |p| var(i, j) = p... ((a[i][j] *= pivot) -= a[i][k]*a[k][j]) /= previous_pivot } } sign * pivot
}
func cramers_rule(A, terms) {
gather { A.each_index { |i| var Ai = A.map{.map{_}} terms.each_index { |j| Ai[j][i] = terms[j] } take(det(Ai)) } } »/» det(A)
}
var matrix = [
[2, -3, 1], [1, -2, -2], [3, -4, 1],
]
var free_terms = [4, -6, 5] var (x, y, z) = cramers_rule(matrix, free_terms)...;
say "x = #{x}" say "y = #{y}" say "z = #{z}"</lang>
- Output:
x = 2 y = 1 z = 3