Module operators

class findiff.operators.Add(left, right)
matrix(shape)

Returns a matrix representation of the differential operator for a given grid shape.

class findiff.operators.BinaryOperation(*args, **kwargs)
class findiff.operators.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.

matrix(shape)

Returns a matrix representation of the differential operator for a given grid shape.

class findiff.operators.FieldOperator(value)

An operator that multiplies an array pointwise.

matrix(shape)

Returns a matrix representation of the differential operator for a given grid shape.

class findiff.operators.Identity

The identity operator.

class findiff.operators.Mul(left, right)
matrix(shape)

Returns a matrix representation of the differential operator for a given grid shape.

class findiff.operators.Node(*args, **kwargs)

Represents a differential operator expression.

abstract matrix(shape)

Returns a matrix representation of the differential operator for a given grid shape.

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.operators.ScalarOperator(value)

A multiple of the identity operator.

matrix(shape)

Returns a matrix representation of the differential operator for a given grid shape.

class findiff.operators.Identity

The identity operator.