```python
import sympy
from sympy import I, pi, oo
import numpy as np
sympy.init_printing(backcolor="Transparent")
```


```python
x = sympy.Symbol("x")
x.is_real is None
```




    True




```python
y = sympy.Symbol("y", real=True)
y.is_real
```




    True




```python
z = sympy.Symbol("z", imaginary=True)
z.is_real
```




    False




```python
x = sympy.Symbol("x")
y = sympy.Symbol("y", positive=True)
```


```python
sympy.sqrt(x ** 2)
```


```python
sympy.sqrt(y ** 2)
```


```python
n1 = sympy.Symbol("n")
n2 = sympy.Symbol("n", integer=True)
n3 = sympy.Symbol("n", odd=True)
```


```python
sympy.cos(n1 * pi)
```


```python
sympy.cos(n2 * pi)
```


```python
sympy.cos(n3 * pi)
```


```python
a, b, c = sympy.symbols("a, b, c", negative=True)
d, e, f = sympy.symbols("d, e, f", positive=True)
```

### Numbers


```python
i = sympy.Integer(19)
type(i)
```




    sympy.core.numbers.Integer




```python
i.is_Integer, i.is_real, i.is_odd
```




    (True, True, True)




```python
f = sympy.Float(2.3)
type(f)
```




    sympy.core.numbers.Float




```python
f.is_Integer, f.is_real, f.is_odd
```




    (False, True, False)




```python
i, f = sympy.sympify(19), sympy.sympify(2.3)
type(i), type(f)
```




    (sympy.core.numbers.Integer, sympy.core.numbers.Float)




```python
n = sympy.Symbol("n", integer=True)
```


```python
n.is_integer, n.is_Integer, n.is_positive, n.is_Symbol
```




    (True, False, None, True)




```python
i = sympy.Integer(19)
```


```python
i.is_integer, i.is_Integer, i.is_positive, i.is_Symbol
```




    (True, True, True, False)




```python
sympy.factorial(100)
```


```python
sympy.Float(0.3, 25)
```


```python
sympy.Rational(11, 13)
```


```python
r1 = sympy.Rational(2, 3)
r2 = sympy.Rational(4, 5)
```


```python
r1 * r2
```


```python
r1 / r2
```

### Functions


```python
x, y, z = sympy.symbols("x, y, z")
f = sympy.Function("f")
type(f)
```




    sympy.core.function.UndefinedFunction




```python
f(x)
```


```python
g = sympy.Function("g")(x, y, z)
g
```


```python
g.free_symbols
```


```python
sympy.sin
```




    sin




```python
sympy.sin(x)
```


```python
sympy.sin(pi * 1.5)
```


```python
n = sympy.Symbol("n", integer=True)
sympy.sin(pi * n)
```


```python
h = sympy.Lambda(x, x**2)
h
```


```python
h(5)
```


```python
h(1 + x)
```


```python
x = sympy.Symbol("x")
expr = 1 + 2 * x**2 + 3 * x**3
expr
```

### Simplify


```python
expr = 2 * (x**2 - x) - x * (x + 1)
expr
```


```python
sympy.simplify(expr)
```


```python
expr.simplify()
```


```python
expr
```


```python
expr = 2 * sympy.cos(x) * sympy.sin(x)
expr
```


```python
sympy.simplify(expr)
```


```python
expr = sympy.exp(x) * sympy.exp(y)
expr
```


```python
sympy.simplify(expr)
```

### Expand


```python
expr = (x + 1) * (x + 2)
sympy.expand(expr)
```


```python
sympy.sin(x + y).expand(trig=True)
```


```python
a, b = sympy.symbols("a, b", positive=True)
sympy.log(a * b).expand(log=True)
```


```python
sympy.exp(I*a + b).expand(complex=True)
```


```python
sympy.expand((a * b)**x, power_base=True)
```


```python
sympy.exp((a-b)*x).expand(power_exp=True)
```

### Factor, Collect and Combine


```python
sympy.factor(x**2 - 1)
```


```python
sympy.factor(x * sympy.cos(y) + sympy.sin(z) * x)
```


```python
sympy.logcombine(sympy.log(a) - sympy.log(b))
```


```python
expr = x + y + x * y * z
expr.collect(x)
```


```python
expr.collect(y)
```


```python
expr = sympy.cos(x + y) + sympy.sin(x - y)
expr.expand(trig=True).collect([
    sympy.cos(x),sympy.sin(x)
]).collect(sympy.cos(y) - sympy.sin(y))
```


```python
sympy.apart(1/(x**2 + 3*x + 2), x)
```


```python
sympy.together(1 / (y * x + y) + 1 / (1+x))
```


```python
sympy.cancel(y / (y * x + y))
```


```python
(x + y).subs(x, y)
```


```python
sympy.sin(x * sympy.exp(x)).subs(x, y)
```


```python
sympy.sin(x * z).subs({z: sympy.exp(y), x: y, sympy.sin: sympy.cos})
```


```python
sympy.sin(x * z).subs({z: sympy.exp(y), x: y, sympy.sin: sympy.cos})
```


```python
expr = x * y + z**2 *x
values = {x: 1.25, y: 0.4, z: 3.2}
expr.subs(values)
```

### Numerical evaluation


```python
sympy.N(1 + pi)
```


```python
sympy.N(pi, 50)
```


```python
(x + 1/pi).evalf(10)
```


```python
expr = sympy.sin(pi * x * sympy.exp(x))
[expr.subs(x, xx).evalf(3) for xx in range(0, 10)]
```


```python
expr_func = sympy.lambdify(x, expr)
expr_func(1.0)
```


```python
expr_func = sympy.lambdify(x, expr, 'numpy')
xvalues = np.arange(0, 10)
expr_func(xvalues)
```




    array([ 0.        ,  0.77394269,  0.64198244,  0.72163867,  0.94361635,
            0.20523391,  0.97398794,  0.97734066, -0.87034418, -0.69512687])



### Calculus


```python
f = sympy.Function('f')(x)
sympy.diff(f, x)
```


```python
sympy.diff(f, x, x)
```


```python
sympy.diff(f, x, 3)
```


```python
g = sympy.Function('g')(x, y)
g.diff(x, y)
```


```python
g.diff(x, 3, y, 2)
```


```python
expr = x**4 + x**3 + x**2 + x + 1
expr.diff(x)
```


```python
expr.diff(x, x)
```


```python
expr = (x + 1)**3 * y ** 2 * (z - 1)
expr.diff(x, y, z)
```


```python
expr = sympy.sin(x * y) * sympy.cos(x / 2)
expr.diff(x)
```


```python
expr = sympy.functions.special.polynomials.hermite(x, 0)
expr.diff(x).doit()
```


```python
d = sympy.Derivative(sympy.exp(sympy.cos(x)), x)
d
```


```python
d.doit()
```


```python
a, b, x, y = sympy.symbols("a, b, x, y")
f = sympy.Function("f")(x)
```


```python
sympy.integrate(f)
```


```python
sympy.integrate(f, (x, a, b))
```


```python
sympy.integrate(sympy.sin(x))
```


```python
sympy.integrate(sympy.sin(x), (x, a, b))
```


```python
sympy.integrate(sympy.exp(-x**2), (x, 0, oo))
```


```python
a, b, c = sympy.symbols("a, b, c", positive=True)
```


```python
sympy.integrate(a * sympy.exp(-((x-b)/c)**2), (x, -oo, oo))
```


```python
sympy.integrate(sympy.sin(x * sympy.cos(x)))
```


```python
expr = sympy.sin(x*sympy.exp(y))
sympy.integrate(expr, x)
```


```python
expr = (x + y)**2
sympy.integrate(expr, x)
```


```python
sympy.integrate(expr, (x, 0, 1), (y, 0, 1))
```


```python
x, y = sympy.symbols("x, y")
f = sympy.Function("f")(x)
```


```python
sympy.series(f, x)
```




$\displaystyle f{\left(0 \right)} + x \left. \frac{d}{d \xi} f{\left(\xi \right)} \right|_{\substack{ \xi=0 }} + \frac{x^{2} \left. \frac{d^{2}}{d \xi^{2}} f{\left(\xi \right)} \right|_{\substack{ \xi=0 }}}{2} + \frac{x^{3} \left. \frac{d^{3}}{d \xi^{3}} f{\left(\xi \right)} \right|_{\substack{ \xi=0 }}}{6} + \frac{x^{4} \left. \frac{d^{4}}{d \xi^{4}} f{\left(\xi \right)} \right|_{\substack{ \xi=0 }}}{24} + \frac{x^{5} \left. \frac{d^{5}}{d \xi^{5}} f{\left(\xi \right)} \right|_{\substack{ \xi=0 }}}{120} + O\left(x^{6}\right)$




```python
x0 = sympy.Symbol("{x_0}")
f.series(x, x0, n=2)
```




$\displaystyle f{\left({x_0} \right)} + \left(x - {x_0}\right) \left. \frac{d}{d \xi_{1}} f{\left(\xi_{1} \right)} \right|_{\substack{ \xi_{1}={x_0} }} + O\left(\left(x - {x_0}\right)^{2}; x\rightarrow {x_0}\right)$




```python
f.series(x, x0, n=2).removeO()
```




$\displaystyle \left(x - {x_0}\right) \left. \frac{d}{d \xi_{1}} f{\left(\xi_{1} \right)} \right|_{\substack{ \xi_{1}={x_0} }} + f{\left({x_0} \right)}$




```python
sympy.cos(x).series()
```


```python
sympy.sin(x).series()
```


```python
sympy.exp(x).series()
```


```python
(1/(1+x)).series()
```


```python
expr = sympy.cos(x) / (1 + sympy.sin(x * y))
expr.series(x, n=4)
```


```python
expr.series(y, n=4)
```


```python
sympy.limit(sympy.sin(x) / x, x, 0)
```


```python
f = sympy.Function('f')
x, h = sympy.symbols("x, h")
diff_limit = (f(x + h) - f(x))/h
sympy.limit(diff_limit.subs(f, sympy.cos), h, 0)
```


```python
sympy.limit(diff_limit.subs(f, sympy.sin), h, 0)
```


```python
expr = (x**2 - 3*x) / (2*x - 2)
p = sympy.limit(expr/x, x, sympy.oo)
q = sympy.limit(expr - p*x, x, sympy.oo)
p, q
```


```python
n = sympy.symbols("n", integer=True)
x = sympy.Sum(1/(n**2), (n, 1, oo))
x
```


```python
x.doit()
```


```python
x = sympy.Product(n, (n, 1, 7))
x
```


```python
x.doit()
```


```python
x = sympy.Symbol("x")
sympy.Sum((x)**n/(sympy.factorial(n)), (n, 1, oo)).doit().simplify()
```


```python
sympy.solve(x**2 + 2*x - 3)
```


```python
a, b, c = sympy.symbols("a, b, c")
sympy.solve(a * x**2 + b * x + c, x)
```


```python
sympy.solve(sympy.sin(x) - sympy.cos(x), x)
```


```python
sympy.solve(sympy.exp(x) + 2 * x, x)
```


```python
sympy.solve(x**5 - x**2 + 1, x)
```


```python
eq1 = x + 2 * y - 1
eq2 = x - y + 1
sympy.solve([eq1, eq2], [x, y], dict=True)

```


```python
eq1 = x**2 - y
eq2 = y**2 - x
sols = sympy.solve([eq1, eq2], [x, y], dict=True)
sols
```


```python
[eq1.subs(sol).simplify() == 0 and eq2.subs(sol).simplify() == 0 for sol in sols]
```




    [True, True, True, True]



### Matrix


```python
sympy.Matrix([1, 2])
```




$\displaystyle \left[\begin{matrix}1\\2\end{matrix}\right]$




```python
sympy.Matrix([[1, 2]])
```




$\displaystyle \left[\begin{matrix}1 & 2\end{matrix}\right]$




```python
sympy.Matrix([[1, 2], [3, 4]])
```




$\displaystyle \left[\begin{matrix}1 & 2\\3 & 4\end{matrix}\right]$




```python
sympy.Matrix(3, 4, lambda m, n: 10 * m + n)
```




$\displaystyle \left[\begin{matrix}0 & 1 & 2 & 3\\10 & 11 & 12 & 13\\20 & 21 & 22 & 23\end{matrix}\right]$




```python
a, b, c, d = sympy.symbols("a, b, c, d")
M = sympy.Matrix([[a, b], [c, d]])
M
```




$\displaystyle \left[\begin{matrix}a & b\\c & d\end{matrix}\right]$




```python
M * M
```




$\displaystyle \left[\begin{matrix}a^{2} + b c & a b + b d\\a c + c d & b c + d^{2}\end{matrix}\right]$




```python
x = sympy.Matrix(sympy.symbols("x_1, x_2"))
M * x
```




$\displaystyle \left[\begin{matrix}a x_{1} + b x_{2}\\c x_{1} + d x_{2}\end{matrix}\right]$




```python
p, q = sympy.symbols("p, q")
M = sympy.Matrix([[1, p], [q, 1]])
M
```




$\displaystyle \left[\begin{matrix}1 & p\\q & 1\end{matrix}\right]$




```python
b = sympy.Matrix(sympy.symbols("b_1, b_2"))
b
```




$\displaystyle \left[\begin{matrix}b_{1}\\b_{2}\end{matrix}\right]$




```python
x = M.LUsolve(b)
x
```




$\displaystyle \left[\begin{matrix}b_{1} - \frac{p \left(- b_{1} q + b_{2}\right)}{- p q + 1}\\\frac{- b_{1} q + b_{2}}{- p q + 1}\end{matrix}\right]$




```python
x = M.inv() * b
x
```




$\displaystyle \left[\begin{matrix}\frac{b_{1}}{- p q + 1} - \frac{b_{2} p}{- p q + 1}\\- \frac{b_{1} q}{- p q + 1} + \frac{b_{2}}{- p q + 1}\end{matrix}\right]$




```python

```


```python

```


```python

```


```python

```


```python

```


```python

```


```python

```

4cd66c7fb4cade22421f9a265cd6c01522cc91e1
ipynb
Jupyter Notebook
NumericalPython/2.SymPy.ipynb
nickovchinnikov/Computational-Science-and-Engineering
45620e432c97fce68a24e2ade9210d30b341d2e4
MIT
2021-01-14T08:00:23.000Z
2022-01-31T14:00:11.000Z
NumericalPython/2.SymPy.ipynb
nickovchinnikov/Computational-Science-and-Engineering
45620e432c97fce68a24e2ade9210d30b341d2e4
MIT
NumericalPython/2.SymPy.ipynb
nickovchinnikov/Computational-Science-and-Engineering
45620e432c97fce68a24e2ade9210d30b341d2e4
MIT
2022-01-25T15:21:40.000Z
2022-01-25T15:21:40.000Z
Qwen/Qwen-72B
1. YES
2. YES