Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

tr.pMatrix

Trace of a 'matrix' or 'polyMatrix' class matrix


Description

Returns the trace of the given matrix.

Usage

tr(matrixObject)
    ## S3 method for class 'matrix'
tr(matrixObject)
    ## S3 method for class 'polyMatrix'
tr(matrixObject)

Arguments

matrixObject

a matrix or polyMatrix class object

Details

Calculate the sum of the diagonal elements of the given matrix.

Value

  • A numeric class object, if the given matrix is a matrix class object.

  • A polynomial class object, if the given matrix is a polyMatrix class object.

See Also

tr, polyMatrix-package, polynomial, but the trace is a debugging utility

Examples

# the case of matrix a class input object
M <- matrix(1:9,3,3)
class(M)
M
# 1  4  7
# 2  5  8
# 3  6  9
tr(M) # 15

( M <- matrix(1:12,3,4) )
# 1  4  7  10
# 2  5  8  11
# 3  6  9  12
tr(M) # 15

( M <- matrix(1:12,4,3) )
# 1  5   9
# 2  6  10
# 3  7  11
# 4  8  12
tr(M) # 18

# case of polyMatrix class input objects
pM <- polyMgen.d(3,3,rawData=
       ch2pn(c("-3 + z^2","2 + 4*z","-z^2",
               "1",       "2","3 + z",
               "2*z","0","2 - 3*z"),"z"),byrow=TRUE,symb="z")
class(pM) # "polyMdlist" "polyMatrix"
pM

( p <- tr(pM) ) # 1 - 3*x + x^2
class(p) # "polinomial"

# clean up
# rm(M, pM, p)

polyMatrix

Infrastructure for Manipulation Polynomial Matrices

v0.3.1
MIT + file LICENSE
Authors
Tamas Prohle [aut], Peter Prohle [aut], Nikolai Ryzhkov [aut, cre], Ildiko Laszlo [aut] (<https://orcid.org/0000-0003-2324-8183>), Ulas Onat Alakent [ctb]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.