To begin, we should make something about SymPy
clear. SymPy
is nothing more than a Python library, like NumPy
, Django
, or even modules in the Python standard library sys
or re
. What this means is that SymPy
does not add anything to the Python language. Limitations that are inherent in the Python language are also inherent in SymPy
. It also means that SymPy
tries to use Python idioms whenever possible, making programming with SymPy
easy for those already familiar with programming with Python. As a simple example, SymPy
uses Python syntax to build expressions. Implicit multiplication (like 3x
or 3 x
) is not allowed in Python, and thus not allowed in SymPy
. To multiply 3
and x
, you must type 3*x
with the *
.
Simboli
One consequence of this fact is that SymPy can be used in any environment where Python is available. We just import it, like we would any other library:
This imports all the functions and classes from SymPy into our interactive Python session. Now, suppose we start to do a computation.
Oops! What happened here? We tried to use the variable x
, but it tells us that x
is not defined. In Python, variables have no meaning until they are defined. SymPy is no different. Unlike many symbolic manipulation systems you may have used, in SymPy, variables are not defined automatically. To define variables, we must use symbols
.
symbols
takes a string of variable names separated by spaces or commas, and creates Symbols
out of them. We can then assign these to variable names. Later, we will investigate some convenient ways we can work around this issue. For now, let us just define the most common variable names, x
, y
, and z
, for use through the rest of this section
As a final note, we note that the name of a Symbol and the name of the variable it is assigned to need not have anything to do with one another.
Here we have done the very confusing thing of assigning a Symbol with the name a
to the variable b
, and a Symbol of the name b
to the variable a
. Now the Python variable named a points to the SymPy Symbol
named b
, and visa versa. How confusing. We could have also done something like
This also shows that Symbols
can have names longer than one character if we want.
Usually, the best practice is to assign Symbols
to Python variables of the same name, although there are exceptions: Symbol names can contain characters that are not allowed in Python variable names, or may just want to avoid typing long names by assigning Symbols
with long names to single letter Python variables.
To avoid confusion, throughout this tutorial, Symbol names and Python variable names will always coincide. Furthermore, the word “Symbol” will refer to a SymPy Symbol and the word “variable” will refer to a Python variable.
Finally, let us be sure we understand the difference between SymPy Symbols and Python variables. Consider the following:
What do you think the output of this code will be? If you thought 3
, you’re wrong. Let’s see what really happens
Changing x
to 2
had no effect on expr
. This is because x = 2
changes the Python variable x
to 2
, but has no effect on the SymPy Symbol x
, which was what we used in creating expr
. When we created expr
, the Python variable x
was a Symbol. After we created, it, we changed the Python variable x
to 2
. But expr
remains the same. This behavior is not unique to SymPy. All Python programs work this way: if a variable is changed, expressions that were already created with that variable do not change automatically. For example
In this example, if we want to know what expr
is with the new value of x
, we need to reevaluate the code that created expr
, namely, expr = x + 1
. This can be complicated if several lines created expr
. One advantage of using a symbolic computation system like SymPy is that we can build a symbolic representation for expr
, and then substitute x
with values. The correct way to do this in SymPy is to use subs
, which will be discussed in more detail later.