
eigs()
function in the RSpectra package. Also see the documentation there.Given an $n$ by $n$ matrix $A$,
function eigs()
can calculate a limited
number of eigenvalues and eigenvectors of $A$.
Users can specify the selection criteria by argument
which
, e.g., choosing the $k$ largest or smallest
eigenvalues and the corresponding eigenvectors.
Currently eigs()
supports matrices of the following classes:
matrix |
The most commonly used matrix type, defined in base package. |
dgeMatrix |
General matrix, equivalent to matrix ,
defined in Matrix package. |
dgCMatrix |
Column oriented sparse matrix, defined in Matrix package. |
dgRMatrix |
Row oriented sparse matrix, defined in Matrix package. |
dsyMatrix |
Symmetrix matrix, defined in Matrix package. |
eigs_sym()
assumes the matrix is symmetric,
and only the lower triangle (or upper triangle, which is
controlled by the argument lower
) is used for
computation, which guarantees that the eigenvalues and eigenvectors are
real, and in some cases reduces the workload. One exception is when
A
is a function, in which case the user is responsible for the
symmetry of the operator.
eigs_sym()
supports "matrix", "dgeMatrix", "dgCMatrix", "dgRMatrix"
and "function" typed matrices.
eigs(A, k, which = "LM", sigma = NULL, opts = list(), ...)
eigs_sym(A, k, which = "LM", sigma = NULL, opts = list(), lower = TRUE, ...)
n
and args
that are
related to the Function Interface. See
eigs()
in the RSpectra package.vectors[, j]
corresponds to values[j]
.sigma
argument is used in the shift-and-invert mode. When sigma
is not NULL
, the selection criteria specified
by argument which
will apply to which = "LM"
will select the
largest values of $1/|\lambda|$, which turns out to select
eigenvalues of $A$ that have the smallest magnitude. The result of
using which = "LM", sigma = 0
will be the same as
which = "SM"
, but the former one is preferable
in that ARPACK is good at finding large
eigenvalues rather than small ones. More explanation of the
shift-and-invert mode can be found in the SciPy document,
http://docs.scipy.org/doc/scipy/reference/tutorial/arpack.html.function(x, args) { ## should return A %*% x }which receives a vector
x
as an argument and returns a vector
of the same length. The function should have the effect of calculating
$A * x$, and extra arguments can be passed in through the
args
parameter. In eigs()
, user should also provide
the dimension of the implicit matrix through the argument n
.which
argument is a character string
that specifies the type of eigenvalues to be computed.
Possible values are:"LM" |
The $k$ eigenvalues with largest magnitude. Here the magnitude means the Euclidean norm of complex numbers. |
"SM" |
The $k$ eigenvalues with smallest magnitude. |
"LR" |
The $k$ eigenvalues with largest real part. |
"SR" |
The $k$ eigenvalues with smallest real part. |
"LI" |
The $k$ eigenvalues with largest imaginary part. |
"SI" |
The $k$ eigenvalues with smallest imaginary part. |
"LA" |
The $k$ largest (algebraic) eigenvalues, considering any negative sign. |
"SA" |
The $k$ smallest (algebraic) eigenvalues, considering any negative sign. |
eigs()
with matrix type "matrix", "dgeMatrix", "dgCMatrix"
and "dgRMatrix" can use "LM",
"SM", "LR", "SR", "LI" and "SI".
eigs_sym()
, and eigs()
with matrix type "dsyMatrix"
can use "LM", "SM", "LA", "SA" and "BE".
The opts
argument is a list that can supply any of the
following parameters:
ncv
ncv
must satisfy
$k+2 <= ncv="" <="n$," and="" for="" symmetric="" matrix,="" the="" constraint="" is="" $k="" default min(n, max(2*k+1, 20)).
tol
maxitr
retvec
eigen()
, svd()
,
svds()
library(Matrix)
n = 20
k = 5
## general matrices have complex eigenvalues
set.seed(111)
A1 = matrix(rnorm(n^2), n) ## class "matrix"
A2 = Matrix(A1) ## class "dgeMatrix"
eigs(A1, k)
eigs(A2, k, opts = list(retvec = FALSE)) ## eigenvalues only
## sparse matrices
A1[sample(n^2, n^2 / 2)] = 0
A3 = as(A1, "dgCMatrix")
A4 = as(A1, "dgRMatrix")
eigs(A3, k)
eigs(A4, k)
## function interface
f = function(x, args)
{
as.numeric(args %*% x)
}
eigs(f, k, n = n, args = A3)
## symmetric matrices have real eigenvalues
A5 = crossprod(A1)
eigs_sym(A5, k)
## find the smallest (in absolute value) k eigenvalues of A5
eigs_sym(A5, k, which = "SM")
## another way to do this: use the sigma argument
eigs_sym(A5, k, sigma = 0)
## The results should be the same,
## but the latter method is far more stable on large matrices
Run the code above in your browser using DataLab