Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fromJSON - rebuild sparse matrix from results of toJSON #2293

Closed
slutske22 opened this issue Jul 30, 2021 · 3 comments
Closed

fromJSON - rebuild sparse matrix from results of toJSON #2293

slutske22 opened this issue Jul 30, 2021 · 3 comments
Labels

Comments

@slutske22
Copy link

Perhaps this is just a lack of understanding, but here's what I'm trying to do. I create a matrix in a NodeJS application. After some manipulation, it needs to be sent to the front end, and manipulated further.

To send it, it needs to be serialized, which can be done with the toJSON method. The results of a toJSON method look something like this:

Screen Shot 2021-07-30 at 11 31 35 AM

How can I transform this back into a sparse matrix on the front end, with all the utility of the class methods (.set, .get, Symbol[Iterator], etc)? Is there a simple way to do that? Surely I must be missing something. Thank you!

@josdejong
Copy link
Owner

You can stringify and parse like:

const myMatrix = math.sparse([[1, 2], [3, 4]]) // SparseMatrix
const myStringifiedMatrix = JSON.stringify(myMatrix) // string

// ...do something: persist, or transfer, or something else

const myRevivedMatrix = JSON.parse(myStringifiedMatrix, math.reviver) // SparseMatrix

You'll need a mathjs instance in the frontend too, then you can use JSON.parse(..., math.reviver). If you know you're dealing with a SparseMatrix you could also directly call SparseMatrix.fromJSON(myStringifiedMatrix).

See docs: https://mathjs.org/docs/core/serialization.html

@slutske22
Copy link
Author

SparseMatrix.fromJSON(myStringifiedMatrix) is exactly what I was looking for!

I realize it is indeed in the docs, but I think my biggest issue is the way the docs are organized -there's a wealth of information in the reference section, but you sort of have to dig to realize its there. Perhaps a dedicated docs website, with a left-hand nested menu, where its very obvious what classes are available, and all the methods on a class, would be helpful. Anyway, I'm just nitpicking, thank you for your answer and this library!

@josdejong
Copy link
Owner

👍

I realize it is indeed in the docs, but I think my biggest issue is the way the docs are organized -there's a wealth of information in the reference section, but you sort of have to dig to realize its there. Perhaps a dedicated docs website, with a left-hand nested menu, where its very obvious what classes are available, and all the methods on a class, would be helpful. Anyway, I'm just nitpicking, thank you for your answer and this library!

Yeah, agree, thanks for your suggestions. Related: #1769

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants