This is one of the 100 recipes of the IPython Cookbook, the definitive guide to high-performance scientific computing and data science in Python.
from sympy import * init_printing()
var('x y z a')
Use the function solve to resolve equations (the right hand side is always 0).
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.
x = Symbol('x') solve_univariate_inequality(x**2 > 4, x)
This function also accepts systems of equations (here a linear system).
solve([x + 2*y + 1, x - 3*y - 2], x, y)
Non-linear systems are also supported.
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).
solve([x + 2*y + 1, -x - 2*y - 1], x, y)
Now, let's solve a linear system using matrices with symbolic variables.
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.
M = Matrix([[a, b, u], [c, d, v]]); M
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).