8 - SciPy
Published online by Cambridge University Press: 05 February 2016
Summary
SciPy is a library of Python modules for scientific computing that provides more specific functionality than the generic data structures and mathematical algorithms of NumPy. For example, it contains modules for the evaluation of special functions frequently encountered in science and engineering, optimization, integration, interpolation and image manipulation. As with the NumPy library, many of SciPy's underlying algorithms are executed as compiled C code, so they are fast. Also like NumPy and Python itself, SciPy is free software.
There is little new syntax to learn in using the SciPy routines, so this chapter will focus on examples of the library's use in short programs of relevance to science and engineering.
Physical constants and special functions
The useful scipy.constants package provides the internationally agreed standard values and uncertainties for physical constants. The scipy.special package also supplies a large number of algorithms for calculating functions that appear in science, mathematical analysis and engineering, including:
• Airy functions
• Elliptic functions and integrals
• Bessel functions, their zeros, derivatives and integrals
• Spherical Bessel functions
• Struve functions
• A variety of statistical functions and distributions
• Gamma and beta functions
• The error function
• Fresnel integrals
• Legendre functions and associated Legendre functions
• A variety of orthogonal polynomials
• Hypergeometric functions
• Parabolic cylinder functions
• Matheiu functions
• Spheroidal functions
• Kelvin functions
They are described in detail in the documentation; we focus in this section on a few representative examples.
Most of these special functions are implemented in SciPy as universal functions: that is, they support broadcasting and vectorization (automatic array-looping), and so work as expected with NumPy arrays.
Physical constants
SciPy contains the 2010 CODATA internationally recommended values of many physical constants. They are held, with their units and uncertainties, in a dictionary, scipy.constants.physical_constants, keyed by an identifying string. For example,
In [x]: import scipy.constants as pc
In [x]: pc.physical_constants[’Avogadro constant’]
Out[x]: (6.02214129e+23, ‘mol^-1’, 2.7e+16)
The convenience methods value, unit and precision retrieve the corresponding properties on their own:
In [x]: pc.value(‘elementary charge’)
Out[x]: 1.602176565e-19
In [x]: pc.unit(’elementary charge’)
Out[x]: ‘C’
In [x]: pc.precision(’elementary charge’)
2.1845282701410628e-08
To save typing, it is usual to assign the value to a variable name at the start of a program, for example, …
- Type
- Chapter
- Information
- Learning Scientific Programming with Python , pp. 333 - 401Publisher: Cambridge University PressPrint publication year: 2016
- 1
- Cited by