# 15 - Symbolic Computations


```python
from sympy import *
init_printing()
```

## What are symbolic calculations?


```python
x = symbols('x')
```


```python
f = Lambda(x, 1/(x**2 + x + 1))
```


```python
integrate(f(x),x)
```


```python
pf = Lambda(x, integrate(f(x),x))
```


```python
diff(pf(x),x)
```


```python
simplify(diff(pf(x),x))
```


```python
simplify(pf(4)-pf(0))
```


```python
(pf(4)-pf(0)).evalf()
```


```python
from scipy.integrate import quad
```


```python
quad(lambda x : 1/(x**2+x+1),a=0, b=4)
```

## Basic elements of SymPy


```python
x,y,mass,torque, row = symbols('x y mass torque i')
```


```python
symbol_list=[symbols(l) for l in 'x y mass torque'.split()]
x, y, mass, torque = symbol_list
```


```python
row_index=symbols('i',integer=True)
row_index**2 # returns i**2
```


```python
x, y, mass, torque=symbols('x y mass torque')
symbol_list=[symbols(l) for l in 'x y mass torque'.split()]
x, y, mass, torque = symbol_list 
```


```python
integervariables = symbols('i:l', integer=True)
dimensions = symbols('m:n', integer=True)
realvariables = symbols('x:z', real=True)
```


```python
integervariables
```


```python
A = symbols('A0:3(0:4)')
A
```

### Numbers


```python
1/3  # returns 0.3333333333333333
sympify(1)/sympify(3)  # returns '1/3'
```


```python
Rational(1,3)
```

### Functions


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


```python
f = Function('f')
g = Function('g')
```


```python
x = symbols('x')
f, g = symbols('f g', cls=Function)
diff(f(x*g(x)),x)
```


```python
x = symbols('x:3')
f(*x)
```


```python
[diff(f(*x),xx) for xx in x]
```


```python
[f(*x).diff(xx) for xx in x]
```


```python
x = symbols('x')
f(x).series(x,0,n=4)
```

### Elementary Functions


```python
x = symbols('x')
simplify(cos(x)**2 + sin(x)**2)  # returns 1
```


```python
atan(x).diff(x) - 1./(x**2+1)  # returns 0
```


```python
import scipy as sp
import sympy as sym
# working with numbers
x=3
y=sp.sin(x)
# working with symbols
x=sym.symbols('x')
y=sym.sin(x)  
```

###  Lambda functions


```python
C,rho,A,v=symbols('C rho A v')
# C drag coefficient, A coss-sectional area, rho density
# v speed
f_drag = Lambda(v,-Rational(1,2)*C*rho*A*v**2)
f_drag
```


```python
x = symbols('x')
f_drag(2)
f_drag(x/3)
```


```python
y = symbols('y')
t=Lambda((x,y),sin(x) + cos(2*y))
```


```python
t(pi,pi/2)  # returns -1
```


```python
p=(pi,pi/2)
t(*p)   # returns -1
```


```python
F=Lambda((x,y),Matrix([sin(x) + cos(2*y), sin(x)*cos(y)]))
```


```python
F(x,y).jacobian((x,y))
```


```python
x=symbols('x:2')
F=Lambda(x,Matrix([sin(x[0]) + cos(2*x[1]),sin(x[0])*cos(x[1])]))  
F(*x).jacobian(x)
```

# Symbolic Linear Algebra


```python
phi=symbols('phi')
rotation=Matrix([[cos(phi), -sin(phi)],
                 [sin(phi), cos(phi)]])
```


```python
phi=symbols('phi')
rotation=Matrix([[cos(phi), -sin(phi)],
                 [sin(phi), cos(phi)]])

```


```python
simplify(rotation.T - rotation.inv())
```


```python
M = Matrix(3,3, symbols('M:3(:3)'))
M
```


```python
def toeplitz(n):
    a = symbols('a:'+str(2*n))
    f = lambda i,j: a[i-j+n-1]
    return Matrix(n,n,f)
```


```python
toeplitz(5)
```


```python
a=symbols('a')
M[0,2]=0  # changes one element
M[1,:]=Matrix(1,3,[1,2,3]) # changes an entire row
M
```


```python
---------------------so far-------------------------------------------
```


```python
t=Lambda((x,y),sin(x) + cos(2*y))
t
```


```python
t(pi,pi/2)
```


```python
p=(pi,pi/2)
t(*p)
```


```python
F=Lambda((x,y),Matrix([sin(x) + cos(2*y), sin(x)*cos(y)]))
F
```


```python
F(x,y).jacobian((x,y))
```


```python
x=symbols('x:2')
F=Lambda(x,Matrix([sin(x[0]) + cos(2*x[1]),sin(x[0])*cos(x[1])])) 
F
```


```python
F(*x)
```


```python
F(*x).jacobian(x)
```


```python
phi=symbols('phi')
rotation=Matrix([[cos(phi), -sin(phi)],[sin(phi), cos(phi)]])
rotation
```


```python
simplify(rotation.transpose()*rotation)
```


```python
simplify(rotation.transpose()*rotation -eye(2))
```


```python
simplify(rotation.T - rotation.inv())
```


```python
M = Matrix(3,3, symbols('M:3(:3)'))
M
```


```python
M.subs({M[2,1]:0})
```


```python
M[0,2]=0  # changes one element
M[1,:]=Matrix(1,3,[1,2,3]) # changes an entire row
M
```


```python
A = Matrix(3,3,symbols('A1:4(1:4)'))
b = Matrix(3,1,symbols('b1:4'))
x = A.LUsolve(b)
x
```


```python
print(latex(simplify(x)))
```


```python
A*x-b
```


```python
simplify(A*x-b)
```


```python
A.det()
```


```python
simplify(x*A.det())
```


```python
x, a = symbols('x a')
b = x + a
```


```python
b
```


```python
x, a = symbols('x a')
b = x + a
c=b.subs(x,0)
d=c.subs(a,2*a)
```


```python
c, d
```


```python
b.subs(x,0)
```


```python
x, a = symbols('x a')
b = x + a
b.subs(x,0)
b.subs({x:0})
```


```python
d=b.subs({x:0,a:2*a})
```


```python
d
```


```python
b.subs({x:2*x, a:a*x})
```


```python
b.subs({a:a*x, x:2*x})
```


```python
y = symbols('y')
```


```python
b.subs({a:a*y, x:2*x, y:a/y})
```


```python
print(latex(b.subs({y:a/y, a:a*y, x:2*x})))
```


```python
c=b.subs({y:a/y})
c.subs({a:a/y, x:2*x})
```


```python
x, y, n = symbols('x y n')
b = acos(n*cos(x))
b.subs(cos(x),y)
```


```python
x y n = symbols('x y n')
b = acos(n*cos(x))
b.subs(cos(x),y)
```


```python
Tn = cos(n*acos(t))
```


```python
alpha = symbols('alpha',real=True)
Tn=Tn.subs(t,cos(alpha))
simplify(Tn)
```


```python
simplify(Tn)
```


```python
n, alpha = symbols('n alpha')
b = (2*cos(n*alpha)*cos(alpha*(n-1))-cos(alpha*(n-2)))**2
b.expand().subs(2*cos(alpha)*cos((n-1)*alpha)-cos((n-2)*alpha),cos(n*alpha))
```


```python
b.expand()
```


```python
b
```


```python
x, a = symbols('x a')
b = x + a
b.subs([(y,a/y), (a,a*y), (x,2*x)])
```


```python
T=toeplitz(6)
```


```python
t=symbols('t')
x=[0,t,1]
# The Vandermone Matrix
V = Matrix([[0, 0, 1], [t**2, t, 1], [1, 1,1]])
y = Matrix([0,1,-1])  # the data vcetor
a=V.LUsolve(y) # the coefficients
# the leading coefficient as a function of the parameter
a2 = Lambda(t,a[0](t))
```

## 


```python
a2
```


```python

```

59c2f9c3dc058ff0e996fb355ec96fe7bb95b169
ipynb
Jupyter Notebook
15-symbolicmanipulations/15-SymbolicComputations.ipynb
karant17/Scientific-Computing-with-Python-3
b300338dea1bb7676e908eb3643ad5d01bd19d72
MIT
2016-12-29T15:16:27.000Z
2021-11-08T09:30:08.000Z
15-symbolicmanipulations/15-SymbolicComputations.ipynb
karant17/Scientific-Computing-with-Python-3
b300338dea1bb7676e908eb3643ad5d01bd19d72
MIT
15-symbolicmanipulations/15-SymbolicComputations.ipynb
karant17/Scientific-Computing-with-Python-3
b300338dea1bb7676e908eb3643ad5d01bd19d72
MIT
2016-12-30T12:36:49.000Z
2021-05-06T07:44:05.000Z
Qwen/Qwen-72B
1. YES
2. YES