Next: , Previous: Complex expressions, Up: Methods and functions

### 5.14 Solving linear systems of equations

The function `lsolve()` provides a convenient wrapper around some matrix operations that comes in handy when a system of linear equations needs to be solved:

```     ex lsolve(const ex & eqns, const ex & symbols,
unsigned options = solve_algo::automatic);
```

Here, `eqns` is a `lst` of equalities (i.e. class `relational`) while `symbols` is a `lst` of indeterminates. (See The class hierarchy, for an exposition of class `lst`).

It returns the `lst` of solutions as an expression. As an example, let us solve the two equations `a*x+b*y==3` and `x-y==b`:

```     {
symbol a("a"), b("b"), x("x"), y("y");
lst eqns, vars;
eqns = a*x+b*y==3, x-y==b;
vars = x, y;
cout << lsolve(eqns, vars) << endl;
// -> {x==(3+b^2)/(b+a),y==(3-b*a)/(b+a)}
```

When the linear equations `eqns` are underdetermined, the solution will contain one or more tautological entries like `x==x`, depending on the rank of the system. When they are overdetermined, the solution will be an empty `lst`. Note the third optional parameter to `lsolve()`: it accepts the same parameters as `matrix::solve()`. This is because `lsolve` is just a wrapper around that method.