# Advanced Expression Manipulation


```
import sys
import os
sys.path.insert(1, os.path.join(os.path.pardir, "ipython_doctester"))
from sympy import *
from ipython_doctester import test
x, y, z = symbols('x y z')
```

For each exercise, fill in the function according to its docstring. Execute the cell to see if you did it right. 

## Creating expressions from classes

Create the following objects without using any mathematical operators like `+`, `-`, `*`, `/`, or `**` by explicitly using the classes `Add`, `Mul`, and `Pow`.  You may use `x` instead of `Symbol('x')` and `4` instead of `Integer(4)`.

$$x^2 + 4xyz$$
$$x^{(x^y)}$$
$$x - \frac{y}{z}$$



```
@test
def explicit_classes1():
    """
    Returns the expression x**2 + 4*x*y*z, built using SymPy classes explicitly.

    >>> explicit_classes1()
    x**2 + 4*x*y*z
    """

```


```
@test
def explicit_classes2():
    """
    Returns the expression x**(x**y), built using SymPy classes explicitly.

    >>> explicit_classes2()
    x**(x**y)
    """

```


```
@test
def explicit_classes3():
    """
    Returns the expression x - y/z, built using SymPy classes explicitly.

    >>> explicit_classes3()
    x - y/z
    """

```

## Nested args


```
expr = x**2 - y*(2**(x + 3) + z)
```

Use nested `.args` calls to get the 3 in expr.


```
@test
def nested_args():
    """
    Get the 3 in the above expression.

    >>> nested_args()
    3
    """

```

## Traversal 

Write a post-order traversal function that prints each node.


```
@test
def post(expr):
    """
    Post-order traversal

    >>> expr = x**2 - y*(2**(x + 3) + z)
    >>> post(expr)
    -1
    y
    2
    3
    x
    x + 3
    2**(x + 3)
    z
    2**(x + 3) + z
    -y*(2**(x + 3) + z)
    x
    2
    x**2
    x**2 - y*(2**(x + 3) + z)
    """

```


```
for i in postorder_traversal(expr):
    print i
```

3b252b71f9fcad47ffb684bc8e74ebf103494962
ipynb
Jupyter Notebook
tutorial_exercises/Advanced Expression Manipulation.ipynb
certik/scipy-2013-tutorial
26a1cab3a16402afdc20088cedf47acd9bc58483
BSD-3-Clause
2015-02-28T08:53:05.000Z
2021-12-05T05:37:59.000Z
sympy/Advanced Expression Manipulation.ipynb
certik/scipy-in-13
418c139ab6e1b0c9acd53e7e1a02b8b930005096
BSD-3-Clause
2021-04-17T15:05:46.000Z
2021-04-17T15:05:46.000Z
sympy/Advanced Expression Manipulation.ipynb
certik/scipy-in-13
418c139ab6e1b0c9acd53e7e1a02b8b930005096
BSD-3-Clause
2015-03-11T00:25:21.000Z
2021-08-25T14:52:40.000Z
Qwen/Qwen-72B
1. YES
2. YES