Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

Jupyter notebook 2016-12-05-152305.ipynb

56 views
Kernel: Python 3 (Anaconda)
import numpy as np import matplotlib import matplotlib.pyplot as plt import matplotlib.image as img %matplotlib inline
x = np.diag([i for i in range(50)]) x
array([[ 0, 0, 0, ..., 0, 0, 0], [ 0, 1, 0, ..., 0, 0, 0], [ 0, 0, 2, ..., 0, 0, 0], ..., [ 0, 0, 0, ..., 47, 0, 0], [ 0, 0, 0, ..., 0, 48, 0], [ 0, 0, 0, ..., 0, 0, 49]])
plt.spy(x)
<matplotlib.image.AxesImage at 0x7fa9b5242b38>
Image in a Jupyter notebook
plt.imshow(x)
<matplotlib.image.AxesImage at 0x7fa9c8738978>
Image in a Jupyter notebook
cm = matplotlib.colors.LinearSegmentedColormap.from_list('', ['#000000', '#FF0000']) plt.imshow(x, cmap = "jet")
<matplotlib.image.AxesImage at 0x7fa9976cdba8>
Image in a Jupyter notebook
plt.imsave('grad_diag.png', x, cmap = "jet")
y = plt.imread('grad_diag.png') y.shape y[:, :, 3] = np.diag([1] * 50)
fig, ax = plt.subplots(1, 4, figsize = (15, 15)) cm1 = matplotlib.colors.LinearSegmentedColormap.from_list('', ['#000000', '#FF0000']) ax[0].imshow(y[:, :, 0], cmap = cm1) cm2 = matplotlib.colors.LinearSegmentedColormap.from_list('', ['#000000', '#00FF00']) ax[1].imshow(y[:, :, 1], cmap = cm2) cm3 = matplotlib.colors.LinearSegmentedColormap.from_list('', ['#000000', '#0000FF']) ax[2].imshow(y[:, :, 2], cmap = cm3) cm4 = matplotlib.colors.LinearSegmentedColormap.from_list('', ['#000000', '#FFFFFF']) ax[3].imshow(y[:, :, 3], cmap = cm4)
<matplotlib.image.AxesImage at 0x7fa9967f1160>
Image in a Jupyter notebook
plt.imshow(y)
<matplotlib.image.AxesImage at 0x7fa996702cf8>
Image in a Jupyter notebook
from scipy.interpolate import interp1d from scipy.integrate import quad
x = np.linspace(0, 2 * np.pi, 10) y = 2 * x + 2 * np.sin(x) plt.plot(x, y, '.')
[<matplotlib.lines.Line2D at 0x7fa9d254eb00>]
Image in a Jupyter notebook
f = interp1d(x, y) plt.plot(x, f(x), '.')
[<matplotlib.lines.Line2D at 0x7fa9d24d0048>]
Image in a Jupyter notebook
(y - f(x)).any()
False
x1 = np.linspace(0, 2 * np.pi, 100) plt.plot(x1, f(x1), '.')
[<matplotlib.lines.Line2D at 0x7fa9d2434ef0>]
Image in a Jupyter notebook
int1, _ = quad(f, 0, 2 * np.pi) int1
39.47841760435743
f2 = interp1d(x, y, kind = 'cubic') plt.plot(x, f2(x), '.')
[<matplotlib.lines.Line2D at 0x7fa98bff9160>]
Image in a Jupyter notebook
(y - f2(x)).any()
True
plt.plot(x1, f2(x1), '.')
[<matplotlib.lines.Line2D at 0x7fa98ae0f710>]
Image in a Jupyter notebook
int2, _ = quad(f2, 0, 2 * np.pi) int2
39.47841760435741
print(np.abs(int1 - int2))
2.13162820728e-14
from scipy.interpolate import interp2d, RectBivariateSpline from scipy.integrate import dblquad
x = np.linspace(0, 2 * np.pi, 10) y = np.linspace(0, 2 * np.pi, 10) X, Y = np.meshgrid(x, y)
Z = np.abs(X - np.pi) + np.abs(Y - np.pi) + np.sin(X + Y - 2 * np.pi)
from mpl_toolkits.mplot3d.axes3d import Axes3D
fig = plt.figure() ax = fig.add_subplot(1, 1, 1, projection = '3d') #ax.plot_surface(X, Y, Z) ax.scatter3D(X, Y, Z) ax.set_zlim([0, 10])
(0, 10)
Image in a Jupyter notebook
f3 = interp2d(X, Y, Z, kind='cubic')
fig = plt.figure() ax = fig.add_subplot(1, 1, 1, projection = '3d') ax.plot_surface(X, Y, f3(x, y)) ax.set_zlim([0, 10])
(0, 10)
Image in a Jupyter notebook
%%timeit dblquad(f3, 0, 2 * np.pi, lambda x : 0, lambda x : 2 * np.pi)
1 loop, best of 3: 6.83 s per loop
f4 = RectBivariateSpline(x, y, Z)
%%timeit dblquad(f4, 0, 2 * np.pi, lambda x : 0, lambda x : 2 * np.pi)
1 loop, best of 3: 1.83 s per loop
fig = plt.figure() ax = fig.add_subplot(1, 1, 1, projection = '3d') ax.plot_surface(X, Y, f4(x, y)) ax.set_zlim([0, 10])
(0, 10)
Image in a Jupyter notebook
%%timeit f4.integral(0, 2 * np.pi, 0, 2 * np.pi)
The slowest run took 18.95 times longer than the fastest. This could mean that an intermediate result is being cached. 1000000 loops, best of 3: 2 µs per loop