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
Create slice indices equivalent to |
|
Create slice indices equivalent to |
|
Create slice indices for embedding input into a padded output. |
|
Compute output shape after padding. |
Classes
A base class for linear operators that can be applied to boundlab expressions. |
|
Composition of two LinearOps: |
|
Sum of two LinearOps: |
|
A LinearOp that scales its input by a scalar factor. |
|
A LinearOp that always returns zero, regardless of input. |
|
A linear operator defined by an Einstein summation with a fixed tensor. |
|
Reshape (view) the input tensor to target_shape. |
|
Flatten dimensions [start_dim . |
|
Unflatten dimension dim into sizes. |
|
Permute dimensions of the input tensor. |
|
Swap two dimensions of the input tensor ā special case of PermuteOp. |
|
Remove size-1 dimension(s). |
|
Insert a size-1 dimension at dim. |
|
Broadcast-expand dimensions (adjoint sums over expanded dims). |
|
Tile-repeat the tensor (adjoint folds and sums repeated blocks). |
|
Alias for repeat with dimension-padding handled like |
|
Reverse elements along dims (self-adjoint). |
|
Circular-shift elements (adjoint is the inverse shift). |
|
Extract or create a diagonal (1Dā2D). |
|
A LinearOp that implements |
|
A LinearOp that implements |
|
Advanced indexing with a tuple of index tensors. |
|
Scatter values to advanced index positions in a zero-initialized tensor. |
|
Basic slicing via |
|
Embed input into zeros at specified slice positions. |
|
Select a contiguous slice along dim. |
|
Select a single index along dim, removing that dimension. |
|
Indexing / slicing via |
|
Zero-pad an input tensor. |