# [vector](https://ghebook.blogspot.com/2010/07/vector.html)
> # Vector $\leftrightarrow$ Scalar
>> ## quaternion $\to$ vector
>> ## scalar multiply
>> ## addictive
>>> ### addition, subtraction


```python
import sympy as sm
import sympy.vector
B = sm.vector.CoordSys3D('B')
q0,q1,q2,q3 = sm.symbols('q0:4')
Q = B.orient_new_quaternion('Q',q0,q1,q2,q3)
Q.transformation_to_parent()
```




    (Q.x*(q0**2 + q1**2 - q2**2 - q3**2) + Q.y*(2*q0*q3 + 2*q1*q2) + Q.z*(-2*q0*q2 + 2*q1*q3),
     Q.x*(-2*q0*q3 + 2*q1*q2) + Q.y*(q0**2 - q1**2 + q2**2 - q3**2) + Q.z*(2*q0*q1 + 2*q2*q3),
     Q.x*(2*q0*q2 + 2*q1*q3) + Q.y*(-2*q0*q1 + 2*q2*q3) + Q.z*(q0**2 - q1**2 - q2**2 + q3**2))




```python
Q.transformation_from_parent()
```




$\displaystyle \left[\begin{matrix}\frac{\mathbf{{x}_{B}} \left(q_{0}^{2} + q_{1}^{2} - q_{2}^{2} - q_{3}^{2}\right)}{q_{0}^{4} + 2 q_{0}^{2} q_{1}^{2} + 2 q_{0}^{2} q_{2}^{2} + 2 q_{0}^{2} q_{3}^{2} + q_{1}^{4} + 2 q_{1}^{2} q_{2}^{2} + 2 q_{1}^{2} q_{3}^{2} + q_{2}^{4} + 2 q_{2}^{2} q_{3}^{2} + q_{3}^{4}} + \frac{\mathbf{{y}_{B}} \left(- 2 q_{0} q_{3} + 2 q_{1} q_{2}\right)}{q_{0}^{4} + 2 q_{0}^{2} q_{1}^{2} + 2 q_{0}^{2} q_{2}^{2} + 2 q_{0}^{2} q_{3}^{2} + q_{1}^{4} + 2 q_{1}^{2} q_{2}^{2} + 2 q_{1}^{2} q_{3}^{2} + q_{2}^{4} + 2 q_{2}^{2} q_{3}^{2} + q_{3}^{4}} + \frac{\mathbf{{z}_{B}} \left(2 q_{0} q_{2} + 2 q_{1} q_{3}\right)}{q_{0}^{4} + 2 q_{0}^{2} q_{1}^{2} + 2 q_{0}^{2} q_{2}^{2} + 2 q_{0}^{2} q_{3}^{2} + q_{1}^{4} + 2 q_{1}^{2} q_{2}^{2} + 2 q_{1}^{2} q_{3}^{2} + q_{2}^{4} + 2 q_{2}^{2} q_{3}^{2} + q_{3}^{4}}\\\frac{\mathbf{{x}_{B}} \left(2 q_{0} q_{3} + 2 q_{1} q_{2}\right)}{q_{0}^{4} + 2 q_{0}^{2} q_{1}^{2} + 2 q_{0}^{2} q_{2}^{2} + 2 q_{0}^{2} q_{3}^{2} + q_{1}^{4} + 2 q_{1}^{2} q_{2}^{2} + 2 q_{1}^{2} q_{3}^{2} + q_{2}^{4} + 2 q_{2}^{2} q_{3}^{2} + q_{3}^{4}} + \frac{\mathbf{{y}_{B}} \left(q_{0}^{2} - q_{1}^{2} + q_{2}^{2} - q_{3}^{2}\right)}{q_{0}^{4} + 2 q_{0}^{2} q_{1}^{2} + 2 q_{0}^{2} q_{2}^{2} + 2 q_{0}^{2} q_{3}^{2} + q_{1}^{4} + 2 q_{1}^{2} q_{2}^{2} + 2 q_{1}^{2} q_{3}^{2} + q_{2}^{4} + 2 q_{2}^{2} q_{3}^{2} + q_{3}^{4}} + \frac{\mathbf{{z}_{B}} \left(- 2 q_{0} q_{1} + 2 q_{2} q_{3}\right)}{q_{0}^{4} + 2 q_{0}^{2} q_{1}^{2} + 2 q_{0}^{2} q_{2}^{2} + 2 q_{0}^{2} q_{3}^{2} + q_{1}^{4} + 2 q_{1}^{2} q_{2}^{2} + 2 q_{1}^{2} q_{3}^{2} + q_{2}^{4} + 2 q_{2}^{2} q_{3}^{2} + q_{3}^{4}}\\\frac{\mathbf{{x}_{B}} \left(- 2 q_{0} q_{2} + 2 q_{1} q_{3}\right)}{q_{0}^{4} + 2 q_{0}^{2} q_{1}^{2} + 2 q_{0}^{2} q_{2}^{2} + 2 q_{0}^{2} q_{3}^{2} + q_{1}^{4} + 2 q_{1}^{2} q_{2}^{2} + 2 q_{1}^{2} q_{3}^{2} + q_{2}^{4} + 2 q_{2}^{2} q_{3}^{2} + q_{3}^{4}} + \frac{\mathbf{{y}_{B}} \left(2 q_{0} q_{1} + 2 q_{2} q_{3}\right)}{q_{0}^{4} + 2 q_{0}^{2} q_{1}^{2} + 2 q_{0}^{2} q_{2}^{2} + 2 q_{0}^{2} q_{3}^{2} + q_{1}^{4} + 2 q_{1}^{2} q_{2}^{2} + 2 q_{1}^{2} q_{3}^{2} + q_{2}^{4} + 2 q_{2}^{2} q_{3}^{2} + q_{3}^{4}} + \frac{\mathbf{{z}_{B}} \left(q_{0}^{2} - q_{1}^{2} - q_{2}^{2} + q_{3}^{2}\right)}{q_{0}^{4} + 2 q_{0}^{2} q_{1}^{2} + 2 q_{0}^{2} q_{2}^{2} + 2 q_{0}^{2} q_{3}^{2} + q_{1}^{4} + 2 q_{1}^{2} q_{2}^{2} + 2 q_{1}^{2} q_{3}^{2} + q_{2}^{4} + 2 q_{2}^{2} q_{3}^{2} + q_{3}^{4}}\end{matrix}\right]$




```python
import numpy as np
A1 = np.matrix([1,2,3])
A2 = np.matrix([4,5,6])
np.dot(A1,A2.T) == 1*4 + 2*5 + 3*6
np.cross(A1,A2) == np.matrix([2*6 - 5*3, 3*4 -1*6, 1*5 - 2*4])

a1 = np.array([1,2,3])
a2 = np.array([4,5,6])
np.dot(a1,a2) == 1*4 + 2*5 + 3*6
np.cross(a1,a2) == [2*6 - 5*3, 3*4 -1*6, 1*5 - 2*4]

c1 = np.array([1+2j,7+8j])
c2 = np.array([3+4j,5+6j])
# np.vdot(c1,c2) \iff \conj{c1} \dot c2
np.vdot(c1,c2) == np.conj(c1) * c2
np.dot(c1,c2) == c1*c2
np.dot(c1,c2)  == (1*3 - 2*4 + 7*5 - 8*6)+(1*4j + 2*3j + 7*6j + 5*8j)
np.cross(c1,c2) == (1+2j)*(5+6j) - (7+8j)*(3+4j)
```




    True




```python
import sympy as sm
import sympy.vector
C1 = sm.vector.CoordSys3D('C1')
C2 = sm.vector.CoordSys3D('C2')
x,y,z,u1,u2,u3,v1,v2,v3 = sm.symbols('x y z u1:4 v1:4')
u = sm.Matrix([u1,u2,u3])
v = sm.Matrix([v1,v2,u3])
sm.vector.dot(u,v)
u.dot(v)

sm.vector.cross(u,v)
u.cross(v)

u.project(v)
v = C1.i + C2.i
v.separate()
```




    {C1: C1.i, C2: C2.i}




```python

```




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



## Addition and Multiplication
> ## dot product
>> ## $ \vec{v} \cdot \vec{u} = ||\vec{v}||\: ||\vec{u}||\: cos(\theta)$
> ## cross product
>> ## $ \vec{v} \times \vec{u} = ||\vec{v}||\: ||\vec{u}||\: sin(\theta)\: \hat{k}$
> ## dyad product
>> ## $ \vec{v} \otimes \vec{u}$


```python
a*b, a*b , np.dot(a,b), np.cross(a,b)
```




    (array([ 7, 16, 27]), array([ 7, 16, 27]), 50, array([-6, 12, -6]))




```python
u+v, v.dot(u), u.cross(v),
```




    (Matrix([
     [u_1 + v_1],
     [u_2 + v_2],
     [    2*u_3]]),
     u_1*v_1 + u_2*v_2 + u_3**2,
     Matrix([
     [ u_2*u_3 - u_3*v_2],
     [-u_1*u_3 + u_3*v_1],
     [ u_1*v_2 - u_2*v_1]]))



> ## Length of vectors
>> ## norm
>>> ## $||\vec{v}||$


```python
np.linalg.norm(a)
```




    3.7416573867739413




```python
u.norm()
```




$\displaystyle \sqrt{\left|{u_{1}}\right|^{2} + \left|{u_{2}}\right|^{2} + \left|{u_{3}}\right|^{2}}$



> ## Projection 
>> ## $$ proj_v(u) = \frac{\vec{u}\cdot \vec{v}}{||\vec{v}||^2} = ||\vec{u}||\:cos(\theta)$$


```python
np.dot(a,b)/np.linalg.norm(b)
```




    50




```python

```

2dc4465f185695d603b443ca75bf2d3a251c1d34
ipynb
Jupyter Notebook
python/Vectors/vector.ipynb
karng87/nasm_game
a97fdb09459efffc561d2122058c348c93f1dc87
MIT
python/Vectors/vector.ipynb
karng87/nasm_game
a97fdb09459efffc561d2122058c348c93f1dc87
MIT
python/Vectors/vector.ipynb
karng87/nasm_game
a97fdb09459efffc561d2122058c348c93f1dc87
MIT
Qwen/Qwen-72B
1. YES
2. YES