代码之家  ›  专栏  ›  技术社区  ›  blue_note

在评估矩阵产品之前,如何打印矩阵产品?

  •  0
  • blue_note  · 技术社区  · 5 年前

    比如说,我有两个矩阵 a, b

    1. <a> @ <b> , 没有 实际执行矩阵乘法
    2. 乘法的实际结果(由sympy执行)

    有什么办法吗??

    0 回复  |  直到 5 年前
        1
  •  1
  •   Oscar Benjamin    5 年前

    您可以使用 MatMul : https://docs.sympy.org/latest/modules/matrices/expressions.html#sympy.matrices.expressions.MatMul

    In [16]: from sympy import *                                                                                                                   
    
    In [17]: A = Matrix([[1, 2], [3, 4]])                                                                                                          
    
    In [18]: B = Matrix([[5, 6], [7, 8]])                                                                                                          
    
    In [19]: product = MatMul(A, B)                                                                                                                
    
    In [20]: product                                                                                                                               
    Out[20]: 
    ⎡1  2⎤ ⎡5  6⎤
    ⎢    ⎥⋅⎢    ⎥
    ⎣3  4⎦ ⎣7  8⎦
    
    In [21]: product.doit()                                                                                                                        
    Out[21]: 
    ⎡19  22⎤
    ⎢      ⎥
    ⎣43  50⎦
    
    In [22]: Eq(product, product.doit())                                                                                                           
    Out[22]: 
    ⎡1  2⎤ ⎡5  6⎤   ⎡19  22⎤
    ⎢    ⎥⋅⎢    ⎥ = ⎢      ⎥
    ⎣3  4⎦ ⎣7  8⎦   ⎣43  50⎦
    
    推荐文章