@@ -5,14 +5,15 @@ require(spatstat.sparse)
55ALWAYS <- FULLTEST <- TRUE
66# ' tests/sparsemarkov.R
77# ' Tests of code for Markov chain with sparse transition matrix
8- # ' $Revision: 1.1 $ $Date: 2026/04/11 04:29:47 $
8+ # ' $Revision: 1.3 $ $Date: 2026/04/23 05:20:03 $
99
1010if (! exists(" ALWAYS" )) ALWAYS <- TRUE
1111if (! exists(" FULLTEST" )) FULLTEST <- ALWAYS
1212
1313if (FULLTEST ) {
1414 local({
15- testit <- function (P , Pname , xstart = 50 , ns = 10 , np = 3 , walkies = TRUE ) {
15+ testit <- function (P , Pname , xstart = 50 , ns = 10 , np = 3 ,
16+ walkies = FALSE , absorb = NULL ) {
1617 cat(paste(" >>>>>> " , Pname , " <<<<<<<\n " ))
1718 cat(" \t single particle, final state..\n " )
1819 X1 <- runSparseMarkovChain(P , x0 = xstart , nsteps = ns , result = " l" )
@@ -26,24 +27,40 @@ if(FULLTEST) {
2627 result = " h" )
2728 if (length(X1 ) != 1 )
2829 stop(paste(Pname , " end state was not a single particle" ))
29- if (abs(X1 - xstart ) > ns )
30- stop(paste(Pname , " wandered impossibly far" ))
3130 if (length(X2 ) != (ns + 1 ))
3231 stop(paste(Pname , " history has wrong length" ))
33- if (walkies && ! all(abs(diff(X2 )) == 1 ))
34- stop(paste(Pname , " jumps were not all +- 1" ))
3532 if (length(X3 ) != np )
3633 stop(paste(Pname , " final number of particles was not preserved" ))
37- if (max(abs(X3 - xstart )) > ns )
38- stop(paste(Pname , " some particles wandered impossibly far" ))
3934 if (! is.matrix(X4 ))
4035 stop(paste(Pname , " a matrix was expected for the history" ))
4136 if (nrow(X4 ) != np )
4237 stop(paste(Pname , " number of particles was not preserved" ))
4338 if (ncol(X4 ) != (ns + 1 ))
4439 stop(paste(Pname , " histories have wrong length" ))
45- if (walkies && ! all(abs(apply(X4 , 1 , diff )) == 1 ))
46- stop(paste(Pname , " jumps were not all +- 1" ))
40+
41+ if (walkies ) {
42+ # # jumps are all +-1
43+ if (abs(X1 - xstart ) > ns )
44+ stop(paste(Pname , " wandered impossibly far" ))
45+ if (! all(abs(diff(X2 )) == 1 ))
46+ stop(paste(Pname , " jumps were not all +- 1" ))
47+ if (max(abs(X3 - xstart )) > ns )
48+ stop(paste(Pname , " some particles wandered impossibly far" ))
49+ if (! all(abs(apply(X4 , 1 , diff )) == 1 ))
50+ stop(paste(Pname , " jumps of particles were not all +- 1" ))
51+ }
52+
53+ if (! is.null(absorb )) {
54+ if (X1 != absorb )
55+ stop(" Absorbing chain didn't absorb (final)" )
56+ if (X2 [ns + 1 ] != absorb )
57+ stop(" Absorbing chain didn't absorb (history)" )
58+ if (any(X3 != absorb ))
59+ stop(" Absorbing chains didn't absorb (final)" )
60+ if (any(X4 [,ns + 1 ] != absorb ))
61+ stop(" Absorbing chains didn't absorb (history)" )
62+ }
63+
4764 cat(" OK\n " )
4865 return (invisible (list (X1 = X1 , X2 = X2 , X3 = X3 , X4 = X4 )))
4966 }
@@ -52,15 +69,13 @@ if(FULLTEST) {
5269 Pwalk <- matrix (0 , 100 , 100 )
5370 Pwalk [abs(row(Pwalk ) - col(Pwalk )) == 1 ] <- 1
5471 Pwalk <- Pwalk / rowSums(Pwalk )
55- testit(Pwalk , " Simple random walk" )
72+ testit(Pwalk , " Simple random walk" , walkies = TRUE )
5673
5774 # ' Absorbing
5875 Pabsorb <- matrix (0 , 10 , 10 )
5976 Pabsorb [row(Pabsorb ) < col(Pabsorb )] <- 1
60- Pabsorb [10 ,10 ] <- 1
77+ Pabsorb [,10 ] <- 1
6178 Pabsorb <- Pabsorb / rowSums(Pabsorb )
62- a <- testit(Pabsorb , " Absorbing chain" , xstart = 1 , ns = 10 , walkies = FALSE )
63- if (a $ X1 != 10 ) stop(" Absorbing chain didn't absorb" )
64- if (any(a $ X3 != 10 )) stop(" Absorbing chains didn't absorb" )
79+ a <- testit(Pabsorb , " Absorbing chain" , xstart = 1 , ns = 10 , absorb = 10 )
6580 }
6681)}
0 commit comments