isTriangular: Test whether a Matrix is Triangular or Diagonal In Matrix: Sparse and Dense Matrix Classes and Methods

 isTriangular R Documentation

Test whether a Matrix is Triangular or Diagonal

Description

`isTriangular` and `isDiagonal` test whether their argument is a triangular or diagonal matrix, respectively. Unlike the analogous `isSymmetric`, these two functions are generically from Matrix rather than `base`. Hence Matrix defines methods for traditional matrices of implicit class `"matrix"` in addition to matrices inheriting from virtual class `"Matrix"`.

By our definition, triangular and diagonal matrices are square, i.e., they have the same number of rows and columns.

Usage

```isTriangular(object, upper = NA, ...)

isDiagonal(object)
```

Arguments

 `object` an R object, typically a matrix. `upper` a logical, either `TRUE` or `FALSE`, in which case `TRUE` is returned only for upper or lower triangular `object`; or otherwise `NA` (the default), in which case `TRUE` is returned for any triangular `object`. `...` further arguments passed to methods (currently unused by Matrix).

Value

A logical, either `TRUE` or `FALSE` (never `NA`).

If `object` is triangular and `upper` is `NA`, then `isTriangular` returns `TRUE` with an attribute `kind`, either `"U"` or `"L"`, indicating that `object` is upper or lower triangular, respectively. Users should not rely on how `kind` is determined for diagonal matrices, which are both upper and lower triangular.

`isSymmetric`; virtual classes `"triangularMatrix"` and `"diagonalMatrix"` and their subclasses.

Examples

```isTriangular(Diagonal(4))
## is TRUE: a diagonal matrix is also (both upper and lower) triangular
(M <- Matrix(c(1,2,0,1), 2,2))
isTriangular(M) # TRUE (*and* of formal class "dtrMatrix")
isTriangular(as(M, "generalMatrix")) # still triangular, even if not "formally"
isTriangular(crossprod(M)) # FALSE

isDiagonal(matrix(c(2,0,0,1), 2,2)) # TRUE

## Look at implementations:
showMethods("isTriangular", includeDefs = TRUE)
showMethods("isDiagonal", includeDefs = TRUE)
```

Matrix documentation built on Nov. 11, 2022, 9:06 a.m.