forceSymmetric: Force a Matrix to 'symmetricMatrix' Without Symmetry Checks

Description Usage Arguments Value See Also Examples

Description

Force a square matrix x to a symmetricMatrix, without a symmetry check as it would be applied for as(x, "symmetricMatrix").

Usage

1
forceSymmetric(x, uplo)

Arguments

x

any square matrix (of numbers), either “"traditional"” (matrix) or inheriting from Matrix.

uplo

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.

Value

a square matrix inheriting from class symmetricMatrix.

See Also

symmpart for the symmetric part of a matrix, or the coercions as(x, <symmetricMatrix class>).

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
 ## 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)

Example output

[1] FALSE
Error in asMethod(object) : matrix is not symmetric [2,4]
6 x 6 Matrix of class "dgeMatrix"
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    1    7   13   19   25   31
[2,]    2    8   14   20   26   32
[3,]    3    9   15   21   27   33
[4,]    4   10   16   22   28   34
[5,]    5   11   17   23   29   35
[6,]    6   12   18   24   30   36
6 x 6 Matrix of class "dsyMatrix"
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    1    7   13   19   25   31
[2,]    7    8   14   20   26   32
[3,]   13   14   15   21   27   33
[4,]   19   20   21   22   28   34
[5,]   25   26   27   28   29   35
[6,]   31   32   33   34   35   36
6 x 6 Matrix of class "dtrMatrix"
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    1    .    .    .    .    .
[2,]    2    8    .    .    .    .
[3,]    3    9   15    .    .    .
[4,]    4   10   16   22    .    .
[5,]    5   11   17   23   29    .
[6,]    6   12   18   24   30   36
6 x 6 Matrix of class "dsyMatrix"
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    1    2    3    4    5    6
[2,]    2    8    9   10   11   12
[3,]    3    9   15   16   17   18
[4,]    4   10   16   22   23   24
[5,]    5   11   17   23   29   30
[6,]    6   12   18   24   30   36

Matrix documentation built on June 11, 2021, 3 p.m.