# Making ^ work as MatrixPower for matrices

I'd like my expressions like A^d get interpreted as MatrixPower[A, d] for matrix arguments A, any suggestions how to do this?

• Power (^) is a built-in symbol. My suggestion: Don't mess with built-in symbols. You will be happier in the long run if you don't. – Henrik Schumacher Sep 23 '19 at 0:53
• Yes, I'm aware this may break some things – Yaroslav Bulatov Sep 23 '19 at 1:15

One way to do it is to use Notation package.

Needs["Notation"]

distinguishMatrixPowerAndPower[A_, d_] :=
If[SquareMatrixQ[A], MatrixPower[A, d], Power[A, d]]

Notation[A_^d_\[DoubleLongRightArrow]distinguishMatrixPowerAndPower[A_,d_]] (* this line is inserted using Notation Palette. *)


This is the result:

In[]:= {{1, 1}, {1, 2}}^2

Out[]= {{2, 3}, {3, 5}}

In[]:= 5^2

Out[]= 25
`