In [1]:
import sympy as sym
import numpy as np
import matplotlib.pyplot as plt
from IPython.display import display,Math

Vector-scalar multiplication: Algebra

$\lambda v \rightarrow 7 \left[ \begin{matrix} -1 \\ 0 \\ 1 \end{matrix} \right] = \left[ \begin{matrix} -7 \\ 0 \\ 7 \end{matrix} \right]$

In [2]:
v = np.array([.5,1])
s = [1,-.5,2,.5]

for si in s:
    sv = si*v
    plt.plot([0,sv[0]],[0,sv[1]],'o-',linewidth=3,label='$\\lambda=%g$' %si)
    
plt.axis('square')
plt.axis([-3,3,-3,3])
plt.grid()
plt.legend()
plt.show()

Vector addition and subtraction: Algebra

$\left[ \begin{matrix} 1 \\ 0 \\ 4 \\ 3 \end{matrix} \right] + \left[ \begin{matrix} 2 \\ -3 \\ -2 \\ 1 \end{matrix} \right] = \left[ \begin{matrix} 3 \\ -3 \\ 2 \\ 4 \end{matrix} \right]$

In [3]:
v1 = np.array([-1,2])
v2 = np.array([1,1])

v3a = v1+v2
v3b = np.add(v1,v2)
v3c = np.zeros(2)
for i in range(0,2):
    v3c[i] = v1[i] + v2[i]
    
print(v3a,v3b,v3c)
[0 3] [0 3] [0. 3.]

Vector addition: Geometry

$\left[ \begin{matrix} 1 \\ 2 \end{matrix} \right] + \left[ \begin{matrix} 2 \\ 1 \end{matrix} \right] = \left[ \begin{matrix} 3 \\ 3 \end{matrix} \right]$

In [4]:
plt.plot([0,v1[0]],[0,v1[1]],linewidth=3,label='$v_1$')
plt.plot([0,v2[0]]+v1[0],[0,v2[1]]+v1[1],linewidth=3,label='$v_2$')
plt.plot([0,v3a[0]],[0,v3a[1]],linewidth=3,label='$v_3$')

s1 = sym.latex(sym.sympify(v1))
print(str(v1) + ' ' + str(type(v1)))
print(str(sym.latex(v1)) + ' ' + str(type(sym.latex(v1))))
print(str(sym.sympify(v1)) + ' ' + str(type(sym.sympify(v1))))
print(str(sym.latex(sym.sympify(v1))) + ' ' + str(type(sym.latex(sym.sympify(v1)))))

s2 = sym.latex(sym.sympify(v2))
s3 = sym.latex(sym.sympify(v3a))

display(Math('%s+%s=%s' %(s1,s2,s3)))

plt.axis('square')
plt.axis([-2,2,-2,4])
plt.legend()
plt.grid()
plt.show()
[-1  2] <class 'numpy.ndarray'>
[-1  2] <class 'str'>
[-1, 2] <class 'sympy.tensor.array.dense_ndim_array.ImmutableDenseNDimArray'>
\left[\begin{matrix}-1 & 2\end{matrix}\right] <class 'str'>
$\displaystyle \left[\begin{matrix}-1 & 2\end{matrix}\right]+\left[\begin{matrix}1 & 1\end{matrix}\right]=\left[\begin{matrix}0 & 3\end{matrix}\right]$