python - 矩阵乘法。 python

标签 python numpy matrix

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

最佳答案

how does multiplication differ for NumPy Matrix vs Array classes?

http://wiki.scipy.org/NumPy_for_Matlab_Users

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

关于python - 矩阵乘法。 python ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19755815/

相关文章:

python - 从大型 Pandas DataFrame 中删除行的快速、高效的方法

python - 属性错误: module 'tensorflow.python.pywrap_tensorflow' has no attribute 'TFE_Py_RegisterExceptionClass'

python - 为什么 numpy 在查找矩阵中的非零元素方面更快?

python - numpy 线性代数求解器

python - 从每行中选择 N 个元素,不循环

r - 使随机相关矩阵半定正

c++ - 复制构造函数中的内存分配错误 (c++)

python - 用转置版本填充矩阵

python - 使用 R 抓取 PDF

python - 如何更改 python 日志记录以显示从脚本执行开始时耗时?