|
| 1 | +\name{symmatrix} |
| 2 | +\alias{symmatrix} |
| 3 | +\title{ |
| 4 | + Create a Symmetric Matrix from a Triangular Subset |
| 5 | +} |
| 6 | +\description{ |
| 7 | + Given a vector containing values for the upper or lower triangular |
| 8 | + entries of a matrix, reconstruct the original matrix, assuming it |
| 9 | + is symmetric. |
| 10 | +} |
| 11 | +\usage{ |
| 12 | +symmatrix(x, from = c("lower", "upper"), diag = TRUE) |
| 13 | +} |
| 14 | +\arguments{ |
| 15 | + \item{x}{ |
| 16 | + A vector of atomic values. |
| 17 | + } |
| 18 | + \item{from}{ |
| 19 | + Character string (partially matched) specifying whether the |
| 20 | + entries of \code{x} represent the upper or lower triangular |
| 21 | + entries of the matrix. |
| 22 | + } |
| 23 | + \item{diag}{ |
| 24 | + Logical value specifying whether \code{x} includes diagonal entries |
| 25 | + of the matrix. |
| 26 | + } |
| 27 | +} |
| 28 | +\details{ |
| 29 | + This function reconstructs a symmetric matrix from its |
| 30 | + lower triangular entries, or from its upper triangular entries. |
| 31 | + \itemize{ |
| 32 | + \item |
| 33 | + If \code{from="lower"} (the default), the vector \code{x} is assumed |
| 34 | + to contain the lower-triangular entries of a matrix \code{M} (that is, the |
| 35 | + entries \code{M[i,j]} in row \eqn{i} and column \eqn{j} where |
| 36 | + \eqn{i \ge j}{i >= j}) |
| 37 | + listed in column-major order (that is, all of column 1 first, then all |
| 38 | + the relevant entries in column 2, etc.). This vector could have been |
| 39 | + obtained from the matrix by \code{x <- M[lower.tri(M, diag)]}. |
| 40 | + \item |
| 41 | + If \code{from="upper"}, the vector \code{x} is assumed |
| 42 | + to contain the upper-triangular entries (those for which |
| 43 | + \eqn{i \le j}{i <= j}) listed in column-major order. |
| 44 | + This vector could have been |
| 45 | + obtained from the matrix by \code{x <- M[upper.tri(M, diag)]}. |
| 46 | + } |
| 47 | + Assuming \code{M} was a |
| 48 | + symmetric matrix, it will be reconstructed from these values. |
| 49 | + The reconstructed matrix is returned. |
| 50 | + |
| 51 | + If \code{diag} is FALSE, then the diagonal entries of the matrix |
| 52 | + are assumed to have been omitted, and the |
| 53 | + resulting matrix will contain \code{NA} entries in the diagonal. |
| 54 | +} |
| 55 | +\value{ |
| 56 | + A symmetric square matrix, containing values of the same atomic type |
| 57 | + as \code{x}. |
| 58 | +} |
| 59 | +\author{ |
| 60 | + \adrian. |
| 61 | +} |
| 62 | +\seealso{ |
| 63 | + \code{\link[base]{lower.tri}} |
| 64 | +} |
| 65 | +\examples{ |
| 66 | + M <- matrix(c(10, 2, 3, 4, |
| 67 | + 2, 20, 5, 6, |
| 68 | + 3, 5, 30, 7, |
| 69 | + 4, 6, 7, 40), 4, 4) |
| 70 | + M |
| 71 | + |
| 72 | + (x <- M[lower.tri(M, diag=TRUE)]) |
| 73 | + symmatrix(x) |
| 74 | + |
| 75 | + (y <- M[upper.tri(M, diag=FALSE)]) |
| 76 | + symmatrix(y, from="upper", diag=FALSE) |
| 77 | +} |
| 78 | +\keyword{manip} |
| 79 | +\keyword{array} |
0 commit comments