Learn R Programming

RTMB (version 1.8)

ADmatrix: AD matrix methods (sparse and dense)

Description

Matrices (base package) and sparse matrices (Matrix package) can be used inside the RTMB objective function as part of the calculations. Behind the scenes these R objects are converted to AD representations when needed. AD objects have a temporary lifetime, so you probably won't see them / need to know them. The only important thing is which methods work for the objects.

Usage

# S3 method for advector
chol(x, ...)

# S3 method for advector determinant(x, logarithm = TRUE, ...)

# S4 method for adcomplex eigen(x, symmetric, only.values = FALSE, EISPACK = FALSE)

# S4 method for advector eigen(x, symmetric, only.values = FALSE, EISPACK = FALSE)

# S4 method for advector svd(x, nu, nv, LINPACK = FALSE)

# S3 method for adsparse t(x)

# S3 method for adsparse [(x, ...)

# S3 method for adsparse [(x, ...) <- value

# S3 method for adsparse as.matrix(x, ...)

# S4 method for adsparse,missing,missing diag(x)

# S4 method for adsparse band(x, k1, k2)

# S4 method for adsparse tril(x, k)

# S4 method for adsparse triu(x, k)

# S4 method for advector expm(x)

# S4 method for adsparse expm(x)

# S4 method for adsparse dim(x)

# S4 method for anysparse,ad %*%(x, y)

# S4 method for ad,anysparse %*%(x, y)

# S4 method for adsparse,adsparse %*%(x, y)

# S4 method for ad,ad %*%(x, y)

# S4 method for ad,ad. tcrossprod(x, y)

# S4 method for ad,ad. crossprod(x, y)

# S4 method for advector cov2cor(V)

# S4 method for ad,ad. solve(a, b)

# S4 method for num,num. solve(a, b)

# S4 method for anysparse,ad. solve(a, b)

# S4 method for advector colSums(x, na.rm, dims)

# S4 method for advector rowSums(x, na.rm, dims)

# S4 method for adsparse colSums(x, na.rm, dims)

# S4 method for adsparse rowSums(x, na.rm, dims)

# S3 method for advector cbind(...)

# S3 method for advector rbind(...)

# S4 method for adsparse Math(x)

Value

List (vectors/values) with adcomplex components.

List (vectors/values) with advector components in symmetric case and adcomplex components otherwise.

Object of class advector with a dimension attribute for dense matrix operations; Object of class adsparse for sparse matrix operations.

Arguments

x

matrix (sparse or dense)

...

As cbind

logarithm

Not used

symmetric

Logical; Is input matrix symmetric (Hermitian) ?

only.values

Ignored

EISPACK

Ignored

nu

Ignored

nv

Ignored

LINPACK

Ignored

value

Replacement value

k1

See Matrix package

k2

See Matrix package

k

See Matrix package

y

matrix (sparse or dense)

V

Covariance matrix

a

matrix

b

matrix, vector or missing

na.rm

Logical; Remove NAs while taping.

dims

Same as colSums and rowSums.

Functions

  • chol(advector): AD matrix cholesky

  • determinant(advector): AD log determinant

  • eigen(adcomplex): General AD eigen decomposition for complex matrices. Note that argument symmetric is not auto-detected so must be specified.

  • eigen(advector): AD eigen decomposition for real matrices. The non-symmetric case is redirected to the adcomplex method. Note that argument symmetric is not auto-detected so must be specified.

  • svd(advector): AD svd decomposition for real matrices.

  • t(adsparse): AD sparse matrix transpose. Re-directs to t,CsparseMatrix-method.

  • [: AD sparse matrix subsetting. Re-directs to [-methods.

  • `[`(adsparse) <- value: AD sparse matrix subset assignment. Re-directs to [<--methods.

  • as.matrix(adsparse): Convert AD sparse to dense matrix.

  • diag(x = adsparse, nrow = missing, ncol = missing): AD sparse matrix diagonal extract. Re-directs to diag,CsparseMatrix-method.

  • band(adsparse): AD sparse matrix band extract. Re-directs to band,CsparseMatrix-method.

  • tril(adsparse): AD sparse matrix lower triangle extract. Re-directs to tril,CsparseMatrix-method.

  • triu(adsparse): AD sparse matrix upper triangle extract. Re-directs to triu,CsparseMatrix-method.

  • expm(advector): AD matrix exponential

  • expm(adsparse): AD matrix exponential

  • dim(adsparse): AD sparse matrix dimension

  • x %*% y: AD matrix multiply

  • x %*% y: AD matrix multiply

  • x %*% y: AD matrix multiply

  • x %*% y: AD matrix multiply

  • tcrossprod(x = ad, y = ad.): AD matrix multiply

  • crossprod(x = ad, y = ad.): AD matrix multiply

  • cov2cor(advector): AD matrix cov2cor

  • solve(a = ad, b = ad.): AD matrix inversion and solve

  • solve(a = num, b = num.): AD matrix inversion and solve

  • solve(a = anysparse, b = ad.): Sparse AD matrix solve

  • colSums(advector): AD matrix (or array) colsums

  • rowSums(advector): AD matrix (or array) rowsums

  • colSums(adsparse): AD sparse matrix colsums

  • rowSums(adsparse): AD sparse matrix rowsums

  • cbind(advector): AD matrix column bind

  • rbind(advector): AD matrix row bind

  • Math(adsparse): AD sparse matrix 'Math group' works for functions that preserve sparsity.

Examples

Run this code
F <- MakeTape(function(x) matrix(1:9,3,3) %*% x, numeric(3))
F$jacobian(1:3)
F <- MakeTape(function(x) Matrix::expm(matrix(x,2,2)), numeric(4))
F$jacobian(1:4)
F <- MakeTape(det, diag(2)) ## Indirectly available via 'determinant'
F$jacobian(matrix(1:4,2))

Run the code above in your browser using DataLab