In [1]:
# setup SymPy
from sympy import *
x, y, z, t = symbols('x y z t')
init_printing()

# setup plotting
%matplotlib notebook
import matplotlib.pyplot as mpl
from util.plot_helpers import plot_vec, plot_vecs, autoscale_arrows

SymPy Matrix objects


In [2]:
v = Matrix([1,2,3])
v


Out[2]:
$$\left[\begin{matrix}1\\2\\3\end{matrix}\right]$$

In [3]:
# define symbolically
v_1, v_2, v_3 = symbols('v_1 v_2 v_3')
v = Matrix([v_1,v_2,v_3])

In [4]:
v


Out[4]:
$$\left[\begin{matrix}v_{1}\\v_{2}\\v_{3}\end{matrix}\right]$$

In [5]:
v.T


Out[5]:
$$\left[\begin{matrix}v_{1} & v_{2} & v_{3}\end{matrix}\right]$$

In [6]:
A = Matrix(
    [   [1,7],
        [2,8], 
        [3,9]   ])
A


Out[6]:
$$\left[\begin{matrix}1 & 7\\2 & 8\\3 & 9\end{matrix}\right]$$

In [7]:
# define symbolically
a_11, a_12, a_21, a_22, a_31, a_32 = symbols('a_11 a_12 a_21 a_22 a_31 a_32')
A = Matrix([
        [a_11, a_12],
        [a_21, a_22], 
        [a_31, a_32]])

In [8]:
A


Out[8]:
$$\left[\begin{matrix}a_{11} & a_{12}\\a_{21} & a_{22}\\a_{31} & a_{32}\end{matrix}\right]$$

Vector operations


In [9]:
u_1, u_2, u_3 = symbols('u_1 u_2 u_3')
u = Matrix([u_1,u_2,u_3])
v_1, v_2, v_3 = symbols('v_1 v_2 v_3')
v = Matrix([v_1,v_2,v_3])
alpha = symbols('alpha')

u


Out[9]:
$$\left[\begin{matrix}u_{1}\\u_{2}\\u_{3}\end{matrix}\right]$$

In [10]:
alpha*u


Out[10]:
$$\left[\begin{matrix}\alpha u_{1}\\\alpha u_{2}\\\alpha u_{3}\end{matrix}\right]$$

In [11]:
u+v


Out[11]:
$$\left[\begin{matrix}u_{1} + v_{1}\\u_{2} + v_{2}\\u_{3} + v_{3}\end{matrix}\right]$$

In [12]:
u.norm()


Out[12]:
$$\sqrt{\left|{u_{1}}\right|^{2} + \left|{u_{2}}\right|^{2} + \left|{u_{3}}\right|^{2}}$$

In [13]:
uhat = u/u.norm()
uhat


Out[13]:
$$\left[\begin{matrix}\frac{u_{1}}{\sqrt{\left|{u_{1}}\right|^{2} + \left|{u_{2}}\right|^{2} + \left|{u_{3}}\right|^{2}}}\\\frac{u_{2}}{\sqrt{\left|{u_{1}}\right|^{2} + \left|{u_{2}}\right|^{2} + \left|{u_{3}}\right|^{2}}}\\\frac{u_{3}}{\sqrt{\left|{u_{1}}\right|^{2} + \left|{u_{2}}\right|^{2} + \left|{u_{3}}\right|^{2}}}\end{matrix}\right]$$

In [14]:
u = Matrix([1.5,1])
w = 2*u
uhat = u/u.norm()

fig = mpl.figure()
plot_vecs(u, w, uhat)
autoscale_arrows()


Dot product


In [15]:
u = Matrix([u_1,u_2,u_3])
v = Matrix([v_1,v_2,v_3])

u.dot(v)


Out[15]:
$$u_{1} v_{1} + u_{2} v_{2} + u_{3} v_{3}$$

In [16]:
fig = mpl.figure()
u = Matrix([1,1])
v = Matrix([3,0])
plot_vecs(u,v)
autoscale_arrows()

u_dot_v = u.dot(v)
u_dot_v


Out[16]:
$$3$$

In [17]:
phi = acos( u.dot(v)/(u.norm()*v.norm()) )
print('angle between u and v is', phi)
u.norm()*v.norm()*cos(phi)


angle between u and v is pi/4
Out[17]:
$$3$$

Cross product


In [18]:
u = Matrix([u_1,u_2,u_3])
v = Matrix([v_1,v_2,v_3])

u.cross(v)


Out[18]:
$$\left[\begin{matrix}u_{2} v_{3} - u_{3} v_{2}\\- u_{1} v_{3} + u_{3} v_{1}\\u_{1} v_{2} - u_{2} v_{1}\end{matrix}\right]$$

In [19]:
u = Matrix([1,0,0])
v = Matrix([1,1,0])
w = u.cross(v)      # a vector perpendicular to both u and v

mpl.figure()
plot_vecs(u, v, u.cross(v))



In [20]:
print('length of cross product', w.norm())

phi = acos( u.dot(v)/(u.norm()*v.norm()) )

w.norm() == u.norm()*v.norm()*sin(phi)


length of cross product 1
Out[20]:
True

Projection operation


In [21]:
def proj(vec, d):
    """Computes the projection of vector `vec` onto vector `d`."""
    return d.dot(vec)/d.norm() * d/d.norm()

In [22]:
fig = mpl.figure()
u = Matrix([1,1])
v = Matrix([3,0])

pu_on_v = proj(u,v)

plot_vecs(u, v, pu_on_v)


# autoscale_arrows()
ax = mpl.gca()
ax.set_xlim([-1,3])
ax.set_ylim([-1,3])


Out[22]:
$$\left ( -1, \quad 3\right )$$

Matrix operations


In [23]:
a_11, a_12, a_21, a_22, a_31, a_32 = symbols('a_11 a_12 a_21 a_22 a_31 a_32')
A = Matrix([
        [a_11, a_12],
        [a_21, a_22], 
        [a_31, a_32]])
b_11, b_12, b_21, b_22, b_31, b_32 = symbols('b_11 b_12 b_21 b_22 b_31 b_32')
B = Matrix([
        [b_11, b_12],
        [b_21, b_22], 
        [b_31, b_32]])
alpha = symbols('alpha')

In [24]:
A


Out[24]:
$$\left[\begin{matrix}a_{11} & a_{12}\\a_{21} & a_{22}\\a_{31} & a_{32}\end{matrix}\right]$$

In [25]:
A + B


Out[25]:
$$\left[\begin{matrix}a_{11} + b_{11} & a_{12} + b_{12}\\a_{21} + b_{21} & a_{22} + b_{22}\\a_{31} + b_{31} & a_{32} + b_{32}\end{matrix}\right]$$

In [26]:
alpha*A


Out[26]:
$$\left[\begin{matrix}a_{11} \alpha & a_{12} \alpha\\a_{21} \alpha & a_{22} \alpha\\a_{31} \alpha & a_{32} \alpha\end{matrix}\right]$$

In [27]:
v_1, v_2 = symbols('v_1 v_2')
v = Matrix([v_1,v_2])

A*v


Out[27]:
$$\left[\begin{matrix}a_{11} v_{1} + a_{12} v_{2}\\a_{21} v_{1} + a_{22} v_{2}\\a_{31} v_{1} + a_{32} v_{2}\end{matrix}\right]$$

In [28]:
A[:,0]*v[0] + A[:,1]*v[1]


Out[28]:
$$\left[\begin{matrix}a_{11} v_{1} + a_{12} v_{2}\\a_{21} v_{1} + a_{22} v_{2}\\a_{31} v_{1} + a_{32} v_{2}\end{matrix}\right]$$

In [29]:
A = Matrix([
        [a_11,a_12],
        [a_21, a_22], 
        [a_31, a_32]])
B = Matrix([
        [b_11,b_12],
        [b_21, b_22]])

A*B


Out[29]:
$$\left[\begin{matrix}a_{11} b_{11} + a_{12} b_{21} & a_{11} b_{12} + a_{12} b_{22}\\a_{21} b_{11} + a_{22} b_{21} & a_{21} b_{12} + a_{22} b_{22}\\a_{31} b_{11} + a_{32} b_{21} & a_{31} b_{12} + a_{32} b_{22}\end{matrix}\right]$$

In [30]:
A.T


Out[30]:
$$\left[\begin{matrix}a_{11} & a_{21} & a_{31}\\a_{12} & a_{22} & a_{32}\end{matrix}\right]$$

In [31]:
print('the shape of v is ', v.shape)
v


the shape of v is  (2, 1)
Out[31]:
$$\left[\begin{matrix}v_{1}\\v_{2}\end{matrix}\right]$$

In [32]:
print('the shape of v.T is ', v.T.shape)
v.T


the shape of v.T is  (1, 2)
Out[32]:
$$\left[\begin{matrix}v_{1} & v_{2}\end{matrix}\right]$$

In [33]:
u = Matrix([u_1,u_2,u_3])
v = Matrix([v_1,v_2,v_3])

u.T*v


Out[33]:
$$\left[\begin{matrix}u_{1} v_{1} + u_{2} v_{2} + u_{3} v_{3}\end{matrix}\right]$$

In [34]:
u * v.T


Out[34]:
$$\left[\begin{matrix}u_{1} v_{1} & u_{1} v_{2} & u_{1} v_{3}\\u_{2} v_{1} & u_{2} v_{2} & u_{2} v_{3}\\u_{3} v_{1} & u_{3} v_{2} & u_{3} v_{3}\end{matrix}\right]$$

In [35]:
A = Matrix([
  [3,       3],
  [2,  S(3)/2]
])
A


Out[35]:
$$\left[\begin{matrix}3 & 3\\2 & \frac{3}{2}\end{matrix}\right]$$

In [36]:
A.inv()


Out[36]:
$$\left[\begin{matrix}-1 & 2\\\frac{4}{3} & -2\end{matrix}\right]$$

In [37]:
A * A.inv()


Out[37]:
$$\left[\begin{matrix}1 & 0\\0 & 1\end{matrix}\right]$$

In [38]:
A.inv() * A


Out[38]:
$$\left[\begin{matrix}1 & 0\\0 & 1\end{matrix}\right]$$

In [39]:
B = Matrix([
        [b_11,b_12],
        [b_21, b_22]])
B


Out[39]:
$$\left[\begin{matrix}b_{11} & b_{12}\\b_{21} & b_{22}\end{matrix}\right]$$

In [40]:
B.trace()


Out[40]:
$$b_{11} + b_{22}$$

In [41]:
B.det()


Out[41]:
$$b_{11} b_{22} - b_{12} b_{21}$$

In [ ]: