How to overload the operator “*” as KroneckerProduct [closed]

Can I use the expression below

a * b * c * d

for short to instead


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.



1 Answer


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

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

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!
– matheorem
Sep 25 at 1:34