| 1 |
;;; MATRIX -- Obtained from Andrew Wright. |
| 2 |
|
| 3 |
(library (rnrs-benchmarks matrix) |
| 4 |
(export main) |
| 5 |
(import (rnrs) (rnrs r5rs) (rnrs mutable-pairs) (rnrs-benchmarks)) |
| 6 |
; Chez-Scheme compatibility stuff: |
| 7 |
|
| 8 |
(define (chez-box x) (cons x '())) |
| 9 |
(define (chez-unbox x) (car x)) |
| 10 |
(define (chez-set-box! x y) (set-car! x y)) |
| 11 |
|
| 12 |
; Test that a matrix with entries in {+1, -1} is maximal among the matricies |
| 13 |
; obtainable by |
| 14 |
; re-ordering the rows |
| 15 |
; re-ordering the columns |
| 16 |
; negating any subset of the columns |
| 17 |
; negating any subset of the rows |
| 18 |
; Where we compare two matricies by lexicographically comparing the first row, |
| 19 |
; then the next to last, etc., and we compare a row by lexicographically |
| 20 |
; comparing the first entry, the second entry, etc., and we compare two |
| 21 |
; entries by +1 > -1. |
| 22 |
; Note, this scheme obeys the useful fact that if (append mat1 mat2) is |
| 23 |
; maximal, then so is mat1. Thus, we can build up maximal matricies |
| 24 |
; row by row. |
| 25 |
; |
| 26 |
; Once you have chosen the row re-ordering so that you know which row goes |
| 27 |
; last, the set of columns to negate is fixed (since the last row must be |
| 28 |
; all +1's). |
| 29 |
; |
| 30 |
; Note, the column ordering is really totally determined as follows: |
| 31 |
; all columns for which the second row is +1 must come before all |
| 32 |
; columns for which the second row is -1. |
| 33 |
; among columns for which the second row is +1, all columns for which |
| 34 |
; the third row is +1 come before those for which the third is |
| 35 |
; -1, and similarly for columns in which the second row is -1. |
| 36 |
; etc |
| 37 |
; Thus, each succeeding row sorts columns withing refinings equivalence |
| 38 |
; classes. |
| 39 |
; |
| 40 |
; Maximal? assumes that mat has atleast one row, and that the first row |
| 41 |
; is all +1's. |
| 42 |
(define maximal? |
| 43 |
(lambda (mat) |
| 44 |
(let pick-first-row |
| 45 |
((first-row-perm |
| 46 |
(gen-perms mat))) |
| 47 |
(if first-row-perm |
| 48 |
(and (zunda first-row-perm mat) |
| 49 |
(pick-first-row (first-row-perm 'brother))) |
| 50 |
#t)))) |
| 51 |
|
| 52 |
(define zunda |
| 53 |
(lambda (first-row-perm mat) |
| 54 |
(let* ((first-row |
| 55 |
(first-row-perm 'now)) |
| 56 |
(number-of-cols |
| 57 |
(length first-row)) |
| 58 |
(make-row->func |
| 59 |
(lambda (if-equal if-different) |
| 60 |
(lambda (row) |
| 61 |
(let ((vec |
| 62 |
(make-vector number-of-cols))) |
| 63 |
(do ((i 0 (+ i 1)) |
| 64 |
(first first-row |
| 65 |
(cdr first)) |
| 66 |
(row row |
| 67 |
(cdr row))) |
| 68 |
((= i number-of-cols)) |
| 69 |
(vector-set! vec |
| 70 |
i |
| 71 |
(if (= (car first) (car row)) |
| 72 |
if-equal |
| 73 |
if-different))) |
| 74 |
(lambda (i) |
| 75 |
(vector-ref vec i)))))) |
| 76 |
(mat |
| 77 |
(cdr mat))) |
| 78 |
(zebra (first-row-perm 'child) |
| 79 |
(make-row->func 1 -1) |
| 80 |
(make-row->func -1 1) |
| 81 |
mat |
| 82 |
number-of-cols)))) |
| 83 |
|
| 84 |
|
| 85 |
(define zebra |
| 86 |
(lambda (row-perm row->func+ row->func- mat number-of-cols) |
| 87 |
(let _-*- |
| 88 |
((row-perm |
| 89 |
row-perm) |
| 90 |
(mat |
| 91 |
mat) |
| 92 |
(partitions |
| 93 |
(list (miota number-of-cols)))) |
| 94 |
(or (not row-perm) |
| 95 |
(and |
| 96 |
(zulu (car mat) |
| 97 |
(row->func+ (row-perm 'now)) |
| 98 |
partitions |
| 99 |
(lambda (new-partitions) |
| 100 |
(_-*- (row-perm 'child) |
| 101 |
(cdr mat) |
| 102 |
new-partitions))) |
| 103 |
(zulu (car mat) |
| 104 |
(row->func- (row-perm 'now)) |
| 105 |
partitions |
| 106 |
(lambda (new-partitions) |
| 107 |
(_-*- (row-perm 'child) |
| 108 |
(cdr mat) |
| 109 |
new-partitions))) |
| 110 |
(let ((new-row-perm |
| 111 |
(row-perm 'brother))) |
| 112 |
(or (not new-row-perm) |
| 113 |
(_-*- new-row-perm |
| 114 |
mat |
| 115 |
partitions)))))))) |
| 116 |
|
| 117 |
|
| 118 |
(define zulu |
| 119 |
(let ((cons-if-not-null |
| 120 |
(lambda (lhs rhs) |
| 121 |
(if (null? lhs) |
| 122 |
rhs |
| 123 |
(cons lhs rhs))))) |
| 124 |
(lambda (old-row new-row-func partitions equal-cont) |
| 125 |
(let _-*- |
| 126 |
((p-in |
| 127 |
partitions) |
| 128 |
(old-row |
| 129 |
old-row) |
| 130 |
(rev-p-out |
| 131 |
'())) |
| 132 |
(let _-split- |
| 133 |
((partition |
| 134 |
(car p-in)) |
| 135 |
(old-row |
| 136 |
old-row) |
| 137 |
(plus |
| 138 |
'()) |
| 139 |
(minus |
| 140 |
'())) |
| 141 |
(if (null? partition) |
| 142 |
(let _-minus- |
| 143 |
((old-row |
| 144 |
old-row) |
| 145 |
(m |
| 146 |
minus)) |
| 147 |
(if (null? m) |
| 148 |
(let ((rev-p-out |
| 149 |
(cons-if-not-null |
| 150 |
minus |
| 151 |
(cons-if-not-null |
| 152 |
plus |
| 153 |
rev-p-out))) |
| 154 |
(p-in |
| 155 |
(cdr p-in))) |
| 156 |
(if (null? p-in) |
| 157 |
(equal-cont (reverse rev-p-out)) |
| 158 |
(_-*- p-in old-row rev-p-out))) |
| 159 |
(or (= 1 (car old-row)) |
| 160 |
(_-minus- (cdr old-row) |
| 161 |
(cdr m))))) |
| 162 |
(let ((next |
| 163 |
(car partition))) |
| 164 |
(case (new-row-func next) |
| 165 |
((1) |
| 166 |
(and (= 1 (car old-row)) |
| 167 |
(_-split- (cdr partition) |
| 168 |
(cdr old-row) |
| 169 |
(cons next plus) |
| 170 |
minus))) |
| 171 |
((-1) |
| 172 |
(_-split- (cdr partition) |
| 173 |
old-row |
| 174 |
plus |
| 175 |
(cons next minus))))))))))) |
| 176 |
|
| 177 |
(define all? |
| 178 |
(lambda (ok? lst) |
| 179 |
(let _-*- |
| 180 |
((lst |
| 181 |
lst)) |
| 182 |
(or (null? lst) |
| 183 |
(and (ok? (car lst)) |
| 184 |
(_-*- (cdr lst))))))) |
| 185 |
|
| 186 |
(define gen-perms |
| 187 |
(lambda (objects) |
| 188 |
(let _-*- |
| 189 |
((zulu-future |
| 190 |
objects) |
| 191 |
(past |
| 192 |
'())) |
| 193 |
(if (null? zulu-future) |
| 194 |
#f |
| 195 |
(lambda (msg) |
| 196 |
(case msg |
| 197 |
((now) |
| 198 |
(car zulu-future)) |
| 199 |
((brother) |
| 200 |
(_-*- (cdr zulu-future) |
| 201 |
(cons (car zulu-future) |
| 202 |
past))) |
| 203 |
((child) |
| 204 |
(gen-perms |
| 205 |
(fold past cons (cdr zulu-future)))) |
| 206 |
((puke) |
| 207 |
(cons (car zulu-future) |
| 208 |
(fold past cons (cdr zulu-future)))) |
| 209 |
(else |
| 210 |
(fatal-error 'gen-perms "Bad msg: ~a" msg)))))))) |
| 211 |
|
| 212 |
(define fold |
| 213 |
(lambda (lst folder state) |
| 214 |
(let _-*- |
| 215 |
((lst |
| 216 |
lst) |
| 217 |
(state |
| 218 |
state)) |
| 219 |
(if (null? lst) |
| 220 |
state |
| 221 |
(_-*- (cdr lst) |
| 222 |
(folder (car lst) |
| 223 |
state)))))) |
| 224 |
|
| 225 |
(define miota |
| 226 |
(lambda (len) |
| 227 |
(let _-*- |
| 228 |
((i 0)) |
| 229 |
(if (= i len) |
| 230 |
'() |
| 231 |
(cons i |
| 232 |
(_-*- (+ i 1))))))) |
| 233 |
|
| 234 |
(define proc->vector |
| 235 |
(lambda (size proc) |
| 236 |
(let ((res |
| 237 |
(make-vector size))) |
| 238 |
(do ((i 0 |
| 239 |
(+ i 1))) |
| 240 |
((= i size)) |
| 241 |
(vector-set! res |
| 242 |
i |
| 243 |
(proc i))) |
| 244 |
res))) |
| 245 |
|
| 246 |
; Given a prime number P, return a procedure which, given a `maker' procedure, |
| 247 |
; calls it on the operations for the field Z/PZ. |
| 248 |
(define make-modular |
| 249 |
(lambda (modulus) |
| 250 |
(let* ((reduce |
| 251 |
(lambda (x) |
| 252 |
(modulo x modulus))) |
| 253 |
(coef-zero? |
| 254 |
(lambda (x) |
| 255 |
(zero? (reduce x)))) |
| 256 |
(coef-+ |
| 257 |
(lambda (x y) |
| 258 |
(reduce (+ x y)))) |
| 259 |
(coef-negate |
| 260 |
(lambda (x) |
| 261 |
(reduce (- x)))) |
| 262 |
(coef-* |
| 263 |
(lambda (x y) |
| 264 |
(reduce (* x y)))) |
| 265 |
(coef-recip |
| 266 |
(let ((inverses |
| 267 |
(proc->vector (- modulus 1) |
| 268 |
(lambda (i) |
| 269 |
(extended-gcd (+ i 1) |
| 270 |
modulus |
| 271 |
(lambda (gcd inverse ignore) |
| 272 |
inverse)))))) |
| 273 |
; Coef-recip. |
| 274 |
(lambda (x) |
| 275 |
(let ((x |
| 276 |
(reduce x))) |
| 277 |
(vector-ref inverses (- x 1))))))) |
| 278 |
(lambda (maker) |
| 279 |
(maker 0 ; coef-zero |
| 280 |
1 ; coef-one |
| 281 |
coef-zero? |
| 282 |
coef-+ |
| 283 |
coef-negate |
| 284 |
coef-* |
| 285 |
coef-recip))))) |
| 286 |
|
| 287 |
; Extended Euclidean algorithm. |
| 288 |
; (extended-gcd a b cont) computes the gcd of a and b, and expresses it |
| 289 |
; as a linear combination of a and b. It returns calling cont via |
| 290 |
; (cont gcd a-coef b-coef) |
| 291 |
; where gcd is the GCD and is equal to a-coef * a + b-coef * b. |
| 292 |
(define extended-gcd |
| 293 |
(let ((n->sgn/abs |
| 294 |
(lambda (x cont) |
| 295 |
(if (>= x 0) |
| 296 |
(cont 1 x) |
| 297 |
(cons -1 (- x)))))) |
| 298 |
(lambda (a b cont) |
| 299 |
(n->sgn/abs a |
| 300 |
(lambda (p-a p) |
| 301 |
(n->sgn/abs b |
| 302 |
(lambda (q-b q) |
| 303 |
(let _-*- |
| 304 |
((p |
| 305 |
p) |
| 306 |
(p-a |
| 307 |
p-a) |
| 308 |
(p-b |
| 309 |
0) |
| 310 |
(q |
| 311 |
q) |
| 312 |
(q-a |
| 313 |
0) |
| 314 |
(q-b |
| 315 |
q-b)) |
| 316 |
(if (zero? q) |
| 317 |
(cont p p-a p-b) |
| 318 |
(let ((mult |
| 319 |
(quotient p q))) |
| 320 |
(_-*- q |
| 321 |
q-a |
| 322 |
q-b |
| 323 |
(- p (* mult q)) |
| 324 |
(- p-a (* mult q-a)) |
| 325 |
(- p-b (* mult q-b))))))))))))) |
| 326 |
|
| 327 |
; Given elements and operations on the base field, return a procedure which |
| 328 |
; computes the row-reduced version of a matrix over that field. The result |
| 329 |
; is a list of rows where the first non-zero entry in each row is a 1 (in |
| 330 |
; the coefficient field) and occurs to the right of all the leading non-zero |
| 331 |
; entries of previous rows. In particular, the number of rows is the rank |
| 332 |
; of the original matrix, and they have the same row-space. |
| 333 |
; The items related to the base field which are needed are: |
| 334 |
; coef-zero additive identity |
| 335 |
; coef-one multiplicative identity |
| 336 |
; coef-zero? test for additive identity |
| 337 |
; coef-+ addition (two args) |
| 338 |
; coef-negate additive inverse |
| 339 |
; coef-* multiplication (two args) |
| 340 |
; coef-recip multiplicative inverse |
| 341 |
; Note, matricies are stored as lists of rows (i.e., lists of lists). |
| 342 |
(define make-row-reduce |
| 343 |
(lambda (coef-zero coef-one coef-zero? coef-+ coef-negate coef-* coef-recip) |
| 344 |
(lambda (mat) |
| 345 |
(let _-*- |
| 346 |
((mat |
| 347 |
mat)) |
| 348 |
(if (or (null? mat) |
| 349 |
(null? (car mat))) |
| 350 |
'() |
| 351 |
(let _-**- |
| 352 |
((in |
| 353 |
mat) |
| 354 |
(out |
| 355 |
'())) |
| 356 |
(if (null? in) |
| 357 |
(map |
| 358 |
(lambda (x) |
| 359 |
(cons coef-zero x)) |
| 360 |
(_-*- out)) |
| 361 |
(let* ((prow |
| 362 |
(car in)) |
| 363 |
(pivot |
| 364 |
(car prow)) |
| 365 |
(prest |
| 366 |
(cdr prow)) |
| 367 |
(in |
| 368 |
(cdr in))) |
| 369 |
(if (coef-zero? pivot) |
| 370 |
(_-**- in |
| 371 |
(cons prest out)) |
| 372 |
(let ((zap-row |
| 373 |
(map |
| 374 |
(let ((mult |
| 375 |
(coef-recip pivot))) |
| 376 |
(lambda (x) |
| 377 |
(coef-* mult x))) |
| 378 |
prest))) |
| 379 |
(cons (cons coef-one zap-row) |
| 380 |
(map |
| 381 |
(lambda (x) |
| 382 |
(cons coef-zero x)) |
| 383 |
(_-*- |
| 384 |
(fold in |
| 385 |
(lambda (row mat) |
| 386 |
(cons |
| 387 |
(let ((first-col |
| 388 |
(car row)) |
| 389 |
(rest-row |
| 390 |
(cdr row))) |
| 391 |
(if (coef-zero? first-col) |
| 392 |
rest-row |
| 393 |
(map |
| 394 |
(let ((mult |
| 395 |
(coef-negate first-col))) |
| 396 |
(lambda (f z) |
| 397 |
(coef-+ f |
| 398 |
(coef-* mult z)))) |
| 399 |
rest-row |
| 400 |
zap-row))) |
| 401 |
mat)) |
| 402 |
out)))))))))))))) |
| 403 |
|
| 404 |
|
| 405 |
; Given elements and operations on the base field, return a procedure which |
| 406 |
; when given a matrix and a vector tests to see if the vector is in the |
| 407 |
; row-space of the matrix. This returned function is curried. |
| 408 |
; The items related to the base field which are needed are: |
| 409 |
; coef-zero additive identity |
| 410 |
; coef-one multiplicative identity |
| 411 |
; coef-zero? test for additive identity |
| 412 |
; coef-+ addition (two args) |
| 413 |
; coef-negate additive inverse |
| 414 |
; coef-* multiplication (two args) |
| 415 |
; coef-recip multiplicative inverse |
| 416 |
; Note, matricies are stored as lists of rows (i.e., lists of lists). |
| 417 |
(define make-in-row-space? |
| 418 |
(lambda (coef-zero coef-one coef-zero? coef-+ coef-negate coef-* coef-recip) |
| 419 |
(let ((row-reduce |
| 420 |
(make-row-reduce coef-zero |
| 421 |
coef-one |
| 422 |
coef-zero? |
| 423 |
coef-+ |
| 424 |
coef-negate |
| 425 |
coef-* |
| 426 |
coef-recip))) |
| 427 |
(lambda (mat) |
| 428 |
(let ((mat |
| 429 |
(row-reduce mat))) |
| 430 |
(lambda (row) |
| 431 |
(let _-*- |
| 432 |
((row |
| 433 |
row) |
| 434 |
(mat |
| 435 |
mat)) |
| 436 |
(if (null? row) |
| 437 |
#t |
| 438 |
(let ((r-first |
| 439 |
(car row)) |
| 440 |
(r-rest |
| 441 |
(cdr row))) |
| 442 |
(cond ((coef-zero? r-first) |
| 443 |
(_-*- r-rest |
| 444 |
(map cdr |
| 445 |
(if (or (null? mat) |
| 446 |
(coef-zero? (caar mat))) |
| 447 |
mat |
| 448 |
(cdr mat))))) |
| 449 |
((null? mat) |
| 450 |
#f) |
| 451 |
(else |
| 452 |
(let* ((zap-row |
| 453 |
(car mat)) |
| 454 |
(z-first |
| 455 |
(car zap-row)) |
| 456 |
(z-rest |
| 457 |
(cdr zap-row)) |
| 458 |
(mat |
| 459 |
(cdr mat))) |
| 460 |
(if (coef-zero? z-first) |
| 461 |
#f |
| 462 |
(_-*- |
| 463 |
(map |
| 464 |
(let ((mult |
| 465 |
(coef-negate r-first))) |
| 466 |
(lambda (r z) |
| 467 |
(coef-+ r |
| 468 |
(coef-* mult z)))) |
| 469 |
r-rest |
| 470 |
z-rest) |
| 471 |
(map cdr mat))))))))))))))) |
| 472 |
|
| 473 |
|
| 474 |
; Given a prime number, return a procedure which takes integer matricies |
| 475 |
; and returns their row-reduced form, modulo the prime. |
| 476 |
(define make-modular-row-reduce |
| 477 |
(lambda (modulus) |
| 478 |
((make-modular modulus) |
| 479 |
make-row-reduce))) |
| 480 |
|
| 481 |
|
| 482 |
(define make-modular-in-row-space? |
| 483 |
(lambda (modulus) |
| 484 |
((make-modular modulus) |
| 485 |
make-in-row-space?))) |
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
; Usual utilities. |
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
; Given a bound, find a prime greater than the bound. |
| 494 |
(define find-prime |
| 495 |
(lambda (bound) |
| 496 |
(let* ((primes |
| 497 |
(list 2)) |
| 498 |
(last |
| 499 |
(chez-box primes)) |
| 500 |
(is-next-prime? |
| 501 |
(lambda (trial) |
| 502 |
(let _-*- |
| 503 |
((primes |
| 504 |
primes)) |
| 505 |
(or (null? primes) |
| 506 |
(let ((p |
| 507 |
(car primes))) |
| 508 |
(or (< trial (* p p)) |
| 509 |
(and (not (zero? (modulo trial p))) |
| 510 |
(_-*- (cdr primes)))))))))) |
| 511 |
(if (> 2 bound) |
| 512 |
2 |
| 513 |
(let _-*- |
| 514 |
((trial |
| 515 |
3)) |
| 516 |
(if (is-next-prime? trial) |
| 517 |
(let ((entry |
| 518 |
(list trial))) |
| 519 |
(set-cdr! (chez-unbox last) entry) |
| 520 |
(chez-set-box! last entry) |
| 521 |
(if (> trial bound) |
| 522 |
trial |
| 523 |
(_-*- (+ trial 2)))) |
| 524 |
(_-*- (+ trial 2)))))))) |
| 525 |
|
| 526 |
; Given the size of a square matrix consisting only of +1's and -1's, |
| 527 |
; return an upper bound on the determinant. |
| 528 |
(define det-upper-bound |
| 529 |
(lambda (size) |
| 530 |
(let ((main-part |
| 531 |
(expt size |
| 532 |
(quotient size 2)))) |
| 533 |
(if (even? size) |
| 534 |
main-part |
| 535 |
(* main-part |
| 536 |
(do ((i 0 (+ i 1))) |
| 537 |
((>= (* i i) size) |
| 538 |
i))))))) |
| 539 |
|
| 540 |
; Fold over all maximal matrices. |
| 541 |
(define go |
| 542 |
(lambda (number-of-cols inv-size folder state) |
| 543 |
(let* ((in-row-space? |
| 544 |
(make-modular-in-row-space? |
| 545 |
(find-prime |
| 546 |
(det-upper-bound inv-size)))) |
| 547 |
(make-tester |
| 548 |
(lambda (mat) |
| 549 |
(let ((tests |
| 550 |
(let ((old-mat |
| 551 |
(cdr mat)) |
| 552 |
(new-row |
| 553 |
(car mat))) |
| 554 |
(fold-over-subs-of-size old-mat |
| 555 |
(- inv-size 2) |
| 556 |
(lambda (sub tests) |
| 557 |
(cons |
| 558 |
(in-row-space? |
| 559 |
(cons new-row sub)) |
| 560 |
tests)) |
| 561 |
'())))) |
| 562 |
(lambda (row) |
| 563 |
(let _-*- |
| 564 |
((tests |
| 565 |
tests)) |
| 566 |
(and (not (null? tests)) |
| 567 |
(or ((car tests) row) |
| 568 |
(_-*- (cdr tests))))))))) |
| 569 |
(all-rows ; all rows starting with +1 in decreasing order |
| 570 |
(fold |
| 571 |
(fold-over-rows (- number-of-cols 1) |
| 572 |
cons |
| 573 |
'()) |
| 574 |
(lambda (row rows) |
| 575 |
(cons (cons 1 row) |
| 576 |
rows)) |
| 577 |
'()))) |
| 578 |
(let _-*- |
| 579 |
((number-of-rows |
| 580 |
1) |
| 581 |
(rev-mat |
| 582 |
(list |
| 583 |
(car all-rows))) |
| 584 |
(possible-future |
| 585 |
(cdr all-rows)) |
| 586 |
(state |
| 587 |
state)) |
| 588 |
(let ((zulu-future |
| 589 |
(remove-in-order |
| 590 |
(if (< number-of-rows inv-size) |
| 591 |
(in-row-space? rev-mat) |
| 592 |
(make-tester rev-mat)) |
| 593 |
possible-future))) |
| 594 |
(if (null? zulu-future) |
| 595 |
(folder (reverse rev-mat) |
| 596 |
state) |
| 597 |
(let _-**- |
| 598 |
((zulu-future |
| 599 |
zulu-future) |
| 600 |
(state |
| 601 |
state)) |
| 602 |
(if (null? zulu-future) |
| 603 |
state |
| 604 |
(let ((rest-of-future |
| 605 |
(cdr zulu-future))) |
| 606 |
(_-**- rest-of-future |
| 607 |
(let* ((first |
| 608 |
(car zulu-future)) |
| 609 |
(new-rev-mat |
| 610 |
(cons first rev-mat))) |
| 611 |
(if (maximal? (reverse new-rev-mat)) |
| 612 |
(_-*- (+ number-of-rows 1) |
| 613 |
new-rev-mat |
| 614 |
rest-of-future |
| 615 |
state) |
| 616 |
state)))))))))))) |
| 617 |
|
| 618 |
(define go-folder |
| 619 |
(lambda (mat bsize.blen.blist) |
| 620 |
(let ((bsize |
| 621 |
(car bsize.blen.blist)) |
| 622 |
(size |
| 623 |
(length mat))) |
| 624 |
(if (< size bsize) |
| 625 |
bsize.blen.blist |
| 626 |
(let ((blen |
| 627 |
(cadr bsize.blen.blist)) |
| 628 |
(blist |
| 629 |
(cddr bsize.blen.blist))) |
| 630 |
(if (= size bsize) |
| 631 |
(let ((blen |
| 632 |
(+ blen 1))) |
| 633 |
; (if |
| 634 |
; (let _-*- |
| 635 |
; ((blen |
| 636 |
; blen)) |
| 637 |
; (or (< blen 10) |
| 638 |
; (and (zero? (remainder blen 10)) |
| 639 |
; (_-*- (quotient blen 10))))) |
| 640 |
; |
| 641 |
; (begin |
| 642 |
; (display blen) |
| 643 |
; (display " of size ") |
| 644 |
; (display bsize) |
| 645 |
; (newline))) |
| 646 |
|
| 647 |
(cons bsize |
| 648 |
(cons blen |
| 649 |
(cond ((< blen 3000) |
| 650 |
(cons mat blist)) |
| 651 |
((= blen 3000) |
| 652 |
(cons "..." blist)) |
| 653 |
(else |
| 654 |
blist))))) |
| 655 |
; (begin |
| 656 |
; (newline) |
| 657 |
; (display "First of size ") |
| 658 |
; (display size) |
| 659 |
; (display ":") |
| 660 |
; (newline) |
| 661 |
; (for-each |
| 662 |
; (lambda (row) |
| 663 |
; (display " ") |
| 664 |
; (for-each |
| 665 |
; (lambda (e) |
| 666 |
; (case e |
| 667 |
; ((1) |
| 668 |
; (display " 1")) |
| 669 |
; ((-1) |
| 670 |
; (display " -1")))) |
| 671 |
; row) |
| 672 |
; (newline)) |
| 673 |
; mat) |
| 674 |
|
| 675 |
(list size 1 mat))))))) |
| 676 |
|
| 677 |
(define really-go |
| 678 |
(lambda (number-of-cols inv-size) |
| 679 |
(cddr |
| 680 |
(go number-of-cols |
| 681 |
inv-size |
| 682 |
go-folder |
| 683 |
(list -1 -1))))) |
| 684 |
|
| 685 |
(define remove-in-order |
| 686 |
(lambda (remove? lst) |
| 687 |
(reverse |
| 688 |
(fold lst |
| 689 |
(lambda (e lst) |
| 690 |
(if (remove? e) |
| 691 |
lst |
| 692 |
(cons e lst))) |
| 693 |
'())))) |
| 694 |
|
| 695 |
; The first fold-over-rows is slower than the second one, but folds |
| 696 |
; over rows in lexical order (large to small). |
| 697 |
(define fold-over-rows |
| 698 |
(lambda (number-of-cols folder state) |
| 699 |
(if (zero? number-of-cols) |
| 700 |
(folder '() |
| 701 |
state) |
| 702 |
(fold-over-rows (- number-of-cols 1) |
| 703 |
(lambda (tail state) |
| 704 |
(folder (cons -1 tail) |
| 705 |
state)) |
| 706 |
(fold-over-rows (- number-of-cols 1) |
| 707 |
(lambda (tail state) |
| 708 |
(folder (cons 1 tail) |
| 709 |
state)) |
| 710 |
state))))) |
| 711 |
|
| 712 |
; Fold over subsets of a given size. |
| 713 |
(define fold-over-subs-of-size |
| 714 |
(lambda (universe size folder state) |
| 715 |
(let ((usize |
| 716 |
(length universe))) |
| 717 |
(if (< usize size) |
| 718 |
state |
| 719 |
(let _-*- |
| 720 |
((size |
| 721 |
size) |
| 722 |
(universe |
| 723 |
universe) |
| 724 |
(folder |
| 725 |
folder) |
| 726 |
(csize |
| 727 |
(- usize size)) |
| 728 |
(state |
| 729 |
state)) |
| 730 |
(cond ((zero? csize) |
| 731 |
(folder universe state)) |
| 732 |
((zero? size) |
| 733 |
(folder '() state)) |
| 734 |
(else |
| 735 |
(let ((first-u |
| 736 |
(car universe)) |
| 737 |
(rest-u |
| 738 |
(cdr universe))) |
| 739 |
(_-*- size |
| 740 |
rest-u |
| 741 |
folder |
| 742 |
(- csize 1) |
| 743 |
(_-*- (- size 1) |
| 744 |
rest-u |
| 745 |
(lambda (tail state) |
| 746 |
(folder (cons first-u tail) |
| 747 |
state)) |
| 748 |
csize |
| 749 |
state)))))))))) |
| 750 |
|
| 751 |
(define (main) |
| 752 |
(run-benchmark |
| 753 |
"matrix" |
| 754 |
matrix-iters |
| 755 |
(lambda (result) |
| 756 |
(equal? result |
| 757 |
'(((1 1 1 1 1) (1 1 1 1 -1) (1 1 1 -1 1) |
| 758 |
(1 1 -1 -1 -1) (1 -1 1 -1 -1) (1 -1 -1 1 1)) |
| 759 |
((1 1 1 1 1) (1 1 1 1 -1) (1 1 1 -1 1) |
| 760 |
(1 1 -1 1 -1) (1 -1 1 -1 -1) (1 -1 -1 1 1)) |
| 761 |
((1 1 1 1 1) (1 1 1 1 -1) (1 1 1 -1 1) |
| 762 |
(1 1 -1 1 -1) (1 -1 1 -1 1) (1 -1 -1 1 1)) |
| 763 |
((1 1 1 1 1) (1 1 1 1 -1) (1 1 1 -1 1) |
| 764 |
(1 1 -1 1 1) (1 -1 1 1 -1) (1 -1 -1 -1 1)) |
| 765 |
((1 1 1 1 1) (1 1 1 1 -1) (1 1 1 -1 1) |
| 766 |
(1 1 -1 1 1) (1 -1 1 1 1) (1 -1 -1 -1 -1))))) |
| 767 |
(lambda (number-of-cols inv-size) (lambda () (really-go number-of-cols inv-size))) |
| 768 |
5 |
| 769 |
5))) |