0
$\begingroup$

I have a simple problem, I would like to get the following:

In=  Distribute[a.(b+c)*d]
Out= a.b*d+a.c*d

Meaning that I would like the dot product and the multiplication to be distributed over the sum. I want it to work for more complicated patterns too, such as a.(b+c+d+e).f*x.(y+z). How can that be done? Maybe a function can be defined, to replace mathematica's Dot, that somehow is distributive over all inputs? I tried using this answer but could only make a function distributive over one input.

$\endgroup$
0
$\begingroup$

Rewriting problems can often be directly solved with replacements. Since there are a relatively small number of replacement rules involved, that would be my go to. I use ReplaceRepeated (//.) in dist below to implement these replacements, as there may be several layers of replacements to unravel as the distribution continues:

dist[expr_] := 
  expr //. {Dot[x_, y_Plus] :> Map[x.# &, y], 
    Dot[x_Plus, y_] :> Map[#.y &, x], 
    Times[x_, y_Plus] :> Map[x*# &, y], 
    Times[x_Plus, y_] :> Map[#*y &, x]};

This preserves the ordering of the arguments, but Mathematica always assumes that Times is commutative, so it will still rearrange the outputs some as a result:

dist[a.(b + c)*d]

d a.b + d a.c

dist[a.(b + c + d + e).f*x.(y + z)]

x.y a.b.f + x.z a.b.f + x.y a.c.f + x.z a.c.f + x.y a.d.f + x.z a.d.f + x.y a.e.f + x.z a.e.f

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.