Cramer's rule: Difference between revisions
(Create "Cramer's rule" draft task) |
m (Added the actual rule) |
||
Line 1: | Line 1: | ||
{{draft task}} In [[wp:linear algebra|linear algebra]], [[wp:Cramer's rule|Cramer's rule]] is an explicit formula for the solution of a [[wp:system of linear equations|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. |
{{draft task}} In [[wp:linear algebra|linear algebra]], [[wp:Cramer's rule|Cramer's rule]] is an explicit formula for the solution of a [[wp:system of linear equations|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 |
|||
:<math>\left\{\begin{matrix}a_1x + b_1y + c_1z&= {\color{red}d_1}\\a_2x + b_2y + c_2z&= {\color{red}d_2}\\a_3x + b_3y + c_3z&= {\color{red}d_3}\end{matrix}\right.</math> |
|||
which in matrix format is |
|||
<math>\begin{bmatrix} a_1 & b_1 & c_1 \\ a_2 & b_2 & c_2 \\ a_3 & b_3 & c_3 \end{bmatrix}\begin{bmatrix} x \\ y \\ z \end{bmatrix}=\begin{bmatrix} {\color{red}d_1} \\ {\color{red}d_2} \\ {\color{red}d_3} \end{bmatrix}.</math> |
|||
Then the values of <math>x, y</math> and <math>z</math> can be found as follows: |
|||
:<math>x = \frac{\begin{vmatrix} {\color{red}d_1} & b_1 & c_1 \\ {\color{red}d_2} & b_2 & c_2 \\ {\color{red}d_3} & b_3 & c_3 \end{vmatrix} } { \begin{vmatrix} a_1 & b_1 & c_1 \\ a_2 & b_2 & c_2 \\ a_3 & b_3 & c_3 \end{vmatrix}}, \quad y = \frac {\begin{vmatrix} a_1 & {\color{red}d_1} & c_1 \\ a_2 & {\color{red}d_2} & c_2 \\ a_3 & {\color{red}d_3} & c_3 \end{vmatrix}} {\begin{vmatrix} a_1 & b_1 & c_1 \\ a_2 & b_2 & c_2 \\ a_3 & b_3 & c_3 \end{vmatrix}}, \text{ and }z = \frac { \begin{vmatrix} a_1 & b_1 & {\color{red}d_1} \\ a_2 & b_2 & {\color{red}d_2} \\ a_3 & b_3 & {\color{red}d_3} \end{vmatrix}} {\begin{vmatrix} a_1 & b_1 & c_1 \\ a_2 & b_2 & c_2 \\ a_3 & b_3 & c_3 \end{vmatrix} }.</math> |
|||
;Task |
;Task |
||
Given the following system of equations: |
Given the following system of equations: |
||
<math> |
:<math> |
||
\begin{cases} |
\begin{cases} |
||
2x - 3y + z = 4 \\ |
2x - 3y + z = 4 \\ |
||
Line 57: | Line 69: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
<lang ruby>func det(A) { |
<lang ruby>func det(A) { |
||
gather { |
|||
A.each_index { |i| |
|||
var (p1=1, p2=1) |
|||
A.each_index { |i| |
|||
A[i].each_index { |j| |
|||
A[i |
p1 *= A[(j+i)%A.len][j] |
||
p2 *= A[(j+i)%A.len][-j] |
|||
} |
|||
take(p1-p2) |
|||
} |
} |
||
} -> sum |
|||
} |
|||
dt |
|||
} |
} |
||
func cramers_rule(A, terms) { |
func cramers_rule(A, terms) { |
||
gather { |
|||
A.each_index { |i| |
A.each_index { |i| |
||
var Ai = A.map{.map{_}} |
var Ai = A.map{.map{_}} |
||
terms.each_index { |j| |
terms.each_index { |j| |
||
Ai[j][i] = terms[j] |
Ai[j][i] = terms[j] |
||
} |
|||
take(det(Ai)) |
|||
} |
} |
||
} »/» det(A) |
|||
} |
|||
dxyz »/» det(A) |
|||
} |
} |
||
Revision as of 12:31, 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(@A) {
[+] gather for ^@A -> $i { my ($p1, $p2) = (1, 1); for ^@A[$i] -> $j { $p1 *= @A[($j+$i)%@A][$j]; $p2 *= @A[($j+$i)%@A][*-$j-1]; } take $p1-$p2; }
}
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) {
gather { A.each_index { |i| var (p1=1, p2=1) A[i].each_index { |j| p1 *= A[(j+i)%A.len][j] p2 *= A[(j+i)%A.len][-j] } take(p1-p2) } } -> sum
}
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