pydata / sparse

Sparse multi-dimensional arrays for the PyData ecosystem

Home Page:https://sparse.pydata.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Proper usage of sparse 3D array: initialization, assignment of values and use in calculations

e-dub opened this issue · comments

commented

Very cool library, nice work everyone!

I would now like to use a sparse 3D array for calculation. I would like to preallocate the sparse array size then assign value by index. Finally I would like to use this in mathematical operations, i.e. multiplications and addition.

I think I need to first use dok format to initialize and then transform it into gcxs for manipulation. Is this correct? Is there a best practice for this?

Here is a code to show what I mean:

import sparse
import numpy as np

ndof = 1000
nx = 100
x = np.ones((ndof,))
A = sparse.DOK((nx, ndof, ndof))
A[10,10,10] = 10.0
# more index-based value assignments 
# ...
# change DOK to gcxs for efficient calculation
# operations, e.g. 
b = A@x

Many thanks in advance!

I would recommend COO over GCXS as it is better tested, but in essence, yes; that's what you'd do.

Closing as the original question was answered. Please open a new issue or comment/re-open if needed.