nitrogen.math¶
General mathematical tools and functions
- nitrogen.math.clenshawcurtis(n, bounds=(- 1, 1))¶
Calculate the quadrature grid points and weights for an n-point Clenshaw-Curtis quadrature.
- Parameters
n (integer) – The number of quadrature points.
bounds ((2,) tuple, optional) – The integration end-points. The default is
(-1,1). If the bounds are in reverse order, the weights will be negative.
- Returns
x (ndarray) – The quadrature points
w (ndarray) – The quadrature weights
Notes
The quadrature rule approximates the integral \(\int_{-1}^{1} f(x) dx\).
See ``Fast Construction of the Fejer and Clenshaw–Curtis Quadrature Rules’’, by Jörg Waldvogel, BIT Num. Math. 46, 195 (2006). doi: 10.1007/s10543-006-0045-4.
- nitrogen.math.constrainedFourier(x, df, period=None)¶
Calculate a single-variable Fourier series that exactly satisfies the value and derivatives at one or more points
- Parameters
x ((n,) array_like) – The matching positions
df ((deriv+1, n) array_like) – The scaled derivative arrays of \(f(x)\) at the matching positions
period (float, optional) – The period of the coordinate x. If None, \(2\pi\) is assumed.
- Returns
c – The Fourier series coefficients.
- Return type
(nc,) ndarray
Notes
The df derivative array contains the scaled derivatives,
df[n]= \(f^{(n)} = \partial_x^n f / n!\).The expansions coefficients are defined as
\[f(x) = c_0 + c_1 \sin \sigma x + c_2 \cos \sigma x + c_3 \sin 2 \sigma x + c_4 \cos 2 \sigma x + \cdots\]where \(\sigma = 2\pi/\)period.
- nitrogen.math.constrainedPolynomial(x, df, x0=None)¶
Calculate a single-variable polynomial that exactly satisfies the value and derivatives at one or more points
- Parameters
x ((n,) array_like) – The matching positions
df ((deriv+1, n) array_like) – The scaled derivative arrays of \(f(x)\) at the matching positions
x0 (scalar, optional) – The expansion point of the polynomial. If None, x0 is zero.
- Returns
c – The power series coefficients for the matching polynomial,
c[0] + c[1]*(x-x0) + c[2]*(x-x0)**2 + ..., where nc = n * (deriv + 1).- Return type
(nc,) ndarray
Notes
The df derivative array contains the scaled derivatives,
df[n]= \(f^{(n)} = \partial_x^n f / n!\).
- nitrogen.math.cumsimp(y, x, axis=0)¶
Calculate the cumulative integral by Simpson’s Rule
- Parameters
y (array_like) – The uniformly spaced samples
x (array_like or scalar) – The sample points along the integration axis or, if scalar, the sample spacing.
axis (int, optional) – The integration axis. The default is 0.
- Returns
res – The result of cumulative integration.
- Return type
ndarray
Notes
The first element of the cumulative integral is fixed to zero. The second element is handled in one of two ways. If the length of y is only 2, then the trapezoid rule is used. Otherwise, the next element in y is used for quadratic interpolation. The remaining elements are evaluated via Simpson’s 1/3 rule.
- nitrogen.math.gaussianFWHM(x, fwhm, norm='area')¶
Calculate a Gaussian function.
- Parameters
x (array_like) – Input array
fwhm (scalar) – The full-width at half-maximum value.
norm ({'area', 'amplitude'}) – The normalization convention. The default is ‘area’. See Notes for definitions.
- Returns
y – The result.
- Return type
ndarray
Notes
For norm =
\`area\`, the integrated area is equal to unity. For norm =\`amplitude\`, the peak amplitude is equal to unity.
- nitrogen.math.levi3()¶
Return the 3-index Levi-Civita \(\epsilon_{ijk}\) tensor.
- Returns
eps – The Levi-Civita tensor
- Return type
(3,3,3) ndarray
- nitrogen.math.mpolyfit(x, y, deg, rcond=None)¶
Multivariable polynomial least-squares fitting.
- Parameters
x ((N,nx) or (N,) array_like) – The input coordinates.
y ((N,) array_like) – The output value
deg (int) – The degree of the polynomial
rcond (float, optional) – rcond parameter passed to
lstsq.
- Returns
p ((nt,) ndarray) – The polynomial coefficients
res ((N,) ndarray) – The residuals.
Notes
The polynomial is ordered using the standard lexical ordering defined by the
autodiffandDFunmodules.
- nitrogen.math.mpolyfit_grad(x, y, yg, deg, scale=None)¶
Multivariable polynomial least-squares fitting including gradient constraints.
- Parameters
x ((N,nx) or (N,) array_like) – The input coordinates.
y ((N,) array_like) – The output value
yg ((N,nx) array_like) – The output gradient
deg (int) – The degree of the polynomial
scale ((nx,) array_like, optional) – The coordinate scale. The default is 1 for each coordinate.
- Returns
p ((nt,) ndarray) – The polynomial coefficients
res ((N,nx+1) ndarray) – The residuals. The first column is the y residual. The second is the gradient residual including scale.
Notes
The polynomial is ordered using the standard lexical ordering defined by the
autodiffandDFunmodules.The gradient data is premultiplied by scale for each coordinate before least-squares optimization.
- nitrogen.math.mpolyval(p, x)¶
Evaluate a polynomial in standard lexical order.
- Parameters
p (array_like) – The polynomial coefficients.
x ((N,nx) or (N,) array_like) – The input values.
- Returns
y – The polynomial value.
- Return type
(N,) ndarray
- nitrogen.math.spech_fft(C, dt, sample_factor=1, damping=0.0)¶
Calculate the intensity spectrum from a hermitian autocorrelation function C.
- Parameters
C (ndarray) – The autocorrelation function.
dt (float) – The time step.
sample_factor (int) – The over-sampling factor. The default is 1.
damping (float) – The Gaussian damping factor. The default is 0.
- Returns
g (ndarray) – The (real) intensity spectrum
freq (ndarray) – The angular frequency axis
Notes
The autocorrelation function is provided for \(t \geq 0\), i.e. C[0] is the \(t = 0\) value. It is assumed that \(C(-t) = C(t)^*\).
The returned spectrum approximates the Fourier transformation
\[g(\omega) = \frac{1}{2\pi}\, \int_{-\infty}^{\infty} dt e^{i \omega t} C(t)\]This normalization means that the angular frequency integral of \(g(\omega)\) equals \(C(0)\), which is typically unity.
The calculated spectrum may be over-sampled by zero-padding the autocorrelation function. This sampling factor is controlled by sample_factor.
A Gaussian window function is also applied to the autocorrelation function, of the form \(C(t) \exp(-a (t/T)^2)\), where \(T\) is length of the correlation function (before any padding). The damping factor \(a\) is controlled by the damping keyword. At sufficiently large \(a\), the line shape becomes Gaussian with an angular frequency full-width at half-maximum equal to \(\omega_\text{FWHM} = 4 T^{-1} \sqrt{a \ln 2}\).