# Commutator of block matrices

How to implement a commutator of matrices composed of operators?

## Background:

Let $\hat A_{ij}$, $\hat B_{kl}$ be some sets of some operators. $\hat I$ is the identity operator. Their commutators are defined as usual $[\hat A_{ij},\hat B_{kl}]=\hat A_{ij}\hat B_{kl}-\hat B_{kl}\hat A_{ij}$. Out of these operators two matrices $mH$ and $mQ$ (both can be sparse) are constructed. I would like to compute a commutator $mC=[mH,mQ]$ and express (where possible) the elements of $mC$ in terms of commutators $[\hat A_{ij},\hat B_{kl}]$.

## Example:

Define $mH$ and $mQ$ as $2\times 2$ matrices:

Clear[mH, mQ]
mH = {{h, v},{v, h + i}};
mQ = {{q0, p},{p, q1}};


Define $f$ as non-commutative but distributive operation; $i$ is the identity operator

f[a_ + b_, c_] := f[a, c] + f[b, c]
f[c_, a_ + b_] := f[c, a] + f[c, b]
f[i, a_] := a;
f[a_, i] := a;


Dot only works for the normal multiplication operation, therefore, compute the commutator using Inner

Clear[mC]
mC[0] = Inner[f, mH, mQ, Plus] - Inner[f, mQ, mH, Plus];


Express matrix elements in terms of commutators (denoted as c). This part is not working properly...

mC[1] = mC[0] /. {f[a_, b_] - f[b_, a_] -> c[a, b]};
MatrixForm[mC[1]]


$\left( \begin{array}{cc} c(h,\text{q0})-f(p,v)+f(v,p) & -p+c(h,p)-f(\text{q0},v)+f(v,\text{q1}) \\ p+c(h,p)-f(\text{q1},v)+f(v,\text{q0}) & c(h,\text{q1})-f(p,v)+f(v,p) \\ \end{array} \right)$

## Problem:

We can see that some diagonal entries can still be expressed in terms of commutators. The FullForm reveals the reason. However, I do not know a nice way to solve this problem. Your help is very much appreciated!

mC[1] = mC[0] //. {f[a_, b_] - f[b_, a_] :> c[a, b]};

$\left( \begin{array}{cc} c(h,\text{q0})+c(v,p) & c(h,p)-f(\text{q0},v)+f(v,\text{q1})-p \\ c(h,p)+f(v,\text{q0})-f(\text{q1},v)+p & c(h,\text{q1})+c(v,p) \\ \end{array} \right)$