[ RDocumentation ](/)

Moon  [ ](https://github.com/datacamp/rdocumentation-2.0) [ Learn R
](https://www.datacamp.com/learn/r)

Search all packages and functions

[ base  (version  3.6.2  ) ](/packages/base/versions/3.6.2)

#  isSymmetric: Test if a Matrix or other Object is Symmetric (Hermitian)

##  Description

Generic function to test if ` object ` is symmetric or not. Currently only a
matrix method is implemented, where a ` [ complex
](/link/complex?package=base&version=3.6.2) ` matrix ` Z ` must be “Hermitian”
for ` isSymmetric(Z) ` to be true.

##  Usage

    
    
    isSymmetric(object, …)
    # S3 method for matrix
    isSymmetric(object, tol = 100 * .Machine$double.eps,
                tol1 = 8 * tol, …)

##  Arguments

object

any  R  object; a ` [ matrix ](/link/matrix?package=base&version=3.6.2) ` for
the matrix method.

tol

numeric scalar >= 0. Smaller differences are not considered, see ` [
all.equal.numeric ](/link/all.equal.numeric?package=base&version=3.6.2) ` .

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.

…

further arguments passed to methods; the matrix method passes these to ` [
all.equal ](/link/all.equal?package=base&version=3.6.2) ` . If the row and
column names of ` object ` are allowed to differ for the symmetry check do use
` check.attributes = FALSE ` !

##  Value

logical indicating if ` object ` is symmetric or not.

##  Details

The ` [ matrix ](/link/matrix?package=base&version=3.6.2) ` method is used
inside ` [ eigen ](/link/eigen?package=base&version=3.6.2) ` by default to
test symmetry of matrices _up to rounding error_ , using ` [ all.equal
](/link/all.equal?package=base&version=3.6.2) ` . It might not be appropriate
in all situations.

Note that a matrix ` m ` is only symmetric if its ` rownames ` and ` colnames
` are identical. Consider using ` [ unname
](/link/unname?package=base&version=3.6.2) (m) ` .

##  See Also

` [ eigen ](/link/eigen?package=base&version=3.6.2) ` which calls `
isSymmetric ` when its ` symmetric ` argument is missing.

##  Examples

[ Run this code
](https://app.datacamp.com/workspace/preview?_tag=rdocs&rdocsPath=packages%2Fbase%2Fversions%2F3.6.2%2Ftopics%2FisSymmetric&utm_source=r-docs&utm_medium=docs&utm_term=isSymmetric&utm_content=run_example_in_workspace)

    
    
    # NOT RUN {
    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
    Z
    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)
    # }
    

Run the code above in your browser using  [ DataCamp Workspace
](https://app.datacamp.com/workspace/preview?_tag=rdocs&rdocsPath=packages%2Fbase%2Fversions%2F3.6.2%2Ftopics%2FisSymmetric&utm_source=r-docs&utm_medium=docs&utm_term=isSymmetric&utm_content=run_example_in_workspace)

Powered by  [ DataCamp ](https://www.datacamp.com/)