6 added 25 characters in body edited Aug 26 '17 at 10:42 J. M. will be back soon♦ 101k1010 gold badges317317 silver badges477477 bronze badges As an exercise ofin writing a good Compile function, I want to do the simple task of coding a routine that outputs the real part of the dilogarithm function reLi2[z], given a complex number z as input. I would like some feedback in my code below that does the job. I am following the strategy outlined in Celestial Mech. Dynam. Astron. 62 (1): 93–98. TheThe idea is to divide the complex $$z$$-plane into four regions, as I show in the figure below, and to implement a different approximation in each region. In region 1, approximate the dilogarithm by the defining sum: $$\text{Li}_2(z) \approx \sum_{k=1}^\infty x^k/k^2$$$$\operatorname{Li}_2(z) \approx \sum_{k=1}^\infty x^k/k^2$$. Here is the code: realRegion1 = Compile[{{x, _Real}}, Sum[x^k/k^2, {k, 1., 23}]];  In region 2, approximate the dilogarithm by evaluating the integral $$\text{Li}_2(z) \approx -\int_0^1 \frac{\ln(1-z t)}{t}\,dt$$$$\operatorname{Li}_2(z) \approx -\int_0^1 \frac{\ln(1-z t)}{t}\,dt$$ by Gaussian quadrature (9 divisions): With[{div = 9}, With[{x = Sort[formalX /. Solve[LegendreP[div, formalX] == 0, formalX] // N]}, With[{ y = Chop[x/2 + 1/2], w = Table[2/((1 - x[[i]]^2)*Derivative[0, 1][LegendreP][div, x[[i]]]^2), {i, 1, div}]}, With[{ realRegion2expr = -1/4.*Sum[ Chop[w[[i]]] Log[1 - 2 y[[i]] var + y[[i]]^2 var^2]/y[[i]], {i, 1, div}]}, realRegion2 = Compile[{{var, _Real}}, realRegion2expr]] ] ] ];  In region 3, apply the dilogarithm identity $$\text{Li}_2(z) = -\underbrace{\text{Li}_2(1-z)}_\text{region I} - \ln(z)\ln(1-z)+\pi^2/6$$$$\operatorname{Li}_2(z) = -\underbrace{\operatorname{Li}_2(1-z)}_\text{region I} - \ln(z)\ln(1-z)+\pi^2/6$$, where the dilogarithm in the RHS is to be evaluated in region I. realRegion3 = Compile[{{x, _Real}}, If[x == 1, Pi^2/6., -realRegion1[1 - x] - Log[x] Log[1 - x] + Pi^2/6.], CompilationOptions -> {"InlineExternalDefinitions" -> True}];  In region 4, apply the dilogarithm identity $$\text{Li}_2(z) = -\underbrace{\text{Li}_2(1/z)}_\text{region I,II,III} - \frac{1}{2}\ln^2(-z)-\pi^2/6$$$$\operatorname{Li}_2(z) = -\underbrace{\operatorname{Li}_2(1/z)}_\text{region I,II,III} - \frac{1}{2}\ln^2(-z)-\pi^2/6$$, where the dilogarithm in the RHS is to be appropriately evaluated in region I, II or III depending on the value of $$1/z$$. So first, I need to put together the functions realRegion1, realRegion2 and realRegion3 so that it correctly evaluates on the real line segment $$-1 \leq z \leq +1$$ appropriately: realSegment = Compile[{{x, _Real}}, If[-0.5 <= x <= 0.5, realRegion1[x], If[x <= 0, realRegion2[x], realRegion3[x]] ], CompilationOptions -> {"InlineExternalDefinitions" -> True} ];  The output is quite satisfactory. You can compare Plot[reLi2[x], {x, -5, 5}] with Plot[Re@PolyLog[2Plot[Re @ PolyLog[2, x], {x, -5, 5}]. As an exercise of writing a good Compile function, I want to do the simple task of coding a routine that outputs the real part of the dilogarithm function reLi2[z], given a complex number z as input. I would like some feedback in my code below that does the job. I am following the strategy outlined in Celestial Mech. Dynam. Astron. 62 (1): 93–98. The idea is to divide the complex $$z$$-plane into four regions, as I show in the figure below, and to implement a different approximation in each region. In region 1, approximate the dilogarithm by the defining sum: $$\text{Li}_2(z) \approx \sum_{k=1}^\infty x^k/k^2$$. Here is the code: realRegion1 = Compile[{{x, _Real}}, Sum[x^k/k^2, {k, 1., 23}]];  In region 2, approximate the dilogarithm by evaluating the integral $$\text{Li}_2(z) \approx -\int_0^1 \frac{\ln(1-z t)}{t}\,dt$$ by Gaussian quadrature (9 divisions): With[{div = 9}, With[{x = Sort[formalX /. Solve[LegendreP[div, formalX] == 0, formalX] // N]}, With[{ y = Chop[x/2 + 1/2], w = Table[2/((1 - x[[i]]^2)*Derivative[0, 1][LegendreP][div, x[[i]]]^2), {i, 1, div}]}, With[{ realRegion2expr = -1/4.*Sum[ Chop[w[[i]]] Log[1 - 2 y[[i]] var + y[[i]]^2 var^2]/y[[i]], {i, 1, div}]}, realRegion2 = Compile[{{var, _Real}}, realRegion2expr]] ] ] ];  In region 3, apply the dilogarithm identity $$\text{Li}_2(z) = -\underbrace{\text{Li}_2(1-z)}_\text{region I} - \ln(z)\ln(1-z)+\pi^2/6$$, where the dilogarithm in the RHS is to be evaluated in region I. realRegion3 = Compile[{{x, _Real}}, If[x == 1, Pi^2/6., -realRegion1[1 - x] - Log[x] Log[1 - x] + Pi^2/6.], CompilationOptions -> {"InlineExternalDefinitions" -> True}];  In region 4, apply the dilogarithm identity $$\text{Li}_2(z) = -\underbrace{\text{Li}_2(1/z)}_\text{region I,II,III} - \frac{1}{2}\ln^2(-z)-\pi^2/6$$, where the dilogarithm in the RHS is to be appropriately evaluated in region I, II or III depending on the value of $$1/z$$. So first, I need to put together the functions realRegion1, realRegion2 and realRegion3 so that it correctly evaluates on the real line segment $$-1 \leq z \leq +1$$ appropriately: realSegment = Compile[{{x, _Real}}, If[-0.5 <= x <= 0.5, realRegion1[x], If[x <= 0, realRegion2[x], realRegion3[x]] ], CompilationOptions -> {"InlineExternalDefinitions" -> True} ];  The output is quite satisfactory. You can compare Plot[reLi2[x], {x, -5, 5}] with Plot[Re@PolyLog[2, x], {x, -5, 5}]. As an exercise in writing a good Compile function, I want to do the simple task of coding a routine that outputs the real part of the dilogarithm function reLi2[z], given a complex number z as input. I would like some feedback in my code below that does the job. I am following the strategy outlined in Celestial Mech. Dynam. Astron. 62 (1): 93–98. The idea is to divide the complex $$z$$-plane into four regions, as I show in the figure below, and to implement a different approximation in each region. In region 1, approximate the dilogarithm by the defining sum: $$\operatorname{Li}_2(z) \approx \sum_{k=1}^\infty x^k/k^2$$. Here is the code: realRegion1 = Compile[{{x, _Real}}, Sum[x^k/k^2, {k, 1., 23}]];  In region 2, approximate the dilogarithm by evaluating the integral $$\operatorname{Li}_2(z) \approx -\int_0^1 \frac{\ln(1-z t)}{t}\,dt$$ by Gaussian quadrature (9 divisions): With[{div = 9}, With[{x = Sort[formalX /. Solve[LegendreP[div, formalX] == 0, formalX] // N]}, With[{ y = Chop[x/2 + 1/2], w = Table[2/((1 - x[[i]]^2)*Derivative[0, 1][LegendreP][div, x[[i]]]^2), {i, 1, div}]}, With[{ realRegion2expr = -1/4.*Sum[ Chop[w[[i]]] Log[1 - 2 y[[i]] var + y[[i]]^2 var^2]/y[[i]], {i, 1, div}]}, realRegion2 = Compile[{{var, _Real}}, realRegion2expr]] ] ] ];  In region 3, apply the dilogarithm identity $$\operatorname{Li}_2(z) = -\underbrace{\operatorname{Li}_2(1-z)}_\text{region I} - \ln(z)\ln(1-z)+\pi^2/6$$, where the dilogarithm in the RHS is to be evaluated in region I. realRegion3 = Compile[{{x, _Real}}, If[x == 1, Pi^2/6., -realRegion1[1 - x] - Log[x] Log[1 - x] + Pi^2/6.], CompilationOptions -> {"InlineExternalDefinitions" -> True}];  In region 4, apply the dilogarithm identity $$\operatorname{Li}_2(z) = -\underbrace{\operatorname{Li}_2(1/z)}_\text{region I,II,III} - \frac{1}{2}\ln^2(-z)-\pi^2/6$$, where the dilogarithm in the RHS is to be appropriately evaluated in region I, II or III depending on the value of $$1/z$$. So first, I need to put together the functions realRegion1, realRegion2 and realRegion3 so that it correctly evaluates on the real line segment $$-1 \leq z \leq +1$$ appropriately: realSegment = Compile[{{x, _Real}}, If[-0.5 <= x <= 0.5, realRegion1[x], If[x <= 0, realRegion2[x], realRegion3[x]] ], CompilationOptions -> {"InlineExternalDefinitions" -> True} ];  The output is quite satisfactory. You can compare Plot[reLi2[x], {x, -5, 5}] with Plot[Re @ PolyLog[2, x], {x, -5, 5}]. Tweeted twitter.com/#!/StackMma/status/607649470134779904 occurred Jun 7 '15 at 20:44 5 deleted 15 characters in body edited Jun 7 '15 at 20:31 J. M. will be back soon♦ 101k1010 gold badges317317 silver badges477477 bronze badges I am following the strategy outlined in Celestial Mech. Dynam. Astron. 62 (1): 93–98Celestial Mech. Dynam. Astron. 62 (1): 93–98. The idea is to divide the complex $$z$$-plane into four regions, as I show in the figure below, and to implement a different approximation in each region. I am following the strategy outlined in Celestial Mech. Dynam. Astron. 62 (1): 93–98. The idea is to divide the complex $$z$$-plane into four regions, as I show in the figure below, and to implement a different approximation in each region. I am following the strategy outlined in Celestial Mech. Dynam. Astron. 62 (1): 93–98. The idea is to divide the complex $$z$$-plane into four regions, as I show in the figure below, and to implement a different approximation in each region. 4 edited tags | link edited Jun 7 '15 at 20:12 J. M. will be back soon♦ 101k1010 gold badges317317 silver badges477477 bronze badges 3 Edited code according to ybeltukov's comments edited Oct 11 '14 at 22:31 QuantumDot 8,37044 gold badges3232 silver badges9696 bronze badges 2 added 146 characters in body edited Oct 11 '14 at 13:42 m_goldberg 91.7k88 gold badges7575 silver badges209209 bronze badges 1 asked Oct 11 '14 at 12:39 QuantumDot 8,37044 gold badges3232 silver badges9696 bronze badges