I have 2 rules of recursive relation of the derivative, I want to use it several times get the higher derivative on [\Theta] of P[\[Theta],n,m] only expressed by P[\[Theta], n, m] and P[\[Theta], n-1, m]. (Yes, P is Associated Legendre polynomials here)

This is my rule (Sorry, I don't know how to wrap this long line of code):

rule1 = D[P[\[Theta], n, m], {\[Theta], i}] :> D[D[P[\[Theta], n, m], {\[Theta], i - 1}], {\[Theta], 1}];(*i>=2*)
rule2 = D[P[\[Theta], n, m], {\[Theta], 1}] :> 1/Sin[\[Theta]] (n Cos[\[Theta]]*P[\[Theta], n, m] - (n + m) P[\[Theta], n - 1, m]);
rule = {rule1, rule2};

Then I use this first code and it return the needed result.

D[P[\[Theta], n, m], {\[Theta], 1}] //. rule

However, the higher derivative on [\Theta] canot work. How to the let it works?

D[P[\[Theta], n, m], {\[Theta], 2}] //. rule (* return with calcuation *)

We can make rule 1 execute first, consider rule 2 during the compute, and repeat the process to get what I want. But how to implement it in code?

Another try (Try these two rules in each calculation step) :

r1 = D[P[\[Theta], n_, m], {\[Theta], 1}] :> (1/Sin[\[Theta]] (n Cos[\[Theta]]*P[\[Theta], n, m] - (n + m) P[\[Theta], n - 1, m]));
r2 = D[P[\[Theta], n_, m], {\[Theta], i_}] :> (D[D[P[\[Theta], n, m], {\[Theta], i - 1}] /. r1, \[Theta] ] /. r1);(*i>=2*)
D[P[\[Theta], n, m], {\[Theta], 2}] /. r2 (* Out contains P[\[Theta], -2 + n, m], This is not what you want to see *)
D[P[\[Theta], n, m], {\[Theta], 3}] /. r2 (* Nothing changed *)

1 Answer 1


Your problem is that MMA simplifies the exprerssion D[D[P[\[Theta], n, m], {\[Theta], i - 1}], {\[Theta], 1}]at once to D[P[\[Theta], n, m], {\[Theta], i}]. And you are where you started. To prevent this, we may formulate it in a recursive way. E.g.:

p[1] = 1/
   Sin[\[Theta]] (n Cos[\[Theta]]*
     P[\[Theta], n, m] - (n + m) P[\[Theta], n - 1, m])
p[i_] := D[p[i - 1], \[Theta]];

Now with this we get e.g.:


enter image description here



enter image description here

A new editon base on the previous method

f[1,n_,m_,\[Theta]_]:=1/Sin[\[Theta]] (n Cos[\[Theta]]*f[0,n,m,\ 
 [Theta]]-(n+m)f[0,n-1,m,\[Theta]]);(*Eq.(8) at 
    f[0,-2+n,m,\[Theta]]:>((-1+2 n) Cos[\[Theta]] f[0,-1+n,m,\[Theta]]+ 


    Table[f[j, n, m, \[Theta]], {j, 0, 4}] // TableForm

The out put is: enter image description here The above code works, but how to simplify them (Are there redundant items in the replacement rule??)?

  • $\begingroup$ Thank you @Daniel Huber. I modeyied your good answer to further simplify the results. $\endgroup$
    – He Tang
    Feb 22, 2023 at 5:23

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.