1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
#| -*- Scheme -*-
Copyright (c) 1987, 1988, 1989, 1990, 1991, 1995, 1997, 1998,
1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014,
2015, 2016, 2017, 2018, 2019, 2020
Massachusetts Institute of Technology
This file is part of MIT scmutils.
MIT scmutils is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at
your option) any later version.
MIT scmutils is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with MIT scmutils; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301,
USA.
|#
;;;; Structure iterators
(declare (usual-integrations))
;;; Structural Lists
#|
(define (generate-list n proc) ; ==> ( (proc 0) (proc 1) ... (proc n-1) )
(let loop ((i (fix:- n 1)) (list '()))
(if (fix:< i 0)
list
(loop (fix:- i 1) (cons (proc i) list)))))
|#
(define generate-list make-initialized-list)
(define list:generate make-initialized-list)
(define (list-with-substituted-coord lst i x)
(append (list-head lst i)
(list x)
(cdr (list-tail lst i))))
;;; Structural Vectors
#|
;;; Scheme supplies
vector-ref
vector-set!
vector
make-initialized-vector
(define (generate-vector size proc)
(let ((ans (make-vector size)))
(let loop ((i 0))
(if (fix:= i size)
ans
(begin (vector-set! ans i (proc i))
(loop (fix:+ i 1)))))))
|#
(define generate-vector make-initialized-vector)
(define ((vector-elementwise f) . vectors)
(make-initialized-vector
(vector-length (car vectors))
(lambda (i)
(apply f
(map (lambda (v) (vector-ref v i))
vectors)))))
(define (vector-forall p? . vectors)
(let lp ((i (fix:- (vector-length (car vectors)) 1)))
(cond ((fix:= i 0)
(apply p? (map (lambda (v) (vector-ref v i))
vectors)))
((apply p? (map (lambda (v) (vector-ref v i))
vectors))
(lp (fix:- i 1)))
(else #f))))
(define (vector-exists p? . vectors)
(let lp ((i (fix:- (vector-length (car vectors)) 1)))
(cond ((fix:= i 0)
(apply p? (map (lambda (v) (vector-ref v i))
vectors)))
((apply p? (map (lambda (v) (vector-ref v i))
vectors))
#t)
(else
(lp (fix:- i 1))))))
(define (vector-accumulate acc fun init v)
(let ((l (vector-length v)))
(if (fix:= l 0)
init
(let loop ((i 1)
(ans (fun (vector-ref v 0))))
(if (fix:= i l)
ans
(loop (fix:+ i 1)
(acc ans (fun (vector-ref v i)))))))))
(define (vector-with-substituted-coord v i x)
(make-initialized-vector (vector-length v)
(lambda (j)
(if (fix:= j i)
x
(vector-ref v j)))))
;;; Structural 2-dimensional arrays
;;; Structrure procedures -- operate on raw array material
(define (array-ref m i j)
(vector-ref (vector-ref m i) j))
(define (array-set! m i j v)
(vector-set! (vector-ref m i) j v))
(define (generate-array rows cols proc)
(make-initialized-vector
rows
(lambda (row)
(make-initialized-vector
cols
(lambda (col)
(proc row col))))))
(define ((array-elementwise f) . arrays)
(generate-array
(num-rows (car arrays))
(num-cols (car arrays))
(lambda (i j)
(apply f
(map (lambda (m)
(array-ref m i j))
arrays)))))
(define (array-copy m)
(generate-array (num-rows m) (num-cols m)
(lambda (i j) (array-ref m i j))))
(define (num-rows array)
(vector-length array))
(define (num-cols array)
(vector-length (vector-ref array 0)))
(define (nth-row M n) ;return as a vector
(vector-ref M n))
(define (nth-col M j)
(generate-vector (num-rows M)
(lambda (i)
(array-ref M i j))))
(define (array-with-substituted-row A i V)
(vector-with-substituted-coord A i V))
(define (array-with-substituted-col A k V)
(generate-array (num-rows A) (num-cols A)
(lambda (i j)
(if (fix:= j k)
(vector-ref v i)
(array-ref A i j)))))
(define (array-by-rows M)
(apply vector (map list->vector M)))
(define (array-by-cols M)
(transpose-array (array-by-rows M)))
(define (transpose-array M)
(generate-array (num-cols M) (num-rows M)
(lambda (i j) (array-ref M j i))))
|