Utilities¶
Calculus on time series¶
- GWFrames.ScalarDerivative(f: ndarray, t: ndarray) ndarray¶
Differentiate a real function of time by finite differences.
The times need not be uniformly spaced. When there are at least five points, the fourth-order five-point formula of Eq. (A 5b) of Bowen and Smith, “Derivative formulas and errors for non-uniformly spaced points”, is used. With three or four points a three-point formula is used, and with two points the derivative is the slope of the line through them. Fewer than two points raise an error.
- Parameters:
f (array_like of
float) – Values of the function.t (array_like of
float) – Times corresponding to the elements off; the two must have the same length.
- Returns:
- GWFrames.ComplexDerivative(f: ndarray, t: ndarray) ndarray¶
Differentiate a complex function of time by finite differences.
This is the complex counterpart of
ScalarDerivative, and uses the same formulas.- Parameters:
f (array_like of
complex) – Values of the function.t (array_like of
float) – Times corresponding to the elements off; the two must have the same length.
- Returns:
- GWFrames.ScalarIntegral(fdot: ndarray, t: ndarray) ndarray¶
Integrate a scalar function of time by the trapezoidal rule.
The integral is cumulative: element
iof the result is the integral fromt[0]tot[i], so that the first element is zero.- Parameters:
fdot (array_like of
float) – Values of the integrand.t (array_like of
float) – Times corresponding to the elements offdot; the two must have the same length.
- Returns:
See also
CumulativeScalarIntegralThe integral over the whole time span.
- GWFrames.CumulativeScalarIntegral(fdot: ndarray, t: ndarray) float¶
Integrate a scalar function over its whole time span by the trapezoidal rule.
Despite the name, only the total is returned: the result is the integral from
t[0]tot[-1], which is the last element of the result ofScalarIntegral.- Parameters:
fdot (array_like of
float) – Values of the integrand.t (array_like of
float) – Times corresponding to the elements offdot; the two must have the same length.
- Returns:
float– The integral over the whole time span.
- GWFrames.VectorIntegral(fdot: ndarray, t: ndarray) ndarray¶
Integrate a vector function of time by the trapezoidal rule.
Each component is integrated separately, as in
ScalarIntegral. The integral is cumulative: rowiof the result is the integral fromt[0]tot[i], so that the first row is zero.- Parameters:
fdot (array_like of
float,shape (N,M)) – Values of the integrand, with time along the first axis.t (array_like of
float,shape (N,)) – Times corresponding to the rows offdot.
- Returns:
- GWFrames.CumulativeVectorIntegral(fdot: ndarray, t: ndarray) ndarray¶
Integrate a vector function over its whole time span by the trapezoidal rule.
Despite the name, only the total is returned: the result is the integral from
t[0]tot[-1], which is the last row of the result ofVectorIntegral.- Parameters:
fdot (array_like of
float,shape (N,M)) – Values of the integrand, with time along the first axis.t (array_like of
float,shape (N,)) – Times corresponding to the rows offdot.
- Returns:
ndarrayoffloat,shape (M,)– The integral over the whole time span.
- GWFrames.Unwrap(In: ndarray) ndarray¶
Unwrap a phase so that it is (roughly) continuous.
Multiples of \(2\pi\) are added to the elements of
Inso that the difference between successive elements of the result lies between \(-\pi\) and \(\pi\). This is the algorithm of MATLAB’sunwrap.- Parameters:
In (array_like of
float) – The phase, in radians.- Returns:
ndarrayoffloat– The unwrapped phase, with the same length asIn.
Time series¶
- GWFrames.Intersection(
- t1: ndarray,
- t2: ndarray,
- MinStep: float = 0.005,
- MinTime: float = -1e+300,
- MaxTime: float = 1e+300,
Return a time series covering the intersection of two time series.
The result starts at the latest of
t1[0],t2[0], andMinTime, and ends no later than the earliest oft1[-1],t2[-1], andMaxTime. The step from each time to the next is the smaller of the local steps oft1andt2at that time, orMinStepif that is larger. Both inputs are assumed to be strictly increasing. An error is raised if either input is empty or the intersection is empty.- Parameters:
t1 (array_like of
float) – The first time series.t2 (array_like of
float) – The second time series.MinStep (
float, default0.005) – Smallest time step in the result.MinTime (
float, default-1e300) – Earliest time allowed in the result.MaxTime (
float, default1e300) – Latest time allowed in the result.
- Returns:
See also
- GWFrames.Union(t1: ndarray, t2: ndarray, MinStep: float = 0.005) ndarray¶
Return a time series covering the union of two time series.
The result runs from the earlier of
t1[0]andt2[0]to the later oft1[-1]andt2[-1]. Where only one input covers a time, the step from that time to the next is the local step of that input; where both do, it is the smaller of the two local steps. In either case the step is never smaller thanMinStep. Both inputs are assumed to be strictly increasing. If one input is empty, the other is returned; if the two do not overlap, an error is raised.- Parameters:
t1 (array_like of
float) – The first time series.t2 (array_like of
float) – The second time series.MinStep (
float, default0.005) – Smallest time step in the result.
- Returns:
See also
Matrices¶
- class GWFrames.Matrix¶
Real matrix stored in a GSL matrix.
This class is a thin wrapper around a GSL matrix of floats. It is used as the input to the eigen-system helpers, such as
DominantPrincipalAxisandEigensystem, most of which require a symmetric \(3\times3\) matrix. It is rarely needed directly. The element in rowrowand columncolis returned byM(row, col), and the methodMatrix.numpyreturns a copy of all the elements as a numpy array. The differenceM1 - M2of two matrices of the same shape is taken element by element, and the productM * bwith a sequencebofM.ncols()floats is the matrix-vector product, returned as a numpy array.A matrix with zero rows or zero columns is empty: it has no storage, and it reports zero rows and zero columns. An empty matrix may be copied, printed, converted to a numpy array of shape
(0, 0), resized, and cleared.A
Matrixmay be constructed in any of these ways:Matrix()An empty matrix, which may be given a size with
Matrix.resize.Matrix(rows, cols)A matrix of zeros with
rowsrows andcolscolumns.Matrix(rows, cols, a)A matrix with
rowsrows andcolscolumns, each of whose elements is the floata.Matrix(M)A copy of the matrix
M.Matrix(DataIn)A matrix with the elements of
DataIn, a sequence of rows, each of which is a sequence of floats. All rows must have the same length, orValueErroris raised. The matrix is empty ifDataInor its rows are empty.
- classmethod __new__(*args, **kwargs)¶
- Matrix.ncols() int¶
Return the number of columns of this matrix.
- Returns:
int– The number of columns.
- Matrix.resize(newNRows: int, newNCols: int, value: float = 0.0) None¶
Change the shape of this matrix, discarding its contents.
The previous elements are not preserved; every element of the resized matrix is set to
value. If either new dimension is zero, the matrix becomes empty.
- Matrix.clear() None¶
Release the storage of this matrix, leaving it empty.
Afterward the matrix has zero rows and zero columns, and it may be given a new size with
Matrix.resize. Clearing an empty matrix does nothing.
- Matrix.numpy() numpy.typing.NDArray[numpy.float64]¶
Return a copy of this Matrix as a two-dimensional numpy array.
- GWFrames.Eigenvalues(M: Matrix) ndarray¶
Return the eigenvalues of a symmetric matrix in decreasing order.
- Parameters:
M (
Matrix) – A symmetric \(3\times3\) matrix. It is modified; see the notes.- Returns:
ndarrayoffloat,shape (3,)– The eigenvalues, from largest to smallest.
Notes
The eigen-system is computed by GSL’s
gsl_eigen_symmv, which uses only the diagonal and lower triangle ofMand overwrites them. Pass a copy if the matrix will be needed afterward.
- GWFrames.Eigenvectors(M: Matrix) ndarray¶
Return the eigenvectors of a symmetric matrix.
The eigenvectors are normalized and ordered by decreasing eigenvalue, and are returned one after another in a flat array: elements
3*jthrough3*j+2are the eigenvector with thej-th largest eigenvalue.- Parameters:
M (
Matrix) – A symmetric \(3\times3\) matrix. It is modified; see the notes.- Returns:
ndarrayoffloat,shape (9,)– The three eigenvectors, concatenated.
Notes
The eigen-system is computed by GSL’s
gsl_eigen_symmv, which uses only the diagonal and lower triangle ofMand overwrites them. Pass a copy if the matrix will be needed afterward.
- GWFrames.Eigensystem(M: Matrix) ndarray¶
Return the eigenvectors and eigenvalues of a symmetric matrix.
The eigenpairs are ordered by decreasing eigenvalue, and are returned one after another in a flat array: elements
4*jthrough4*j+2are the normalized eigenvector with thej-th largest eigenvalue, and element4*j+3is that eigenvalue.- Parameters:
M (
Matrix) – A symmetric \(3\times3\) matrix. It is modified; see the notes.- Returns:
ndarrayoffloat,shape (12,)– The three eigenpairs, concatenated.
Notes
The eigen-system is computed by GSL’s
gsl_eigen_symmv, which uses only the diagonal and lower triangle ofMand overwrites them. Pass a copy if the matrix will be needed afterward.
- GWFrames.DominantPrincipalAxis(M: Matrix) ndarray¶
Return the eigenvector of a symmetric matrix with the largest eigenvalue.
- Parameters:
M (
Matrix) – A symmetric \(3\times3\) matrix. It is modified; see the notes.- Returns:
ndarrayoffloat,shape (3,)– The normalized eigenvector belonging to the largest eigenvalue.
Notes
The eigen-system is computed by GSL’s
gsl_eigen_symmv, which uses only the diagonal and lower triangle ofMand overwrites them. Pass a copy if the matrix will be needed afterward.
- GWFrames.SubordinatePrincipalAxis(M: Matrix) ndarray¶
Return the eigenvector of a symmetric matrix with the smallest eigenvalue.
- Parameters:
M (
Matrix) – A symmetric \(3\times3\) matrix. It is modified; see the notes.- Returns:
ndarrayoffloat,shape (3,)– The normalized eigenvector belonging to the smallest eigenvalue.
Notes
The eigen-system is computed by GSL’s
gsl_eigen_symmv, which uses only the diagonal and lower triangle ofMand overwrites them. Pass a copy if the matrix will be needed afterward.
- GWFrames.DominantPrincipalValue(M: Sequence[Matrix]) ndarray¶
Return the largest eigenvalue of each of a sequence of symmetric matrices.
Each matrix must be a symmetric \(3\times3\)
Matrix; only the shape of the first is checked. The eigenvalues are computed with GSL from the diagonal and lower triangle of each matrix.
- GWFrames.SubordinatePrincipalValue(M: Sequence[Matrix]) ndarray¶
Return the smallest eigenvalue of each of a sequence of symmetric matrices.
Each matrix must be a symmetric \(3\times3\)
Matrix; only the shape of the first is checked. The eigenvalues are computed with GSL from the diagonal and lower triangle of each matrix.