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

Description Usage Arguments Details Value Examples

View source: R/method_trace.R

Description

Trace of a matrix is the sum of the diagonal elements of the given matrix.

Usage

1
tr(x)

Arguments

x

a matrix or a polynomial matrix

Details

If the given matrix is a polynomial matrix, the result will be a polynomial.

Value

Returns the trace of the given matrix as a number or a polynomial.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# numerical matrices
m <- matrix(1:12, 3, 4)
##      [,1] [,2] [,3] [,4]
## [1,]    1    4    7   10
## [2,]    2    5    8   11
## [3,]    3    6    9   12
tr(m)  ## 15

# polynomial matrix
pm <- parse.polyMatrix(
  "-3 + x^2, 2 + 4 x,  -x^2",
  "       1,       2, 3 + x",
  "     2*x,       0, 2 - 3 x"
)
tr(pm)  ## 1 - 3*x + x^2

polyMatrix documentation built on July 18, 2021, 5:06 p.m.