# Define inverse for the custom operator

I want to define simple matrix algebra (inspired by the following posts: Block Matrix Algebra with Mathematica ; How to define custom operators ).

I assume the funtion MatrixMult[A_,B_] to be the matrix product. I surely can define some properties of this function, like linearity, associativity etc. (see referenced posts). Now I want to solve simple matrix equation

Solve[MatrixMult[A, X]==B,X]


{{X -> InverseFunction[MatrixMult, 2, 2][A, B]}}


Now the question is how can I explicitly define that the inverse of my function is the following:

InverseFunction[MatrixMult, 2, 2][A_, B_] := MatrixMult[Inverse[A], B]


(the last line results in "Tag InverseFunction is Protected" error)

• Either unprotect the symbol or change the name of your predicate. Also look up What are the most common pitfalls ... thread and see the difference between = and := when defining predicates. – Sektor Mar 3 '14 at 10:22
• I get the idea about Unprotect, but still couldn't get it work. After I define the inverse function (with SetDelayed) the Solve function returns {}. What do you mean by "change the name of your predicate"? – bcp Mar 3 '14 at 10:59
• Well, InverseFunction is a built-in predicate, so you can't just use it overwrite it. – Sektor Mar 3 '14 at 11:05
• Both Solve and InverseFunction are meant to be used with scalars only. What you are asking for would not be useful in this specific situation. For symbolic matrix algebra, google for the NCAlgebra package. – Szabolcs Mar 3 '14 at 14:30
• Redefining built-ins is usually not a good idea, as it might break random and unexpected things. (This is a good example: Solve won't even return the InverseFunction any more.) What you could do instead is use a replacement rule that is not tied to InverseFunction and apply it manually, i.e. result /. InverseFunction[MatrixMult, 2, 2][A_, B_] :> MatrixMult[Inverse[A], B]. – Szabolcs Mar 3 '14 at 21:34

One can use UpValues to control the behavior of your MatrixMult function. We also need to teach MatrixMult how to handle Inverse as well:

MatrixMult[a_, MatrixMult[Inverse[a_], b_]] := b

MatrixMult /: InverseFunction[MatrixMult, 2, 2] = MatrixMult[Inverse[#1], #2]&


Notice that I don't try to give a definition for InverseFunction[MatrixMult, 2, 2][a_, b_] because then MatrixMult is buried to deep for UpValues to work. Now, let's try using Solve:

Solve[MatrixMult[A, X] == B, X]


Solve::ifun: Inverse functions are being used by Solve, so some solutions may not be found; use Reduce for complete solution information.

{{X -> MatrixMult[Inverse[A], B]}}

One could avoid the message by explicitly telling Solve that you want to use inverse functions:

Solve[MatrixMult[A, X] == B, X, InverseFunctions->True]


{{X -> MatrixMult[Inverse[A], B]}}

VerifySolutions

The MatrixMult DownValues above is needed because Solve tries to verify its solution. An alternative to giving MatrixMult the above DownValues is to tell Solve not to verify its solution:

Clear[MatrixMult]
MatrixMult /: InverseFunction[MatrixMult, 2, 2] = MatrixMult[Inverse[#1], #2]&;

Solve[MatrixMult[A, X] == B, X, InverseFunctions->True, VerifySolutions->False]


{{X -> MatrixMult[Inverse[A], B]}}