I am trying to define a commutator. The way I am working this out is the following. I have a list with integers. I want to put the biggest on the right commuting them. For example, if I have a list like {1,1,1,-4,-3}, then I want to commute the three 1's to the right of the negative number.
I have a recursive function that does the job but I have issues with it since I overcharged this function's definition and the way Mathematica interprets it is not totally clear to me. Therefore, I want to go with an If,else condition. The first recursive function I have is the following
C3[a___, h2_, {n1___, m_, n_, n4___}, b___] := C3[a, h2, {n1, n, m, n4}, b] + (m - n) C3[a, h2, {n1, n + m, n4}, b] +
If[m + n == 0, c (m^3 - m)/12 C3[a, h2, {n1, n4}, b], 0] /; m > n;
This function works indeed as expected, and we have
In[6]:= C3[1, {}, 1, {1, 1, 1, -4, -3}, 1, {}] // FullSimplify
Out[6]= 48 (3 C3[1, {}, 1, {-4}, 1, {}] + 5 (C3[1, {}, 1, {-3, -1}, 1, {}] + C3[1, {}, 1, {-2, -2}, 1, {}]))
Now I don't understand why an equivalent thing with an If does not work. If I use the function
C3p[a___, h2_, {n1___, m_, n_, n4___}, b___] := If[m > n,C3p[a, h2, {n1, n, m, n4}, b] + (m - n) C3p[a, h2, {n1, n + m, n4}, b] + If[m + n == 0, c (m^3 - m)/12 C3p[a, h2, {n1, n4}, b], 0], 0]
This one does not enter in the condition and we obtain
In[8]:= C3p[1, {}, 1, {1, 1, 1, -4, -3}, 1, {}]
Out[9]= 0
This function does not even produce an output and never enters the condition. I am also interested in the rapidity of execution since in the end, this function may be called a huge number of time. Any help would be much appreciated.
In full generality, what I am trying to achive is the following. In a recursive manner, I have this six functions, but I am almost sure that Mathematica is confused since their call is somewhat similar. C3 is a function that take six arguments of the form C3[h1_,{n1___},h2_,{n2___},h3_,{n3___}],
three integers and three lists of integers. My code yields the correct result but become incredibly slow. Basically, I am searching a clever way to write
C3[a___, h3_, {n___, m_}] := 0 /; m > 0;
C3[a___, h2_, {n___, m_}, b___] := 0 /; m > 0;
C3[h_, {n___, m_}, a___] := 0 /; m > 0;
C3[a___, h3_, {n1___, m_, n_, n4___}] := C3[a, h3, {n1, n, m, n4}] + (m - n) C3[a, h3, {n1, n + m, n4}] +
If[m + n == 0, 24 (m^3 - m)/12 C3[a, h3, {n1, n4}], 0] /; m > n;
C3[a___, h2_, {n1___, m_, n_, n4___}, b___] :=
C3[a, h2, {n1, n, m, n4}, b] + (m - n) C3[a, h2, {n1, n + m, n4}, b] +
If[m + n == 0, c (m^3 - m)/12 C3[a, h2, {n1, n4}, b], 0] /; m > n;
C3[h_, {n1___, m_, n_, n4___}, a___] := C3[h, {n1, n, m, n4}, a] + (m - n) C3[h, {n1, n + m, n4}, a] +
If[m + n == 0, 24 (m^3 - m)/12 C3[h, {n1, n4}, a], 0] /; m > n;
so that mathematica is not confused and know exactly how to commute the thing until the desired order is reached.
If
version doesn't work, apart from havingC3
on the right-hand side rather thanC3p
(which is required for it to be recursive), sit that it evaluates (for your list)m == 1
andn==1
first (i.e. the first elements in the list), and since1 >1
is False, it spits out 0 and doesn't call itself. Unfortunately I don't have time to test further. $\endgroup$If
? This is because in replacingCondition
withIf
as you did, the pattern on the left hand side ofSetDelayed
is more general -- it's possible that the pattern will match whenm>n
evaluates to neither true nor false, unlike when you usedCondition
. $\endgroup$