```python
import sympy as sm
```


```python
sm.init_printing()
```

# Symbols and Functions


```python
a, b, th, gamma, x, t, y, z = sm.symbols('a, b, theta, gamma, x, t, y, z')
```


```python
%whos
```


```python
a, b, th, gamma, x, t, y, z
```


```python
f = sm.Function('f')
```


```python
f(t)
```


```python
f(x, y, z)
```


```python
a1, a2, a3 = sm.symbols('a1, a2, a3')
```


```python
a1, a2, a3
```

# Expressions


```python
expr1 = a + b - x
expr1
```


```python
expr2 = f(t) + 2*f(x, y, z) + a/b
expr2
```


```python
expr3 = sm.sin(f(t)) - sm.tan(a/b)/sm.log(gamma)
expr3
```

# Printing


```python
print(expr3)
```


```python
repr(expr3)
```


```python
sm.srepr(expr1)
```


```python
sm.pprint(expr3)
```


```python
print(sm.latex(expr3))
```


```python
sm.ccode(expr1)
```


```python
print(sm.octave_code(expr3))
```

# Derivatives


```python
expr3
```


```python
sm.diff(expr3, a)
```


```python
part1 = sm.diff(expr3, a)
```


```python
part2 = sm.diff(part1, b)
```


```python
part2
```


```python
expr3.diff(a)
```


```python
expr3.diff(t)
```


```python
expr3.diff(t, 2)
```


```python
expr3.diff(t).diff(t)
```

# Numerical Evaluation


```python
expr1
```


```python
repl = {a: 5, b: -38, x: 102}
repl
```


```python
expr1.subs(repl)
```


```python
expr1.xreplace(repl)
```


```python
type(expr1.subs(repl))
```


```python
type(-135)
```


```python
type(int(expr1.subs(repl)))
```


```python
expr4 = sm.pi/4 + sm.sin(x*y)
expr4
```


```python
expr4.xreplace({x: 12, y: 24})
```


```python
expr4.evalf()
```


```python
expr4.evalf(subs={x: 12, y:24})
```


```python
type(expr4.evalf(subs={x: 12, y:24}))
```


```python
type(float(expr4.evalf(subs={x: 12, y:24})))
```


```python
expr4.evalf(subs={x: 12, y:24}, n=1000)
```


```python
expr1
```


```python
eval_expr1 = sm.lambdify((a, b, x), expr1)
```


```python
eval_expr1(12.0, 34.3, -2.0)
```


```python
type(eval_expr1(12.0, 34.3, -2.0))
```

# Matrices & Linear Algebra


```python
mat1 = sm.Matrix([[1, 2], [3, 4]])
mat1
```


```python
mat1.shape
```


```python
mat1.det()
```


```python
mat2 = sm.Matrix([[expr1, expr2], [expr3, expr4]])
mat2
```


```python
mat2.diff(t)
```


```python
mat1 + mat2
```


```python
mat1 * mat2
```


```python
sm.hadamard_product(mat1, mat2)
```


```python
mat1**2
```


```python
mat1 * mat1
```


```python
sm.eye(5)
```


```python
sm.zeros(2,4)
```

# Linear systems


```python
lin_expr_1 = a*x + b**2*y + sm.sin(gamma)*z
lin_expr_1
```


```python
lin_expr_2 = sm.sin(f(t))*x + sm.log(f(t))*z
lin_expr_2
```


```python
sm.Eq(lin_expr_1, 0)
```


```python
sm.Eq(lin_expr_2, 0)
```


```python
res = sm.solve([lin_expr_1, lin_expr_2], x, z, dict=True)
res
```


```python
res_dict = res[0]
res_dict
```


```python
sm.Eq(x, res_dict[x])
```


```python
lin_mat_exprs = sm.Matrix([lin_expr_1, lin_expr_2])
lin_mat_exprs
```


```python
A = lin_mat_exprs.jacobian([x, z])
A
```


```python
b = -lin_mat_exprs.xreplace({x: 0, z: 0})
b
```


```python
A.LUsolve(b)
```

# Simplification


```python
sm.simplify(A.LUsolve(b))
```


```python
sm.cos(gamma)**2 + sm.sin(gamma)**2
```


```python
sm.trigsimp(sm.cos(gamma)**2 + sm.sin(gamma)**2)
```


```python
sub_exprs, simp_expr = sm.cse(A.LUsolve(b).diff(t))
```


```python
simp_expr
```


```python
sub_exprs
```

8bdb6837eca6569b4b95758f877201d7d731cfa2
ipynb
Jupyter Notebook
content/notebooks/sympy.ipynb
moorepants/me41055
5c941d28f6f61fa1a02fbda4772d88010014eec0
CC-BY-4.0
content/notebooks/sympy.ipynb
moorepants/me41055
5c941d28f6f61fa1a02fbda4772d88010014eec0
CC-BY-4.0
2022-01-07T18:05:36.000Z
2022-03-22T15:15:01.000Z
content/notebooks/sympy.ipynb
moorepants/me41055
5c941d28f6f61fa1a02fbda4772d88010014eec0
CC-BY-4.0
2022-01-24T17:18:40.000Z
2022-01-24T17:18:40.000Z
Qwen/Qwen-72B
1. YES
2. YES