Learn R Programming

polyMatrix (version 0.3.1)

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

Description

Returns the trace of the given matrix.

Usage

tr(matrixObject)

Arguments

matrixObject

a matrix or polyMatrix class object

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

Details

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

See Also

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

Examples

Run this code
# NOT RUN {
# the case of a matrix 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) # "polynomial"

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

# }

Run the code above in your browser using DataLab