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 ** 2**, i.e.,

^{x}+xy

**. We can see what this expression looks like internally by using**

`2**x + x*y`

`srepr`

.* Quick Tip*: To play with the

**form of expressions in the SymPy Live shell, change the output format to**

`srepr`

**in the settings.**

`Repr`

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

**function.**

`dotprint`

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

**[We have been using**

`“x”`

**instead of**

`symbols`

**because it automatically splits apart strings into multiple**

`Symbol`

**.**

`Symbols`

**returns a tuple of three**

`symbols('x y z')`

**.**

`Symbols`

**returns a single**

`Symbol('x y z')`

**called**

`Symbol`

**]. For the number in the expression, 2, we got**

`x y z`

**. Integer is the SymPy class for integers. It is similar to the Python built-in type**

`Integer(2)`

**, except that**

`int`

**plays nicely with other SymPy types.**

`Integer`

When we write ** 2**x**, this creates a

**object.**

`Pow`

**is short for “power”.**

`Pow`

We could have created the same object by calling `Pow(2, x)`

Note that in the ** srepr** output, we see

**, the SymPy version of integers, even though technically, we input 2, a Python**

`Integer(2)`

**. 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**

`int`

**, which differs from**

`sympify`

**in that it does not convert strings.**

`sympify`

**is not allowed].**

`x + '2'`

We have seen that ** 2**x** is represented as

**. What about**

`Pow(2, x)`

**? As we might expect, this is the multiplication of**

`x*y`

**and**

`x`

**. The SymPy class for multiplication is**

`y`

**.**

`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,

**, and**

`Pow(2, x)`

**. The SymPy class for addition is**

`Mul(x, y)`

**, so, as you might expect, to create this object, we use**

`Add`

**.**

`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

**. One level up, we see we have**

`Pow(x, 2)`

**. There is no subtraction class in SymPy.**

`Mul(-1, Pow(x, 2))`

**is represented as**

`x - y`

**, or, more completely,**

`x + -y`

**, i.e.,**

`x + -1*y`

**.**

`Add(x, Mul(-1, y))`

Next, look at ** 1/y**. We might expect to see something like

**, but similar to subtraction, there is no class in SymPy for division. Rather, division is represented by a power of**

`Div(1, y)`

**. Hence, we have**

`-1`

**. What if we had divided something other than 1 by**

`Pow(y, -1)`

**, like**

`y`

**? Let’s see.**

`x/y`

We see that ** x/y** is represented as

**, i.e.,**

`x*y**-1`

**.**

`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

**, Pow(Integer(2), -1)). But instead, we have**

`Mul(sin(x*y)`

**.**

`Mul(Rational(1, 2), sin(x*y))`

**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.**

`Rational`

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

**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**

`Mul`

**using**

`Symbols`

**, and the order of multiplication for noncommutative**

`Symbol('A', commutative=False)`

**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.**

`Symbols`

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 😯

## Trackback

[…] da qui, copio […]