Skip to content

Commit 743bb00

Browse files
committed
mathfun: consolidate import fallback
1 parent b0360bf commit 743bb00

14 files changed

+61
-82
lines changed

.flake8

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ max-line-length = 132
33
ignore = E501, W503, W504
44
exclude = .git,__pycache__,.eggs/,doc/,docs/,build/,dist/,archive/
55
per-file-ignores =
6-
__init__.py:F401, F403
6+
__init__.py:F401, F403
7+
mathfun.py:F401

src/pymap3d/ecef.py

+3-15
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,16 @@
22
from __future__ import annotations
33

44
try:
5-
from numpy import (
6-
radians,
7-
sin,
8-
cos,
9-
tan,
10-
arctan as atan,
11-
hypot,
12-
degrees,
13-
arctan2 as atan2,
14-
sqrt,
15-
finfo,
16-
where,
17-
asarray,
18-
)
5+
from numpy import finfo, where, asarray
196
from .eci import eci2ecef, ecef2eci
207
except ImportError:
21-
from math import radians, sin, cos, tan, atan, hypot, degrees, atan2, sqrt # type: ignore
8+
pass
229

2310
from math import pi
2411
from datetime import datetime
2512

2613
from .ellipsoid import Ellipsoid
14+
from .mathfun import radians, degrees, sin, cos, tan, atan2, hypot, sqrt, atan
2715
from .utils import sanitize
2816

2917

src/pymap3d/enu.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
from math import tau
55

66
try:
7-
from numpy import asarray, radians, sin, cos, hypot, arctan2 as atan2, degrees
7+
from numpy import asarray
88
except ImportError:
9-
from math import radians, sin, cos, hypot, atan2, degrees # type: ignore
9+
pass
1010

1111
from .ecef import geodetic2ecef, ecef2geodetic, enu2ecef, uvw2enu
1212
from .ellipsoid import Ellipsoid
13+
from .mathfun import radians, sin, cos, hypot, atan2, degrees
1314

1415
__all__ = ["enu2aer", "aer2enu", "enu2geodetic", "geodetic2enu"]
1516

src/pymap3d/haversine.py

+3-7
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,13 @@
99
within double precision arithmetic limitations
1010
"""
1111

12-
13-
try:
14-
from numpy import cos, arcsin, sqrt, radians, degrees
15-
except ImportError:
16-
from math import cos, sqrt, radians, degrees, asin as arcsin # type: ignore
17-
1812
try:
1913
from astropy.coordinates.angle_utilities import angular_separation
2014
except ImportError:
2115
pass
2216

17+
from .mathfun import cos, asin, sqrt, radians, degrees
18+
2319
__all__ = ["anglesep", "anglesep_meeus", "haversine"]
2420

2521

@@ -63,7 +59,7 @@ def anglesep_meeus(lon0: float, lat0: float, lon1: float, lat1: float, deg: bool
6359
lon1 = radians(lon1)
6460
lat1 = radians(lat1)
6561

66-
sep_rad = 2 * arcsin(
62+
sep_rad = 2 * asin(
6763
sqrt(haversine(lat0 - lat1) + cos(lat0) * cos(lat1) * haversine(lon0 - lon1))
6864
)
6965

src/pymap3d/latitude.py

+3-10
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,13 @@
22

33
from __future__ import annotations
44

5+
from math import pi
6+
57
from .ellipsoid import Ellipsoid
8+
from .mathfun import atan, radians, degrees, tan, sin, cos, asinh, atanh, exp, sqrt, inf
69
from .utils import sanitize, sign
710
from . import rcurve
811

9-
try:
10-
from numpy import radians, degrees, tan, sin, cos, exp, pi, sqrt, inf
11-
from numpy import arctan as atan, arcsinh as asinh, arctanh as atanh # noqa: A001
12-
13-
use_numpy = True
14-
except ImportError:
15-
from math import atan, radians, degrees, tan, sin, cos, asinh, atanh, exp, pi, sqrt, inf # type: ignore
16-
17-
use_numpy = False
18-
1912
COS_EPS = 1e-9 # tolerance for angles near abs([90, 270])
2013

2114
__all__ = [

src/pymap3d/los.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
from math import pi, nan
66

77
try:
8-
from numpy import sqrt, asarray
8+
from numpy import asarray
99
except ImportError:
10-
from math import sqrt # type: ignore
10+
pass
1111

1212
from .aer import aer2enu
1313
from .ecef import enu2uvw, geodetic2ecef, ecef2geodetic
1414
from .ellipsoid import Ellipsoid
15+
from .mathfun import sqrt
1516

1617
__all__ = ["lookAtSpheroid"]
1718

src/pymap3d/lox.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
from __future__ import annotations
44

55
try:
6-
from numpy import radians, degrees, cos, arctan2 as atan2, tan, array, broadcast_arrays
6+
from numpy import array, broadcast_arrays
77
except ImportError:
8-
from math import radians, degrees, cos, atan2, tan # type: ignore
8+
pass
99

1010
from math import pi, tau
1111

1212
from .ellipsoid import Ellipsoid
13+
from .mathfun import radians, degrees, cos, atan2, tan
1314
from .utils import sign
1415
from . import rcurve
1516
from . import rsphere

src/pymap3d/mathfun.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""
2+
import from Numpy, and if not available fallback to math stdlib
3+
"""
4+
5+
try:
6+
from numpy import (
7+
sin,
8+
cos,
9+
sqrt,
10+
exp,
11+
log,
12+
inf,
13+
isnan,
14+
radians,
15+
tan,
16+
arctan as atan,
17+
hypot,
18+
degrees,
19+
arctan2 as atan2,
20+
arcsin as asin,
21+
arcsinh as asinh,
22+
arctanh as atanh,
23+
power,
24+
)
25+
except ImportError:
26+
from math import sin, cos, sqrt, exp, log, inf, isnan, radians, tan, atan, hypot, degrees, atan2, asin, asinh, atanh # type: ignore
27+
28+
def power(x, y): # type: ignore
29+
return pow(x, y)

src/pymap3d/rcurve.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,8 @@
22

33
from __future__ import annotations
44

5-
try:
6-
from numpy import sin, cos, sqrt
7-
except ImportError:
8-
from math import sin, cos, sqrt # type: ignore
9-
105
from .ellipsoid import Ellipsoid
6+
from .mathfun import cos, sin, sqrt
117
from .utils import sanitize
128

139
__all__ = ["parallel", "meridian", "transverse", "geocentric_radius"]

src/pymap3d/rsphere.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
from __future__ import annotations
44

55
try:
6-
from numpy import radians, sin, cos, log, sqrt, degrees, asarray
6+
from numpy import asarray
77
except ImportError:
8-
from math import radians, sin, cos, log, sqrt, degrees # type: ignore
8+
pass
99

1010
from .ellipsoid import Ellipsoid
11+
from .mathfun import radians, sin, cos, log, sqrt, degrees
1112
from . import rcurve
1213
from .vincenty import vdist
1314

src/pymap3d/spherical.py

+2-15
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,7 @@
55
"""
66
from __future__ import annotations
77

8-
try:
9-
from numpy import ( # noqa: A001
10-
radians,
11-
sin,
12-
arcsin as asin,
13-
hypot,
14-
degrees,
15-
arctan2 as atan2,
16-
sqrt,
17-
power as pow,
18-
)
19-
except ImportError:
20-
from math import radians, sin, hypot, degrees, atan2, asin, sqrt # type: ignore
21-
8+
from .mathfun import radians, sin, hypot, degrees, atan2, asin, sqrt, power
229

2310
from .ellipsoid import Ellipsoid
2411
from .utils import sanitize, cbrt
@@ -148,7 +135,7 @@ def spherical2geodetic(
148135
coslat = sqrt(1 - sinlat**2)
149136

150137
Z = radius * sinlat
151-
p_0 = pow(radius, 2) * coslat**2 / ell.semimajor_axis**2
138+
p_0 = power(radius, 2) * coslat**2 / ell.semimajor_axis**2
152139
q_0 = (1 - ell.eccentricity**2) / ell.semimajor_axis**2 * Z**2
153140
r_0 = (p_0 + q_0 - ell.eccentricity**4) / 6
154141
s_0 = ell.eccentricity**4 * p_0 * q_0 / 4 / r_0**3

src/pymap3d/utils.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
from math import pi
77

88
from .ellipsoid import Ellipsoid
9+
from .mathfun import atan2, hypot, cos, sin, radians
910

1011
try:
11-
from numpy import hypot, cos, sin, arctan2 as atan2, radians, asarray, sign, cbrt
12+
from numpy import asarray, sign, cbrt
1213
except ImportError:
13-
from math import atan2, hypot, cos, sin, radians # type: ignore
1414

1515
def sign(x) -> float: # type: ignore
1616
"""signum function"""

src/pymap3d/vallado.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@
99
from __future__ import annotations
1010
from datetime import datetime
1111

12-
try:
13-
from numpy import sin, cos, degrees, radians, arcsin as asin, arctan2 as atan2
14-
except ImportError:
15-
from math import sin, cos, degrees, radians, asin, atan2 # type: ignore
16-
12+
from .mathfun import sin, cos, degrees, radians, asin, atan2
1713
from .sidereal import datetime2sidereal
1814

1915
__all__ = ["azel2radec", "radec2azel"]

src/pymap3d/vincenty.py

+3-14
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,11 @@
99
from copy import copy
1010

1111
try:
12-
from numpy import (
13-
atleast_1d,
14-
sqrt,
15-
tan,
16-
sin,
17-
cos,
18-
isnan,
19-
arctan as atan,
20-
arctan2 as atan2,
21-
arcsin as asin,
22-
radians,
23-
degrees,
24-
)
12+
from numpy import atleast_1d
2513
except ImportError:
26-
from math import sqrt, tan, sin, cos, isnan, atan, atan2, asin, radians, degrees # type: ignore
14+
pass
2715

16+
from .mathfun import sqrt, tan, sin, cos, isnan, atan, atan2, asin, radians, degrees
2817
from .ellipsoid import Ellipsoid
2918
from .utils import sign
3019

0 commit comments

Comments
 (0)