Python - 具有不同列数的乘法矩阵

时间:2015-05-24 23:58:30

标签: python numpy matrix matrix-multiplication

我正在尝试多个两个矩阵A和B,其中B的列比A使用python和numpy更多。

示例:

A = numpy.matrix([[2,3,15],[5,8,12],[1,13,4]], dtype=numpy.object)
B = numpy.matrix([[2,15,6,15,8,14],[17,19,17,7,18,14],[24,14,0,24,2,11]], dtype=numpy.object)

( A*B ) = [[415,297,63,411,100,235],[434,395,166,419,208,314], [319,318,227,202,250,240]]

我发现了一些例子,如果它们是数组,但如果它们是矩阵则没有。有人可以帮助我吗?

2 个答案:

答案 0 :(得分:3)

你真的试过这个吗?它在这里工作正常:

import numpy as np

def main():
    A = np.matrix([[2,3,15],[5,8,12],[1,13,4]], dtype=np.object)
    B = np.matrix([[2,15,6,15,8,14],[17,19,17,7,18,14],[24,14,0,24,2,11]], dtype=np.object)

    C = ( A*B ) % 26 #  = [[25,11,11,21,22,1],[18,5,10,3,0,2], [7,6,19,20,16,6]]
    print(C)
    return 0

if __name__ == '__main__':
    main()

打印:

[[25 11 11 21 22 1]
 [18 5 10 3 0 2]
 [7 6 19 20 16 6]]

答案 1 :(得分:2)

您也可以使用点积。

theory ToyList
imports Main
begin
     datatype 'a list = Nil |  Cons 'a "'a list"

     fun app :: "'a list ⇒ 'a list ⇒ 'a list"  where 
                 "app Nil ys   = ys" |
                 "app (Cons x xs) ys = Cons x (app xs ys)"

它会输出您想要的相同答案。