### Simplification - *Polynomial*


```python
from sympy import *

x,y,z = symbols('x y z')
init_printing(use_unicode=False)
```


```python
simplify(sin(x)**2+cos(x)**2)

simplify(x**3-y**3)
simplify((x-y)*(x**2+x*y+y**2))

# Note: this func doesn't fit every expr, also maybe too slow.
simplify(x**2+2*x+1)
```


```python
factor(x**2*y+x*y)

factor(x**3-y**3)
factor_list(x**3-y**3)
```


```python
expand((x+2)**2)
expand((x-5)*(x-2))

factor(x**2+4*x+4)
factor(x**2-7*x+10)
```


```python
factor(sin(x)**2+2*sin(x)*cos(x)+cos(x)**2) 
expand((cos(x)+sin(x))**2)
```


```python
expr = x*y**2*3+x-3+2*x**2-x**2*y+x**3

expr
collect(expr,x)

# the coefficient(系数) of x**n in factor 
collect(expr,x).coeff(3)
collect(expr,x).coeff(x,2)
collect(expr,x).coeff(x,3)
```


```python
expr = (x**2+2*x+5)/(x**3+x)+x

expr

# use the former one!
cancel(expr)
factor(expr)
```


```python
expr = (4*x**3+22*x**4+13)/(12*x+6*x**2)

expr
apart(expr)
```

### Simplification - *Trigonometic*


```python
cos(pi/4)

acos(x)
cos(acos(x))
```


```python
# simplify is okay as well :)

trigsimp(
    sin(x)**2
    +cos(x)**2
)

trigsimp(
    sin(x)**4
    -2*cos(x)**2*sin(x)**2
    +cos(x)**4
)
```


```python
trigsimp(expand_trig(sin(x+y)))

print()
expand_trig(sin(x+y))
expand_trig(cos(x+y))

print()
expand_trig(sin(2*x))
expand_trig(cos(2*x))
```

### Simplification - *Powers*


```python
# y got know:
#   x**a * y**a !always= (x*y)**a
#   (x**a)**b   !always= x**(a*b)

x,y = symbols('x y',positive=True)
a,b = symbols('a b',real=True)
z,t,c = symbols('z t c')
```


```python
sqrt(x) == x**Rational(1/2)
```




    True




```python
# x**(a+b)
powsimp(x**a*x**b)
expand_power_exp(x**(a+b))

# (x*y)**a
powsimp(x**a*y**a)
expand_power_base((x*y)**a)
```


```python
# "t,z,c isn't read/postive" 
#   means that it (may) not valid for simplifying
powsimp(t**c*z**c)
powsimp(t**c*z**c,force=True)

expand_power_base((z*t)**c)
expand_power_base((z*t)**c,force=True)

# or like this (there's a '2' inside)
(z*t)**2
```


```python
#     valid -> expand (like "2**(5+3)" => "2**8")
# not valid -> Nope!
powdenest((x**a)**b)
powdenest((z**a)**b)
```

### Simplification - *Logarithms*


```python
ln(x) == log(x)
```




    True




```python
x,y = symbols('x y',positive=True)
n = symbols('n',real=True)
```


```python
# some rules:
#   valid (pos/real)  -> do the thing you wanna do 
#   not valid (uncer) -> not applied until u use 'force=True'
```


```python
expand_log(log(x*y))
expand_log(log(x/y))

expand_log(log(x**2))
expand_log(log(x**n))

# Hmm..
expand_log(log(x**z))             # not z*log(x)
expand_log(log(x**z),force=True)  # okay :>
```


```python
logcombine(log(x)+log(y))
logcombine(log(x)-log(y))

logcombine(n*log(x))

logcombine(n*log(z))
logcombine(n*log(z),force=True)
```

### Special Functions 


```python
x,y,z = symbols('x y z')
k,m,n = symbols('k m n')
```


```python
factorial(n)

binomial(n,k) 
hyper([1,2],[3],z)
```


```python
sin(x).rewrite(cos)
cos(x).rewrite(cos)

tan(x).rewrite(sin)
sin(2*x).rewrite(cos)
```


```python
expr = factorial(n)/factorial(n-3)

expr
combsimp(expr)
```

c3ed13f278dc5c8a9474729a853468e37b667fa4
ipynb
Jupyter Notebook
sympy-part02-simplify.ipynb
codingEzio/code_python_learn_math
bd7869d05e1b4ec250cc5fa13470a960b299654e
Unlicense
sympy-part02-simplify.ipynb
codingEzio/code_python_learn_math
bd7869d05e1b4ec250cc5fa13470a960b299654e
Unlicense
sympy-part02-simplify.ipynb
codingEzio/code_python_learn_math
bd7869d05e1b4ec250cc5fa13470a960b299654e
Unlicense
Qwen/Qwen-72B
1. YES
2. YES