Skip to content

Commit df068db

Browse files
committed
Improvements to prime factorisation etc
1 parent 67da024 commit df068db

File tree

5 files changed

+20
-12
lines changed

5 files changed

+20
-12
lines changed

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.1-2.001
3-
Date: 2025-01-13
2+
Version: 3.1-2.002
3+
Date: 2025-01-19
44
Title: Utility Functions for 'spatstat'
55
Authors@R: c(person("Adrian", "Baddeley",
66
role = c("aut", "cre"),

NEWS

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

2-
CHANGES IN spatstat.utils VERSION 3.1-2.001
2+
CHANGES IN spatstat.utils VERSION 3.1-2.002
33

44
OVERVIEW
55

@@ -11,6 +11,10 @@ NEW FUNCTIONS
1111
Recognise whether a given integer is a square number, a cube,
1212
or a power of another integer.
1313

14+
SIGNIFICANT USER-VISIBLE CHANGES
15+
16+
o primefactors
17+
Computation for large numbers improved.
1418

1519
CHANGES IN spatstat.utils VERSION 3.1-2
1620

R/primefactors.R

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#
22
# primefactors.R
33
#
4-
# $Revision: 1.14 $ $Date: 2025/01/13 09:01:24 $
4+
# $Revision: 1.16 $ $Date: 2025/01/19 05:11:27 $
55
#
66

77
## Table of prime numbers is now in sysdata object 'Spatstat.PrimesTable'
@@ -33,7 +33,8 @@ primefactors <- function(n, method=c("C", "interpreted")) {
3333
check.1.integer(n)
3434
if(n <= 0) return(integer(0))
3535
method <- match.arg(method)
36-
if(method == "C" && n > .Machine$integer.max)
36+
MaxInt <- .Machine$integer.max
37+
if(method == "C" && n > MaxInt)
3738
method <- "interpreted"
3839
switch(method,
3940
interpreted = {
@@ -49,7 +50,8 @@ primefactors <- function(n, method=c("C", "interpreted")) {
4950
## reduce problem size
5051
primedivisors <- candidateprimes[divides]
5152
result <- sort(c(result, primedivisors))
52-
n <- as.integer(n/prod(primedivisors))
53+
n <- n/prod(primedivisors)
54+
if(n <= MaxInt) n <- as.integer(n)
5355
prmax <- floor(sqrt(n))
5456
candidateprimes <- primedivisors
5557
}
@@ -133,14 +135,16 @@ divisors <- local({
133135
is.square <- function(n) {
134136
check.1.integer(n)
135137
if(n < 0) return(FALSE)
136-
tp <- table(primefactors(n))
137-
all(tp %% 2 == 0)
138+
v <- sqrt(n)
139+
m <- c(floor(v), ceiling(v))
140+
any(m^2 == n)
138141
}
139142

140143
is.cube <- function(n) {
141144
check.1.integer(n)
142-
tp <- table(primefactors(abs(n)))
143-
all(tp %% 3 == 0)
145+
v <- n^(1/3)
146+
m <- c(floor(v), ceiling(v))
147+
any(m^3 == n)
144148
}
145149

146150
is.power <- function(n) {

inst/doc/packagesizes.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@ date version nhelpfiles nobjects ndatasets Rlines srclines
2727
"2024-06-17" "3.0-5" 41 189 0 3688 2031
2828
"2024-08-17" "3.1-0" 41 189 0 3613 2429
2929
"2024-11-02" "3.1-1" 41 189 0 3614 2429
30-
"2025-01-13" "3.1-2.001" 42 192 0 3646 2429
30+
"2025-01-19" "3.1-2.002" 42 192 0 3650 2429

inst/info/packagesizes.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@ date version nhelpfiles nobjects ndatasets Rlines srclines
2727
"2024-06-17" "3.0-5" 41 189 0 3688 2031
2828
"2024-08-17" "3.1-0" 41 189 0 3613 2429
2929
"2024-11-02" "3.1-1" 41 189 0 3614 2429
30-
"2025-01-13" "3.1-2.001" 42 192 0 3646 2429
30+
"2025-01-19" "3.1-2.002" 42 192 0 3650 2429

0 commit comments

Comments
 (0)