Replies: 6 comments 1 reply
-
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? |
Beta Was this translation helpful? Give feedback.
-
@josdejong What is Vide: mathjs/src/function/matrix/transpose.js Lines 158 to 164 in 429276a |
Beta Was this translation helpful? Give feedback.
-
Uh oh, I completely forgot I wanted to implement the thing... |
Beta Was this translation helpful? Give feedback.
-
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:
|
Beta Was this translation helpful? Give feedback.
-
@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. |
Beta Was this translation helpful? Give feedback.
-
Hi! Has this been implemented yet? math.sparse(rowIndexArray, colIndexArray, valueArray, rowSize, colSize) The documentation could have some examples on how to create sparse matrices. |
Beta Was this translation helpful? Give feedback.
-
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.
Beta Was this translation helpful? Give feedback.
All reactions