-
Notifications
You must be signed in to change notification settings - Fork 48
First pass at EFT-based spherical geometry accuracy (port from AccuSphGeom) #1513
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
Draft
rajeeja
wants to merge
4
commits into
main
Choose a base branch
from
rajeeja/accusphere
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7563aba
Port AccuSphGeom EFT algorithms and add spherical geometry user guide
rajeeja 16cc263
Fix pre-commit: remove unused imports and variable
rajeeja ab3d514
Fix pre-commit: split semicolons in notebook cells
rajeeja b4624fe
Merge branch 'main' into rajeeja/accusphere
rajeeja File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
1,776 changes: 1,776 additions & 0 deletions
1,776
docs/user-guide/spherical-geometry-accuracy.ipynb
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| """Error-free transformations (EFT) for accurate floating-point arithmetic. | ||
|
|
||
| In spherical-geometry computations the critical operations are cross products | ||
| and dot products over unit vectors. When two vectors are nearly parallel, the | ||
| difference of products that forms each cross-product component suffers | ||
| catastrophic cancellation: both products round to the same floating-point | ||
| value and their difference carries no significant bits. This affects | ||
| GCA-GCA intersection of nearly tangent arcs, constant-latitude intersection | ||
| near arc endpoints, and the ray-crossing test in point-in-polygon near polygon | ||
| edges. | ||
|
|
||
| The functions here represent each result as an unevaluated sum of two | ||
| ``float64`` values ``(hi, lo)`` such that ``hi + lo`` equals the | ||
| mathematically exact result. This effectively doubles the significant bits | ||
| available for cross-product components without resorting to arbitrary- | ||
| precision arithmetic. | ||
|
|
||
| These primitives are a Python/Numba port of the error-free transformation | ||
| layer from the AccuSphGeom C++ library: | ||
|
|
||
| Chen, H. (2026). Accurate and Robust Algorithms for Spherical Polygon | ||
| Operations. EGUsphere preprint. | ||
| https://egusphere.copernicus.org/preprints/2026/egusphere-2026-636/ | ||
|
|
||
| Chen, H. Accurate and Robust Great Circle Arc Intersection and Great | ||
| Circle Arc Constant Latitude Intersection on the Sphere. SIAM J. Sci. | ||
| Comput. https://doi.org/10.1137/25M1737614 | ||
|
|
||
| AccuSphGeom reference implementation (C++): | ||
| https://github.com/hongyuchen1030/AccuSphGeom | ||
|
|
||
| What this module omits: AccuSphGeom's full robustness stack has three | ||
| tiers — an EFT filter (what this module implements), Shewchuk adaptive | ||
| predicates for results that fall inside the filter threshold, and a geogram | ||
| exact-arithmetic fallback. This port implements only the EFT tier. For | ||
| non-degenerate inputs in double precision this is sufficient; callers that | ||
| need to handle geometrically degenerate inputs (coincident arcs, a query | ||
| point exactly on a polygon edge) should add their own perturbation or | ||
| fall-back logic. | ||
| """ | ||
|
|
||
| from numba import njit | ||
|
|
||
|
|
||
| @njit(cache=True, inline="always") | ||
| def two_sum(a, b): | ||
| """Knuth's TwoSum: return (s, e) with s = fl(a + b) and s + e = a + b exactly. | ||
|
|
||
| Floating-point addition rounds the mathematical result to the nearest | ||
| representable value. ``two_sum`` captures that rounding error in the | ||
| companion term ``e`` so that ``s + e`` equals the true sum with no | ||
| information lost. The cost is four extra floating-point operations beyond | ||
| the addition itself. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| a, b : float | ||
| Input values. | ||
|
|
||
| Returns | ||
| ------- | ||
| s : float | ||
| Rounded sum fl(a + b). | ||
| e : float | ||
| Rounding error term; s + e = a + b exactly. | ||
| """ | ||
| s = a + b | ||
| bp = s - a | ||
| e = (a - (s - bp)) + (b - bp) | ||
| return s, e | ||
|
|
||
|
|
||
| @njit(cache=True, inline="always") | ||
| def two_prod(a, b): | ||
| """Dekker/Veltkamp TwoProd: return (p, e) with p = fl(a * b) and p + e = a * b exactly. | ||
|
|
||
| Like ``two_sum`` for multiplication. Uses the Veltkamp splitting constant | ||
| 2**27 + 1 to decompose each operand into a high and low half, then | ||
| reconstructs the exact rounding error from the four partial products. | ||
| On hardware with a fused multiply-add (FMA) instruction the error term | ||
| could be obtained in one step as ``fma(a, b, -p)``; the split used here | ||
| is portable across all Numba targets. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| a, b : float | ||
| Input values. | ||
|
|
||
| Returns | ||
| ------- | ||
| p : float | ||
| Rounded product fl(a * b). | ||
| e : float | ||
| Rounding error term; p + e = a * b exactly. | ||
| """ | ||
| p = a * b | ||
| factor = 134217729.0 # 2**27 + 1 | ||
| a_hi = factor * a - (factor * a - a) | ||
| a_lo = a - a_hi | ||
| b_hi = factor * b - (factor * b - b) | ||
| b_lo = b - b_hi | ||
| e = a_lo * b_lo - (((p - a_hi * b_hi) - a_lo * b_hi) - a_hi * b_lo) | ||
| return p, e | ||
|
|
||
|
|
||
| @njit(cache=True, inline="always") | ||
| def diff_of_products(a, b, c, d): | ||
| """Kahan's accurate a*b - c*d using two_prod and two_sum. | ||
|
|
||
| Naive evaluation of ``a*b - c*d`` loses all significant bits when the two | ||
| products are nearly equal (catastrophic cancellation). This routine | ||
| computes each product exactly via ``two_prod``, subtracts the rounded | ||
| high parts, then folds the residual low parts back in. The result has | ||
| rounding error bounded by one ulp of the true value regardless of | ||
| cancellation. | ||
|
|
||
| This is the core operation that makes cross products accurate: every | ||
| component of ``a x b`` is a difference of two products of exactly this | ||
| form. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| a, b, c, d : float | ||
| Input scalars; computes a*b - c*d. | ||
|
|
||
| Returns | ||
| ------- | ||
| hi : float | ||
| High-order part of the accurate result. | ||
| lo : float | ||
| Low-order correction term; hi + lo equals the accurate value. | ||
| """ | ||
| w, e_w = two_prod(c, d) | ||
| x, e_x = two_prod(a, b) | ||
| s, e_s = two_sum(x, -w) | ||
| lo = (e_x - e_w) + e_s | ||
| return s, lo | ||
|
|
||
|
|
||
| @njit(cache=True, inline="always") | ||
| def accucross(a0, a1, a2, b0, b1, b2): | ||
| """Accurate cross product a x b returning (hi[3], lo[3]) component pairs. | ||
|
|
||
| Each component of a cross product is a difference of two products — the | ||
| exact form that ``diff_of_products`` handles. This function computes all | ||
| three components that way, returning six scalars such that the | ||
| mathematically exact cross product satisfies ``result[i] = hi[i] + lo[i]`` | ||
| for each component. Callers that need single-precision accuracy can use | ||
| the hi parts alone; callers that need the full compensated result add | ||
| hi and lo before further use. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| a0, a1, a2 : float | ||
| Components of vector a. | ||
| b0, b1, b2 : float | ||
| Components of vector b. | ||
|
|
||
| Returns | ||
| ------- | ||
| x_hi, y_hi, z_hi, x_lo, y_lo, z_lo : float | ||
| High and low parts of each cross-product component. | ||
| """ | ||
| x_hi, x_lo = diff_of_products(a1, b2, a2, b1) | ||
| y_hi, y_lo = diff_of_products(a2, b0, a0, b2) | ||
| z_hi, z_lo = diff_of_products(a0, b1, a1, b0) | ||
| return x_hi, y_hi, z_hi, x_lo, y_lo, z_lo |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
We have an existed implementation for these functions in
uxarray/uxarray/utils/computing.py
Line 189 in 735a1dd
And I would suggest use other name instead of eft here, the term "EFT" specifically refers to "Error-Free" floating point operations, but the
AccuCrossanddiff_of_productthemself is not completely Error-Free, is just more accurate than normal floating point cross (doubling the precision)