diagU2N.RdTransform a triangular matrix x, i.e., of class
triangularMatrix,
from (internally!) unit triangular (“unitriangular”) to
“general” triangular (diagU2N(x)) or back (diagN2U(x)).
Note that the latter, diagN2U(x), also sets the diagonal to one
in cases where diag(x) was not all one.
.diagU2N(x) and .diagN2U(x) assume without
checking that x is a triangularMatrix with
suitable diag slot ("U" and "N", respectively),
hence they should be used with care.
a triangularMatrix, often sparse.
(optional, for speedup only:) class (definition) of x.
logical indicating if dense (see
denseMatrix) matrices should be considered at
all; i.e., when false, as per default, the result will be sparse even
when x is dense.
The concept of unit triangular matrices with a diag slot of
"U" stems from LAPACK.
Such internal storage details should rarely be of relevance to the user. Hence, these functions really are rather internal utilities.
a triangular matrix of the same class but with a
different diag slot. For diagU2N (semantically) with
identical entries as x, whereas in diagN2U(x), the
off-diagonal entries are unchanged and the diagonal is set to all
1 even if it was not previously.
"triangularMatrix",
"dtCMatrix".
(T <- Diagonal(7) + triu(Matrix(rpois(49, 1/4), 7, 7), k = 1))
#> 7 x 7 sparse Matrix of class "dtCMatrix"
#>
#> [1,] 1 . . . 1 . .
#> [2,] . 1 . . . . 1
#> [3,] . . 1 . . 1 1
#> [4,] . . . 1 . . .
#> [5,] . . . . 1 1 .
#> [6,] . . . . . 1 1
#> [7,] . . . . . . 1
(uT <- diagN2U(T)) # "unitriangular"
#> 7 x 7 sparse Matrix of class "dtCMatrix" (unitriangular)
#>
#> [1,] I . . . 1 . .
#> [2,] . I . . . . 1
#> [3,] . . I . . 1 1
#> [4,] . . . I . . .
#> [5,] . . . . I 1 .
#> [6,] . . . . . I 1
#> [7,] . . . . . . I
(t.u <- diagN2U(10*T))# changes the diagonal!
#> 7 x 7 sparse Matrix of class "dtCMatrix" (unitriangular)
#>
#> [1,] I . . . 10 . .
#> [2,] . I . . . . 10
#> [3,] . . I . . 10 10
#> [4,] . . . I . . .
#> [5,] . . . . I 10 .
#> [6,] . . . . . I 10
#> [7,] . . . . . . I
stopifnot(all(T == uT), diag(t.u) == 1,
identical(T, diagU2N(uT)))
T[upper.tri(T)] <- 5 # still "dtC"
T <- diagN2U(as(T,"triangularMatrix"))
dT <- as(T, "denseMatrix") # (unitriangular)
dT.n <- diagU2N(dT, checkDense = TRUE)
sT.n <- diagU2N(dT)
stopifnot(is(dT.n, "denseMatrix"), is(sT.n, "sparseMatrix"),
dT@diag == "U", dT.n@diag == "N", sT.n@diag == "N",
all(dT == dT.n), all(dT == sT.n))