I have defined a non-commutative product:

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.


2 Answers 2

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.

  • $\begingroup$ Exactly what I needed, thank you! $\endgroup$
    – Okazaki
    Mar 12, 2018 at 12:52

Here is another method:

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


myProduct[C[k], {k, 10}, Dot]

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


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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