# Expression Trees¶

What's an expression tree?

In [1]:

    import pymbolic.primitives as p
    x = p.Variable("x")
    y = p.Variable("y")
    x

Out[1]:

    Variable('x')

Let's look what happens with a simple expression:

In [2]:

    x+5

Out[2]:

    Sum((Variable('x'), 5))

It does not get evaluated.

Let's look at its type and structure in more detail.

In [3]:

    u = x+5

In [4]:

    type(u)

Out[4]:

    pymbolic.primitives.Sum

In [5]:

    u.children

Out[5]:

    (Variable('x'), 5)

OK, easy. What if we introduce a product?

In [6]:

    u = x + 4*y
    u

Out[6]:

    Sum((Variable('x'), Product((4, Variable('y')))))

In [7]:

    u.children[0]

Out[7]:

    Variable('x')

In [8]:

    u.children[1]

Out[8]:

    Product((4, Variable('y')))

In [9]:

    u.children[1].children[0]

Out[9]:

    4

In [22]:

    u.children[1].children[1]

Out[22]:

    Variable('y')

This structure is a called a tree, because there is a root and branches.