<snip>
Oops, corrected version attached. I think we need generalized functions in
linalg for mat x mat, mat x vec, and vec x mat, and versions that also work
for object arrays. I've used einsum in the attached file for the products,
but it doesn't work for object arrays.
This work is mainly to have a prototype version for the matmul operator
that can be used to verify behavior and write some tests.
Chuck
import numpy as np
class marray(np.ndarray):
__array_priority__ = 1000
def __new__(cls, *args, **kwargs):
return np.array(*args, **kwargs).view(cls)
def __matmul__(self, other):
if not isinstance(other, marray):
other = marray(other)
# matrix multiplication by scalars not allowed
if self.ndim == 0 or other.ndim == 0:
raise TypeError("matrix multiplication with scalars not "
"allowed, use '*' instead'")
if self.ndim == 1 and other.ndim == 1:
res = np.einsum('i, i', self, other)
elif self.ndim == 1:
res = np.einsum('i, ...ij', self, other)
elif other.ndim == 1:
res = np.einsum('...i, i', self, other)
else:
res = np.einsum('...ij, ...jk', self, other)
return res
def __rmatmul__(self, other):
return marray(other) @ self
_______________________________________________
NumPy-Discussion mailing list
[email protected]
http://mail.scipy.org/mailman/listinfo/numpy-discussion