Skip to contents

Define a graph of the union of the supplied matrices and return the row ordered diagonal plus upper triangle after padding with zeroes each one so that all the returned matrices have the same pattern.

Usage

upperPadding(M, relative = FALSE)

Arguments

M

a matrix or a list of matrices

relative

logical. If a list of matrices is supplied, it indicates if it is to be returned a relative index and the value for each matrix. See details.

Value

If a unique matrix is given, return the upper triangle considering the 'T' representation in the Matrix package. If a list of matrices is given, return a list of two elements: 'graph' and 'xx'. The 'graph' is the union of the graph from each matrix. If relative=FALSE, 'xx' is a matrix with number of column equals the the number of matrices imputed. If relative=TRUE, it is a list of length equal the number of matrices imputed. See details.

Details

If relative=FALSE, each columns of 'xx' is the elements of the corresponding matrix after being padded to fill the pattern of the union graph. If relative=TRUE, each element of 'xx' would be a list with a relative index, 'r', for each non-zero elements of each matrix is returned relative to the union graph, the non-lower elements, 'x', of the corresponding matrix, and a vector, 'o', with the number of non-zero elements for each line of each resulting matrix.

Examples

A <- sparseMatrix(
  i = c(1, 1, 2, 3, 3, 5),
  j = c(2, 5, 3, 4, 5, 5),
  x = -(0:5), symmetric = TRUE
)
A
#> 5 x 5 sparse Matrix of class "dsCMatrix"
#>                    
#> [1,]  .  0  .  . -1
#> [2,]  0  . -2  .  .
#> [3,]  . -2  . -3 -4
#> [4,]  .  . -3  .  .
#> [5,] -1  . -4  . -5
upperPadding(A)
#> 5 x 5 sparse Matrix of class "dgTMatrix"
#>                  
#> [1,] . .  .  . -1
#> [2,] . . -2  .  .
#> [3,] . .  . -3 -4
#> [4,] . .  .  .  .
#> [5,] . .  .  . -5
B <- Diagonal(nrow(A), -colSums(A))
list(a = A, a = B)
#> $a
#> 5 x 5 sparse Matrix of class "dsCMatrix"
#>                    
#> [1,]  .  0  .  . -1
#> [2,]  0  . -2  .  .
#> [3,]  . -2  . -3 -4
#> [4,]  .  . -3  .  .
#> [5,] -1  . -4  . -5
#> 
#> $a
#> 5 x 5 diagonal matrix of class "ddiMatrix"
#>      [,1] [,2] [,3] [,4] [,5]
#> [1,]    1    .    .    .    .
#> [2,]    .    2    .    .    .
#> [3,]    .    .    9    .    .
#> [4,]    .    .    .    3    .
#> [5,]    .    .    .    .   10
#> 
upperPadding(list(a = A, b = B))
#> $graph
#> 5 x 5 sparse Matrix of class "dgTMatrix"
#>               
#> [1,] 1 . . . 1
#> [2,] . 1 1 . .
#> [3,] . . 1 1 1
#> [4,] . . . 1 .
#> [5,] . . . . 1
#> 
#> $xx
#>        a  b
#>  [1,]  0  1
#>  [2,] -1  0
#>  [3,]  0  2
#>  [4,] -2  0
#>  [5,]  0  9
#>  [6,] -3  0
#>  [7,] -4  0
#>  [8,]  0  3
#>  [9,] -5 10
#> 
upperPadding(list(a = A, b = B), relative = TRUE)
#> $graph
#> 5 x 5 sparse Matrix of class "dgTMatrix"
#>               
#> [1,] 1 . . . 1
#> [2,] . 1 1 . .
#> [3,] . . 1 1 1
#> [4,] . . . 1 .
#> [5,] . . . . 1
#> 
#> $xx
#> $xx$a
#> $xx$a$r
#> [1] 2 4 6 7 9
#> 
#> $xx$a$x
#> [1] -1 -2 -3 -4 -5
#> 
#> 
#> $xx$b
#> $xx$b$r
#> [1] 1 3 5 8 9
#> 
#> $xx$b$x
#> [1]  1  2  9  3 10
#> 
#> 
#>