> This is one of the 100 recipes of the [IPython Cookbook](http://ipython-books.github.io/), the definitive guide to high-performance scientific computing and data science in Python.


# 15.2. Solving equations and inequalities


```python
from sympy import *
init_printing()
```


```python
var('x y z a')
```

Use the function solve to resolve equations (the right hand side is always 0).


```python
solve(x**2 - a, x)
```

You can also solve inequations. You may need to specify the domain of your variables. Here, we tell SymPy that x is a real variable.


```python
x = Symbol('x')
solve_univariate_inequality(x**2 > 4, x)
```

## Systems of equations

This function also accepts systems of equations (here a linear system).


```python
solve([x + 2*y + 1, x - 3*y - 2], x, y)
```

Non-linear systems are also supported.


```python
solve([x**2 + y**2 - 1, x**2 - y**2 - S(1)/2], x, y)
```

Singular linear systems can also be solved (here, there are infinitely many equations because the two equations are colinear).


```python
solve([x + 2*y + 1, -x - 2*y - 1], x, y)
```

Now, let's solve a linear system using matrices with symbolic variables.


```python
var('a b c d u v')
```

We create the augmented matrix, which is the horizontal concatenation of the system's matrix with the linear coefficients, and the right-hand side vector.


```python
M = Matrix([[a, b, u], [c, d, v]]); M
```


```python
solve_linear_system(M, x, y)
```

This system needs to be non-singular to have a unique solution, which is equivalent to say that the determinant of the system's matrix needs to be non-zero (otherwise the denominators in the fractions above are equal to zero).


```python
det(M[:2,:2])
```

> You'll find all the explanations, figures, references, and much more in the book (to be released later this summer).

> [IPython Cookbook](http://ipython-books.github.io/), by [Cyrille Rossant](http://cyrille.rossant.net), Packt Publishing, 2014 (500 pages).

53c2adf45eafa23c4ba40c2873593195b6ba2e3d
ipynb
Jupyter Notebook
notebooks/chapter15_symbolic/02_solvers.ipynb
hidenori-t/cookbook-code
750f546ed87b09d28532884b8074b96cd8d32a38
BSD-2-Clause
2015-01-01T18:15:54.000Z
2022-03-06T16:15:07.000Z
notebooks/chapter15_symbolic/02_solvers.ipynb
bndxn/cookbook-code
90c31341edccf039187e6a3809fb336f83bb758f
BSD-2-Clause
2015-02-25T22:08:09.000Z
2018-09-28T08:41:38.000Z
notebooks/chapter15_symbolic/02_solvers.ipynb
bndxn/cookbook-code
90c31341edccf039187e6a3809fb336f83bb758f
BSD-2-Clause
2015-01-02T13:53:11.000Z
2022-03-18T21:05:16.000Z
Qwen/Qwen-72B
1. YES
2. YES