Skip to content
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

Does ad support any sparse matrix package? #4

Open
facat opened this issue Oct 7, 2013 · 4 comments
Open

Does ad support any sparse matrix package? #4

facat opened this issue Oct 7, 2013 · 4 comments

Comments

@facat
Copy link

facat commented Oct 7, 2013

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?

@tisimst
Copy link
Owner

tisimst commented Oct 8, 2013

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?

@facat
Copy link
Author

facat commented Oct 8, 2013

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

@tisimst
Copy link
Owner

tisimst commented Oct 8, 2013

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.

@facat
Copy link
Author

facat commented Oct 12, 2013

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants