[英]How do I get __matmul__ of two different numpy.ndarray subclasses to return a particular subclass?
我有兩個np.ndarray
子類。 Tuple @ Matrix
返回一個Tuple
,但是Matrix @ Tuple
返回一個Matrix
。 我如何讓它返回一個Tuple
呢?
import numpy as np
class Tuple(np.ndarray):
def __new__(cls, input_array, info=None):
return np.asarray(input_array).view(cls)
class Matrix(np.ndarray):
def __new__(cls, input_array, info=None):
return np.asarray(input_array).view(cls)
def scaling(x, y, z):
m = Matrix(np.identity(4))
m[0, 0] = x
m[1, 1] = y
m[2, 2] = z
return m
例:
>>> Tuple([1,2,3,4]) @ scaling(2,2,2)
Tuple([2., 4., 6., 4.])
>>> scaling(2,2,2) @ Tuple([1,2,3,4])
Matrix([2., 4., 6., 4.]) # XXXX I'd like this to be a Tuple
PS: Matrix @ Matrix
應該返回Matrix
您可以重載__matmul__
方法以返回一個Tuple
-並且如果您想成為一個Tuple
如果任何變量是Tuple
和Matrix
否則我認為這會起作用:
class Matrix(np.ndarray):
def __new__(cls, input_array, info=None):
return np.asarray(input_array).view(cls)
def __matmul__(m1, m2):
return (m2.T @ m1.T).T if isinstance(m2, Tuple) else np.matmul(m1, m2)
在從np.matrix
示例進行復制時,我犯了一個錯誤。
class Tuple(np.ndarray):
__array_priority__ = 10
def __new__(cls, input_array, info=None):
return np.asarray(input_array).view(cls)
class Matrix(np.ndarray):
__array_priority__ = 5.0
def __new__(cls, input_array, info=None):
return np.asarray(input_array).view(cls)
In [2]: def scaling(x, y, z):
...: ...: m = Matrix(np.identity(4))
...: ...: m[0, 0] = x
...: ...: m[1, 1] = y
...: ...: m[2, 2] = z
...: ...: return m
...:
In [3]: Tuple([1,2,3,4]) @ scaling(2,2,2)
Out[3]: Tuple([2., 4., 6., 4.])
In [4]: scaling(2,2,2) @ Tuple([1,2,3,4])
Out[4]: Tuple([2., 4., 6., 4.])
===
從np.matrix
定義中獲取線索:numpy.matrixlib.defmatrix.py
添加__array_priority__
屬性:
In [382]: class Tuple(np.ndarray):
...: def __new__(cls, input_array, info=None):
...: __array_priority = 10
...: return np.asarray(input_array).view(cls)
...:
...: class Matrix(np.ndarray):
...: def __new__(cls, input_array, info=None):
...: __array_priority = 5
...: return np.asarray(input_array).view(cls)
...:
In [383]:
In [383]: def scaling(x, y, z):
...: m = Matrix(np.identity(4))
...: m[0, 0] = x
...: m[1, 1] = y
...: m[2, 2] = z
...: return m
...:
In [384]: Tuple([1,2,3,4]) @ scaling(2,2,2)
Out[384]: Tuple([2., 4., 6., 4.])
In [385]: scaling(2,2,2) @ Tuple([1,2,3,4])
Out[385]: Matrix([2., 4., 6., 4.])
解決此問題的一種方法是在Matrix
實現自定義__matmul__
,在Tuple
實現__rmatmul__
:
import numpy as np
class Tuple(np.ndarray):
def __new__(cls, input_array, info=None):
return np.asarray(input_array).view(cls)
def __rmatmul__(self, other):
return super().__matmul__(other)
class Matrix(np.ndarray):
def __new__(cls, input_array, info=None):
return np.asarray(input_array).view(cls)
def __matmul__(self, other):
if not isinstance(other, Matrix):
return NotImplemented
return super().__matmul__(other)
def scaling(x, y, z):
m = Matrix(np.identity(4))
m[0, 0] = x
m[1, 1] = y
m[2, 2] = z
return m
scaling(2,2,2) @ scaling(2,2,2)
# Matrix([[4., 0., 0., 0.],
# [0., 4., 0., 0.],
# [0., 0., 4., 0.],
# [0., 0., 0., 1.]])
Tuple([1,2,3,4]) @ scaling(2,2,2)
# Tuple([2., 4., 6., 4.])
scaling(2,2,2) @ Tuple([1,2,3,4])
# Tuple([2., 4., 6., 4.])
只需重載Matrix
類的__matmul__
即可返回元組
class Matrix(np.ndarray):
def __new__(cls, input_array, info=None):
return np.asarray(input_array).view(cls)
def __matmul__(self, other):
return other @ self
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.