# Matrix identities

I've just started to work in Mathematica with matrices. Until now I've just managed to perform operation between particular matrix. I wonder if it is possible to specify a class of matrixes, for instance symmetric positive definitive in order to write general operations between them and simplify the expression using well-known identity in Mathematica.

For instance is it possibile to answer this question using Mathematica?

Mathematica has built-ins SymmetricMatrixQ and PositiveDefiniteMatrixQ which can be combined into a composite test as in testQ=SymmetricMatrixQ[#]&&PositiveDefiniteMatrixQ[#]&; this can be used to restrict the application of arbitrary operators to matrices that are symmetric and positive definite eg operator[mat_?testQ]:=operations will evaluate only if its input is indeed symmetric and positive definite otherwise it will return unevaluated.

I am not aware of a built-in to check for a diagonal matrix but it is not difficult to make one (see end notes).

As far as the linked question is concerned, one way to code the proposed solution would be the following:

invert[dMAt_?diagonalMatrixQ, aMat_?testQ] := With[{evals = Eigenvalues[aMat], evecs = Eigenvectors[aMat]},
Transpose[evecs].Inverse[DiagonalMatrix[evals] + dMAt].evecs
]


Here, invert implements the solution proposed in the link (see question).

The following code block tests invert:

(* generate 3x3 matrices *)
With[{dim = 3, seed = 7884458},
(* for reproducibility purposes *)
BlockRandom[
(* generate random numbers *)
With[{rands = RandomReal[{-100, 100}, {dim, dim}], lambda = RandomReal[{-10, 10}]},
(* construct matrices A and D *)
With[{sym = Transpose[rands].rands, dMat = lambda IdentityMatrix[dim]},
(* test that A is positive-definite and symmetric and that invert performs as expectd *)
{PositiveDefiniteMatrixQ[sym], SymmetricMatrixQ[sym], Chop[Inverse[dMat + sym] - invert[dMat, sym]]}
]
]
, RandomSeeding -> seed
]
]

{True, True, {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}}


## notes

Define a function to be used along with a pattern test to identify if a matrix is diagonal:

diagonalMatrixQ::usage = "diagonalMatrixQ[mat] returns True if mat is a diagonal matrix."

diagonalMatrixQ[m_?SquareMatrixQ] := SameQ[DiagonalMatrix[Diagonal[m]], m]


Testing diagonalMatrixQ

(* define the dimensions to use - here we're using a non-square matrix*)
With[{rows = 4, cols = 2, seed = 1478554},
(* make reproducible *)
BlockRandom[
(* generate random non-square matrix *)
With[{rands = RandomReal[{-100, 100}, {rows, cols}]},
(* construct a diagonal matrix *)
With[{diag = DiagonalMatrix[Diagonal[rands]]},
(* test a diagonal and a rectangular matrix *)
{diagonalMatrixQ[diag], diagonalMatrixQ[rands]}
]
]
, RandomSeeding -> seed
]
]

{True, False}


update: The purpose of the update is to respond to the issues raised in the comments. For Mathematica to simplify an expression "using all the relevant identities it is aware of" it must be made aware of those identities in the first place.

The predominant way to achieve this is to provide rules attached to symbols.

If by class of matrices you mean something like a type (used in strongly typed languages) or a class used in oop then you probably can't do it without some effort.

In any case, you would have to define more precisely both what are the relevant objects (matrices and their respective class) and the relevant operations that is required to simplify over.

To use the example provided in the link, a possible solution along the lines of the spirit of the question would be

Inverse[specialMatrix[A]] ^:= Transpose[Eigenvectors[specialMatrix[A]]].Inverse[diagonalMatrix[specialMatrix[A]] + DiagonalMatrix[Eigenvalues[specialMatrix[A]]]].Eigenvectors[specialMatrix[A]]


where specialMatrix denotes the discrete class of matrices to which A is an instance of and diagonalMatrix somehow evaluates to the equivalent of $D=λI$.

The purpose of this demonstration is to show that for Mathematica to use all the relevant rules at its disposal to reach at a result, it will have to be instructed on how to deal with the particulars of those identities eg it would have to know how to implement diagonalMatrix, in this case.

• Thanks @user42582 for your detailed answer! I apologised if my question was not clear, but I would like to simplify general expressions of class of matrices using well-known relationship like the one in the link. I would like that Mathematica simplifies a general expression trying all the identities that Mathematica knows rather than me to check a particular identity with particular matrices values. – Bruno Feb 26 '18 at 11:08
• My pleasure! please check the update for a response – user42582 Feb 26 '18 at 13:43
• I would like to point out that SymmetrizedArray can do a lot of heavy lifting for you when it comes to defining matrix symmetries. – Sjoerd Smit Feb 26 '18 at 14:01
• @SjoerdSmit thank you very much for the suggestion; Indeed, I wasn't aware of that option – user42582 Feb 26 '18 at 14:05
• Thanks again @user42582! I am new to Mathematica and my naive hope was to work with matrix in Mathematica easily as with constant. For instance, specifying some restriction with Assumptions (e.g. PSD) and let then Mathematica to simplify some expressions using identities that I do not have to know. However, it seems that it is not possible. Anyway, I will study more about working with matrices in Mathematica in order to be able to work effectively with special class of matrices as you kindly suggested. – Bruno Feb 26 '18 at 14:52