sympy / sympy

A computer algebra system written in pure Python
https://sympy.org/
Other
12.64k stars 4.35k forks source link

Exploiting distributive property when simplifying matrix expressions. #21058

Open sangongs opened 3 years ago

sangongs commented 3 years ago

When simplifying a scalar expression, SymPy smartly applies the distributive property:

>>> from sympy.abc import *
>>> simplify(a*b+a*c)
a*(b + c)

But it is not the case for matrix expressions:

>>> M=MatrixSymbol('M', n, n)
>>> N=MatrixSymbol('N', n, n)
>>> O=MatrixSymbol('O', n, n)
>>> simplify(M*N + M*O)
M*N + M*O

Although factor() function works:

>>> factor(M*N + M*O)
M*(N + O)

Reproduced with SymPy 1.7.1.

sylee957 commented 3 years ago

Matrix expressions generally follow the noncommutative algebra and I'm not sure that sympy have much framework built for noncommutative polynomials yet. Someone could try to implement such simplification as some word matching problems, but there could be some better systemic way.