Skip to content

Numbers (SymPy)

SymPy is a Python library for symbolic algebra. It can interface with other Python libraries making it very powerful. On this page we demonstrate how to get started with SymPy by importing the library and working with numbers.

Let's begin by importing the SymPy package.

from sympy import *

The * means the namespace for all functions in SymPy are loaded, so we can call them directly without having to preface them with the library/package name.

Note: the commad from sympy import * is not best practice. It can be convenient when you want to do a quick calculation, but it can have unintended consequences - such as overwriting function names in other libraries.

If you are using SymPy in conjuction with other Python libraries you will want to use prefixes to indicate what library you intend to be used. For example, either use import sympy as sy, then each SymPy command will be prefixed with sy. (much like we did with NumPy and np.). For example, you would write sy.Matrix instead of just Matrix.

The best practice for working with SymPy is to import just the parts of the library you are going to use; for example:

from sympy import Rational, Float, Matrix

One thing we need to keep in mind when working in SymPy is the type of number we are working with. For example, if we enter 1/2 Python will automatically interpret this as a floating point number. It views it as a command to divide 1 by 2.

1/2
0.5

It it fair to say that if you are working with SymPy you probably want to work with numbers in their rational form. Especially when we start working with matrices over rational numbers. We'll start our dive into SymPy by looking at how to work with rational numbers.

SymPy Rational Numbers

To tell SymPy you want to work with numbers in their rational form use the Rational function.

a = Rational(1,2)

\(\frac{1}{2}\)

You can also use Rational(1/2) but sometimes unexpected things can happen due to Python converting the division to binary, then a decimal, then a sympy.Rational. To avoid weird behaviour pass input number as a string.

Rational('3/7')

\(\frac{3}{7}\)

Rational(3/7)

\(\frac{7720456504063707}{18014398509481984}\)

Let's look at the data type of the object created.

a = Rational(3,7)
type(a)
sympy.core.numbers.Rational

We can also construct and unsimplified fraction and SymPy will reduce it to lowest terms.

Rational(4,14)

\(\frac{2}{7}\)

Another way to construct rational numbers is to use the sympify command, which has the shortcut S. For example, 2 is interpreted by Python as an int, by S(2) is a SymPy Integer.

print( type(2) )
print( type(S(2)) )
<class 'int'>
<class 'sympy.core.numbers.Integer'>

Now we can construct a rational numbers as follows:

S(1)/2

\(\frac{1}{2}\)

Repeated decimal

Repeating decimals are entered as strings, with square brackets around the repeating part. Then we can sympify:

S('0.[3]')

\(\frac{1}{3}\)

Rational Arithmetic

Now that we know how to construction rational numbers (either with Rational or S), we can now do rational arithmetic.

Rational(4,14) + Rational(3,5)
S(4)/14 + S(3)/5

\(\frac{31}{35}\)

SymPy Floating Point Numbers

Sometimes you do want to use floating point, and you can specify this to. The simplest way is to just enter in a decimal value.

2.5

There is also a Float function in Sympy, which creates a sympy.Float object, not a core Python float object. The code block below illustrates the difference.

print( Float(S(3)/7) )
type( Float(S(3)/7) )
0.428571428571429
sympy.core.numbers.Float

print( float(S(3)/7) )
type( float(S(3)/7) )
0.42857142857142855
float

Construction of floats with scientific notation:

Float(2.5e10)

\(25000000000.0\)

Float(2.5e-10)

\(2.5 \cdot 10 ^{-10}\)

SymPy Algebra with Symbols

First we declare a variable a, and include the optional arguments real and constant.

a = Symbol('a', real = True, constant = True)

Now we can use a as a parameter in algebraic expressions.

a*Rational(2,14)-a*Rational(3,7)

\(-\frac{2a}{7}\)

To declare more than one variable at a time use the symbols function.

a,b,c = symbols('a,b,c')