Scipy -- Library of scientific algorithms for Python¶
Based on lecture at http://github.com/jrjohansson/scientific-python-lectures.
The SciPy framework builds on top of the low-level NumPy framework for multidimensional arrays, and provides a large number of higher-level scientific algorithms. Some of the topics that SciPy covers are:
- Special functions (scipy.special)
- Integration (scipy.integrate)
- Optimization (scipy.optimize)
- Interpolation (scipy.interpolate)
- Fourier Transforms (scipy.fftpack)
- Signal Processing (scipy.signal)
- Linear Algebra (scipy.linalg) [moved to numpy]
- Sparse Eigenvalue Problems (scipy.sparse)
- Statistics (scipy.stats)
- Multi-dimensional image processing (scipy.ndimage)
- File IO (scipy.io)
Each of these submodules provides a number of functions and classes that can be used to solve problems in their respective topics.
In this lecture we will look at how to use some of these subpackages.
To access the SciPy package in a Python program, we start by importing everything from the scipy
module.
WARNING: In the new version of python many functionalities are now moved from scipy
to numpy
, but they are still available in scipy
and a deprecated warning is displayed. The work-around is to first import functions from scipy
and after that from numpy
, to overwrite scipy functions with the same name.
from scipy import *
from numpy import *
If we only need to use part of the SciPy framework we can selectively include only those modules we are interested in. For example, to include the linear algebra package under the name la
, we can do:
import scipy.linalg as la
Special functions¶
A large number of mathematical special functions are important for many computional physics problems. SciPy provides implementations of a very extensive set of special functions. For details, see the list of functions in the reference documention at http://docs.scipy.org/doc/scipy/reference/special.html#module-scipy.special.
To demonstrate the typical usage of special functions we will look in more detail at the Bessel functions:
from scipy import special
#help(special)
Bessel functions are special functions that appear when solving equations (like the wave equation) in cylindrical or spherical geometries.
Physical examples abound: vibrating circular membranes, heat conduction in a cylinder, electromagnetic waves in waveguides, and quantum particles in cylindrical potentials.
Example: Bessel functions appear as a solutions of the problem with circular
Chladni Plates
.
#
# The scipy.special module includes a large number of Bessel-functions
# Here we will use the functions jn and yn, which are the Bessel functions
# of the first and second kind and real-valued order. We also include the
# function jn_zeros and yn_zeros that gives the zeroes of the functions jn
# and yn.
#
from scipy.special import jn, yn, jn_zeros, yn_zeros
n = 0 # order
x = 0.0 # x-point
# Bessel function of first kind
print("J_%d(%f) = %f" % (n, x, jn(n, x)))
x = 1e-5
# Bessel function of second kind
print("Y_%d(%f) = %f" % (n, x, yn(n, x)))
J_0(0.000000) = 1.000000 Y_0(0.000010) = -7.403160
%matplotlib inline
import matplotlib.pyplot as plt
from IPython.display import Image
x = linspace(0, 10, 100)
for n in range(4):
plt.plot(x, jn(n, x), label=('$J_%d(x)$' % n))
plt.legend(loc='best')
plt.show()
Second solution of the same Bessel differential equation: $$x^2 y_n''(x)+x y_n'(x)+(x^2-n^2) y_n(x)=0$$
Bessel functions of the first kind $J_n(x)$ are regular at the origin and $Y_n(x)$ are the solution which is irregular at the origin.
x = linspace(1, 10, 100)
for n in range(4):
plt.plot(x, yn(n, x), label=('$J_%d(x)$' % n))
plt.legend(loc='best')
plt.show()
Spherical harmonics¶
What are spherical harmonics used for?
Angular Momentum in Quantum Mechanics: Spherical harmonics arise naturally as eigenfunctions of the angular part of the Laplacian operator in spherical coordinates. They describe the angular dependence of wavefunctions in central potentials (like the hydrogen atom).
Electron Orbitals: The familiar s, p, d, f orbitals of electrons in atoms are labeled according to their angular momentum quantum numbers, with their angular part given by spherical harmonics.
Multipole Expansion of Potentials in Electrodynamics: In classical electromagnetism and gravitation, spherical harmonics are used to express the angular dependence of potentials due to charge or mass distributions. For example, the electric or gravitational field around a nonspherical object can be expanded in terms of monopole, dipole, quadrupole, and higher moments, each involving spherical harmonics.
Radiation Patterns in Electrodynamics: The angular distribution of radiation from antennas or other sources is often expressed using spherical harmonics.
import numpy as np
theta=linspace(0,pi,4)
phi = linspace(0,2*pi,3)
print('shape(theta)=', shape(theta), 'shape(phi)=', shape(phi))
phi,theta = np.meshgrid(phi,theta) # from 1D arrays into 2D array of points!
print('shape(theta)=', shape(theta), 'shape(phi)=', shape(phi))
Ylm = special.sph_harm(0,2,phi,theta)
print(f"{'i':>3} {'j':>3} {'phi':>10} {'theta':>10} {'|Ylm|':>10}")
for i in range(theta.shape[0]):
for j in range(theta.shape[1]):
print(f"{i:3d} {j:3d} {phi[i,j]:10.5f} {theta[i,j]:10.5f} {abs(Ylm[i,j]):10.5f}")
shape(theta)= (4,) shape(phi)= (3,) shape(theta)= (4, 3) shape(phi)= (4, 3) i j phi theta |Ylm| 0 0 0.00000 0.00000 0.63078 0 1 3.14159 0.00000 0.63078 0 2 6.28319 0.00000 0.63078 1 0 0.00000 1.04720 0.07885 1 1 3.14159 1.04720 0.07885 1 2 6.28319 1.04720 0.07885 2 0 0.00000 2.09440 0.07885 2 1 3.14159 2.09440 0.07885 2 2 6.28319 2.09440 0.07885 3 0 0.00000 3.14159 0.63078 3 1 3.14159 3.14159 0.63078 3 2 6.28319 3.14159 0.63078
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# Define the range of spherical coordinates
r = np.linspace(0.5, 1, 10) # radial distance
theta = np.linspace(0,pi,30) # polar angle
phi = np.linspace(0,2*pi,60) # azimuthal angle
# Create a meshgrid for theta and phi
phi, theta = np.meshgrid(phi, theta)
# Use a single radius value to create a spherical surface
r = np.ones_like(theta) * 1
# Convert to Cartesian coordinates
x = r * sin(theta) * cos(phi)
y = r * sin(theta) * sin(phi)
z = r * cos(theta)
# Plot
fig = plt.figure(figsize=plt.figaspect(1.))
ax = fig.add_subplot(projection='3d')
# Plot the spherical surface
ax.plot_surface(x, y, z, alpha=0.8, edgecolor='none')
# Add labels and title
ax.set_title("3D Spherical Coordinates")
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_zlabel("z")
# Show the plot
plt.show()
help(ax.plot_surface)
Help on method plot_surface in module mpl_toolkits.mplot3d.axes3d: plot_surface(X, Y, Z, *, norm=None, vmin=None, vmax=None, lightsource=None, **kwargs) method of mpl_toolkits.mplot3d.axes3d.Axes3D instance Create a surface plot. By default, it will be colored in shades of a solid color, but it also supports colormapping by supplying the *cmap* argument. .. note:: The *rcount* and *ccount* kwargs, which both default to 50, determine the maximum number of samples used in each direction. If the input data is larger, it will be downsampled (by slicing) to these numbers of points. .. note:: To maximize rendering speed consider setting *rstride* and *cstride* to divisors of the number of rows minus 1 and columns minus 1 respectively. For example, given 51 rows rstride can be any of the divisors of 50. Similarly, a setting of *rstride* and *cstride* equal to 1 (or *rcount* and *ccount* equal the number of rows and columns) can use the optimized path. Parameters ---------- X, Y, Z : 2D arrays Data values. rcount, ccount : int Maximum number of samples used in each direction. If the input data is larger, it will be downsampled (by slicing) to these numbers of points. Defaults to 50. rstride, cstride : int Downsampling stride in each direction. These arguments are mutually exclusive with *rcount* and *ccount*. If only one of *rstride* or *cstride* is set, the other defaults to 10. 'classic' mode uses a default of ``rstride = cstride = 10`` instead of the new default of ``rcount = ccount = 50``. color : :mpltype:`color` Color of the surface patches. cmap : Colormap, optional Colormap of the surface patches. facecolors : list of :mpltype:`color` Colors of each individual patch. norm : `~matplotlib.colors.Normalize`, optional Normalization for the colormap. vmin, vmax : float, optional Bounds for the normalization. shade : bool, default: True Whether to shade the facecolors. Shading is always disabled when *cmap* is specified. lightsource : `~matplotlib.colors.LightSource`, optional The lightsource to use when *shade* is True. **kwargs Other keyword arguments are forwarded to `.Poly3DCollection`.
from scipy import special
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
theta = linspace(0, pi, 100) # polar angle
phi = linspace(0, 2*pi, 100) # azimuthal angle
phi, theta = np.meshgrid(phi,theta) # numpy routine
l,m=2,0
Ylm = special.sph_harm(m,l,phi,theta)
# Convert to Cartesian coordinates for plotting
r = abs(Ylm)
x = r * sin(theta) * cos(phi)
y = r * sin(theta) * sin(phi)
z = r * cos(theta)
fig = plt.figure(figsize=plt.figaspect(1.))
ax = fig.add_subplot(projection='3d')
ax.set_box_aspect([1, 1, 3])
# Use plot_surface to create a 3D plot
# You can color the surface based on the value of the magnitude
surface = ax.plot_surface(x, y, z, rstride=1, cstride=1,
facecolors=plt.cm.viridis(r/np.max(r)),
antialiased=True, linewidth=0)
# Add color bar and labels
mappable = plt.cm.ScalarMappable(cmap='viridis')
mappable.set_array(r)
plt.colorbar(mappable, ax=ax, shrink=0.6)
ax.set_title(f"$Y_{{{l}{m}}}$")
# Adjust the view angle for better visualization
ax.view_init(elev=30, azim=45)
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_zlabel("z")
plt.show()
Real spherical harmonics are linear combination of spherical harmonics, which are often used in theory of crystals and crystal potentials. If the crystal potential has symmetry of a cube, eigenstates will not be spherical harmonics, but will be real spherical harmonics.
$$p_z=Y_{1,0}^R = Y_{1,0}$$$$p_y = Y_{1,-1}^R = \sqrt{2} \, \text{Im}(Y_{1,-1}) = \sqrt{2} \, \frac{Y_{1,-1} - Y_{1,1}}{2i}$$$$p_x = Y_{1,1}^R = \sqrt{2} \, \text{Re}(Y_{1,1}) = \sqrt{2} \, \frac{Y_{1,1} + Y_{1,-1}}{2}$$$$d_{z^2}=Y_{2,0}^R = Y_{2,0}$$$$d_{yz}=Y_{2,-1}^R = \sqrt{2} \, \text{Im}(Y_{2,-1}) = \sqrt{2} \, \frac{Y_{2,-1} - Y_{2,1}}{2i}$$$$d_{xz}=Y_{2,1}^R = \sqrt{2} \, \text{Re}(Y_{2,1}) = \sqrt{2} \, \frac{Y_{2,1} + Y_{2,-1}}{2}$$$$d_{xy}=Y_{2,-2}^R = \sqrt{2} \, \text{Im}(Y_{2,-2}) = \sqrt{2} \, \frac{Y_{2,-2} - Y_{2,2}}{2i}$$$$d_{x^2-y^2}=Y_{2,2}^R = \sqrt{2} \, \text{Re}(Y_{2,2}) = \sqrt{2} \, \frac{Y_{2,2} + Y_{2,-2}}{2}$$theta = linspace(0, pi, 100) # polar angle
phi = linspace(0, 2*pi, 100) # azimuthal angle
phi, theta = np.meshgrid(phi,theta) # numpy routine
l,m=4,-4
Ylm = special.sph_harm(m,l,phi,theta)
# conversion to real spherical harmonics
if m < 0:
Ylm = sqrt(2) * (-1)**m * Ylm.imag
elif m > 0:
Ylm = sqrt(2) * (-1)**m * Ylm.real
else: # m==0
Ylm = Ylm.real # should be real anyway
# Convert to Cartesian coordinates for plotting
r = np.abs(Ylm)
x = r * sin(theta) * cos(phi)
y = r * sin(theta) * sin(phi)
z = r * cos(theta)
fig = plt.figure(figsize=plt.figaspect(1.))
ax = fig.add_subplot(projection='3d')
ax.set_box_aspect([1, 1, 0.3]) # if you want to stretch aspect ratio
# Set up colormap based on the real spherical harmonic values
cmap = plt.get_cmap('PRGn')
norm = plt.Normalize(vmin=-r.max(), vmax=r.max())
colors = cmap(norm(Ylm))
# Use plot_surface to create a 3D plot
# Color the plotted surface according to the sign of Y.
#cmap = plt.cm.ScalarMappable(cmap=plt.get_cmap('PRGn'))
#cmap.set_clim(-0.5, 0.5)
surface = ax.plot_surface(x, y, z, rstride=1, cstride=1,
facecolors=colors,
antialiased=True, linewidth=0)
# Add a color bar to show the relationship between color and value
mappable = plt.cm.ScalarMappable(cmap=cmap, norm=norm)
mappable.set_array(Ylm)
plt.colorbar(mappable, ax=ax, shrink=0.6)
ax.set_title(f"$Y_{{{l}{m}}}$")
# Adjust the view angle for better visualization
ax.view_init(elev=80, azim=45)
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_zlabel("z")
plt.show()
Numerical evaluation of a function of the type
$\displaystyle \int_a^b f(x) dx$
is called numerical quadrature, or simply quadature. SciPy provides a series of functions for different kind of quadrature, for example the quad
, dblquad
and tplquad
for single, double and triple integrals, respectively.
from scipy.integrate import quad, dblquad, tplquad
The quad
function takes a large number of optional arguments, which can be used to fine-tune the behaviour of the function (try help(quad)
for details).
The basic usage is as follows:
x_lower, x_upper = 0.0, 1.0
val, abserr = quad( lambda x: x**2, x_lower, x_upper)
print('Value=', val, 'error=', abserr)
Value= 0.3333333333333333 error= 3.700743415417188e-15
# help(quad)
print( quad( lambda x: sin(x)/x, 0, 1000))
(1.5702669821983255, 0.24409510202674356)
/var/folders/j8/d9m3r0zx7j37l3ktfl_n1xw00000gn/T/ipykernel_72981/1866967918.py:1: IntegrationWarning: The maximum number of subdivisions (50) has been achieved. If increasing the limit yields no improvement it is advised to analyze the integrand in order to determine the difficulties. If the position of a local difficulty can be determined (singularity, discontinuity) one will probably gain from splitting up the interval and calling the integrator on the subranges. Perhaps a special-purpose integrator should be used. print( quad( lambda x: sin(x)/x, 0, 1000))
L=10*pi
x=linspace(1e-12,L,100)
plt.plot(x,sin(x)/x)
plt.grid()
plt.show()
If we need to pass extra arguments to integrand function we can use the args
keyword argument. Let's say we want to evaluate
$f(x) = \displaystyle \int_0^x \frac{j_n(t)+j_m(t)}{t} dt$
help(quad)
Help on function quad in module scipy.integrate._quadpack_py: quad(func, a, b, args=(), full_output=0, epsabs=1.49e-08, epsrel=1.49e-08, limit=50, points=None, weight=None, wvar=None, wopts=None, maxp1=50, limlst=50, complex_func=False) Compute a definite integral. Integrate func from `a` to `b` (possibly infinite interval) using a technique from the Fortran library QUADPACK. Parameters ---------- func : {function, scipy.LowLevelCallable} A Python function or method to integrate. If `func` takes many arguments, it is integrated along the axis corresponding to the first argument. If the user desires improved integration performance, then `f` may be a `scipy.LowLevelCallable` with one of the signatures:: double func(double x) double func(double x, void *user_data) double func(int n, double *xx) double func(int n, double *xx, void *user_data) The ``user_data`` is the data contained in the `scipy.LowLevelCallable`. In the call forms with ``xx``, ``n`` is the length of the ``xx`` array which contains ``xx[0] == x`` and the rest of the items are numbers contained in the ``args`` argument of quad. In addition, certain ctypes call signatures are supported for backward compatibility, but those should not be used in new code. a : float Lower limit of integration (use -numpy.inf for -infinity). b : float Upper limit of integration (use numpy.inf for +infinity). args : tuple, optional Extra arguments to pass to `func`. full_output : int, optional Non-zero to return a dictionary of integration information. If non-zero, warning messages are also suppressed and the message is appended to the output tuple. complex_func : bool, optional Indicate if the function's (`func`) return type is real (``complex_func=False``: default) or complex (``complex_func=True``). In both cases, the function's argument is real. If full_output is also non-zero, the `infodict`, `message`, and `explain` for the real and complex components are returned in a dictionary with keys "real output" and "imag output". Returns ------- y : float The integral of func from `a` to `b`. abserr : float An estimate of the absolute error in the result. infodict : dict A dictionary containing additional information. message A convergence message. explain Appended only with 'cos' or 'sin' weighting and infinite integration limits, it contains an explanation of the codes in infodict['ierlst'] Other Parameters ---------------- epsabs : float or int, optional Absolute error tolerance. Default is 1.49e-8. `quad` tries to obtain an accuracy of ``abs(i-result) <= max(epsabs, epsrel*abs(i))`` where ``i`` = integral of `func` from `a` to `b`, and ``result`` is the numerical approximation. See `epsrel` below. epsrel : float or int, optional Relative error tolerance. Default is 1.49e-8. If ``epsabs <= 0``, `epsrel` must be greater than both 5e-29 and ``50 * (machine epsilon)``. See `epsabs` above. limit : float or int, optional An upper bound on the number of subintervals used in the adaptive algorithm. points : (sequence of floats,ints), optional A sequence of break points in the bounded integration interval where local difficulties of the integrand may occur (e.g., singularities, discontinuities). The sequence does not have to be sorted. Note that this option cannot be used in conjunction with ``weight``. weight : float or int, optional String indicating weighting function. Full explanation for this and the remaining arguments can be found below. wvar : optional Variables for use with weighting functions. wopts : optional Optional input for reusing Chebyshev moments. maxp1 : float or int, optional An upper bound on the number of Chebyshev moments. limlst : int, optional Upper bound on the number of cycles (>=3) for use with a sinusoidal weighting and an infinite end-point. See Also -------- dblquad : double integral tplquad : triple integral nquad : n-dimensional integrals (uses `quad` recursively) fixed_quad : fixed-order Gaussian quadrature simpson : integrator for sampled data romb : integrator for sampled data scipy.special : for coefficients and roots of orthogonal polynomials Notes ----- For valid results, the integral must converge; behavior for divergent integrals is not guaranteed. **Extra information for quad() inputs and outputs** If full_output is non-zero, then the third output argument (infodict) is a dictionary with entries as tabulated below. For infinite limits, the range is transformed to (0,1) and the optional outputs are given with respect to this transformed range. Let M be the input argument limit and let K be infodict['last']. The entries are: 'neval' The number of function evaluations. 'last' The number, K, of subintervals produced in the subdivision process. 'alist' A rank-1 array of length M, the first K elements of which are the left end points of the subintervals in the partition of the integration range. 'blist' A rank-1 array of length M, the first K elements of which are the right end points of the subintervals. 'rlist' A rank-1 array of length M, the first K elements of which are the integral approximations on the subintervals. 'elist' A rank-1 array of length M, the first K elements of which are the moduli of the absolute error estimates on the subintervals. 'iord' A rank-1 integer array of length M, the first L elements of which are pointers to the error estimates over the subintervals with ``L=K`` if ``K<=M/2+2`` or ``L=M+1-K`` otherwise. Let I be the sequence ``infodict['iord']`` and let E be the sequence ``infodict['elist']``. Then ``E[I[1]], ..., E[I[L]]`` forms a decreasing sequence. If the input argument points is provided (i.e., it is not None), the following additional outputs are placed in the output dictionary. Assume the points sequence is of length P. 'pts' A rank-1 array of length P+2 containing the integration limits and the break points of the intervals in ascending order. This is an array giving the subintervals over which integration will occur. 'level' A rank-1 integer array of length M (=limit), containing the subdivision levels of the subintervals, i.e., if (aa,bb) is a subinterval of ``(pts[1], pts[2])`` where ``pts[0]`` and ``pts[2]`` are adjacent elements of ``infodict['pts']``, then (aa,bb) has level l if ``|bb-aa| = |pts[2]-pts[1]| * 2**(-l)``. 'ndin' A rank-1 integer array of length P+2. After the first integration over the intervals (pts[1], pts[2]), the error estimates over some of the intervals may have been increased artificially in order to put their subdivision forward. This array has ones in slots corresponding to the subintervals for which this happens. **Weighting the integrand** The input variables, *weight* and *wvar*, are used to weight the integrand by a select list of functions. Different integration methods are used to compute the integral with these weighting functions, and these do not support specifying break points. The possible values of weight and the corresponding weighting functions are. ========== =================================== ===================== ``weight`` Weight function used ``wvar`` ========== =================================== ===================== 'cos' cos(w*x) wvar = w 'sin' sin(w*x) wvar = w 'alg' g(x) = ((x-a)**alpha)*((b-x)**beta) wvar = (alpha, beta) 'alg-loga' g(x)*log(x-a) wvar = (alpha, beta) 'alg-logb' g(x)*log(b-x) wvar = (alpha, beta) 'alg-log' g(x)*log(x-a)*log(b-x) wvar = (alpha, beta) 'cauchy' 1/(x-c) wvar = c ========== =================================== ===================== wvar holds the parameter w, (alpha, beta), or c depending on the weight selected. In these expressions, a and b are the integration limits. For the 'cos' and 'sin' weighting, additional inputs and outputs are available. For finite integration limits, the integration is performed using a Clenshaw-Curtis method which uses Chebyshev moments. For repeated calculations, these moments are saved in the output dictionary: 'momcom' The maximum level of Chebyshev moments that have been computed, i.e., if ``M_c`` is ``infodict['momcom']`` then the moments have been computed for intervals of length ``|b-a| * 2**(-l)``, ``l=0,1,...,M_c``. 'nnlog' A rank-1 integer array of length M(=limit), containing the subdivision levels of the subintervals, i.e., an element of this array is equal to l if the corresponding subinterval is ``|b-a|* 2**(-l)``. 'chebmo' A rank-2 array of shape (25, maxp1) containing the computed Chebyshev moments. These can be passed on to an integration over the same interval by passing this array as the second element of the sequence wopts and passing infodict['momcom'] as the first element. If one of the integration limits is infinite, then a Fourier integral is computed (assuming w neq 0). If full_output is 1 and a numerical error is encountered, besides the error message attached to the output tuple, a dictionary is also appended to the output tuple which translates the error codes in the array ``info['ierlst']`` to English messages. The output information dictionary contains the following entries instead of 'last', 'alist', 'blist', 'rlist', and 'elist': 'lst' The number of subintervals needed for the integration (call it ``K_f``). 'rslst' A rank-1 array of length M_f=limlst, whose first ``K_f`` elements contain the integral contribution over the interval ``(a+(k-1)c, a+kc)`` where ``c = (2*floor(|w|) + 1) * pi / |w|`` and ``k=1,2,...,K_f``. 'erlst' A rank-1 array of length ``M_f`` containing the error estimate corresponding to the interval in the same position in ``infodict['rslist']``. 'ierlst' A rank-1 integer array of length ``M_f`` containing an error flag corresponding to the interval in the same position in ``infodict['rslist']``. See the explanation dictionary (last entry in the output tuple) for the meaning of the codes. **Details of QUADPACK level routines** `quad` calls routines from the FORTRAN library QUADPACK. This section provides details on the conditions for each routine to be called and a short description of each routine. The routine called depends on `weight`, `points` and the integration limits `a` and `b`. ================ ============== ========== ===================== QUADPACK routine `weight` `points` infinite bounds ================ ============== ========== ===================== qagse None No No qagie None No Yes qagpe None Yes No qawoe 'sin', 'cos' No No qawfe 'sin', 'cos' No either `a` or `b` qawse 'alg*' No No qawce 'cauchy' No No ================ ============== ========== ===================== The following provides a short description from [1]_ for each routine. qagse is an integrator based on globally adaptive interval subdivision in connection with extrapolation, which will eliminate the effects of integrand singularities of several types. qagie handles integration over infinite intervals. The infinite range is mapped onto a finite interval and subsequently the same strategy as in ``QAGS`` is applied. qagpe serves the same purposes as QAGS, but also allows the user to provide explicit information about the location and type of trouble-spots i.e. the abscissae of internal singularities, discontinuities and other difficulties of the integrand function. qawoe is an integrator for the evaluation of :math:`\int^b_a \cos(\omega x)f(x)dx` or :math:`\int^b_a \sin(\omega x)f(x)dx` over a finite interval [a,b], where :math:`\omega` and :math:`f` are specified by the user. The rule evaluation component is based on the modified Clenshaw-Curtis technique An adaptive subdivision scheme is used in connection with an extrapolation procedure, which is a modification of that in ``QAGS`` and allows the algorithm to deal with singularities in :math:`f(x)`. qawfe calculates the Fourier transform :math:`\int^\infty_a \cos(\omega x)f(x)dx` or :math:`\int^\infty_a \sin(\omega x)f(x)dx` for user-provided :math:`\omega` and :math:`f`. The procedure of ``QAWO`` is applied on successive finite intervals, and convergence acceleration by means of the :math:`\varepsilon`-algorithm is applied to the series of integral approximations. qawse approximate :math:`\int^b_a w(x)f(x)dx`, with :math:`a < b` where :math:`w(x) = (x-a)^{\alpha}(b-x)^{\beta}v(x)` with :math:`\alpha,\beta > -1`, where :math:`v(x)` may be one of the following functions: :math:`1`, :math:`\log(x-a)`, :math:`\log(b-x)`, :math:`\log(x-a)\log(b-x)`. The user specifies :math:`\alpha`, :math:`\beta` and the type of the function :math:`v`. A globally adaptive subdivision strategy is applied, with modified Clenshaw-Curtis integration on those subintervals which contain `a` or `b`. qawce compute :math:`\int^b_a f(x) / (x-c)dx` where the integral must be interpreted as a Cauchy principal value integral, for user specified :math:`c` and :math:`f`. The strategy is globally adaptive. Modified Clenshaw-Curtis integration is used on those intervals containing the point :math:`x = c`. **Integration of Complex Function of a Real Variable** A complex valued function, :math:`f`, of a real variable can be written as :math:`f = g + ih`. Similarly, the integral of :math:`f` can be written as .. math:: \int_a^b f(x) dx = \int_a^b g(x) dx + i\int_a^b h(x) dx assuming that the integrals of :math:`g` and :math:`h` exist over the interval :math:`[a,b]` [2]_. Therefore, ``quad`` integrates complex-valued functions by integrating the real and imaginary components separately. References ---------- .. [1] Piessens, Robert; de Doncker-Kapenga, Elise; Überhuber, Christoph W.; Kahaner, David (1983). QUADPACK: A subroutine package for automatic integration. Springer-Verlag. ISBN 978-3-540-12553-2. .. [2] McCullough, Thomas; Phillips, Keith (1973). Foundations of Analysis in the Complex Plane. Holt Rinehart Winston. ISBN 0-03-086370-8 Examples -------- Calculate :math:`\int^4_0 x^2 dx` and compare with an analytic result >>> from scipy import integrate >>> import numpy as np >>> x2 = lambda x: x**2 >>> integrate.quad(x2, 0, 4) (21.333333333333332, 2.3684757858670003e-13) >>> print(4**3 / 3.) # analytical result 21.3333333333 Calculate :math:`\int^\infty_0 e^{-x} dx` >>> invexp = lambda x: np.exp(-x) >>> integrate.quad(invexp, 0, np.inf) (1.0, 5.842605999138044e-11) Calculate :math:`\int^1_0 a x \,dx` for :math:`a = 1, 3` >>> f = lambda x, a: a*x >>> y, err = integrate.quad(f, 0, 1, args=(1,)) >>> y 0.5 >>> y, err = integrate.quad(f, 0, 1, args=(3,)) >>> y 1.5 Calculate :math:`\int^1_0 x^2 + y^2 dx` with ctypes, holding y parameter as 1:: testlib.c => double func(int n, double args[n]){ return args[0]*args[0] + args[1]*args[1];} compile to library testlib.* :: from scipy import integrate import ctypes lib = ctypes.CDLL('/home/.../testlib.*') #use absolute path lib.func.restype = ctypes.c_double lib.func.argtypes = (ctypes.c_int,ctypes.c_double) integrate.quad(lib.func,0,1,(1)) #(1.3333333333333333, 1.4802973661668752e-14) print((1.0**3/3.0 + 1.0) - (0.0**3/3.0 + 0.0)) #Analytic result # 1.3333333333333333 Be aware that pulse shapes and other sharp features as compared to the size of the integration interval may not be integrated correctly using this method. A simplified example of this limitation is integrating a y-axis reflected step function with many zero values within the integrals bounds. >>> y = lambda x: 1 if x<=0 else 0 >>> integrate.quad(y, -1, 1) (1.0, 1.1102230246251565e-14) >>> integrate.quad(y, -1, 100) (1.0000000002199108, 1.0189464580163188e-08) >>> integrate.quad(y, -1, 10000) (0.0, 0.0)
f2 = lambda t,n,m: (jn(n,t)+jn(m,t))/t
def f3(t,n,m):
return (jn(n,t)+jn(m,t))/t
# First specific case for n=1, m=2, x=1
# integrating (j_1(t)+j_2(t))/t
quad(f2, 0, 1, args=(1,2))
(0.5396292385998932, 5.991088054545437e-15)
Let's consider the following integral:
$$f_n(x) = n \displaystyle \int_0^x \frac{j_n(t)}{t} dt$$xs = linspace(1e-10,30,300) # mesh for x-variable
for n in range(1,4): # n in 0,1,2,3
fs=[n * quad(lambda t: jn(n, t)/t, 0, x)[0] for x in xs]
plt.plot(xs,fs, label='n='+str(n))
plt.legend(loc='best')
plt.show()
Higher-dimensional integration works in the same way:
Note that we pass lambda functions for the limits for the y integration, since these in general can be functions of x.
dblquad
:
\begin{equation}
\int_{x_a}^{x_b} dx \int_{y_a(x)}^{y_b(x)} dy f(y,x)
\end{equation}
tplquad
:
\begin{equation}
\int_{x_a}^{x_b} dx \int_{y_a(x)}^{y_b(x)} dy \int_{z_a(x,y)}^{z_b(x,y)} dz f(z,y,x)
\end{equation}
def integrand(x, y, z):
return exp(-x**2-y**2-z**2)
x_a,x_b = 0,10
y_a,y_b = 0,10
z_a,z_b = 0,10
# careful: dblquad requires f(y,x),x_a,x_b,y_a,y_b
# careful: tplquad requires f(z,y,z),x_a,x_b,y_a,y_b,z_a,z_b
val1, abserr = dblquad(lambda y,x: integrand(x,y,0), x_a, x_b, lambda x:y_a, lambda x:y_b)
val2, abserr = tplquad(lambda z,y,x: integrand(x,y,z), x_a, x_b, lambda x:y_a, lambda x:y_b, lambda x,y:z_a, lambda x,y:z_b)
print(val1, val2, abserr)
0.7853981633974476 0.6960409996039546 1.4675161390126584e-08
Ordinary differential equations (ODEs)¶
SciPy provides two different ways to solve ODEs: An API based on the function odeint
, and object-oriented API based on the class ode
. Usually odeint
is easier to get started with, but the ode
class offers some finer level of control.
Here we will use the odeint
functions. For more information about the class ode
, try help(ode)
. It does pretty much the same thing as odeint
, but in an object-oriented fashion.
To use odeint
, first import it from the scipy.integrate
module
from scipy import *
from numpy import *
from scipy.integrate import odeint, ode
A system of ODEs should be formulated in standard form, which is:
$y' = f(y, t)$
where we are searching for function $y(t)$ and where
$y = [y_1(t), y_2(t), ..., y_n(t)]$
and $f$ is some function that gives the derivatives of the function $y_i(t)$. To solve an ODE we need to know the function $f$ and its initial condition, $y(0)$.
Note that higher-order ODEs can always be written in this form by introducing new variables for the intermediate derivatives.
For example, for second order differential equation $$y''(t)=f(y,y',t)$$ we can choose $$Y(t) = [ y(t), y'(t)]==[Y_0(t),Y_1(t)]$$ and then $$\frac{dY(t)}{dt}=[y'(t),y''(t)]==[Y_1(t),f(Y_0(t),Y_1(t),t)].$$
Once we have defined the Python function f
and array y_0
(that is $f$ and $y(0)$ in the mathematical formulation), we can use the odeint
function as:
y_t = odeint(f, y_0, t)
where t
is and array with time-coordinates for which to solve the ODE problem. y_t
is an array with one row for each point in time in t
, where each column corresponds to a solution y_i(t)
at that point in time.
We will see how we can implement f
and y_0
in Python code in the examples below.
Example: double pendulum¶
Let's consider a physical example: The double compound pendulum, described in some detail here: http://en.wikipedia.org/wiki/Double_pendulum
from IPython.display import Image
Image(url='http://upload.wikimedia.org/wikipedia/commons/c/c9/Double-compound-pendulum-dimensioned.svg')
Very short sketch of deriving equations¶
The best way to derive these Newton's equationsis through Lagrangian formulation of mechanics.
$$L = T - V$$where $T$ is kinetic energy and $V$ is potential energy. Note that the total energy is $H=T+V$ and is different object, called Hamiltonian.
The crucial point here is that $L[x_i,\dot{x}_i]$ is a functional of $x_i$ and $\dot{x}_i$ only. Newton's equation of motion are derived by the following derivatives
$$\frac{d}{dt}\left(\frac{\partial L}{\partial \dot{x}_i}\right) = \frac{\partial L}{\partial x_i}$$Example: Harmonic oscilator¶
Kinetic and potential energy are: \begin{eqnarray} T&=&\frac{1}{2} m l^2 \dot{\theta}^2\\ V&=&mgl(1-\cos(\theta) \end{eqnarray} and Lagrangian is: $$L[\theta,\dot{\theta}] = \frac{1}{2} m l^2 \dot{\theta}^2 - mgl(1-\cos(\theta)$$
Partial derivatives are: \begin{eqnarray} &&\frac{\partial L}{\partial\dot{\theta}}=m l^2 \dot{\theta}\\ &&\frac{\partial L}{\partial\theta}= -m g l\sin(\theta) \end{eqnarray}
hence Newton's equations are: \begin{eqnarray} \frac{d}{dt} m l^2 \dot{\theta} = -m g l\sin(\theta) \end{eqnarray} or $$\ddot{\theta} +\frac{g}{l}\sin(\theta)=0$$
Double pendulum¶
The position of the centers of mass are: \begin{eqnarray} \vec{r}_1&=& [\frac{1}{2} l \sin\theta_1,-\frac{1}{2} l \cos\theta_1]\\ \vec{r}_2&=& [l\sin\theta_1+\frac{1}{2} l\sin\theta_2,-l\cos\theta_1-\frac{1}{2}l\cos\theta_2] \end{eqnarray}
Kinetic energy: \begin{eqnarray} T=\frac{1}{2}m (\dot{\vec{r}}_1^2 + \dot{\vec{r}}_2^2)+\frac{1}{2} J (\dot{\theta_1}^2+\dot{\theta_2}^2)= \frac{1}{2}ml^2\left(\left(\frac{1}{2}\dot{\theta}_1\right)^2+ \left(\dot{\theta}_1\cos\theta_1+\dot{\theta}_2\frac{1}{2}\cos\theta_2\right)^2+ \left(\dot{\theta}_1\sin\theta_1+\dot{\theta}_2\frac{1}{2}\sin\theta_2\right)^2+ \frac{1}{12}(\dot{\theta}_1^2+\dot{\theta}_2^2) \right) \end{eqnarray} and Potential energy \begin{eqnarray} V = mg(y_1+y_2)=-mgl(\frac{1}{2}\cos\theta_1+\cos\theta_1+\frac{1}{2}\cos\theta_2) \end{eqnarray}
The Lagrangian is: \begin{eqnarray} L[\theta_1,\theta_2,\dot{\theta}_1,\dot{\theta}_2]=\frac{1}{2}ml^2\left(\frac{4}{3}\dot{\theta}_1^2+\frac{1}{3}\dot{\theta}_2^2+\cos(\theta_1-\theta_2)\dot{\theta}_1\dot{\theta}_2\right)+mgl\left(\frac{3}{2}\cos\theta_1+\frac{1}{2}\cos\theta_2\right) \end{eqnarray} The two second-order equations are derived by $$ \frac{d}{dt}\frac{\partial L}{\partial\dot{\theta}_i}=\frac{\partial L}{\partial\theta_i}$$
We need the following partial derivatives: \begin{eqnarray} \frac{\partial L}{\partial \dot{\theta}_1} = ml^2(\frac{4}{3}\dot{\theta}_1+\frac{1}{2}\cos(\theta_1-\theta_2)\dot{\theta}_2)\\ \frac{\partial L}{\partial \dot{\theta}_2} = ml^2(\frac{1}{3}\dot{\theta}_2+\frac{1}{2}\cos(\theta_1-\theta_2)\dot{\theta}_1)\\ \frac{\partial L}{\partial \theta_1}=-\frac{1}{2}ml^2 \sin(\theta_1-\theta_2)\dot{\theta}_1\dot{\theta}_2-\frac{3}{2}mgl\sin\theta_1\\ \frac{\partial L}{\partial \theta_2}=\frac{1}{2}ml^2 \sin(\theta_1-\theta_2)\dot{\theta}_1\dot{\theta}_2-\frac{1}{2}mgl\sin\theta_2 \end{eqnarray} Hence the equation of motions are \begin{eqnarray} \frac{4}{3}\ddot{\theta}_1+\frac{1}{2}\cos(\theta_1-\theta_2)\ddot{\theta}_2+\frac{1}{2}\sin(\theta_1-\theta_2)\dot{\theta}_2^2=-\frac{g}{l} \frac{3}{2} \sin\theta_1\\ \frac{1}{3}\ddot{\theta}_2+\frac{1}{2}\cos(\theta_1-\theta_2)\ddot{\theta}_1-\frac{1}{2}\sin(\theta_1-\theta_2)\dot{\theta}_1^2=-\frac{g}{l} \frac{1}{2} \sin\theta_2 \end{eqnarray}
Next we change these equations into four first order equations. There are many ways to do that. We follow Hamiltnoian formulation and introduce the generalized momenta: \begin{eqnarray} p_{\theta_1}&\equiv& \frac{\partial L}{\partial \dot{\theta}_1}=ml^2(\frac{4}{3}\dot{\theta}_1+\frac{1}{2}\cos(\theta_1-\theta_2)\dot{\theta}_2)\\ p_{\theta_2}&\equiv& \frac{\partial L}{\partial \dot{\theta}_2}=ml^2(\frac{1}{3}\dot{\theta}_2+\frac{1}{2}\cos(\theta_1-\theta_2)\dot{\theta}_1) \end{eqnarray} We can invert these two equations and express $\dot{\theta}_1$ and $\dot{\theta}_2$ in terms of $p_{\theta_1}$ and $p_{\theta_2}$. We get two of the next four equations. The other two are obtained from the second derivatives above.
${\dot \theta_1} = \frac{6}{m\ell^2} \frac{ 2 p_{\theta_1} - 3 \cos(\theta_1-\theta_2) p_{\theta_2}}{16 - 9 \cos^2(\theta_1-\theta_2)}$
${\dot \theta_2} = \frac{6}{m\ell^2} \frac{ 8 p_{\theta_2} - 3 \cos(\theta_1-\theta_2) p_{\theta_1}}{16 - 9 \cos^2(\theta_1-\theta_2)}.$
${\dot p_{\theta_1}} = -\frac{1}{2} m \ell^2 \left [ {\dot \theta_1} {\dot \theta_2} \sin (\theta_1-\theta_2) + 3 \frac{g}{\ell} \sin \theta_1 \right ]$
${\dot p_{\theta_2}} = -\frac{1}{2} m \ell^2 \left [ -{\dot \theta_1} {\dot \theta_2} \sin (\theta_1-\theta_2) + \frac{g}{\ell} \sin \theta_2 \right]$
To make the Python code simpler to follow, let's introduce new variable names and the vector notation: $x = [\theta_1, \theta_2, p_{\theta_1}, p_{\theta_2}]$
${\dot x_1} = \frac{6}{m\ell^2} \frac{ 2 x_3 - 3 \cos(x_1-x_2) x_4}{16 - 9 \cos^2(x_1-x_2)}$
${\dot x_2} = \frac{6}{m\ell^2} \frac{ 8 x_4 - 3 \cos(x_1-x_2) x_3}{16 - 9 \cos^2(x_1-x_2)}$
${\dot x_3} = -\frac{1}{2} m \ell^2 \left [ {\dot x_1} {\dot x_2} \sin (x_1-x_2) + 3 \frac{g}{\ell} \sin x_1 \right ]$
${\dot x_4} = -\frac{1}{2} m \ell^2 \left [ -{\dot x_1} {\dot x_2} \sin (x_1-x_2) + \frac{g}{\ell} \sin x_2 \right]$
# help(odeint)
def dx(x,t):
"""
The right-hand side of the pendulum ODE
x=[x1,x2,x3,x4]
"""
g, L, m = 9.82, 1., 1.
x1,x2,x3,x4 = x # x is array
c1 = 1/(m*L**2)
ccx = cos(x1-x2)
ddx = 6.*c1/(16.-9.*ccx**2)
dx1 = ddx*(2*x3-3*ccx*x4)
dx2 = ddx*(8*x4-3*ccx*x3)
ddy = dx1*dx2 * sin(x1-x2)
dx3 = -0.5/c1 * ( ddy + 3*g/L * sin(x1))
dx4 = -0.5/c1 * (-ddy + g/L * sin(x2))
return array([dx1,dx2,dx3,dx4])
# choose an initial state
x0 = [pi/2, pi/4, 0, 0]
# time coodinate to solve the ODE for: from 0 to 10 seconds
t = linspace(0, 100, 1000)
# solve the ODE problem
x = odeint(dx, x0, t)
%matplotlib inline
import matplotlib.pyplot as plt
# plot the angles as a function of time
fig, axes = plt.subplots(1,2,figsize=(12,4))
axes[0].plot(t, x[:, 0], label="theta1")
axes[0].plot(t, x[:, 1], label="theta2")
axes[0].legend(loc='best')
axes[0].set_xlabel('time')
L = 0.5
x1 = L * sin(x[:, 0])
y1 = -L * cos(x[:, 0])
x2 = x1 + L * sin(x[:, 1])
y2 = y1 - L * cos(x[:, 1])
axes[1].plot(x1, y1, label="pendulum1")
axes[1].plot(x2, y2, label="pendulum2")
axes[1].set_ylim([-1, 0])
axes[1].set_xlim([-1, 1])
axes[1].legend(loc='best')
plt.show()
matplotlib examples: https://matplotlib.org/3.1.1/gallery/index.html
See animation in pendulum.py
.
(To get it work within jupyter seems a bit challenging at the moment.)
from numpy import *
import matplotlib.pyplot as plt
import scipy.integrate as integrate
import matplotlib.animation as animation #
from numba import jit # This is the new line with numba
g = 9.8 # acceleration due to gravity, in m/s^2
L = 1.0 # length of pendulums
m = 1.0 # mass of pendulums
@jit(nopython=True)
def dx(x,t):
"""
The right-hand side of the pendulum ODE
x=[x1,x2,x3,x4]
"""
x1,x2,x3,x4 = x
c1 = 1/(m*L**2)
dx1 = 6.*c1 * (2*x3-3*cos(x1-x2)*x4)/(16.-9.*cos(x1-x2)**2)
dx2 = 6.*c1 * (8*x4-3*cos(x1-x2)*x3)/(16.-9.*cos(x1-x2)**2)
dx3 = -0.5/c1 * (dx1*dx2 * sin(x1-x2) + 3*g/L * sin(x1))
dx4 = -0.5/c1 * (-dx1*dx2 * sin(x1-x2)+ g/L * sin(x2))
return array([dx1,dx2,dx3,dx4])
# create a time array from 0..100 sampled at 0.1 second steps
# independent variable time
t = linspace(0,20.,800)
dt = t[1]-t[0]
# initial state
x0 = array([pi-0.1, -pi/2, 0, 0])
# integrate your ODE using scipy.integrate.
x = integrate.odeint(dx, x0, t)
x1 = L*sin(x[:,0])
y1 = -L*cos(x[:,0])
x2 = x1 + L*sin(x[:,1])
y2 = y1 - L*cos(x[:,1])
#################################################### copy from jupyter
fig, ax = plt.subplots(1,1)
ax.set_xlim(-2*L,2*L)
ax.set_ylim(-2*L,2*L)
#### alternative way of doing the same thing
#fig = plt.figure()
#ax = fig.add_subplot(111, autoscale_on=False, xlim=(-2*L, 2*L), ylim=(-2*L, 2*L))
ax.grid()
line, = ax.plot([], [], 'o-', lw=2)
time_template = 'time = %.1fs'
time_text = ax.text(0.05, 0.9, '', transform=ax.transAxes) # transform: position text relative to the axes.
def init():
line.set_data([], [])
time_text.set_text('')
return line, time_text
#pass
def animate(i):
thisx = [0, x1[i], x2[i]]
thisy = [0, y1[i], y2[i]]
line.set_data(thisx, thisy)
time_text.set_text(time_template%(i*dt))
return line, time_text
# class FuncAnimation:
# __init__(fig, func, frames=None, init_func=None, fargs=None, save_count=None, *, cache_frame_data=True, **kwargs)
#
# interval = delay between frames in milliseconds: default: 200
# blit=Whether blitting is used to optimize drawing. Note: when using
# blitting, any animated artists will be drawn according to their zorder;
# however, they will be drawn on top of any previous artists, regardless
# of their zorder.
ani = animation.FuncAnimation(fig, animate, arange(1, len(t)), interval=25, init_func=init, blit=True)
from IPython.display import HTML
HTML(ani.to_jshtml())