source: CGBLisp/src/RCS/makelist.lisp,v@ 1

Last change on this file since 1 was 1, checked in by Marek Rychlik, 15 years ago

First import of a version circa 1997.

File size: 5.7 KB
Line 
1head 1.4;
2access;
3symbols;
4locks; strict;
5comment @;;; @;
6
7
81.4
9date 2009.01.23.10.45.43; author marek; state Exp;
10branches;
11next 1.3;
12
131.3
14date 2009.01.23.10.43.25; author marek; state Exp;
15branches;
16next 1.2;
17
181.2
19date 2009.01.22.04.03.29; author marek; state Exp;
20branches;
21next 1.1;
22
231.1
24date 2009.01.19.07.51.00; author marek; state Exp;
25branches;
26next ;
27
28
29desc
30@@
31
32
331.4
34log
35@*** empty log message ***
36@
37text
38@#|
39 $Id: makelist.lisp,v 1.3 2009/01/23 10:43:25 marek Exp marek $
40 *--------------------------------------------------------------------------*
41 | Copyright (C) 1994, Marek Rychlik (e-mail: rychlik@@math.arizona.edu) |
42 | Department of Mathematics, University of Arizona, Tucson, AZ 85721 |
43 | |
44 | Everyone is permitted to copy, distribute and modify the code in this |
45 | directory, as long as this copyright note is preserved verbatim. |
46 *--------------------------------------------------------------------------*
47|#
48
49(defpackage "MAKELIST"
50 (:use "COMMON-LISP")
51 (:export makelist-1 makelist list-of set-of union-of select sum summation difference standard-vector))
52
53(in-package "MAKELIST")
54
55#+debug(proclaim '(optimize (speed 0) (debug 3)))
56#-debug(proclaim '(optimize (speed 3) (debug 0)))
57
58
59;; Macros for making lists with iterators - an exammple of GENSYM
60;; MAKELIST-1 makes a list with one iterator, while MAKELIST accepts an
61;; arbitrary number of iterators
62
63;; Sample usage:
64;; Without a step:
65;; >(makelist-1 (* 2 i) i 0 10)
66;; (0 2 4 6 8 10 12 14 16 18 20)
67;; With a step of 3:
68;; >(makelist-1 (* 2 i) i 0 10 3)
69;; (0 6 12 18)
70
71;; Generate sums of squares of numbers between 1 and 4:
72;; >(makelist (+ (* i i) (* j j)) (i 1 4) (j 1 i))
73;; (2 5 8 10 13 18 17 20 25 32)
74;; >(makelist (list i j '---> (+ (* i i) (* j j))) (i 1 4) (j 1 i))
75;; ((1 1 ---> 2) (2 1 ---> 5) (2 2 ---> 8) (3 1 ---> 10) (3 2 ---> 13)
76;; (3 3 ---> 18) (4 1 ---> 17) (4 2 ---> 20) (4 3 ---> 25) (4 4 ---> 32))
77
78;; Summation with SUM:
79;; Sum of squares of integers from 1 to 10:
80;; >(sum (expt i 2) (i 1 10))
81;; 385
82;; Sum of inverses of integers from 1 to 50:
83;; >(sum (/ n) (n 1 50))
84;; 13943237577224054960759/3099044504245996706400
85;; Sum of 1/(m^2+n^2) where m,n vary from 1 to 10:
86;; >(sum (/ (+ (expt m 2) (expt n 2))) (n 1 10) (m 1 10))
87;; 125085870045079516933345908893314157/42204464874461454985621846571472000
88;; Sum of 1/(m^2+n^2) where m,n vary from 1 to 10 and m>n:
89;; >(sum (/ (+ (expt m 2) (expt n 2))) (n 1 10) (m 1 (1- n)))
90;; 13092731173226115661182811487147/11962716801151206061684196874000
91
92;; Evaluate expression expr with variable set to lo, lo+1,... ,hi
93;; and put the results in a list.
94(defmacro makelist-1 (expr var lo hi &optional (step 1))
95 (let ((l (gensym)))
96 `(do ((,var ,lo (+ ,var ,step))
97 (,l nil (cons ,expr ,l)))
98 ((> ,var ,hi) (reverse ,l))
99 (declare (fixnum ,var)))))
100
101(defmacro makelist (expr (var lo hi &optional (step 1)) &rest more)
102 (if (endp more)
103 `(makelist-1 ,expr ,var ,lo ,hi ,step)
104 (let* ((l (gensym)))
105 `(do ((,var ,lo (+ ,var ,step))
106 (,l nil (nconc ,l `,(makelist ,expr ,@@more))))
107 ((> ,var ,hi) ,l)
108 (declare (fixnum ,var))))))
109
110(defmacro sum (&body body)
111 `(reduce #'+ (makelist ,@@body)))
112
113(defmacro summation (&body body)
114 ``(+ ,@@(makelist ,@@body)))
115
116(defmacro difference (&body body)
117 ``(- ,@@(makelist ,@@body)))
118
119;; List of all EXPR where VAR varies over the list LST
120(defmacro list-of (expr (var lst) &rest more)
121 (if (endp more)
122 `(list-of-1 ,expr ,var ,lst)
123 (let ((l (gensym)))
124 `(let ((,l))
125 (dolist (,var ,lst ,l)
126 (setf ,l (nconc ,l ,`(list-of ,expr ,@@more))))))))
127
128(defmacro list-of-1 (expr var lst)
129 (let ((l (gensym)))
130 `(let ((,l))
131 (dolist (,var ,lst (reverse ,l))
132 (setf ,l ,`(cons ,expr ,l))))))
133
134;; Union of all EXPR where VAR varies over the list LST
135(defmacro union-of (expr (var lst) &rest more)
136 (if (endp more)
137 `(union-of-1 ,expr ,var ,lst)
138 (let ((l (gensym)))
139 `(let ((,l))
140 (dolist (,var ,lst ,l)
141 (setf ,l (union ,l ,`(union-of ,expr ,@@more) :test #'equalp)))))))
142
143(defmacro union-of-1 (expr var lst)
144 (let ((l (gensym)))
145 `(let ((,l))
146 (dolist (,var ,lst (reverse ,l))
147 (setf ,l ,`(union ,expr ,l :test #'equalp))))))
148
149
150;; Set of all EXPR where VAR varies over the list LST
151(defmacro set-of (expr (var lst) &rest more)
152 (if (endp more)
153 `(set-of-1 ,expr ,var ,lst)
154 (let ((l (gensym)))
155 `(let ((,l))
156 (dolist (,var ,lst ,l)
157 (setf ,l (union ,l ,`(set-of ,expr ,@@more) :test #'equalp)))))))
158
159(defmacro set-of-1 (expr var lst)
160 (let ((l (gensym)))
161 `(let ((,l))
162 (dolist (,var ,lst (reverse ,l))
163 (pushnew ,expr ,l :test #'equalp)))))
164
165;; sublist of LST consisting of elements with indecies in IND
166(defun select (ind lst)
167 (cond
168 ((endp ind) nil)
169 (t (cons (elt lst (car ind)) (select (cdr ind) lst)))))
170
171(defun standard-vector (n k &optional (coeff 1)
172 &aux (v (make-list n :initial-element 0)))
173 "Returns vector (0 0 ... 1 ... 0 0) of length N, where 1 appears on K-th place."
174 (setf (elt v k) coeff)
175 v)
176@
177
178
1791.3
180log
181@*** empty log message ***
182@
183text
184@d2 1
185a2 1
186 $Id: makelist.lisp,v 1.2 2009/01/22 04:03:29 marek Exp marek $
187d14 1
188a14 1
189 (:export makelist-1 makelist list-of set-of union-of select sum summation difference std-vector))
190d134 1
191a134 1
192(defun std-vector (n k &optional (coeff 1)
193@
194
195
1961.2
197log
198@*** empty log message ***
199@
200text
201@d2 1
202a2 1
203 $Id$
204d14 1
205a14 1
206 (:export makelist-1 makelist list-of set-of union-of select sum summation difference standard-vector))
207d134 1
208a134 1
209(defun standard-vector (n k &optional (coeff 1)
210@
211
212
2131.1
214log
215@Initial revision
216@
217text
218@d2 1
219a2 1
220 $Id: makelist.lisp,v 1.12 2001/11/06 05:54:25 marek Exp $
221d18 2
222a19 1
223(proclaim '(optimize (speed 0) (debug 3)))
224@
Note: See TracBrowser for help on using the repository browser.