Replies: 15 comments
-
Yes indeed. Not sure how complicated that will be to implement - typed arrays don't have the same methods available as regular arrays, so you really need to reckon with them. |
Beta Was this translation helpful? Give feedback.
-
Maybe implementing a wrapper to have a common interface between typed arrays and normal arrays. |
Beta Was this translation helpful? Give feedback.
-
yes, just not use the native funcitons like |
Beta Was this translation helpful? Give feedback.
-
It might be a bit tricky though because it's obviously more efficient to use native typed array methods. If for example there is a |
Beta Was this translation helpful? Give feedback.
-
So for typed arrays, maybe it would make sense to do like numpy : you have an option to specify which type your array should be when creating it. And I think that as @garrison suggested if using Typed arrays, it actually makes more sense to store the whole matrix in one array. But this sounds like it will have big implications on the API and the implementation. However I see this as something critical anyways, cause performance is a major goal (for me at least). |
Beta Was this translation helpful? Give feedback.
-
I would (at least for a first version) limit to a 2d array, stored in a single (1d) array. You address specific cells via indexing like described here on wikipedia. It will not affect the (external) API, though you will need to add support for this new Array type in each of the functions. Most functions perform element wise operations on a matrix, and that is done in |
Beta Was this translation helpful? Give feedback.
-
I'll give it a shot this week-end if I find time. |
Beta Was this translation helpful? Give feedback.
-
from #1438 -- why used TypedArrays We use HDF5 files for recording lots data in a compact format. We use https://hdf-ni.github.io/hdf5.node to pull the data into TypedArrays and we need to transform / save / send to client browsers. It would be nice to do the transforms using mathjs over the top of the TypedArrays without having to convert them because of size and performance reasons. Also if you want to use mathjs for any webgl computations - lots of this data can be sent via typedArrays... Thanks, Chad |
Beta Was this translation helpful? Give feedback.
-
Thanks for your inputs Chad. I'm curious to see how compact it is. And if/how much performance improves (I've done tests in the past with Anyone interested in looking into how we could start supporting TypedArrays, work out a proof of concept to see what it can bring? |
Beta Was this translation helpful? Give feedback.
-
We have already used math.js for low sample rate data and it works great. We are now looking to use the math.js expression parser to handle TypedArrays with millions of elements. I've been experimenting with overriding typed functions and as we're only using 1D arrays we can see some big boosts in performance. See the example code here, which attempts to profile the built in multipler, an override of For multiplying an array with 1 million entries by a constant number we get:
We're running in Electron so we could even call into native code for huge arrays where SIMD instructions can give us perhaps another 2-4x boost depending on hardware: 'Float32Array, number': function (x, y) {
const outArray = new Float32Array(x.length);
if(x.length > 1000000) {
native.multiply_32_constant(x, x.len, y, outArray)
} else {
for(var i = 0; i < x.length; i++){
outArray[i] = x[i] * y;
}
}
return outArray;
}, Obviously, exiting JavaScript land to do this is only going to be worth where it's actually faster! |
Beta Was this translation helpful? Give feedback.
-
Cool @timfish, thanks for sharing your experiments, appreciated! I did run your code locally, there it gives me the following results:
The large difference between |
Beta Was this translation helpful? Give feedback.
-
Happy to do a PR. A great stop-gap solution would be to add
Because Typed arrays only contain numbers, they can be targeted so types aren't checked on every element:
|
Beta Was this translation helpful? Give feedback.
-
Yes indeed, we can prevent type checking on all elements quite easily, luckily. I'm not sure about the stop gap solution, it gives a bit of a false sense of support for typed arrays, and it's probably just as easy to create a util function We do not have to implement support for typed arrays for all functions right away but I think it would at least implement a basic set of arithmetic and trigo functions before we publish it. One other idea: typed arrays are one dimensional. It would be nice to have a Matrix implementation which uses a typed array under the hood but allows putting two and/or multiple dimensions in it. Could be there are proper libraries out there that we could integrate. This could replace the current DenseMatrix implementation which uses nested Arrays. |
Beta Was this translation helpful? Give feedback.
-
I had assumed that I did notice the the README says:
Typed arrays will work in all the above mentioned environments although I think they are technically ES6. 🤷♂ |
Beta Was this translation helpful? Give feedback.
-
Yes that is correct. I was just thinking about a more generic Matrix implementation which allows you to select different types of Arrays to be used under the hood. In case of regular Arrays, the elements can contain any data type. Or maybe we should enforce all elements to have the same type. Just thinking aloud here. Most important and difficult change would be to change from a nested Array to a flat Array internally. |
Beta Was this translation helpful? Give feedback.
-
Right now mathjs doesn't work with typed arrays. For example :
Beta Was this translation helpful? Give feedback.
All reactions