-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvector-functions.lisp
344 lines (288 loc) · 12.1 KB
/
vector-functions.lisp
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
;; -*- Mode:Lisp; Syntax:Common-Lisp; Package: (*SIM-I COMMON-LISP-GLOBAL); Muser: Yes -*-
(in-package :*sim-i)
;;;> *+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+
;;;>
;;;> The Thinking Machines *Lisp Simulator is in the public domain.
;;;> You are free to do whatever you like with it, including but
;;;> not limited to distributing, modifying, and copying.
;;;> Bugs, comments and revisions due to porting can be sent to:
;;;> bug-starlisp@think.com. Other than to Thinking Machines'
;;;> customers, no promise of support is intended or implied.
;;;>
;;;> *+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+
;;; Author: JP Massar.
(defun new-vector-pvar-check (pvar function)
(new-pvar-check pvar function)
(assert (vector-pvar-p pvar) () "~S: The argument, ~S, is not a vector pvar" function pvar)
)
(defun vector-lengths-all-same-check (&rest vector-pvars)
(if (zerop (length vector-pvars))
t
(let ((length (array-pvar-total-size (car vector-pvars))))
(every #'(lambda (x) (= length (array-pvar-total-size x))) (cdr vector-pvars))
)))
(*defun *vset-components (vector-pvar &rest component-pvars)
(simple-pvar-argument!! &rest component-pvars) ;;; can't modify a temporary
(let ((length (length component-pvars)))
(safety-check
(new-vector-pvar-check vector-pvar '*vset-components)
(new-multiple-pvar-check component-pvars '*vset-components)
(assert (or (eql length 1) (eql length (array-pvar-total-size vector-pvar))) ()
"~S: The number of components, ~D, does not agree with the length of the vector pvar, ~D"
'*vset-components length (array-pvar-total-size vector-pvar)
))
(let ((count 0))
(*map
#'(lambda (x) (*set x (if (eql 1 length) (car component-pvars) (nth count component-pvars))) (incf count))
vector-pvar
))
))
(defun v+-*-args-check (function-name vector-pvar more-vector-pvars)
(safety-check
(new-vector-pvar-check vector-pvar function-name)
(mapcar #'(lambda (pvar) (new-vector-pvar-check pvar function-name)) more-vector-pvars)
(apply #'vector-lengths-all-same-check vector-pvar more-vector-pvars)
))
(defun v+!! (vector-pvar &rest more-vector-pvars)
(simple-pvar-argument!! vector-pvar &rest more-vector-pvars)
(v+-*-args-check 'v+!! vector-pvar more-vector-pvars)
(*let ((dest-pvar vector-pvar))
(apply
'*sim::*defun-*map
#'(lambda (dest &rest sources)
(*set dest (apply #'+!! dest sources))
)
dest-pvar
more-vector-pvars
)
dest-pvar
))
(defun v-!! (vector-pvar &rest more-vector-pvars)
(simple-pvar-argument!! vector-pvar &rest more-vector-pvars)
(v+-*-args-check 'v-!! vector-pvar more-vector-pvars)
(*let ((dest-pvar vector-pvar))
(apply
'*sim::*defun-*map
#'(lambda (dest &rest sources)
(if sources
(*set dest (+!! dest (-!! (apply '+!! sources))))
(*set dest (-!! dest))
))
dest-pvar
more-vector-pvars
)
dest-pvar
))
(defun v*!! (vector-pvar &rest more-vector-pvars)
(simple-pvar-argument!! vector-pvar &rest more-vector-pvars)
(v+-*-args-check 'v*!! vector-pvar more-vector-pvars)
(*let ((dest-pvar vector-pvar))
(apply
'*sim::*defun-*map
#'(lambda (dest &rest sources)
(*set dest (apply #'*!! dest sources))
)
dest-pvar
more-vector-pvars
)
dest-pvar
))
(defun v/!! (vector-pvar &rest more-vector-pvars)
(simple-pvar-argument!! vector-pvar &rest more-vector-pvars)
(v+-*-args-check 'v/!! vector-pvar more-vector-pvars)
(*let ((dest-pvar vector-pvar))
(apply
'*sim::*defun-*map
#'(lambda (dest &rest sources)
(*set dest (apply #'/!! dest sources))
)
dest-pvar
more-vector-pvars
)
dest-pvar
))
(defun dot-product!! (vector-pvar1 vector-pvar2)
(simple-pvar-argument!! (vector-pvar1 vector-pvar2))
(funcall 'v+-*-args-check 'dot-product!! vector-pvar1 (list vector-pvar2))
(*let ((result (!! 0)))
(let* ((result-pvar-array (pvar-array result))
(vector-pvar1-displaced-array (pvar-array-displaced-array vector-pvar1))
(vector-pvar2-displaced-array (pvar-array-displaced-array vector-pvar2))
(vector-length (length vector-pvar2-displaced-array))
(void t)
)
(with-simple-vectors (result-pvar-array vector-pvar1-displaced-array vector-pvar2-displaced-array)
(dotimes (element vector-length)
(let* ((element-pvar-1 (aref vector-pvar1-displaced-array element))
(element-pvar-2 (aref vector-pvar2-displaced-array element))
(element-array-1 (pvar-array element-pvar-1))
(element-array-2 (pvar-array element-pvar-2))
)
(do-for-selected-processors-internal (processor)
(setq void nil)
(incf (aref result-pvar-array processor)
(* (aref element-array-1 processor) (aref element-array-2 processor))
)))))
(when (not void) (make-non-void result))
result
)))
(defun cross-product!! (vector-pvar1 vector-pvar2)
(simple-pvar-argument!! vector-pvar1 vector-pvar2)
(funcall 'v+-*-args-check 'cross-product!! vector-pvar1 (list vector-pvar2))
(assert (eql 3 (*array-total-size vector-pvar1)) () "Cross-product!! only works in 3 dimensions")
(*let ((result vector-pvar1))
(let* ((result-pvar-displaced-array (pvar-array-displaced-array result))
(vector-pvar1-displaced-array (pvar-array-displaced-array vector-pvar1))
(vector-pvar2-displaced-array (pvar-array-displaced-array vector-pvar2))
)
(with-simple-vectors
(result-pvar-displaced-array vector-pvar1-displaced-array vector-pvar2-displaced-array)
(if (void-pvar-p result)
result
(let ((x1a (pvar-array (aref vector-pvar1-displaced-array 0)))
(y1a (pvar-array (aref vector-pvar1-displaced-array 1)))
(z1a (pvar-array (aref vector-pvar1-displaced-array 2)))
(x2a (pvar-array (aref vector-pvar2-displaced-array 0)))
(y2a (pvar-array (aref vector-pvar2-displaced-array 1)))
(z2a (pvar-array (aref vector-pvar2-displaced-array 2)))
(xdest (pvar-array (aref result-pvar-displaced-array 0)))
(ydest (pvar-array (aref result-pvar-displaced-array 1)))
(zdest (pvar-array (aref result-pvar-displaced-array 2)))
)
(with-simple-vectors (xdest ydest zdest x1a y1a z1a x2a y2a z2a)
(do-for-selected-processors-internal (processor)
(let ((x1 (aref x1a processor))
(y1 (aref y1a processor))
(z1 (aref z1a processor))
(x2 (aref x2a processor))
(y2 (aref y2a processor))
(z2 (aref z2a processor))
)
(setf (aref xdest processor) (- (* y1 z2) (* z1 y2)))
(setf (aref ydest processor) (- (* z1 x2) (* x1 z2)))
(setf (aref zdest processor) (- (* x1 y2) (* y1 x2)))
)))
result
))))))
(defun vabs-squared!! (vector-pvar) (dot-product!! vector-pvar vector-pvar))
(defun vabs!! (vector-pvar) (sqrt!! (vabs-squared!! vector-pvar)))
(defun vscale!! (vector-pvar scalar-pvar)
(simple-pvar-argument!! vector-pvar scalar-pvar)
(safety-check
(new-vector-pvar-check vector-pvar 'vscale!!)
(new-pvar-check scalar-pvar 'vscale!!)
)
(*let ((dest-pvar vector-pvar))
(funcall
'*sim::*defun-*map
#'(lambda (dest)
(*set dest (*!! dest scalar-pvar))
)
dest-pvar
)
dest-pvar
))
(defun vscale-to-unit-vector!! (vector-pvar)
(vscale!! vector-pvar (/!! (vabs!! vector-pvar)))
)
(*defun *sf-vset-components (vector-pvar &rest component-pvars)
(*apply '*vset-components vector-pvar component-pvars)
)
(defun general-sf-constant!! (function-name function vector-pvar scalar-pvar)
(safety-check
(new-vector-pvar-check vector-pvar function-name)
(new-pvar-check scalar-pvar function-name)
)
(*let ((dest-pvar vector-pvar))
(funcall
'*sim::*defun-*map
#'(lambda (dest)
(*set dest (funcall function dest scalar-pvar))
)
dest-pvar
)
dest-pvar
))
(defun sf-v+-constant!! (vector-pvar scalar-pvar)
(general-sf-constant!! 'sf-v+-constant!! '+!! vector-pvar scalar-pvar)
)
(defun sf-v--constant!! (vector-pvar scalar-pvar)
(general-sf-constant!! 'sf-v--constant!! '-!! vector-pvar scalar-pvar)
)
(defun sf-v*-constant!! (vector-pvar scalar-pvar)
(general-sf-constant!! 'sf-v*-constant!! '*!! vector-pvar scalar-pvar)
)
(defun sf-v/-constant!! (vector-pvar scalar-pvar)
(general-sf-constant!! 'sf-v/-constant!! '/!! vector-pvar scalar-pvar)
)
(*defun dsf-v+-constant!! (vdest vector-pvar scalar-pvar)
(*set vdest (general-sf-constant!! 'sf-v+-constant!! '+!! vector-pvar scalar-pvar))
vdest
)
(*defun dsf-v--constant!! (vdest vector-pvar scalar-pvar)
(*set vdest (general-sf-constant!! 'sf-v--constant!! '-!! vector-pvar scalar-pvar))
vdest
)
(*defun dsf-v*-constant!! (vdest vector-pvar scalar-pvar)
(*set vdest (general-sf-constant!! 'sf-v*-constant!! '*!! vector-pvar scalar-pvar))
vdest
)
(*defun dsf-v/-constant!! (vdest vector-pvar scalar-pvar)
(*set vdest (general-sf-constant!! 'sf-v/-constant!! '/!! vector-pvar scalar-pvar))
vdest
)
(defun sf-v+!! (vector-pvar &rest more-vector-pvars) (apply 'v+!! vector-pvar more-vector-pvars))
(defun sf-v-!! (vector-pvar &rest more-vector-pvars) (apply 'v-!! vector-pvar more-vector-pvars))
(defun sf-v*!! (vector-pvar &rest more-vector-pvars) (apply 'v*!! vector-pvar more-vector-pvars))
(*defun dsf-v+!! (vdest vector-pvar &rest more-vector-pvars)
(*set vdest (apply 'sf-v+!! vector-pvar more-vector-pvars))
vdest
)
(*defun dsf-v-!! (vdest vector-pvar &rest more-vector-pvars)
(*set vdest (apply 'sf-v-!! vector-pvar more-vector-pvars))
vdest
)
(*defun dsf-v*!! (vdest vector-pvar &rest more-vector-pvars)
(*set vdest (apply 'sf-v*!! vector-pvar more-vector-pvars))
vdest
)
(defun sf-vabs!! (vector-pvar) (vabs!! vector-pvar))
(defun sf-vabs-squared!! (vector-pvar) (vabs-squared!! vector-pvar))
(defun sf-dot-product!! (vector-pvar1 vector-pvar2) (dot-product!! vector-pvar1 vector-pvar2))
(defun sf-vscale!! (vector-pvar scalar-pvar) (vscale!! vector-pvar scalar-pvar))
(defun sf-vscale-to-unit-vector!! (vector-pvar) (vscale-to-unit-vector!! vector-pvar))
(defun sf-cross-product!! (vector-pvar1 vector-pvar2) (cross-product!! vector-pvar1 vector-pvar2))
(defun sf-vector-normal!! (vector-pvar1 vector-pvar2)
(sf-vscale-to-unit-vector!! (sf-cross-product!! vector-pvar1 vector-pvar2))
)
(*defun dsf-vscale!! (vdest vector-pvar scalar-pvar)
(*set vdest (sf-vscale!! vector-pvar scalar-pvar))
vdest
)
(*defun dsf-vscale-to-unit-vector!! (vdest vector-pvar)
(*set vdest (sf-vscale-to-unit-vector!! vector-pvar))
vdest
)
(*defun dsf-cross-product!! (vdest vector-pvar1 vector-pvar2)
(*set vdest (cross-product!! vector-pvar1 vector-pvar2))
vdest
)
(*defun dsf-vector-normal!! (vdest vector-pvar1 vector-pvar2)
(*set vdest (sf-vector-normal!! vector-pvar1 vector-pvar2))
vdest
)
(defun v+scalar!! (vector-pvar scalar-pvar)
(amap!! #'(lambda (vector-element-pvar) (+!! vector-element-pvar scalar-pvar))
vector-pvar))
(defun v-scalar!! (vector-pvar scalar-pvar)
(amap!! #'(lambda (vector-element-pvar) (-!! vector-element-pvar scalar-pvar))
vector-pvar))
(defun v*scalar!! (vector-pvar scalar-pvar)
(amap!! #'(lambda (vector-element-pvar) (*!! vector-element-pvar scalar-pvar))
vector-pvar))
(defun v/scalar!! (vector-pvar scalar-pvar)
(amap!! #'(lambda (vector-element-pvar) (/!! vector-element-pvar scalar-pvar))
vector-pvar))
(defun vector-normal!! (vector-pvar1 vector-pvar2)
(vscale-to-unit-vector!!
(cross-product!! vector-pvar1 vector-pvar2)))