| 1 |
# test_Permutation.praat |
| 2 |
# djmw 20050710, 20070820, 20100525, 20100819, 20110418, 20220825, 20250110 |
| 3 |
|
| 4 |
appendInfoLine: "test_Permutation.praat" |
| 5 |
|
| 6 |
@testPermutePart |
| 7 |
@test_600_12 |
| 8 |
@rotate |
| 9 |
@sequence |
| 10 |
@swap |
| 11 |
@invertp |
| 12 |
@multiply |
| 13 |
@jump |
| 14 |
@distributionTest: 10, 10000 |
| 15 |
@countInversions |
| 16 |
@testCreateSimple |
| 17 |
|
| 18 |
appendInfoLine: "test_Permutation OK" |
| 19 |
|
| 20 |
procedure multiply: |
| 21 |
appendInfo: tab$, "Multiply" |
| 22 |
.numberOfPermutations = 8 |
| 23 |
.numberOfElements = 5 |
| 24 |
for .i to .numberOfPermutations / 2 |
| 25 |
.p [2*.i -1] = Create Permutation: "p", .numberOfElements, "no" |
| 26 |
.p [2*.i] = Invert |
| 27 |
endfor |
| 28 |
selectObject: .p [1] |
| 29 |
for .i from 2 to .numberOfPermutations |
| 30 |
plusObject: .p [.i] |
| 31 |
endfor |
| 32 |
.p [.numberOfPermutations + 1] = Multiply |
| 33 |
for .j to .numberOfElements |
| 34 |
.val = Get value: .j |
| 35 |
assert .val = .j; p ['.j']='.val' |
| 36 |
appendInfo: "." |
| 37 |
endfor |
| 38 |
for .i to .numberOfPermutations + 1 |
| 39 |
removeObject: .p [.i] |
| 40 |
endfor |
| 41 |
appendInfoLine: ".OK" |
| 42 |
endproc |
| 43 |
|
| 44 |
procedure rotate |
| 45 |
appendInfoLine: tab$, "Rotate" |
| 46 |
for .i to 20 |
| 47 |
.p = Create Permutation: "test", .i, "yes" |
| 48 |
|
| 49 |
appendInfo: tab$, tab$, .i |
| 50 |
for .k to .i |
| 51 |
selectObject: .p |
| 52 |
.pk = Rotate: 0, 0, .k |
| 53 |
.pkk = Get value: .k + 1 |
| 54 |
if .pkk > 0 |
| 55 |
assert .pkk = 1 ; i='.i', k='.k' |
| 56 |
endif |
| 57 |
.pki = Rotate: 0, 0, -.k |
| 58 |
for .l to .k |
| 59 |
.pkil = Get value: .l |
| 60 |
assert .pkil = .l |
| 61 |
endfor |
| 62 |
removeObject: .pk, .pki |
| 63 |
appendInfo: "." |
| 64 |
endfor |
| 65 |
appendInfoLine: " OK" |
| 66 |
removeObject: .p |
| 67 |
endfor |
| 68 |
endproc |
| 69 |
|
| 70 |
procedure invertp |
| 71 |
appendInfo: tab$, "Invert" |
| 72 |
for .i to 20 |
| 73 |
.p = Create Permutation: "test", .i, "yes" |
| 74 |
.pin = Invert |
| 75 |
plusObject: .p |
| 76 |
.pm = Multiply |
| 77 |
for .k to .i |
| 78 |
.pmk = Get value: .k |
| 79 |
assert .pmk = .k ; i='.i', k='.k' |
| 80 |
endfor |
| 81 |
appendInfo: "." |
| 82 |
removeObject: .pin, .p, .pm |
| 83 |
endfor |
| 84 |
appendInfoLine: "OK" |
| 85 |
endproc |
| 86 |
|
| 87 |
procedure test_600_12 |
| 88 |
appendInfo: tab$, "test_600_12" |
| 89 |
.p = Create Permutation: "600", 600, "yes" |
| 90 |
.pin = Interleave: 0, 0, 12, 1 |
| 91 |
.pininv = Invert |
| 92 |
plusObject: .pin |
| 93 |
.pm = Multiply |
| 94 |
for .l to 600 |
| 95 |
.pkil = Get value: .l |
| 96 |
assert .pkil = .l; l='.l' |
| 97 |
endfor |
| 98 |
removeObject: .pin, .pininv, .p, .pm |
| 99 |
appendInfoLine: " OK" |
| 100 |
endproc |
| 101 |
|
| 102 |
procedure sequence |
| 103 |
appendInfo: tab$, "Sequences" |
| 104 |
.n = 4 |
| 105 |
.np = 4*3*2 - 1 |
| 106 |
p = Create Permutation: "ps", .n, "yes" |
| 107 |
for .j to .np |
| 108 |
for .i to .j |
| 109 |
Next |
| 110 |
endfor |
| 111 |
for .i to .j |
| 112 |
Previous |
| 113 |
endfor |
| 114 |
for .i to .n |
| 115 |
.val = Get value: .i |
| 116 |
assert .val = .i |
| 117 |
endfor |
| 118 |
appendInfo: "." |
| 119 |
endfor |
| 120 |
removeObject: p |
| 121 |
appendInfoLine: "OK" |
| 122 |
endproc |
| 123 |
|
| 124 |
procedure swap |
| 125 |
appendInfo: tab$, "Swap positions" |
| 126 |
.p = Create Permutation: "id", 10, "yes" |
| 127 |
for .i to 10 |
| 128 |
for .j to 10 |
| 129 |
selectObject: .p |
| 130 |
.ps = Copy: "swap" |
| 131 |
Swap positions: .i, .j |
| 132 |
for .k to 10 |
| 133 |
selectObject: .p |
| 134 |
.pk = Get value: .k |
| 135 |
selectObject: .ps |
| 136 |
.psk = Get value: .k |
| 137 |
if .k <> .i and .k <> .j |
| 138 |
assert .pk = .psk |
| 139 |
endif |
| 140 |
endfor |
| 141 |
removeObject: .ps |
| 142 |
appendInfo: "." |
| 143 |
endfor |
| 144 |
endfor |
| 145 |
removeObject: .p |
| 146 |
appendInfoLine: "OK" |
| 147 |
endproc |
| 148 |
|
| 149 |
procedure jump |
| 150 |
appendInfo: tab$, "Jump" |
| 151 |
.p1 = Create Permutation: "id", 10, "yes" |
| 152 |
Jump: 3, 1 |
| 153 |
.p# = List values |
| 154 |
assert .p# [1] = 1 |
| 155 |
assert .p# [2] = 4 |
| 156 |
assert .p# [3] = 7 |
| 157 |
assert .p# [4] = 10 |
| 158 |
assert .p# [5] = 2 |
| 159 |
assert .p# [6] = 5 |
| 160 |
assert .p# [7] = 8 |
| 161 |
assert .p# [8] = 3 |
| 162 |
assert .p# [9] = 6 |
| 163 |
assert .p# [10] = 9 |
| 164 |
appendInfo: "." |
| 165 |
.p2 = Create Permutation: "id", 10, "yes" |
| 166 |
Jump: 3, 2 |
| 167 |
.p# = List values |
| 168 |
assert .p# [1] = 2 |
| 169 |
assert .p# [2] = 5 |
| 170 |
assert .p# [3] = 8 |
| 171 |
assert .p# [4] = 3 |
| 172 |
assert .p# [5] = 6 |
| 173 |
assert .p# [6] = 9 |
| 174 |
assert .p# [7] = 1 |
| 175 |
assert .p# [8] = 4 |
| 176 |
assert .p# [9] = 7 |
| 177 |
assert .p# [10] = 10 |
| 178 |
removeObject: .p1, .p2 |
| 179 |
appendInfoLine: ".OK" |
| 180 |
endproc |
| 181 |
|
| 182 |
procedure distributionTest: .size, .numberOfRepetitions |
| 183 |
appendInfoLine: tab$, "distributionTest" |
| 184 |
.permutation = Create Permutation: "p", .size, "yes" |
| 185 |
.distribution## = zero## (.size, .size) |
| 186 |
for .iperm to .numberOfRepetitions |
| 187 |
Permute randomly (in-place): 0, 0 |
| 188 |
.values# = List values |
| 189 |
for .ipos to .size |
| 190 |
.distribution## [.ipos, .values# [.ipos]] += 1 |
| 191 |
endfor |
| 192 |
endfor |
| 193 |
# the table is created but not used. Just in case you want to have a look at the distribution |
| 194 |
# of the numbers over the index positions. |
| 195 |
.tor = Create TableOfReal: "distribution", .size, .size |
| 196 |
for .irow to .size |
| 197 |
Set row label (index): .irow, string$ (.irow) |
| 198 |
Set column label (index): .irow, "#" + string$ (.irow) |
| 199 |
for .icol to .size |
| 200 |
Set value: .irow, .icol, .distribution## [.irow, .icol] |
| 201 |
endfor |
| 202 |
endfor |
| 203 |
.expected = .numberOfRepetitions / .size |
| 204 |
.diff## = .distribution## - .expected |
| 205 |
.chiSq = sum (.diff## * .diff##) / .expected^2 |
| 206 |
.df = (.size - 1)^2 ; last value in each row and whole last row are completely determined |
| 207 |
.p = chiSquareQ (.chiSq, .df) |
| 208 |
assert .p > 0.999 |
| 209 |
appendInfoLine: tab$, tab$, "p = ", .p, " for chiSquare = ", |
| 210 |
... .chiSq, " with ", .df, " degrees of freedom." |
| 211 |
removeObject: .permutation, .tor |
| 212 |
appendInfoLine: tab$, tab$, "Realized## - expected (=", fixed$ (.expected, 0), "):" |
| 213 |
for .irow to .size |
| 214 |
appendInfo: tab$, tab$, tab$, .diff## [.irow, 1] |
| 215 |
for .icol from 2 to .size |
| 216 |
appendInfo: " ", .diff## [.irow, .icol] |
| 217 |
endfor |
| 218 |
appendInfoLine: "" |
| 219 |
endfor |
| 220 |
appendInfoLine: tab$, "distributionTest OK" |
| 221 |
endproc |
| 222 |
|
| 223 |
procedure testPermutePart |
| 224 |
appendInfo: tab$, "Permute part" |
| 225 |
.p10 = Create Permutation: "p", 10, "yes" |
| 226 |
.v10# = List values |
| 227 |
.p5 = Create Permutation: "p", 5, "no" |
| 228 |
.v5# = List values |
| 229 |
selectObject: .p10, .p5 |
| 230 |
.p1 = Permute part: 1 |
| 231 |
.v1# = List values |
| 232 |
for i to 5 |
| 233 |
assert .v1# [i] == .v5# [i] |
| 234 |
assert .v1# [5 + i] = 5 + i |
| 235 |
endfor |
| 236 |
selectObject: .p10, .p5 |
| 237 |
.p2 = Permute part: 6 |
| 238 |
.v2# = List values |
| 239 |
for i to 5 |
| 240 |
assert .v2# [i] == i |
| 241 |
assert .v2# [5 + i] = 5 + .v5# [i] |
| 242 |
endfor |
| 243 |
removeObject: .p2, .p1, .p5, .p10 |
| 244 |
appendInfoLine: " OK" |
| 245 |
endproc |
| 246 |
|
| 247 |
procedure countInversions |
| 248 |
appendInfoLine: tab$, "Count inversions" |
| 249 |
appendInfo: tab$, tab$, "Counting" |
| 250 |
.size = 20 |
| 251 |
for .i from 2 to .size |
| 252 |
.p = Create Permutation: "p", .i, "yes" |
| 253 |
for .repetions to max (5, min (10, .size * (.size - 1) / 2)) |
| 254 |
Permute randomly (in-place): 0, 0 |
| 255 |
.inversions = Get number of inversions |
| 256 |
.inversionsc = 0 |
| 257 |
.values# = List values |
| 258 |
for .k to .i - 1 |
| 259 |
.valk = .values# [.k] |
| 260 |
for .j from .k + 1 to .i |
| 261 |
if .valk > .values# [.j] |
| 262 |
.inversionsc += 1 |
| 263 |
endif |
| 264 |
endfor |
| 265 |
endfor |
| 266 |
assert .inversions = .inversionsc |
| 267 |
endfor |
| 268 |
removeObject: .p |
| 269 |
endfor |
| 270 |
appendInfoLine: " OK" |
| 271 |
|
| 272 |
.p = Create Permutation: "p", 10, "yes" |
| 273 |
# 1,2,3,4,5,6,7,8,9,10 -> 0 |
| 274 |
.pi = Interleave: 0, 0, 5, 0 |
| 275 |
# 1,6,2,7,3,8,4,9,5,10 -> 10 |
| 276 |
.numberOfInversions = Get number of inversions |
| 277 |
assert .numberOfInversions = 10 |
| 278 |
|
| 279 |
appendInfo: tab$, tab$, "List all inversions" |
| 280 |
.inversions_known## = {{6,2}, {6,3}, {6,4}, {6,5}, {7,3}, {7,4}, {7,5}, {8,4}, {8,5}, {9,5}} |
| 281 |
.inversions## = List all inversions |
| 282 |
assert numberOfRows (.inversions##) = .numberOfInversions |
| 283 |
for .irow to .numberOfInversions |
| 284 |
.known = .inversions_known## [.irow, 1] |
| 285 |
.test = .inversions## [.irow, 1] |
| 286 |
assert .known = .test ; col1 '.known' '.test' |
| 287 |
.known = .inversions_known## [.irow, 2] |
| 288 |
.test = .inversions## [.irow, 2] |
| 289 |
assert .known = .test ; col2 '.known' '.test' |
| 290 |
endfor |
| 291 |
appendInfoLine: " OK" |
| 292 |
|
| 293 |
appendInfo: tab$, tab$, "List random inversions" |
| 294 |
.nrandom = 2 |
| 295 |
.randomInversions## = List random inversions: .nrandom |
| 296 |
@checkInversions: .randomInversions##, .inversions_known##; |
| 297 |
|
| 298 |
.nrandom = .numberOfInversions + 5 |
| 299 |
.randomInversions## = List random inversions: .nrandom |
| 300 |
@checkInversions: .randomInversions##, .inversions_known##; |
| 301 |
appendInfoLine: " OK" |
| 302 |
|
| 303 |
removeObject: .pi, .p |
| 304 |
appendInfoLine: tab$, "Count inversions OK" |
| 305 |
endproc |
| 306 |
|
| 307 |
procedure checkInversions: .r##, .ref## |
| 308 |
for .irowr to numberOfRows (.r##) |
| 309 |
.found = 0 |
| 310 |
.irow = 1 |
| 311 |
repeat |
| 312 |
if .r## [.irowr, 1] = .ref## [.irow, 1] and .r## [.irowr, 2] = .ref## [.irow, 2] |
| 313 |
.found = 1 |
| 314 |
endif |
| 315 |
.irow += 1 |
| 316 |
until .found or .irow > numberOfRows (.ref##) |
| 317 |
assert .found; '.irowr' |
| 318 |
endfor |
| 319 |
endproc |
| 320 |
|
| 321 |
procedure testCreateSimple |
| 322 |
appendInfo: tab$ ,"testCreateSimple" |
| 323 |
.p1 = Create simple Permutation: "p1", {1, 2} |
| 324 |
asserterror There should be at least one element in a Permutation. |
| 325 |
.p2 = Create simple Permutation: "p2", {} |
| 326 |
asserterror All numbers from 1 to 3 should occur exactly once, e.g. the value 1 occurs 2 times. |
| 327 |
.p3 = Create simple Permutation: "p3", {3, 1, 1} |
| 328 |
asserterror Your maximum number (4) should not be larger than the number of elements you supplied (3). |
| 329 |
.p4 = Create simple Permutation: "p4", {3, 1, 4} |
| 330 |
asserterror Your minimum number should be 1 (it is 0). |
| 331 |
.p5 = Create simple Permutation: "p5", {0, 1, 2} |
| 332 |
removeObject: .p1 |
| 333 |
appendInfoLine: " OK" |
| 334 |
endproc |
| 335 |
|
| 336 |
procedure multiplyP: .a, .b |
| 337 |
assert .a < .b |
| 338 |
selectObject: .a, .b |
| 339 |
.a$ = selected$ ("Permutation", 1) |
| 340 |
.b$ = selected$ ("Permutation", 2) |
| 341 |
.r = Multiply |
| 342 |
.newName$ = .a$ + "_x_" + .b$ |
| 343 |
Rename: .newName$ |
| 344 |
.numberOfInversions = Get number of inversions |
| 345 |
appendInfoLine: .numberOfInversions, " inversions for: ", .newName$ |
| 346 |
endproc |
| 347 |
|
| 348 |
procedure checkDifferenceBetweenTwoPermutations |
| 349 |
.b = Create simple Permutation: "b", {2, 6, 7, 10, 5, 1, 4, 9, 3, 8} |
| 350 |
.e = Create simple Permutation: "e", {10, 9, 7, 8, 6, 5, 4, 2, 3, 1} |
| 351 |
.nie = Get number of inversions |
| 352 |
.ei = Invert |
| 353 |
Rename: "ei" |
| 354 |
selectObject: .b |
| 355 |
.nib = Get number of inversions |
| 356 |
appendInfoLine: "We want ", .nie - .nib, " inversions" |
| 357 |
.bi = Invert |
| 358 |
Rename: "bi" |
| 359 |
selectObject: .b |
| 360 |
.bc = Copy: "b" |
| 361 |
selectObject: .e |
| 362 |
.ec = Copy: "e" |
| 363 |
selectObject: .ei |
| 364 |
.eic = Copy: "ei" |
| 365 |
@multiplyP: .b, .e |
| 366 |
@multiplyP: .e, .bc |
| 367 |
@multiplyP: .b, .ei |
| 368 |
@multiplyP: .ei, .bc |
| 369 |
@multiplyP: .ei, .bi |
| 370 |
@multiplyP: .e, .bi |
| 371 |
@multiplyP: .bi, .ec |
| 372 |
@multiplyP: .bi, .eic |
| 373 |
|
| 374 |
removeObject: .b, .e, .bi, .ei, .b_x_ei, .ei_x_b, .ei_x_bi |
| 375 |
endproc |