Skip to content

Commit 35aabe0

Browse files
committed
new function symmatrix
1 parent c647519 commit 35aabe0

7 files changed

Lines changed: 113 additions & 4 deletions

File tree

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Package: spatstat.sparse
2-
Version: 3.1-0.006
2+
Version: 3.1-0.007
33
Date: 2026-04-23
44
Title: Sparse Three-Dimensional Arrays and Linear Algebra Utilities
55
Authors@R: c(person("Adrian", "Baddeley",

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ export("Summary.sparse3Darray")
6464
export("sumouter")
6565
export("sumsymouter")
6666
export("sumsymouterSparse")
67+
export("symmatrix")
6768
export("tensor1x1")
6869
export("tensorSparse")
6970
export("unionOfSparseIndices")

NEWS

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11

2-
CHANGES IN spatstat.sparse VERSION 3.1-0.006
2+
CHANGES IN spatstat.sparse VERSION 3.1-0.007
33

44
OVERVIEW
55

66
o Simulate a Markov chain with sparse transition matrix.
77

8+
o Recreate a symmetric matrix from its lower triangular entries.
9+
810
NEW FUNCTIONS
911

1012
o runSparseMarkovChain
1113
Generate simulated realisations of a Markov chain
1214
using a sparse matrix representation of the transition probability matrix.
1315

16+
o symmatrix
17+
Recreate a symmetric matrix from a vector containing its lower triangular
18+
(or upper triangular) entries.
19+
1420
CHANGES IN spatstat.sparse VERSION 3.1-0
1521

1622
OVERVIEW

R/matrixpower.R

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,26 @@ matrixpower <- function(x, power, complexOK=TRUE) {
112112
dimnames(y) <- rev(dn)
113113
return(y)
114114
}
115+
116+
symmatrix <- function(x, from=c("lower", "upper"), diag=TRUE) {
117+
from <- match.arg(from)
118+
x <- as.vector(x)
119+
n <- length(x)
120+
if(diag) {
121+
m <- (sqrt(8*n+1)-1)/2
122+
E <- m^2 + m - 2*n
123+
} else {
124+
m <- (sqrt(8*n+1)+1)/2
125+
E <- m^2 - m - 2*n
126+
}
127+
if(E != 0)
128+
stop("x has the wrong length for a triangular subset", call.=FALSE)
129+
M <- matrix(, m, m)
130+
A <- switch(from,
131+
lower = lower.tri(M, diag=diag),
132+
upper = upper.tri(M, diag=diag))
133+
M[ A ] <- x
134+
M[ !A ] <- t(M)[ !A ]
135+
return(M)
136+
}
137+

inst/doc/packagesizes.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ date version nhelpfiles nobjects ndatasets Rlines srclines
1919
"2023-03-12" "3.0-1" 15 48 0 2092 740
2020
"2023-10-24" "3.0-3" 15 48 0 2092 740
2121
"2024-06-21" "3.1-0" 15 48 0 2092 740
22-
"2026-04-23" "3.1-0.006" 16 49 0 2194 943
22+
"2026-04-23" "3.1-0.007" 17 50 0 2217 943

inst/info/packagesizes.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ date version nhelpfiles nobjects ndatasets Rlines srclines
1919
"2023-03-12" "3.0-1" 15 48 0 2092 740
2020
"2023-10-24" "3.0-3" 15 48 0 2092 740
2121
"2024-06-21" "3.1-0" 15 48 0 2092 740
22-
"2026-04-23" "3.1-0.006" 16 49 0 2194 943
22+
"2026-04-23" "3.1-0.007" 17 50 0 2217 943

man/symmatrix.Rd

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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

Comments
 (0)