# How to avoid problems with the large imaginary part of LegendreQ?

I am trying to evaluate sums involving LegendreQ[n,x], the n-th order Legendre function of the second kind $Q_n(x)$, at large $x>1$. However, the problem is that every $Q_n(x)$ contains a term proportional to $$\mathrm{Log}\left(\frac{1 + x}{1 - x}\right)$$ which for $x>1$ and the usual definition of the logarithm adds an imaginary part to the result.

Mathematically speaking, to obtain a result in the Reals, one must use a different branch of the complex logarithm appearing in the Legendre functions so that $$\mathrm{Log}(z) = \ln(|z|) + i \mathrm{Arg}(z) - i\pi$$ then we have $$\mathrm{Log}\left(\frac{1 + x}{1 - x}\right) = \ln \left(\frac{1 + x}{|1 - x|}\right) \,, \;x>1$$ and the Legendre functions are now real for $x>1$. What I do in practice, however, is that I observe that the different choice of the branch influences only the imaginary part of any $Q_n(x)$, so I do not attempt to shift the branch of the logarithm and just use Re[LegendreQ[n, x]].

But this causes some issues on the numerical level because for $x\to \infty$ and the usual branch of the logarithm $$\Re(Q_n(x)) \sim x^{-n}$$ $$\Im(Q_n(x)) \sim x^{n}$$ The function Re then probably takes in a small contribution from the imaginary part at large $x$, and what results is a disaster. This is a plot of Re[LegendreQ[4, x]] in an interval which is relevant for me: Do you have any idea how to troubleshoot this?

• You could try using extended precision (e.g., use WorkingPrecision->30 in your plot), or you could try using FullSimplify @ ComplexExpand[Re @ LegendreQ[4,x]]. Feb 14 '18 at 17:11
• Thanks for the tip. I tried it and it produced this: i.imgur.com/mEGdzMm.jpg It might not look like it, but it is an improvement by two orders of magnitude and this noise comes only from close-number subtraction. I think I will have to manually implement some cut-offs or increase precision but this is what I needed. Feel free to post this as an answer and I will accept it.
– Void
Feb 14 '18 at 17:26
• Ok, just increasing WorkingPrecision and keeping Re[LegendreQ[n,x]] works as well, that is the easiest solution.
– Void
Feb 14 '18 at 17:34

As you suspected, the issue is that when using machine numbers, the error will be approximately 10^-16 times the magnitude of the number. Since the imaginary part becomes very large, the error in the real part becomes significant:

Through @ {Abs, Re} @ LegendreQ[4, 1000]


{6.87223*10^12, -0.00126241}

Since the issue is with precision loss when using machine numbers, an alternative is to use extended precision numbers (bignums) instead. For example:

Re @ LegendreQ[4, N @ 1000]
Re @ LegendreQ[4, N[1000, 30]]
N[Re @ LegendreQ[4, 1000], 30]


-0.00126241

0.*10^-18

2.53968600288973249346209708201*10^-17

The first two alternatives experience precision loss due to subtractive cancellations, while the third approach works around this by internally raising precision until the precision goal can be met.

For plots, you can use a higher precision with the option WorkingPrecision:

Plot[LegendreQ[4, x], {x, 0, 1000}, WorkingPrecision->30] Another alternative is to try to symbolically simplify the real part:

s = FullSimplify @ ComplexExpand[Re @ LegendreQ[4, x]]


1/96 (220 x - 420 x^3 + 6 (3 - 30 x^2 + 35 x^4) ArcTanh[(2 x)/(1 + x^2)])

This expression will still suffer from precision degradation, but the loss of precision should be much smaller.

From your considerations, it seems to me that what you actually want are the Legendre/Ferrers functions that have, in Mathematica's notation, "type 3" branch cuts. That is, you are interested in LegendreQ[n, 0, 3, z] instead of the default LegendreQ[n, z] == LegendreQ[n, 0, 1, z] with "type 1" branch cuts.

Using Carl's examples:

Through @ {Abs, Re} @ LegendreQ[4, 0, 3, 1000]
{2.53969*10^-17, 2.53969*10^-17}

{Re @ LegendreQ[4, 0, 3, N @ 1000], Re @ LegendreQ[4, 0, 3, N[1000, 30]],
N[Re @ LegendreQ[4, 0, 3, 1000], 30]}
{2.53969*10^-17, 2.53968600288973249346209708201*10^-17,
2.53968600288973249346209708201*10^-17}

FullSimplify @ ComplexExpand[Re @ LegendreQ[4, 0, 3, x]]
1/96 (220 x - 420 x^3 - 3 (3 - 30 x^2 + 35 x^4) Log[(-1 + x)^2] +
3 (3 - 30 x^2 + 35 x^4) Log[(1 + x)^2])