nitorch_fastmath.qr
eig_sym
Compute the eigendecomposition of a Hermitian square matrix.
Note
We use the explicit QR algorithm, which is probably less stable than the implicit QR algorithm used in Lapack.
Note
Eigenvalues are not sorted
To sort eigenvalues and eigenvectors according to some criterion:
s, u = eig_sym(x, compute_u=True)
_, i = crit(s).sort()
s = s.gather(-1, i) # permute eigenvalues
i = i.unsqueeze(-2).expand(y.shape)
u = u.gather(-1, i) # permute eigenvectors
If the criterion is the value of the eigenvalues, it simplifies:
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a |
`(..., m, m) tensor`
|
Input hermitian matrix or field of matrices |
required |
compute_u |
`bool`
|
Compute the eigenvectors. If False, only return |
False
|
upper |
`bool`
|
Whether to use the upper or lower triangular component. |
True
|
inplace |
`bool`
|
If |
False
|
check_finite |
`bool`
|
If |
True
|
max_iter |
`int`
|
Maximum number of iterations. |
1024
|
tol |
`float`
|
Tolerance for early stopping.
Default: machine precision for |
1e-32
|
Returns:
| Name | Type | Description |
|---|---|---|
s |
`(..., m) tensor`
|
Eigenvalues. |
u |
`(..., m, m) tensor`, optional
|
Corresponding eigenvectors. |
rq_hessenberg
Compute the QR decomposition of a Hessenberg matrix and R` @ Q.
Note
This is slower than torch.qr when the batch size is small,
even though torch.qr does not know that h has a
Hessenberg form. It's just hard to beat lapack. With
larger batch size, both algorithms are on par.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
h |
`(..., n, n) tensor`
|
Hessenberg matrix, with shape |
required |
u |
`(..., n) tensor`
|
Vectors to transform with. With shape |
None
|
inplace |
`bool`
|
Process inplace. |
False
|
check_finite |
`bool`
|
Check that all input values are finite. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
rq |
`tensor`
|
Reverse product of the QR decomposition: |
qr_hessenberg
QR decomposition for Hessenberg matrices.
Note
This is slower than torch.qr when the batch size is small,
even though torch.qr does not know that h has a
Hessenberg form. It's just hard to beat lapack. With
larger batch size, both algorithms are on par.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
h |
`(..., n, n) tensor`
|
Hessenberg matrix, with shape |
required |
inplace |
`bool`
|
Process inplace. |
False
|
check_finite |
`bool`
|
Check that all input values are finite. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
q |
`tensor`
|
|
r |
`tensor`
|
|
hessenberg
Return an Hessenberg form of the matrix (or matrices) a.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a |
`(..., n, n) tensor`
|
Input matrix, with shape |
required |
inplace |
`bool`
|
Overwrite |
False
|
check_finite |
`bool`
|
Check that all values in |
True
|
compute_u |
`bool`
|
Compute and return the transformation matrix |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
h |
`(..., n, n) tensor`
|
Hessenberg form of |
u |
`list[tensor]`, optional
|
Set of Householder reflectors. |
References
- Hessenberg matrix, Wikipedia
hessenberg_sym
Return a tridiagonal form of the hermitian matrix (or matrices) a.
Note
The Hessenberg form of a Hermitian matrix is tridiagonal.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a |
`(..., n, n) tensor`
|
Input hermitian matrix, with shape (..., n, n)`. Can be complex. |
required |
upper |
`bool`
|
Whether to use the upper or lower triangular part of the matrix. |
True
|
fill |
`bool`
|
Fill the other half of the output matrix by conjugate symmetry. |
False
|
inplace |
`bool`
|
Overwrite |
False
|
check_finite |
`bool`
|
Check that all values in |
True
|
compute_u |
`bool`
|
Compute and return the transformation matrix |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
h |
`(..., n, n) tensor`
|
Hessenberg (tridiagonal) form of |
u |
`list[tensor]`, optional
|
Set of Householder reflectors. |
References
- Hessenberg matrix, Wikipedia
householder
Compute the Householder reflector of a (batch of) vector(s).
Note
Householder reflectors are matrices of the form \(\mathbf{P} = \mathbf{I} - 2\mathbf{u}\mathbf{u}^\ast\), where \(\mathbf{u}\) is a Householder vector. Reflectors are typically used to project a (complex) vector onto a Euclidean basis: \(\mathbf{P} \times \mathbf{x} = r \lVert\mathbf{x}\rVert \mathbf{e}_i\), with \(r = -\exp(i*\operatorname{angle}(x_i))\). This function returns a Householder vector tailored to a specific (complex) vector.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x |
`(..., n) tensor`
|
Input vector, with shape |
required |
basis |
`int`
|
Index of the Euclidean basis. |
1
|
inplace |
`bool`
|
If True, overwrite |
False
|
check_finite |
`bool`
|
If True, checks that the input matrix does not contain any non finite value. Disabling this may speed up the algorithm. |
True
|
return_alpha |
`bool`
|
Return alpha, the 'projection' of x on the Euclidean basis: \(-\lVert \mathbf{x} \rVert \times \exp(i*\operatorname{angle}(x_i))\), where \(i\) is the index of the Euclidean basis. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
u |
`(..., n) tensor`
|
Householder vector, with shape |
a |
`(...) tensor`, optional
|
Projection on the Euclidean basis, with shape |
householder_apply
Apply a series of Householder reflectors to a matrix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a |
`(..., n, n) tensor`
|
\(N \times N\) matrix, with shape |
required |
u |
`tensor or list[tensor]`
|
A list of Householder reflectors \(\left\{\mathbf{u}_k\right\}_{k=1}^K\). Each reflector forms the Householder matrix \(\mathbf{P}_k = \mathbf{I} - 2 \mathbf{u}_k \mathbf{u}_k^H\). |
required |
k |
`int or list[int]`
|
The index corresponding to each reflector. |
None
|
side |
`{'left', 'right', 'both'}`
|
Side to apply to. |
'both'
|
inverse |
`bool`
|
Apply the inverse transform |
False
|
inplace |
`bool`
|
Apply transformation inplace. |
False
|
check_finite |
`bool`
|
Check that the input does not have any nonfinite values |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
h |
`(..., n, n) tensor`
|
The transformed matrix: \(\mathbf{H} = \mathbf{U} \times \mathbf{A} \times \mathbf{U}^H\) with \(\mathbf{U} = \mathbf{P}_{k-2} \times ... \times \mathbf{P}_1\). |
givens
Compute a Givens rotation matrix.
Note
A Givens rotation is a rotation in a plane that aligns a specific vector (in this plane) with the first axis of the plane: $$ \mathbf{G} \times \mathbf{x} = \left(\lVert\mathbf{x}\rVert, \mathbf{0} \right)^\mathrm{T}. $$
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x |
`tensor`
|
First vector component |
required |
y |
`tensor`
|
Second vector component |
required |
Returns:
| Name | Type | Description |
|---|---|---|
c |
`tensor`
|
Cosine of the Givens rotation: |
s |
`tensor`
|
Sine of the Givens rotation: |
References
- Givens rotation, Wikipedia
givens_apply
Apply a Givens rotation to a matrix
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a |
`(..., n, n) tensor`
|
Input matrix |
required |
c |
`tensor`
|
Cosine of the Givens rotation |
required |
s |
`tensor`
|
Sine of the Givens rotation |
required |
i |
int
|
Index in |
0
|
j |
`int`
|
Index in |
`i+1`
|
side |
`{'left', 'right', 'both'}`
|
Whether to apply the rotation on the left or on the right. |
'both'
|
inplace |
`bool`
|
Apply the rotation in-place. |
False
|
check_finite |
`bool`
|
Check for non-finite values |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
a |
`(..., n, n) tensor`
|
Rotated matrix |
References
- Givens rotation, Wikipedia