Skip to content

Commit 13cb849

Browse files
committed
Use np.hypot whereever possible.
Except examples, where it may be too obscure? Also added -T to CI sphinx-build invocation, to help troubleshooting e.g. https://circleci.com/gh/anntzer/matplotlib/2505?utm_campaign=vcs-integration-link&utm_medium=referral&utm_source=github-build-link (show traceback on sphinx failure).
1 parent fb4b612 commit 13cb849

File tree

23 files changed

+68
-82
lines changed

23 files changed

+68
-82
lines changed

.circleci/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ mpl-run: &mpl-install
6868

6969
doc-run: &doc-build
7070
name: Build documentation
71-
command: make html
71+
command: make html O=-T
7272
working_directory: doc
7373

7474
doc-bundle-run: &doc-bundle

examples/event_handling/pick_event_demo.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ def line_picker(line, mouseevent):
122122
xdata = line.get_xdata()
123123
ydata = line.get_ydata()
124124
maxd = 0.05
125-
d = np.sqrt((xdata - mouseevent.xdata)**2. + (ydata - mouseevent.ydata)**2.)
125+
d = np.sqrt(
126+
(xdata - mouseevent.xdata)**2 + (ydata - mouseevent.ydata)**2)
126127

127128
ind = np.nonzero(np.less_equal(d, maxd))
128129
if len(ind):

examples/images_contours_and_fields/barb_demo.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@
2828

2929
# Arbitrary set of vectors, make them longer and change the pivot point
3030
# (point around which they're rotated) to be the middle
31-
axs1[0, 1].barbs(data['x'], data['y'], data['u'], data['v'], length=8, pivot='middle')
31+
axs1[0, 1].barbs(
32+
data['x'], data['y'], data['u'], data['v'], length=8, pivot='middle')
3233

3334
# Showing colormapping with uniform grid. Fill the circle for an empty barb,
3435
# don't round the values, and change some of the size parameters
35-
axs1[1, 0].barbs(X, Y, U, V, np.sqrt(U * U + V * V), fill_empty=True, rounding=False,
36-
sizes=dict(emptybarb=0.25, spacing=0.2, height=0.3))
36+
axs1[1, 0].barbs(
37+
X, Y, U, V, np.sqrt(U ** 2 + V ** 2), fill_empty=True, rounding=False,
38+
sizes=dict(emptybarb=0.25, spacing=0.2, height=0.3))
3739

3840
# Change colors as well as the increments for parts of the barbs
3941
axs1[1, 1].barbs(data['x'], data['y'], data['u'], data['v'], flagcolor='r',

examples/images_contours_and_fields/contourf_demo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
Z[:nr // 6, :nc // 6] = np.ma.masked
3131

3232
# mask a circle in the middle:
33-
interior = np.sqrt((X**2) + (Y**2)) < 0.5
33+
interior = np.sqrt(X**2 + Y**2) < 0.5
3434
Z[interior] = np.ma.masked
3535

3636
# We are using automatic selection of contour levels;

examples/images_contours_and_fields/plot_streamplot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
Y, X = np.mgrid[-w:w:100j, -w:w:100j]
2121
U = -1 - X**2 + Y
2222
V = 1 + X - Y**2
23-
speed = np.sqrt(U*U + V*V)
23+
speed = np.sqrt(U**2 + V**2)
2424

2525
fig = plt.figure(figsize=(7, 9))
2626
gs = gridspec.GridSpec(nrows=3, ncols=2, height_ratios=[1, 1, 2])

examples/lines_bars_and_markers/scatter_masked.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
y = 0.9 * np.random.rand(N)
2121
area = np.pi * (10 * np.random.rand(N))**2 # 0 to 10 point radii
2222
c = np.sqrt(area)
23-
r = np.sqrt(x * x + y * y)
23+
r = np.sqrt(x ** 2 + y ** 2)
2424
area1 = np.ma.masked_where(r < r0, area)
2525
area2 = np.ma.masked_where(r >= r0, area)
2626
plt.scatter(x, y, s=area1, marker='^', c=c)

lib/matplotlib/contour.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -232,11 +232,9 @@ def print_label(self, linecontour, labelwidth):
232232

233233
def too_close(self, x, y, lw):
234234
"Return *True* if a label is already near this location."
235-
for loc in self.labelXYs:
236-
d = np.sqrt((x - loc[0]) ** 2 + (y - loc[1]) ** 2)
237-
if d < 1.2 * lw:
238-
return True
239-
return False
235+
thresh = (1.2 * lw) ** 2
236+
return any((x - loc[0]) ** 2 + (y - loc[1]) ** 2 < thresh
237+
for loc in self.labelXYs)
240238

241239
def get_label_coords(self, distances, XX, YY, ysize, lw):
242240
"""

lib/matplotlib/lines.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -481,17 +481,16 @@ def contains(self, mouseevent):
481481
else:
482482
pixels = self.figure.dpi / 72. * self.pickradius
483483

484-
# the math involved in checking for containment (here and inside of
485-
# segment_hits) assumes that it is OK to overflow. In case the
486-
# application has set the error flags such that an exception is raised
487-
# on overflow, we temporarily set the appropriate error flags here and
488-
# set them back when we are finished.
484+
# The math involved in checking for containment (here and inside of
485+
# segment_hits) assumes that it is OK to overflow, so temporarily set
486+
# the error flags accordingly.
489487
with np.errstate(all='ignore'):
490488
# Check for collision
491489
if self._linestyle in ['None', None]:
492490
# If no line, return the nearby point(s)
493-
d = (xt - mouseevent.x) ** 2 + (yt - mouseevent.y) ** 2
494-
ind, = np.nonzero(np.less_equal(d, pixels ** 2))
491+
ind, = np.nonzero(
492+
(xt - mouseevent.x) ** 2 + (yt - mouseevent.y) ** 2
493+
<= pixels ** 2)
495494
else:
496495
# If line, return the nearby segment(s)
497496
ind = segment_hits(mouseevent.x, mouseevent.y, xt, yt, pixels)

lib/matplotlib/patches.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1338,10 +1338,8 @@ def get_path(self):
13381338
xb1, yb1, xb2, yb2 = self.getpoints(x1, y1, x2, y2, k1)
13391339

13401340
# a point on the segment 20% of the distance from the tip to the base
1341-
theta = math.atan2(y2 - y1, x2 - x1)
1342-
r = math.sqrt((y2 - y1) ** 2. + (x2 - x1) ** 2.)
1343-
xm = x1 + self.frac * r * math.cos(theta)
1344-
ym = y1 + self.frac * r * math.sin(theta)
1341+
xm = x1 + self.frac * (x2 - x1)
1342+
ym = y1 + self.frac * (y2 - y1)
13451343
xc1, yc1, xc2, yc2 = self.getpoints(x1, y1, xm, ym, k1)
13461344
xd1, yd1, xd2, yd2 = self.getpoints(x1, y1, xm, ym, k2)
13471345

@@ -2899,10 +2897,10 @@ def connect(self, posA, posB):
28992897
codes.append(Path.LINETO)
29002898
else:
29012899
dx1, dy1 = x1 - cx, y1 - cy
2902-
d1 = (dx1 ** 2 + dy1 ** 2) ** .5
2900+
d1 = np.hypot(dx1, dy1)
29032901
f1 = self.rad / d1
29042902
dx2, dy2 = x2 - cx, y2 - cy
2905-
d2 = (dx2 ** 2 + dy2 ** 2) ** .5
2903+
d2 = np.hypot(dx2, dy2)
29062904
f2 = self.rad / d2
29072905
vertices.extend([(cx + dx1 * f1, cy + dy1 * f1),
29082906
(cx, cy),
@@ -3295,7 +3293,7 @@ def transmute(self, path, mutation_size, linewidth):
32953293

32963294
head_length = self.head_length * mutation_size
32973295
head_width = self.head_width * mutation_size
3298-
head_dist = math.sqrt(head_length ** 2 + head_width ** 2)
3296+
head_dist = np.hypot(head_length, head_width)
32993297
cos_t, sin_t = head_length / head_dist, head_width / head_dist
33003298

33013299
# begin arrow

lib/matplotlib/projections/geo.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -465,15 +465,12 @@ def transform_non_affine(self, ll):
465465
diff_long = longitude - clong
466466
cos_diff_long = np.cos(diff_long)
467467

468-
inner_k = (1.0 +
469-
np.sin(clat)*sin_lat +
470-
np.cos(clat)*cos_lat*cos_diff_long)
471-
# Prevent divide-by-zero problems
472-
inner_k = np.where(inner_k == 0.0, 1e-15, inner_k)
473-
k = np.sqrt(2.0 / inner_k)
474-
x = k*cos_lat*np.sin(diff_long)
475-
y = k*(np.cos(clat)*sin_lat -
476-
np.sin(clat)*cos_lat*cos_diff_long)
468+
inner_k = np.maximum( # Prevent divide-by-zero problems
469+
1 + np.sin(clat)*sin_lat + np.cos(clat)*cos_lat*cos_diff_long,
470+
1e-15)
471+
k = np.sqrt(2 / inner_k)
472+
x = k * cos_lat*np.sin(diff_long)
473+
y = k * (np.cos(clat)*sin_lat - np.sin(clat)*cos_lat*cos_diff_long)
477474

478475
return np.concatenate((x, y), 1)
479476
transform_non_affine.__doc__ = Transform.transform_non_affine.__doc__
@@ -497,8 +494,7 @@ def transform_non_affine(self, xy):
497494
y = xy[:, 1:2]
498495
clong = self._center_longitude
499496
clat = self._center_latitude
500-
p = np.sqrt(x*x + y*y)
501-
p = np.where(p == 0.0, 1e-9, p)
497+
p = np.max(np.hypot(x, y), 1e-9)
502498
c = 2.0 * np.arcsin(0.5 * p)
503499
sin_c = np.sin(c)
504500
cos_c = np.cos(c)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy