The SciPy library is one of the core packages that make up the SciPy stack. It provides many user-friendly and efficient numerical routines such as routines for numerical integration and optimization.
Library documentation: http://www.scipy.org/scipylib/index.html
# needed to display the graphs
%matplotlib inline
from pylab import *
from numpy import *
from scipy.integrate import quad, dblquad, tplquad
# integration
val, abserr = quad(lambda x: exp(-x ** 2), Inf, Inf)
val, abserr
(0.0, 0.0)
from scipy.integrate import odeint, ode
# differential equation
def dy(y, t, zeta, w0):
x, p = y[0], y[1]
dx = p
dp = -2 * zeta * w0 * p - w0**2 * x
return [dx, dp]
# initial state
y0 = [1.0, 0.0]
# time coodinate to solve the ODE for
t = linspace(0, 10, 1000)
w0 = 2*pi*1.0
# solve the ODE problem for three different values of the damping ratio
y1 = odeint(dy, y0, t, args=(0.0, w0)) # undamped
y2 = odeint(dy, y0, t, args=(0.2, w0)) # under damped
y3 = odeint(dy, y0, t, args=(1.0, w0)) # critial damping
y4 = odeint(dy, y0, t, args=(5.0, w0)) # over damped
fig, ax = subplots()
ax.plot(t, y1[:,0], 'k', label="undamped", linewidth=0.25)
ax.plot(t, y2[:,0], 'r', label="under damped")
ax.plot(t, y3[:,0], 'b', label=r"critical damping")
ax.plot(t, y4[:,0], 'g', label="over damped")
ax.legend();
from scipy.fftpack import *
# fourier transform
N = len(t)
dt = t[1]-t[0]
# calculate the fast fourier transform
# y2 is the solution to the under-damped oscillator from the previous section
F = fft(y2[:,0])
# calculate the frequencies for the components in F
w = fftfreq(N, dt)
fig, ax = subplots(figsize=(9,3))
ax.plot(w, abs(F));
A = array([[1,2,3], [4,5,6], [7,8,9]])
b = array([1,2,3])
# solve a system of linear equations
x = solve(A, b)
x
array([-0.33333333, 0.66666667, 0. ])
# eigenvalues and eigenvectors
A = rand(3,3)
B = rand(3,3)
evals, evecs = eig(A)
evals
array([ 1.34211023, 0.22314378, -0.11368553])
evecs
array([[-0.24560175, -0.93054452, 0.46758953], [-0.84521949, -0.00332278, -0.56599975], [-0.4746407 , 0.36616371, 0.67897299]])
svd(A)
(array([[-0.23411483, 0.96016594, 0.15255034], [-0.84815445, -0.12501447, -0.51478677], [-0.47520972, -0.24990547, 0.84363676]]), array([ 1.3457633 , 0.23442523, 0.1079208 ]), array([[-0.21657397, -0.81553512, -0.53665463], [ 0.7658064 , 0.19902251, -0.61149865], [-0.60550497, 0.54340824, -0.58143892]]))
from scipy import optimize
def f(x):
return 4*x**3 + (x-2)**2 + x**4
fig, ax = subplots()
x = linspace(-5, 3, 100)
ax.plot(x, f(x));
x_min = optimize.fmin_bfgs(f, -0.5)
x_min
Optimization terminated successfully. Current function value: 2.804988 Iterations: 4 Function evaluations: 24 Gradient evaluations: 8
array([ 0.46961745])
from scipy import stats
# create a (continous) random variable with normal distribution
Y = stats.norm()
x = linspace(-5,5,100)
fig, axes = subplots(3,1, sharex=True)
# plot the probability distribution function (PDF)
axes[0].plot(x, Y.pdf(x))
# plot the commulative distributin function (CDF)
axes[1].plot(x, Y.cdf(x));
# plot histogram of 1000 random realizations of the stochastic variable Y
axes[2].hist(Y.rvs(size=1000), bins=50);
Y.mean(), Y.std(), Y.var()
(0.0, 1.0, 1.0)
# t-test example
t_statistic, p_value = stats.ttest_ind(Y.rvs(size=1000), Y.rvs(size=1000))
t_statistic, p_value
(-1.193733722708372, 0.23272385793970249)