Learn R Programming

polyMatrix (version 0.3.1)

diag.polyMatrix: Extracts the diagonal of a polyMatrix, or constructs a diagonal polyMatrix

Description

If the input is a polyMatrix then the output is its diagonal. If the input is a vector then the output is a diagonal matrix.

Usage

# S3 method for list
diag(x, nrow=NULL, ncol=NULL, names=NULL, type=CLASS_LIST, ...)
# S3 method for polyMatrix
diag(x, nrow=NULL, ncol=NULL, names=NULL, type=CLASS_LIST, ...)

Arguments

v

the list class input material for the diagonal matrix

nrow

number of rows in the output object

ncol

number of columns in the output object

pm

the polyMatrix class input matrix

type

the required form and class of the result: CLASS_LIST or CLASS_MATRIX

Value

The returned value is a

list

class object, when it extracts the diagonal from a polyMatrix, and the value of type isn't 'polyMatrix'.

polyMatrix

class object, when builds a matrix from the given diagonal or when returns the diagonal of a polyMatrix, and the type="polyMatrix".

Details

The diag has two different applications. When the class of the first input parameter is

  • 'list', then returns a polyMatrix with the given diagonal and zero off-diagonal entries.

  • 'polyMatrix', then extracts the diagonal.

Note. In the first case the elements of the list must be polynomial class objects. In the second case the result is a list or polyMatrix class object determined by the value of the type parameter.

See Also

polyMatrix-package.

Examples

Run this code
# NOT RUN {
# ---
# case of real matrices

m<-matrix(1:12,3,4)
m
diag(m) # first type usage

v<-c(1,5,9) # second type usage
diag(v)
diag(v,4)
diag(v,4,2)
diag(v,ncol=4)

# ---
# case of polynomial matrices

# first kind usage: catch the diagonal elements

px<-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)
class(px) # "polyMdlist" "polyMatrix"
px

w<-diag(px) # the default: type="list"
class(w) # "list"
# List of 3
# $ :Class 'polynomial'  num [1:3] -3 0 1
# $ :Class 'polynomial'  num 2
# $ :Class 'polynomial'  num [1:2] 2 -3
w

w<-diag(px,type="polyMatrix")
class(w) # "polyMdlist" "polyMatrix"
w


# second kind usage: compose a diagonal polyMatrix

ci<-c(4,1,3,2)
v<-vector("list",length(ci))
for(i in 1:length(ci))
  v[[i]]<-polynom::polynomial(c(rep(0,ci[i]),1))
class(v) # "list"
v

diag(v)
diag(v,4)
diag(v,3)
diag(v,5)
diag(v,3,5)

pd<-diag(v,4,5)
class(pd) # "polyMdlist" "polyMatrix"
pd

# clean up
# rm(ci, i, m, pd, px, v, w)

# }

Run the code above in your browser using DataLab