Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/sphstat/descriptives.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def rotationmatrix_withaxis(ua: np.array, ub: np.array) -> np.ndarray:
ca /= np.linalg.norm(ca)
B = ub.reshape((3, 1)) @ ca.reshape((1, 3))
B -= B.T
th = np.arccos(ab)
th = np.arccos(np.clip(ab, -1.0, 1.0))
A = np.eye(3) + np.sin(th) * B + (np.cos(th) - 1) * ((ub.reshape((3, 1)) @ ub.reshape((1, 3))) + ca.reshape((3, 1))
@ ca.reshape((1, 3)))
return A
Expand Down Expand Up @@ -381,7 +381,7 @@ def errmin(thph, sample):
pts = sample['points']
err = 0
for pt in pts:
err += np.arccos(np.dot(pt, x))
err += np.arccos(np.clip(np.dot(pt, x), -1.0, 1.0))
return err
# We will use Nelder-Mead to minimise the arc lengths
res = minimize(errmin, np.array([ms[0], ms[1]]), args = (sample), method='Nelder-Mead', tol=1e-6)
Expand Down
4 changes: 2 additions & 2 deletions src/sphstat/distributions.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ def watson(numsamp: int, lamb: float, mu: float, nu: float, kappa: float) -> dic
U = rng.uniform(0, 1, 1)
V = rng.uniform(0, 1, 1)
S = 1 / kappa * np.log(U / C + 1)
the = np.arccos(S)
the = np.arccos(np.clip(S, -1.0, 1.0))
phi = 2 * np.pi * rng.uniform(0, 1, 1)
sample['tetas'].append(the)
sample['phis'].append(phi)
Expand All @@ -310,7 +310,7 @@ def watson(numsamp: int, lamb: float, mu: float, nu: float, kappa: float) -> dic
U = rng.uniform(0, 1, 1)
V = rng.uniform(0, 1, 1)
S = (1 / c1) * np.tan(c2 * U)
the = np.arccos(S)
the = np.arccos(np.clip(S, -1.0, 1.0))
phi = 2 * np.pi * rng.uniform(0, 1, 1)
sample['tetas'].append(the)
sample['phis'].append(phi)
Expand Down
2 changes: 1 addition & 1 deletion src/sphstat/twosample.py
Original file line number Diff line number Diff line change
Expand Up @@ -898,5 +898,5 @@ def errors(samplecart: dict, srcpos: tuple) -> list:
errs = []
srcvec = sph2cart(srcpos[0], srcpos[1])
for pt in samplecart['points']:
errs.append(np.arccos(np.dot(pt, srcvec)))
errs.append(np.arccos(np.clip(np.dot(pt, srcvec), -1.0, 1.0)))
return errs
4 changes: 2 additions & 2 deletions src/sphstat/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ def angle(pt1: np.ndarray, pt2: np.ndarray) -> float:
assert type(pt2) == np.ndarray
assert np.isclose(np.linalg.norm(pt1), 1)
assert np.isclose(np.linalg.norm(pt2), 1)
return np.arccos(np.dot(pt1, pt2))
return np.arccos(np.clip(np.dot(pt1, pt2), -1.0, 1.0))


def cart2sph(pt: np.array) -> tuple:
Expand All @@ -270,7 +270,7 @@ def cart2sph(pt: np.array) -> tuple:
:rtype: tuple
"""
assert np.isclose(np.linalg.norm(pt), 1)
th = np.arccos(pt[2])
th = np.arccos(np.clip(pt[2], -1.0, 1.0))
if pt[1] == 0:
ph = 0
return th, ph
Expand Down