Hi All,

I've attached a subclass of ndarray that implements the new '@' operator
for experimentation and comment. It is only intended for playing with that
operator and may not work for other things. You will need to install python
3.5.0a1 to play with it.

Chuck
import numpy as np

class array(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, array):
            other = array(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'")

        # one of the arguments is 1d
        if self.ndim == 1:
            res = np.dot(self[None, :], other)
            res.shape = res.shape[1:]
            return res
        if other.ndim == 1:
            res = np.dot(self, other[:, None])
            res.shape = res.shape[:-1]
            return res

        # dot handles rest of these
        return np.dot(self, other)

    def __rmatmul__(self, other):
        return array(other) @ self
_______________________________________________
NumPy-Discussion mailing list
[email protected]
http://mail.scipy.org/mailman/listinfo/numpy-discussion

Reply via email to