symmpart.Rd
symmpart(x)
computes the symmetric part (x + t(x))/2
and
skewpart(x)
the
skew symmetric part (x - t(x))/2
of a square matrix x
,
more efficiently for specific Matrix classes.
Note that x == symmpart(x) + skewpart(x)
for all square
matrices – apart from extraneous NA
values in the RHS.
symmpart(x)
skewpart(x)
a square matrix; either “traditional” of class
"matrix"
, or typically, inheriting from the
Matrix
class.
These are generic functions with several methods for different matrix
classes, use e.g., showMethods(symmpart)
to see them.
If the row and column names differ, the result will use the column
names unless they are (partly) NULL
where the row names are
non-NULL
(see also the examples).
symmpart(x)
returns a symmetric matrix,
inheriting from symmetricMatrix
or diagonalMatrix
if x
inherits from Matrix
.
skewpart(x)
returns a skew-symmetric matrix,
inheriting from generalMatrix
,
symmetricMatrix
or
diagonalMatrix
if x
inherits from Matrix
.
m <- Matrix(1:4, 2,2)
symmpart(m)
#> 2 x 2 Matrix of class "dsyMatrix"
#> [,1] [,2]
#> [1,] 1.0 2.5
#> [2,] 2.5 4.0
skewpart(m)
#> 2 x 2 Matrix of class "dgeMatrix"
#> [,1] [,2]
#> [1,] 0.0 0.5
#> [2,] -0.5 0.0
stopifnot(all(m == symmpart(m) + skewpart(m)))
dn <- dimnames(m) <- list(row = c("r1", "r2"), col = c("var.1", "var.2"))
stopifnot(all(m == symmpart(m) + skewpart(m)))
colnames(m) <- NULL
stopifnot(all(m == symmpart(m) + skewpart(m)))
dimnames(m) <- unname(dn)
stopifnot(all(m == symmpart(m) + skewpart(m)))
## investigate the current methods:
showMethods(skewpart, include = TRUE)
#> Function: skewpart (package Matrix)
#> x="CsparseMatrix"
#> function (x)
#> .Call(R_sparse_skewpart, x)
#>
#>
#> x="RsparseMatrix"
#> function (x)
#> .Call(R_sparse_skewpart, x)
#>
#>
#> x="TsparseMatrix"
#> function (x)
#> .Call(R_sparse_skewpart, x)
#>
#>
#> x="denseMatrix"
#> function (x)
#> .Call(R_dense_skewpart, x)
#>
#>
#> x="diagonalMatrix"
#> function (x)
#> {
#> kind <- .M.kind(x)
#> r <- new(if (kind == "z")
#> "zdiMatrix"
#> else "ddiMatrix")
#> r@Dim <- d <- x@Dim
#> r@Dimnames <- symDN(x@Dimnames)
#> r@x <- if (kind == "z") {
#> if (x@diag != "N")
#> complex(d[1L])
#> else complex(real = 0, imaginary = Im(x@x))
#> }
#> else double(d[1L])
#> r
#> }
#>
#>
#> x="indMatrix"
#> function (x)
#> skewpart(.M2kind(x, "d"))
#>
#>
#> x="matrix"
#> function (x)
#> symmetrizeDN(0.5 * (x - t(x)))
#>
#>
#>