Debian Codegraph @

benchmarks.larceny/src/matrix.scm

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