
Last chance! 50% off unlimited learning
Sale ends in
Force a square matrix x
to a symmetricMatrix
,
without a symmetry check as it would be applied for as(x,
"symmetricMatrix")
.
forceSymmetric(x, uplo)
a square matrix inheriting from class
symmetricMatrix
.
any square matrix (of numbers), either “"traditional"”
(matrix
) or inheriting from
Matrix
.
optional string, "U"
or "L"
indicating which
“triangle” half of x
should determine the result. The
default is "U"
unless x
already has a uplo
slot
(i.e., when it is symmetricMatrix
, or
triangularMatrix
), where the default will be
x@uplo
.
symmpart
for the symmetric part of a matrix, or
the coercions as(x, <symmetricMatrix class>)
.
## Hilbert matrix
i <- 1:6
h6 <- 1/outer(i - 1L, i, "+")
sd <- sqrt(diag(h6))
hh <- t(h6/sd)/sd # theoretically symmetric
isSymmetric(hh, tol=0) # FALSE; hence
try( as(hh, "symmetricMatrix") ) # fails, but this works fine:
H6 <- forceSymmetric(hh)
## result can be pretty surprising:
(M <- Matrix(1:36, 6))
forceSymmetric(M) # symmetric, hence very different in lower triangle
(tm <- tril(M))
forceSymmetric(tm)
Run the code above in your browser using DataLab