isTriangular-methods.RdisTriangular 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.
isTriangular(object, upper = NA, ...)
isDiagonal(object)an R object, typically a matrix.
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).
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.
isTriangular(Diagonal(4))
#> [1] TRUE
#> attr(,"kind")
#> [1] "U"
## is TRUE: a diagonal matrix is also (both upper and lower) triangular
(M <- Matrix(c(1,2,0,1), 2,2))
#> 2 x 2 Matrix of class "dtrMatrix"
#> [,1] [,2]
#> [1,] 1 .
#> [2,] 2 1
isTriangular(M) # TRUE (*and* of formal class "dtrMatrix")
#> [1] TRUE
#> attr(,"kind")
#> [1] "L"
isTriangular(as(M, "generalMatrix")) # still triangular, even if not "formally"
#> [1] TRUE
#> attr(,"kind")
#> [1] "L"
isTriangular(crossprod(M)) # FALSE
#> [1] FALSE
isDiagonal(matrix(c(2,0,0,1), 2,2)) # TRUE
#> [1] TRUE
## Look at implementations:
showMethods("isTriangular", includeDefs = TRUE)
#> Function: isTriangular (package Matrix)
#> object="CsparseMatrix"
#> function (object, upper = NA, ...)
#> .Call(R_sparse_is_triangular, object, upper)
#>
#>
#> object="RsparseMatrix"
#> function (object, upper = NA, ...)
#> .Call(R_sparse_is_triangular, object, upper)
#>
#>
#> object="TsparseMatrix"
#> function (object, upper = NA, ...)
#> .Call(R_sparse_is_triangular, object, upper)
#>
#>
#> object="denseMatrix"
#> function (object, upper = NA, ...)
#> .Call(R_dense_is_triangular, object, upper)
#>
#>
#> object="diagonalMatrix"
#> function (object, upper = NA, ...)
#> if (is.na(upper)) `attr<-`(TRUE, "kind", "U") else TRUE
#>
#>
#> object="indMatrix"
#> function (object, upper = NA, ...)
#> {
#> d <- object@Dim
#> if ((n <- d[2L]) != d[1L])
#> return(FALSE)
#> if (object@margin == 1L) {
#> i <- seq_len(n)
#> j <- object@perm
#> }
#> else {
#> i <- object@perm
#> j <- seq_len(n)
#> }
#> if (is.na(upper)) {
#> if (all(j >= i))
#> return(`attr<-`(TRUE, "kind", "U"))
#> if (all(i <= j))
#> return(`attr<-`(TRUE, "kind", "L"))
#> FALSE
#> }
#> else if (upper) {
#> all(j >= i)
#> }
#> else {
#> all(i <= j)
#> }
#> }
#>
#>
#> object="matrix"
#> function (object, upper = NA, ...)
#> .Call(R_dense_is_triangular, object, upper)
#>
#>
#>
showMethods("isDiagonal", includeDefs = TRUE)
#> Function: isDiagonal (package Matrix)
#> object="CsparseMatrix"
#> function (object)
#> .Call(R_sparse_is_diagonal, object)
#>
#>
#> object="RsparseMatrix"
#> function (object)
#> .Call(R_sparse_is_diagonal, object)
#>
#>
#> object="TsparseMatrix"
#> function (object)
#> .Call(R_sparse_is_diagonal, object)
#>
#>
#> object="denseMatrix"
#> function (object)
#> .Call(R_dense_is_diagonal, object)
#>
#>
#> object="diagonalMatrix"
#> function (object)
#> TRUE
#>
#>
#> object="indMatrix"
#> function (object)
#> {
#> d <- object@Dim
#> if ((n <- d[2L]) != d[1L])
#> return(FALSE)
#> all(object@perm == seq_len(n))
#> }
#>
#>
#> object="matrix"
#> function (object)
#> .Call(R_dense_is_diagonal, object)
#>
#>
#>