Skip to content

Commit 7c601ff

Browse files
committed
Bug fixes in trivial cases of sumsymouterSparse
1 parent 35aabe0 commit 7c601ff

13 files changed

Lines changed: 120 additions & 24 deletions

DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: spatstat.sparse
2-
Version: 3.1-0.007
3-
Date: 2026-04-23
2+
Version: 3.1-0.009
3+
Date: 2026-04-24
44
Title: Sparse Three-Dimensional Arrays and Linear Algebra Utilities
55
Authors@R: c(person("Adrian", "Baddeley",
66
role = c("aut", "cre", "cph"),

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export("evalSparse3Dentrywise")
4242
export("expandSparse")
4343
export("gridadjacencymatrix")
4444
export("inside3Darray")
45+
export("length.sparse3Darray")
4546
export("mapSparseEntries")
4647
export("marginSumsSparse")
4748
export("Math.sparse3Darray")
@@ -84,6 +85,7 @@ S3method("Summary", "sparse3Darray")
8485
S3method("as.array", "sparse3Darray")
8586
S3method("dimnames", "sparse3Darray")
8687
S3method("dim", "sparse3Darray")
88+
S3method("length", "sparse3Darray")
8789
S3method("print", "sparse3Darray")
8890
S3method("[", "sparse3Darray")
8991
# .........................................

NEWS

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

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

44
OVERVIEW
55

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

88
o Recreate a symmetric matrix from its lower triangular entries.
99

10+
o Weighted sum of outer products in a 3D array or sparse array.
11+
12+
o Minor improvements.
13+
1014
NEW FUNCTIONS
1115

1216
o runSparseMarkovChain
@@ -17,6 +21,12 @@ NEW FUNCTIONS
1721
Recreate a symmetric matrix from a vector containing its lower triangular
1822
(or upper triangular) entries.
1923

24+
o length.sparse3Darray
25+
Method for 'length' for sparse 3D arrays.
26+
27+
o sumsymouter
28+
Weighted sum of outer products in a 3D array or sparse array
29+
2030
CHANGES IN spatstat.sparse VERSION 3.1-0
2131

2232
OVERVIEW

R/linalg.R

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#' Copyright (c) Adrian Baddeley, Ege Rubak and Rolf Turner 2016-2020
77
#' GNU Public Licence >= 2.0
88
#'
9-
#' $Revision: 1.38 $ $Date: 2022/05/23 02:33:06 $
9+
#' $Revision: 1.39 $ $Date: 2026/04/24 05:57:32 $
1010
#'
1111

1212
sumouter <- function(x, w=NULL, y=x) {
@@ -239,8 +239,8 @@ bilinearform <- function(x, v, y) {
239239
}
240240

241241
sumsymouter <- function(x, w=NULL, distinct=TRUE) {
242-
## x is a 3D array
243-
## w is a matrix
242+
## x is a 3D array or sparse 3D array
243+
## w is a matrix or sparse matrix
244244
## Computes the sum of outer(x[,i,j], x[,j,i]) * w[i,j] over all pairs i != j
245245
## handle complex values
246246
if(is.complex(w)) {
@@ -256,20 +256,25 @@ sumsymouter <- function(x, w=NULL, distinct=TRUE) {
256256
result <- a - b + (d - a - b) * 1i
257257
return(result)
258258
}
259-
## handle sparse arrays
260-
if(inherits(x, c("sparseSlab", "sparse3Darray")) &&
261-
(is.null(w) || inherits(w, "sparseMatrix")))
262-
return(sumsymouterSparse(x, w, distinct=distinct))
263259
## arguments are numeric
264-
x <- as.array(x)
260+
## validate dimensions
265261
stopifnot(length(dim(x)) == 3)
266262
if(dim(x)[2L] != dim(x)[3L])
267263
stop("The second and third dimensions of x should be equal")
268264
if(!is.null(w)) {
269-
w <- as.matrix(w)
265+
stopifnot(length(dim(w)) == 2)
270266
if(!all(dim(w) == dim(x)[-1L]))
271267
stop("Dimensions of w should match the second and third dimensions of x")
272268
}
269+
## handle sparse arrays
270+
if(inherits(x, c("sparseSlab", "sparse3Darray"))) {
271+
if(!is.null(w))
272+
w <- as(w, "sparseMatrix")
273+
return(sumsymouterSparse(x, w, distinct=distinct))
274+
}
275+
x <- as.array(x)
276+
if(!is.null(w))
277+
w <- as.matrix(w)
273278
p <- dim(x)[1L]
274279
n <- dim(x)[2L]
275280
if(!distinct) {

R/sparse3Darray.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ as.sparse3Darray <- function(x, ...) {
126126
return(y)
127127
}
128128

129+
length.sparse3Darray <- function(x) { prod(dim(x)) }
130+
129131
dim.sparse3Darray <- function(x) { x$dim }
130132

131133
"dim<-.sparse3Darray" <- function(x, value) {

R/sparselinalg.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#' Copyright (c) Adrian Baddeley, Ege Rubak and Rolf Turner 2016-2020
77
#' GNU Public Licence >= 2.0
88
#'
9-
#' $Revision: 1.19 $ $Date: 2021/01/08 01:16:41 $
9+
#' $Revision: 1.21 $ $Date: 2026/04/24 05:22:07 $
1010

1111
marginSumsSparse <- function(X, MARGIN) {
1212
#' equivalent to apply(X, MARGIN, sum)
@@ -221,17 +221,17 @@ sumsymouterSparse <- function(x, w=NULL, distinct=TRUE, dbg=FALSE) {
221221
if(distinct) {
222222
#' remove entries with j = k
223223
ok <- with(df, j != k)
224-
df <- df[ok, , drop=TRUE]
224+
df <- df[ok, , drop=FALSE]
225225
}
226226
## trivial?
227-
if(nrow(df) < 2) {
227+
if(nrow(df) == 0) {
228228
y <- matrix(0, m, m)
229229
dimnames(y) <- rep(dimnames(x)[1], 2)
230230
return(y)
231231
}
232232
## order by increasing j, then k
233233
oo <- with(df, order(j, k, i))
234-
df <- df[oo, ]
234+
df <- df[oo, , drop=FALSE]
235235
## now provide ordering by increasing k then j
236236
ff <- with(df, order(k,j,i))
237237
##

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.007" 17 50 0 2217 943
22+
"2026-04-24" "3.1-0.009" 18 51 0 2224 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.007" 17 50 0 2217 943
22+
"2026-04-24" "3.1-0.009" 18 51 0 2224 943

man/methods.sparse3Darray.Rd

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
\alias{dim<-.sparse3Darray}
77
\alias{dimnames.sparse3Darray}
88
\alias{dimnames<-.sparse3Darray}
9+
\alias{length.sparse3Darray}
910
\alias{print.sparse3Darray}
1011
\title{
1112
Methods for Sparse Three-Dimensional Arrays
@@ -20,6 +21,7 @@
2021
\method{dim}{sparse3Darray}(x) <- value
2122
\method{dimnames}{sparse3Darray}(x)
2223
\method{dimnames}{sparse3Darray}(x) <- value
24+
\method{length}{sparse3Darray}(x)
2325
\method{print}{sparse3Darray}(x, \dots)
2426
}
2527
\arguments{
@@ -40,8 +42,9 @@
4042
\code{\link[base]{dim<-}},
4143
\code{\link[base]{dimnames}},
4244
\code{\link[base]{dimnames<-}}
45+
\code{\link[base]{length}}
4346
and \code{\link[base]{print}}
44-
for the class \code{"sparse#Darray"} of sparse three-dimensional
47+
for the class \code{"sparse3Darray"} of sparse three-dimensional
4548
arrays.
4649

4750
For \code{dimnames(x) <- value}, the \code{value} should either be
@@ -52,7 +55,10 @@
5255
vector of length 3 giving the new dimensions of the array. Note that this
5356
operation does not change the array positions of the non-zero entries
5457
(unlike \code{dim(x) <- value} for a full array). An error occurs if
55-
some of the non-zero entries would lie outside the new extent of the array.
58+
some of the non-zero entries would lie outside the new extent of the
59+
array.
60+
61+
The \code{length} of a sparse array is the product of its dimensions.
5662
}
5763
\value{
5864
\code{anyNA} returns a single logical value.
@@ -64,6 +70,8 @@
6470

6571
\code{dim<-} and \code{dimnames<-} return a sparse 3D array.
6672

73+
\code{length} returns an integer.
74+
6775
\code{print} returns \code{NULL}, invisibly.
6876
}
6977
\author{

man/spatstat.sparse-internal.Rd

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
%%%% undocumented linear algebra
77
\alias{checksolve}
88
\alias{check.mat.mul}
9-
\alias{sumsymouter}
109
%% sparse 3D arrays
1110
\alias{unionOfSparseIndices}
1211
\alias{inside3Darray}
@@ -31,7 +30,6 @@ representativeRows(x)
3130
%%% undocumented linear algebra
3231
checksolve(M, action, descrip, target)
3332
check.mat.mul(A, B, Acols, Brows, fatal)
34-
sumsymouter(x, w, distinct)
3533
%% sparse 3D arrays
3634
unionOfSparseIndices(A,B)
3735
inside3Darray(d, i, j, k)

0 commit comments

Comments
 (0)