forceSymmetric-methods: Symmetrize a Square Matrix

forceSymmetric-methodsR Documentation

Symmetrize a Square Matrix

Description

Returns the symmetric matrix that results from reflecting one triangle of a square matrix onto the other.

Usage

forceSymmetric(x, ...)

## S4 method for signature 'denseMatrix'
forceSymmetric(x, uplo = NULL, trans = "C", ...)
## S4 method for signature 'CsparseMatrix'
forceSymmetric(x, uplo = NULL, trans = "C", ...)
## S4 method for signature 'RsparseMatrix'
forceSymmetric(x, uplo = NULL, trans = "C", ...)
## S4 method for signature 'TsparseMatrix'
forceSymmetric(x, uplo = NULL, trans = "C", ...)
## S4 method for signature 'diagonalMatrix'
forceSymmetric(x, uplo = "U", trans = "C", ...)
## S4 method for signature 'indMatrix'
forceSymmetric(x, ...)
## S4 method for signature 'matrix'
forceSymmetric(x, uplo = "U", trans = "C", ...)

Arguments

x

a square matrix or Matrix.

uplo

a character string, "U" or "L", indicating which of the upper and lower triangles should be reflected. Where it is the default, NULL is accepted and equivalent to "U", unless x has an uplo slot, in which case that is used instead.

trans

a character string, "C" or "T", indicating either the conjugate transpose or transpose, i.e., whether the result is formally Hermitian or symmetric. It matters only in the complex case. The default is "C", for consistency with methods for isSymmetric.

...

optional arguments passed from or to other methods.

Details

forceSymmetric(x) can be understood as a coercion to virtual class symmetricMatrix. However, unlike as(x, "symmetricMatrix"), methods for forceSymmetric do not require that x is already approximately symmetric.

Value

An object inheriting from virtual class symmetricMatrix, representing a Hermitian or symmetric matrix.

See Also

isSymmetric, symmpart, skewpart.

Examples

 ## 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)

Matrix documentation built on Aug. 13, 2024, 3:01 p.m.