Skip to content

Commit 2f22488

Browse files
authored
gh-912: use math.pi (#913)
1 parent 61f4e5b commit 2f22488

File tree

3 files changed

+22
-20
lines changed

3 files changed

+22
-20
lines changed

glass/points.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ def displace(
498498
# cosθ cos|α| + sinθ sin|α| cosγ)
499499
# δ = arctan2(sin|α| sinγ, sinθ cos|α| - cosθ sin|α| cosγ)
500500

501-
t = xp.asarray(lat) / 180 * xp.pi
501+
t = xp.asarray(lat) / 180 * math.pi
502502
ct, st = xp.sin(t), xp.cos(t) # sin and cos flipped: lat not co-lat
503503

504504
a = xp.hypot(alpha1, alpha2) # abs(alpha)
@@ -511,7 +511,7 @@ def displace(
511511

512512
d = xp.atan2(sa * sg, st * ca - ct * sa * cg)
513513

514-
return lon - d / xp.pi * 180, tp / xp.pi * 180
514+
return lon - d / math.pi * 180, tp / math.pi * 180
515515

516516

517517
def displacement(
@@ -551,9 +551,9 @@ def displacement(
551551
use_compat=False,
552552
)
553553

554-
a = (90.0 - to_lat) / 180 * xp.pi
555-
b = (90.0 - from_lat) / 180 * xp.pi
556-
g = (from_lon - to_lon) / 180 * xp.pi
554+
a = (90.0 - to_lat) / 180 * math.pi
555+
b = (90.0 - from_lat) / 180 * math.pi
556+
g = (from_lon - to_lon) / 180 * math.pi
557557

558558
sa, ca = xp.sin(a), xp.cos(a)
559559
sb, cb = xp.sin(b), xp.cos(b)

glass/shapes.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
from __future__ import annotations
2626

27+
import math
2728
from typing import TYPE_CHECKING
2829

2930
import array_api_compat
@@ -98,7 +99,7 @@ def triaxial_axis_ratio(
9899
cos2_theta = rng.uniform(low=-1.0, high=1.0, size=size)
99100
cos2_theta *= cos2_theta
100101
sin2_theta = 1 - cos2_theta
101-
cos2_phi = xp.cos(rng.uniform(low=0.0, high=2 * xp.pi, size=size))
102+
cos2_phi = xp.cos(rng.uniform(low=0.0, high=2 * math.pi, size=size))
102103
cos2_phi *= cos2_phi
103104
sin2_phi = 1 - cos2_phi
104105

@@ -207,7 +208,7 @@ def ellipticity_ryden04( # noqa: PLR0913
207208
q = triaxial_axis_ratio(zeta, xi, rng=rng)
208209

209210
# assemble ellipticity with random complex phase
210-
e = xp.exp(1j * rng.uniform(0, 2 * xp.pi, size=q.shape))
211+
e = xp.exp(1j * rng.uniform(0, 2 * math.pi, size=q.shape))
211212
e *= (1 - q) / (1 + q)
212213

213214
# return the ellipticity

tests/core/test_points.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import math
34
from typing import TYPE_CHECKING
45

56
import healpix
@@ -339,7 +340,7 @@ def test_position_weights(
339340
def test_displace_arg_complex(compare: type[Compare], xp: ModuleType) -> None:
340341
"""Test displace function with complex-valued displacement."""
341342
d = 5.0 # deg
342-
r = d / 180 * xp.pi
343+
r = d / 180 * math.pi
343344

344345
# displace the origin so everything is easy
345346
lon0 = xp.asarray(0.0)
@@ -365,7 +366,7 @@ def test_displace_arg_complex(compare: type[Compare], xp: ModuleType) -> None:
365366
def test_displace_arg_real(compare: type[Compare], xp: ModuleType) -> None:
366367
"""Test displace function with real-valued argument."""
367368
d = 5.0 # deg
368-
r = d / 180 * xp.pi
369+
r = d / 180 * math.pi
369370

370371
# displace the origin so everything is easy
371372
lon0 = xp.asarray(0.0)
@@ -395,17 +396,17 @@ def test_displace_abs(
395396
) -> None:
396397
"""Check that points are displaced by the correct angular distance."""
397398
n = 1_000
398-
abs_alpha = urng.uniform(0, 2 * xp.pi, size=n)
399-
arg_alpha = urng.uniform(-xp.pi, xp.pi, size=n)
399+
abs_alpha = urng.uniform(0, 2 * math.pi, size=n)
400+
arg_alpha = urng.uniform(-math.pi, math.pi, size=n)
400401

401-
lon_ = urng.uniform(-xp.pi, xp.pi, size=n) / xp.pi * 180
402-
lat_ = xp.asin(urng.uniform(-1, 1, size=n)) / xp.pi * 180
402+
lon_ = urng.uniform(-math.pi, math.pi, size=n) / math.pi * 180
403+
lat_ = xp.asin(urng.uniform(-1, 1, size=n)) / math.pi * 180
403404

404405
lon, lat = glass.displace(lon_, lat_, abs_alpha * xp.exp(1j * arg_alpha))
405406

406-
th = (90.0 - lat) / 180 * xp.pi
407-
th_ = (90.0 - lat_) / 180 * xp.pi
408-
delta = (lon - lon_) / 180 * xp.pi
407+
th = (90.0 - lat) / 180 * math.pi
408+
th_ = (90.0 - lat_) / 180 * math.pi
409+
delta = (lon - lon_) / 180 * math.pi
409410

410411
cos_a = xp.cos(th) * xp.cos(th_) + xp.cos(delta) * xp.sin(th) * xp.sin(th_)
411412

@@ -419,11 +420,11 @@ def test_displacement(
419420
) -> None:
420421
"""Check that displacement of points is computed correctly."""
421422
# unit changes for displacements
422-
deg5 = xp.asarray(5.0) / 180 * xp.pi
423+
deg5 = xp.asarray(5.0) / 180 * math.pi
423424
north = xp.exp(xp.asarray(1j * 0.0))
424-
east = xp.exp(xp.asarray(1j * (xp.pi / 2)))
425-
south = xp.exp(xp.asarray(1j * xp.pi))
426-
west = xp.exp(xp.asarray(1j * (3 * xp.pi / 2)))
425+
east = xp.exp(xp.asarray(1j * (math.pi / 2)))
426+
south = xp.exp(xp.asarray(1j * math.pi))
427+
west = xp.exp(xp.asarray(1j * (3 * math.pi / 2)))
427428

428429
zero = xp.asarray(0.0)
429430
five = xp.asarray(5.0)

0 commit comments

Comments
 (0)