boundlab.linearop#

Linear Operator Library for Expression Backpropagation.

This module defines linear operators used by BoundLab expressions during symbolic transformation and backward bound propagation.

Key operators:

  • ComposedOp: Functional composition of linear maps (\(A \circ B\)), used to chain transformations efficiently.

  • SumOp: Pointwise sum of linear maps, used when multiple affine contributions target the same expression.

  • EinsumOp: General tensor-linear map based on Einstein summation; this is the most flexible primitive for dense affine transformations.

The module also exposes shape/indexing operators (reshape, permute, gather, scatter, slicing, padding) that are all represented as LinearOp instances and can therefore be composed, summed, and propagated uniformly.

Examples

Apply a shape operator to a concrete tensor:

>>> import torch
>>> from boundlab.linearop import ReshapeOp
>>> op = ReshapeOp(torch.Size([2, 3]), (3, 2))
>>> y = op.forward(torch.arange(6.0).reshape(2, 3))
>>> y.shape
torch.Size([3, 2])

Functions

narrow_indices

Create slice indices equivalent to tensor.narrow(dim, start, length).

select_indices

Create slice indices equivalent to tensor.select(dim, index).

pad_indices

Create slice indices for embedding input into a padded output.

pad_output_shape

Compute output shape after padding.

Classes

LinearOp

A base class for linear operators that can be applied to boundlab expressions.

ComposedOp

Composition of two LinearOps: (outer ∘ inner)(x) = outer(inner(x)).

SumOp

Sum of two LinearOps: (a + b)(x) = a(x) + b(x).

ScalarOp

A LinearOp that scales its input by a scalar factor.

ZeroOp

A LinearOp that always returns zero, regardless of input.

EinsumOp

A linear operator defined by an Einstein summation with a fixed tensor.

ReshapeOp

Reshape (view) the input tensor to target_shape.

FlattenOp

Flatten dimensions [start_dim .

UnflattenOp

Unflatten dimension dim into sizes.

PermuteOp

Permute dimensions of the input tensor.

TransposeOp

Swap two dimensions of the input tensor — special case of PermuteOp.

SqueezeOp

Remove size-1 dimension(s).

UnsqueezeOp

Insert a size-1 dimension at dim.

ExpandOp

Broadcast-expand dimensions (adjoint sums over expanded dims).

RepeatOp

Tile-repeat the tensor (adjoint folds and sums repeated blocks).

TileOp

Alias for repeat with dimension-padding handled like torch.tile.

FlipOp

Reverse elements along dims (self-adjoint).

RollOp

Circular-shift elements (adjoint is the inverse shift).

DiagOp

Extract or create a diagonal (1D↔2D).

GatherOp

A LinearOp that implements torch.gather along a specified dimension.

ScatterOp

A LinearOp that implements torch.scatter along a specified dimension.

GetIndicesOp

Advanced indexing with a tuple of index tensors.

SetIndicesOp

Scatter values to advanced index positions in a zero-initialized tensor.

GetSliceOp

Basic slicing via x[indices] where indices contains int/slice/None/Ellipsis.

SetSliceOp

Embed input into zeros at specified slice positions.

NarrowOp

Select a contiguous slice along dim.

SelectOp

Select a single index along dim, removing that dimension.

GetItemOp

Indexing / slicing via x[indices].

PadOp

Zero-pad an input tensor.