Commutant of set of matrices in python

81 Views Asked by At

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.

1

There are 1 best solutions below

0
Nick ODell On

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.

import sympy
sympy.init_printing(use_unicode=True)


def find_commutant(B):
    n = B.shape[0] * B.shape[1]
    unknowns = [[sympy.Symbol("M{}{}".format(i,j)) for j in range(B.shape[1])] for i in range(B.shape[0])]
    M = sympy.Matrix(unknowns)
    eqn = M@B - B@M
    return eqn, sympy.solve(eqn, dict=True)


B = sympy.Matrix([[1, 2], [3, 4]])
find_commutant(B)

Output:

enter image description here

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.