mathjs icon indicating copy to clipboard operation
mathjs copied to clipboard

Matrix sum along a dimension is defined to return a `MathScalarType`, but might return higher dimension

Open itaiperi opened this issue 1 year ago • 5 comments

Describe the bug When summing over an axis of a matrix, we always lose just one dimension. If the matrix is 2D, we'll get a 1D array. If the matrix is 3D, we'll get a 2D matrix. What happens now, is that math.sum declares that it returns a MathScalarType, instead of optionally returning a matrix.

To Reproduce

const mat = math.matrix([[1,2,3],[4,5,6]])
console.log(math.sum(mat, 0))

> DenseMatrix { _data: [ 5, 7, 9 ], _size: [ 3 ], _datatype: undefined }

itaiperi avatar May 12 '24 14:05 itaiperi

Thanks for reporting Itai.

We have to fix the TypeScript type definitions for sum (and check functions like prod to see if there are similar issues there).

Anyone able to help with a PR fixing the types?

josdejong avatar May 15 '24 07:05 josdejong

Could I get assigned to this?

deflucaseng avatar May 25 '24 00:05 deflucaseng

Thanks @deflucaseng, I've assigned you for this task.

josdejong avatar May 25 '24 09:05 josdejong

@josdejong I also saw that deepForEach.test.js was left as TODO. Is that something I can also work on?

deflucaseng avatar May 26 '24 04:05 deflucaseng

Definitely! Thanks Lucas. Can you please create two separate PR's, so each addresses 1 thing?

josdejong avatar May 27 '24 15:05 josdejong