Skip to content

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

Closed
mike239x opened this issue Jul 12, 2018 · 5 comments
Closed

Constructing sparse matrices on your own? #1166

mike239x opened this issue Jul 12, 2018 · 5 comments

Comments

@mike239x
Copy link

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.

@josdejong
Copy link
Owner

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?

@stared
Copy link

stared commented Sep 6, 2019

@josdejong What is ptr anyway?

Vide:

return m.createSparseMatrix({
values: cvalues,
index: cindex,
ptr: cptr,
size: [columns, rows],
datatype: m._datatype
})
.

@mike239x
Copy link
Author

mike239x commented Sep 6, 2019

Uh oh, I completely forgot I wanted to implement the thing...
@stared see CSR storage for sparse matrices, ptr is supposed to be column index for the non-zero elements in the matrix.
I haven't touched that in a while though so better double-check.

@stared
Copy link

stared commented Sep 7, 2019

Thanks @mike239x !

Now I see (I only knew about the (i, j, val) format).

In any case, I care about outer products (as I am developing Quantum Game v2.0). Is it easy to:

@mike239x
Copy link
Author

mike239x commented Sep 8, 2019

@stared
so, first of all, regarding index and ptr - I made a minimal example to just see what exactly what field means:

#!/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, index indicates which row the value is in and ptr indicates where in values new columns start.

Now, the conversion to (i, j, val) should be fairly easy: i-s are values in index, val-s are values in values, and an array of j-s can be constructed from ptr, so altogether one could code something like that:

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 to_COO(m) would return

[ [ 1, 0, 3 ], [ 0, 1, 1 ], [ 1, 1, 4 ], [ 0, 2, 2 ], [ 1, 2, 5 ] ]

as expected.
As for Kronecker product I think I better write in the comments to the corresponding issue.

Repository owner locked and limited conversation to collaborators Sep 2, 2022
@josdejong josdejong converted this issue into discussion #2758 Sep 2, 2022

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Projects
None yet
Development

No branches or pull requests

3 participants