GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — flatten "fails" on SparseMatrix · Issue #3530 · josdejong/mathjs

flatten "fails" on SparseMatrix #3530

New issue
Copy link
New issue
Copy link
Closed
Closed
flatten "fails" on SparseMatrix#3530
Copy link
Labels
bugdesign decision
Milestone
v15

Description

@gwhitney
gwhitney
opened on Sep 11, 2025
Issue body actions

Describe 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:

  1. 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:

  1. 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.)

Activity

  1. josdejong commented on Sep 17, 2025

    @josdejong
    josdejong
    on Sep 17, 2025
    Owner
    More actions

    EDIT: sorry, I reacted about the size function issue in this topic instead of flatten, moved that to #3529.

  2. josdejong
    added
    design decision
    on Sep 17, 2025
  3. josdejong commented on Sep 17, 2025

    @josdejong
    josdejong
    on Sep 17, 2025
    Owner
    More actions

    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.

  4. josdejong
    added
    bug
    on Sep 17, 2025
  5. gwhitney commented on Sep 17, 2025

    @gwhitney
    gwhitney
    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.

  6. josdejong
    added this to the v15 milestone on Sep 17, 2025
  7. josdejong commented on Sep 17, 2025

    @josdejong
    josdejong
    on Sep 17, 2025
    Owner
    More actions

    👍 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.

  8. gwhitney commented on Sep 17, 2025

    @gwhitney
    gwhitney
    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

  9. gwhitney commented on Sep 17, 2025

    @gwhitney
    gwhitney
    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.

  10. josdejong commented on Sep 17, 2025

    @josdejong
    josdejong
    on Sep 17, 2025
    Owner
    More actions

    👍 yes makes sense.

  11. josdejong
    added
    help wanted
    and removed
    help wanted
    on Sep 24, 2025
  12. josdejong
    added a commit that references this issue on Sep 24, 2025

    fix: #3530 throw an error when trying to `flatten` a `SparseMatrix`

    This issue will close once commit a6795b7 is merged into the 'develop' branch.
    a6795b7
  13. josdejong commented on Sep 24, 2025

    @josdejong
    josdejong
    on Sep 24, 2025
    Owner
    More actions

    Addressed via #3536. @gwhitney can you have a short look at the PR? It's very small.

  14. josdejong commented on Sep 24, 2025

    @josdejong
    josdejong
    on Sep 24, 2025
    Owner
    More actions

    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.
  15. gwhitney commented on Sep 24, 2025

    @gwhitney
    gwhitney
    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.

    Reacted by Jos de Jong
  16. gwhitney
    added a commit that references this issue on Oct 8, 2025

    fix: #3530 throw an error when trying to `flatten` a `SparseMatrix` (#…

    This issue will close once commit 338dfe9 is merged into the 'develop' branch.
    338dfe9
  17. josdejong
    closed this as completedon Oct 9, 2025
  18. josdejong commented on Oct 10, 2025

    @josdejong
    josdejong
    on Oct 10, 2025
    Owner
    More actions

    Fixed now in v15.0.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugdesign decision

    Type

    No type

    Projects

    No projects

      Milestone

      • v15No due date

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402792 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.