Continuo da qui, copio qui.
In this section, we discuss some ways that we can perform advanced manipulation of expressions.
Capire l’albero delle espressioni
Before we can do this, we need to understand how expressions are represented in SymPy. A mathematical expression is represented as a tree. Let us take the expression 2x+xy
, i.e., 2**x + x*y
. We can see what this expression looks like internally by using srepr
.
Quick Tip: To play with the srepr
form of expressions in the SymPy Live shell, change the output format to Repr
in the settings.
The easiest way to tear this apart is to look at a diagram of the expression tree:
Note: The above diagram was made using Graphviz and the dotprint
function.
First, let’s look at the leaves of this tree. Symbols are instances of the class Symbol
. While we have been doing
x = symbols('x')
we could have also done
x = Symbol('x')
Either way, we get a Symbol
with the name “x”
[We have been using symbols
instead of Symbol
because it automatically splits apart strings into multiple Symbols
. symbols('x y z')
returns a tuple of three Symbols
. Symbol('x y z')
returns a single Symbol
called x y z
]. For the number in the expression, 2, we got Integer(2)
. Integer is the SymPy class for integers. It is similar to the Python built-in type int
, except that Integer
plays nicely with other SymPy types.
When we write 2**x
, this creates a Pow
object. Pow
is short for “power”.
We could have created the same object by calling Pow(2, x)
Note that in the srepr
output, we see Integer(2)
, the SymPy version of integers, even though technically, we input 2, a Python int
. In general, whenever you combine a SymPy object with a non-SymPy object via some function or operation, the non-SymPy object will be converted into a SymPy object. The function that does this is sympify [Technically, it is an internal function called sympify
, which differs from sympify
in that it does not convert strings. x + '2'
is not allowed].
We have seen that 2**x
is represented as Pow(2, x)
. What about x*y
? As we might expect, this is the multiplication of x
and y
. The SymPy class for multiplication is Mul
.
Thus, we could have created the same object by writing Mul(x, y)
.
Now we get to our final expression, 2**x + x*y
. This is the addition of our last two objects, Pow(2, x)
, and Mul(x, y)
. The SymPy class for addition is Add
, so, as you might expect, to create this object, we use Add(Pow(2, x), Mul(x, y))
.
SymPy expression trees can have many branches, and can be quite deep or quite broad. Here is a more complicated example
Here is a diagram
This expression reveals some interesting things about SymPy expression trees. Let’s go through them one by one.
Let’s first look at the term x**2
. As we expected, we see Pow(x, 2)
. One level up, we see we have Mul(-1, Pow(x, 2))
. There is no subtraction class in SymPy. x - y
is represented as x + -y
, or, more completely, x + -1*y
, i.e., Add(x, Mul(-1, y))
.
Next, look at 1/y
. We might expect to see something like Div(1, y)
, but similar to subtraction, there is no class in SymPy for division. Rather, division is represented by a power of -1
. Hence, we have Pow(y, -1)
. What if we had divided something other than 1 by y
, like x/y
? Let’s see.
We see that x/y
is represented as x*y**-1
, i.e., Mul(x, Pow(y, -1))
.
Finally, let’s look at the sin(x*y)/2
term. Following the pattern of the previous example, we might expect to see Mul(sin(x*y)
, Pow(Integer(2), -1)). But instead, we have Mul(Rational(1, 2), sin(x*y))
. Rational
numbers are always combined into a single term in a multiplication, so that when we divide by 2, it is represented as multiplying by 1/2.
Finally, one last note. You may have noticed that the order we entered our expression and the order that it came out from srepr
or in the graph were different. You may have also noticed this phenomenon earlier in the tutorial. For example
This because in SymPy, the arguments of the commutative operations Add
and Mul
are stored in an arbitrary (but consistent!) order, which is independent of the order inputted (if you’re worried about noncommutative multiplication, don’t be. In SymPy, you can create noncommutative Symbols
using Symbol('A', commutative=False)
, and the order of multiplication for noncommutative Symbols
is kept the same as the input). Furthermore, as we shall see in the next section, the printing order and the order in which things are stored internally need not be the same either.
In general, an important thing to keep in mind when working with SymPy expression trees is this: the internal representation of an expression and the way it is printed need not be the same. The same is true for the input form. If some expression manipulation algorithm is not working in the way you expected it to, chances are, the internal representation of the object is different from what you thought it was.
Quick Tip: The way an expression is represented internally and the way it is printed are often not the same.
OK (quasi) panico 😯