Skip to content

Commit 39baf8f

Browse files
committed
Bug fixes in new code
1 parent 3f6f594 commit 39baf8f

10 files changed

Lines changed: 73 additions & 24 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.010
2+
Version: 3.1-0.011
33
Date: 2026-04-27
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
@@ -42,6 +42,7 @@ export("evalSparse3Dentrywise")
4242
export("expandSparse")
4343
export("gridadjacencymatrix")
4444
export("inside3Darray")
45+
export("isComplex")
4546
export("length.sparse3Darray")
4647
export("mapSparseEntries")
4748
export("marginSumsSparse")

NEWS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
CHANGES IN spatstat.sparse VERSION 3.1-0.010
2+
CHANGES IN spatstat.sparse VERSION 3.1-0.011
33

44
OVERVIEW
55

R/linalg.R

Lines changed: 3 additions & 3 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.39 $ $Date: 2026/04/24 05:57:32 $
9+
#' $Revision: 1.40 $ $Date: 2026/04/27 07:36:07 $
1010
#'
1111

1212
sumouter <- function(x, w=NULL, y=x) {
@@ -243,13 +243,13 @@ sumsymouter <- function(x, w=NULL, distinct=TRUE) {
243243
## 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
246-
if(is.complex(w)) {
246+
if(isComplex(w)) {
247247
a <- sumsymouter(x, Re(w), distinct=distinct)
248248
b <- sumsymouter(x, Im(w), distinct=distinct)
249249
result <- a + b * 1i
250250
return(result)
251251
}
252-
if(is.complex(x)) {
252+
if(isComplex(x)) {
253253
a <- sumsymouter(Re(x), w=w, distinct=distinct)
254254
b <- sumsymouter(Im(x), w=w, distinct=distinct)
255255
d <- sumsymouter(Re(x)+Im(x), w=w, distinct=distinct)

R/sparse3Darray.R

Lines changed: 20 additions & 1 deletion
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.47 $ $Date: 2026/04/27 02:38:37 $
9+
#' $Revision: 1.49 $ $Date: 2026/04/27 07:47:02 $
1010
#'
1111

1212
sparse3Darray <- function(i=integer(0), j=integer(0), k=integer(0),
@@ -864,6 +864,25 @@ Summary.sparse3Darray <- function(..., na.rm=FALSE) {
864864
}
865865

866866

867+
isComplex <- function(x) {
868+
if(is.null(x)) return(FALSE)
869+
if(is.vector(x) || is.matrix(x) || is.array(x))
870+
return(is.complex(x))
871+
nd <- length(dim(x))
872+
if(nd == 0 || nd == 1) {
873+
x <- as(x, "sparseVector")
874+
return(is.complex(x@x))
875+
} else if(nd == 2) {
876+
x <- as(x, "TsparseMatrix")
877+
return(is.complex(x@x)) ## currently not supported by Matrix package
878+
} else if(nd == 3) {
879+
x <- as.sparse3Darray(x)
880+
return(is.complex(x$x))
881+
} else
882+
stop("Arrays of more than 3 dimensions are not supported", call.=FALSE)
883+
}
884+
885+
867886
SparseIndices <- function(x) {
868887
#' extract indices of entries of sparse vector/matrix/array
869888
nd <- length(dim(x))

R/sparselinalg.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ sumsymouterSparse <- function(x, w=NULL, distinct=TRUE, dbg=FALSE) {
202202
## result <- a + b * 1i
203203
## return(result)
204204
## }
205-
if(is.complex(x)) {
205+
if(isComplex(x)) {
206206
a <- sumsymouter(Re(x), w=w, distinct=distinct)
207207
b <- sumsymouter(Im(x), w=w, distinct=distinct)
208208
d <- sumsymouter(Re(x)+Im(x), w=w, distinct=distinct)

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-27" "3.1-0.010" 18 53 0 2325 943
22+
"2026-04-27" "3.1-0.011" 18 54 0 2344 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-27" "3.1-0.010" 18 53 0 2325 943
22+
"2026-04-27" "3.1-0.011" 18 54 0 2344 943

man/spatstat.sparse-internal.Rd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
%% sparse 3D arrays
1010
\alias{unionOfSparseIndices}
1111
\alias{inside3Darray}
12+
\alias{isComplex}
1213
\alias{SparseEntries}
1314
\alias{SparseIndices}
1415
\alias{SparseMatrixEntries}
@@ -35,6 +36,7 @@ check.mat.mul(A, B, Acols, Brows, fatal)
3536
%% sparse 3D arrays
3637
unionOfSparseIndices(A,B)
3738
inside3Darray(d, i, j, k)
39+
isComplex(x)
3840
SparseEntries(x)
3941
SparseIndices(x)
4042
SparseMatrixEntries(x, base)

tests/sparse3Darrays.R

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ require(spatstat.sparse)
55
ALWAYS <- FULLTEST <- TRUE
66
#' tests/sparse3Darrays.R
77
#' Basic tests of code in sparse3Darray.R and sparsecommon.R
8-
#' $Revision: 1.32 $ $Date: 2023/06/23 02:34:57 $
8+
#' $Revision: 1.33 $ $Date: 2026/04/27 06:49:58 $
99

1010
if(!exists("ALWAYS")) ALWAYS <- TRUE
1111
if(!exists("FULLTEST")) FULLTEST <- ALWAYS
1212

13-
if(ALWAYS) { # fundamental, C code
13+
if(ALWAYS) { # fundamental R and/or C code
1414
local({
1515
#' forming arrays
1616

@@ -106,7 +106,18 @@ local({
106106
stop("Incorrect answer from marginSumsSparse")
107107
}
108108

109-
}
109+
#' check strategy for avoiding compressed representation of symmetric matrix
110+
A <- matrix(c(10, 0, 0, 1,
111+
0, 20, 2, 7,
112+
0, 2, 30, 0,
113+
1, 7, 0, 40),
114+
4,4)
115+
As <- as(A, "sparseMatrix")
116+
dfA <- SparseEntries(As)
117+
if(nrow(dfA) != sum(A != 0))
118+
stop(paste("SparseEntries() does not correctly handle",
119+
"the compressed representation of a symmetric matrix"))
120+
}
110121
})
111122

112123

@@ -325,19 +336,35 @@ local({
325336
Mmap3 <- mapSparseEntries(Mempty, 1, matrix(1:10, 5, 2), across=3)
326337

327338
#' -------------- sparselinalg.R -------------------------
328-
U <- aperm(M,c(3,1,2)) # 2 x 5 x 5
329-
UU <- sumsymouterSparse(U, dbg=TRUE)
330-
w <- matrix(0, 5, 5)
331-
w[cbind(1:3,2:4)] <- 0.5
332-
w <- as(w, "sparseMatrix")
333-
UU <- sumsymouterSparse(U, w, dbg=TRUE)
334-
Uempty <- sparse3Darray(dims=c(2,5,5))
335-
UU <- sumsymouterSparse(Uempty, w, dbg=TRUE)
339+
Us <- aperm(M,c(3,1,2)) # 2 x 5 x 5, sparse3Darray
340+
Um <- as.array(Us)
341+
wm <- matrix(0, 5, 5)
342+
wm[cbind(1:3,2:4)] <- 0.5
343+
ws <- as(wm, "sparseMatrix")
344+
bm <- wm + t(wm)
345+
bs <- as(as(bm, "symmetricMatrix"), "sparseMatrix")
346+
## run different cases
347+
UUm <- sumsymouter(Um)
348+
UUs <- sumsymouterSparse(Us, dbg=TRUE)
349+
UUwm <- sumsymouter(Um, wm)
350+
UUws <- sumsymouterSparse(Us, ws, dbg=TRUE)
351+
UUbm <- sumsymouter(Um, bm)
352+
UUbs <- sumsymouterSparse(Us, bs)
353+
Vempty <- sparse3Darray(dims=c(2,5,5))
354+
VVws <- sumsymouterSparse(Vempty, ws)
355+
VVwm <- sumsymouter(as.array(Vempty), wm)
336356
#' complex
337-
Ucom <- U + U * 1i
338-
UU <- sumsymouterSparse(Ucom)
339-
UU <- sumsymouterSparse(Ucom, w)
340-
#'
357+
Ucom <- Us + Us * 1i
358+
UUc <- sumsymouter(Ucom)
359+
UUcw <- sumsymouter(Ucom, wm)
360+
## check validity
361+
if(!all(UUs == UUm))
362+
stop("sumsymouter(x): sparse and non-sparse algorithms disagree")
363+
if(!all(UUws == UUwm))
364+
stop("sumsymouter(x, w): sparse and non-sparse algorithms disagree")
365+
if(!all(UUbs == UUbm))
366+
stop(paste("sumsymouter(x, w): sparse and non-sparse algorithms disagree",
367+
"when w is symmetric"))
341368
}
342369

343370
## 1 x 1 x 1 arrays

0 commit comments

Comments
 (0)