forked from dbmcclain/vmath
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple-vector-ops.lisp
More file actions
121 lines (93 loc) · 2.78 KB
/
simple-vector-ops.lisp
File metadata and controls
121 lines (93 loc) · 2.78 KB
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
(in-package vector-ops)
;; --------------------------------------------------------
;; Vector Routines
(defun blit (src srcoff dst dstoff nel)
(loop for ix from 0 below nel do
(setf (aref dst (+ dstoff ix))
(aref src (max 0 (+ srcoff ix))))))
(defun vec (arr offs &optional (nel (- (length arr) offs)))
(if (and (zerop offs)
(= nel (length arr)))
arr
(make-array nel
:displaced-to arr
:displaced-index-offset offs
:element-type (array-element-type arr))))
(defun vlog (v)
(map 'vector #'log v))
(defun vexp (v)
(map 'vector #'exp v))
(defun vabs (v)
(map 'vector #'abs v))
(defun vsub (&rest vs)
(apply #'map 'vector #'- vs))
(defun vadd (&rest vs)
(apply #'map 'vector #'+ vs))
(defun vmul (&rest vs)
(apply #'map 'vector #'* vs))
(defun vdiv (&rest vs)
(apply #'map 'vector #'/ vs))
(defun vscale (sf v)
(map 'vector (lambda (x)
(* sf x))
v))
(defun voffset (offs v)
(map 'vector (lambda (x)
(+ offs x))
v))
(defun vmin (v)
(reduce #'min v))
(defun vmax (v)
(reduce #'max v))
(defun vsum (v)
(reduce #'+ v))
(defun vprod (v)
(reduce #'* v))
(defun vround (v &optional (unit 1))
(map 'vector (um:rcurry #'round unit) v))
(defun vfloor (v &optional (unit 1))
(map 'vector (um:rcurry #'floor unit) v))
(defun vceiling (v &optional (unit 1))
(map 'vector (um:rcurry #'ceiling unit) v))
(defun vsqr (v)
(map 'vector #'* v v))
(defun vsqrt (v)
(map 'vector #'sqrt v))
(defun vlog10 (v)
(map 'vector (um:rcurry #'log 10) v))
(defun vpow10 (v)
(map 'vector (um:curry #'expt 10) v))
(defun vexpt (v expon)
(map 'vector (um:rcurry #'expt expon) v))
(defun undot-the-list (lst &optional ans)
(cond ((null lst) (nreverse ans))
((consp lst)
(undot-the-list (rest lst) (cons (first lst) ans)))
(t (nreverse (cons lst (cons '&rest ans))))
))
(defmacro destructure-vector ((&rest args) vec &body body)
(let* ((v (gensym))
(ctr 0)
(udargs (undot-the-list args))
(rest-args (position '&rest udargs))
(first-args (if rest-args
(subseq udargs 0 rest-args)
udargs)))
`(let* ((,v ,vec)
,@(mapcar (lambda (arg)
(let ((ct ctr))
(incf ctr)
`(,arg (aref ,v ,ct))
))
first-args)
,@(if rest-args
(list `(,(nth (1+ rest-args) udargs) (vec ,v ,rest-args)))
))
,@body)
))
#|
;; test it out...
(destructure-vector (a b) vec-expr my-body)
(destructure-vector (a b &rest others) vec-expr my-body)
(destructure-vector (a b . c) vec-expr my-body)
|#