Test if a Matrix or other Object is Symmetric (Hermitian) (original) (raw)

isSymmetric {base} R Documentation

Description

Generic function to test if object is symmetric or not. Currently only a matrix method is implemented, where a[complex](../../base/help/complex.html) matrix Z must be “Hermitian” forisSymmetric(Z) to be true, and (since R >= 4.5.0),isSymmetric(Z, trans = "T") checks for “simple” symmetry.

Usage

isSymmetric(object, ...)
## S3 method for class 'matrix'
isSymmetric(object, tol = 100 * .Machine$double.eps,
            tol1 = 8 * tol, trans = "C", ...)

Arguments

object any R object; a matrix for the matrix method.
tol numeric scalar >= 0. Smaller differences are not considered, see all.equal.numeric.
tol1 numeric scalar >= 0. isSymmetric.matrix()‘pre-tests’ the first and last few rows for fast detection of ‘obviously’ asymmetric cases with this tolerance. Setting it to length zero will skip the pre-tests.
trans a single character, only relevant for acomplex matrix Z: if it is "C" (as by default), Conj(t(Z)) must be the same as Z whereas otherwise (typically it is "T") t(Z) must equalZ. The argument name is inherited from LAPACK.
... further arguments passed to methods; the matrix method passes these to all.equal. If the row and column names of object are allowed to differ for the symmetry check do use check.attributes = FALSE!

Details

The [matrix](../../base/help/matrix.html) method is used inside [eigen](../../base/help/eigen.html) by default to test symmetry of matrices up to rounding error, using[all.equal](../../base/help/all.equal.html). It might not be appropriate in all situations.

Note that a matrix m is only symmetric if its rownames andcolnames are identical. Consider using [unname](../../base/help/unname.html)(m).

Value

logical indicating if object is symmetric or not.

See Also

[eigen](../../base/help/eigen.html) which calls isSymmetric when itssymmetric argument is missing.

Examples

isSymmetric(D3 <- diag(3)) # -> TRUE

D3[2, 1] <- 1e-100
D3
isSymmetric(D3) # TRUE
isSymmetric(D3, tol = 0) # FALSE for zero-tolerance

## Complex Matrices - Hermitian or not
z <- sqrt(matrix(-1:2 + 0i, 2)); Z <- t(Conj(z)) %*% z
ZtZ <- t(z) %*% z
Z ; ZtZ
isSymmetric(Z)      # TRUE
isSymmetric(Z + 1)  # TRUE
isSymmetric(Z + 1i) # FALSE -- a Hermitian matrix has a *real* diagonal

colnames(D3) <- c("X", "Y", "Z")
isSymmetric(D3)                         # FALSE (as row and column names differ)
isSymmetric(D3, check.attributes=FALSE) # TRUE  (as names are not checked)

[Package _base_ version 4.6.0 Index]