1

__mul__matrix中的方法如何numpy?我想实现一个二进制矩阵乘法,我有类Binary

class Binary(int):
    def __init__(self, val):
        if val != 0:
            self.val = 1
        else: self.val = 0
    def __add__(self,other):
        print('add')
        return self.val^other
    def __radd__(self, other):
        print('radd')
        return self.val^other

我的测试:

from Binary import Binary
from numpy import matrix

i = Binary(1)
o = Binary(0)
a = matrix([i, i, o, i, i, o, o], dtype=Binary)
b = matrix([[o, o, i],
           [o, i, o],
           [o, i, i],
           [i, o, o],
           [i, o, i],
           [i, i, o],
           [i, i, i]], dtype=Binary)
print(a*b)

结果:

/test.py
[[2 1 2]]

__add__没用过的方法。而矩阵乘法中有求和?

4

1 回答 1

1

NumPy Matrix 与 Array 类的乘法有何不同?

http://wiki.scipy.org/NumPy_for_Matlab_Users

A*B 是矩阵乘法,所以更方便线性代数。确保 A & B 是 numpy 数组/矩阵

于 2013-11-03T17:36:22.547 回答