dot product vectors code example

Example 1: dot product ocaml

let rec safe_dot_product (vect1: int list) (vect2: int list) : int option = 
  match (vect1, vect2) with
    | [],[] -> Some 0
    | [],_ -> None
    | _,[] -> None
    | (x::xs, y::ys) ->
      match safe_dot_product xs ys with
        | None -> None
        | Some m -> Some ((x*y) + m)

Example 2: dot product

Let a be a vector with coordinates (a1, a2, a3)
Let b be a vector with coordinates (b1, b2, b3)

Dot Product: 
 = a1*b1 + a2*b2 + a3*b3

a • b = |a||b|cosθ, where θ is the angle between the two vectors

Example 3: dot product array

import numpy.matlib 
import numpy as np 

a = np.array([[1,2],[3,4]]) 
b = np.array([[11,12],[13,14]]) 
np.dot(a,b)

Tags:

Misc Example