Ability to perform symbolic computations is a crucial component of any mathematics-oriented package. Symbolic mathematics is used to work with complex expressions, sets and probabilities, perform integrals or derivatives, plot charts based on user input, all without explicit numeric computations. This way, the Python interpreter becomes very much like a piece of paper on which one can jot down equations. To exemplify these, by the end of the article I will implement a short gradient descent function to demonstrate the power of sympy
to code easy-to-work-with generic algorithms.
SymPy; SymPy Live GitHub sympy/sympy - A computer algebra system written in pure Python GitHub sympy/symengine - SymEngine is a fast symbolic manipulation library, written in C; Wikipedia - SymPy.
with the output:
We alredy see simplification for basic expresssion.
with the expected output:
And the code:
with the output:
with the output of 9
We can also use expression substitution, like this:
The first line outputs y**2 + 2*y*(y - 1) + (y - 1)**2
while the second line simplifies the expression to 4*y**2 - 4*y + 1
Let’s write a simple program which computes the expanded product of two expressions:
with the following input / output:
with the following output if ran from the ipython console
The solve
function is not limited only to polynomials. For example, solve(sin(x)/x)
will correctly output the value [pi]
- docs
Another example for solving more complex equations:
And the output is [asin(y*z) + pi, -asin(y*z)]
. If we want to obtain a numeric result, we can do ret = sp.solve(eq, x)
and then [r.subs({y:0.4, z:-0.3}) for r in ret]
.
For a system of equations, it works like this:
Solving single variable inequalities is a little bit more complex as we need to clasify them according to the solver involved:
expr.is_polynomial()
)(x ** 2 + 4) / (x + 2)
; can use expr.is_rational_function()
to deternime if the case)sin
or cos
)Link to tutorial here
Sympy supports simplified plotting out of the box. This is a handy addition for when we don’t want to use matplotlib directly.
Beside FiniteSet
which I exemplify below, sympy
also includes support for infinite sets and intervals.
Let’s define the following terms:
{head, tail}
{2, 4, 6}
Obviously, for a uniform distribution, the probability of an event E is P(E) = len(E) / len(S)
Some formulas:
P(A and B) = P(A intersect B) = P(A) * P(B)
P(A or B) = P (A union B) = P(A) + P(B) - P(A) * P(B)
P(A|B) = P(B|A) * P(A) / P(B)
. Speaking of Bayes theorem, this is a very interesting link: Base rate fallacyThe behavior of symbols can be modified through what is called assumptions. Below is a list of assumptions with their default behavior and an usage example.
### Calculus
The following code should be run line by line in an interpreter like IPython. For my own play, I am using select line + CTRL+ENTER
in Spyder.
A more complex example which employs both derivative and integrals in the same formula: computing the length of the x**2
curve between -1 and 1. Please note that .doit()
may only called once.
Length of the curve is computed by: Integral(sqrt(1 + (df/dx)**2))
on the desired domain.
I wrote this article as a short cheat-sheet for myself. Python is a great tool for mathemathics, not only for the numeric but also for the symbolic domain. Libraries like sympy
make it both a powerful tool to write large programs but also a useful super easy-to-use desktop calculator. Of course, sympy
is much larger than presented here, but the article should be helpful for a quicker start next time you are in front of a mathematical problem.