torch_symeig: Symeig

View source: R/gen-namespace.R

torch_symeigR Documentation





torch_symeig(self, eigenvectors = FALSE, upper = TRUE)



(Tensor) the input tensor of size (*, n, n) where * is zero or more batch dimensions consisting of symmetric matrices.


(boolean, optional) controls whether eigenvectors have to be computed


(boolean, optional) controls whether to consider upper-triangular or lower-triangular region

symeig(input, eigenvectors=False, upper=TRUE) -> (Tensor, Tensor)

This function returns eigenvalues and eigenvectors of a real symmetric matrix input or a batch of real symmetric matrices, represented by a namedtuple (eigenvalues, eigenvectors).

This function calculates all eigenvalues (and vectors) of input such that \mbox{input} = V \mbox{diag}(e) V^T.

The boolean argument eigenvectors defines computation of both eigenvectors and eigenvalues or eigenvalues only.

If it is FALSE, only eigenvalues are computed. If it is TRUE, both eigenvalues and eigenvectors are computed.

Since the input matrix input is supposed to be symmetric, only the upper triangular portion is used by default.

If upper is FALSE, then lower triangular portion is used.


The eigenvalues are returned in ascending order. If input is a batch of matrices, then the eigenvalues of each matrix in the batch is returned in ascending order.

Irrespective of the original strides, the returned matrix V will be transposed, i.e. with strides ⁠V.contiguous().transpose(-1, -2).stride()⁠.

Extra care needs to be taken when backward through outputs. Such operation is really only stable when all eigenvalues are distinct. Otherwise, NaN can appear as the gradients are not properly defined.


if (torch_is_installed()) {

a = torch_randn(c(5, 5))
a = a + a$t()  # To make a symmetric
o = torch_symeig(a, eigenvectors=TRUE)
e = o[[1]]
v = o[[2]]
a_big = torch_randn(c(5, 2, 2))
a_big = a_big + a_big$transpose(-2, -1)  # To make a_big symmetric
o = a_big$symeig(eigenvectors=TRUE)
e = o[[1]]
v = o[[2]]
torch_allclose(torch_matmul(v, torch_matmul(e$diag_embed(), v$transpose(-2, -1))), a_big)

torch documentation built on June 7, 2023, 6:19 p.m.