Learn R Programming

torch (version 0.8.1)

torch_symeig: Symeig

Description

Symeig

Usage

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

Arguments

self

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

eigenvectors

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

upper

(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.

Examples

Run this code
if (torch_is_installed()) {

a = torch_randn(c(5, 5))
a = a + a$t()  # To make a symmetric
a
o = torch_symeig(a, eigenvectors=TRUE)
e = o[[1]]
v = o[[2]]
e
v
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)
}

Run the code above in your browser using DataLab