Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.


To work with symbolic mathematics in Python we have chosen to use the

...

library SymPy. It aims to become a full-featured computer algebra system (CAS) while keeping the code as simple as possible in order to be comprehensible and easily extensible. SymPy is written entirely in Python.

Info

If you have not already installed SymPy, see our installation page, or find packages for installations at http://sympy.org/ alongside more detailed SymPy documentation. As NumPy is used a lot when working with SymPy, we also recommend checking it out.


Info

Want to get better at programming, check out our page Tips and tricks for coding.

...




Panel
borderColor#dfe1e5
bgColor#eff9ff
borderWidth2
titlePage content

Table of Contents

...

indent20px
styledisk
separatorklammer

Tutorial


Symbols

To make symbolic variables in SymPy you have to declare the variable explicitly:

Code Block
languagepy
>>> from sympy import *

>>> x = Symbol('x')
>>> y = Symbol('y')

You may then manipulate them as you want:

Code Block
languagepy
>>> x + y + x - y
2*x

>>> (x + y)**2
(x + y)**2

>>> ((x + y)**2).expand()
x**2 + 2*x*y + y**2

You can also substitute variables for numbers or other symbolic variables with subs(var, substitution).

Code Block
languagepy
>>> ((x + y)**2).subs(x, 1)
(y + 1)**2

>>> ((x + y)**2).subs(x, y)
4*y**2

Some regular constants are already included in SymPy as symbols, like e, pi and infiniteevalf() evaluates the symbols as floation-point numbers.

Code Block
languagepy
>>> pi**2
pi**2

>>> pi.evalf()
3.141592653589793238462643383

<<< E**2
exp(2)

>>> oo > 99999
True

>>> oo + 1
oo


Differentiation

You can differentiate any SymPy expression

...

using diff(func, var). Higher derivatives can be solved using diff(func, var, n).

Code Block
languagepy
>>> from sympy import *
>>> x = Symbol('x')

>>> diff(sin(x), x)
cos(x)

>>> diff(sin(2*x), x, 1)
2*cos(2*x)

>>> diff(sin(2*x), x, 2)
-4*sin(2*x)


Integration

SymPy has support for both indefinite and definite integration

...

.

Code Block
languagepy
>>> from sympy import *
>>> x, y = symbols('x y')

Indefinite integration of some elementary functions:

Code Block
languagepy
>>> integrate(6*x**5, x)
x**6

>>> integrate(log(x), x)
x*log(x) - x

>>> integrate(2*x + sinh(x), x)
x**2 + cosh(x)	

Definite integration:

Code Block
languagepy
>>> integrate(x**3, (x, -1, 1))
0

>>> integrate(sin(x), (x, 0, pi/2))
1

Some special integrals:

Code Block
languagepy
>>> integrate(exp(-x), (x, 0, oo))
1

>>> integrate(log(x), (x, 0, 1))
-1


Algebraic equations

SymPy is able to solve algebraic equations with one or several variables. solve(equation, variable)

...

 takes as first argument an equation that is supposed to be equaled to 0, and as second argument the

...

variable to solve for. In case of multiple equations and variables, solve

...

 returns a dictonary containing the results. Note that SymPy also handles complex numbers using the symbol I.

Code Block
languagepy
>>> from sympy import *
>>> x = Symbol('x')

>>> solve(x**2 - 1, x)
[-1, 1]

...

>>> solve([x + 5*y - 2, -3*x + 6*y - 15], [x, y])
{x: -3, y: 1}

...

>>> solve(Eq(x**4, 1), x)
[-1, 1, -I, I]

nsolve(function, [variables], x0) is a useful tool to solve nonlinear equation systems numerically, where x0 is a starting vector close to the solution. If there is only one variable, the second argument may be left out.

Code Block
languagepy
>>> nsolve(sin(x), x, 2)
3.14159265358979

>>> nsolve(sin(x), 2)
3.14159265358979

>>> nsolve(sin(x**2)/(pi - x), x, pi.evalf()/2)
1.77245385090552

However, if you would like to compute all solutions to a problem, solveset is the way to go. Considering the difficulty of some of the return values computed by solveset, we highly recommend you to read the official documentation. We will only cover the function brifely, although some more advanced examples will be added to the Exercises and solutions, symbolic mathematics in Python.

Code Block
languagepy
>>> solveset(x - x, x) # Solving the function x = x for x
S.Complexes

The return value of solveset is alwyas a Set, in the case above the set class Complexes, which represents the set of all complex numbers, 

 Another example is :

Code Block
languagepy
>>> solveset(sin(x), x)		# Solving sin(x) = 0 for x
Union(ImageSet(Lambda(_n, 2*_n*pi), Integers), ImageSet(Lambda(_n, 2*_n*pi + pi), Integers))

As we can see, the output may seem a bit overwhelming at first. Let's try to interpret it.

"Union" tells us that the answer consists of several independent sets. "ImageSet" is simply a set class, representing the image of a set under a mathematical function. The Lambda function (no good documentation page found) is used in ImageSets to describe the solutions using variables (here "_n"), the functions containing the variables (here "2*_n*pi") and the range appliable to the variables (here the set Integers, representing all integers). All things considered, the returned solution to 

is

, which may also be written as


Exercises and solutions

If you want have a look at some exercises and solutions regarding SymPy and sybolic mathematics in Python, check out Exercises and solutions, symbolic mathematics in Python.

Info

The exercises provided will have a varying degree of difficulty, thus including more advanced and detailed methods than described here.