Differential Operators
- class findiff.Diff(axis=0, grid=None, acc=2)
Represents a partial derivative (along one axis).
For higher derivatives, exponentiate. For mixed partial derivatives, multiply. See examples below.
Examples
Set up grid (equidistant here): >>> import numpy as np >>> x = np.linspace(0, 10, 100)
The array to differentiate >>> f = np.sin(x) # as an example
# Define the first derivative: >>> from findiff import Diff >>> d_dx = Diff(0) >>> d_dx = d_dx.set_grid({0: x[1] - x[0]})
# now apply it: >>> df_dx = d_dx(f)
# The second derivative is constructed by exponentiation: >>> d2_dx2 = d_dx ** 2 >>> d2f_dx2 = d2_dx2(f)
In multiple dimensions with meshed grids, the usage is accordingly: >>> x = y = z = np.linspace(0, 10, 100) >>> dx = dy = dz = x[1] - x[0] >>> X, Y, Z = np.meshgrid(x, y, z, indexing=’ij’) >>> f = np.sin(X) * np.sin(Y) * np.sin(Z) >>> d_dx = Diff(0) >>> d_dy = Diff(1) >>> d_dz = Diff(2) >>> d3_dxdydz = d_dx * d_dy * d_dz >>> d3_dxdydz.set_grid({0: dx, 1: dy, 2: dz}) >>> d3f_dxdydz = d3_dxdydz(f)
Initializes a Diff instance.
Parameters
- axis: int
The 0-based index of the axis along which to take the derivative.
- grid: (optional) float | numpy.ndarray
Specifies the grid to use. A float value assumes an equidistant grid and denoted the grid spacing along the given axis. A 1-D numpy array assumes an non-equidistant (tensor product) grid and denotes the coordinates along the given axis.
- acc: (optional) int
The accuracy order to use. Must be a positive even number.
- DEFAULT_ACC = 2
- property grid
- matrix(shape)
Returns a matrix representation of the differential operator for a given grid shape.
- property order
- set_accuracy(acc)
Sets the requested accuracy for the given differential operator expression.
Parameters
- acc: int
The accuracy order. Must be a positive, even number.
- set_grid(grid)
Sets the grid for the given differential operator expression.
Parameters
- grid: dict | Grid
Specifies the grid to use. If a dict is given, an equidistant grid is assumed and the dict specifies the spacings along the required axes.
- stencil(shape)
Returns a stencil representation of the differential operator for a given grid shape.
- class findiff.Gradient(**kwargs)
The N-dimensional gradient.
\[\nabla = \left(\frac{\partial}{\partial x_0}, \frac{\partial}{\partial x_1}, ... , \frac{\partial}{\partial x_{N-1}}\right)\]- Parameters:
kwargs –
exactly one of h and coords must be specified
- h
list with the grid spacings of an N-dimensional uniform grid
- coords
list of 1D arrays with the coordinate values along the N axes. This is used for non-uniform grids.
- acc
accuracy order, must be positive integer, default is 2
Constructor for the VectorOperator base class.
kwargs:
h list with the grid spacings of an N-dimensional uniform grid
- coords list of 1D arrays with the coordinate values along the N axes.
This is used for non-uniform grids.
Either specify “h” or “coords”, not both.
- class findiff.Divergence(**kwargs)
The N-dimensional divergence.
\[{\rm \bf div} = \nabla \cdot\]- Parameters:
kwargs –
exactly one of h and coords must be specified
- h
list with the grid spacings of an N-dimensional uniform grid
- coords
list of 1D arrays with the coordinate values along the N axes. This is used for non-uniform grids.
- acc
accuracy order, must be positive integer, default is 2
Constructor for the VectorOperator base class.
kwargs:
h list with the grid spacings of an N-dimensional uniform grid
- coords list of 1D arrays with the coordinate values along the N axes.
This is used for non-uniform grids.
Either specify “h” or “coords”, not both.
- class findiff.Curl(**kwargs)
The curl operator.
\[{\rm \bf rot} = \nabla \times\]Is only defined for 3D.
- Parameters:
kwargs –
exactly one of h and coords must be specified
- h
list with the grid spacings of a 3-dimensional uniform grid
- coords
list of 1D arrays with the coordinate values along the 3 axes. This is used for non-uniform grids.
- acc
accuracy order, must be positive integer, default is 2
Constructor for the VectorOperator base class.
kwargs:
h list with the grid spacings of an N-dimensional uniform grid
- coords list of 1D arrays with the coordinate values along the N axes.
This is used for non-uniform grids.
Either specify “h” or “coords”, not both.
- class findiff.Laplacian(h=[1.0], acc=2)
The N-dimensional Laplace operator.
\[{\rm \bf \nabla^2} = \sum_{k=0}^{N-1} \frac{\partial^2}{\partial x_k^2}\]- Parameters:
kwargs –
exactly one of h and coords must be specified
- h
list with the grid spacings of an N-dimensional uniform grid
- coords
list of 1D arrays with the coordinate values along the N axes. This is used for non-uniform grids.
- acc
accuracy order, must be positive integer, default is 2