2
$\begingroup$

Continuing the topic of my previous questions (link1 and link2), let me ask one more question.

I have problems with the following code:

ClearAll["Global`*"]
SetAttributes[R, HoldAll]
SetAttributes[P, HoldAll]

Q[r_, f_, s0_] := Q[r, f, s0] = F[r, s0] D[f[s0], s0]

R[r_, f_[q_, g_, s0_], s0_] := F[r, s0] (r R[r - 1, f[q, g, s0], s0] + P[r - 1, f[q, g, s0], s0])
P[r_, f_[q_, g_, s0_], s0_] := F[r, s0] (r R[r - 1, f[q, g, s0], s0] - P[r - 1, f[q, g, s0], s0])
R[1, f_[q_, g_, s0_], s0_] := D[f[q, g, s0], s0]
P[1, f_[q_, g_, s0_], s0_] := 0

where functions R and P are defined via recursive relationship.

When I'm calling the following command:

R[1, Q[1, f, s0], s0]

I'm obtaining correct answer. However, when I'm calling the following:

R[2, Q[1, f, s0], s0]

it gives me an error of $RecursionLimit.

Could you help me please with fixing this problem? I will appreciate any help!

$\endgroup$

1 Answer 1

3
$\begingroup$

You need to provide a condition to stop the recursion. I removed your R[1,...] and P[1,...] lines and used an If in the definition of R and P:

Q[r_, f_, s0_] := Q[r, f, s0] = F[r, s0] D[f[s0], s0]

R[r_, f_[q_, g_, s0_], s0_] := 
 If[r > 1, 
  F[r, s0] (r R[r - 1, f[q, g, s0], s0] + P[r - 1, f[q, g, s0], s0]), D[f[q, g, s0], s0]]
P[r_, f_[q_, g_, s0_], s0_] := 
 If[r > 1, 
  F[r, s0] (r R[r - 1, f[q, g, s0], s0] - P[r - 1, f[q, g, s0], s0]), 0]
$\endgroup$
1
  • $\begingroup$ It works! Thank you very much for your help! $\endgroup$
    – Svetlana
    Commented May 31, 2020 at 19:34

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.