Convolution Operators

convolve(kernel, shape, order='C')[source]

Convolve two N-dimensional arrays as a LinearOperator.

Note that this only implements the “same” convolution mode seen in other functions. This is often the desired mode for linear systems since the problem does not alter dimensions when the convolution is applied.

For this operator to work, the number of dimensions in the kernel must match the number of fields in the shape tuple.

Parameters:

kernel : ndarray

The kernel by which to do the convolving.

shape : tuple

The shape of the array in non-vector form.

order = {‘C’, ‘F’, ‘A’}, optional

The order by which the vectorized array is reshaped. This is the same parameter as given to functions like numpy.reshape. For a discussion of the memory efficiency of different orders and how that is determined by the underlying format, see the documentation of commands that take an order argument.

Returns:

LinearOperator

A LinearOperator implementing the convolution on vectorized inputs.

Raises:

ValueError

When the inputs are not the same dimension.

See also

scipy.signal.convolve
The array based version of this operation.
fft()
LinearOperator version of fftn.
ifft()
LinearOperator version of ifftn.

Examples

>>> import numpy as np
>>> from pyop.operators import convolve
>>> A = np.linspace(1,9,9).reshape(3,3)
>>> kernel = np.array([[-1, 1]])
>>> C = convolve(kernel, (3, 3))
>>> C(np.ravel(A))
array([-1., -1., -1., -4., -1., -1., -7., -1., -1.])
>>> C(np.ravel(A)).reshape(3,3)
array([[-1., -1., -1.],
       [-4., -1., -1.],
       [-7., -1., -1.]])
gradient(derivative, points, shape, step=None, order='C')[source]

Approximate the derivative with a central difference.

Parameters:

derivative : int

The order of the derivative to approximate.

points : int

The number of points in the central difference. Must be an odd integer greater than derivative.

shape : tuple

The shape of the array in non-vector form.

step: tuple, optional

The step sizes between adjacent values along each dimension in the array. Passing None defaults to step size 1.0 along all dimensions.

order = {‘C’, ‘F’, ‘A’}, optional

The order by which the vectorized array is reshaped. This is the same parameter as given to functions like numpy.reshape. For a discussion of the memory efficiency of different orders and how that is determined by the underlying format, see the documentation of commands that take an order argument.

Returns:

LinearOperator

A LinearOperator implementing the central difference on vectorized inputs.

Raises:

ValueError

When the inputs are not the right dimensions.

See also

convolve
The central difference is calculated using this LinearOperator.
scipy.misc.central_diff_weights
The scipy function returning the required weights for calculating the central difference.

Examples

>>> import numpy as np
>>> from pyop.operators import gradient
>>> grad = gradient(1, 3, (5,6))
>>> laplace = gradient(2, 3, (5,6), step=(0.5,2))
>>> A = np.indices((5,6)).sum(0)**2 / 2.
>>> grad(np.ravel(A)).reshape(5,6)
array([[  0.5 ,   2.  ,   4.25,   7.  ,  10.25,   5.  ],
       [  2.  ,   4.  ,   6.  ,   8.  ,  10.  ,  -0.25],
       [  4.25,   6.  ,   8.  ,  10.  ,  12.  ,  -2.  ],
       [  7.  ,   8.  ,  10.  ,  12.  ,  14.  ,  -4.25],
       [  4.  ,   1.  ,  -0.25,  -2.  ,  -4.25, -32.  ]])
>>> laplace(np.ravel(A)).reshape(5,6)
array([[   2.125,    4.25 ,    2.25 ,   -3.75 ,  -13.75 ,  -32.25 ],
       [   4.25 ,    4.25 ,    4.25 ,    4.25 ,    4.25 ,   -1.875],
       [   4.125,    4.25 ,    4.25 ,    4.25 ,    4.25 ,   -3.75 ],
       [   3.75 ,    4.25 ,    4.25 ,    4.25 ,    4.25 ,   -5.875],
       [ -46.875,  -67.75 ,  -93.75 , -123.75 , -157.75 , -208.25 ]])

Matrix Free Linear Operators

Navigation