Projection with dot product
import numpy as np
import matplotlib.pyplot as plt
import seaborn as snsProjection with dot product¶
# We are trying to solve for the p-vector
u = np.array([[0,0],[2,4]])
v = np.array([[0,0],[5,5]])
def project(a,b):
return a*(np.dot(a.T,b))/(np.dot(a.T,a))
p = project(v,u)
parray([[0., 0.],
[2., 4.]])# Projection Matrix
P = np.outer(a,a.T)/np.dot(a.T, a)
Parray([[0.02173913, 0.04347826, 0.08695652, 0.10869565],
[0.04347826, 0.08695652, 0.17391304, 0.2173913 ],
[0.08695652, 0.17391304, 0.34782609, 0.43478261],
[0.10869565, 0.2173913 , 0.43478261, 0.54347826]])u = u.flatten()
v = v.flatten()
p = [2,4,1,-1] # This is our projection
plt.quiver([u[0], v[0], p[0]],
[u[1], v[1], p[1]],
[u[2], v[2], p[2]],
[u[3], v[3], p[3]],
angles='xy', scale_units='xy', scale=1, color=sns.color_palette())
# plt.rc('text', usetex=True)
plt.xlim(-2, 6)
plt.ylim(-2, 8)
plt.axvline(x=0, color='grey')
plt.axhline(y=0, color='grey')
plt.text(0, 3, r'$||\vec{u}||$', color=sns.color_palette()[0], size=20)
plt.text(3, 1.5, r'$||\vec{v}||$', color=sns.color_palette()[1], size=20)
plt.text(2, 5, r'$||\vec{p}||$', color=sns.color_palette()[2], size=20)
plt.show()
plt.close()