# Finite-difference approximations

devito.finite_differences.finite_difference.cross_derivative(expr, dims, fd_order, deriv_order, **kwargs)[source]

Arbitrary-order cross derivative of a given expression.

Parameters
• expr (expr-like) – Expression for which the cross derivative is produced.

• dims (tuple of Dimension) – Dimensions w.r.t. which to differentiate.

• fd_order (tuple of ints) – Coefficient discretization order. Note: this impacts the width of the resulting stencil.

• deriv_order (tuple of ints) – Derivative order, e.g. 2 for a second-order derivative.

• matvec (Transpose, optional) – Forward (matvec=direct) or transpose (matvec=transpose) mode of the finite difference. Defaults to direct.

• x0 (dict, optional) – Origin of the finite-difference scheme as a map dim: origin_dim.

• symbolic (bool, optional) – Use default or custom coefficients (weights). Defaults to False.

• expand (bool, optional) – If True, the derivative is fully expanded as a sum of products, otherwise an IndexSum is returned. Defaults to True.

Returns

Cross-derivative of `expr`.

Return type

expr-like

Examples

```>>> from devito import Function, Grid
>>> grid = Grid(shape=(4, 4))
>>> x, y = grid.dimensions
>>> f = Function(name='f', grid=grid, space_order=2)
>>> g = Function(name='g', grid=grid, space_order=2)
>>> cross_derivative(f*g, dims=(x, y), fd_order=(2, 2), deriv_order=(1, 1))
(-1/h_y)*(-f(x, y)*g(x, y)/h_x + f(x + h_x, y)*g(x + h_x, y)/h_x) + (-f(x, y + h_y)*g(x, y + h_y)/h_x + f(x + h_x, y + h_y)*g(x + h_x, y + h_y)/h_x)/h_y
```

Semantically, this is equivalent to

```>>> (f*g).dxdy
Derivative(f(x, y)*g(x, y), x, y)
```

The only difference is that in the latter case derivatives remain unevaluated. The expanded form is obtained via `evaluate`

```>>> (f*g).dxdy.evaluate
(-1/h_y)*(-f(x, y)*g(x, y)/h_x + f(x + h_x, y)*g(x + h_x, y)/h_x) + (-f(x, y + h_y)*g(x, y + h_y)/h_x + f(x + h_x, y + h_y)*g(x + h_x, y + h_y)/h_x)/h_y
```

Finally the x0 argument allows to choose the origin of the finite-difference

```>>> cross_derivative(f*g, dims=(x, y), fd_order=(2, 2), deriv_order=(1, 1),     x0={x: 1, y: 2})
(-1/h_y)*(-f(1, 2)*g(1, 2)/h_x + f(h_x + 1, 2)*g(h_x + 1, 2)/h_x) + (-f(1, h_y + 2)*g(1, h_y + 2)/h_x + f(h_x + 1, h_y + 2)*g(h_x + 1, h_y + 2)/h_x)/h_y
```
devito.finite_differences.finite_difference.first_derivative(expr, dim, fd_order=None, side=centered, matvec=direct, x0=None, symbolic=False, expand=True)[source]

First-order derivative of a given expression.

Parameters
• expr (expr-like) – Expression for which the first-order derivative is produced.

• dim (Dimension) – The Dimension w.r.t. which to differentiate.

• fd_order (int, optional) – Coefficient discretization order. Note: this impacts the width of the resulting stencil. Defaults to expr.space_order.

• side (Side, optional) – Side of the finite difference location, centered (at x), left (at x - 1) or right (at x +1). Defaults to centered.

• matvec (Transpose, optional) – Forward (matvec=direct) or transpose (matvec=transpose) mode of the finite difference. Defaults to direct.

• x0 (dict, optional) – Origin of the finite-difference scheme as a map dim: origin_dim.

• symbolic (bool, optional) – Use default or custom coefficients (weights). Defaults to False.

• expand (bool, optional) – If True, the derivative is fully expanded as a sum of products, otherwise an IndexSum is returned. Defaults to True.

Returns

First-order derivative of `expr`.

Return type

expr-like

Examples

```>>> from devito import Function, Grid, first_derivative, transpose
>>> grid = Grid(shape=(4, 4))
>>> x, _ = grid.dimensions
>>> f = Function(name='f', grid=grid)
>>> g = Function(name='g', grid=grid)
>>> first_derivative(f*g, dim=x)
-f(x, y)*g(x, y)/h_x + f(x + h_x, y)*g(x + h_x, y)/h_x
```

Semantically, this is equivalent to

```>>> (f*g).dx
Derivative(f(x, y)*g(x, y), x)
```

The only difference is that in the latter case derivatives remain unevaluated. The expanded form is obtained via `evaluate`

```>>> (f*g).dx.evaluate
-f(x, y)*g(x, y)/h_x + f(x + h_x, y)*g(x + h_x, y)/h_x
```

For the adjoint mode of the first derivative, pass `matvec=transpose`

```>>> g = Function(name='g', grid=grid)
>>> first_derivative(f*g, dim=x, matvec=transpose)
-f(x, y)*g(x, y)/h_x + f(x - h_x, y)*g(x - h_x, y)/h_x
```

This is also accessible via the .T shortcut

```>>> (f*g).dx.T.evaluate
-f(x, y)*g(x, y)/h_x + f(x - h_x, y)*g(x - h_x, y)/h_x
```

Finally the x0 argument allows to choose the origin of the finite-difference

```>>> first_derivative(f, dim=x, x0={x: 1})
-f(1, y)/h_x + f(h_x + 1, y)/h_x
```
devito.finite_differences.finite_difference.generate_indices(expr, dim, order, side=None, matvec=None, x0=None)[source]

Indices for the finite-difference scheme.

Parameters
• expr (expr-like) – Expression that is differentiated.

• dim (Dimension) – Dimensions w.r.t which the derivative is taken.

• order (int) – Order of the finite-difference scheme.

• side (Side, optional) – Side of the scheme (centered, left, right).

• matvec (Transpose, optional) – Forward (matvec=direct) or transpose (matvec=transpose) mode of the finite difference. Defaults to direct.

• x0 (dict of {Dimension: Dimension or Expr or Number}, optional) – Origin of the scheme, ie. x, x + .5 * x.spacing, …

Return type

An IndexSet, representing an ordered list of indices.

devito.finite_differences.finite_difference.generic_derivative(expr, dim, fd_order, deriv_order, matvec=direct, x0=None, symbolic=False, expand=True)[source]

Arbitrary-order derivative of a given expression.

Parameters
• expr (expr-like) – Expression for which the derivative is produced.

• dim (Dimension) – The Dimension w.r.t. which to differentiate.

• fd_order (int) – Coefficient discretization order. Note: this impacts the width of the resulting stencil.

• deriv_order (int) – Derivative order, e.g. 2 for a second-order derivative.

• matvec (Transpose, optional) – Forward (matvec=direct) or transpose (matvec=transpose) mode of the finite difference. Defaults to direct.

• x0 (dict, optional) – Origin of the finite-difference scheme as a map dim: origin_dim.

• symbolic (bool, optional) – Use default or custom coefficients (weights). Defaults to False.

• expand (bool, optional) – If True, the derivative is fully expanded as a sum of products, otherwise an IndexSum is returned. Defaults to True.

Returns

`deriv-order` derivative of `expr`.

Return type

expr-like