# pymor.operators.numpy¶

Operators based on NumPy arrays.

This module provides the following NumPy-based Operators:

## Module Contents¶

class pymor.operators.numpy.NumpyGenericOperator(mapping, adjoint_mapping=None, dim_source=1, dim_range=1, linear=False, parameters={}, source_id=None, range_id=None, solver_options=None, name=None)[source]

Wraps an arbitrary Python function between NumPy arrays as an Operator.

Parameters

mapping

The function to wrap. If parameters is None, the function is of the form mapping(U) and is expected to be vectorized. In particular:

mapping(U).shape == U.shape[:-1] + (dim_range,).


If parameters is not None, the function has to have the signature mapping(U, mu).

The adjoint function to wrap. If parameters is None, the function is of the form adjoint_mapping(U) and is expected to be vectorized. In particular:

adjoint_mapping(U).shape == U.shape[:-1] + (dim_source,).


If parameters is not None, the function has to have the signature adjoint_mapping(U, mu).

dim_source

Dimension of the operator’s source.

dim_range

Dimension of the operator’s range.

linear

Set to True if the provided mapping and adjoint_mapping are linear.

parameters

The Parameters the operator depends on.

solver_options

The solver_options for the operator.

name

Name of the operator.

Methods

 apply Apply the operator to a VectorArray. apply_adjoint Apply the adjoint operator.
apply(U, mu=None)[source]

Apply the operator to a VectorArray.

Parameters

U

VectorArray of vectors to which the operator is applied.

mu

The parameter values for which to evaluate the operator.

Returns

VectorArray of the operator evaluations.

For any given linear Operator op, parameter values mu and VectorArrays U, V in the source resp. range we have:

op.apply_adjoint(V, mu).dot(U) == V.inner(op.apply(U, mu))


Thus, when op is represented by a matrix M, apply_adjoint is given by left-multiplication of (the complex conjugate of) M with V.

Parameters

V

VectorArray of vectors to which the adjoint operator is applied.

mu

The parameter values for which to apply the adjoint operator.

Returns

VectorArray of the adjoint operator evaluations.

class pymor.operators.numpy.NumpyHankelOperator(h, source_id=None, range_id=None, name=None)[source]

Implicit representation of a Hankel operator by a NumPy array.

Let

$h = \begin{pmatrix} h_1 & h_2 & \dots & h_n \end{pmatrix},\quad h_i\in\mathbb{C}^{p\times m},\,i=1,\,\dots,\,n,\quad n,m,p\in\mathbb{N}$

be a finite (matrix-valued) sequence. If $$h$$ has an odd number $$n=2s-1$$ of elements, the corresponding Hankel operator can be represented by the matrix

$\begin{split}H = \begin{bmatrix} h_1 & h_2 & \dots & h_s \\ h_2 & h_3 & \dots & h_{s+1}\\ \vdots & \vdots & \ddots & \vdots\\ h_s & h_{s+1} & \dots & h_{2s-1} \end{bmatrix}\in\mathbb{C}^{ms\times ps}.\end{split}$

For an even number $$n=2s$$ of elements, the corresponding matrix representation is given by

$\begin{split}H = \begin{bmatrix} h_1 & h_2 & \dots & h_s & h_{s+1}\\ h_2 & h_3 & \dots & h_{s+1} & h_{s+2}\\ \vdots & \vdots & \ddots & \vdots & \vdots\\ h_s & h_{s+1} & \dots & h_{2s-1} & h_{2s}\\ h_{s+1} & h_{s+2} & \dots & h_{2s} & 0 \end{bmatrix}\in\mathbb{C}^{m(s+1)\times p(s+1)}.\end{split}$

The matrix $$H$$ as seen above is not explicitly constructed, only the sequence h is stored. Efficient matrix-vector multiplications are realized via circulant matrices with DFT in the class’ apply method (see [MSKC21] Algorithm 3.1. for details).

Parameters

h

The NumPy array that defines the Hankel operator. Has to be one- or three-dimensional with either:

h.shape = (n,)

for scalar-valued sequences or:

h.shape = (n, p, m)


for matrix-valued of sequences with elements of dimension $$p\times m$$.

source_id

The id of the operator’s source VectorSpace.

range_id

The id of the operator’s range VectorSpace.

name

Name of the operator.

Methods

 H apply Apply the operator to a VectorArray. apply_adjoint Apply the adjoint operator.
property H[source]
apply(U, mu=None)[source]

Apply the operator to a VectorArray.

Parameters

U

VectorArray of vectors to which the operator is applied.

mu

The parameter values for which to evaluate the operator.

Returns

VectorArray of the operator evaluations.

For any given linear Operator op, parameter values mu and VectorArrays U, V in the source resp. range we have:

op.apply_adjoint(V, mu).dot(U) == V.inner(op.apply(U, mu))


Thus, when op is represented by a matrix M, apply_adjoint is given by left-multiplication of (the complex conjugate of) M with V.

Parameters

V

VectorArray of vectors to which the adjoint operator is applied.

mu

The parameter values for which to apply the adjoint operator.

Returns

VectorArray of the adjoint operator evaluations.

class pymor.operators.numpy.NumpyMatrixBasedOperator[source]

Base class for operators which assemble into a NumpyMatrixOperator.

sparse[source]

True if the operator assembles into a sparse matrix, False if the operator assembles into a dense matrix, None if unknown.

Methods

 H apply Apply the operator to a VectorArray. apply_adjoint Apply the adjoint operator. apply_inverse Apply the inverse operator. as_range_array Return a VectorArray representation of the operator in its range space. as_source_array Return a VectorArray representation of the operator in its source space. assemble Assemble the operator for given parameter values. export_matrix Save the matrix of the operator to a file.
linear = True[source]
property H[source]
apply(U, mu=None)[source]

Apply the operator to a VectorArray.

Parameters

U

VectorArray of vectors to which the operator is applied.

mu

The parameter values for which to evaluate the operator.

Returns

VectorArray of the operator evaluations.

For any given linear Operator op, parameter values mu and VectorArrays U, V in the source resp. range we have:

op.apply_adjoint(V, mu).dot(U) == V.inner(op.apply(U, mu))


Thus, when op is represented by a matrix M, apply_adjoint is given by left-multiplication of (the complex conjugate of) M with V.

Parameters

V

VectorArray of vectors to which the adjoint operator is applied.

mu

The parameter values for which to apply the adjoint operator.

Returns

VectorArray of the adjoint operator evaluations.

apply_inverse(V, mu=None, initial_guess=None, least_squares=False)[source]

Apply the inverse operator.

Parameters

V

VectorArray of vectors to which the inverse operator is applied.

mu

The parameter values for which to evaluate the inverse operator.

initial_guess

VectorArray with the same length as V containing initial guesses for the solution. Some implementations of apply_inverse may ignore this parameter. If None a solver-dependent default is used.

least_squares

If True, solve the least squares problem:

u = argmin ||op(u) - v||_2.


Since for an invertible operator the least squares solution agrees with the result of the application of the inverse operator, setting this option should, in general, have no effect on the result for those operators. However, note that when no appropriate solver_options are set for the operator, most implementations will choose a least squares solver by default which may be undesirable.

Returns

VectorArray of the inverse operator evaluations.

Raises

InversionError

The operator could not be inverted.

as_range_array(mu=None)[source]

Return a VectorArray representation of the operator in its range space.

In the case of a linear operator with NumpyVectorSpace as source, this method returns for given parameter values mu a VectorArray V in the operator’s range, such that

V.lincomb(U.to_numpy()) == self.apply(U, mu)


for all VectorArrays U.

Parameters

mu

The parameter values for which to return the VectorArray representation.

Returns

V

The VectorArray defined above.

as_source_array(mu=None)[source]

Return a VectorArray representation of the operator in its source space.

In the case of a linear operator with NumpyVectorSpace as range, this method returns for given parameter values mu a VectorArray V in the operator’s source, such that

self.range.make_array(V.inner(U).T) == self.apply(U, mu)


for all VectorArrays U.

Parameters

mu

The parameter values for which to return the VectorArray representation.

Returns

V

The VectorArray defined above.

assemble(mu=None)[source]

Assemble the operator for given parameter values.

The result of the method strongly depends on the given operator. For instance, a matrix-based operator will assemble its matrix, a LincombOperator will try to form the linear combination of its operators, whereas an arbitrary operator might simply return a FixedParameterOperator. The only assured property of the assembled operator is that it no longer depends on a Parameter.

Parameters

mu

The parameter values for which to assemble the operator.

Returns

Parameter-independent, assembled Operator.

export_matrix(filename, matrix_name=None, output_format='matlab', mu=None)[source]

Save the matrix of the operator to a file.

Parameters

filename

Name of output file.

matrix_name

The name, the output matrix is given. (Comment field is used in case of Matrix Market output_format.) If None, the Operator’s name is used.

output_format

Output file format. Either matlab or matrixmarket.

mu

The parameter values to assemble the to be exported matrix for.

class pymor.operators.numpy.NumpyMatrixOperator(matrix, source_id=None, range_id=None, solver_options=None, name=None)[source]

Wraps a 2D NumPy array or SciPy spmatrix as an Operator.

Parameters

matrix

The NumPy array or SciPy spmatrix which is to be wrapped.

source_id

The id of the operator’s source VectorSpace.

range_id

The id of the operator’s range VectorSpace.

solver_options

The solver_options for the operator.

name

Name of the operator.

Methods

 H apply Apply the operator to a VectorArray. apply_adjoint Apply the adjoint operator. apply_inverse Apply the inverse operator. apply_inverse_adjoint Apply the inverse adjoint operator. as_range_array Return a VectorArray representation of the operator in its range space. as_source_array Return a VectorArray representation of the operator in its source space. assemble Assemble the operator for given parameter values. from_file
property H[source]
apply(U, mu=None)[source]

Apply the operator to a VectorArray.

Parameters

U

VectorArray of vectors to which the operator is applied.

mu

The parameter values for which to evaluate the operator.

Returns

VectorArray of the operator evaluations.

For any given linear Operator op, parameter values mu and VectorArrays U, V in the source resp. range we have:

op.apply_adjoint(V, mu).dot(U) == V.inner(op.apply(U, mu))


Thus, when op is represented by a matrix M, apply_adjoint is given by left-multiplication of (the complex conjugate of) M with V.

Parameters

V

VectorArray of vectors to which the adjoint operator is applied.

mu

The parameter values for which to apply the adjoint operator.

Returns

VectorArray of the adjoint operator evaluations.

apply_inverse(V, mu=None, initial_guess=None, least_squares=False, check_finite=True, default_sparse_solver_backend='scipy')[source]

Apply the inverse operator.

Parameters

V

VectorArray of vectors to which the inverse operator is applied.

mu

The parameter values for which to evaluate the inverse operator.

initial_guess

VectorArray with the same length as V containing initial guesses for the solution. Some implementations of apply_inverse may ignore this parameter. If None a solver-dependent default is used.

least_squares

If True, solve the least squares problem:

u = argmin ||op(u) - v||_2.


Since for an invertible operator the least squares solution agrees with the result of the application of the inverse operator, setting this option should, in general, have no effect on the result for those operators. However, note that when no appropriate solver_options are set for the operator, most implementations will choose a least squares solver by default which may be undesirable.

check_finite

Test if solution only contains finite values.

default_sparse_solver_backend

Default sparse solver backend to use (scipy, generic).

Returns

VectorArray of the inverse operator evaluations.

Raises

InversionError

The operator could not be inverted.

Parameters

U

VectorArray of vectors to which the inverse adjoint operator is applied.

mu

The parameter values for which to evaluate the inverse adjoint operator.

initial_guess

VectorArray with the same length as U containing initial guesses for the solution. Some implementations of apply_inverse_adjoint may ignore this parameter. If None a solver-dependent default is used.

least_squares

If True, solve the least squares problem:

v = argmin ||op^*(v) - u||_2.


Since for an invertible operator the least squares solution agrees with the result of the application of the inverse operator, setting this option should, in general, have no effect on the result for those operators. However, note that when no appropriate solver_options are set for the operator, most operator implementations will choose a least squares solver by default which may be undesirable.

Returns

VectorArray of the inverse adjoint operator evaluations.

Raises

InversionError

The operator could not be inverted.

as_range_array(mu=None)[source]

Return a VectorArray representation of the operator in its range space.

In the case of a linear operator with NumpyVectorSpace as source, this method returns for given parameter values mu a VectorArray V in the operator’s range, such that

V.lincomb(U.to_numpy()) == self.apply(U, mu)


for all VectorArrays U.

Parameters

mu

The parameter values for which to return the VectorArray representation.

Returns

V

The VectorArray defined above.

as_source_array(mu=None)[source]

Return a VectorArray representation of the operator in its source space.

In the case of a linear operator with NumpyVectorSpace as range, this method returns for given parameter values mu a VectorArray V in the operator’s source, such that

self.range.make_array(V.inner(U).T) == self.apply(U, mu)


for all VectorArrays U.

Parameters

mu

The parameter values for which to return the VectorArray representation.

Returns

V

The VectorArray defined above.

assemble(mu=None)[source]

Assemble the operator for given parameter values.

The result of the method strongly depends on the given operator. For instance, a matrix-based operator will assemble its matrix, a LincombOperator will try to form the linear combination of its operators, whereas an arbitrary operator might simply return a FixedParameterOperator. The only assured property of the assembled operator is that it no longer depends on a Parameter.

Parameters

mu

The parameter values for which to assemble the operator.

Returns

Parameter-independent, assembled Operator.

classmethod from_file(path, key=None, source_id=None, range_id=None, solver_options=None, name=None)[source]