Skip to content

Commit 5d4234a

Browse files
committed
speed by removing unneeded explicit broadcasts
1 parent cdfa697 commit 5d4234a

5 files changed

Lines changed: 28 additions & 55 deletions

File tree

src/pymap3d/eci.py

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def eci2ecef(
6060
else:
6161
raise ImportError("eci2ecef requires either Numpy or Astropy")
6262

63-
return xe.squeeze()[()], ye.squeeze()[()], ze.squeeze()[()]
63+
return xe, ye, ze
6464

6565

6666
def eci2ecef_astropy(
@@ -89,23 +89,15 @@ def eci2ecef_numpy(x, y, z, t: datetime) -> tuple:
8989
see eci2ecef() for description
9090
"""
9191

92-
x = np.atleast_1d(x)
93-
y = np.atleast_1d(y)
94-
z = np.atleast_1d(z)
95-
gst = np.atleast_1d(greenwichsrt(juliandate(t)))
96-
assert x.shape == y.shape == z.shape
97-
if gst.size == 1 and x.size != 1:
98-
gst = np.broadcast_to(gst, x.shape[0])
99-
assert x.size == gst.size
92+
gst = greenwichsrt(juliandate(t))
10093

10194
c = np.cos(gst)
10295
s = np.sin(gst)
10396

104-
x_ecef = (c * x.ravel() + s * y.ravel()).reshape(x.shape)
105-
y_ecef = (-s * x.ravel() + c * y.ravel()).reshape(y.shape)
106-
z_ecef = z.reshape(z.shape)
97+
x_ecef = c * x + s * y
98+
y_ecef = -s * x + c * y
10799

108-
return x_ecef.squeeze()[()], y_ecef.squeeze()[()], z_ecef.squeeze()[()]
100+
return x_ecef, y_ecef, z
109101

110102

111103
def ecef2eci(
@@ -168,21 +160,13 @@ def ecef2eci_numpy(x, y, z, t: datetime) -> tuple:
168160
"""ecef2eci using Numpy
169161
see ecef2eci() for description
170162
"""
171-
x = np.atleast_1d(x)
172-
y = np.atleast_1d(y)
173-
z = np.atleast_1d(z)
174-
gst = np.atleast_1d(greenwichsrt(juliandate(t)))
175163

176-
assert x.shape == y.shape == z.shape
177-
if gst.size == 1 and x.size != 1:
178-
gst = np.broadcast_to(gst, x.shape[0])
179-
assert x.size == gst.size
164+
gst = greenwichsrt(juliandate(t))
180165

181166
c = np.cos(gst)
182167
s = np.sin(gst)
183168

184-
x_eci = (c * x.ravel() - s * y.ravel()).reshape(x.shape)
185-
y_eci = (s * x.ravel() + c * y.ravel()).reshape(y.shape)
186-
z_eci = z.reshape(z.shape)
169+
x_eci = c * x - s * y
170+
y_eci = s * x + c * y
187171

188-
return x_eci.squeeze()[()], y_eci.squeeze()[()], z_eci.squeeze()[()]
172+
return x_eci, y_eci, z

src/pymap3d/tests/test_vincenty.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ def test_track2_unit(deg):
1010

1111
lat1, lon1 = 0.0, 80.0
1212
lat2, lon2 = 0.0, 81.0
13-
lat0 = [0.0, 0.0, 0.0, 0.0]
14-
lon0 = [80.0, 80.33333, 80.66666, 81.0]
13+
lat0 = np.array([0.0, 0.0, 0.0, 0.0])
14+
lon0 = np.array([80.0, 80.33333, 80.66666, 81.0])
1515
if not deg:
1616
lat1, lon1, lat2, lon2 = np.radians((lat1, lon1, lat2, lon2))
1717
lat0 = np.radians(lat0)

src/pymap3d/tests/test_vincenty_dist.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@ def test_unit_vdist(lat, lon, lat1, lon1, srange, az):
3131

3232

3333
def test_vector():
34-
pytest.importorskip("numpy")
35-
asr, aaz = vincenty.vdist(10, 20, [10.02137267, 10.01917819], [20.0168471, 20.0193493])
34+
np = pytest.importorskip("numpy")
35+
lat2 = np.array([10.02137267, 10.01917819])
36+
lon2 = np.array([20.0168471, 20.0193493])
37+
asr, aaz = vincenty.vdist(10, 20, lat2, lon2)
3638

3739
assert 3e3 == approx(asr)
3840
assert aaz == approx([38, 45])

src/pymap3d/tests/test_vincenty_vreckon.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,18 @@ def test_vreckon_unit(deg, lat, lon, srange, az, lato, lono):
4444

4545

4646
def test_az_vector():
47-
pytest.importorskip("numpy")
48-
a, b = vincenty.vreckon(*ll0, sr1[0], az1)
47+
np =pytest.importorskip("numpy")
48+
az = np.array(az1)
49+
a, b = vincenty.vreckon(*ll0, sr1[0], az)
4950
assert a == approx(lat2)
5051
assert b == approx(lon2)
5152

5253

5354
def test_both_vector():
54-
pytest.importorskip("numpy")
55-
a, b = vincenty.vreckon(10, 20, sr1, az1)
55+
np = pytest.importorskip("numpy")
56+
sr = np.array(sr1)
57+
az = np.array(az1)
58+
59+
a, b = vincenty.vreckon(10, 20, sr, az)
5660
assert a == approx(lat3)
5761
assert b == approx(lon3)

src/pymap3d/vincenty.py

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@
1010
from copy import copy
1111
from math import nan, pi, tau
1212

13-
try:
14-
from numpy import atleast_1d
15-
except ImportError:
16-
pass
17-
1813
from .ellipsoid import Ellipsoid
1914
from .mathfun import (
2015
asin,
@@ -106,15 +101,6 @@ def vdist(Lat1, Lon1, Lat2, Lon2, ell: Ellipsoid | None = None, deg: bool = True
106101
12. No warranties; use at your own risk.
107102
"""
108103

109-
# %% Input check:
110-
try:
111-
Lat1 = atleast_1d(Lat1)
112-
Lon1 = atleast_1d(Lon1)
113-
Lat2 = atleast_1d(Lat2)
114-
Lon2 = atleast_1d(Lon2)
115-
except NameError:
116-
pass
117-
118104
if ell is None:
119105
ell = Ellipsoid.from_name("wgs84")
120106
# %% Supply WGS84 earth ellipsoid axis lengths in meters:
@@ -141,13 +127,14 @@ def vdist(Lat1, Lon1, Lat2, Lon2, ell: Ellipsoid | None = None, deg: bool = True
141127
try:
142128
i = abs(pi / 2 - abs(lat1)) < 1e-10
143129
lat1[i] = sign(lat1[i]) * (pi / 2 - 1e-10)
144-
145-
i = abs(pi / 2 - abs(lat2)) < 1e-10
146-
lat2[i] = sign(lat2[i]) * (pi / 2 - 1e-10)
147130
except TypeError:
148131
if abs(pi / 2 - abs(lat1)) < 1e-10:
149132
lat1 = sign(lat1) * (pi / 2 - 1e-10)
150133

134+
try:
135+
i = abs(pi / 2 - abs(lat2)) < 1e-10
136+
lat2[i] = sign(lat2[i]) * (pi / 2 - 1e-10)
137+
except TypeError:
151138
if abs(pi / 2 - abs(lat2)) < 1e-10:
152139
lat2 = sign(lat2) * (pi / 2 - 1e-10)
153140

@@ -345,13 +332,9 @@ def vreckon(Lat1, Lon1, Rng, Azim, ell: Ellipsoid | None = None, deg: bool = Tru
345332
"""
346333

347334
try:
348-
Lat1 = atleast_1d(Lat1)
349-
Lon1 = atleast_1d(Lon1)
350-
Rng = atleast_1d(Rng)
351-
Azim = atleast_1d(Azim)
352335
if (Rng < 0.0).any():
353336
raise ValueError("Ground distance must be positive")
354-
except NameError:
337+
except AttributeError:
355338
if Rng < 0.0:
356339
raise ValueError("Ground distance must be positive")
357340

0 commit comments

Comments
 (0)