-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
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
Constructing sparse matrices on your own? #1166
Comments
That is an interesting suggestion, thanks Mike. What comes close to this already is serialization of a SparseMatrix, though it has a different intention of course: const a = math.sparse(math.diag([1,2,3,4])
// a = [[1, 0, 0, 0], [0, 2, 0, 0], [0, 0, 3, 0], [0, 0, 0, 4]]
const jsonStr = JSON.stringify(a)
// jsonStr = '{"mathjs":"SparseMatrix","values":[1,2,3,4],"index":[0,1,2,3],"ptr":[0,1,2,3,4],"size":[4,4]}'
const b = JSON.parse(jsonStr, math.json.reviver)
// b = [[1, 0, 0, 0], [0, 2, 0, 0], [0, 0, 3, 0], [0, 0, 0, 4]] I think it would be relatively easy to create a factory function and a getter to get the internal arrays (Probably writing documentation for it the most work). Anyone interested in implementing this? |
@josdejong What is Vide: mathjs/src/function/matrix/transpose.js Lines 158 to 164 in 429276a
|
Uh oh, I completely forgot I wanted to implement the thing... |
Thanks @mike239x ! Now I see (I only knew about the In any case, I care about outer products (as I am developing Quantum Game v2.0). Is it easy to:
|
@stared #!/usr/bin/env node
const { sparse } = require('mathjs');
let m = sparse([[0, 1, 2], [3, 4, 5]]);
console.log(m); This yields Matrix {
_values: [ 3, 1, 4, 2, 5 ],
_index: [ 1, 0, 1, 0, 1 ],
_ptr: [ 0, 1, 3, 5 ],
_datatype: undefined,
_size: [ 2, 3 ]
} so it is actually column-wise sparse storage, Now, the conversion to function to_COO(m) {
let re = [];
for (let j = 0; j < m._ptr.length - 1; j++) {
let from = m._ptr[j];
let to = m._ptr[j+1];
for (let index = from; index < to; index++) {
let i = m._index[index];
let val = m._values[index];
re.push([ i, j, val])
}
}
return re;
} and if we continue the previous example [ [ 1, 0, 3 ], [ 0, 1, 1 ], [ 1, 1, 4 ], [ 0, 2, 2 ], [ 1, 2, 5 ] ] as expected. |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
It appears the only way to create a sparse matrix is to use
sparse
function, or construct the matrix piece by piece.How about creating a sparse matrix from data that represents sparse matrix?
For example, I'd like to have a function, that given the 3 arrays needed for the CCS format and the matrix size, will make that into a SparseMatrix object.
The text was updated successfully, but these errors were encountered: