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
$\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.$
which in matrix format is
$\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}.$
Then the values of $x, y$ and $z$ can be found as follows:
$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} }.$
TaskGiven the following system of equations:
$\begin{cases} 2w-x+5y+z=-3 \\ 3w+2x+2y-6z=-32 \\ w+3x+3y-z=-47 \\ 5w-2x-3y+3z=49 \\ \end{cases}$
solve for $w$, $x$, $y$ and $z$, using Cramer's rule.
cramersRule
is a function.
testString: assert(typeof cramersRule === 'function', 'cramersRule
is a function.');
- text: cramersRule([[2, -1, 5, 1], [3, 2, 2, -6], [1, 3, 3, -1], [5, -2, -3, 3]], [-3, -32, -47, 49])
should return [2, -12, -4, 1]
.
testString: assert.deepEqual(cramersRule(matrices[0], freeTerms[0]), answers[0], 'cramersRule([[2, -1, 5, 1], [3, 2, 2, -6], [1, 3, 3, -1], [5, -2, -3, 3]], [-3, -32, -47, 49])
should return [2, -12, -4, 1]
.');
- text: cramersRule([[3, 1, 1], [2, 2, 5], [1, -3, -4]], [3, -1, 2])
should return [1, 1, -1]
.
testString: assert.deepEqual(cramersRule(matrices[1], freeTerms[1]), answers[1], 'cramersRule([[3, 1, 1], [2, 2, 5], [1, -3, -4]], [3, -1, 2])
should return [1, 1, -1]
.');
```