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.
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:
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.
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.
\(\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.
Let's look at the data type of the object created.
We can also construct and unsimplified fraction and SymPy will reduce it to lowest terms.
\(\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
.
Now we can construct a rational numbers as follows:
\(\frac{1}{2}\)
Repeated decimal
Repeating decimals are entered as strings, with square brackets around the repeating part. Then we can sympify
:
\(\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.
\(\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.
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.
Construction of floats with scientific notation:
\(25000000000.0\)
\(2.5 \cdot 10 ^{-10}\)
SymPy Algebra with Symbols
First we declare a variable a
, and include the optional arguments real
and constant
.
Now we can use a
as a parameter in algebraic expressions.
\(-\frac{2a}{7}\)
To declare more than one variable at a time use the symbols
function.