Skip to content

Instantly share code, notes, and snippets.

@iszlai
Created December 28, 2014 20:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iszlai/494128e7967ac0d475f9 to your computer and use it in GitHub Desktop.
Save iszlai/494128e7967ac0d475f9 to your computer and use it in GitHub Desktop.
Dot product calc
def dot(first: Array[Array[Double]], second: Array[Int]): Array[Double] = {
val result = new Array[Double](first.size)
for (i <- 0 to (first.size - 1)) {
result(i) = dotProduct(first(i), second)
}
return result
}
def dotProduct(first: Array[Double], second: Array[Int]): Double = {
require(first.size == second.size)
(for ((a, b) <- first zip second) yield a * b) sum
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment