Trace of a 'matrix' or 'polyMatrix' class matrix
Returns the trace of the given matrix.
tr(matrixObject)
matrixObject |
a |
Calculates the sum of the diagonal elements of the given matrix.
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
tr.polyMatrix, polyMatrix-package, polynomial,
but the trace is a debugging utility
# 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)Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.