You are previewing Ruby Cookbook.

# 2.11. Solving a System of Linear Equations

## Problem

You have a number of linear equations (that is, equations that look like "2x + 10y + 8z = 54"), and you want to figure out the solution: the values of x, y, and z. You have as many equations as you have variables, so you can be certain of a unique solution.

## Solution

Create two `Matrix` objects. The first `Matrix` should contain the coefficients of your equations (the 2, 10, and 8 of "2x + 10y + 8z = 54"), and the second should contain the constant results (the 54 of the same equation). The numbers in both matrices should be represented as floating-point numbers, rational numbers, or `BigDecimal` objects: anything other than plain Ruby integers.

Then invert the coefficient matrix with ` Matrix#inverse`, and multiply the result by the matrix full of constants. The result will be a third `Matrix` containing the solutions to your equations.

For instance, consider these three linear equations in three variables:

```	2x + 10y + 8z = 54
7y + 4z = 30
5x + 5y + 5z = 35```

To solve these equations, create the two matrices:

```	require 'matrix'
require 'rational'
coefficients = [[2, 10, 8], [0, 7, 4], [5, 5, 5]].collect! do |row|
row.collect! { |x| Rational(x) }
end
coefficients = Matrix[*coefficients]
# => Matrix[[Rational(2, 1), Rational(10, 1), Rational(8, 1)],
# =>        [Rational(0, 1), Rational(7, 1), Rational(4, 1)],
# =>        [Rational(5, 1), Rational(5, 1), Rational(5, 1)]]

constants = Matrix[[Rational(54)], [Rational(30)], [Rational(35)]]```

Take the inverse of the coefficient ...