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 — Change return type of size() of Dense and SparseMatrix to avoid inconsistencies · Issue #3529 · josdejong/mathjs

Change return type of size() of Dense and SparseMatrix to avoid inconsistencies #3529

New issue
Copy link
New issue
Copy link
Closed
Closed
Change return type of size() of Dense and SparseMatrix to avoid inconsistencies#3529
Copy link
Labels
bug
Milestone
v15

Description

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

Describe the bug
Consider

const SM = math.sparse([[1, 0]])
const DM = math.matrix([[1, 0]])

Both SM and DM represent the 2×1 matrix [[1, 0]], just with different internal representations. But as we shall see, they do not have equal or even equivalent results of the size() function which purports to give the length of each dimension of a matrix. Since those lengths match for these two entities, size() should produce at least equivalent results on them.

To Reproduce
Now execute

console.log(size(SM).toString()) // [[1], [2]]
console.log(size(DM).toString()) // [1, 2]

The difficulty is that the current convention is that size() returns a collection of the same type as its argument, when its argument is a collection. But critically, SparseMatrix cannot currently encode anything but 2-dimensional collections, so size(SM) returns a column vector as a 1×2 SparseMatrix, whereas size(DM) returns a 1-dimensional DenseMatrix (of length 2). These two return values are not === or == or even math.deepEqual(), which is inappropriate given that SM and DM are surely of identical size.

Discussion

The mathjs library should have a size() function with the property that the return values are at least deepEqual on any two entities that are the same size. I think the size() function provides an example in which it is counterproductive to return different types depending on the type of the input. The size() function is not like, say, the diff() function that returns a new collection based on the entries of an input collection, where it is helpful to preserve the type of the input in generating the output. Rather, it is providing a property of the input, and it is a property that all representations of collections have in common, and so that property should have a fixed type regardless of the specific type of the collection argument. It is just a coincidence that the property happens to be collection-valued, and so it is a red herring to make the type of that property depend on the type of the input, since it's the same property regardless.

Therefore, I have two proposals to resolve the difficulty, either of which could be adopted (they are mutually exclusive), or I would be open to other ideas that would preserve the condition that matrices of the same size have deepEqual size() values.

  1. Just adopt the convention that the size of (any instance of any type of) matrix is a plain JavaScript array. This type for the size is completely adequate and appropriate in that the size is just a list of numbers. This proposal is also completely consistent with the fact that the M.size() method always returns a plain JavaScript array. (This latter fact is not sufficient, however, to resolve the problem with the size() function, because there is no A.size() method on plain arrays, and there needs to be some consistent way to get the size of any collection that might be an Array or either type of Matrix.)

  2. Instead, always have size(M) return a collection of the type config.matrix (defaulting to DenseMatrix if config.matrix is unspecified), regardless of the specific type of the collection M.

Note that both of these proposals have the property that the type of the size of a collection does not depend on the specific type of the collection, which I would recommend as the proper way to go. Personally, I have a slight preference for (1), but could really go either way.

Unfortunately either of these proposals is a breaking change, so I would hope this issue could be considered, and a fix be chosen, in time to include a PR in mathjs 15 or at worst 16.

Activity

  1. josdejong commented on Sep 17, 2025

    @josdejong
    josdejong
    on Sep 17, 2025
    Owner
    More actions

    Thanks for bringing this up Glen. To summarize, I think we have the following options:

    1. Leave this inconsistency as it is (returning a 2D SparseMatrix when the input is a SparseMatrix)
    2. Solve this in an inconsistent way as a "special" case by letting size(SparseMatrix) return a 1D Array or DenseMatrix.
    3. Extend SparseMatrix to support 1D data.
    4. Change function size() to for example always return an Array regardless of the input type.

    Thoughts on this:

    • I don't like (1) and (2) very much.
    • It would be good to investigate what is needed for option (3). My feeling is that it will involve more than I would like. And it will make the SparseMatrix class more complicated. Right now SparseMatrix is easy to reason about, being "just" for 2D data and that's it.
    • Like you Glen, I have a preference for (4). I think this is most useful in practice. Also, the .size() methods of both SparseMatrix and DenseMatrix already return an Array, so it would make the function size(...) consistent with these methods. The only downside is that it would be a breaking change.
  2. josdejong
    added
    bug
    design decision
    on Sep 17, 2025
  3. gwhitney commented on Sep 17, 2025

    @gwhitney
    gwhitney
    on Sep 17, 2025
    CollaboratorAuthor
    More actions

    You missed Option 4(b), change function size to always return a 1-dimensional config.matrix type entity, regardless of the input type. I have no strong preference between 4 and 4(b), either of which I like better than the others. The only reason to think about (3) would be if we were generalizing SparseMatrix for other reasons, like #3530.

  4. josdejong commented on Sep 17, 2025

    @josdejong
    josdejong
    on Sep 17, 2025
    Owner
    More actions

    Yes indeed, (3) is only interesting is there is a broader need for this.

    I do have a preference for option (4) over (4b): when having Array input it feels odd to always have size() return a Matrix, which is a "higher level" data type than Array.

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

    @gwhitney
    gwhitney
    on Sep 17, 2025
    CollaboratorAuthor
    More actions

    Fine by me. I am presuming (4) is the planned resolution then.

  7. josdejong commented on Sep 17, 2025

    @josdejong
    josdejong
    on Sep 17, 2025
    Owner
    More actions

    Yes let's go for (4): function size() and methods .size() always return an Array regardless of the input type.

  8. gwhitney
    removed
    design decision
    on Sep 17, 2025
  9. gwhitney
    changed the title [-]Inconsistency in size() of Dense and SparseMatrix[/-] [+]Change return type of size() of Dense and SparseMatrix to avoid inconsistencies[/+] on Sep 17, 2025
  10. josdejong
    added a commit that references this issue on Sep 24, 2025

    fix: #3529 function `size` now always returns an `Array`

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

    @josdejong
    josdejong
    on Sep 24, 2025
    Owner
    More actions

    Will be addressed via #3535

  12. josdejong
    added a commit that references this issue on Oct 9, 2025

    fix: #3529 change function `size` to always return an `Array` (#3535)

    This issue will close once commit 302ed81 is merged into the 'develop' branch.
    302ed81
  13. josdejong
    closed this as completedon Oct 9, 2025
  14. josdejong commented on Oct 10, 2025

    @josdejong
    josdejong
    on Oct 10, 2025
    Owner
    More actions

    Published 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

    bug

    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.