Skip to content

Commit f1545a1

Browse files
committed
Handle diff() of long vector
1 parent fb739d7 commit f1545a1

10 files changed

Lines changed: 122 additions & 8 deletions

File tree

DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: spatstat.utils
2-
Version: 3.2-0.002
3-
Date: 2025-11-19
2+
Version: 3.2-0.003
3+
Date: 2025-12-29
44
Title: Utility Functions for 'spatstat'
55
Authors@R: c(person("Adrian", "Baddeley",
66
role = c("aut", "cre"),

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export("choptext")
4848
export("choptextline")
4949
export("commasep")
5050
export("complaining")
51+
export("difflong")
5152
export("distpl")
5253
export("distppl")
5354
export("distppll")

NEWS

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

2-
CHANGES IN spatstat.utils VERSION 3.2-0.002
2+
CHANGES IN spatstat.utils VERSION 3.2-0.003
33

44
OVERVIEW
55

R/longvec.R

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#'
2+
#' longvec.R
3+
#'
4+
#' Utilities for long vectors
5+
#'
6+
#' $Revision: 1.5 $ $Date: 2025/12/29 05:58:39 $
7+
8+
difflong <- function(x, drop=TRUE) {
9+
if(!is.null(dim(x))) stop("difflong is only implemented for vectors")
10+
sm <- storage.mode(x)
11+
switch(sm,
12+
double = {
13+
z <- .Call(C_Cdiffdouble,
14+
rx=as.double(x))
15+
},
16+
integer = {
17+
z <- .Call(C_Cdiffint,
18+
rx=as.integer(x))
19+
},
20+
logical = {
21+
z <- .Call(C_Cdiffint,
22+
rx=as.integer(x))
23+
},
24+
complex = {
25+
Rez <- .Call(C_Cdiffdouble,
26+
rx=as.double(Re(x)))
27+
Imz <- .Call(C_Cdiffdouble,
28+
rx=as.double(Im(x)))
29+
z <- complex(real=Rez, imaginary=Imz)
30+
},
31+
stop(paste("difflong is not supported for vectors of type",
32+
sQuote(sm)),
33+
call.=FALSE)
34+
)
35+
if(drop) z <- z[-1]
36+
return(z)
37+
}
38+

inst/doc/packagesizes.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@ date version nhelpfiles nobjects ndatasets Rlines srclines
3131
"2025-05-15" "3.1-4" 42 193 0 3689 2462
3232
"2025-07-17" "3.1-5" 42 194 0 3703 2462
3333
"2025-09-20" "3.2-0" 42 195 0 3716 2462
34-
"2025-11-19" "3.2-0.002" 42 195 0 3716 2462
34+
"2025-12-29" "3.2-0.003" 42 196 0 3754 2535

inst/info/packagesizes.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@ date version nhelpfiles nobjects ndatasets Rlines srclines
3131
"2025-05-15" "3.1-4" 42 193 0 3689 2462
3232
"2025-07-17" "3.1-5" 42 194 0 3703 2462
3333
"2025-09-20" "3.2-0" 42 195 0 3716 2462
34-
"2025-11-19" "3.2-0.002" 42 195 0 3716 2462
34+
"2025-12-29" "3.2-0.003" 42 196 0 3754 2535

man/spatstat.utils-internal.Rd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
\alias{choptext}
1515
\alias{choptextline}
1616
\alias{complaining}
17+
\alias{difflong}
1718
\alias{distpl}
1819
\alias{distppl}
1920
\alias{distppll}
@@ -126,6 +127,7 @@ check.satisfies(cond, xname, should, context, fatal, warn)
126127
choptext(\dots, prefix, indent)
127128
choptextline(st, w, prefix, indent)
128129
complaining(whinge, fatal, value)
130+
difflong(x, drop)
129131
distpl(p, l)
130132
distppl(p, l)
131133
distppll(p, l, mintype, method, listit)

src/init.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,11 @@ static const R_CMethodDef CEntries[] = {
3838
};
3939

4040
static const R_CallMethodDef CallEntries[] = {
41-
{"circMseg", (DL_FUNC) &circMseg, 7},
42-
{"circPseg", (DL_FUNC) &circPseg, 7},
43-
{"circXseg", (DL_FUNC) &circXseg, 7},
41+
{"Cdiffdouble", (DL_FUNC) &Cdiffdouble, 1},
42+
{"Cdiffint", (DL_FUNC) &Cdiffint, 1},
43+
{"circMseg", (DL_FUNC) &circMseg, 7},
44+
{"circPseg", (DL_FUNC) &circPseg, 7},
45+
{"circXseg", (DL_FUNC) &circXseg, 7},
4446
{NULL, NULL, 0}
4547
};
4648

src/longvec.c

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include <R.h>
2+
#include <Rinternals.h>
3+
4+
/*
5+
6+
longvec.c
7+
8+
Operations for long vectors
9+
10+
Cdiffdouble 'diff'
11+
Cdiffint
12+
Cnzdiff 'nonzero diff'
13+
14+
$Revision: 1.4 $ $Date: 2025/12/29 05:36:18 $
15+
16+
*/
17+
18+
SEXP Cdiffdouble(SEXP rx) {
19+
// diff for long vectors of doubles
20+
R_xlen_t n = xlength(rx);
21+
R_xlen_t i;
22+
double *x = REAL(rx);
23+
double xcur, xprev;
24+
// compute successive differences, with leading 0
25+
i = 1;
26+
xprev = x[i];
27+
for(i = 0; i < n; i++) {
28+
xcur = x[i];
29+
x[i] = xcur - xprev;
30+
xprev = xcur;
31+
}
32+
return rx;
33+
}
34+
35+
SEXP Cdiffint(SEXP rx) {
36+
// diff for long vectors of integers
37+
R_xlen_t n = xlength(rx);
38+
R_xlen_t i;
39+
// pointer to data
40+
int *x = INTEGER(rx);
41+
int xcur, xprev;
42+
// compute successive differences, with leading 0
43+
i = 1;
44+
xprev = x[i];
45+
for(i = 0; i < n; i++) {
46+
xcur = x[i];
47+
x[i] = xcur - xprev;
48+
xprev = xcur;
49+
}
50+
return rx;
51+
}
52+
53+
SEXP Cnzdiffint(SEXP rx) {
54+
// test whether diff() is non-zero (with leading TRUE)
55+
R_xlen_t n = xlength(rx);
56+
R_xlen_t i;
57+
// pointer to data
58+
int *x = INTEGER(rx);
59+
int xcur, xprev;
60+
i = 1;
61+
xprev = x[i];
62+
for(i = 0; i < n; i++) {
63+
xcur = x[i];
64+
x[i] = (xcur == xprev)? 0:1;
65+
xprev = xcur;
66+
}
67+
return rx;
68+
}
69+

src/proto.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ void nnd2segs(double *, double *, int *, double *, double *, double *, double *,
3535
Functions invoked by .Call
3636
3737
*/
38+
SEXP Cdiffdouble(SEXP); SEXP Cdiffint(SEXP); SEXP Cdiffint(SEXP);
39+
SEXP Cdiffdouble(SEXP); SEXP Cdiffdouble(SEXP);
3840
SEXP circXseg(SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP);
3941
SEXP circMseg(SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP);
4042
SEXP circPseg(SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP);

0 commit comments

Comments
 (0)