# How to make valid expressions with arbitrary number of named Blanks?

I want to make the rule for a generalized distributivity. In what I'm doing, I'll have expressions such as:

$$(a_1 + ...+ a_n)**a_{n+1}$$

And I want to transform them to:

$$(a_1 ** a_{n+1} + ...+ a_n ** a_{n+1})$$

I know how to do in case I have a foreseeable number of variables. I could use:

(a + b) ** c /. {(x_ + y_) ** z_ -> (x ** z + y ** z)}


As the number $$n$$ is arbitrary, I thought I could do something in the lines of:

t=2;
s=Sum[a[i]_,{i,t}]**(a[t+1]_)
ss=Sum[a[i]**a[t+1],{i,t}]

(a+b)**c/.{s->ss}


But this does not work. I tried to probe why this doesn't work but found nothing relevant. Can you help me?

Not sure exactly how you want to use this, but the built in Distribute can do the work for you:

expression = (a + b + c) ** z;
Distribute[expression]
(* a ** z + b ** z + c ** z *)


You can always use ReplaceRepeated (//.):

expr = (a + b + c + d) ** z;
expr //. {(x_ + y_) ** z_ -> (x ** z + y ** z)}
(* a ** z + b ** z + c ** z + d ** z *)


which applies the rule repeatedly until the expression doesn't change anymore.