linop — Base classes and algebraic operators¶
Base classes¶
- class LinOp(ishape: tuple[int, ...], oshape: tuple[int, ...], name: str = "·"))[source]¶
Bases:
ABCAn abstract base class for linear operators.
User must implement at least forward and adjoint methods in their concrete class.
- Parameters:
- metadata¶
Timing information populated automatically after each method call. See the guide for details.
- Type:
- matvec(point: Array) Array[source]¶
Vectorized forward application A·x.
- Parameters:
point (Array) – Column vector of shape
(N, 1).- Returns:
Column vector of shape
(M, 1).- Return type:
Array
- rmatvec(point: Array) Array[source]¶
Vectorized adjoint application Aᴴ·y.
- Parameters:
point (Array) – Column vector of shape
(M, 1).- Returns:
Column vector of shape
(N, 1).- Return type:
Array
- fwadj(point: Array) Array[source]¶
Apply Aᴴ·A to point.
- Parameters:
point (Array) – Input array of shape
ishape.- Returns:
Output array of shape
ishape.- Return type:
Array
- asmatrix(like: Array | None = None) Array[source]¶
Return the matrix corresponding to the linear operator.
Applies forward to N unit vectors where N = linop.isize.
- Parameters:
like (Array, optional) – If provided, use its array namespace; otherwise use float64 numpy array. See guide.
- Returns:
2D array of shape
(osize, isize).- Return type:
Array
Notes
Can be very heavy depending on the size of the operator.
- class BaseOp(forward: Callable[[Array], Array], adjoint: Callable[[Array], Array], ishape: tuple[int, ...], oshape: tuple[int, ...], fwadj: Callable[[Array], Array] | None = None, name: str = '·')[source]¶
Bases:
LinOpA LinOp defined by callables rather than subclassing.
- Parameters:
forward (callable) – The forward function
x → A·x.adjoint (callable) – The adjoint function
y → Aᴴ·y.fwadj (callable, optional) – The
Aᴴ·Afunction. Defaults toadjoint(forward(x)).name (str, optional) – Name of the operator.
Algebraic operators¶
- class Adjoint(linop: LinOp)[source]¶
Bases:
LinOpThe adjoint Aᴴ of a linear operator A.
Adjoint is an involution: Adjoint(Adjoint(A)) is A.
Delegates to A methods.
- Parameters:
linop (LinOp) – The operator to adjoint.
- asmatrix(like: Array | None = None) Array[source]¶
Return the matrix corresponding to the linear operator.
Applies forward to N unit vectors where N = linop.isize.
- Parameters:
like (Array, optional) – If provided, use its array namespace; otherwise use float64 numpy array. See guide.
- Returns:
2D array of shape
(osize, isize).- Return type:
Array
Notes
Can be very heavy depending on the size of the operator.
- class Scaled(baseop: LinOp, scale: complex | float)[source]¶
Bases:
LinOpAn operator B scaled by a scalar γ (i.e. A = γ·B).
- Parameters:
- fwadj(point: Array) Array[source]¶
Apply Aᴴ·A to point.
- Parameters:
point (Array) – Input array of shape
ishape.- Returns:
Output array of shape
ishape.- Return type:
Array
- asmatrix(like: Array | None = None) Array[source]¶
Return the matrix corresponding to the linear operator.
Applies forward to N unit vectors where N = linop.isize.
- Parameters:
like (Array, optional) – If provided, use its array namespace; otherwise use float64 numpy array. See guide.
- Returns:
2D array of shape
(osize, isize).- Return type:
Array
Notes
Can be very heavy depending on the size of the operator.
- class Symmetric(forward: Callable[[Array], Array], shape: tuple[int, ...], name: str = 'S')[source]¶
Bases:
LinOpA operator where Aᴴ = A.
For any Symmetric instance A,
Adjoint(A) is AisTrue: it is fully defined by forward since adjoint delegates to it.- Parameters:
- class Dense(matrix: Array, ishape: tuple[int, ...] | None = None, oshape: tuple[int, ...] | None = None, name: str = '_')[source]¶
Bases:
LinOpDense linear operator from matrix instance.
- Parameters:
matrix (Array) – A 2D array representing the operator. The namespace is inferred from this array.
ishape (tuple of int, optional) – Input shape. Defaults to
(matrix.shape[1], 1).oshape (tuple of int, optional) – Output shape. Defaults to
(matrix.shape[0], 1).name (str, optional) – Name of the operator.
- asmatrix(like: Array | None = None) Array[source]¶
Return the matrix corresponding to the linear operator.
Applies forward to N unit vectors where N = linop.isize.
- Parameters:
like (Array, optional) – If provided, use its array namespace; otherwise use float64 numpy array. See guide.
- Returns:
2D array of shape
(osize, isize).- Return type:
Array
Notes
Can be very heavy depending on the size of the operator.
Compound operators¶
- class ProdOp(left: LinOp, right: LinOp)[source]¶
Bases:
LinOpThe product of two operators A·B.
- fwadj(point: Array) Array[source]¶
Apply Aᴴ·A to point.
- Parameters:
point (Array) – Input array of shape
ishape.- Returns:
Output array of shape
ishape.- Return type:
Array
- asmatrix(like: Array | None = None) Array[source]¶
Return the matrix corresponding to the linear operator.
Applies forward to N unit vectors where N = linop.isize.
- Parameters:
like (Array, optional) – If provided, use its array namespace; otherwise use float64 numpy array. See guide.
- Returns:
2D array of shape
(osize, isize).- Return type:
Array
Notes
Can be very heavy depending on the size of the operator.
- class AddOp(left: LinOp, right: LinOp)[source]¶
Bases:
LinOpThe sum of two operators A + B.
- asmatrix(like: Array | None = None) Array[source]¶
Return the matrix corresponding to the linear operator.
Applies forward to N unit vectors where N = linop.isize.
- Parameters:
like (Array, optional) – If provided, use its array namespace; otherwise use float64 numpy array. See guide.
- Returns:
2D array of shape
(osize, isize).- Return type:
Array
Notes
Can be very heavy depending on the size of the operator.
- class SubOp(left: LinOp, right: LinOp)[source]¶
Bases:
LinOpThe subtraction of two operators A - B.
- asmatrix(like: Array | None = None) Array[source]¶
Return the matrix corresponding to the linear operator.
Applies forward to N unit vectors where N = linop.isize.
- Parameters:
like (Array, optional) – If provided, use its array namespace; otherwise use float64 numpy array. See guide.
- Returns:
2D array of shape
(osize, isize).- Return type:
Array
Notes
Can be very heavy depending on the size of the operator.
- class VStack(oplist: ~typing.Sequence[~aljabr.linop.LinOp], name: str = "[·]"))[source]¶
Bases:
LinOpVertical stack: maps x → vect([A₀x, A₁x, …]).
All operators must share the same
ishape.forwardreturns a column vector of shape(sum(op.osize), 1). Useapplyto get per-operator outputs as a list, orsplitto slice a column vector back into per-operator shapes.VStack([A, B, C]).HreturnsHStack([Aᴴ, Bᴴ, Cᴴ]), andHStack([A, B, C]).HreturnsVStack([Aᴴ, Bᴴ, Cᴴ]).So if y = vect([y₀, y₁, …]) of shape (N, 1), and A = VStack([A₀, A₁, …]), then y = Ax can be obtained with
>>> y_list = A.apply(x) # list of per-operator outputs >>> y = A.forward(x) # stacked column vector, shape A.oshape >>> A.split(y) # per-operator shapes, same as y_list
- Parameters:
Notes
This operator is for convenience. The recommendation is to write a custom operator that directly inherits from LinOp and implements forward and adjoint.
- class HStack(oplist: ~typing.Sequence[~aljabr.linop.LinOp], name: str = "[·|·]"):)[source]¶
Bases:
LinOpHorizontal stack: maps vect([x₀, x₁, …]) → Σ opᵢ(xᵢ).
Dual of
VStack: all operators must share the sameoshape.forwardsplits the input column vector by each operator’sishape, appliesop.forward, and sums.adjointapplies eachop.adjointto the same input and vectorizes the results.VStack([A, B, C]).HreturnsHStack([Aᴴ, Bᴴ, Cᴴ]), andHStack([A, B, C]).HreturnsVStack([Aᴴ, Bᴴ, Cᴴ]).So if x = vect([x₀, x₁, …]) of shape (M, 1), and A = HStack([A₀, A₁, …]), then y = Ax can be obtained with
>>> x_list = A.split(x) # list of per-operator inputs >>> y = A.forward(x) # sum of op.forward(xᵢ), shape A.oshape >>> A.apply(x) # per-operator outputs, same as x_list
- Parameters:
Notes
This operator is for convenience. The recommendation is to write a custom operator that directly inherits from LinOp and implements forward and adjoint.
Types and utilities¶
- vectorize(point: Array | Sequence[Array]) Array[source]¶
Vectorize an array or list of arrays as a column vector.
- Parameters:
point (Array or list of Array) – A single array or a list of arrays to concatenate.
- Returns:
Column vector of shape
(N, 1).- Return type:
Array
- unvectorize(point: Array, shapes: Sequence[tuple[int, ...]]) list[Array][source]¶
Unvectorize a column vector into a list of arrays.
- asmatrix(linop: Array | LinOp, like: Array | None = None) Array[source]¶
Return the matrix corresponding to a linear operator or array.
Calls linop.asmatrix() if linop is a LinOp. Otherwise converts to array using xp.asarray (inferred from like) or numpy.asarray.
- Parameters:
linop (Array or LinOp) – The linear operator or array to convert.
like (Array, optional) – If provided and linop is not a LinOp, use its array namespace.
- Returns:
2D array representation.
- Return type:
Array
Notes
The LinOp.asmatrix() method can be very heavy depending on operator size.