问题
I want to overload Times and Plus for matrix multiplication in mathematica, for example, let Times be BitAnd, and Plus be BitOr, then do the matrix multiplication.
Is there anyway to do this in a simple way, without rewriting my own matrix multiplication?
Thanks.
回答1:
The question is what you want to alter - the behavior of Times and Plus, or Dot. Generally, Block trick is often the simplest way. In this case, since Dot does not call high-level Plus or Times, you can do:
mat1 = {{1,2},{3,4}};
mat2= {{5,6},{7,8}};
Block[{Dot = Inner[BitAnd,#1,#2,BitOr]&},
mat1.mat2]
{{3,0},{5,2}}
But note that this is effectively re-implementing the matrix multiplication (using Inner) - there is no other way since Dot is implemented internally and does not use Plus or Times.
来源:https://stackoverflow.com/questions/5252179/how-to-overload-times-and-plus-for-matrix-multiplication-in-mathematica