# How to plot Ramanujan's continued fraction in Mathematica?

I want to plot Ramanujan's continued fraction: $$R(q)=\cfrac{q^{1/5}}{1+\cfrac{q}{1+\cfrac{q^2}{1+\ddots}}}$$ but I do not know how to define this function in Mathematica. How do I define and plot $R(q)$?

• Formula 9 from here gives a closed form for the Rogers-Ramanujan CF in terms of the built-in Mathematica function QPochhammer[]. See Trott's article as well. – J. M. is away Jun 20 '15 at 18:52
• (P.S. My current Gravatar is a domain-colored plot of the RRCF over the unit disk.) – J. M. is away Jun 20 '15 at 19:05
• A complete notebook with functions and plots is available here – ciao Jun 21 '15 at 4:59

...and now, the answer I promised to write.

As I noted in the comments, there is in fact an explicit formula for the RRCF in terms of built-in Mathematica functions, thanks to the deep theory of modular forms:

$$\mathcal{R}(q)=\sqrt[5]{q}\frac{\left(q;q^5\right)_\infty \left(q^4;q^5\right)_\infty}{\left(q^2;q^5\right)_\infty \left(q^3;q^5\right)_\infty}$$

Here, $\left(a;q\right)_\infty$ is the $q$-Pochhammer symbol,

$$\left(a;q\right)_\infty=\prod_{j=0}^\infty \left(1-a q^j\right)$$

which is built-in as QPochhammer[a, q]. Thus, here is the definition of the RRCF in Mathematica:

RogersRamanujanR[q_] :=
q^(1/5) (QPochhammer[q, q^5] QPochhammer[q^4, q^5])/
(QPochhammer[q^2, q^5] QPochhammer[q^3, q^5])


The other answers have already shown you a plot of the RRCF on the real line, but it looks so boring and flat in that domain. The complexities of this function become apparent when you consider it as a function of complex $q$. In particular, the unit circle is a natural boundary of analyticity for the RRCF, and thus it does not make sense to evaluate it for $|q| > 1$ (though the truncations of the continued fraction exhibit interesting properties for $|q| > 1$; see Trott's article for more details).

Here, then, are density plots of the real and imaginary plots of the RRCF over the unit disk:

With[{ε = 1*^-4}, GraphicsRow[
{DensityPlot[Re[RogersRamanujanR[x + I y]],
{x, -1 + ε, 1 - ε}, {y, -1 + ε, 1 - ε},
ColorFunction -> "ThermometerColors", MaxRecursion -> 0,
PlotPoints -> 135, RegionFunction -> (Norm[{#1, #2}] < 1 &)],
DensityPlot[Im[RogersRamanujanR[x + I y]],
{x, -1 + ε, 1 - ε}, {y, -1 + ε, 1 - ε},
ColorFunction -> "ThermometerColors", MaxRecursion -> 0,
PlotPoints -> 135, RegionFunction -> (Norm[{#1, #2}] < 1 &)]}]]


Noteworthy features include the fence of singularities along the unit circle, and the branch cut along the negative real axis (inherited from the $\sqrt[5]{q}$ factor).

In his answer, m_goldberg shows how to use ContinuedFractionK[] to evaluate convergents of the RRCF. This approach, however, is unwieldy to use for numerical evaluation, since in this case you do not know in advance how many terms of the CF are needed to reach convergence. One can instead use specially-adapted forward evaluation algorithms, which provide a running check on the convergence. Here is a routine for evaluating the RRCF, which uses Steed's algorithm:

rrcf[q_?InexactNumberQ, n_Integer: 500] :=
Module[{eps = 10^(-Precision[q] - 1), d, f, h, k, z},
f = h = d = z = k = 1;
While[
z *= q; d = 1/(1 + z d);
h *= d - 1; f += h; k++;
k < n && Abs[h] > Abs[f] eps];
q^(1/5) f]


Some quick tests show that rrcf[] is slightly faster than RogersRamanujanR[] for values not too near the singularities of the RRCF.

The built-in function ContinuedFractionK can be used to generate an approximation to R[q] good enough for plotting purposes.

 r[q_, n_] = q^(1/5) ContinuedFractionK[q^i, 1, {i, 0, n}];
r[q, 4]


A very reasonable plot can be made with

Plot[r[q, 20], {q, 0, 3}]


• The plot is suspect for $q>1$; as mentioned in the MathWorld article I linked to, the CF's even and odd parts converge to different limits (i.e. the CF is divergent) outside the unit disk. – J. M. is away Jun 21 '15 at 0:46
• So, why not just use the expression in terms of QPochhammer[] instead? :) – J. M. is away Jun 21 '15 at 0:47
• @J.M. I had two goals in mind when I wrote this answer. 1) To give an answer that didn't require any additional math knowlege beyond a little familiarity with continued fractions. 2) To raise awareness of ContinuedFractionK in the community. It would be good if someone wrote an answer showing the application of QPochhammer to this question, but not me. I don't have the math chops. How about you? – m_goldberg Jun 21 '15 at 2:17
• I do not have access to a computer with Mathematica at the moment; thus, if nobody writes an answer involving QPochhammer[] up until I am able, I shall write an answer. Thanks for the explanation! (I have, of course, already upvoted.) – J. M. is away Jun 21 '15 at 2:40
n = 5;
(q^(1/5)/Fold[HoldForm @ Evaluate[1 + q^#2/#1] &, 1, Reverse @ Range @ n])

f[n_] := q^(1/5) Module[{i = 1}, Nest[(q^(n + 1 - i++))/(1 + #) &, 0, n + 1]]