Dot product of two arrays in Javascript
Topics related to client-side programming language.
Post questions and answers about JavaScript, Ajax, or jQuery codes and scripts.
-
Marius
- Posts:107
Dot product of two arrays in Javascript
What's an efficient way of implementing the dotProduct method (to get the Dot product [or scalar product] of two arrays) without importing any new Javascript libraries?
For example:
Code: Select all
const a = [1,2,3]
const b = [1,0,1]
const c = dotProduct(a,b) // will equal 4
Admin
Posts:805
Here is a method.
Use the map() function to create a new array with multiplied results of each index and the reduce() function to sum the values of resulting array.
Code: Select all
var dot = (a, b) => a.map((x, i) => a[i] * b[i]).reduce((m, n) => m + n);
const a = [1,2,3]
const b = [1,0,1]
console.log(dot(a, b)); // 4
Similar Topics
- Display message to every minute in Javascript
JavaScript - jQuery - Ajax
First post
Hello,
On eatch minute from the current hour I wanna have an message
I can not find out how to complete
I hope to get something like this (code...
Last post
If you only want to display a message to every minute, just use the setInterval() function. It calls a function repeatedly, over and over again, at...
- Hour and Minutes togheter in Javascript
JavaScript - jQuery - Ajax
First post
Dear Coursesweb I can not find out how to add the hours + minutes togheter.
<SCRIPT LANGUAGE= JavaScript >
day = new Date()
hr =...
Last post
See and use the following example:
<script>
var day = new Date();
let hr = day.getHours();
let mn = day.getMinutes();
let se =...