Add matrix iteration like JS does #2656
Replies: 2 comments
-
The idea of the Matrix classes is that they handle multi dimensional arrays. The If you do need an array containing arrays, I think it's best to simply use a plain |
Beta Was this translation helpful? Give feedback.
-
This will be probably solved by #2177. Then you could do: for (const row of rows(m)) {
// here, row is [1] or [2]
} |
Beta Was this translation helpful? Give feedback.
-
For multi dimensional arrays in JS this is how forEach works:
e.g.
let ar1 = [1];
let ar2 = [2];
let arr = [ ar1, ar2 ];
arr.forEach( el => { /** el will equal ar1 or ar2 array object */ } );
In Mathjs the matrix().forEach returns the deepest/innermost element:
e.g.
let ar1 = [1];
let ar2 = [2];
let arr = matrix([ ar1, ar2 ]);
arr.forEach( el => { /** el will equal the number 1 or 2. NOT [1] or [2] */ } );
Right now if I want to loop through the element with the JS behavior I have to grab the hidden matrix array member "_data" which I assume is "wrong".
Please add some support to allow JS style looping through matrix elements.
This could be "easily" done by adding an optional/default parameter to the matrix.forEach method. Maybe "useJSBehavior" defaulted to false?
e.g.
arr.forEach( el => { /** el will equal the array [1] or [2]. */ }, true);
Beta Was this translation helpful? Give feedback.
All reactions