Skip to content

Commit b7b313a

Browse files
committed
MAINT: circular distance
1 parent 886e1fe commit b7b313a

1 file changed

Lines changed: 17 additions & 1 deletion

File tree

refellips/objectiveSE.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,22 @@
1818
from refnx._lib import flatten
1919

2020

21+
def circular_distance(angle1, angle2, period=2*np.pi):
22+
"""
23+
Calculates the circular distance between two angles.
24+
25+
Args:
26+
angle1 (float or np.ndarray): The first angle(s) in radians.
27+
angle2 (float or np.ndarray): The second angle(s) in radians.
28+
period (float): The period of the circular domain (e.g., 2*np.pi for full circle).
29+
30+
Returns:
31+
float or np.ndarray: The shortest circular distance between the angles.
32+
"""
33+
diff = np.abs(angle1 - angle2)
34+
return np.minimum(diff, period - diff)
35+
36+
2137
class ObjectiveSE(BaseObjective):
2238
"""
2339
Objective function for using with curvefitters such as
@@ -179,7 +195,7 @@ def residuals(self, pvals=None):
179195
wavelength, aoi, psi_d, delta_d = self.data.data
180196
wavelength_aoi = np.c_[wavelength, aoi]
181197
psi, delta = self.model(wavelength_aoi)
182-
delta_err = (delta - delta_d + 180) % 360 - 180
198+
delta_err = circular_distance(delta, delta_d, period=360)
183199
return np.r_[psi - psi_d, delta_err]
184200

185201
def chisqr(self, pvals=None):

0 commit comments

Comments
 (0)