<a href="https://colab.research.google.com/github/tofighi/Linear-Algebra/blob/main/7B_Rotation_Matrices_with_sympy_and_numpy.ipynb" target="_parent"></a>


```python
import numpy as np
import sympy as sp
import math
sp.init_printing(use_unicode=True)
```

# Compare sin30 and cos30 (numpy, sympy)


```python
# sin30 and cos30 using numpy: 
sin30 = np.sin(30*math.pi/180)
cos30 = np.cos(30*math.pi/180)
print(sin30, cos30)
```

    0.49999999999999994 0.8660254037844387



```python
# sin30 and cos30 using sympy ... with 'regular' pi: 
sin30 = sp.sin(30*math.pi/180)
cos30 = sp.cos(30*math.pi/180)
print(sin30, cos30)
```

    0.500000000000000 0.866025403784439



```python
# sin30 and cos30 using sympy with exact pi: 
sin30e = sp.sin(30*sp.pi/180)
cos30e = sp.cos(30*sp.pi/180)
print(sin30e, cos30e)
```

    1/2 sqrt(3)/2



```python
# results for sympy calculations are shown in LaTeX if called outside of print():
sin30e
```


```python
# that's more obvious here: 
cos30e
```

# Setup rotation matrices and apply them to u=[3,2]


```python
# rot30 will be a sympy matrix with exact values. 
rot30 = sp.Matrix([[cos30e, -1*sin30e], [sin30e, cos30e]])
rot30
```




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




```python
u = sp.Matrix([3,2])
u
```




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




```python
# Remember you can use * for matrix multiplication in sympy only
R30u = rot30*u
R30u
```




$\displaystyle \left[\begin{matrix}-1 + \frac{3 \sqrt{3}}{2}\\\frac{3}{2} + \sqrt{3}\end{matrix}\right]$




```python
# Let's repeat our results using numpy: 
rot30n = np.array([[cos30, -1*sin30], [sin30, cos30]])
rot30n
```




    array([[0.866025403784439, -0.500000000000000],
           [0.500000000000000, 0.866025403784439]], dtype=object)




```python
# Numpy does floating point calculations so all answers are in decimals. It's fast but less precise than sympy. 
R30un = np.dot(rot30n, u)
R30un
```




    array([[1.59807621135332],
           [3.23205080756888]], dtype=object)



9d4a30521520424dfa623258fc33038f46416c27
ipynb
Jupyter Notebook
7B_Rotation_Matrices_with_sympy_and_numpy.ipynb
tofighi/Linear-Algebra
bea7d2a4a81e0c49b324f23c47cf03db72e376cf
CC0-1.0
7B_Rotation_Matrices_with_sympy_and_numpy.ipynb
tofighi/Linear-Algebra
bea7d2a4a81e0c49b324f23c47cf03db72e376cf
CC0-1.0
7B_Rotation_Matrices_with_sympy_and_numpy.ipynb
tofighi/Linear-Algebra
bea7d2a4a81e0c49b324f23c47cf03db72e376cf
CC0-1.0
Qwen/Qwen-72B
1. YES
2. YES