From 470472beae8e731fa7b24658cda1094dc9944f3f Mon Sep 17 00:00:00 2001 From: Hongyu Chen Date: Thu, 29 Oct 2026 20:57:42 -0700 Subject: [PATCH 1/2] Fix _small_angle_of_2_vectors to evaluate Kahan's formula accurately _small_angle_of_2_vectors implements Kahan's angle formula for vectors of arbitrary length, 2*atan2(|v|u| - u|v||, |v|u| + u|v||), but formed each component of v|u| - u|v| from two separately rounded products. For nearly parallel vectors this is a naive difference of products, and the relative error grew as eps/angle (a mean of about 2e-8 at 1e-9 rad). Each component of v|u| - u|v| is now evaluated with diff_of_products. The norms and the sum are still computed directly: compensating the norms only removes a second-order (eps/angle)^2 term below ~1e-7 rad, and the sum's cancellation near antiparallel vectors does not affect the returned angle. The mean relative error is now below 1e-16 down to 1e-7 rad and 3.3e-15 at 1e-9 rad, at a mean of 48.5 ns per call (41.9 ns before). Add tests against an exact rational reference; they fail on the previous implementation. --- test/test_helpers.py | 54 ++++++++++++++++++++++++++++++++++++++++++- uxarray/grid/utils.py | 52 ++++++++++++++++++++++++++++++++--------- 2 files changed, 94 insertions(+), 12 deletions(-) diff --git a/test/test_helpers.py b/test/test_helpers.py index 63de3e294..5a6c6d8e3 100644 --- a/test/test_helpers.py +++ b/test/test_helpers.py @@ -9,7 +9,7 @@ from uxarray.constants import INT_DTYPE, INT_FILL_VALUE from uxarray.grid.coordinates import _lonlat_rad_to_xyz, _normalize_xyz, _xyz_to_lonlat_rad from uxarray.grid.arcs import point_within_gca, _angle_of_2_vectors, in_between -from uxarray.grid.utils import _get_cartesian_face_edge_nodes_array, _get_lonlat_rad_face_edge_nodes_array +from uxarray.grid.utils import _get_cartesian_face_edge_nodes_array, _get_lonlat_rad_face_edge_nodes_array, _small_angle_of_2_vectors from uxarray.grid.geometry import pole_point_inside_polygon, _pole_point_inside_polygon_cartesian err_tolerance = 1.0e-12 @@ -245,6 +245,58 @@ def test_angle_of_2_vectors_180_degree(): # The angle between the two vectors should be 99 degrees assert pytest.approx(res, abs=1e-8) == np.deg2rad(89.0+10.0) + +def _exact_small_angle(u, v): + """Angle between two float vectors, for angles well below 1 rad. + + The cross and dot products are formed exactly in rational arithmetic, then + atan(|u x v| / (u . v)) is evaluated with a 50-digit Taylor series. + """ + from decimal import Decimal, localcontext + from fractions import Fraction + + u = [Fraction(float(c)) for c in u] + v = [Fraction(float(c)) for c in v] + cross = (u[1] * v[2] - u[2] * v[1], u[2] * v[0] - u[0] * v[2], u[0] * v[1] - u[1] * v[0]) + cross2 = sum(c * c for c in cross) + dot = sum(a * b for a, b in zip(u, v)) + with localcontext() as ctx: + ctx.prec = 50 + t = (Decimal(cross2.numerator) / Decimal(cross2.denominator)).sqrt() / ( + Decimal(dot.numerator) / Decimal(dot.denominator)) + term, total, k = t, Decimal(0), 0 + while abs(term) > Decimal(10) ** -45: + total += term / (2 * k + 1) * (-1) ** k + term *= t * t + k += 1 + return float(total) + + +def test_small_angle_of_2_vectors_special_values(): + assert _small_angle_of_2_vectors((1.0, 0.0, 0.0), (0.0, 3.0, 0.0)) == pytest.approx(np.pi / 2, rel=1e-15) + assert _small_angle_of_2_vectors((2.0, 0.0, 0.0), (0.5, 0.0, 0.0)) == 0.0 + assert _small_angle_of_2_vectors((0.0, 0.0, 1.0), (0.0, 0.0, -7.0)) == pytest.approx(np.pi, rel=1e-15) + assert _small_angle_of_2_vectors((0.0, 0.0, 0.0), (1.0, 2.0, 3.0)) == 0.0 + + +@pytest.mark.parametrize("theta, rtol", [(1e-3, 1e-14), (1e-5, 1e-14), (1e-7, 1e-14), (1e-9, 1e-13)]) +def test_small_angle_of_2_vectors_accuracy(theta, rtol): + """Nearly parallel vectors of different, arbitrary lengths. + + A naive evaluation of v|u| - u|v| loses accuracy as eps / theta (about 1e-7 + at theta = 1e-9); with compensated products it stays within a few ulps, + plus a second-order (eps / theta)^2 term from the rounded norms. + """ + rng = np.random.default_rng(0) + for _ in range(50): + u = rng.normal(size=3) + w = np.cross(u, rng.normal(size=3)) + v = u / np.linalg.norm(u) * np.cos(theta) + w / np.linalg.norm(w) * np.sin(theta) + u, v = u * rng.uniform(0.1, 10.0), v * rng.uniform(0.1, 10.0) + expected = _exact_small_angle(u, v) + assert _small_angle_of_2_vectors(u, v) == pytest.approx(expected, rel=rtol, abs=0.0) + assert _small_angle_of_2_vectors(v, u) == pytest.approx(expected, rel=rtol, abs=0.0) + def test_get_cartesian_face_edge_nodes_pipeline(): vertices = [[0.5, 0.5, 0.5], [-0.5, 0.5, 0.5], [-0.5, -0.5, 0.5], [0.5, -0.5, 0.5]] vertices = [x / np.linalg.norm(x) for x in vertices] diff --git a/uxarray/grid/utils.py b/uxarray/grid/utils.py index ce50d983c..6cd42981c 100644 --- a/uxarray/grid/utils.py +++ b/uxarray/grid/utils.py @@ -3,13 +3,11 @@ from numba import njit, prange from uxarray.constants import EDGE_NODE_SORT_THRESHOLD, INT_DTYPE, INT_FILL_VALUE +from uxarray.utils.computing import diff_of_products from uxarray.utils.numba_math import ( - _numba_add3, _numba_cross3, _numba_dot3, - _numba_mul3_scalar, _numba_norm3, - _numba_sub3, ) from uxarray.utils.parallel import parallel_njit @@ -17,7 +15,19 @@ @njit(cache=True) def _small_angle_of_2_vectors(u, v): """ - Compute the smallest angle between two vectors using the new _angle_of_2_vectors. + Compute the angle in [0, pi] between two vectors of arbitrary lengths. + + Uses Kahan's formula + + angle = 2 * atan2(| v |u| - u |v| |, | v |u| + u |v| |), + + which avoids the cancellation of the arcsin/arccos forms and of the cross + product in atan2(|u x v|, u . v). When the vectors are nearly parallel, + every component of ``v |u| - u |v|`` is a difference of two nearly equal + products, so it is evaluated with the FMA-based ``diff_of_products`` + (Kahan's 2x2 determinant). The result is accurate to a few ulps for angles + down to about 1e-7 rad, for any input lengths; below that, the rounding of + the norms adds a second-order relative error of about (eps / angle)^2. Parameters ---------- @@ -29,14 +39,34 @@ def _small_angle_of_2_vectors(u, v): Returns ------- float - The smallest angle between `u` and `v` in radians. + The angle between `u` and `v` in radians. Zero if either vector is zero. + + References + ---------- + Kahan, W. (2006). How Futile are Mindless Assessments of Roundoff in + Floating-Point Computation? https://people.eecs.berkeley.edu/~wkahan/Mindless.pdf + + Chen, H., Ullrich, P. A., Panetta, J., Marsico, D., Hanke, M., Jain, R., + Zhang, C., and Jacob, R. L. (2026). Accurate and robust geometric + algorithms for regridding on the sphere. Geoscientific Model + Development, 19(14), 6545-6570. https://doi.org/10.5194/gmd-19-6545-2026 """ - u_times_v_norm = _numba_mul3_scalar(v, _numba_norm3(u)) - v_times_u_norm = _numba_mul3_scalar(u, _numba_norm3(v)) - vec_minus = _numba_sub3(u_times_v_norm, v_times_u_norm) - vec_sum = _numba_add3(u_times_v_norm, v_times_u_norm) - angle_u_v_rad = 2 * np.arctan2(_numba_norm3(vec_minus), _numba_norm3(vec_sum)) - return angle_u_v_rad + u0, u1, u2 = float(u[0]), float(u[1]), float(u[2]) + v0, v1, v2 = float(v[0]), float(v[1]), float(v[2]) + nu = _numba_norm3((u0, u1, u2)) + nv = _numba_norm3((v0, v1, v2)) + + # v|u| - u|v|, each component as a compensated difference of products. + m0_hi, m0_lo = diff_of_products(v0, nu, u0, nv) + m1_hi, m1_lo = diff_of_products(v1, nu, u1, nv) + m2_hi, m2_lo = diff_of_products(v2, nu, u2, nv) + m = (m0_hi + m0_lo, m1_hi + m1_lo, m2_hi + m2_lo) + + # v|u| + u|v|: its cancellation near antiparallel vectors does not affect + # the returned angle, so it is evaluated directly. + s = (v0 * nu + u0 * nv, v1 * nu + u1 * nv, v2 * nu + u2 * nv) + + return 2.0 * np.arctan2(_numba_norm3(m), _numba_norm3(s)) @njit(cache=True) From f3ebd7f9a9a77e4bee9c8079027f89f14ce73e8d Mon Sep 17 00:00:00 2001 From: Hongyu Chen Date: Fri, 30 Oct 2026 10:02:34 -0700 Subject: [PATCH 2/2] Address review: add _numba_diff_of_products3 and move angle tests - Add _numba_diff_of_products3(u, a, v, b) to utils/numba_math.py: the component-wise u*a - v*b of 3-vectors via diff_of_products, returning (hi, lo) tuples. - Write _small_angle_of_2_vectors with vector helpers instead of spelling out components; this also drops the float() conversions, which were only there to unpack components. - Move the _angle_of_2_vectors and _small_angle_of_2_vectors tests from test_helpers.py to test/grid/geometry/test_angles.py, and test the new helper against exact rational arithmetic. Accuracy and speed are unchanged. --- test/grid/geometry/test_angles.py | 92 +++++++++++++++++++++++++++++++ test/test_helpers.py | 91 +----------------------------- test/utils/test_numba_math.py | 14 +++++ uxarray/grid/utils.py | 25 ++++----- uxarray/utils/numba_math.py | 23 ++++++++ 5 files changed, 143 insertions(+), 102 deletions(-) diff --git a/test/grid/geometry/test_angles.py b/test/grid/geometry/test_angles.py index 5987a2d04..1a228111a 100644 --- a/test/grid/geometry/test_angles.py +++ b/test/grid/geometry/test_angles.py @@ -3,9 +3,13 @@ """ import numpy as np +import pytest import xarray as xr import uxarray as ux +from uxarray.grid.arcs import _angle_of_2_vectors +from uxarray.grid.coordinates import _lonlat_rad_to_xyz +from uxarray.grid.utils import _small_angle_of_2_vectors def test_face_node_angles_triangle(): @@ -109,3 +113,91 @@ def test_equiangle_skewness(): assert skewness.max() < 0.15 # skewness is well defined for all faces assert not np.any(skewness.isnull()) + + +def test_angle_of_2_vectors(): + # Test the angle between two vectors + v1 = np.array([1.0, 0.0, 0.0]) + v2 = np.array([0.0, 1.0, 0.0]) + assert pytest.approx(_angle_of_2_vectors(v1, v2)) == np.pi / 2.0 + + v1 = np.array([1.0, 0.0, 0.0]) + v2 = np.array([1.0, 0.0, 0.0]) + assert pytest.approx(_angle_of_2_vectors(v1, v2)) == 0.0 + +def test_angle_of_2_vectors_180_degree(): + GCR1_cart = np.array([ + _lonlat_rad_to_xyz(np.deg2rad(0.0), + np.deg2rad(0.0)), + _lonlat_rad_to_xyz(np.deg2rad(181.0), + np.deg2rad(0.0)) + ]) + + res = _angle_of_2_vectors(GCR1_cart[0], GCR1_cart[1]) + + # The angle between the two vectors should be 181 degree + assert pytest.approx(res, abs=1e-8) == np.deg2rad(181.0) + + GCR1_cart = np.array([ + _lonlat_rad_to_xyz(np.deg2rad(170.0), + np.deg2rad(89.0)), + _lonlat_rad_to_xyz(np.deg2rad(170.0), + np.deg2rad(-10.0)) + ]) + + res = _angle_of_2_vectors(GCR1_cart[0], GCR1_cart[1]) + + # The angle between the two vectors should be 99 degrees + assert pytest.approx(res, abs=1e-8) == np.deg2rad(89.0+10.0) + + +def _exact_small_angle(u, v): + """Angle between two float vectors, for angles well below 1 rad. + + The cross and dot products are formed exactly in rational arithmetic, then + atan(|u x v| / (u . v)) is evaluated with a 50-digit Taylor series. + """ + from decimal import Decimal, localcontext + from fractions import Fraction + + u = [Fraction(float(c)) for c in u] + v = [Fraction(float(c)) for c in v] + cross = (u[1] * v[2] - u[2] * v[1], u[2] * v[0] - u[0] * v[2], u[0] * v[1] - u[1] * v[0]) + cross2 = sum(c * c for c in cross) + dot = sum(a * b for a, b in zip(u, v)) + with localcontext() as ctx: + ctx.prec = 50 + t = (Decimal(cross2.numerator) / Decimal(cross2.denominator)).sqrt() / ( + Decimal(dot.numerator) / Decimal(dot.denominator)) + term, total, k = t, Decimal(0), 0 + while abs(term) > Decimal(10) ** -45: + total += term / (2 * k + 1) * (-1) ** k + term *= t * t + k += 1 + return float(total) + + +def test_small_angle_of_2_vectors_special_values(): + assert _small_angle_of_2_vectors((1.0, 0.0, 0.0), (0.0, 3.0, 0.0)) == pytest.approx(np.pi / 2, rel=1e-15) + assert _small_angle_of_2_vectors((2.0, 0.0, 0.0), (0.5, 0.0, 0.0)) == 0.0 + assert _small_angle_of_2_vectors((0.0, 0.0, 1.0), (0.0, 0.0, -7.0)) == pytest.approx(np.pi, rel=1e-15) + assert _small_angle_of_2_vectors((0.0, 0.0, 0.0), (1.0, 2.0, 3.0)) == 0.0 + + +@pytest.mark.parametrize("theta, rtol", [(1e-3, 1e-14), (1e-5, 1e-14), (1e-7, 1e-14), (1e-9, 1e-13)]) +def test_small_angle_of_2_vectors_accuracy(theta, rtol): + """Nearly parallel vectors of different, arbitrary lengths. + + A naive evaluation of v|u| - u|v| loses accuracy as eps / theta (about 1e-7 + at theta = 1e-9); with compensated products it stays within a few ulps, + plus a second-order (eps / theta)^2 term from the rounded norms. + """ + rng = np.random.default_rng(0) + for _ in range(50): + u = rng.normal(size=3) + w = np.cross(u, rng.normal(size=3)) + v = u / np.linalg.norm(u) * np.cos(theta) + w / np.linalg.norm(w) * np.sin(theta) + u, v = u * rng.uniform(0.1, 10.0), v * rng.uniform(0.1, 10.0) + expected = _exact_small_angle(u, v) + assert _small_angle_of_2_vectors(u, v) == pytest.approx(expected, rel=rtol, abs=0.0) + assert _small_angle_of_2_vectors(v, u) == pytest.approx(expected, rel=rtol, abs=0.0) diff --git a/test/test_helpers.py b/test/test_helpers.py index 5a6c6d8e3..cf70a8d8d 100644 --- a/test/test_helpers.py +++ b/test/test_helpers.py @@ -8,8 +8,8 @@ from uxarray.grid.connectivity import _replace_fill_values from uxarray.constants import INT_DTYPE, INT_FILL_VALUE from uxarray.grid.coordinates import _lonlat_rad_to_xyz, _normalize_xyz, _xyz_to_lonlat_rad -from uxarray.grid.arcs import point_within_gca, _angle_of_2_vectors, in_between -from uxarray.grid.utils import _get_cartesian_face_edge_nodes_array, _get_lonlat_rad_face_edge_nodes_array, _small_angle_of_2_vectors +from uxarray.grid.arcs import point_within_gca, in_between +from uxarray.grid.utils import _get_cartesian_face_edge_nodes_array, _get_lonlat_rad_face_edge_nodes_array from uxarray.grid.geometry import pole_point_inside_polygon, _pole_point_inside_polygon_cartesian err_tolerance = 1.0e-12 @@ -210,93 +210,6 @@ def test_in_between(): assert in_between(0, 1, 2) assert in_between(-1, -1.5, -2) -def test_angle_of_2_vectors(): - # Test the angle between two vectors - v1 = np.array([1.0, 0.0, 0.0]) - v2 = np.array([0.0, 1.0, 0.0]) - assert pytest.approx(_angle_of_2_vectors(v1, v2)) == np.pi / 2.0 - - v1 = np.array([1.0, 0.0, 0.0]) - v2 = np.array([1.0, 0.0, 0.0]) - assert pytest.approx(_angle_of_2_vectors(v1, v2)) == 0.0 - -def test_angle_of_2_vectors_180_degree(): - GCR1_cart = np.array([ - _lonlat_rad_to_xyz(np.deg2rad(0.0), - np.deg2rad(0.0)), - _lonlat_rad_to_xyz(np.deg2rad(181.0), - np.deg2rad(0.0)) - ]) - - res = _angle_of_2_vectors(GCR1_cart[0], GCR1_cart[1]) - - # The angle between the two vectors should be 181 degree - assert pytest.approx(res, abs=1e-8) == np.deg2rad(181.0) - - GCR1_cart = np.array([ - _lonlat_rad_to_xyz(np.deg2rad(170.0), - np.deg2rad(89.0)), - _lonlat_rad_to_xyz(np.deg2rad(170.0), - np.deg2rad(-10.0)) - ]) - - res = _angle_of_2_vectors(GCR1_cart[0], GCR1_cart[1]) - - # The angle between the two vectors should be 99 degrees - assert pytest.approx(res, abs=1e-8) == np.deg2rad(89.0+10.0) - - -def _exact_small_angle(u, v): - """Angle between two float vectors, for angles well below 1 rad. - - The cross and dot products are formed exactly in rational arithmetic, then - atan(|u x v| / (u . v)) is evaluated with a 50-digit Taylor series. - """ - from decimal import Decimal, localcontext - from fractions import Fraction - - u = [Fraction(float(c)) for c in u] - v = [Fraction(float(c)) for c in v] - cross = (u[1] * v[2] - u[2] * v[1], u[2] * v[0] - u[0] * v[2], u[0] * v[1] - u[1] * v[0]) - cross2 = sum(c * c for c in cross) - dot = sum(a * b for a, b in zip(u, v)) - with localcontext() as ctx: - ctx.prec = 50 - t = (Decimal(cross2.numerator) / Decimal(cross2.denominator)).sqrt() / ( - Decimal(dot.numerator) / Decimal(dot.denominator)) - term, total, k = t, Decimal(0), 0 - while abs(term) > Decimal(10) ** -45: - total += term / (2 * k + 1) * (-1) ** k - term *= t * t - k += 1 - return float(total) - - -def test_small_angle_of_2_vectors_special_values(): - assert _small_angle_of_2_vectors((1.0, 0.0, 0.0), (0.0, 3.0, 0.0)) == pytest.approx(np.pi / 2, rel=1e-15) - assert _small_angle_of_2_vectors((2.0, 0.0, 0.0), (0.5, 0.0, 0.0)) == 0.0 - assert _small_angle_of_2_vectors((0.0, 0.0, 1.0), (0.0, 0.0, -7.0)) == pytest.approx(np.pi, rel=1e-15) - assert _small_angle_of_2_vectors((0.0, 0.0, 0.0), (1.0, 2.0, 3.0)) == 0.0 - - -@pytest.mark.parametrize("theta, rtol", [(1e-3, 1e-14), (1e-5, 1e-14), (1e-7, 1e-14), (1e-9, 1e-13)]) -def test_small_angle_of_2_vectors_accuracy(theta, rtol): - """Nearly parallel vectors of different, arbitrary lengths. - - A naive evaluation of v|u| - u|v| loses accuracy as eps / theta (about 1e-7 - at theta = 1e-9); with compensated products it stays within a few ulps, - plus a second-order (eps / theta)^2 term from the rounded norms. - """ - rng = np.random.default_rng(0) - for _ in range(50): - u = rng.normal(size=3) - w = np.cross(u, rng.normal(size=3)) - v = u / np.linalg.norm(u) * np.cos(theta) + w / np.linalg.norm(w) * np.sin(theta) - u, v = u * rng.uniform(0.1, 10.0), v * rng.uniform(0.1, 10.0) - expected = _exact_small_angle(u, v) - assert _small_angle_of_2_vectors(u, v) == pytest.approx(expected, rel=rtol, abs=0.0) - assert _small_angle_of_2_vectors(v, u) == pytest.approx(expected, rel=rtol, abs=0.0) - def test_get_cartesian_face_edge_nodes_pipeline(): vertices = [[0.5, 0.5, 0.5], [-0.5, 0.5, 0.5], [-0.5, -0.5, 0.5], [0.5, -0.5, 0.5]] vertices = [x / np.linalg.norm(x) for x in vertices] diff --git a/test/utils/test_numba_math.py b/test/utils/test_numba_math.py index e2732043b..a9f018d75 100644 --- a/test/utils/test_numba_math.py +++ b/test/utils/test_numba_math.py @@ -16,6 +16,7 @@ _numba_norm3, _numba_dot3, _numba_cross3, + _numba_diff_of_products3, ) def test_numba_add3(): @@ -79,3 +80,16 @@ def test_numba_cross3(): assert _numba_cross3((1, 2, 30), (4, 0, 6)) == (12, 114, -8) assert _numba_cross3([0,1,0],[0,0,10]) == (10,0,0) assert _numba_cross3(np.array([1,10,0]), np.array([0,-1,5])) == (50, -5, -1) + +def test_numba_diff_of_products3(): + """ensure _numba_diff_of_products3 gives u*a - v*b accurately when the products nearly cancel.""" + from fractions import Fraction + assert _numba_diff_of_products3((1.0, 2.0, 3.0), 2.0, (0.5, 1.0, 1.0), 4.0) == ((0.0, 0.0, 2.0), (0.0, 0.0, 0.0)) + u = np.array([0.1, 0.7, -1.3]) + v = u * (1.0 + 1e-12) # nearly parallel, so u*a and v*b nearly cancel + a, b = 1.0 + 1e-12, 1.0 + hi, lo = _numba_diff_of_products3(u, a, v, b) + for i in range(3): + exact = Fraction(u[i]) * Fraction(a) - Fraction(v[i]) * Fraction(b) + assert hi[i] == u[i] * a - v[i] * b # hi alone is the naive result + assert Fraction(hi[i]) + Fraction(lo[i]) == exact # hi + lo is exact here diff --git a/uxarray/grid/utils.py b/uxarray/grid/utils.py index 6cd42981c..68cae8790 100644 --- a/uxarray/grid/utils.py +++ b/uxarray/grid/utils.py @@ -3,10 +3,12 @@ from numba import njit, prange from uxarray.constants import EDGE_NODE_SORT_THRESHOLD, INT_DTYPE, INT_FILL_VALUE -from uxarray.utils.computing import diff_of_products from uxarray.utils.numba_math import ( + _numba_add3, _numba_cross3, + _numba_diff_of_products3, _numba_dot3, + _numba_mul3_scalar, _numba_norm3, ) from uxarray.utils.parallel import parallel_njit @@ -24,8 +26,8 @@ def _small_angle_of_2_vectors(u, v): which avoids the cancellation of the arcsin/arccos forms and of the cross product in atan2(|u x v|, u . v). When the vectors are nearly parallel, every component of ``v |u| - u |v|`` is a difference of two nearly equal - products, so it is evaluated with the FMA-based ``diff_of_products`` - (Kahan's 2x2 determinant). The result is accurate to a few ulps for angles + products, so it is evaluated with the FMA-based ``_numba_diff_of_products3`` + (Kahan's 2x2 determinant per component). The result is accurate to a few ulps for angles down to about 1e-7 rad, for any input lengths; below that, the rounding of the norms adds a second-order relative error of about (eps / angle)^2. @@ -51,20 +53,17 @@ def _small_angle_of_2_vectors(u, v): algorithms for regridding on the sphere. Geoscientific Model Development, 19(14), 6545-6570. https://doi.org/10.5194/gmd-19-6545-2026 """ - u0, u1, u2 = float(u[0]), float(u[1]), float(u[2]) - v0, v1, v2 = float(v[0]), float(v[1]), float(v[2]) - nu = _numba_norm3((u0, u1, u2)) - nv = _numba_norm3((v0, v1, v2)) + nu = _numba_norm3(u) + nv = _numba_norm3(v) - # v|u| - u|v|, each component as a compensated difference of products. - m0_hi, m0_lo = diff_of_products(v0, nu, u0, nv) - m1_hi, m1_lo = diff_of_products(v1, nu, u1, nv) - m2_hi, m2_lo = diff_of_products(v2, nu, u2, nv) - m = (m0_hi + m0_lo, m1_hi + m1_lo, m2_hi + m2_lo) + # v|u| - u|v| as a compensated difference of products: m_hi alone is the + # naive rounded difference, adding m_lo applies the products' rounding errors. + m_hi, m_lo = _numba_diff_of_products3(v, nu, u, nv) + m = _numba_add3(m_hi, m_lo) # v|u| + u|v|: its cancellation near antiparallel vectors does not affect # the returned angle, so it is evaluated directly. - s = (v0 * nu + u0 * nv, v1 * nu + u1 * nv, v2 * nu + u2 * nv) + s = _numba_add3(_numba_mul3_scalar(v, nu), _numba_mul3_scalar(u, nv)) return 2.0 * np.arctan2(_numba_norm3(m), _numba_norm3(s)) diff --git a/uxarray/utils/numba_math.py b/uxarray/utils/numba_math.py index 7f75f2da2..6fcafb656 100644 --- a/uxarray/utils/numba_math.py +++ b/uxarray/utils/numba_math.py @@ -15,6 +15,8 @@ import numpy as np from numba import njit +from uxarray.utils.computing import diff_of_products + # ------- basic arithmetic with vectors ------- # @@ -114,6 +116,27 @@ def _numba_cross3(u, v): return (cx, cy, cz) +@njit(cache=True, inline="always") +def _numba_diff_of_products3(u, a, v, b): + """component-wise u * a - v * b for 3-vectors u, v and scalars a, b, without cancellation. + + Each component is evaluated with Kahan's 2x2 determinant + (:func:`uxarray.utils.computing.diff_of_products`), so it is accurate even + when ``u * a`` and ``v * b`` nearly cancel. + + Returns + ------- + hi, lo : tuple of 3 floats + High and low parts of each component; ``hi[i] + lo[i]`` is the + accurate value. ``hi`` alone equals the naive ``u * a - v * b``, so the + two must be added, e.g. with ``_numba_add3(hi, lo)``. + """ + h0, l0 = diff_of_products(u[0], a, v[0], b) + h1, l1 = diff_of_products(u[1], a, v[1], b) + h2, l2 = diff_of_products(u[2], a, v[2], b) + return (h0, h1, h2), (l0, l1, l2) + + # ------- convenience functions / helpers ------- #