I'm trying to compute the commutant of a set of matrices, so the set of matrices that commutes with all matrices in the original set.
In principle I thought it may be possible to define a matrix of variables M in scipy and then simplify and solve M@B-B@M=0 to get all solutions for a single matrix B, which I could then expand to check for every B in my set of matrices. However, I am not quite sure how to execute it as Im not very familiar with symbolic stuff in python.
Maybe there is also a numeric way I am unaware of.
Sympy can do this. The following makes every element of M an unknown, and solves for the set of values which makes M@B - B@M zero.
Output:
This represents the result in two ways. The first is a matrix of values which must all be equal to zero for M to commute. The second is a dictionary where the previous system of equations is solved for the first two variables. This problem is underconstrained, so there are many solutions. For example, [[0, 2], [3, 3]] is a matrix which commutes with B.