Friday, 9 August 2024

Matrix shading in Mathematica

Not exactly plotting, but at least data visualisation ... 

I have a matrix which looks as follows, generated here in Mathematica from a 4x4 identity matrix and a tensor product of Pauli matrices:


H := 5.907  IdentityMatrix[4] + .2183  KroneckerProduct[
    PauliMatrix[3], IdentityMatrix[2]] -
  6.125  KroneckerProduct[IdentityMatrix[2], PauliMatrix[3]] -
  2.143  (KroneckerProduct[PauliMatrix[1], PauliMatrix[1]] +
     KroneckerProduct[PauliMatrix[2], PauliMatrix[2]]);
 

H // MatrixForm

what I'd like to do is highlight the block diagonal bits - i.e. elements (1,1), (4,4) and the 2x2 block in the middle. From some searching of the internet (particularly this useful page) and the Mathematica documentation, I see that the Background attribute can be applied to a Grid view of the matrix:

bg = {None,
   None, {{1, 1} -> LightGreen, {4, 4} ->
     LightGreen, {{2, 3}, {2, 3}} -> LightGreen}};

Grid[H, Background -> bg]

which has indeed highlighted the elements that I wanted it to, but at the expense of no longer using the brackets of MatrixForm.  I you ask for this to be printed in MatrixForm, the background colouring disappears.  As far as I have been able to work out, the Background attribute cannot be combined with MatrixForm.  Oh well, the blocking is still as I wanted.

The syntax for Background is given in the hyperlink above.

No comments:

Post a Comment