Learn R Programming

nimble (version 1.2.1)

nimSvd: Singular Value Decomposition of a Matrix

Description

Computes singular values and, optionally, left and right singular vectors of a numeric matrix.

Usage

nimSvd(x, vectors = "full")

Value

The singular value decomposition of x is returned as a nimbleList with elements:

  • d length m vector containing the singular values of x, sorted in decreasing order.

  • v matrix with columns containing the left singular vectors of x, or an empty matrix if vectors = 'none'.

  • u matrix with columns containing the right singular vectors of x, or an empty matrix if vectors = 'none'.

Arguments

x

a symmetric numeric matrix (double or integer) whose spectral decomposition is to be computed.

vectors

character that determines whether to calculate left and right singular vectors. Can take values 'none', 'thin' or 'full'. Defaults to 'full'. See ‘Details’.

Author

NIMBLE development team

Details

Computes the singular value decomposition of a numeric matrix using the Eigen C++ template library.

The vectors character argument determines whether to compute no left and right singular vectors ('none'), thinned left and right singular vectors ('thin'), or full left and right singular vectors ('full'). For a matrix x with dimensions n and p, setting vectors = 'thin' will does the following (quoted from eigen website): In case of a rectangular n-by-p matrix, letting m be the smaller value among n and p, there are only m singular vectors; the remaining columns of U and V do not correspond to actual singular vectors. Asking for thin U or V means asking for only their m first columns to be formed. So U is then a n-by-m matrix, and V is then a p-by-m matrix. Notice that thin U and V are all you need for (least squares) solving.

Setting vectors = 'full' will compute full matrices for U and V, so that U will be of size n-by-n, and V will be of size p-by-p.

In a nimbleFunction, svd is identical to nimSvd.

returnType(svdNimbleList()) can be used within a link{nimbleFunction} to specify that the function will return a nimbleList generated by the nimSvd function. svdNimbleList() can also be used to define a nested nimbleList element. See the User Manual for usage examples.

See Also

nimEigen for spectral decompositions.

Examples

Run this code
 singularValuesDemoFunction <- nimbleFunction(
   setup = function(){
     demoMatrix <- diag(4) + 2
   },
   run = function(){
     singularValues <- svd(demoMatrix)$d
     returnType(double(1))
     return(singularValues)
 })

Run the code above in your browser using DataLab