Open susilehtola opened 1 year ago
Pull requests welcome!
I don't think we need to pull in another library. The closed-form approximations in this paper get more accurate as the degree increases, so we can just switch algorithms once the degree gets beyond a threshold. I only coded up the first three terms of a series, and it looks like the worst error is 618 ULP with $n=100$, 41 ULP with $n=150$, and 9 ULP with $n=200$. The paper claims that with all the terms, the approach gives roots and weights accurate to machine precision for $n \geq 30$.
n = 200
x, w = gauss_legendre2(n)
x0, w0 = gauss_legendre(n, 20)
x0, w0 = np.asarray(x0, dtype=np.float64), np.asarray(w0, dtype=np.float64)
np.testing.assert_array_almost_equal_nulp(w, w0)
# AssertionError: Arrays are not equal to 1 ULP (max is 9)
Just a comment to suggest N. Hale and A. Townsend, Fast and accurate computation of Gauss-Legendre and Gauss-Jacobi quadrature nodes and weights, SIAM Journal on Scientific Computing, 35 (2013), A652-A672. This is used in Chebfun and is impressively fast
Faster than the code above? With everything implemented, it would require no iteration under the hood. The paper behind it cites Hale and Townsend and notes several other advantages.
In any case, this is about the accuracy and what can get done. I would be willing to review a PR based on the Bogaert paper since I'm now familiar with it and there is already code.
That is a good question, I'm away from a computer for some time so can't investigate. Do of course feel free to proceed with what you have proposed, it was just that I remembered that one of Chebfun's party tricks was the computation of guass Legendre weights so thought it was worth throwing this paper into the ring
Describe your issue.
The Gauss-Legendre weights generated by scipy are inaccurate. A comparison to SymPy's analytic roots shows that while the accuracy is good for small numbers of nodes, the discrepancies become noticeable for large numbers of nodes.
Our C++ implementation of Gauss-Legendre quadrature in https://github.com/wavefunction91/IntegratorXX/blob/master/include/integratorxx/quadratures/primitive/gausslegendre.hpp maintains an accuracy of
10*eps
in the nodes and weights at least up to 920 nodes. In contrast, the discrepancy in scipy grows with the number of nodes, and reaches an error for the weights of1.9785e-14
with 500 nodes, which is 89 times machine epsilon, and1.3362e-13
with 1000 nodes or 601 times epsilon.Reproducing Code Example
Error message
SciPy/NumPy/Python version and system information