Would you like to clone this notebook?

When you clone a notebook you are able to make changes without affecting the original notebook.

Cancel

math.js Typed Arrays

node v11.15.0
version: 1.0.3
endpointsharetweet
var math = require("mathjs") var testArray1 = Array.from({ length: 1000000 }, () => Math.floor(Math.random() * 2000000) ); let output; // Warm up for(var i = 0; i < 50; i++) { output = math.multiply(testArray1, Math.random()) } console.time('built-in') output = math.multiply(testArray1, Math.random()) console.timeEnd('built-in') math.typed.addType({ name: 'Float32Array', test: function (x) { return x instanceof Float32Array } }) // Override the function const multiply = math.typed('multiply', { 'Array, number': function (a, b) { const outArray = new Array(a.length); for(var i = 0; i < a.length; i++){ outArray[i] = a[i] * b; } return outArray; }, 'Float32Array, number': function (a, b) { const outArray = new Float32Array(a.length); for(var i = 0; i < a.length; i++){ outArray[i] = a[i] * b; } return outArray; } }) math.import({ multiply: multiply }, { override: true } ) // Warm up for(var i = 0; i < 50; i++) { output = math.multiply(testArray1, Math.random()) } console.time('override') output = math.multiply(testArray1, Math.random()) console.timeEnd('override') const typedArray1 = Float32Array.from(testArray1); // Warm up for(var i = 0; i < 50; i++) { output = math.multiply(typedArray1, Math.random()) } console.time('override-typed') output = math.multiply(typedArray1, Math.random()) console.timeEnd('override-typed')
Loading…

no comments

    sign in to comment