# Creating a function that performs a "DotExpand"

When you make a Dot[a,(a+b+c)] the output will be:

a.(a + b + c)


I want to make a function that expands the dot product, for the previous example would be:

a^2 + a.b + a.c


I don't have much knowledge about pure function. I start to create the function as following:

DotExpand[Dot[l_, r_]] := Map[Times[#, #] &, {l, r}]

In[36]:= DotExpand[(a + b + c).a]

Out[36]= {(a + b + c)^2, a^2}


As you see, is not working. What I have do modify to works?

• seeTensorExpand?
– kglr
Jan 31, 2019 at 15:02
• Ok, but if I want to create my own TensorExpand, some like a DotExpand? Jan 31, 2019 at 15:59
• Replace the first line by Apply[Times[#1, #2] &, {l, r}]; Jan 31, 2019 at 16:08
• The output still the same @WenChern if I use Dot: a (a + b + c) Jan 31, 2019 at 16:52

Not sure, are you looking for something using Distribute?

DotExpand[u_,v_]:=Distribute[Dot[u,v]]


For example,

DotExpand[a,a+b+c]


a.a + a.b + a.c

DotExpand[a+b,b+c]


a.b + a.c + b.b + b.c