# Defining a recursive product function

I have defined a non-commutative product:

ClearAll[M];
M[a___, b_ + c_, d___] := M[a, b, d] + M[a, c, d];
M[a___, 1, b___] := M[a, b];
M[a___, b_. c_, d___] :=
c M[a, b, d] /; FreeQ[c, R | t];
SetAttributes[M, Flat];


I would like to define an analogue of the usual Product function, in which I would input

NewProduct[Subscript[t, i], {i, 1, n}]


which outputs, for say n = 3,

M[Subscript[t, 1], Subscript[t, 2], Subscript[t, 3]]


Any advice would be much appreciated.

NewProduct[x_, iter_] := M @@ Table[x, iter];
NewProduct[Subscript[t, i], {i, 1, 3}]


M[Subscript[t, 1], Subscript[t, 2], Subscript[t, 3]]

Let Table handle the iteration and then replace the List it gives you with the head you want (in this case, M).

Note that this won't handle infinite iteration well. If you need that, you'll probably want to investigate the properties of your M function further to see if you can reduce it by hand or to already built-in functions.

• Exactly what I needed, thank you! – Okazaki Mar 12 '18 at 12:52

Here is another method:

myProduct[expr_, {k_, kmin_: 1, kmax_}, times_: Times] :=
Array[Function[k, expr], kmax - kmin + 1, kmin, times]


Example:

myProduct[C[k], {k, 10}, Dot]
C[1].C[2].C[3].C[4].C[5].C[6].C[7].C[8].C[9].C[10]


In your case, use your custom operation M as the third argument for myProduct[].