Here we introduce the basic elements of SymPy. You will find it favorable to be already familiar with classes and data types in Python.
The basic construction element to build a formula in SymPy is the symbol. As we saw in the introductory example, a symbol is created by the command symbols
. This SymPy command generates symbol objects from a given string:
x, y, mass, torque = symbols('x y mass torque')
It is actually a short form of following command:
symbol_list=[symbols(l) for l in 'x y mass torque'.split()]
followed by a unpacking step to obtain variables:
x, y, mass, torque = symbol_list
The arguments of the command define the string representation of the symbol. The variable name of the symbol is often chosen identical to its string representation, but this is not required by the language:
row_index=symbols('i',integer=True) print(row_index**2) # returns i**2
Here, we also defined that the symbol is assumed to be an integer.
An entire set of symbols can be defined in a very compact way:
integervariables = symbols('i:l', integer=True) dimensions = symbols('m:n', integer=True) realvariables = symbols('x:z', real=True)
Similarly, symbols for indexed variables can be defined by using the following:
A = symbols('A1:3(1:4)')
This gives a tuple of symbols,
The rules for the range of the indexes are those we saw earlier in this book when working with slices (refer Chapter 3, Container Types for more details).
Python evaluates operations on numbers directly and introduces unavoidably rounding errors. These would obstruct all symbolic calculations. This is avoided when we sympify
numbers:
1/3 # returns 0.3333333333333333 sympify(1)/sympify(3) # returns '1/3'
The sympify
command converts an integer to an object of type sympy.core.numbers.Integer
.
Instead of writing 1/3 as an operation of two integers, it can also be represented directly as a rational number by Rational(1,3)
.
SymPy distinguishes between defined and undefined functions. The term undefined functions (might be a bit misleading) refers to well-defined Python objects for generic functions that have no special properties.
An example of a function with special properties is atan
or the Lambda
function used in the introductory example of this chapter.
Note the different names for the different implementations of the same mathematical function: sympy.atan
and scipy.arctan
.
A symbol for an undefined function is created by giving the symbols
command an extra class argument:
f, g = symbols('f g', cls=Function)
The same can be achieved by using the Function
constructor:
f = Function('f') g = Function('g')
with undefined functions, we can evaluate general rules of calculus.
For example, let us evaluate the following expression:
This is symbolically computed in Python by using the following command:
x = symbols('x') f, g = symbols('f g', cls=Function) diff(f(x*g(x)),x)
When executed, the previous code returns the following as output:
This example shows how the product rule and the chain rule were applied.
We can even use undefined functions as a function in several variables, for example:
x = symbols('x:3') f(*x)
which returns the following output:
Note the use of the star operator to unpack a tuple to form f with arguments; refer to section Anonymous functions, Chapter 7, Functions
By using list comprehension, we can construct a list of all partial derivatives of f :
[diff(f(*x),xx) for xx in x]
This returns a list with the elements of (the gradient of f):
The command can also be rewritten by using the diff
method of the Function
object:
[f(*x).diff(xx) for xx in x]
Another method is Taylor series expansion :
x = symbols('x') f(x).series(x,0,n=4)
This returns Taylor's formula, together with the rest term expressed by the Landau symbol:
3.141.31.125