import sympy as sym
import numpy as np
import matplotlib.pyplot as plt
from IPython.display import display,Math
$\alpha = a \cdot b = \langle a, b \rangle = a^Tb = \sum_{i=1}^n a_i b_i$
v = np.arange(10,22,3)
w = np.arange(5,15,3)
print(v)
print(w)
dp1 = 0
for i in range(0,len(v)):
dp1 += v[i] * w[i]
dp2 = np.sum(np.multiply(v,w))
dp3 = np.dot(v,w)
print(dp1,dp2,dp3)
Exercise
def myDotProduct(v,w):
print(np.shape(v))
v = np.squeeze(v)
print(np.shape(v))
w = np.squeeze(w)
# test whether the dot product is defined
if len(v)!=len(w):
raise ValueError('Vectors must have the same length!')
# compute and display the dot product
display(Math('v^Tw=%g' %np.dot(v,w)))
v1 = np.random.randn(1,10)
v2 = np.random.randn(1,10)
myDotProduct(v1,v2)