```python
!pip install sympy tornado
```

# Symbolic Computation with Python

## Declaring Symbols, Printing Functions


```python
import sympy as sp
sp.init_printing()
```


```python
x=sp.Symbol('x')
y,z=sp.symbols('y,z')
f=sp.Function('f')
g=x**2 + y**2 + z**2
```


```python
print(g)
```


```python
g
```


```python
sp.pprint(g)
```

## Math Expressions and Evaluating at certain values


```python
h=x**2+2*x-5
h
```


```python
print(h.subs(x,2))

```


```python
print(h.subs(x,y**2.5))
print(h.subs(x,z**2))
```

### Simplifying, Expanding and Factorizing Expressions


```python
f=(x**2-x-6)/(x**2-3*x)
sp.simplify(f)
```


```python
f=(x+1)**3*(x+2)**2
sp.expand(f)
```


```python
f=3*x**4-36*x**3+99*x**2-6*x-144
sp.factor(f)
```

## Differentiating and Intergrating Functions


```python
y=(sp.sin(x))**2 *sp.exp(2*x)
y
```


```python
z=sp.diff(y,x)
z
```


```python
z.subs(x,3.2)
```


```python
f=x**2*sp.sin(x**2)
f

```


```python
g=sp.integrate(f,(x,0,5))
g
```


```python
g.evalf()
```

## Solving Equations and Groups of Equations


```python
y1=sp.Eq(x**3+15*x**2,3*x-10)
y1

```


```python
z=sp.solve(y1,x)
z
```


```python
z[0].evalf()
```


```python
z[1].evalf()
```


```python
z[2].evalf()
```


```python
for w in z:
    sp.pprint(w.evalf())
```


```python
x,y,z=sp.symbols('x,y,z')
eq1=sp.Eq(x+y+z,0)
eq1
```


```python
eq2=sp.Eq(2*x-y-z,10)
eq2

```


```python
eq3=sp.Eq(y+2*z,5)
eq3
```


```python
sp.solve([eq1,eq2,eq3],[x,y,z])
```

### Verifying answer using scipy methods of solving equations


```python
from scipy.optimize import fsolve
def f(w):
    x=w[0]
    y=w[1]
    z=w[2]
    f1=x+y+z
    f2=2*x-y-z-10
    f3=y+2*z-5
    return [f1,f2,f3]

result=fsolve(f,[0,0,0])
sp.pprint(result)
```

## Solving differential equations symbolically using the ```dsolve``` function:
$$\frac{df(x)}{dx} = x\cos(x).$$


```python
pf=sp.Function('pf')
y=sp.dsolve(sp.Derivative(pf(x),x)-x*sp.cos(x),pf(x))
y
```


```python
z=sp.integrate(x*sp.cos(x))
z
```

## Matrices, Vectors and Solving Equations of the form `Ax=b` :


```python
from sympy import Matrix
A=sp.Matrix([[1,2,5],[3,4,6],[-1,0,3]])
b=sp.Matrix([1,0,-2])
sp.pprint(A)
sp.pprint(b)
```


```python
sp.pprint(A.inv()*b)
sp.pprint(A.LUsolve(b))
```


```python
sp.pprint(A[1:2,:])
```


```python
sp.pprint(A[:,1:2])
```


```python
M=sp.zeros(2,2)
M[1,1]=3
M[1,0]=x**2
sp.pprint(M)
```


```python
M=sp.ones(2,2)
M[1,1]=0
sp.pprint(M.inv())
```


```python
!pip install keras
```


```python
!pip install rise
```


```python
!jupyter-nbextension install rise --py --sys-prefix
```


```python
!jupyter-nbextension enable rise --py --sys-prefix
```


```python
!pip install tensorflow
```


```python
!pip install plotly
```


```python

```

922f10a77b794a31c319b95419c043e82b240a9d
ipynb
Jupyter Notebook
SymbolicComputationAlgebraWithPythonSymPy.ipynb
TralahM/myjupyternotebooks
53f4ea8f64da90e1dcb5e67d0ac9db4c038d0e24
MIT
SymbolicComputationAlgebraWithPythonSymPy.ipynb
TralahM/myjupyternotebooks
53f4ea8f64da90e1dcb5e67d0ac9db4c038d0e24
MIT
SymbolicComputationAlgebraWithPythonSymPy.ipynb
TralahM/myjupyternotebooks
53f4ea8f64da90e1dcb5e67d0ac9db4c038d0e24
MIT
Qwen/Qwen-72B
1. YES
2. YES