nitorch_fastmath.sym
Overview
This module contains linear-algebra routines (matrix-vector product, matrix inversion, etc.) for batches of symmetric matrices stored in a compact way (that is, only \(N(N+1)/2\) values are stored, instead of \(N^2\)).
Our compact representation differs from classical "columns" or "rows" layouts. The compact flattened matrix should contain the diagonal of the matrix first, followed by the rows of the upper triangle of the matrix, i.e.:
[ a d e ]
[ . b f ] => [a b c d e f]
[ . . c ]
Note that matrix-vector functions (matvec, solve) also accept (and
automatically detect) compact diagonal matrices and compact scaled identity
matrices. If the vector has shape (*, N) and the matrix has shape (*, NN),
where * is any number of leading batch dimensions, NN can take values:
1: and the matrix is assumed to be a scaled identity,N: and the matrix is assumed to be a diagonal matrix,N*(N+1)//2: and the matrix is assumed to be symmetric,N*N: and the matrix is assumed to be full.
sym_to_full
Transform a symmetric matrix into a full matrix
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mat |
`(..., M*(M+1)//2) tensor`
|
A \(M \times M\) symmetric matrix that is stored in a compact way,
with shape |
required |
Returns:
| Name | Type | Description |
|---|---|---|
full |
`(..., M, M) tensor`
|
Full matrix |
sym_matvec
Matrix-vector product for compact symmetric matrices
Equivalent to out = mat @ vec.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mat |
`(..., CC) tensor`
|
Symmetric matrix with compact storage, with shape
|
required |
vec |
`(..., C) tensor`
|
Vector with shape |
required |
dtype |
`torch.dtype`
|
Data type used to carry the computation. By default, same as input. |
None
|
out |
`(..., C) tensor`
|
Output placeholder, with shape |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
out |
(..., C) tensor
|
Matrix-vector product, with shape |
sym_diag
Diagonal of a symmetric matrix
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mat |
`(..., M * (M+1) // 2) tensor`
|
A \(M \times M\) symmetric matrix that is stored in a compact way,
with shape |
required |
Returns:
| Name | Type | Description |
|---|---|---|
diag |
`(..., M) tensor`
|
View into the main diagonal of the matrix, with shape |
sym_addmatvec
Add a matrix-vector product for compact symmetric matrices
Equivalent to out = inp + mat @ vec
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inp |
`(..., C) tensor`
|
Vector to which the matrix-vector product is added.
With shape |
required |
mat |
`(..., CC) tensor`
|
Symmetric matrix with compact storage, with shape
|
required |
vec |
`(..., C) tensor`
|
Vector with shape |
required |
dtype |
`torch.dtype`
|
Data type used to carry the computation. By default, same as input. |
None
|
out |
`(..., C) tensor`
|
Output placeholder, with shape |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
out |
`(..., C) tensor`
|
Added matrix-vector product, with shape |
sym_addmatvec_
Inplace add a matrix-vector product for compact symmetric matrices
Equivalent to inp += mat @ vec
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inp |
`(..., C) tensor`
|
Vector to which the matrix-vector product is added.
With shape |
required |
mat |
`(..., CC) tensor`
|
Symmetric matrix with compact storage, with shape
|
required |
vec |
`(..., C) tensor`
|
Vector with shape |
required |
dtype |
`torch.dtype`
|
Data type used to carry the computation. By default, same as input. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
inp |
`(..., C) tensor`
|
Added matrix-vector product, with shape |
sym_submatvec
Subtract a matrix-vector product for compact symmetric matrices
Equivalent to out = inp - mat @ vec
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inp |
`(..., C) tensor`
|
Vector to which the matrix-vector product is added.
With shape |
required |
mat |
`(..., CC) tensor`
|
Symmetric matrix with compact storage, with shape
|
required |
vec |
`(..., C) tensor`
|
Vector with shape |
required |
dtype |
`torch.dtype`
|
Data type used to carry the computation. By default, same as input. |
None
|
out |
`(..., C) tensor`
|
Output placeholder, with shape |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
out |
`(..., C) tensor`
|
Subtracted matrix-vector product, with shape |
sym_submatvec_
Inplace subtract a matrix-vector product for compact symmetric matrices
Equivalent to inp -= mat @ vec
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inp |
`(..., C) tensor`
|
Vector to which the matrix-vector product is added.
With shape |
required |
mat |
`(..., CC) tensor`
|
Symmetric matrix with compact storage, with shape
|
required |
vec |
`(..., C) tensor`
|
Vector with shape |
required |
dtype |
`torch.dtype`
|
Data type used to carry the computation. By default, same as input. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
inp |
`(..., C) tensor`
|
Subtracted matrix-vector product, with shape |
sym_solve
Solve the symmetric linear system
Equivalent to out = (mat + diag).inverse() @ vec
Warning
Does not backpropagate through mat.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mat |
`(..., CC) tensor`
|
Symmetric matrix with compact storage, with shape
|
required |
vec |
`(..., C) tensor`
|
Vector with shape |
required |
diag |
`(..., C) tensor`
|
Diagonal regularizer, with shape |
None
|
dtype |
`torch.dtype`
|
Data type used to carry the computation. By default, same as input. |
None
|
out |
`(..., C) tensor`
|
Output placeholder, with shape |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
out |
`(..., C) tensor`
|
Solution of the linear system, with shape |
sym_solve_
Solve the symmetric linear system in-place
Equivalent to vec = mat.inverse() @ vec
Warning
Does not backpropagate through mat.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mat |
`(..., CC) tensor`
|
Symmetric matrix with compact storage, with shape
|
required |
vec |
`(..., C) tensor`
|
Vector with shape |
required |
diag |
`(..., C) tensor`
|
Diagonal regularizer, with shape |
None
|
dtype |
`torch.dtype`
|
Data type used to carry the computation. By default, same as input. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
vec |
(..., C) tensor
|
Solution of the linear system, with shape |
sym_invert
Invert a compact symmetric matrix
Equivalent to out = mat.inverse()
Warning
Does not backpropagate through mat.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mat |
`(..., C*(C+1)//2) tensor`
|
Symmetric matrix with compact storage, with shape |
required |
dtype |
dtype
|
Data type used to carry the computation. By default, same as input. |
None
|
out |
`(..., C*(C+1)//2) tensor`
|
Output placeholder, with shape |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
mat |
`(..., C*(C+1)//2) tensor`
|
Inverse matrix, with shape |
sym_det
Determinant of a compact symmetric matrix.
Warning
- Currently, autograd does not work through this function.
Notes
- Orders up to 4 are implemented in closed-form.
- Orders > 4 use torch's batched implementation but require building the full matrices.
- Backpropagation works at least for torch >= 1.6 It should be checked on earlier versions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mat |
`(..., M*(M+1)//2) tensor`
|
A \(M \times M\) symmetric matrix that is stored in a compact way,
with shape |
required |
Returns:
| Name | Type | Description |
|---|---|---|
result |
`(...) tensor`
|
Output determinant, with shape |
sym_invert_
Invert a compact symmetric matrix in-place
Equivalent to mat = mat.inverse()
Warning
Does not backpropagate through mat.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mat |
`(..., C*(C+1)//2) tensor`
|
Symmetric matrix with compact storage, with shape |
required |
dtype |
`torch.dtype`
|
Data type used to carry the computation. By default, same as input. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
mat |
`(..., C*(C+1)//2) tensor`
|
Inverse matrix, with shape |
sym_outer
Compute the symmetric outer product of a vector: \(\mathbf{x} \times \mathbf{x}^T\)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x |
`(..., M) tensor`
|
Input vector with shape |
required |
Returns:
| Name | Type | Description |
|---|---|---|
xx |
`(..., M*(M+1)//2) tensor`
|
Output outer product with shape |
sym_matmul
Compute the symmetric matrix product \(\mathbf{J}^\mathrm{T}\mathbf{H}\mathbf{J}\), where \(\mathbf{H}\) is a compact symmetric matrix, and return a compact symmetric matrix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
j |
(..., k, d) tensor
|
Non symmetric matrix |
required |
h |
(..., k*(k+1)//2) tensor
|
Symmetric matrix with compact storage. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
jhj |
(..., d*(d+1)//2) tensor
|
Symmetric matrix with compact storage. |