Cramer's Rule - ben-ng/linalg GitHub Wiki

Solving a system of equations

If you want to solve the system

{{a, b}, {c, d}} . {x, y} == {u, v}

You can use Cramer's Rule, which says the solutions are given by:

x = Det[{{u, b}, {v, d}}] / Det[{{a, b}, {c, d}}]
y = Det[{{a, u}, {c, v}}] / Det[{{a, b}, {c, d}}]

The way to remember this is that you take the given point (in this case, {u, v}) and overwrite the first column of the coefficient matrix with it. Find the determinant of that, and divide it by the determinant of the coefficient matrix. That gives you the answer for the first variable.

Do that for each component of the point you want, overwriting successive columns of the coefficient matrix with the solution point, and you have a complete solution.

The neat thing about this? You can solve for just the variable you care about.

Finding the inverse of a matrix

We can also use Cramer's rule to find a nice formula for the inverse of a matrix.

If we apply Cramer's rule to a system {{a, b}, {c, d}} . {x, y} == {u, v}

We'll find that {x, y} == { { (du - bv) / (ad - bc) }, { (-cu + av) / (ad - bc) } }

Pause for a moment to recall: one method of finding the solution to a system of equations is using the inverse of the matrix:

A . {x, y} = {u, v}
Inverse[A] . A . {x, y} = Inverse[A] . {u, v}
{x, y} = Inverse[A] . {u, v}

We now have two things we know that {x, y} must be equal to:

{x, y} == { { (du - bv) / (ad - bc) }, { (-cu + av) / (ad - bc) } }
{x, y} == Inverse[A] . {u, v}

Therefore,

Inverse[A] . {u, v}
  == { { (du - bv) / (ad - bc) }, { (-cu + av) / (ad - bc) } }
     // We can pull out ad - bc, which if you recall, is Det[A]
  == { { du - bv }, { -cu + av } } / Det[A]
     // Note that the last result is really the dot product of a matrix and {u, v}
  == ( { { d, -b }, { -c, a } } . {u, v} ) / Det[A]
     // Now we can use the linearity of matrix hits to isolate {u, v}
  == ( { { d, -b }, { -c, a } }  / Det[A] ) . {u, v}

At this point, we can remove {u, v} from both sides to see that

Inverse[A] == { { d, -b }, { -c, a } }  / Det[A]

Neat!