Created
September 4, 2017 19:41
-
-
Save butchi/26b74025b21a52afcf7e4a6617eceab8 to your computer and use it in GitHub Desktop.
Inner product by JavaScript (ES2015)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function dot(a, b) { | |
const len = Math.max(a.length, a.length); | |
let zeros = new Array(len).fill(0); | |
let ret; | |
ret = zeros.reduce((p, c, i) => { | |
const itemA = a[i] || 0; | |
const itemB = b[i] || 0; | |
const val = itemA * itemB; | |
return p + val; | |
}, 0); | |
return ret; | |
} | |
const vecA = [1, 2, 3]; | |
const vecB = [4, 5, 6]; | |
vec = dot(vecA, vecB); | |
console.log(vec); // => 32 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment