**Ricorsione attraverso l’albero delle espressioni**

Now that you know how expression trees work in SymPy, let’s look at how to dig our way through an expression tree. Every object in SymPy has two very important attributes, ** func**, and

**.**

`args`

`func`

** func** is the head of the object. For example,

**.func is**

`(x*y)`

**. Usually it is the same as the class of the object (though there are exceptions to this rule).**

`Mul`

Two notes about ** func**. First, the class of an object need not be the same as the one used to create it. For example

We created ** Add(x, x)**, so we might expect

**to be**

`expr.func`

**, but instead we got**

`Add`

**. Why is that? Let’s take a closer look at**

`Mul`

**.**

`expr`

** Add(x, x)**, i.e.,

**, was automatically converted into**

`x + x`

**, i.e.,**

`Mul(2, x)`

**, which is a**

`2*x`

**. SymPy classes make heavy use of the**

`Mul`

**class constructor, which, unlike**

`__new__`

**, allows a different class to be returned from the constructor.**

`__init__`

Second, some classes are special-cased, usually for efficiency reasons [note 3].

* Note 3*: Classes like

**and**

`One`

**are singletonized, meaning that only one object is ever created, no matter how many times the class is called. This is done for space efficiency, as these classes are very common. For example,**

`Zero`

**might occur very often in a sparse matrix represented densely. As we have seen,**

`Zero`

**occurs any time we have**

`NegativeOne`

**or**

`-x`

**. It is also done for speed efficiency because singletonized objects can be compared by is. The unique objects for each singletonized class can be accessed from the**

`1/x`

**object.**

`S`

For the most part, these issues will not bother us. The special classes ** Zero**,

**,**

`One`

**, and so on are subclasses of**

`NegativeOne`

**, so as long as you use isinstance, it will not be an issue.**

`Integer`

`args`

** args** are the top-level arguments of the object.

**would be**

`(x*y).args`

**. Let’s look at some examples**

`(x, y)`

From this, we can see that ** expr == Mul(3, y**2, x)**. In fact, we can see that we can completely reconstruct

**from its**

`expr`

**and its**

`func`

**.**

`args`

Note that although we entered ** 3*y**2*x**, the args are

**. In a**

`(3, x, y**2)`

**, the**

`Mul`

**coefficient will come first in the**

`Rational`

**, but other than that, the order of everything else follows no special pattern. To be sure, though, there is an order.**

`args`

** Mul**’s args are sorted, so that the same

**will have the same**

`Mul`

**. But the sorting is based on some criteria designed to make the sorting unique and efficient that has no mathematical significance.**

`args`

The ** srepr** form of our

**is**

`expr`

**. What if we want to get at the args of**

`Mul(3, x, Pow(y, 2))`

**. Notice that the**

`Pow(y, 2)`

**is in the third slot of**

`y**2`

**, i.e.,**

`expr.args`

**.**

`expr.args[2]`

So to get the ** args** of this, we call

**.**

`expr.args[2].args`

Now what if we try to go deeper. What are the ** args** of

**. Or**

`y`

**. Let’s see.**

`2`

They both have empty ** args**. In SymPy, empty

**signal that we have hit a leaf of the expression tree.**

`args`

So there are two possibilities for a SymPy expression. Either it has empty ** args**, in which case it is a leaf node in any expression tree, or it has

**, in which case, it is a branch node of any expression tree. When it has**

`args`

**, it can be completely rebuilt from its**

`args`

**and its**

`func`

**. This is expressed in the key invariant.**

`args`

**Key invariant**

Every well-formed SymPy expression must either have empty ** args** or satisfy

**.**

`expr == expr.func(*expr.args)`

(Recall that in Python if ** a** is a tuple, then

**means to call**

`f(*a)`

**with arguments from the elements of**

`f`

**, e.g.,**

`a`

**is the same as**

`f(*(1, 2, 3))`

**.)**

`f(1, 2, 3)`

This key invariant allows us to write simple algorithms that walk expression trees, change them, and rebuild them into new expressions. Tutto questo nel prossimo post.