Affine transforms#
regreg.affine#
- regreg.affine.broadcast_first(a, b, op)#
Apply binary operation op, broadcasting a over axis 1 if necessary.
- Parameters:
a (ndarray) – If a is 1D with shape
(N,), convert to shape(N,1)before applying op. This has the effect of making broadcasting happen over axis 1 rather than the default of axis 0.b (ndarray) – If b is 1D with shape
(N,), convert to shape(N,1)before applying op.op (callable) – Binary operation to apply to a, b.
- Returns:
res (ndarray) – Result of
op(a, b), with shape equal tob.shape.
- exception regreg.affine.AffineError#
Bases:
ExceptionError raised for an invalid specification of an affine transform.
- class regreg.affine.affine_transform(linear_operator, affine_offset, diag=False, input_shape=None)#
Bases:
objectAn affine transform \(x \mapsto Dx + \alpha\).
The linear part \(D\) may be a dense array, a sparse matrix, the main diagonal of a diagonal matrix or another object supporting the affine transform API (
linear_map,affine_map,adjoint_map,affine_offset,input_shape,output_shape).- linear_map(x)#
Apply linear part of transform to x.
Return \(Dx\).
- Parameters:
x (ndarray) – Array to which to apply transform. Can be 1D or 2D.
- Returns:
Dx (ndarray) – x transformed with linear component
Notes
This routine is currently a matrix multiplication, but could also call FFTs if D is a DFT matrix, in a subclass.
- dot(x)#
Apply linear part of transform to x.
Returns
self.linear_map(x)unless x is an affine_transform, in which case it returns the composition of self and x.- Parameters:
x (ndarray or affine_transform) – Array to which to apply transform. Can be 1D or 2D.
- Returns:
Dx (ndarray or composition) – x transformed with linear component, or the composition of self with x.
- property T#
The adjoint of the linear part of the transform.
- property ndim#
Total number of dimensions of input and output shapes.
- property shape#
Shape of linear map.
The usual 2-tuple
(output_dim, input_dim)if transform can be represented by a 2-dim array, else(output_shape, input_shape).
- affine_map(x)#
Apply linear and affine offset to x.
Return \(Dx+\alpha\).
- Parameters:
x (ndarray) – Array to which to apply transform. Can be 1D or 2D.
- Returns:
Dx_a (ndarray) – x transformed with linear and offset components.
Notes
This routine is currently a matrix multiplication, but could also call FFTs if D is a DFT matrix, in a subclass.
- adjoint_map(u)#
Apply transpose of linear component to u.
Return \(D^Tu\).
- Parameters:
u (ndarray) – Array to which to apply transposed linear part of transform. Can be 1D or 2D array.
- Returns:
DTu (ndarray) – u transformed with transpose of linear component.
Notes
This routine is currently a matrix multiplication, but could also call FFTs if D is a DFT matrix, in a subclass.
- class regreg.affine.linear_transform(linear_operator, diag=False, input_shape=None)#
Bases:
affine_transformA linear transform is an affine transform with no affine offset.
- Parameters:
linear_operator (ndarray or sparse matrix or affine_transform) – Linear operator; cannot be None.
diag ({False, True}, optional) – If True, interpret 1D linear_operator as the main diagonal of a diagonal array.
input_shape (None or tuple, optional) – Shape of input, if the input is a matrix.
- class regreg.affine.selector(index_obj, initial_shape, affine_transform=None, reshape=None)#
Bases:
linear_transformApply an affine transform after applying an indexing operation to the array.
- Parameters:
index_obj (slice, ndarray or tuple) – Index applied to the input, i.e. the input is
x[index_obj].initial_shape (tuple) – Shape of the input array.
affine_transform (affine_transform, optional) – Transform applied after indexing. Defaults to the identity.
reshape (tuple, optional) – If not None, the indexed array is reshaped to this shape before applying affine_transform.
Examples
>>> from regreg.affine import selector, affine_transform >>> X = np.arange(30).reshape((6,5)) >>> offset = np.arange(6) >>> transform = affine_transform(X, offset) >>> apply_to_first5 = selector(slice(0,5), (7,), transform) >>> apply_to_first5.linear_map(np.arange(7)) array([ 30, 80, 130, 180, 230, 280]) >>> np.dot(X, np.arange(5)) array([ 30, 80, 130, 180, 230, 280])
>>> apply_to_first5.affine_map(np.arange(7)) array([ 30, 81, 132, 183, 234, 285]) >>> np.dot(X, np.arange(5)) + offset array([ 30, 81, 132, 183, 234, 285])
>>> result = np.array([275., 290., 305., 320., 335., 0., 0.]) >>> np.testing.assert_allclose(result, apply_to_first5.adjoint_map(np.arange(6)))
- linear_map(x)#
Apply linear part of transform to x.
Return \(Dx\).
- Parameters:
x (ndarray) – Array to which to apply transform. Can be 1D or 2D.
- Returns:
Dx (ndarray) – x transformed with linear component
Notes
This routine is currently a matrix multiplication, but could also call FFTs if D is a DFT matrix, in a subclass.
- affine_map(x)#
Apply linear and affine offset to x.
Return \(Dx+\alpha\).
- Parameters:
x (ndarray) – Array to which to apply transform. Can be 1D or 2D.
- Returns:
Dx_a (ndarray) – x transformed with linear and offset components.
Notes
This routine is currently a matrix multiplication, but could also call FFTs if D is a DFT matrix, in a subclass.
- adjoint_map(u)#
Apply transpose of linear component to u.
Return \(D^Tu\).
- Parameters:
u (ndarray) – Array to which to apply transposed linear part of transform. Can be 1D or 2D array.
- Returns:
DTu (ndarray) – u transformed with transpose of linear component.
Notes
This routine is currently a matrix multiplication, but could also call FFTs if D is a DFT matrix, in a subclass.
- class regreg.affine.scaler(scalings)#
Bases:
linear_transformElementwise scaling transform \(x \mapsto s \odot x\).
- Parameters:
scalings (ndarray) – Scalings applied elementwise to the input.
- linear_map(x)#
Apply linear part of transform to x.
Return \(Dx\).
- Parameters:
x (ndarray) – Array to which to apply transform. Can be 1D or 2D.
- Returns:
Dx (ndarray) – x transformed with linear component
Notes
This routine is currently a matrix multiplication, but could also call FFTs if D is a DFT matrix, in a subclass.
- affine_map(x)#
Apply linear and affine offset to x.
Return \(Dx+\alpha\).
- Parameters:
x (ndarray) – Array to which to apply transform. Can be 1D or 2D.
- Returns:
Dx_a (ndarray) – x transformed with linear and offset components.
Notes
This routine is currently a matrix multiplication, but could also call FFTs if D is a DFT matrix, in a subclass.
- adjoint_map(u)#
Apply transpose of linear component to u.
Return \(D^Tu\).
- Parameters:
u (ndarray) – Array to which to apply transposed linear part of transform. Can be 1D or 2D array.
- Returns:
DTu (ndarray) – u transformed with transpose of linear component.
Notes
This routine is currently a matrix multiplication, but could also call FFTs if D is a DFT matrix, in a subclass.
- class regreg.affine.reshape(input_shape, output_shape)#
Bases:
linear_transformLinear transform that reshapes its input.
- Parameters:
input_shape (tuple) – Shape of input.
output_shape (tuple) – Shape of output.
- linear_map(x)#
Apply linear part of transform to x.
Return \(Dx\).
- Parameters:
x (ndarray) – Array to which to apply transform. Can be 1D or 2D.
- Returns:
Dx (ndarray) – x transformed with linear component
Notes
This routine is currently a matrix multiplication, but could also call FFTs if D is a DFT matrix, in a subclass.
- affine_map(x)#
Apply linear and affine offset to x.
Return \(Dx+\alpha\).
- Parameters:
x (ndarray) – Array to which to apply transform. Can be 1D or 2D.
- Returns:
Dx_a (ndarray) – x transformed with linear and offset components.
Notes
This routine is currently a matrix multiplication, but could also call FFTs if D is a DFT matrix, in a subclass.
- adjoint_map(u)#
Apply transpose of linear component to u.
Return \(D^Tu\).
- Parameters:
u (ndarray) – Array to which to apply transposed linear part of transform. Can be 1D or 2D array.
- Returns:
DTu (ndarray) – u transformed with transpose of linear component.
Notes
This routine is currently a matrix multiplication, but could also call FFTs if D is a DFT matrix, in a subclass.
- regreg.affine.normalize(transform, center=True, scale=True, intercept=False, ddof=1, value=1)#
Return a transform that centers and/or scales the columns of a matrix.
The matrix itself is not modified: the result is a composition of transforms that has the effect of normalizing the columns.
- Parameters:
transform (ndarray or scipy.sparse) – The matrix to be normalized.
center (bool) – Center the columns?
scale (bool) – Scale the columns?
intercept (bool) – Include an intercept column?
ddof (float) – Used to correct degrees of freedom in standard deviation calculation.
value (float) – Set the std of the columns to be value.
- Returns:
final_map (affine_transform) – Composition of transforms implementing the normalized matrix (with an added intercept if intercept is True).
- class regreg.affine.add_intercept(transform)#
Bases:
affine_transformPrepend an intercept column to a transform.
The first coordinate of the input is the intercept, so
linear_map(x) = transform.linear_map(x[1:]) + x[0].- Parameters:
transform (affine_transform or ndarray) – Transform to which an intercept is added. Assumes a vector input.
- affine_map(x)#
Apply linear and affine offset to x.
Return \(Dx+\alpha\).
- Parameters:
x (ndarray) – Array to which to apply transform. Can be 1D or 2D.
- Returns:
Dx_a (ndarray) – x transformed with linear and offset components.
Notes
This routine is currently a matrix multiplication, but could also call FFTs if D is a DFT matrix, in a subclass.
- linear_map(x)#
Apply linear part of transform to x.
Return \(Dx\).
- Parameters:
x (ndarray) – Array to which to apply transform. Can be 1D or 2D.
- Returns:
Dx (ndarray) – x transformed with linear component
Notes
This routine is currently a matrix multiplication, but could also call FFTs if D is a DFT matrix, in a subclass.
- adjoint_map(u)#
Apply transpose of linear component to u.
Return \(D^Tu\).
- Parameters:
u (ndarray) – Array to which to apply transposed linear part of transform. Can be 1D or 2D array.
- Returns:
DTu (ndarray) – u transformed with transpose of linear component.
Notes
This routine is currently a matrix multiplication, but could also call FFTs if D is a DFT matrix, in a subclass.
- class regreg.affine.centering(input_shape)#
Bases:
affine_transformLinear transform that subtracts the mean along axis 0, i.e.
x - x.mean(0). This map is self-adjoint.- Parameters:
input_shape (tuple) – Shape of input (and output).
- affine_map(x)#
Apply linear and affine offset to x.
Return \(Dx+\alpha\).
- Parameters:
x (ndarray) – Array to which to apply transform. Can be 1D or 2D.
- Returns:
Dx_a (ndarray) – x transformed with linear and offset components.
Notes
This routine is currently a matrix multiplication, but could also call FFTs if D is a DFT matrix, in a subclass.
- linear_map(x)#
Apply linear part of transform to x.
Return \(Dx\).
- Parameters:
x (ndarray) – Array to which to apply transform. Can be 1D or 2D.
- Returns:
Dx (ndarray) – x transformed with linear component
Notes
This routine is currently a matrix multiplication, but could also call FFTs if D is a DFT matrix, in a subclass.
- adjoint_map(x)#
Apply transpose of linear component to u.
Return \(D^Tu\).
- Parameters:
u (ndarray) – Array to which to apply transposed linear part of transform. Can be 1D or 2D array.
- Returns:
DTu (ndarray) – u transformed with transpose of linear component.
Notes
This routine is currently a matrix multiplication, but could also call FFTs if D is a DFT matrix, in a subclass.
- class regreg.affine.identity(input_shape)#
Bases:
affine_transformIdentity transform.
- Parameters:
input_shape (tuple) – Shape of input (and output).
- affine_map(x)#
Apply linear and affine offset to x.
Return \(Dx+\alpha\).
- Parameters:
x (ndarray) – Array to which to apply transform. Can be 1D or 2D.
- Returns:
Dx_a (ndarray) – x transformed with linear and offset components.
Notes
This routine is currently a matrix multiplication, but could also call FFTs if D is a DFT matrix, in a subclass.
- linear_map(x)#
Apply linear part of transform to x.
Return \(Dx\).
- Parameters:
x (ndarray) – Array to which to apply transform. Can be 1D or 2D.
- Returns:
Dx (ndarray) – x transformed with linear component
Notes
This routine is currently a matrix multiplication, but could also call FFTs if D is a DFT matrix, in a subclass.
- adjoint_map(x)#
Apply transpose of linear component to u.
Return \(D^Tu\).
- Parameters:
u (ndarray) – Array to which to apply transposed linear part of transform. Can be 1D or 2D array.
- Returns:
DTu (ndarray) – u transformed with transpose of linear component.
Notes
This routine is currently a matrix multiplication, but could also call FFTs if D is a DFT matrix, in a subclass.
- class regreg.affine.vstack(transforms)#
Bases:
affine_transformStack several affine transforms vertically together though not necessarily as a big matrix.
- Parameters:
transforms (sequence) – Transforms (or arrays) to stack. All must have the same input shape. The output is the concatenation of the (flattened) outputs.
- linear_map(x)#
Apply linear part of transform to x.
Return \(Dx\).
- Parameters:
x (ndarray) – Array to which to apply transform. Can be 1D or 2D.
- Returns:
Dx (ndarray) – x transformed with linear component
Notes
This routine is currently a matrix multiplication, but could also call FFTs if D is a DFT matrix, in a subclass.
- affine_map(x)#
Apply linear and affine offset to x.
Return \(Dx+\alpha\).
- Parameters:
x (ndarray) – Array to which to apply transform. Can be 1D or 2D.
- Returns:
Dx_a (ndarray) – x transformed with linear and offset components.
Notes
This routine is currently a matrix multiplication, but could also call FFTs if D is a DFT matrix, in a subclass.
- adjoint_map(u)#
Apply transpose of linear component to u.
Return \(D^Tu\).
- Parameters:
u (ndarray) – Array to which to apply transposed linear part of transform. Can be 1D or 2D array.
- Returns:
DTu (ndarray) – u transformed with transpose of linear component.
Notes
This routine is currently a matrix multiplication, but could also call FFTs if D is a DFT matrix, in a subclass.
- class regreg.affine.hstack(transforms)#
Bases:
affine_transformStack several affine transforms horizontally together though not necessarily as a big matrix.
- Parameters:
transforms (sequence) – Transforms (or arrays) to stack. All must have the same output shape. The input is the concatenation of the (flattened) inputs, and the output is the sum of the outputs.
- linear_map(x)#
Apply linear part of transform to x.
Return \(Dx\).
- Parameters:
x (ndarray) – Array to which to apply transform. Can be 1D or 2D.
- Returns:
Dx (ndarray) – x transformed with linear component
Notes
This routine is currently a matrix multiplication, but could also call FFTs if D is a DFT matrix, in a subclass.
- affine_map(x)#
Apply linear and affine offset to x.
Return \(Dx+\alpha\).
- Parameters:
x (ndarray) – Array to which to apply transform. Can be 1D or 2D.
- Returns:
Dx_a (ndarray) – x transformed with linear and offset components.
Notes
This routine is currently a matrix multiplication, but could also call FFTs if D is a DFT matrix, in a subclass.
- adjoint_map(u)#
Apply transpose of linear component to u.
Return \(D^Tu\).
- Parameters:
u (ndarray) – Array to which to apply transposed linear part of transform. Can be 1D or 2D array.
- Returns:
DTu (ndarray) – u transformed with transpose of linear component.
Notes
This routine is currently a matrix multiplication, but could also call FFTs if D is a DFT matrix, in a subclass.
- class regreg.affine.product(transforms)#
Bases:
affine_transformCreate a transform that maps the product of the inputs to the product of the outputs (i.e. a block diagonal transform).
- Parameters:
transforms (sequence) – Transforms (or arrays) applied to consecutive blocks of the input.
- linear_map(x)#
Apply linear part of transform to x.
Return \(Dx\).
- Parameters:
x (ndarray) – Array to which to apply transform. Can be 1D or 2D.
- Returns:
Dx (ndarray) – x transformed with linear component
Notes
This routine is currently a matrix multiplication, but could also call FFTs if D is a DFT matrix, in a subclass.
- affine_map(x)#
Apply linear and affine offset to x.
Return \(Dx+\alpha\).
- Parameters:
x (ndarray) – Array to which to apply transform. Can be 1D or 2D.
- Returns:
Dx_a (ndarray) – x transformed with linear and offset components.
Notes
This routine is currently a matrix multiplication, but could also call FFTs if D is a DFT matrix, in a subclass.
- adjoint_map(u)#
Apply transpose of linear component to u.
Return \(D^Tu\).
- Parameters:
u (ndarray) – Array to which to apply transposed linear part of transform. Can be 1D or 2D array.
- Returns:
DTu (ndarray) – u transformed with transpose of linear component.
Notes
This routine is currently a matrix multiplication, but could also call FFTs if D is a DFT matrix, in a subclass.
- regreg.affine.power_L(transform, max_its=500, tol=1e-08, debug=False, rng=0)#
Approximate the largest singular value (squared) of a transform.
Uses power iterations on
transform.adjoint_map(transform.linear_map(v))applied to the linear part of transform.- Parameters:
transform (affine_transform or ndarray) – Transform whose largest squared singular value is sought.
max_its (int, optional) – Maximum number of power iterations.
tol (float, optional) – Relative tolerance for convergence.
debug (bool, optional) – If True, print the estimate at each iteration.
rng (int or np.random.Generator, optional) – Seed or generator for the random starting vector, passed to np.random.default_rng. The default is a fixed seed, so that results are reproducible.
- Returns:
norm (float) – Estimate of the largest squared singular value.
Notes
TODO: should this be the largest singular value instead (i.e. not squared?)
- regreg.affine.astransform(X)#
Cast X as an affine transform.
If X is an affine_transform, return X, else try to cast it as a linear_transform.
- Parameters:
X (affine_transform or ndarray or sparse matrix) – Object to cast.
- Returns:
T (affine_transform)
- class regreg.affine.aslinear(transform)#
Bases:
linear_transformLinear part of an affine transform.
- Parameters:
transform (affine_transform or ndarray) – Transform whose linear part is taken.
- linear_map(x)#
Apply linear part of transform to x.
Return \(Dx\).
- Parameters:
x (ndarray) – Array to which to apply transform. Can be 1D or 2D.
- Returns:
Dx (ndarray) – x transformed with linear component
Notes
This routine is currently a matrix multiplication, but could also call FFTs if D is a DFT matrix, in a subclass.
- affine_map(x)#
Apply linear and affine offset to x.
Return \(Dx+\alpha\).
- Parameters:
x (ndarray) – Array to which to apply transform. Can be 1D or 2D.
- Returns:
Dx_a (ndarray) – x transformed with linear and offset components.
Notes
This routine is currently a matrix multiplication, but could also call FFTs if D is a DFT matrix, in a subclass.
- adjoint_map(x)#
Apply transpose of linear component to u.
Return \(D^Tu\).
- Parameters:
u (ndarray) – Array to which to apply transposed linear part of transform. Can be 1D or 2D array.
- Returns:
DTu (ndarray) – u transformed with transpose of linear component.
Notes
This routine is currently a matrix multiplication, but could also call FFTs if D is a DFT matrix, in a subclass.
- class regreg.affine.adjoint(transform)#
Bases:
linear_transformGiven an affine_transform, return a linear_transform that is the adjoint of its linear part.
- Parameters:
transform (affine_transform or ndarray) – Transform whose adjoint is taken.
- linear_map(x)#
Apply linear part of transform to x.
Return \(Dx\).
- Parameters:
x (ndarray) – Array to which to apply transform. Can be 1D or 2D.
- Returns:
Dx (ndarray) – x transformed with linear component
Notes
This routine is currently a matrix multiplication, but could also call FFTs if D is a DFT matrix, in a subclass.
- affine_map(x)#
Apply linear and affine offset to x.
Return \(Dx+\alpha\).
- Parameters:
x (ndarray) – Array to which to apply transform. Can be 1D or 2D.
- Returns:
Dx_a (ndarray) – x transformed with linear and offset components.
Notes
This routine is currently a matrix multiplication, but could also call FFTs if D is a DFT matrix, in a subclass.
- adjoint_map(x)#
Apply transpose of linear component to u.
Return \(D^Tu\).
- Parameters:
u (ndarray) – Array to which to apply transposed linear part of transform. Can be 1D or 2D array.
- Returns:
DTu (ndarray) – u transformed with transpose of linear component.
Notes
This routine is currently a matrix multiplication, but could also call FFTs if D is a DFT matrix, in a subclass.
- class regreg.affine.tensorize(transform, q)#
Bases:
affine_transformGiven an affine_transform, return a transform that expects q copies of something with transform’s input_shape.
This class effectively makes explicit that a transform may expect a matrix rather than a single vector.
- Parameters:
transform (affine_transform or ndarray) – Transform applied to each of the q columns.
q (int) – Number of copies (columns).
- linear_map(x)#
Apply linear part of transform to x.
Return \(Dx\).
- Parameters:
x (ndarray) – Array to which to apply transform. Can be 1D or 2D.
- Returns:
Dx (ndarray) – x transformed with linear component
Notes
This routine is currently a matrix multiplication, but could also call FFTs if D is a DFT matrix, in a subclass.
- affine_map(x)#
Apply linear and affine offset to x.
Return \(Dx+\alpha\).
- Parameters:
x (ndarray) – Array to which to apply transform. Can be 1D or 2D.
- Returns:
Dx_a (ndarray) – x transformed with linear and offset components.
Notes
This routine is currently a matrix multiplication, but could also call FFTs if D is a DFT matrix, in a subclass.
- adjoint_map(x)#
Apply transpose of linear component to u.
Return \(D^Tu\).
- Parameters:
u (ndarray) – Array to which to apply transposed linear part of transform. Can be 1D or 2D array.
- Returns:
DTu (ndarray) – u transformed with transpose of linear component.
Notes
This routine is currently a matrix multiplication, but could also call FFTs if D is a DFT matrix, in a subclass.
- class regreg.affine.residual(transform)#
Bases:
affine_transformCompute the residual from an affine transform, i.e. \(x \mapsto x - (Dx + \alpha)\).
- Parameters:
transform (affine_transform or ndarray) – Transform with equal input and output shapes.
- linear_map(x)#
Apply linear part of transform to x.
Return \(Dx\).
- Parameters:
x (ndarray) – Array to which to apply transform. Can be 1D or 2D.
- Returns:
Dx (ndarray) – x transformed with linear component
Notes
This routine is currently a matrix multiplication, but could also call FFTs if D is a DFT matrix, in a subclass.
- affine_map(x)#
Apply linear and affine offset to x.
Return \(Dx+\alpha\).
- Parameters:
x (ndarray) – Array to which to apply transform. Can be 1D or 2D.
- Returns:
Dx_a (ndarray) – x transformed with linear and offset components.
Notes
This routine is currently a matrix multiplication, but could also call FFTs if D is a DFT matrix, in a subclass.
- adjoint_map(u)#
Apply transpose of linear component to u.
Return \(D^Tu\).
- Parameters:
u (ndarray) – Array to which to apply transposed linear part of transform. Can be 1D or 2D array.
- Returns:
DTu (ndarray) – u transformed with transpose of linear component.
Notes
This routine is currently a matrix multiplication, but could also call FFTs if D is a DFT matrix, in a subclass.
- class regreg.affine.composition(*transforms)#
Bases:
affine_transformComposes a list of affine transforms, executing right to left.
- Parameters:
transforms (affine_transform or ndarray) – Transforms to compose, passed as positional arguments. The last one is applied first.
- linear_map(x)#
Apply linear part of transform to x.
Return \(Dx\).
- Parameters:
x (ndarray) – Array to which to apply transform. Can be 1D or 2D.
- Returns:
Dx (ndarray) – x transformed with linear component
Notes
This routine is currently a matrix multiplication, but could also call FFTs if D is a DFT matrix, in a subclass.
- affine_map(x)#
Apply linear and affine offset to x.
Return \(Dx+\alpha\).
- Parameters:
x (ndarray) – Array to which to apply transform. Can be 1D or 2D.
- Returns:
Dx_a (ndarray) – x transformed with linear and offset components.
Notes
This routine is currently a matrix multiplication, but could also call FFTs if D is a DFT matrix, in a subclass.
- adjoint_map(x)#
Apply transpose of linear component to u.
Return \(D^Tu\).
- Parameters:
u (ndarray) – Array to which to apply transposed linear part of transform. Can be 1D or 2D array.
- Returns:
DTu (ndarray) – u transformed with transpose of linear component.
Notes
This routine is currently a matrix multiplication, but could also call FFTs if D is a DFT matrix, in a subclass.
- class regreg.affine.affine_sum(transforms, weights=None)#
Bases:
affine_transformCreates the (weighted) sum of a list of affine_transforms.
- Parameters:
transforms (sequence) – Transforms (or arrays) to sum. All should have the same input and output shapes.
weights (ndarray, optional) – Weight for each transform. Defaults to all ones.
- linear_map(x)#
Apply linear part of transform to x.
Return \(Dx\).
- Parameters:
x (ndarray) – Array to which to apply transform. Can be 1D or 2D.
- Returns:
Dx (ndarray) – x transformed with linear component
Notes
This routine is currently a matrix multiplication, but could also call FFTs if D is a DFT matrix, in a subclass.
- affine_map(x)#
Apply linear and affine offset to x.
Return \(Dx+\alpha\).
- Parameters:
x (ndarray) – Array to which to apply transform. Can be 1D or 2D.
- Returns:
Dx_a (ndarray) – x transformed with linear and offset components.
Notes
This routine is currently a matrix multiplication, but could also call FFTs if D is a DFT matrix, in a subclass.
- adjoint_map(x)#
Apply transpose of linear component to u.
Return \(D^Tu\).
- Parameters:
u (ndarray) – Array to which to apply transposed linear part of transform. Can be 1D or 2D array.
- Returns:
DTu (ndarray) – u transformed with transpose of linear component.
Notes
This routine is currently a matrix multiplication, but could also call FFTs if D is a DFT matrix, in a subclass.
- class regreg.affine.scalar_multiply(atransform, scalar)#
Bases:
affine_transformMultiply the output of a transform by a scalar.
- Parameters:
atransform (affine_transform) – Transform to be scaled.
scalar (float) – Multiplier.
- affine_map(x)#
Apply linear and affine offset to x.
Return \(Dx+\alpha\).
- Parameters:
x (ndarray) – Array to which to apply transform. Can be 1D or 2D.
- Returns:
Dx_a (ndarray) – x transformed with linear and offset components.
Notes
This routine is currently a matrix multiplication, but could also call FFTs if D is a DFT matrix, in a subclass.
- linear_map(x)#
Apply linear part of transform to x.
Return \(Dx\).
- Parameters:
x (ndarray) – Array to which to apply transform. Can be 1D or 2D.
- Returns:
Dx (ndarray) – x transformed with linear component
Notes
This routine is currently a matrix multiplication, but could also call FFTs if D is a DFT matrix, in a subclass.
- adjoint_map(x)#
Apply transpose of linear component to u.
Return \(D^Tu\).
- Parameters:
u (ndarray) – Array to which to apply transposed linear part of transform. Can be 1D or 2D array.
- Returns:
DTu (ndarray) – u transformed with transpose of linear component.
Notes
This routine is currently a matrix multiplication, but could also call FFTs if D is a DFT matrix, in a subclass.
- class regreg.affine.posneg(linear_transform)#
Bases:
affine_transformTransform taking a pair \((x_+, x_-)\) to \(D x_+ - D x_-\).
The input has shape
(2,) + linear_transform.input_shape.- Parameters:
linear_transform (affine_transform or ndarray) – The linear transform \(D\).
- linear_map(x)#
Apply linear part of transform to x.
Return \(Dx\).
- Parameters:
x (ndarray) – Array to which to apply transform. Can be 1D or 2D.
- Returns:
Dx (ndarray) – x transformed with linear component
Notes
This routine is currently a matrix multiplication, but could also call FFTs if D is a DFT matrix, in a subclass.
- affine_map(x)#
Apply linear and affine offset to x.
Return \(Dx+\alpha\).
- Parameters:
x (ndarray) – Array to which to apply transform. Can be 1D or 2D.
- Returns:
Dx_a (ndarray) – x transformed with linear and offset components.
Notes
This routine is currently a matrix multiplication, but could also call FFTs if D is a DFT matrix, in a subclass.
- adjoint_map(x)#
Apply transpose of linear component to u.
Return \(D^Tu\).
- Parameters:
u (ndarray) – Array to which to apply transposed linear part of transform. Can be 1D or 2D array.
- Returns:
DTu (ndarray) – u transformed with transpose of linear component.
Notes
This routine is currently a matrix multiplication, but could also call FFTs if D is a DFT matrix, in a subclass.
- regreg.affine.todense(transform)#
Return a dense array representation of a transform.
Use carefully – it could be large.
- Parameters:
transform (affine_transform) – Transform with a 1D input shape.
- Returns:
D (ndarray) – Result of applying
transform.linear_mapto the identity matrix.
regreg.affine.fused_lasso#
- regreg.affine.fused_lasso.difference_transform(X, order=1, sorted=False, transform=False)#
Compute the divided difference matrix for X after sorting X.
- Parameters:
X (ndarray) – 1D array of coordinates where discrete derivative is computed.
order (int) – What order of difference should we compute?
sorted (bool) – Is X sorted?
transform (bool) – If True, return a linear_transform rather than a sparse matrix.
- Returns:
D (scipy.sparse.csr_matrix or linear_transform) – Matrix of divided differences of sorted X, of shape
(n-order, n).
- class regreg.affine.fused_lasso.trend_filter(knots, order=1, sorted=False)#
Bases:
affine_transformLinear transform computing divided differences of a given order on a set of knots, as used in trend filtering.
- Parameters:
knots (ndarray) – 1D array of knot locations.
order (int, optional) – Order of the divided differences.
sorted (bool, optional) – Are the knots sorted?
- classmethod grid(m, order=1, sorted=False)#
Trend filter transform on the equally spaced knots
np.arange(m).
- linear_map(x)#
Apply linear part of transform to x.
Return \(Dx\).
- Parameters:
x (ndarray) – Array to which to apply transform. Can be 1D or 2D.
- Returns:
Dx (ndarray) – x transformed with linear component
Notes
This routine is currently a matrix multiplication, but could also call FFTs if D is a DFT matrix, in a subclass.
- affine_map(x)#
Apply linear and affine offset to x.
Return \(Dx+\alpha\).
- Parameters:
x (ndarray) – Array to which to apply transform. Can be 1D or 2D.
- Returns:
Dx_a (ndarray) – x transformed with linear and offset components.
Notes
This routine is currently a matrix multiplication, but could also call FFTs if D is a DFT matrix, in a subclass.
- adjoint_map(x)#
Apply transpose of linear component to u.
Return \(D^Tu\).
- Parameters:
u (ndarray) – Array to which to apply transposed linear part of transform. Can be 1D or 2D array.
- Returns:
DTu (ndarray) – u transformed with transpose of linear component.
Notes
This routine is currently a matrix multiplication, but could also call FFTs if D is a DFT matrix, in a subclass.
- class regreg.affine.fused_lasso.trend_filter_inverse(knots, order=1, sorted=False)#
Bases:
affine_transformPseudo-inverse of a first order trend_filter transform.
Maps divided differences back to a mean-centered sequence on the knots (via cumulative sums).
- Parameters:
knots (ndarray) – 1D array of knot locations.
order (int, optional) – Order of the divided differences. Only
order=1is implemented.sorted (bool, optional) – Are the knots sorted?
- classmethod grid(m, order=1, sorted=False)#
Inverse trend filter transform on the equally spaced knots
np.arange(m).
- linear_map(x)#
Apply linear part of transform to x.
Return \(Dx\).
- Parameters:
x (ndarray) – Array to which to apply transform. Can be 1D or 2D.
- Returns:
Dx (ndarray) – x transformed with linear component
Notes
This routine is currently a matrix multiplication, but could also call FFTs if D is a DFT matrix, in a subclass.
- affine_map(x)#
Apply linear and affine offset to x.
Return \(Dx+\alpha\).
- Parameters:
x (ndarray) – Array to which to apply transform. Can be 1D or 2D.
- Returns:
Dx_a (ndarray) – x transformed with linear and offset components.
Notes
This routine is currently a matrix multiplication, but could also call FFTs if D is a DFT matrix, in a subclass.
- adjoint_map(x)#
Apply transpose of linear component to u.
Return \(D^Tu\).
- Parameters:
u (ndarray) – Array to which to apply transposed linear part of transform. Can be 1D or 2D array.
- Returns:
DTu (ndarray) – u transformed with transpose of linear component.
Notes
This routine is currently a matrix multiplication, but could also call FFTs if D is a DFT matrix, in a subclass.
regreg.affine.factored_matrix#
Low-rank (factored) matrices and the operator and nuclear norms.
This module contains an implementation of the operator and nuclear norms whose proximal maps use an iterative partial SVD and return the result as a factored_matrix. These are used in matrix completion problems and other low-rank factorization problems.
- class regreg.affine.factored_matrix.factored_matrix(linear_operator, min_singular=0.0, tol=1e-05, initial_rank=None, initial=None, affine_offset=None, debug=False)#
Bases:
objectA class for storing the SVD of a linear_transform. Has affine_transform attributes like linear_map.
- Parameters:
linear_operator (sequence or ndarray or affine_transform) – Either a 3-sequence
(U, D, VT)of SVD factors, or a transform whose (partial) SVD is computed with compute_iterative_svd.min_singular (float, optional) – Smallest singular value to keep; must be non-negative.
tol (float, optional) – Tolerance passed to compute_iterative_svd.
initial_rank (None or int, optional) – Initial rank guess passed to compute_iterative_svd.
initial (None or ndarray, optional) – Warm start passed to compute_iterative_svd.
affine_offset (None or ndarray, optional) – Offset added in affine_map.
debug (bool, optional) – Print debugging statements.
- copy()#
Return a factored_matrix with copies of the SVD factors.
- regreg.affine.factored_matrix.compute_iterative_svd(transform, initial_rank=None, warm_start=None, min_singular=1e-16, tol=1e-05, debug=False, stopping_rule=None, padding=5, update_rank=<function <lambda>>, max_rank=None, rng=0)#
Compute the SVD of a matrix using partial_svd.
If no initial rank is given, it assumes a rank of size
min(n,p) / 10 + 1.Iteratively calls
partial_svd()until the singular values are small enough.- Parameters:
transform ([linear_transform, ndarray]) – Linear_transform whose SVD is computed. If an ndarray, it is first cast with
astransform()initial_rank (None or int, optional) – A guess at the rank of the matrix.
warm_start (ndarray, optional) – A guess at the singular vectors of the matrix. For fat matrices, these should be right singular vectors, while for tall matrices these should be left singular vectors.
min_singular (float, optional) – Stop when the smallest singular value found is below min_singular times the largest; only singular values at least min_singular are returned.
tol (float, optional) – Tolerance at which the norm of the singular values are deemed to have converged.
debug (bool, optional) – Print debugging statements.
stopping_rule (None or callable, optional) – Function of the singular values
D, returning True | False, used to determine whether to stop. Continue whilestopping_rule(D) == False, or when stopping_rule is None.padding (int, optional) – How many more singular vectors are found.
update_rank (callable, optional) – A rule to update rank, defaults to doubling the rank.
max_rank (None or int, optional) – Largest rank considered. Defaults to
2 * min(transform.output_shape[0], transform.input_shape[0]). An exception is raised if algorithm exceeds given value.rng (int or np.random.Generator, optional) – Seed or generator for the random starting vectors, passed to np.random.default_rng. The default is a fixed seed, so that results are reproducible.
- Returns:
U, D, VT (ndarray) – An SVD of the transform.
Examples
>>> import regreg.api as rr >>> rng = np.random.default_rng(0) >>> X = rng.standard_normal((100, 200)) >>> U, D, VT = rr.compute_iterative_svd(X)[:3] >>> bool(np.linalg.norm(np.dot(U.T, U) - np.identity(100)) < 1.e-6) True
>>> bool(np.linalg.norm(np.dot(VT, VT.T) - np.identity(100)) < 1.e-6) True
- regreg.affine.factored_matrix.partial_svd(transform, rank=1, padding=5, max_its=1000, tol=1e-08, warm_start=None, return_full=False, debug=False, stopping_rule=None, rng=0)#
Compute partial SVD of the linear transform transform.
Uses the Mazumder/Hastie algorithm in (TODO: CITE), i.e. block power (subspace) iterations with QR decompositions.
- Parameters:
transform (linear_transform or ndarray) – Linear_transform whose SVD is computed. If an ndarray, it is first cast with
astransform().rank (int, optional) – What rank of SVD to compute.
padding (int, optional) – Compute a few further singular values / vectors. This results in a better estimator of the rank of interest.
max_its (int, optional) – How many iterations of the full cycle to complete.
tol (float, optional) – Tolerance at which the norm of the singular values are deemed to have converged.
warm_start (ndarray, optional) – A guess at the left singular vectors of the matrix, of shape
(n,),(n, k)or(n, rank + padding); it is padded with random columns if necessary.return_full (bool, optional) – Also return the singular values / vectors from padding?
debug (bool, optional) – Print debugging statements.
stopping_rule (None or callable, optional) – Function of the singular values used to determine whether to stop.
rng (int or np.random.Generator, optional) – Seed or generator for the random starting vectors, passed to np.random.default_rng. The default is a fixed seed, so that results are reproducible.
- Returns:
U, D, VT (ndarray) – An SVD up to rank (or
rank + paddingif return_full) of the transform.
Examples
>>> from regreg.affine.factored_matrix import partial_svd >>> rng = np.random.default_rng(0) >>> X = rng.standard_normal((100, 200)) >>> U, D, VT = partial_svd(X, rank=10)[:3] >>> assert(np.linalg.norm(np.dot(U.T, U) - np.identity(10)) < 1.e-4) >>> assert(np.linalg.norm(np.dot(VT, VT.T) - np.identity(10)) < 1.e-4) >>> U_np, D_np, VT_np = np.linalg.svd(X, full_matrices=False) >>> assert(np.linalg.norm(U - np.dot(U, np.dot(U_np.T[:10], U_np[:,:10]))) < 1.e-4) >>> assert(np.linalg.norm(VT - np.dot(VT, np.dot(VT_np[:10].T, VT_np[:10]))) < 1.e-4)
- class regreg.affine.factored_matrix.nuclear_norm(shape, lagrange=None, bound=None, offset=None, quadratic=None, initial=None, initial_rank=10, warm_start=None, rng=0)#
Bases:
nuclear_normThe nuclear norm, with proximal maps computed by an iterative partial SVD and returned as a factored_matrix.
- Parameters:
shape (tuple) – Shape of the matrix argument.
lagrange (float, optional) – Lagrange parameter (Lagrange form).
bound (float, optional) – Bound parameter (bound form).
offset (ndarray, optional) – Offset applied before evaluating the norm.
quadratic (identity_quadratic, optional) – Quadratic term added to the atom.
initial (ndarray, optional) – Ignored.
initial_rank (int, optional) – Initial rank guess for the iterative SVD.
warm_start (ndarray, optional) – Warm start for the singular vectors, of shape
(max(shape), initial_rank).rng (int or np.random.Generator, optional) – Seed or generator for the random warm start, if warm_start is None, passed to np.random.default_rng. The default is a fixed seed, so that results are reproducible.
- seminorm(X, check_feasibility=False, lagrange=None)#
Return \(\lambda \cdot \|B\|_*\).
Here \(\lambda\) is lagrange. If check_feasibility is True, and the seminorm is unbounded, will return np.inf if appropriate.
The base class seminorm just returns the appropriate Lagrange parameter for use by the subclasses.
- Parameters:
arg (np.ndarray) – Argument of the seminorm.
lagrange (float, optional) – Lagrange parameter. Defaults to self.lagrange.
check_feasibility (bool, optional) – If True, return np.inf if arg is infeasible.
- Returns:
value (float) – The value of the seminorm.
- constraint(X, bound=None)#
Verify \(\|B\|_* \leq \delta\).
Here \(\delta\) is bound. If the constraint is satisfied, returns 0, else returns np.inf.
The base class seminorm just returns the appropriate bound parameter for use by the subclasses.
- Parameters:
arg (np.ndarray) – Argument of the seminorm.
bound (float, optional) – Bound parameter. Defaults to self.bound.
- Returns:
value (float) – 0 if the constraint is satisfied, np.inf otherwise.
- lagrange_prox(X, lipschitz=1, lagrange=None)#
Return the unique minimizer of the Lagrange form proximal problem.
\[{B}^{\lambda}(\theta) = \text{argmin}_{B \in \mathbb{R}^{{n \times p}}} \frac{L}{2} \|\theta-B\|^2_2 + \lambda \|B\|_*\]Above, \(\lambda\) is the Lagrange parameter, \(L\) is the Lipschitz parameter and \(\theta\) is arg.
If the argument lagrange is None and the atom is in Lagrange mode,
self.lagrangeis used as the Lagrange parameter, else an exception is raised.The base class seminorm just returns the appropriate Lagrange parameter for use by the subclasses.
- Parameters:
arg (np.ndarray) – Argument of the proximal map.
lipschitz (float, optional) – Coefficient in front of the quadratic.
lagrange (float, optional) – Lagrange factor in front of the seminorm. Defaults to self.lagrange.
- Returns:
Z (np.ndarray) – The proximal map of arg.
- bound_prox(X, bound=None)#
Return the unique minimizer of the bound form proximal problem.
\[{B}^{\delta}(\theta) = \text{argmin}_{B \in \mathbb{R}^{{n \times p}}} \frac{1}{2} \|\theta-B\|^2_2 \ \text{s.t.} \ \|B\|_* \leq \delta\]where \(\delta\) is the bound parameter and \(\theta\) is arg.
If the argument bound is None and the atom is in bound mode,
self.boundis used as the bound parameter, else an exception is raised.The base class seminorm just returns the appropriate bound parameter for use by the subclasses.
- Parameters:
arg (np.ndarray) – Argument of the proximal map.
bound (float, optional) – Bound for the constraint on the seminorm. Defaults to self.bound.
- Returns:
Z (np.ndarray) – The proximal map of arg.
- property conjugate#
The conjugate of an atom.
- class regreg.affine.factored_matrix.operator_norm(shape, lagrange=None, bound=None, offset=None, quadratic=None, initial=None, initial_rank=10, warm_start=None, rng=0)#
Bases:
operator_normThe operator norm, with proximal maps computed via the proximal maps of the dual nuclear_norm.
- Parameters:
shape (tuple) – Shape of the matrix argument.
lagrange (float, optional) – Lagrange parameter (Lagrange form).
bound (float, optional) – Bound parameter (bound form).
offset (ndarray, optional) – Offset applied before evaluating the norm.
quadratic (identity_quadratic, optional) – Quadratic term added to the atom.
initial (ndarray, optional) – Passed to the dual nuclear_norm (where it is ignored).
initial_rank (int, optional) – Initial rank guess for the iterative SVD.
warm_start (ndarray, optional) – Warm start for the singular vectors, of shape
(max(shape), initial_rank).rng (int or np.random.Generator, optional) – Seed or generator for the random warm start, if warm_start is None, passed to np.random.default_rng. The default is a fixed seed, so that results are reproducible.
- seminorm(X, check_feasibility=False, lagrange=None)#
Return \(\lambda \cdot \|B\|_{\text{op}}\).
Here \(\lambda\) is lagrange. If check_feasibility is True, and the seminorm is unbounded, will return np.inf if appropriate.
The base class seminorm just returns the appropriate Lagrange parameter for use by the subclasses.
- Parameters:
arg (np.ndarray) – Argument of the seminorm.
lagrange (float, optional) – Lagrange parameter. Defaults to self.lagrange.
check_feasibility (bool, optional) – If True, return np.inf if arg is infeasible.
- Returns:
value (float) – The value of the seminorm.
- constraint(X, bound=None)#
Verify \(\|B\|_{\text{op}} \leq \delta\).
Here \(\delta\) is bound. If the constraint is satisfied, returns 0, else returns np.inf.
The base class seminorm just returns the appropriate bound parameter for use by the subclasses.
- Parameters:
arg (np.ndarray) – Argument of the seminorm.
bound (float, optional) – Bound parameter. Defaults to self.bound.
- Returns:
value (float) – 0 if the constraint is satisfied, np.inf otherwise.
- lagrange_prox(X, lipschitz=1, lagrange=None)#
Return the unique minimizer of the Lagrange form proximal problem.
\[{B}^{\lambda}(\theta) = \text{argmin}_{B \in \mathbb{R}^{{n \times p}}} \frac{L}{2} \|\theta-B\|^2_2 + \lambda \|B\|_{\text{op}}\]Above, \(\lambda\) is the Lagrange parameter, \(L\) is the Lipschitz parameter and \(\theta\) is arg.
If the argument lagrange is None and the atom is in Lagrange mode,
self.lagrangeis used as the Lagrange parameter, else an exception is raised.The base class seminorm just returns the appropriate Lagrange parameter for use by the subclasses.
- Parameters:
arg (np.ndarray) – Argument of the proximal map.
lipschitz (float, optional) – Coefficient in front of the quadratic.
lagrange (float, optional) – Lagrange factor in front of the seminorm. Defaults to self.lagrange.
- Returns:
Z (np.ndarray) – The proximal map of arg.
- bound_prox(X, bound=None)#
Return the unique minimizer of the bound form proximal problem.
\[{B}^{\delta}(\theta) = \text{argmin}_{B \in \mathbb{R}^{{n \times p}}} \frac{1}{2} \|\theta-B\|^2_2 \ \text{s.t.} \ \|B\|_{\text{op}} \leq \delta\]where \(\delta\) is the bound parameter and \(\theta\) is arg.
If the argument bound is None and the atom is in bound mode,
self.boundis used as the bound parameter, else an exception is raised.The base class seminorm just returns the appropriate bound parameter for use by the subclasses.
- Parameters:
arg (np.ndarray) – Argument of the proximal map.
bound (float, optional) – Bound for the constraint on the seminorm. Defaults to self.bound.
- Returns:
Z (np.ndarray) – The proximal map of arg.
- property conjugate#
The conjugate of an atom.
regreg.affine.multiscale#
- class regreg.affine.multiscale.multiscale(p, minsize=None, slices=[], scaling=None)#
Bases:
affine_transformAn affine transform representing the multiscale changepoint transform.
This transform centers the input and then computes the average over all intervals of a minimum size (or over a given list of intervals).
- update_slices(slices, scaling=None)#
Change the intervals computed by the multiscale transform.
- Parameters:
slices (sequence) – List of
(i,j)intervals to compute. Will be converted to havedtype([('start', int), ('end', int)]).scaling (ndarray, optional) – An optional scaling to apply after computing the mean over each interval.
- linear_map(x)#
Compute the (scaled) average of
x - x.mean()over each interval.If no intervals were specified, these are all intervals of size at least
self.minsize.- Parameters:
x (ndarray) – Signal of shape
self.input_shape.- Returns:
v (ndarray) – Interval averages, of shape
self.output_shape.
- affine_map(x)#
Apply linear and affine offset to x.
Return \(Dx+\alpha\).
- Parameters:
x (ndarray) – Array to which to apply transform. Can be 1D or 2D.
- Returns:
Dx_a (ndarray) – x transformed with linear and offset components.
Notes
This routine is currently a matrix multiplication, but could also call FFTs if D is a DFT matrix, in a subclass.
- adjoint_map(v)#
Apply the adjoint of the multiscale transform to v.
- Parameters:
v (ndarray) – Array of shape
self.output_shape.- Returns:
output (ndarray) – Array of shape
self.input_shape.
- form_matrix(slices)#
Form a matrix with given slices of multiscale transform.
- Parameters:
slices (sequence) – List of
(i,j)intervals.- Returns:
X (ndarray) – Matrix of shape
(p, len(slices))whose columns are the centered indicators of each interval divided by the square root of its length.length (ndarray) – Length of each interval.
- regreg.affine.multiscale.SMUCE_stat(M, Z, sigma=None)#
Compute multiscale test statistic of Chan and Walther.
Used in the SMUCE algorithm and in the multiscale change point algorithm in the square-root LASSO selective inference paper.
- Parameters:
M (multiscale) – Multiscale transform defining the intervals.
Z (ndarray) – Data on which to compute the statistic.
sigma (float, optional) – Noise level. Defaults to
np.std(Z).
- Returns:
T (float) – Value of the statistic.
slice (np.void) – The
(start, end)interval achieving the maximum.
Notes
M’s scaling is ignored, even if not None:
np.sqrt(M.sizes)is used instead, and M’s scaling is restored afterwards.
- regreg.affine.multiscale.choose_tuning_parameter(M, ndraw=100, quantile=0.95, sigma=None, rng=None)#
Choose tuning parameter by Monte Carlo.
Used in the SMUCE algorithm and in the multiscale change point algorithm in the square-root LASSO selective inference paper.
- Parameters:
M (multiscale) – Multiscale transform defining the intervals.
ndraw (int) – How many Monte Carlo draws.
quantile (float) – Which quantile of Monte Carlo draws to choose.
sigma (float, optional) – Noise level passed to SMUCE_stat.
rng (int or np.random.Generator, optional) – Seed or generator for the Monte Carlo draws, passed to np.random.default_rng.
- Returns:
value (float) – The quantile of SMUCE_stat over ndraw draws of standard normal noise.
Notes
M’s scaling is ignored, even if not None.
regreg.affine.block_maps#
- class regreg.affine.block_maps.block_columns(transforms)#
Bases:
affine_transformBlockwise transform: mapping where arguments have common number of columns.
Given a sequence of \(k\) maps \(X_i\) of shape \((n_i, p)\), produces a map taking input of shape \((p, k)\) to output of shape \((\sum_i n_i,)\)
\[\begin{split}(X_1, \dots, X_k)(\beta_{p \times k}) \mapsto \begin{pmatrix} X_1\beta_1 \\ \vdots \\ X_k \beta_k \end{pmatrix}\end{split}\]where \(\beta_i\) is the \(i\)-th column of \(\beta\).
- affine_map(arg)#
Apply linear and affine offset to x.
Return \(Dx+\alpha\).
- Parameters:
x (ndarray) – Array to which to apply transform. Can be 1D or 2D.
- Returns:
Dx_a (ndarray) – x transformed with linear and offset components.
Notes
This routine is currently a matrix multiplication, but could also call FFTs if D is a DFT matrix, in a subclass.
- linear_map(arg)#
Apply linear part of transform to x.
Return \(Dx\).
- Parameters:
x (ndarray) – Array to which to apply transform. Can be 1D or 2D.
- Returns:
Dx (ndarray) – x transformed with linear component
Notes
This routine is currently a matrix multiplication, but could also call FFTs if D is a DFT matrix, in a subclass.
- adjoint_map(arg)#
Apply transpose of linear component to u.
Return \(D^Tu\).
- Parameters:
u (ndarray) – Array to which to apply transposed linear part of transform. Can be 1D or 2D array.
- Returns:
DTu (ndarray) – u transformed with transpose of linear component.
Notes
This routine is currently a matrix multiplication, but could also call FFTs if D is a DFT matrix, in a subclass.
- class regreg.affine.block_maps.block_rows(transforms)#
Bases:
affine_transformBlockwise transform: mapping where arguments have common number of rows.
Given a sequence of \(k\) maps \(Z_i\) of shape \((p, n_i)\), produces a map taking input of shape \((\sum_i n_i,)\) to output of shape \((p, k)\).
Formally this is the adjoint of a block_columns
\[(Z_1, \dots, Z_k)((Y_1, \dots, Y_k)) \mapsto \begin{pmatrix} Z_1 Y_1 & \dots & Z_k Y_k \end{pmatrix}\]- affine_map(arg)#
Apply linear and affine offset to x.
Return \(Dx+\alpha\).
- Parameters:
x (ndarray) – Array to which to apply transform. Can be 1D or 2D.
- Returns:
Dx_a (ndarray) – x transformed with linear and offset components.
Notes
This routine is currently a matrix multiplication, but could also call FFTs if D is a DFT matrix, in a subclass.
- linear_map(arg)#
Apply linear part of transform to x.
Return \(Dx\).
- Parameters:
x (ndarray) – Array to which to apply transform. Can be 1D or 2D.
- Returns:
Dx (ndarray) – x transformed with linear component
Notes
This routine is currently a matrix multiplication, but could also call FFTs if D is a DFT matrix, in a subclass.
- adjoint_map(arg)#
Apply transpose of linear component to u.
Return \(D^Tu\).
- Parameters:
u (ndarray) – Array to which to apply transposed linear part of transform. Can be 1D or 2D array.
- Returns:
DTu (ndarray) – u transformed with transpose of linear component.
Notes
This routine is currently a matrix multiplication, but could also call FFTs if D is a DFT matrix, in a subclass.