Reputation: 5407
I'm working with arrays of sparse matrices using SciPy, and sometimes some of the matrices have no rows or columns (that is, shape = (0,0)
). I would like my various other functions to just ignore these matrices, or do some other smart default action.
It seems I can't use any of the sparse matrix formats, because they complain about "invalid shape". For some reason matrix([])
and matrix([[]])
have shape = (1,0)
, so these are not much good (actually, is that a bug? It makes little sense...).
I know that I could use None
, []
, array([])
or something similar. However standard matrix manipulation functions will fail when handed these, meaning I would need to scatter
if block is not None:
or similar throughout my code.
Is there a matrix class that I've missed which will allow me to handle these cases gracefully?
For anyone curious: this comes out of chopping up a finite element Jacobian matrix into blocks by the type of variable. Some of these variables may be pinned (fixed to a constant value), and so have no derivatives. Hence no entries in the Jacobian.
Upvotes: 2
Views: 1353
Reputation: 102
You can try below codes and it is easy.
import numpy as np np.zeros((0,0))
Upvotes: 0
Reputation: 114946
There are many ways to create a 0x0 sparse matrix. For example,
In [16]: from scipy.sparse import csr_matrix, coo_matrix, dok_matrix, eye
In [17]: csr_matrix(([], [[],[]]), shape=(0,0))
Out[17]:
<0x0 sparse matrix of type '<type 'numpy.float64'>'
with 0 stored elements in Compressed Sparse Row format>
In [18]: coo_matrix(([],[[],[]]), shape=(0,0))
Out[18]:
<0x0 sparse matrix of type '<type 'numpy.float64'>'
with 0 stored elements in COOrdinate format>
In [19]: dok_matrix((0,0))
Out[19]:
<0x0 sparse matrix of type '<type 'numpy.float64'>'
with 0 stored elements in Dictionary Of Keys format>
In [20]: eye(0)
Out[20]:
<0x0 sparse matrix of type '<type 'numpy.float64'>'
with 0 stored elements (1 diagonals) in DIAgonal format>
In [21]: csr_matrix(np.zeros((0, 0)))
Out[21]:
<0x0 sparse matrix of type '<type 'numpy.float64'>'
with 0 stored elements in Compressed Sparse Row format>
You said you were chopping up a Jacobian matrix. If you index a sparse matrix with slices that have zero length, you'll get a 0x0 result:
In [22]: a = np.random.randint(0, 2, size=(10,10))
In [23]: m = csr_matrix(a)
In [24]: m[3:3, 3:3]
Out[24]:
<0x0 sparse matrix of type '<type 'numpy.int64'>'
with 0 stored elements in Compressed Sparse Row format>
(I'm using scipy 0.13.2.)
It is possible that some sparse operations will not handle a 0x0 matrix properly, and that might be a bug. If you have some examples, could you add them to your question?
Upvotes: 1
Reputation: 10769
matrix([[]]) -> shape = (1,0) makes perfect sense; you gave it one row, with zero columns; how is numpy supposed to guess what you really wanted was zero rows instead?
All array creation functions such as np.zeros((0,0)) work for your purposes.
Upvotes: 1
Reputation: 32521
Yes. You can do
from scipy import sparse
result = sparse.eye(0)
# result.shape = (0, 0)
Upvotes: 3