开发者

how to overload Times and Plus for matrix multiplication in mathematica

开发者 https://www.devze.com 2023-02-15 17:28 出处:网络
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.

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?

Th开发者_如何转开发anks.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号