maths: switch dot to faster for loop (#667)

* maths: switch dot to faster for loop

* Update src/utils/maths.js

---------

Co-authored-by: Joshua Lochner <admin@xenova.com>
This commit is contained in:
Varun Patil 2024-04-10 08:01:03 -07:00 committed by GitHub
parent cdbc532cf6
commit 15b90b7523
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 5 additions and 1 deletions

View File

@ -174,7 +174,11 @@ export function log_softmax(arr) {
* @returns {number} The dot product of arr1 and arr2.
*/
export function dot(arr1, arr2) {
return arr1.reduce((acc, val, i) => acc + val * arr2[i], 0);
let result = 0;
for (let i = 0; i < arr1.length; ++i) {
result += arr1[i] * arr2[i];
}
return result;
}