12.07.2015 Views

Numerical and Scientific Python

Numerical and Scientific Python

Numerical and Scientific Python

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Numerical</strong> <strong>and</strong><strong>Scientific</strong> <strong>Python</strong>Jonas Juselius HPC@UiT


Least squares fit1 import numpy as np2 import matplotlib.pyplot as plt3 from scipy.optimize import leastsq45 def f(x, p = [0.01, 1.0, 3.0]):6 a, b, c = p7 return a*(x-2.0)**4 + b*x**3 - c*x**2 + x89 def residual(p, y, x):10 return y - f(x, p)1112 x = np.arange(0.0, 5.0, 0.1)13 measured = f(x) + 2 * np.r<strong>and</strong>om.r<strong>and</strong>n(len(x))1415 fit = leastsq(residual, (1.0, 2.0, 5.0), args=(measured, x))16 print "Fit parameters:", fit17 plt.plot(x, f(x), 'r', x, measured, 'ob', x, f(x, fit[0]))18 plt.show()1


Least squares fit2


Minimization1 import numpy as np2 import matplotlib.pyplot as plt3 from scipy.optimize import fmin45 f = lambda x: 0.01*(x-2.0)**4 + x**3 - 4.0*x**2 + x6 minx = fmin(f, 4.0)7 print "Minimum:", minx, f(minx)89 x = np.linspace(0.0, 5.0, 100)10 plt.plot(x, f(x), minx, f(minx), 'ro')11 plt.show()3


Minimization4


Interpolation1 import math2 import numpy as np3 import matplotlib.pyplot as plt4 from scipy.interpolate import interp1d56 f = lambda x: np.exp(-x**2)78 x = np.linspace(-5.0, 5.0, 200)9 xd = np.linspace(-5.0, 5.0, 20)1011 f1 = interp1d(xd, f(xd))12 f2 = interp1d(xd, f(xd), kind='cubic')1314 plt.plot(x, f(x), 'r', x, f1(x), x, f2(x), xd, f(xd), 'ro')15 plt.show()5


Interpolation6


<strong>Numerical</strong> integration1 import math2 import numpy as np3 import matplotlib.pyplot as plt4 from scipy.integrate import quad56 f = lambda x: np.exp(-x**2)7 I = quad(f, -10.0, 10.0)8 print "Quadrature:", I9 print "Exact:", math.sqrt(math.pi)1011 x = np.linspace(-5.0, 5.0, 100)12 xd = np.linspace(-5.0, 5.0, 31)13 fd = np.vectorize(lambda x: math.exp(-x**2))14 plt.plot(x, f(x), xd, fd(xd), 'ro')15 plt.show()7


<strong>Numerical</strong> integration8


Vectorize1 import os2 import math3 import numpy as np45 x = np.linspace(-5.0, 5.0, 10000000)67 t0 = os.times()[0]8 f = np.vectorize(lambda x: math.exp(-x**2))9 fx = f(x)10 print "t(vec):", os.times()[0] - t01112 t0 = os.times()[0]13 fx = [ math.exp(-z**2) for z in x ]14 print "t(list):", os.times()[0] - t09


Arrays1 import numpy as np23 a = np.array([1, 2, 3, 4])4 b = np.array([[1, 2], [3, 4]])5 c = np.zeros((5, 5))6 d = np.arange(0,4).reshape(2,2)7 e = np.ndarray((10, 10, 100), dtype='float')8 f = np.array(e)9 g = np.ones_like(d)10


R<strong>and</strong>om numbers1 a = np.r<strong>and</strong>om.r<strong>and</strong>(10,10)2 print(a.shape)3 print(a.size)4 print(a.ndim)5 print(a.dtype.name)6 print(a)7 print(a[3, 4])8 print(a[1, :])9 print(a[0:4, 6:9])1011 for row in a:12 print(row)1314 for i in np.nditer(a):15 print(i)11


Masked arrays1 import numpy as np2 import numpy.ma as ma34 x = np.array([1, 2, 3, -1, 5])5 print "Mean:", x.mean()6 mx = ma.masked_array(x, mask=[0, 0, 0, 1, 0])7 print "Masked array:", mx8 print "Masked mean:", mx.mean()9 y = np.zeros(5)10 mask = x < y11 print "Mask:", mask12 mx = ma.masked_array(x, mask=mask)13 print "Masked array:", mx14 print "Masked mean:", mx.mean()15 print "Masked array:", ma.masked_less(x, 3)13


Linear algebra1 import numpy as np2 from numpy import linalg34 a = np.cos(np.arange(9).reshape(3,3))5 linalg.det(a)6 linalg.inv(a)7 linalg.eig(a)8 linalg.norm(a)9 linalg.svd(a)1011 i = np.arange(3)12 a[i,i] += 2 # make a positive definite13 linalg.cholesky(a)14

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!