vector-implimentation-2-vectors.ipynb

In [1]:
from xv.util import listAttr
import numpy as np

1. Create a vector

In [2]:
from sympy import Matrix
# from sympy.abc import a, b, x, y
from sympy.vector import CoordSys3D, matrix_to_vector
In [3]:
a, b = 1, 4
x, y = -2, 5
In [4]:
v1 = Matrix([a, b, 0])
v2 = Matrix([x, y, 0])
In [5]:
C = CoordSys3D('')
In [6]:
v3 = matrix_to_vector(v1, C)
display(v3)
$\displaystyle \mathbf{\hat{i}_{}} + (4)\mathbf{\hat{j}_{}}$
In [7]:
v4 = matrix_to_vector(v2, C)
display(v4)
$\displaystyle (-2)\mathbf{\hat{i}_{}} + (5)\mathbf{\hat{j}_{}}$
In [8]:
m_v3 = v3.to_matrix(C)
m_v3
Out[8]:
$\displaystyle \left[\begin{matrix}1\\4\\0\end{matrix}\right]$
In [9]:
m_v4 = v4.to_matrix(C)
m_v4
Out[9]:
$\displaystyle \left[\begin{matrix}-2\\5\\0\end{matrix}\right]$

2. Plot that vector

In [10]:
from xv.plotter.core.plot2d import Plotter
In [11]:
plotter = Plotter3D()
In [12]:
C_origin = C.origin.position_wrt(C.origin)
m_C_origin = C_origin.to_matrix(C)
In [13]:
arr_C_origin = np.array(m_C_origin).astype(np.float64)[:-1].flatten()
arr_v3 = np.array(m_v3).astype(np.float64)[:-1].flatten()
arr_v4 = np.array(m_v4).astype(np.float64)[:-1].flatten()
In [14]:
fig, ax = plotter.plot_vector(arr_C_origin, arr_v3, color = 'red')

plotter.plot_vector(arr_C_origin, arr_v4, color = 'blue', fig = fig,  ax = ax)
Out[14]:
(<Figure size 432x432 with 1 Axes>, <AxesSubplot:>)

3. Rotate and plot vector

In [15]:
theta  = np.pi/6
In [16]:
D = C.orient_new_axis('D', theta, C.k)
D
Out[16]:
$\displaystyle CoordSys3D\left(D, \left( \left[\begin{matrix}0.866025403784439 & 0.5 & 0\\-0.5 & 0.866025403784439 & 0\\0 & 0 & 1\end{matrix}\right], \ \mathbf{\hat{0}}\right), CoordSys3D\left(, \left( \left[\begin{matrix}1 & 0 & 0\\0 & 1 & 0\\0 & 0 & 1\end{matrix}\right], \ \mathbf{\hat{0}}\right)\right)\right)$
In [17]:
D_origin = D.origin.position_wrt(D.origin)
m_D_origin = D_origin.to_matrix(D)
In [18]:
from sympy.vector import express
In [19]:
v3_D = express(v3, D)
v4_D = express(v4, D)
# display(v3_D, v4_D)
In [20]:
m_v3_D = v3_D.to_matrix(D)
m_v4_D = v4_D.to_matrix(D)
# display(m_v3_D, m_v4_D)
In [21]:
arr_D_origin = np.array(m_D_origin).astype(np.float64)[:-1].flatten()
arr_v3_D = np.array(m_v3_D).astype(np.float64)[:-1].flatten()
arr_v4_D = np.array(m_v4_D).astype(np.float64)[:-1].flatten()
In [22]:
fig, ax = plotter.plot_vector(arr_C_origin, arr_v3, color = 'red')
plotter.plot_vector(arr_D_origin, arr_v3_D, color = 'pink', fig = fig,  ax = ax)

plotter.plot_vector(arr_C_origin, arr_v4, color = 'blue', fig = fig,  ax = ax)
plotter.plot_vector(arr_D_origin, arr_v4_D, color = 'skyblue', fig = fig,  ax = ax)
Out[22]:
(<Figure size 432x432 with 1 Axes>, <AxesSubplot:>)

4. Move and plot

In [23]:
dx = 3
In [24]:
E = C.locate_new('E', dx * C.i)
E
Out[24]:
$\displaystyle CoordSys3D\left(E, \left( \left[\begin{matrix}1 & 0 & 0\\0 & 1 & 0\\0 & 0 & 1\end{matrix}\right], \ (3)\mathbf{\hat{i}_{}}\right), CoordSys3D\left(, \left( \left[\begin{matrix}1 & 0 & 0\\0 & 1 & 0\\0 & 0 & 1\end{matrix}\right], \ \mathbf{\hat{0}}\right)\right)\right)$
In [25]:
E_origin = express(E.origin.position_wrt(C.origin), E)
m_E_origin = E_origin.to_matrix(E)
In [30]:
v3_E = express(v3, E) + E_origin
v4_E = express(v4, E) + E_origin
# display(v3_E, v4_E)
In [31]:
m_v3_E = v3_E.to_matrix(E)
m_v4_E = v4_E.to_matrix(E)
# display(m_v3_E, m_v4_E)
In [28]:
arr_E_origin = np.array(m_E_origin).astype(np.float64)[:-1].flatten()
arr_v3_E = np.array(m_v3_E).astype(np.float64)[:-1].flatten()
arr_v4_E = np.array(m_v4_E).astype(np.float64)[:-1].flatten()
In [29]:
fig, ax = plotter.plot_vector(arr_C_origin, arr_v3, color = 'red')
plotter.plot_vector(arr_D_origin, arr_v3_D, color = 'pink', fig = fig,  ax = ax)
plotter.plot_vector(arr_E_origin, arr_v3_E, color = 'orange', fig = fig,  ax = ax)

plotter.plot_vector(arr_C_origin, arr_v4, color = 'blue', fig = fig,  ax = ax)
plotter.plot_vector(arr_D_origin, arr_v4_D, color = 'skyblue', fig = fig,  ax = ax)
plotter.plot_vector(arr_E_origin, arr_v4_E, color = 'm', fig = fig,  ax = ax)
Out[29]:
(<Figure size 432x432 with 1 Axes>, <AxesSubplot:>)
In [ ]: