GeezerVison is showing github.com — flatten "fails" on SparseMatrix · Issue #3530 · josdejong/mathjs
flatten "fails" on SparseMatrix #3530DescriptionDescribe the bug
The documentation states that flatten returns a 1-dimensional collection. However, since it returns a SparseMatrix when the input is a SparseMatrix, and SparseMatrix cannot represent a 1-dimensional collection, this property is impossible to fulfill.
To Reproduce
const SM = math.sparse([[1],[0]])
const M2 = math.flatten(SM) // this is a no-op, even though the input is two-dimensional!
Discussion
I think the documented property of flatten, that it always return a 1-dimensional result, is the most important property to preserve for computational convenience of mathjs. It is a pain to deal with possibly 2-dimensional outputs of flatten if you happen to get a SparseMatrix to work on. Therefore, one proposal to deal with this issue would be:
- Make
flatten() an exception to the general principle that mathjs functions that take a collection argument return the same type of collection as the input, and return an array when the input is an array, and a 1-dimensional DenseMatrix otherwise.
However, the principle that the type of argument is preserved is also generally speaking (issue #3529 notwithstanding) also very useful for computation with mathjs, so this issue also possibly suggests an alternate proposal:
- Perhaps it's not really semantically consistent for mathjs to have an implementation of Matrix that cannot represent 1-dimensional collections. So instead, enhance SparseMatrix to allow at least for 1-dimensional as well as 2-dimensional collections (if not arbitrary dimensions). Then have flatten() return a 1-dimensional SparseMatrix when its input is a SparseMatrix. Also adjust other instances of return values of SparseMatrix for other functions that would naturally be 1-dimensional. After all, sparse vectors are definitely a thing that one sometimes wants to compute with.
Note that proposal (2) would lead to another possible to solution to issue #3529: if SparseMatrix could represent 1-dimensional collections, then size() could preserve the type of its input while still satisfying the property that inputs of the same size produce results that are deepEqual(). (However, I personally would still recommend having size() return the same type regardless of the type of its argument, on the principle that the size is a property of any collection, and the same property should have the same type for any collection. Just raising the point that allowing 1-dim SparseMatrix would afford a third solution to #3529.) Activityjosdejong commented on Sep 17, 2025 EDIT: sorry, I reacted about the size function issue in this topic instead of flatten, moved that to #3529. josdejong commented on Sep 17, 2025 Since SparseMatrix can only hold a 2D matrix, it has limitations. Maybe it is best to just accept those limitations, and throw an error when you try to do something that is not supported.
For example, when you use the .reshape(...) method on a SparseMatrix and try to reshape it into a 1D or 3D matrix, you will get an "Error: Sparse matrices can only be reshaped in two dimensions". Maybe we should simply throw an error when trying to flatten a SparseMatrix, just like we cannot reshape a SparseMatrix into 1D. If you do want to flatten a sparse matrix, you can always convert it to a DenseMatrix or Array first, making this type conversion explicit.
Looking at the unit tests of function flatten, I do not see tests for SparseMatrix, so I think it was not a concious decision to let flatten(SparseMatrix) return a 2D SparseMatrix. gwhitney commented on Sep 17, 2025 CollaboratorAuthor More actions I agree, making operations that "should" return a collection of dimension other than 2 throw when they must return a SparseMatrix would be a much easier path that would turn this issue into a potential annoyance, hopefully minor, rather than a bug. josdejong commented on Sep 17, 2025 👍 let's go for throwing an Error in case of flatten(SparseMatrix) then. It will be a breaking change, so I'll schedule this for v15. gwhitney commented on Sep 17, 2025 CollaboratorAuthor More actions Whoever attends to this should scan for any other cases (besides size()) where a sparsematrix that "ought" to be 1d is returned gwhitney commented on Sep 17, 2025 CollaboratorAuthor More actions I'd suggest the error message mentio that 1d sparse matrices "currently not implemented " or something like that -' there's no conceptual problem with 1d sparse vectors and they certainly have uses. josdejong commented on Sep 17, 2025 added a commit that references this issue on Sep 24, 2025fix: #3530 throw an error when trying to `flatten` a `SparseMatrix` This issue will close once commit a6795b7 is merged into the 'develop' branch.a6795b7 josdejong commented on Sep 24, 2025 Addressed via #3536. @gwhitney can you have a short look at the PR? It's very small. josdejong commented on Sep 24, 2025 I've gone through the function reference to check if there are other functions with a similar issue, but I couldn't find new cases:
flatten addressed in #3536
size addressed via #3535
reshape, resize: already throw an error when resizing a SparseMatrix into 1-d or n-d
- Maybe we would like to reconsider the return type of
row and column, which currently always return a 2-d Array/DenseMatrix. But I think there is no need for that.
gwhitney commented on Sep 24, 2025 CollaboratorAuthor More actions
* Maybe we would like to reconsider the return type of `row` and `column`, which currently always return a 2-d Array/DenseMatrix. But I think there is no need for that.
I guess to have any distinction between a row and column you need to be returning something 2D. You can always use flatten if you want the 1d collection of the row or column.
I will look at the two PRs as soon as I can -- it will likely be a couple of days. added a commit that references this issue on Oct 8, 2025fix: #3530 throw an error when trying to `flatten` a `SparseMatrix` (#… This issue will close once commit 338dfe9 is merged into the 'develop' branch.338dfe9 josdejong commented on Oct 10, 2025
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|