ad icon indicating copy to clipboard operation
ad copied to clipboard

Does ad support any sparse matrix package?

Open facat opened this issue 11 years ago • 4 comments

ad supports numpy well, but doesn't work well with scipy.sparse. Many calculation involves sparse matrix. Does ad support one or plan to support?

facat avatar Oct 07 '13 04:10 facat

It wasn't designed into it. Admittedly, I don't have much experience with sparse matrix calculations. I'm happy to implement something that is useful. Do you have any recommendations for learning about sparse matrix mathematics?

tisimst avatar Oct 08 '13 03:10 tisimst

I have only basic knowledge about storage of sparse matrix, but I used it often in Matlab. Sparse matrix is very useful. It saves memory as well as computation time, because only non-zero elements are considered. Maybe wiki is a good palce to start. http://en.wikipedia.org/wiki/Sparse_matrix

facat avatar Oct 08 '13 13:10 facat

Ok, I've done some investigating into this issue and I believe that the way that scipy.sparse matrices are constructed and constrained (to basically be used with only numeric formats) doesn't allow this package to work with sparse matrices. Here's what I tried (maybe you can shed some light on the matter):

>>> from scipy.sparse import *  # coo_matrix, csr_matrix, etc.
>>> import numpy as np
>>> from ad import adnumber
>>> rows = np.array([0, 3, 1, 0])
>>> cols = np.array([0, 3, 1, 2])
>>> data = adnumber(np.array([4, 5, 7, 9]))
>>> mat = coo_matrix((data, (rows, cols)), shape=(4, 4))

Up to this point, I didn't have any complaints/warnings/errors show up. I can access the data and sparse indices at my leisure and I get exactly what I expect:

>>> mat.data
array([ad(4), ad(5), ad(7), ad(9)], dtype=object)

However, when I try to perform some sort of arithmetic operation, it coughs up a hairball because of conversions to another sparse format (CSR, to be exact):

>>> mat2 = mat + mat
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\site-packages\scipy\sparse\base.py", line 217, in __add__
    return self.tocsr().__add__(other)
  File "C:\Python27\lib\site-packages\scipy\sparse\coo.py", line 309, in tocsr
    data    = np.empty(self.nnz, dtype=upcast(self.dtype))
  File "C:\Python27\lib\site-packages\scipy\sparse\sputils.py", line 51, in upcast
    raise TypeError('no supported conversion for types: %s' % args)
TypeError: no supported conversion for types: object

If you have tried using sparse matrices, you probably ran into this already...

Part of the problem is the way that NumPy deals with non-numeric type objects (i.e., calling their dtype as just "object"). So, I looked into the source code of ..\scipy\sparse\sputils.py where the "upcast" function is defined and found that the only types of objects that are currently allowed in this "upcast" function are numbers that NumPy can cast into the following numeric types (I assume these are compiled numeric types):

  • int8, uint8,
  • short, ushort,
  • intc, uintc,
  • longlong, ulonglong,
  • single, double, longdouble,
  • csingle, cdouble, clongdouble.

I'm not sure if there's a good or easy way around this kind of constraint.

tisimst avatar Oct 08 '13 16:10 tisimst

I have asked this question to mailing list of scipy, but no one answers by now. Hope there's an easy way to get around.

facat avatar Oct 12 '13 14:10 facat