In [1]:
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$

In [2]:
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)
[10 13 16 19]
[ 5  8 11 14]
596 596 596

Exercise

In [3]:
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)))
In [4]:
v1 = np.random.randn(1,10)
v2 = np.random.randn(1,10)

myDotProduct(v1,v2)
(1, 10)
(10,)
$\displaystyle v^Tw=-1.24636$