Plotting with filling

The SymPy CAS can be installed on virtually any computer with Python 2.6 or above. SymPy does require mpmath Python library to be installed first. The current recommended method of installation is through Anaconda, which includes mpmath, as well as several other useful libraries. Alternatively, executables are available for Windows, and some Linux distributions have SymPy packages available.

You do not need to install SymPy to try it. You can use the online shell at http://live.sympy.org. You can just use the SymPy Live Sphinx extension to run the code blocks in the browser. For example, click on the green “Run code block in SymPy Live” button below

Learning SymPy

Everyone has different ways of understanding the code written by others.

SymPy�s Architecture

We try to make the sources easily understandable, so you can look into the sources and read the doctests, it should be well documented and if you don�t understand something, ask on the mailinglist.

You can find all the decisions archived in the issues, to see rationale for doing this and that.

Basics

All symbolic things are implemented using subclasses of the Basic class. First, you need to create symbols using Symbol("x") or numbers using Integer(5) or Float(34.3). Then you construct the expression using any class from SymPy. For example Add(Symbol("a"), Symbol("b")) gives an instance of the Add class. You can call all methods, which the particular class supports.

For easier use, there is a syntactic sugar for expressions like:

cos(x) + 1 is equal to cos(x).__add__(1) is equal to Add(cos(x), Integer(1))

or

2/cos(x) is equal to cos(x).__rdiv__(2) is equal to Mul(Rational(2), Pow(cos(x), Rational(-1))).

So, you can write normal expressions using python arithmetics like this:

a = Symbol("a")
b = Symbol("b")
e = (a + b)**2
print e

but from the SymPy point of view, we just need the classes Add, Mul, Pow, Rational, Integer.

Automatic evaluation to canonical form

For computation, all expressions need to be in a canonical form, this is done during the creation of the particular instance and only inexpensive operations are performed, necessary to put the expression in the canonical form. So the canonical form doesn�t mean the simplest possible expression. The exact list of operations performed depend on the implementation. Obviously, the definition of the canonical form is arbitrary, the only requirement is that all equivalent expressions must have the same canonical form. We tried the conversion to a canonical (standard) form to be as fast as possible and also in a way so that the result is what you would write by hand - so for example b*a + -4 + b + a*b + 4 + (a + b)**2 becomes 2*a*b + b + (a + b)**2.

Whenever you construct an expression, for example Add(x, x), the Add.__new__() is called and it determines what to return. In this case:

>>> from sympy import Add
>>> from sympy.abc import x
>>> e = Add(x, x)
>>> e
2*x

>>> type(e)
<class 'sympy.core.mul.Mul'>

e is actually an instance of Mul(2, x), because Add.__new__() returned Mul.

We made the following decision in SymPy: a = Symbol("x") and another b = Symbol("x") (with the same string �x�) is the same thing, i.e. a == b is True. We chose a == b, because it is more natural - exp(x) == exp(x) is also True for the same instance of x but different instances of exp, so we chose to have exp(x) == exp(x) even for different instances of x.

Sometimes, you need to have a unique symbol, for example as a temporary one in some calculation, which is going to be substituted for something else at the end anyway. This is achieved using Dummy("x"). So, to sum it up:

>>> from sympy import Symbol, Dummy
>>> Symbol("x") == Symbol("x")
True

>>> Dummy("x") == Dummy("x")
False

Debugging

Starting with 0.6.4, you can turn on/off debug messages with the environment variable SYMPY_DEBUG, which is expected to have the values True or False. For example, to turn on debugging, you would issue:

[user@localhost]: SYMPY_DEBUG=True ./bin/isympy

Functions

Improving the docs