Skip to content

Cleanup: broadcasting #7562

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions doc/api/axis_api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ Ticks, tick labels and Offset text
Axis.axis_date


Data and view internvals
------------------------
Data and view intervals
-----------------------

.. autosummary::
:toctree: _as_gen
Expand Down
2 changes: 1 addition & 1 deletion examples/animation/unchained.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
# Generate random data
data = np.random.uniform(0, 1, (64, 75))
X = np.linspace(-1, 1, data.shape[-1])
G = 1.5 * np.exp(-4 * X * X)
G = 1.5 * np.exp(-4 * X ** 2)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In most cases I think this construct is marginally faster.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

# float is probably the most common case
In [8]: x = np.arange(100000).astype(float)

In [9]: %timeit x * x
10000 loops, best of 3: 65.5 µs per loop

In [10]: %timeit x ** 2
10000 loops, best of 3: 66.4 µs per loop

(and you can easily get them reversed on another run).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interesting. That is an apparently wrong rule of thumb I have been using for a while.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would you think this would be the case? (I am honestly puzzled.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It dates back to the early days of computers and compilers, when computers were slow and compilers were not so bright. Multiplication is faster than taking a power, in general. But now compilers recognize things like this and find the best algorithm. In the case of numpy, I'm pretty sure there is explicit internal optimization of small integer powers so that we wouldn't have to use that ancient manual optimization.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I respectfully request the children remain off of my lawn. 😈

Copy link
Contributor

@eric-wieser eric-wieser Dec 15, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As @efiring says, numpy hard-codes power(x, 2) to fall back on square(x), along with a handful of other constants (I think (-1, 0, 0.5, 1, 2))


# Generate line plots
lines = []
Expand Down
12 changes: 4 additions & 8 deletions examples/api/custom_projection_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,15 +434,11 @@ def __init__(self, resolution):
self._resolution = resolution

def transform_non_affine(self, xy):
x = xy[:, 0:1]
y = xy[:, 1:2]

quarter_x = 0.25 * x
half_y = 0.5 * y
z = np.sqrt(1.0 - quarter_x*quarter_x - half_y*half_y)
longitude = 2 * np.arctan((z*x) / (2.0 * (2.0*z*z - 1.0)))
x, y = xy.T
z = np.sqrt(1 - (x / 4) ** 2 - (y / 2) ** 2)
longitude = 2 * np.arctan((z * x) / (2 * (2 * z ** 2 - 1)))
latitude = np.arcsin(y*z)
return np.concatenate((longitude, latitude), 1)
return np.column_stack([longitude, latitude])
transform_non_affine.__doc__ = Transform.transform_non_affine.__doc__

def inverted(self):
Expand Down
36 changes: 17 additions & 19 deletions examples/api/scatter_piecharts.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

Thanks to Manuel Metz for the example
"""
import math

import numpy as np
import matplotlib.pyplot as plt

Expand All @@ -16,35 +16,33 @@
r2 = r1 + 0.4 # 40%

# define some sizes of the scatter marker
sizes = [60, 80, 120]
sizes = np.array([60, 80, 120])

# calculate the points of the first pie marker
#
# these are just the origin (0,0) +
# some points on a circle cos,sin
x = [0] + np.cos(np.linspace(0, 2*math.pi*r1, 10)).tolist()
y = [0] + np.sin(np.linspace(0, 2*math.pi*r1, 10)).tolist()

x = [0] + np.cos(np.linspace(0, 2 * np.pi * r1, 10)).tolist()
y = [0] + np.sin(np.linspace(0, 2 * np.pi * r1, 10)).tolist()
xy1 = list(zip(x, y))
s1 = max(max(x), max(y))
s1 = np.max(xy1)

# ...
x = [0] + np.cos(np.linspace(2*math.pi*r1, 2*math.pi*r2, 10)).tolist()
y = [0] + np.sin(np.linspace(2*math.pi*r1, 2*math.pi*r2, 10)).tolist()
x = [0] + np.cos(np.linspace(2 * np.pi * r1, 2 * np.pi * r2, 10)).tolist()
y = [0] + np.sin(np.linspace(2 * np.pi * r1, 2 * np.pi * r2, 10)).tolist()
xy2 = list(zip(x, y))
s2 = max(max(x), max(y))
s2 = np.max(xy2)

x = [0] + np.cos(np.linspace(2*math.pi*r2, 2*math.pi, 10)).tolist()
y = [0] + np.sin(np.linspace(2*math.pi*r2, 2*math.pi, 10)).tolist()
x = [0] + np.cos(np.linspace(2 * np.pi * r2, 2 * np.pi, 10)).tolist()
y = [0] + np.sin(np.linspace(2 * np.pi * r2, 2 * np.pi, 10)).tolist()
xy3 = list(zip(x, y))
s3 = max(max(x), max(y))
s3 = np.max(xy3)

fig, ax = plt.subplots()
ax.scatter(np.arange(3), np.arange(3), marker=(xy1, 0),
s=[s1*s1*_ for _ in sizes], facecolor='blue')
ax.scatter(np.arange(3), np.arange(3), marker=(xy2, 0),
s=[s2*s2*_ for _ in sizes], facecolor='green')
ax.scatter(np.arange(3), np.arange(3), marker=(xy3, 0),
s=[s3*s3*_ for _ in sizes], facecolor='red')
ax.scatter(range(3), range(3), marker=(xy1, 0),
s=s1 ** 2 * sizes, facecolor='blue')
ax.scatter(range(3), range(3), marker=(xy2, 0),
s=s2 ** 2 * sizes, facecolor='green')
ax.scatter(range(3), range(3), marker=(xy3, 0),
s=s3 ** 2 * sizes, facecolor='red')

plt.show()
2 changes: 1 addition & 1 deletion examples/event_handling/timers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def update_title(axes):
fig, ax = plt.subplots()

x = np.linspace(-3, 3)
ax.plot(x, x*x)
ax.plot(x, x ** 2)

# Create a new timer object. Set the interval to 100 milliseconds
# (1000 is default) and tell the timer what function should be called.
Expand Down
24 changes: 11 additions & 13 deletions examples/event_handling/trifinder_event_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,15 @@
from matplotlib.tri import Triangulation
from matplotlib.patches import Polygon
import numpy as np
import math


def update_polygon(tri):
if tri == -1:
points = [0, 0, 0]
else:
points = triangulation.triangles[tri]
xs = triangulation.x[points]
ys = triangulation.y[points]
points = triang.triangles[tri]
xs = triang.x[points]
ys = triang.y[points]
polygon.set_xy(list(zip(xs, ys)))


Expand All @@ -39,23 +38,22 @@ def motion_notify(event):
n_radii = 5
min_radius = 0.25
radii = np.linspace(min_radius, 0.95, n_radii)
angles = np.linspace(0, 2*math.pi, n_angles, endpoint=False)
angles = np.linspace(0, 2 * np.pi, n_angles, endpoint=False)
angles = np.repeat(angles[..., np.newaxis], n_radii, axis=1)
angles[:, 1::2] += math.pi / n_angles
angles[:, 1::2] += np.pi / n_angles
x = (radii*np.cos(angles)).flatten()
y = (radii*np.sin(angles)).flatten()
triangulation = Triangulation(x, y)
xmid = x[triangulation.triangles].mean(axis=1)
ymid = y[triangulation.triangles].mean(axis=1)
mask = np.where(xmid*xmid + ymid*ymid < min_radius*min_radius, 1, 0)
triangulation.set_mask(mask)
triang = Triangulation(x, y)
triang.set_mask(np.hypot(x[triang.triangles].mean(axis=1),
y[triang.triangles].mean(axis=1))
< min_radius)

# Use the triangulation's default TriFinder object.
trifinder = triangulation.get_trifinder()
trifinder = triang.get_trifinder()

# Setup plot and callbacks.
plt.subplot(111, aspect='equal')
plt.triplot(triangulation, 'bo-')
plt.triplot(triang, 'bo-')
polygon = Polygon([[0, 0], [0, 0]], facecolor='y') # dummy data for xs,ys
update_polygon(-1)
plt.gca().add_patch(polygon)
Expand Down
14 changes: 6 additions & 8 deletions examples/images_contours_and_fields/tricontour_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import matplotlib.pyplot as plt
import matplotlib.tri as tri
import numpy as np
import math

###############################################################################
# Creating a Triangulation without specifying the triangles results in the
Expand All @@ -20,22 +19,21 @@
min_radius = 0.25
radii = np.linspace(min_radius, 0.95, n_radii)

angles = np.linspace(0, 2 * math.pi, n_angles, endpoint=False)
angles = np.linspace(0, 2 * np.pi, n_angles, endpoint=False)
angles = np.repeat(angles[..., np.newaxis], n_radii, axis=1)
angles[:, 1::2] += math.pi / n_angles
angles[:, 1::2] += np.pi / n_angles

x = (radii * np.cos(angles)).flatten()
y = (radii * np.sin(angles)).flatten()
z = (np.cos(radii) * np.cos(angles * 3.0)).flatten()
z = (np.cos(radii) * np.cos(3 * angles)).flatten()

# Create the Triangulation; no triangles so Delaunay triangulation created.
triang = tri.Triangulation(x, y)

# Mask off unwanted triangles.
xmid = x[triang.triangles].mean(axis=1)
ymid = y[triang.triangles].mean(axis=1)
mask = np.where(xmid * xmid + ymid * ymid < min_radius * min_radius, 1, 0)
triang.set_mask(mask)
triang.set_mask(np.hypot(x[triang.triangles].mean(axis=1),
y[triang.triangles].mean(axis=1))
< min_radius)

###############################################################################
# pcolor plot.
Expand Down
12 changes: 5 additions & 7 deletions examples/images_contours_and_fields/tricontour_smooth_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import numpy as np
import math


#-----------------------------------------------------------------------------
Expand All @@ -36,9 +35,9 @@ def function_z(x, y):
min_radius = 0.15
radii = np.linspace(min_radius, 0.95, n_radii)

angles = np.linspace(0, 2 * math.pi, n_angles, endpoint=False)
angles = np.linspace(0, 2 * np.pi, n_angles, endpoint=False)
angles = np.repeat(angles[..., np.newaxis], n_radii, axis=1)
angles[:, 1::2] += math.pi / n_angles
angles[:, 1::2] += np.pi / n_angles

x = (radii * np.cos(angles)).flatten()
y = (radii * np.sin(angles)).flatten()
Expand All @@ -50,10 +49,9 @@ def function_z(x, y):
triang = tri.Triangulation(x, y)

# Mask off unwanted triangles.
xmid = x[triang.triangles].mean(axis=1)
ymid = y[triang.triangles].mean(axis=1)
mask = np.where(xmid * xmid + ymid * ymid < min_radius * min_radius, 1, 0)
triang.set_mask(mask)
triang.set_mask(np.hypot(x[triang.triangles].mean(axis=1),
y[triang.triangles].mean(axis=1))
< min_radius)

#-----------------------------------------------------------------------------
# Refine data
Expand Down
16 changes: 7 additions & 9 deletions examples/images_contours_and_fields/trigradient_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@

Demonstrates computation of gradient with matplotlib.tri.CubicTriInterpolator.
"""
from matplotlib.tri import Triangulation, UniformTriRefiner,\
CubicTriInterpolator
from matplotlib.tri import (
Triangulation, UniformTriRefiner, CubicTriInterpolator)
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import numpy as np
import math


#-----------------------------------------------------------------------------
Expand All @@ -33,9 +32,9 @@ def dipole_potential(x, y):
min_radius = 0.2
radii = np.linspace(min_radius, 0.95, n_radii)

angles = np.linspace(0, 2*math.pi, n_angles, endpoint=False)
angles = np.linspace(0, 2 * np.pi, n_angles, endpoint=False)
angles = np.repeat(angles[..., np.newaxis], n_radii, axis=1)
angles[:, 1::2] += math.pi/n_angles
angles[:, 1::2] += np.pi / n_angles

x = (radii*np.cos(angles)).flatten()
y = (radii*np.sin(angles)).flatten()
Expand All @@ -46,10 +45,9 @@ def dipole_potential(x, y):
triang = Triangulation(x, y)

# Mask off unwanted triangles.
xmid = x[triang.triangles].mean(axis=1)
ymid = y[triang.triangles].mean(axis=1)
mask = np.where(xmid*xmid + ymid*ymid < min_radius*min_radius, 1, 0)
triang.set_mask(mask)
triang.set_mask(np.hypot(x[triang.triangles].mean(axis=1),
y[triang.triangles].mean(axis=1))
< min_radius)

#-----------------------------------------------------------------------------
# Refine data - interpolates the electrical potential V
Expand Down
14 changes: 6 additions & 8 deletions examples/images_contours_and_fields/tripcolor_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import matplotlib.pyplot as plt
import matplotlib.tri as tri
import numpy as np
import math

###############################################################################
# Creating a Triangulation without specifying the triangles results in the
Expand All @@ -20,22 +19,21 @@
min_radius = 0.25
radii = np.linspace(min_radius, 0.95, n_radii)

angles = np.linspace(0, 2 * math.pi, n_angles, endpoint=False)
angles = np.linspace(0, 2 * np.pi, n_angles, endpoint=False)
angles = np.repeat(angles[..., np.newaxis], n_radii, axis=1)
angles[:, 1::2] += math.pi / n_angles
angles[:, 1::2] += np.pi / n_angles

x = (radii * np.cos(angles)).flatten()
y = (radii * np.sin(angles)).flatten()
z = (np.cos(radii) * np.cos(angles * 3.0)).flatten()
z = (np.cos(radii) * np.cos(3 * angles)).flatten()

# Create the Triangulation; no triangles so Delaunay triangulation created.
triang = tri.Triangulation(x, y)

# Mask off unwanted triangles.
xmid = x[triang.triangles].mean(axis=1)
ymid = y[triang.triangles].mean(axis=1)
mask = np.where(xmid * xmid + ymid * ymid < min_radius * min_radius, 1, 0)
triang.set_mask(mask)
triang.set_mask(np.hypot(x[triang.triangles].mean(axis=1),
y[triang.triangles].mean(axis=1))
< min_radius)

###############################################################################
# tripcolor plot.
Expand Down
12 changes: 5 additions & 7 deletions examples/images_contours_and_fields/triplot_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import matplotlib.pyplot as plt
import matplotlib.tri as tri
import numpy as np
import math

###############################################################################
# Creating a Triangulation without specifying the triangles results in the
Expand All @@ -20,9 +19,9 @@
min_radius = 0.25
radii = np.linspace(min_radius, 0.95, n_radii)

angles = np.linspace(0, 2 * math.pi, n_angles, endpoint=False)
angles = np.linspace(0, 2 * np.pi, n_angles, endpoint=False)
angles = np.repeat(angles[..., np.newaxis], n_radii, axis=1)
angles[:, 1::2] += math.pi / n_angles
angles[:, 1::2] += np.pi / n_angles

x = (radii * np.cos(angles)).flatten()
y = (radii * np.sin(angles)).flatten()
Expand All @@ -31,10 +30,9 @@
triang = tri.Triangulation(x, y)

# Mask off unwanted triangles.
xmid = x[triang.triangles].mean(axis=1)
ymid = y[triang.triangles].mean(axis=1)
mask = np.where(xmid * xmid + ymid * ymid < min_radius * min_radius, 1, 0)
triang.set_mask(mask)
triang.set_mask(np.hypot(x[triang.triangles].mean(axis=1),
y[triang.triangles].mean(axis=1))
< min_radius)

###############################################################################
# Plot the triangulation.
Expand Down
2 changes: 1 addition & 1 deletion examples/lines_bars_and_markers/nan_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import matplotlib.pyplot as plt

t = np.arange(0.0, 1.0 + 0.01, 0.01)
s = np.cos(2 * 2 * np.pi * t)
s = np.cos(2 * 2*np.pi * t)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious: why is this the one where you don't have spaces around it?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nm...I guess I see it now.

t[41:60] = np.nan

plt.subplot(2, 1, 1)
Expand Down
2 changes: 1 addition & 1 deletion examples/misc/multipage_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

plt.rc('text', usetex=False)
fig = plt.figure(figsize=(4, 5))
plt.plot(x, x*x, 'ko')
plt.plot(x, x ** 2, 'ko')
plt.title('Page Three')
pdf.savefig(fig) # or you can pass a Figure object to pdf.savefig
plt.close()
Expand Down
4 changes: 2 additions & 2 deletions examples/misc/pythonic_matplotlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
fig = figure(1)

ax1 = fig.add_subplot(211)
ax1.plot(t, sin(2*pi*t))
ax1.plot(t, sin(2*pi * t))
ax1.grid(True)
ax1.set_ylim((-2, 2))
ax1.set_ylabel('1 Hz')
Expand All @@ -74,7 +74,7 @@


ax2 = fig.add_subplot(212)
ax2.plot(t, sin(2*2*pi*t))
ax2.plot(t, sin(2 * 2*pi * t))
ax2.grid(True)
ax2.set_ylim((-2, 2))
l = ax2.set_xlabel('Hi mom')
Expand Down
2 changes: 1 addition & 1 deletion examples/misc/table_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
bar_width = 0.4

# Initialize the vertical-offset for the stacked bar chart.
y_offset = np.array([0.0] * len(columns))
y_offset = np.zeros(len(columns))

# Plot bars and create text labels for the table
cell_text = []
Expand Down
Loading