diag {polyMatrix} | R Documentation |
Polynomial matrix Diagonals Extract or construct a diagonal polynomial matrix.
Description
Polynomial matrix Diagonals Extract or construct a diagonal polynomial matrix.
Usage
diag(x = 1, nrow, ncol, names = TRUE)
## S4 method for signature 'polynomial'
diag(x, nrow, ncol)
## S4 method for signature 'polyMatrix'
diag(x)
Arguments
x |
a polynomial matrix, or a polynomial, or an R object |
nrow , ncol |
optional dimensions for the result when x is not a matrix |
names |
not used for polynomial matrices |
Details
In case of polynomial objects, diag
has 2 distinct usage:
-
x
is a polynomial, returns a polynomial matrix the given diagonal and zero off-diagonal entries. -
x
is a polynomial matrix, returns a vector as a polynomial matrix of diagonal elements
For polynomial, either nrow
or ncol
must be provided.
Methods (by class)
-
polynomial
: for a polynomial, returns a polynomial matrix with the given diagonal -
polyMatrix
: for a polynomial matrix extracts diagonalFor polynomial matrix, neither
nrow
norncol
cannot be provided.
See Also
Base base::diag()
for numericals and numerical matrices
Examples
# numericals and numerical matrices
diag(matrix(1:12, 3, 4)) ## 1 5 8
diag(9, 2, 2)
## [,1] [,2]
## [1,] 9 0
## [2,] 0 9
# polynomial
diag(parse.polynomial("1+x+3x^2"), 2, 3)
## [,1] [,2] [,3]
## [1,] 1 + x + 3x^2 0 0
## [2,] 0 1 + x + 3x^2 0
# polynomial matrix
diag(parse.polyMatrix(
"-3 + x^2, 2 + 4 x, -x^2",
" 1, 2, 3 + x",
" 2x, 0, 2 - 3x"
))
## [,1] [,2] [,3]
## [1,] -3 + x^2 2 2 - 3x