# Simplification Solutions


```python
from sympy import *
x, y, z = symbols('x y z')
init_printing()
```

For each exercise, fill in the function according to its docstring. 

## Polynomial/Rational Function Simplification

In each exercise, apply specific simplification functions to get the desired result.


```python
def polysimp1(expr):
    """
    >>> polysimp1(cos(x)*sin(x) + cos(x))
    (sin(x) + 1)*cos(x)
    >>> polysimp1(cos(x)*sin(x) + cos(x) + 1)
    (sin(x) + 1)*cos(x) + 1
    """
    return collect(expr, cos(x))
```


```python
polysimp1(cos(x)*sin(x) + cos(x))
```


```python
polysimp1(cos(x)*sin(x) + cos(x) + 1)
```


```python
def polysimp2(expr):
    """
    >>> polysimp2((2*x + 1)/(x**2 + x))
    1/(x + 1) + 1/x
    >>> polysimp2((x**2 + 3*x + 1)/(x**3 + 2*x**2 + x))
    1/(x**2 + 2*x + 1) + 1/x
    """
    return expand(apart(expr))
```


```python
polysimp2((2*x + 1)/(x**2 + x))
```


```python
polysimp2((x**2 + 3*x + 1)/(x**3 + 2*x**2 + x))
```

## Powers

In each exercise, apply specific simplification functions to get the desired result. 


```python
def powersimp1(expr):
    """
    >>> powersimp1(exp(x)*(exp(y) + 1))
    exp(x) + exp(x + y)
    """
    return powsimp(expand(expr))
```


```python
powersimp1(exp(x)*(exp(y) + 1))
```


```python
def powersimp2(expr):
    """
    >>> powersimp2(2**x*x**x)
    (2*x)**x
    >>> powersimp2(x**x*x**x)
    (x**2)**x
    """
    return powsimp(expr, force=True)
```


```python
powersimp2(2**x*x**x)
```


```python
powersimp2(x**x*x**x)
```


```python
def powersimp3(expr):
    """
    >>> a, b, c = symbols('a b c')
    >>> powersimp3((a**b)**c)
    a**(b*c)
    >>> powersimp3((a**b)**(c + 1))
    a**(b*c + b)
    """
    return powdenest(expand_power_exp(expr), force=True)
```


```python
a, b, c = symbols('a b c')
```


```python
powersimp3((a**b)**c)
```


```python
powersimp3((a**b)**(c + 1))
```

## Logs


```python
def logsimp1(expr):
    """
    >>> a, b = symbols('a b', positive=True)
    >>> logsimp1(log(x**y*a**b))
    y*log(x) + log(a**b)
    >>> logsimp1(log(x*y*a*b))
    log(x) + log(y) + log(a*b)
    """
    return logcombine(expand_log(expr, force=True))
```


```python
a, b = symbols('a b', positive=True)
```


```python
logsimp1(log(x**y*a**b))
```


```python
logsimp1(log(x*y*a*b))
```

## Miscellaneous  


```python
def miscsimp1(expr):
    """
    >>> miscsimp1(sin(x + y))
    2*(-tan(x/2)**2 + 1)*tan(y/2)/((tan(x/2)**2 + 1)*(tan(y/2)**2 + 1)) + 2*(-tan(y/2)**2 + 1)*tan(x/2)/((tan(x/2)**2 + 1)*(tan(y/2)**2 + 1))
    """
    return expand_trig(expr).rewrite(tan)
```


```python
miscsimp1(sin(x + y))
```


```python
def miscsimp2(expr):
    """
    >>> miscsimp2(gamma(x + 4))
    x**4*gamma(x) + 6*x**3*gamma(x) + 11*x**2*gamma(x) + 6*x*gamma(x)
    """
    return expand(expand_func(expr))
```


```python
miscsimp2(gamma(x + 4))
```

## Continued Fractions

If we do not cover this, see http://asmeurer.github.io/scipy-2014-tutorial/html/tutorial/simplification.html#example-continued-fractions.


```python
def list_to_frac(l):
    expr = Integer(0)
    for i in reversed(l[1:]):
        expr += i
        expr = 1/expr
    return l[0] + expr
```


```python
a0, a1, a2, a3, a4 = symbols('a0:5')
```

Determine the list used to create the continued fraction $$\frac{a_{0} a_{1} a_{2} a_{3} a_{4} + a_{0} a_{1} a_{2} + a_{0} a_{3} a_{4} + a_{0} + a_{1} a_{2} a_{3} + a_{1} a_{3} a_{4} + a_{1} + a_{3}}{a_{0} a_{1} a_{2} a_{4} + a_{0} a_{4} + a_{1} a_{2} + a_{1} a_{4} + 1}.$$


```python
def continued_frac():
    """
    Determine the original list used to create the fraction.  

    Return the original list from this function.

    >>> orig_frac = (a0*a1*a2*a3*a4 + a0*a1*a2 + a0*a3*a4 + a0 + a1*a2*a3 + a1*a3*a4 + a1 + a3)/(a0*a1*a2*a4 + a0*a4 + a1*a2 + a1*a4 + 1)
    >>> pprint(orig_frac)
    a₀⋅a₁⋅a₂⋅a₃⋅a₄ + a₀⋅a₁⋅a₂ + a₀⋅a₃⋅a₄ + a₀ + a₁⋅a₂⋅a₃ + a₁⋅a₃⋅a₄ + a₁ + a₃
    ─────────────────────────────────────────────────────────────────────────
                     a₀⋅a₁⋅a₂⋅a₄ + a₀⋅a₄ + a₁⋅a₂ + a₁⋅a₄ + 1
    >>> cancel(list_to_frac(continued_frac())) == orig_frac
    True
    """
    return [a3, a4, a0, a2, a1]
```


```python
orig_frac = (a0*a1*a2*a3*a4 + a0*a1*a2 + a0*a3*a4 + a0 + a1*a2*a3 + a1*a3*a4 + a1 + a3)/(a0*a1*a2*a4 + a0*a4 + a1*a2 + a1*a4 + 1)
```


```python
orig_frac
```


```python
cancel(list_to_frac(continued_frac())) == orig_frac
```


```python

```

6bb0ae89df476e624eac56f2597346ff8b0aa530
ipynb
Jupyter Notebook
tutorial_exercises/Advanced-Simplification Solutions.ipynb
gvvynplaine/scipy-2016-tutorial
aa417427a1de2dcab2a9640b631b809d525d7929
BSD-3-Clause
2016-06-21T21:11:02.000Z
2021-02-04T07:51:03.000Z
tutorial_exercises/Advanced-Simplification Solutions.ipynb
gvvynplaine/scipy-2016-tutorial
aa417427a1de2dcab2a9640b631b809d525d7929
BSD-3-Clause
2016-07-02T20:24:06.000Z
2016-07-11T11:31:44.000Z
tutorial_exercises/Advanced-Simplification Solutions.ipynb
gvvynplaine/scipy-2016-tutorial
aa417427a1de2dcab2a9640b631b809d525d7929
BSD-3-Clause
2016-06-25T09:04:24.000Z
2021-08-09T06:46:01.000Z
Qwen/Qwen-72B
1. YES
2. YES