```python
from sympy import *
```


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

### Basic *Calculus*


```python
print('------')

Limit((cos(x)-1),x,0)
Limit((cos(x)-1),x,0).doit()

print('------')

Derivative(5*x**2,x)
Derivative(5*x**2,x).doit()

print('------')

Integral(log(x)**2,x)
Integral(log(x)**2,x).doit()
```


```python
limit(sin(x)/x,x,0)
limit(sin(x)/x,x,oo)
```


```python
# use limit instead of subs (if there's a singularity)
(x/x**x).subs(x,oo)
limit((x/x**x),x,oo)

# 'limit' one side only 
limit(1/x,x,0,'+')
limit(1/x,x,0,'-')
```


```python
diff(cos(x),x)

diff(5*x**3)
diff(5*x**3,x)
diff(5*x**3,x,1)

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


```python
diff(5*x**3,x,0)
diff(5*x**3*y**2,x,0,y,0)

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


```python
integrate(cos(x),x)

# use 'oo' to indicates 'infinity'
integrate(exp(-x),(x,0,oo))
integrate(exp(-x**2-y**2),(x,-oo,oo),(y,-oo,oo))

# oops!
integrate(x**x,x)
```


```python
exp(sin(x))
exp(sin(x)).series(x,0,5)

# hell yeah!
exp(x-5).series(x)
exp(x-5).series(x,x0=5)
```

### *Solver*


```python
# In Sympy, any expression not in an 'Eq' 
#   is automatically assumed to equal 0 by solving funcs.

Eq(x,y)

# equals to 0? u can omit it!
solveset(Eq(x**2-5,0),x)
solveset(x**2-5,x)

# use Eq or not is FINE 
solveset(Eq(x**2,x),x)
solveset(x**2-x,x)
```


```python
solveset(Eq(x**2,x),x,domain=S.Reals)
solveset(sin(x)-1,x,domain=S.Reals)
```


```python
# no solution exists
solveset(exp(x),x)

# not able to find solution 
#   ( C代表虚数, 反V代表"与" )
solveset(cos(x)-x,x)
```


```python
linsolve([x+y+z-1,x+y+2*z-3],(x,y,z))
linsolve(Matrix(([1,1,1,1],[1,1,2,3])), (x,y,z))
```


```python
# nonlinear shit
a,b,c,d = symbols('a b c d',real=True)

nonlinsolve([a**2+a,a-b],[a,b])
nonlinsolve([x*y-1,x-2],x,y)

nonlinsolve([x**2+1,y**2+1],[x,y])
nonlinsolve([x**2-2*y**2-2,x*y-2],[x,y])
```


```python
# differential equations
f,g = symbols('f g',cls=Function)

f(x)
f(g(x))
```


```python
eq = Eq(f(x).diff(x,2) - 2*f(x).diff(x) + f(x),sin(x))

eq
dsolve(eq,f(x))

dsolve(Eq(f(x).diff(x)))
dsolve(f(x).diff(x)*(1-sin(f(x))),f(x))
```

d7ae9098dbbb71f16462b87abe40888f4cb784c5
ipynb
Jupyter Notebook
sympy-part03-calc-solver.ipynb
codingEzio/code_python_learn_math
bd7869d05e1b4ec250cc5fa13470a960b299654e
Unlicense
sympy-part03-calc-solver.ipynb
codingEzio/code_python_learn_math
bd7869d05e1b4ec250cc5fa13470a960b299654e
Unlicense
sympy-part03-calc-solver.ipynb
codingEzio/code_python_learn_math
bd7869d05e1b4ec250cc5fa13470a960b299654e
Unlicense
Qwen/Qwen-72B
1. YES
2. YES