tr {gremlin} | R Documentation |
Methods to efficiently calculate a matrix trace depending on the class of matrix.
tr(X, ...) ## Default S3 method: tr(X, ...) ## S3 method for class 'dgCMatrix' tr(X, ...) ## S3 method for class 'dsCMatrix' tr(X, ...)
X |
A matrix. |
... |
Additional arguments. |
A numeric
value for the sum of the diagonal elements.
default
: Default method
dgCMatrix
: Method for matrix X
of class Matrix:::dgCMatrix
dsCMatrix
: Method for matrix X
of class Matrix:::dsCMatrix
M <- matrix(seq(9), nrow = 3) tr(M) # Create sparse 'Matrix' dgcM <- as(M, "dgCMatrix") # compare `tr()` methods tr(M) == tr(dgcM) #<-- should be TRUE