Skip to content

Commit 511ab3e

Browse files
committed
Use np.hypot whereever possible.
Clearer IMO, although perhaps not for the examples?
1 parent 4872b79 commit 511ab3e

37 files changed

+82
-108
lines changed

examples/event_handling/ginput_manual_clabel_sgskip.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def tellme(s):
7171
def f(x, y, pts):
7272
z = np.zeros_like(x)
7373
for p in pts:
74-
z = z + 1/(np.sqrt((x - p[0])**2 + (y - p[1])**2))
74+
z = z + 1 / np.hypot(x - p[0], y - p[1])
7575
return 1/z
7676

7777

examples/event_handling/path_editor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def get_ind_under_point(self, event):
9090
xy = np.asarray(self.pathpatch.get_path().vertices)
9191
xyt = self.pathpatch.get_transform().transform(xy)
9292
xt, yt = xyt[:, 0], xyt[:, 1]
93-
d = np.sqrt((xt - event.x)**2 + (yt - event.y)**2)
93+
d = np.hypot(xt - event.x, yt - event.y)
9494
ind = d.argmin()
9595

9696
if d[ind] >= self.epsilon:

examples/event_handling/pick_event_demo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def line_picker(line, mouseevent):
123123
xdata = line.get_xdata()
124124
ydata = line.get_ydata()
125125
maxd = 0.05
126-
d = np.sqrt((xdata - mouseevent.xdata)**2. + (ydata - mouseevent.ydata)**2.)
126+
d = np.hypot(xdata - mouseevent.xdata, ydata - mouseevent.ydata)
127127

128128
ind = np.nonzero(np.less_equal(d, maxd))
129129
if len(ind):

examples/images_contours_and_fields/barb_demo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
# Showing colormapping with uniform grid. Fill the circle for an empty barb,
3535
# don't round the values, and change some of the size parameters
3636
ax = plt.subplot(2, 2, 3)
37-
ax.barbs(X, Y, U, V, np.sqrt(U * U + V * V), fill_empty=True, rounding=False,
37+
ax.barbs(X, Y, U, V, np.hypot(U, V), fill_empty=True, rounding=False,
3838
sizes=dict(emptybarb=0.25, spacing=0.2, height=0.3))
3939

4040
# Change colors as well as the increments for parts of the barbs

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.hypot(X, Y) < 0.5
3434
Z[interior] = np.ma.masked
3535

3636
# We are using automatic selection of contour levels;

examples/images_contours_and_fields/image_nonuniform.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
y = np.linspace(-4, 4, 9)
2525

26-
z = np.sqrt(x[np.newaxis, :]**2 + y[:, np.newaxis]**2)
26+
z = np.hypot(x[np.newaxis, :], y[:, np.newaxis])
2727

2828
fig, axs = plt.subplots(nrows=2, ncols=2)
2929
fig.subplots_adjust(bottom=0.07, hspace=0.3)

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.hypot(U, V)
2424

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

examples/images_contours_and_fields/quadmesh_demo.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
X, Y = np.meshgrid(x, y)
2121
Qx = np.cos(Y) - np.cos(X)
2222
Qz = np.sin(Y) + np.sin(X)
23-
Qx = (Qx + 1.1)
24-
Z = np.sqrt(X**2 + Y**2) / 5
25-
Z = (Z - Z.min()) / (Z.max() - Z.min())
23+
Qx = Qx + 1.1
24+
Z = np.hypot(X, Y) / 5
25+
Z = (Z - Z.min()) / Z.ptp()
2626

2727
# The color array can include masked values:
2828
Zm = ma.masked_where(np.abs(Qz) < 0.5 * np.max(Qz), Z)

examples/images_contours_and_fields/tricontour_smooth_delaunay.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@
3636
def experiment_res(x, y):
3737
""" An analytic function representing experiment results """
3838
x = 2. * x
39-
r1 = np.sqrt((0.5 - x)**2 + (0.5 - y)**2)
39+
r1 = np.hypot(0.5 - x, 0.5 - y)
4040
theta1 = np.arctan2(0.5 - x, 0.5 - y)
41-
r2 = np.sqrt((-x - 0.2)**2 + (-y - 0.2)**2)
41+
r2 = np.hypot(-x - 0.2, -y - 0.2)
4242
theta2 = np.arctan2(-x - 0.2, -y - 0.2)
4343
z = (4 * (np.exp((r1 / 10)**2) - 1) * 30. * np.cos(3 * theta1) +
4444
(np.exp((r2 / 10)**2) - 1) * 30. * np.cos(5 * theta2) +

examples/images_contours_and_fields/tricontour_smooth_user.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
#-----------------------------------------------------------------------------
1818
def function_z(x, y):
1919
""" A function of 2 variables """
20-
r1 = np.sqrt((0.5 - x)**2 + (0.5 - y)**2)
20+
r1 = np.hypot(0.5 - x, 0.5 - y)
2121
theta1 = np.arctan2(0.5 - x, 0.5 - y)
22-
r2 = np.sqrt((-x - 0.2)**2 + (-y - 0.2)**2)
22+
r2 = np.hypot(-x - 0.2, -y - 0.2)
2323
theta2 = np.arctan2(-x - 0.2, -y - 0.2)
2424
z = -(2 * (np.exp((r1 / 10)**2) - 1) * 30. * np.cos(7. * theta1) +
2525
(np.exp((r2 / 10)**2) - 1) * 30. * np.cos(11. * theta2) +

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