矩阵乘法。蟒蛇

时间:2013-11-03 17:33:44

标签: python numpy matrix

__mul__中课程matrix中的方法numpy如何? 我想实现二进制矩阵乘法,我有类二进制

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__未使用。矩阵乘法中有求和吗?

1 个答案:

答案 0 :(得分:1)

how does multiplication differ for NumPy Matrix vs Array classes?

http://wiki.scipy.org/NumPy_for_Matlab_Users

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