I am trying to compute thousands of integrals of the below type, that comes up in a conformal mapping problem, to as many accurate digits as possible (preferably 50+):

$$ \int_{-1}^1\textrm{d}t \frac{\mathcal{Re}\{\log[(\cos{(\pi/130)} - t)]\}}{\sqrt{1 - t^2}} $$

The results from PARI/GP, Sage and Python's mpmath library respectively are:

(-2.1775860588840983, 1.2746272565903925e-05),

Clearly $-2.177$ is correct, but what's the best way to find a more accurate answer, accurate to 50+ digits?

I've tried splitting intervals from $[-1, \tau] \cup [\tau, 1]$, where $\tau = \cos{(\pi/130)}$; that doesn't improve things but actually makes it worse. I am working at much higher decimal precision than 3.

UPDATE: Following the suggestion of IgorRivin, the below is the Mathematica attempt:

tau = N[Cos[Pi/130], 50];
epsilon = 2^-10;
limit = N[ArcCos[1 - epsilon], 50];
T1 = NIntegrate[Re[Log[(tau - t)]]/(Sqrt[1 - t^2]), {t, -1, 1 - epsilon}, 
               WorkingPrecision -> 50, AccuracyGoal -> 50]
T2 = NIntegrate[Re[Log[tau - Cos[theta]]], {theta, 0, limit}, 
                WorkingPrecision -> 50, AccuracyGoal -> 50]
answer = T1 + T2

gives the results:

Out[1]= -1.7968036050143567231750633164742621583459497767361

During evaluation of In[881]:= NIntegrate::ncvb: NIntegrate failed to converge to prescribed 
accuracy after 9 recursive bisections in theta near 
{theta} = {0.024170580099064656300274166159163078224443688377727}. 
NIntegrate obtained -0.38078136551592029350719560689304843811203900465893 
and 6.9050153103011684224977203192777088512613553501207`50.*^-7 for the 
integral and error estimates. >>

Out[2]= -0.38078136551592029350719560689304843811203900465893

Out[3]= -2.1775849705302770166822589233673105964579887813950

As may be seen the error is still in the seventh decimal place; increasing the working precision or accuracy does not really improve things.

UPDATE: The integral is exactly soluble; the result is given in my comment to the accepted answer of Emil Jeřábek, and compares well to the exact value.

  • $\begingroup$ To increase the precision: for mpmath: mpmath.mp.dps=50 for pari \p 50 or default("realprecision",50) $\endgroup$ – joro Sep 23 '14 at 13:09
  • $\begingroup$ @joro: thanks for the comment, but I am already working at much higher precision than that. e.g. 100 digits for mpmath. Indeed more digits are spewed out for the integral but the error is still in the 4th or 5th decimal; I would like the error to be in 50+th decimal place. $\endgroup$ – MaviPranav Sep 23 '14 at 13:28
  • $\begingroup$ A, ok. I am not sure any of these can guarantee you bound on the error via numerical methods, though might be wrong. $\endgroup$ – joro Sep 23 '14 at 13:29
  • 5
    $\begingroup$ The integral simplifies to $\int_0^\pi\log\lvert\cos(\pi/130)-\cos\theta\rvert\,d\theta$. Anyway, I'd think your best bet is to first manipulate the integral in some way to get rid of the singularity, which is causing the numerical instability. $\endgroup$ – Emil Jeřábek Sep 23 '14 at 13:48
  • $\begingroup$ I think there is some serious confusion between WorkingPrecision and AccuracyGoal. If I am not mistaken (I am no Mathematica expert), the former is the machine precision that is used for computations, that is, the local error allowed at each computation. Using WorkingPrecision=50 it is impossible to get 50 significant digits on an ill-conditioned problem. $\endgroup$ – Federico Poloni Sep 28 '14 at 21:52

Expanding my comment above: putting $\alpha=\pi/130$, the integral equals $$\int_0^\pi\log\left|\cos\alpha-\cos\theta\right|d\theta=\int_0^\pi\log\frac{\cos\alpha-\cos\theta}{(\theta-\alpha)\sin\alpha}\,d\theta+\int_{-\alpha}^{\pi-\alpha}\log\left|\theta\sin\alpha\right|d\theta$$ $${}=\int_0^\pi\log\frac{\cos\alpha-\cos\theta}{(\theta-\alpha)\sin\alpha}\,d\theta+\pi\log\sin\alpha+\alpha(\log\alpha-1)+(\pi-\alpha)(\log(\pi-\alpha)-1).$$ The new integrand is regular on $[0,\pi]$, hence it has a better chance to be accurately approximated by numerical integration. I leave it to someone knowledgeable with such tools to try it.

In case it helps with cancellation errors, one can further write $$\int_0^\pi\log\frac{\cos\alpha-\cos\theta}{(\theta-\alpha)\sin\alpha}\,d\theta=\int_{-\alpha}^{\pi-\alpha}\log\left(\frac{\sin\theta}\theta+\frac{1-\cos\theta}\theta\cot\alpha\right)d\theta.$$

  • $\begingroup$ Great! Using 1000 digit precision mpmath and pari give -2.177586090303602130500688898237613947338583700369286294325795253194308549176741986430328961610663025 ... for the result, with the error from mpmath being 1.0e-717. $\endgroup$ – MaviPranav Sep 23 '14 at 16:22
  • 3
    $\begingroup$ This kind of thing is a great story to tell a student who thinks "the computer can just do it". Having the insight, and the calculus know how, to transform the integrand into something without a singularity, really showcases why we still need "practice integrating". $\endgroup$ – Steven Gubkin Sep 23 '14 at 16:50
  • 1
    $\begingroup$ @StevenGubkin: just to point out that today many of the integration routines are sophisticated enough that very high precision can be obtained for a large class of integrals without further simplification e.g. see pdf of talk by Cohen. Plus I think the need for computing results to 50+ digits might be better appreciated by researchers than by students, especially when most integrals (including this one) can be handled by straightforward software to give 3-4 digits of accuracy. Nevertheless I largely agree with your comment. $\endgroup$ – MaviPranav Sep 23 '14 at 21:22
  • 2
    $\begingroup$ I agree with Steven Gubkin’s comment. Nevertheless, what I’ve done with the integral is not particularly clever, and I can well imagine that a computer algebra system could automate it: in order to compute $\int_a^bf(x)\,dx$, (1) locate the singularities of $f$ in $[a,b]$ and determine their nature, (2) construct a function $g$ with the same singularities whose integral can be easily evaluated (e.g. with explicit antiderivative), (3) compute $\int_a^bf(x)-g(x)\,dx$ numerically. Clearly, current software has room for improvement. $\endgroup$ – Emil Jeřábek Sep 24 '14 at 11:43
  • 5
    $\begingroup$ @EmilJeřábek: The result is actually independent of $\alpha$ and equals $-\pi \log{2}$, as can be obtained from, of "Integrals and Series", Vol. 1, by Prudnikov et al.; the numerical result compares very well to the exact result to any desired precision. $\endgroup$ – MaviPranav Sep 28 '14 at 20:57

Not really appropriate for this site, but Mathematica gives the below (after complaining about convergence problems). On the other hand, integrating your function from $-1$ to $1-\epsilon,$ and using integration by parts (replacing $1/\sqrt{1-x^2}$ by $\arcsin x$) on $(1-\epsilon, 1)$ will work fine.

 NIntegrate[Re[Log[(Cos[Pi/130] - t)]]/(Sqrt[1 - t^2]), {t, -1, 1}, WorkingPrecision -> 1000]

  • 1
    $\begingroup$ thanks for the comment and suggestion, but the error is still not in the 1000th digit but at something much less. Moreover I have also tried your substitution suggestion but the error is still in the 7th digit (see update to original question). $\endgroup$ – MaviPranav Sep 23 '14 at 14:23
  • $\begingroup$ @MaviPranav Well, 7th is better than 5th :) but you can do better yet, I am quite sure... $\endgroup$ – Igor Rivin Sep 23 '14 at 15:00

To increase the precision: for mpmath: mpmath.mp.dps=50 for pari \p 50 or default("realprecision",50)

Working with precision 1000, mpmath disagrees with Rivin's answer, though the result seems heavily to depend on the precision.

sage: pre=1000;mpmath.mp.dps=pre
sage: time t=mpmath.quad(lambda t:     mpmath.re(mpmath.log(mpmath.cos(mpmath.pi/ mpmath.mpf('130'))-t ))/ mpmath.sqrt(1-t**2),[-1,1])
CPU times: user 34.4 s, sys: 140 ms, total: 34.5 s
Wall time: 34.8 s
sage: t

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.