# How to overload the operator "*" as KroneckerProduct [closed]

Can I use the expression below

a * b * c * d

KroneckerProduct[a,b,c,d]

automatically when a,b,c,d are matrix.

The number of elements (a,b,c,d) is arbitrary, such as a*b, a*b*c and a*b*c*d*e*f all can work well.

It seems overriding the operator * in Mathematica is not a good method. And my special applicaon that importing expressions from external file in string format has some others ways.

• Why does it have to be *? Why not use a different symbol instead? Overloading Times[] is usually not the way to go. Commented May 29, 2016 at 16:02
• ⊗ would be the more traditional symbol for the Kronecker product, then. Commented May 29, 2016 at 16:10
• Well, then... Unprotect[NonCommutativeMultiply]; NonCommutativeMultiply = KroneckerProduct; Protect[NonCommutativeMultiply]; Don't try this at home, kids! Commented May 29, 2016 at 16:12
• You've seen the docs, yes? Commented May 29, 2016 at 16:23
• @cmc please add to your question information from comment, that you want to import to Mathematica expressions from a text file, that has some specific format. This is very different problem than overriding built-in function for expressions that are already in Mathematica. Commented May 30, 2016 at 11:53

It was clarified in comments that expressions are supposed to be read into Mathematica from a text file. To achieve what OP wants one can read the file as list of held expressions, perform appropriate replacements, then release hold.

If we have a test.txt file with following contents:

a * b * c * d
a^dag


We can read it in following way:

ReadList["test.txt", HoldComplete@Expression]
% /. {Times -> KroneckerProduct, x_^dag :> ConjugateTranspose[x]} // ReleaseHold
(* {HoldComplete[a b c d], HoldComplete[a^dag]} *)
(* {KroneckerProduct[a, b, c, d], ConjugateTranspose[a]} *)


If we have more control over format of this text file and can accept non-ASCII characters, then we could use operators without built‐in meaning, as already suggested in comments. With unicode.txt file:

a ⊗ b ⊗ c ⊗ d
a^†


we could do this:

ReadList["unicode.txt", HoldComplete@Expression]
% /. {
CircleTimes -> KroneckerProduct,
x_^\[Dagger] :> ConjugateTranspose[x]
} // ReleaseHold
(* {a ⊗ b ⊗ c ⊗ d, a^†} *)
(* {KroneckerProduct[a, b, c, d], ConjugateTranspose[a]} *)

• That doc page link is so useful. Thank you jkuczm! Commented Sep 25, 2016 at 1:34