# How to make $a1**a2** \cdots **an=0$ for a given $n$?

In order to optimize my computation and save memory, I'd like to modify NonCommutativeMultiply so that for a given $n$ one has $a1**a2** \cdots **an=0$ for any $a1, a2, \ldots, an$.

To make it simpler: I need only $n=3,4,5$.

I've already modified NonCommutativeMultiply to be able to work with formal power series in non-commutative variables.

ClearAll[NCM, n]
NCM[(h :NonCommutativeMultiply)[a___, b_Plus,
c___]] := Distribute[h[a, b, c], Plus, h, Plus, NCM[h[##]] &];
NCM[(h : NonCommutativeMultiply)[c1___, b_Times, c2___]] := Most[b]
NCM[h[c1, Last[b], c2]];
NCM[a_ + b_] := NCM[a] + NCM[b];
NCM[a_ b_] := a NCM[b];
NCM[a_] := ExpandAll[a];


I'd like to have, for example, for $n=3$

NCM[x**(x+y**x)**y+x**y]=x**x**y+x**y


Thanks for any suggestions.

• Not clear enough for me.Can you explain further? – Dr. belisarius Jan 27 '15 at 15:42
• I make computations with formal power series in non-commutative variables. In course of calculating the beginning of the resulting power series (= monomials of degree lower than $n$), I try to get rid of monomials of degree not lower than $n$. – 8k14 Jan 27 '15 at 16:15

ClearAll[ncm];
ncm[x__] := 0 /; Length[{x}] > n
ncm[x__] := NonCommutativeMultiply[x] /; Length[{x}] <= n
n = 3;
{ncm[c, d, f], ncm[c, d, f, g]}

(* {c ** d ** f, 0} *)

• @8k14 Please edit your answer and add how you managed to include the distributive property – Dr. belisarius Jan 27 '15 at 17:11
• Sorry, I can't edit my comment. Instead, I expanded my question. – 8k14 Jan 27 '15 at 17:43
ncm[a__, n_List: {5}] :=
NonCommutativeMultiply[a] && Length[List[a]] < n[[1]] ||
0 && Length[List[a]] >= n[[1]]


I defined a function ncm[a,{n}] where a is a sequence of multiplyers and {n} is an optional argument which says starting from how many multiplyers will evaluate function to zero.

for example by default n is set to length of sequence of arguments = 5

ncm[1, 2, 3, 4]
(*1 ** 2 ** 3 ** 4*)

ncm[1, 2, 3, 4, 5]
(*0*)

ncm[1, 2, 3, 4, 5, {6}]
(*1 ** 2 ** 3 ** 4 ** 5*)


ncm[x___, a_ + b_, y___] := ncm[x, a, y] + ncm[x, b, y]
ncm[a__, n_List: {5}] :=
NonCommutativeMultiply[a] && Length[List[a]] < n[[1]] || 0 && Length[List[a]] >= n[[1]]

• Thanks but your method does not work with power series. For example, for $n=3$ I'd like to have $ncm[x∗∗(x+x∗∗y)∗∗y+x∗∗y]=x∗∗x∗∗y+x∗∗y$ (I have already modified ncm to be distributive) – 8k14 Jan 27 '15 at 17:18
• @8k14 it's a problem of function NonCommutativeMultiply rather then of this realization – k_v Jan 27 '15 at 17:53
• Thus what is your suggestion? – 8k14 Jan 27 '15 at 18:50
• @8k14 look at updated defenition of ncm – k_v Jan 27 '15 at 19:11
• I may be wrong but ncm[x ** (x + x ** y) ** y + x ** y] for $n=3$ results in x ** y + x ** (x + x ** y) ** y – 8k14 Jan 27 '15 at 19:42

It suffices to add just one line

NCM[a___] := 0 /; Head[a] == NonCommutativeMultiply && Length[a] > n;


Thanks to @belisarius and @k_v.