Trace 函数 sympy 中的意外结果:Trace(A*B) 不等于 Trace(B*A)

Unexpected result in Trace function sympy: Trace(A*B) not equal to Trace(B*A)

我很困惑为什么下面的代码在sympy中会产生False,Python

import sympy
from sympy import MatrixSymbol, Trace

A = MatrixSymbol('A', 3, 3)
B = MatrixSymbol('B', 3, 3)

Trace(A*B)==Trace(B*A)

虽然众所周知 Tr(AB)=Tr(BA) https://en.wikipedia.org/wiki/Trace_(linear_algebra)#Trace_of_a_product

感谢任何帮助!

好吧,我看你不乐意放手。这是你如何真正得到 sympy 为你计算的

from sympy import shape, Matrix, symbols, trace
import numpy as np
from itertools import product

A = Matrix(np.array(symbols([f"a_{i}{j}" for i,j in product(range(1,4),range(1,4))])).reshape(3,3))
B = Matrix(np.array(symbols([f"b_{i}{j}" for i,j in product(range(1,4),range(1,4))])).reshape(3,3))
trace(A*B)-trace(B*A) == 0

最后给你 True