Message Boards Message Boards

1 Reply
2 Total Likes
View groups...
Share this post:

Long, nested matrix product with coefficients

Posted 10 years ago

Hello, The following is an example of what I need:

given four numbers: a,b,c,d, and four matrices A,B,C,D, I would like the following input:


to produce the following output:

a*c A.C + a*d A.D + b*c B.C + b*d B.D

instead I can only get it like this (using the 'Distribute' function):

(a A).(c C) + (a A).(d D) + (b B).(c C) + (b B).(d D)

Any ideas? Thank you very much in advance!

POSTED BY: Bryan Zaldivar
Posted 10 years ago

If you can accept needing to use your own function then

In[1]:= mydist[expr_] := Distribute[expr] //. Dot[Times[a_, A_], Times[c_, C_]] -> a*c*A.C;
mydist[(a*A + b*B).(c*C + d*D)]

Out[2]= a c A.C + a d A.D + b c B.C + b d B.D

Please test that carefully before you depend on it.

UnProtecting and redefining Distribute to do this automatically might be feasible, but only if you want to risk that.

POSTED BY: Bill Simpson
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract