Skip to content

__array_ufunc__(np.matmul, ...) fails #266

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

Closed
crusaderky opened this issue Aug 11, 2019 · 2 comments · Fixed by #274
Closed

__array_ufunc__(np.matmul, ...) fails #266

crusaderky opened this issue Aug 11, 2019 · 2 comments · Fixed by #274

Comments

@crusaderky
Copy link
Contributor

crusaderky commented Aug 11, 2019

These work:

  • sparse.COO @ np.ndarray
  • sparse.matmul(sparse.COO, np.ndarray)
  • sparse.matmul(np.ndarray, sparse.COO)

These crash:

  • np.ndarray @ sparse.COO
  • np.matmul(sparse.COO, np.ndarray)
  • np.matmul(np.ndarray, sparse.COO)

Using:

  • Python 3.7
  • numpy 1.16
  • sparse 0.7.0
  • NUMPY_EXPERIMENTAL_ARRAY_FUNCTION=1
>>> import numpy
>>> import sparse
>>> a = numpy.zeros((2, 2))
>>> sa = sparse.COO(a)
>>> sa @ a
array([[0., 0.],
       [0., 0.]])
>>> a @ sa
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-9-85bcf6384716> in <module>
----> 1 a @ sa

/nfs/crusaderky/prg/github/sparse/sparse/coo/core.py in __array_ufunc__(self, ufunc, method, *inputs, **kwargs)
   1608 
   1609         if method == '__call__':
-> 1610             result = elemwise(ufunc, *inputs, **kwargs)
   1611         elif method == 'reduce':
   1612             result = COO._reduce(ufunc, *inputs, **kwargs)

/nfs/crusaderky/prg/github/sparse/sparse/coo/umath.py in elemwise(func, *args, **kwargs)
     46     """
     47 
---> 48     return _Elemwise(func, *args, **kwargs).get_result()
     49 
     50 

/nfs/crusaderky/prg/github/sparse/sparse/coo/umath.py in __init__(self, func, *args, **kwargs)
    427         self.cache = {}
    428 
--> 429         self._get_fill_value()
    430         self._check_broadcast()
    431 

/nfs/crusaderky/prg/github/sparse/sparse/coo/umath.py in _get_fill_value(self)
    476         # Some elemwise functions require a dtype argument, some abhorr it.
    477         try:
--> 478             fill_value_array = self.func(*zero_args, dtype=self.dtype, **self.kwargs)
    479             self.dtype = None
    480         except TypeError:

ValueError: matmul: Input operand 1 does not have enough dimensions (has 0, gufunc core with signature (n?,k),(k,m?)->(n?,m?) requires 1)

>>> numpy.matmul(a, sa)
(same ValueError as above)
@hameerabbasi
Copy link
Collaborator

For matmul, we'd need to special-case anything that's a gufunc but not a regular ufunc in __array_ufunc__. Are you willing to submit a patch for this?

@hameerabbasi
Copy link
Collaborator

(We'd need to route it through __array_function__).

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

Successfully merging a pull request may close this issue.

2 participants