# diag: Polynomial matrix Diagonals Extract or construct a diagonal... In polyMatrix: Infrastructure for Manipulation Polynomial Matrices

## Description

Polynomial matrix Diagonals Extract or construct a diagonal polynomial matrix.

## Usage

 ```1 2 3 4 5 6 7``` ```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 diagonal

For polynomial matrix, neither `nrow` nor `ncol` cannot be provided.

Base `base::diag()` for numericals and numerical matrices
 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24``` ```# 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 ```