Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c86fbc7

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

31 files changed

+70
-89
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/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/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) +

examples/images_contours_and_fields/trigradient_demo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def dipole_potential(x, y):
6161
tci = CubicTriInterpolator(triang, -V)
6262
# Gradient requested here at the mesh nodes but could be anywhere else:
6363
(Ex, Ey) = tci.gradient(triang.x, triang.y)
64-
E_norm = np.sqrt(Ex**2 + Ey**2)
64+
E_norm = np.hypot(Ex, Ey)
6565

6666
#-----------------------------------------------------------------------------
6767
# Plot the triangulation, the potential iso-contours and the vector field

examples/lines_bars_and_markers/scatter_star_poly.py

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

1414
x = np.random.rand(10)
1515
y = np.random.rand(10)
16-
z = np.sqrt(x**2 + y**2)
16+
z = np.hypot(x, y)
1717

1818
plt.subplot(321)
1919
plt.scatter(x, y, s=80, c=z, marker=">")

0 commit comments

Comments
 (0)