Recently I have been doing some algebra of partitioned matrices, where product like below is common:
where $A_{11}, A_{12}, X, Y$ are all partitioned matrix blocks thus all are matrices in essence.
One of the Important features of product between matrices and/or vectors is that: it is not commutative, namely $ AB \neq BA $, so I have to remove the attributes of "Orderless" from Times[] to make sure the system does not change the order of product terms in result as it would often does.
To do this product in Mathematica, first I cleared the attributes:
ClearAttributes[Times, Orderless];
Then I tried to do this product several times, in order to simulate the long computation process I might need to do on this kind of algebra in the future:
({
{Subscript[A, 11], 0},
{0, Subscript[A, 22]}
}) . ({
{0, Y},
{X, 0}
}) // MatrixForm
However, it was to my surprise to find out that the result is unstable. For the same product, sometimes it will give out the result as:
While sometimes the result given is:
But only the second one is true, where in the relevant products $A_{11}, A_{12}$ go first and $X,Y$ go last.
The typical situation where the order/sequence of the product changes:
(1) After you do several other calculations and go back to do the same product;
(2) After you take a break for several minutes and come back to do the same product;
(3) When the name of a multiplier is too long, the system tends to adjust the order/sequence of the product to let the multiplier with long name goes last. For example, for matrix X, suppose the correct product is Transpose[X] X
or Inverse[X] X
but the system would tend to give it as X Transpose[X]
or X Inverse[X]
So I was wondering: why the result would be so unstable even after the attributes of Orderless already being cleared? Is there anyway to clear the attributes stably and reliably so that when I do the algebra of partitioned matrices it will always gives the product in result in correct order/sequence?
.
is notTimes
butDot
. Secondly, I wouldn't mess with the properties of such a fundamental operator. I suggest defining a new operator or using something likeNonCommutativeMultiply
, for which you can find several examples on StackExchange. $\endgroup$Dot
to use instead—consider usingInner
, e.g.a = {{Indexed[A,{1,1}], 0}, {0, Indexed[A, {2,2}]}}; b = {{0,X},{Y,0}}; Inner[NonCommutativeMultiply, a, b, Plus]
. Of course, you'd need to defineNonCommutativeMultiply
appropriately first, since it's nearly completely undefined by default, which is not a trivial task... $\endgroup$Plus
,Times
andPower
ranks high on the list of Things Not To Do. There is past discussion about this here. Generally one instead usesNonCommutativeMultiply
, defining rules as needed. $\endgroup$