Rosetta Code

Dot product

Compute the dot product of two vectors of equal length.

Intro View source
Source rosettacode/popular/dot_product.vibe
# title: Dot product
# source: https://rosettacode.org/wiki/Dot_product
# category: Rosetta Code
# difficulty: Intro
# summary: Compute the dot product of two vectors of equal length.
# tags: popular, math, arrays
# vibe: 0.2

def dot_product(a, b)
  total = 0
  i = 0
  while i < a.size
    total = total + (a[i] * b[i])
    i = i + 1
  end
  total
end

def run
  {
    simple: dot_product([1, 3, -5], [4, -2, -1]),
    zeros: dot_product([0, 0, 0], [1, 2, 3]),
    identity: dot_product([1, 0, 0], [0, 1, 0]),
    large: dot_product([10, 20, 30], [4, 5, 6])
  }
end
Output
Press run to execute run from this example.
rosetta-code popular math arrays browser-runner