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.

Basic usage

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.

Sheet
  1. Tech Cheat Sheets CLOUD Big data & Ai Cheat Sheets for AI, Machine Learning, Neural Networks, Big Data & Deep Learning I have been collecting AI cheat sheets for the last few months, and I’ve been sharing them with friends and colleagues from time to time. Recently, a lot of inquiries concerning the same sheets Continue reading 'Cheat Sheets for AI, Machine Learning, Neural Networks, Big.
  2. Quick cheatsheet. Run this bash script to install theano, lasagne, nolearn: (Testing machine: 64-bit Ubuntu 14.04) #!/bin/bash # Easy installation on Ubuntu 14.04.
  3. Repo using the bot: sympy/sympy Other info: The bot automatically gets release notes for each pull request from the pull request description, or lets the author know how to write it if it is missing. When the pull request is merged, it automatically adds the release notes for it to the release notes document on the SymPy wiki.

with the output:

We alredy see simplification for basic expresssion.

Factorization and expansion

with the expected output:

Sympy Cheat Sheet

Pretty printing

And the code:

with the output:

Computing the numeric result of a symbolic expression

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

Reading expressions from user input

Let’s write a simple program which computes the expanded product of two expressions:

with the following input / output:

Solving equations, inequalities and systems of equations

Sympy Cheat Sheet

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:

  1. Polynomial inequality: expression is a polynomial (can use expr.is_polynomial())
  2. Rational inequality: expression is a rational function of two polynomials (e.g. (x ** 2 + 4) / (x + 2); can use expr.is_rational_function() to deternime if the case)
  3. Univariate solver: one variable, nonlinear (e.g. involving functions like sin or cos)

Link to tutorial here

Plotting

Sympy Cheat Sheet

Sympy supports simplified plotting out of the box. This is a handy addition for when we don’t want to use matplotlib directly.

Sets

Beside FiniteSet which I exemplify below, sympy also includes support for infinite sets and intervals.

Sympy Cheat Sheet Download

Playing with probabilities and sets

Let’s define the following terms:

  1. Experiment - a test we want to perform (coin toss, for instance).
  2. Trial - a single run of an experiment.
  3. Sample space (S) - all possible outcomes of an experiment. For a coin toss is {head, tail}
  4. Event (E) - a set of outcomes we are testing for. For example, for a dice roll, event that the result is even is {2, 4, 6}

Obviously, for a uniform distribution, the probability of an event E is P(E) = len(E) / len(S)

Some formulas:

  1. Probability of event A and event B: P(A and B) = P(A intersect B) = P(A) * P(B)
  2. Probability of event A or event B: P(A or B) = P (A union B) = P(A) + P(B) - P(A) * P(B)
  3. Conditional probability (Bayes Theorem): P(A|B) = P(B|A) * P(A) / P(B). Speaking of Bayes theorem, this is a very interesting link: Base rate fallacy

Assumptions

The 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

Sympy Cheat Sheet Template

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.

Sympy Cheat Sheet

A more complex example - gradient descent

Sympy Cheat Sheet Free

Conclusion

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.