band-methods.RdReturn the matrix obtained by setting to zero elements below a diagonal
(triu), above a diagonal (tril), or outside of a general
band (band).
band(x, k1, k2, ...)
triu(x, k = 0L, ...)
tril(x, k = 0L, ...)a matrix-like object
integers specifying the diagonals that are not set to
zero, k1 <= k2. These are interpreted relative to the main
diagonal, which is k = 0.
Positive and negative values of k indicate
diagonals above and below the main diagonal, respectively.
optional arguments passed to methods, currently unused by package Matrix.
triu(x, k) is equivalent to band(x, k, dim(x)[2]).
Similarly,
tril(x, k) is equivalent to band(x, -dim(x)[1], k).
An object of a suitable matrix class, inheriting from
triangularMatrix where appropriate.
It inherits from sparseMatrix if
and only if x does.
method for compressed, sparse, column-oriented matrices.
method for compressed, sparse, row-oriented matrices.
method for sparse matrices in triplet format.
method for diagonal matrices.
method for dense matrices in packed or unpacked format.
method for traditional matrices
of implicit class matrix.
bandSparse for the construction of a
banded sparse matrix directly from its non-zero diagonals.
## A random sparse matrix :
set.seed(7)
m <- matrix(0, 5, 5)
m[sample(length(m), size = 14)] <- rep(1:9, length=14)
(mm <- as(m, "CsparseMatrix"))
#> 5 x 5 sparse Matrix of class "dgCMatrix"
#>
#> [1,] . . 2 . 9
#> [2,] 4 3 4 . .
#> [3,] 7 6 . 1 .
#> [4,] . . . 2 5
#> [5,] . 1 5 8 3
tril(mm) # lower triangle
#> 5 x 5 sparse Matrix of class "dtCMatrix"
#>
#> [1,] . . . . .
#> [2,] 4 3 . . .
#> [3,] 7 6 . . .
#> [4,] . . . 2 .
#> [5,] . 1 5 8 3
tril(mm, -1) # strict lower triangle
#> 5 x 5 sparse Matrix of class "dtCMatrix"
#>
#> [1,] . . . . .
#> [2,] 4 . . . .
#> [3,] 7 6 . . .
#> [4,] . . . . .
#> [5,] . 1 5 8 .
triu(mm, 1) # strict upper triangle
#> 5 x 5 sparse Matrix of class "dtCMatrix"
#>
#> [1,] . . 2 . 9
#> [2,] . . 4 . .
#> [3,] . . . 1 .
#> [4,] . . . . 5
#> [5,] . . . . .
band(mm, -1, 2) # general band
#> 5 x 5 sparse Matrix of class "dgCMatrix"
#>
#> [1,] . . 2 . .
#> [2,] 4 3 4 . .
#> [3,] . 6 . 1 .
#> [4,] . . . 2 5
#> [5,] . . . 8 3
(m5 <- Matrix(rnorm(25), ncol = 5))
#> 5 x 5 Matrix of class "dgeMatrix"
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] -0.53818915 -0.6319189 -0.3003816 0.5830777 0.01100886
#> [2,] 1.31664590 -0.8959369 -0.1591494 0.3259286 0.50627692
#> [3,] -1.50902855 1.0237546 -0.4689997 -0.2875874 0.40997329
#> [4,] -0.02387779 0.8084547 0.9013132 -0.8881565 -0.04562258
#> [5,] -0.35252883 -0.1082370 1.8316257 -0.6010564 -0.19032738
tril(m5) # lower triangle
#> 5 x 5 Matrix of class "dtrMatrix"
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] -0.53818915 . . . .
#> [2,] 1.31664590 -0.89593695 . . .
#> [3,] -1.50902855 1.02375457 -0.46899973 . .
#> [4,] -0.02387779 0.80845475 0.90131316 -0.88815653 .
#> [5,] -0.35252883 -0.10823695 1.83162570 -0.60105640 -0.19032738
tril(m5, -1) # strict lower triangle
#> 5 x 5 Matrix of class "dtrMatrix"
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] 0.00000000 . . . .
#> [2,] 1.31664590 0.00000000 . . .
#> [3,] -1.50902855 1.02375457 0.00000000 . .
#> [4,] -0.02387779 0.80845475 0.90131316 0.00000000 .
#> [5,] -0.35252883 -0.10823695 1.83162570 -0.60105640 0.00000000
triu(m5, 1) # strict upper triangle
#> 5 x 5 Matrix of class "dtrMatrix"
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] 0.00000000 -0.63191893 -0.30038157 0.58307770 0.01100886
#> [2,] . 0.00000000 -0.15914936 0.32592858 0.50627692
#> [3,] . . 0.00000000 -0.28758745 0.40997329
#> [4,] . . . 0.00000000 -0.04562258
#> [5,] . . . . 0.00000000
band(m5, -1, 2) # general band
#> 5 x 5 Matrix of class "dgeMatrix"
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] -0.5381891 -0.6319189 -0.3003816 0.0000000 0.00000000
#> [2,] 1.3166459 -0.8959369 -0.1591494 0.3259286 0.00000000
#> [3,] 0.0000000 1.0237546 -0.4689997 -0.2875874 0.40997329
#> [4,] 0.0000000 0.0000000 0.9013132 -0.8881565 -0.04562258
#> [5,] 0.0000000 0.0000000 0.0000000 -0.6010564 -0.19032738
(m65 <- Matrix(rnorm(30), ncol = 5)) # not square
#> 6 x 5 Matrix of class "dgeMatrix"
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] 0.2564325 0.6478339 0.18137487 -1.7200493 0.96484367
#> [2,] 0.3076799 -1.4121579 -0.37199734 -0.8282303 -0.06740435
#> [3,] 0.1705786 0.7132121 -0.89222280 -0.2632791 -0.76809014
#> [4,] -0.1561042 -0.2101720 -0.03456105 0.6318391 -0.46436630
#> [5,] 1.4958478 -2.3905787 -0.28704112 0.3296523 -0.99336354
#> [6,] 1.1677221 -0.9069923 -0.40203197 0.8040658 -1.23554166
triu(m65) # result not "dtrMatrix" unless square
#> 6 x 5 Matrix of class "dgeMatrix"
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] 0.2564325 0.6478339 0.1813749 -1.7200493 0.96484367
#> [2,] 0.0000000 -1.4121579 -0.3719973 -0.8282303 -0.06740435
#> [3,] 0.0000000 0.0000000 -0.8922228 -0.2632791 -0.76809014
#> [4,] 0.0000000 0.0000000 0.0000000 0.6318391 -0.46436630
#> [5,] 0.0000000 0.0000000 0.0000000 0.0000000 -0.99336354
#> [6,] 0.0000000 0.0000000 0.0000000 0.0000000 0.00000000
(sm5 <- crossprod(m65)) # symmetric
#> 5 x 5 Matrix of class "dpoMatrix"
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] 3.8150258 -4.748956 -1.1135761 0.5925862 -2.760542
#> [2,] -4.7489565 9.504224 1.0645738 -1.7826242 3.765370
#> [3,] -1.1135761 1.064574 1.2125572 -0.2086916 1.683293
#> [4,] 0.5925862 -1.782624 -0.2086916 4.8682638 -3.015856
#> [5,] -2.7605419 3.765370 1.6832926 -3.0158564 4.254399
band(sm5, -1, 1)# "dsyMatrix": symmetric band preserves symmetry property
#> 5 x 5 Matrix of class "dsyMatrix"
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] 3.815026 -4.748956 0.0000000 0.0000000 0.000000
#> [2,] -4.748956 9.504224 1.0645738 0.0000000 0.000000
#> [3,] 0.000000 1.064574 1.2125572 -0.2086916 0.000000
#> [4,] 0.000000 0.000000 -0.2086916 4.8682638 -3.015856
#> [5,] 0.000000 0.000000 0.0000000 -3.0158564 4.254399
as(band(sm5, -1, 1), "sparseMatrix")# often preferable
#> 5 x 5 sparse Matrix of class "dsCMatrix"
#>
#> [1,] 3.815026 -4.748956 . . .
#> [2,] -4.748956 9.504224 1.0645738 . .
#> [3,] . 1.064574 1.2125572 -0.2086916 .
#> [4,] . . -0.2086916 4.8682638 -3.015856
#> [5,] . . . -3.0158564 4.254399
(sm <- round(crossprod(triu(mm/2)))) # sparse symmetric ("dsC*")
#> 5 x 5 sparse Matrix of class "dsCMatrix"
#>
#> [1,] . . . . .
#> [2,] . 2 3 . .
#> [3,] . 3 5 . 4
#> [4,] . . . 1 2
#> [5,] . . 4 2 29
band(sm, -1,1) # remains "dsC", *however*
#> 5 x 5 sparse Matrix of class "dsCMatrix"
#>
#> [1,] . . . . .
#> [2,] . 2 3 . .
#> [3,] . 3 5 . .
#> [4,] . . . 1 2
#> [5,] . . . 2 29
band(sm, -2,1) # -> "dgC"
#> 5 x 5 sparse Matrix of class "dgCMatrix"
#>
#> [1,] . . . . .
#> [2,] . 2 3 . .
#> [3,] . 3 5 . .
#> [4,] . . . 1 2
#> [5,] . . 4 2 29