Scalar Product

Scalar Product of two vectors in N-th dimension

by evgkch

JavaScript

const getScalarProduct = ([xA, ...restA], [xB, ...restB]) => 
   (restA.length === 0 || restB.length === 0)
      ? xA * xB
      : xA * xB + getScalarProduct(restA, restB)
  
const getSquareModule = ([x, ...rest]) =>
   (rest.length === 0)
      ? x * x
      : x * x + getSquareModule(rest)

const getModule = (A) => Math.sqrt(getSquareModule(A))
  
const getCosOfAngle = (A, B) => getScalarProduct(A, B) / (getModule(A) * getModule(B))

console.log(getCosOfAngle([0,0,0,1],[1,1,1,1]))