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
In [2]:
v = Matrix([1,2,3])
v
Out[2]:
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]:
In [5]:
v.T
Out[5]:
In [6]:
A = Matrix(
[ [1,7],
[2,8],
[3,9] ])
A
Out[6]:
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]:
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]:
In [10]:
alpha*u
Out[10]:
In [11]:
u+v
Out[11]:
In [12]:
u.norm()
Out[12]:
In [13]:
uhat = u/u.norm()
uhat
Out[13]:
In [14]:
u = Matrix([1.5,1])
w = 2*u
uhat = u/u.norm()
fig = mpl.figure()
plot_vecs(u, w, uhat)
autoscale_arrows()
In [15]:
u = Matrix([u_1,u_2,u_3])
v = Matrix([v_1,v_2,v_3])
u.dot(v)
Out[15]:
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]:
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)
Out[17]:
In [18]:
u = Matrix([u_1,u_2,u_3])
v = Matrix([v_1,v_2,v_3])
u.cross(v)
Out[18]:
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)
Out[20]:
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]:
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]:
In [25]:
A + B
Out[25]:
In [26]:
alpha*A
Out[26]:
In [27]:
v_1, v_2 = symbols('v_1 v_2')
v = Matrix([v_1,v_2])
A*v
Out[27]:
In [28]:
A[:,0]*v[0] + A[:,1]*v[1]
Out[28]:
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]:
In [30]:
A.T
Out[30]:
In [31]:
print('the shape of v is ', v.shape)
v
Out[31]:
In [32]:
print('the shape of v.T is ', v.T.shape)
v.T
Out[32]:
In [33]:
u = Matrix([u_1,u_2,u_3])
v = Matrix([v_1,v_2,v_3])
u.T*v
Out[33]:
In [34]:
u * v.T
Out[34]:
In [35]:
A = Matrix([
[3, 3],
[2, S(3)/2]
])
A
Out[35]:
In [36]:
A.inv()
Out[36]:
In [37]:
A * A.inv()
Out[37]:
In [38]:
A.inv() * A
Out[38]:
In [39]:
B = Matrix([
[b_11,b_12],
[b_21, b_22]])
B
Out[39]:
In [40]:
B.trace()
Out[40]:
In [41]:
B.det()
Out[41]:
In [ ]: