-
Notifications
You must be signed in to change notification settings - Fork 71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Numerically approximate ellipse perimeter #383
Conversation
This uses the infinite series for the ellipse perimeter as described by Kummer (1837) and rediscovered by Linderholm and Segal (1995). It converges quadratically in the worst case, and in the common case of ellipses that are nearly circular or moderately eccentric, it converges very fast.
This solves the general case up to arbitrary precision.
|
The method I first added ( The method based on binomial coefficients is still nice, in the sense that the coefficients can be known at compile-time. We can expand that series up to some number of terms, evaluating that in the case the approximation is good enough for the given eccentricity and accuracy. Wikipedia states terms up to and including the 4th power are enough for eccentricities of 0.5 or less to reach the limits of That allows a loopless approximation for the most common cases, using the quadratically converging iterative arithmetic-geometric mean method for very high eccentricity or very precise accuracy. |
Done, and PR text updated. Perhaps we should benchmark whether this is actually enough of a performance improvement for common ellipses, to justify the additional lines of code. |
021bbca
to
395a4dc
Compare
1381f88
to
9fffb75
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very cool, from what I can see the math checks out.
I think the AGM algorithm is probably very good, but for one other possibility that seems to work well in the high eccentricity case, see "Cayley" in The Perimeter of an Ellipse. That formula requires logarithms, though, so it's not clear it will be better than AGM. Also, I suspect the accuracy of table 1, as perimeter is not monotonic.
Very nice, that does seem to work well. It only requires evaluating I wonder if the overlap between Kummer and Cayley is enough to guarantee machine precision for all eccentricities, without too many terms.
Well-spotted. Having checked a few, it seems from b=0.002 onwards the reported perimeter decimals are accidentally shifted left by one. The decimals are otherwise correct. |
I think the Cayley formula is worth exploring, but will leave that for another PR. I haven't looked into it yet. In the meantime this is an improvement and is useful also for #381. |
This implements the (truncated) infinite series for the ellipse perimeter as described by Kummer (1837) and rediscovered by Linderholm and Segal (1995), and the iterative arithmetic-geometric mean method.
In the common case of ellipses that are only moderately eccentric and for "normal" accuracy (say, 0.001), the infinite series converges quickly. The series is known at compile-time, truncated to the 6th power. For a given ellipse and accuracy, a quick check is performed at runtime, to determine whether the truncated series' approximation is within the desired accuracy. If so, the truncated series is evaluated.
If the check determines the approximation is not good enough, the problem is handed to the iterative arithmetic-geometric mean method. This method converges quadratically for all cases.