how to overload Times and Plus for matrix multiplication in mathematica

时光毁灭记忆、已成空白 提交于 2019-12-10 18:29:54

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!