-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsimple-vector-ops.lisp
More file actions
166 lines (130 loc) · 3.91 KB
/
simple-vector-ops.lisp
File metadata and controls
166 lines (130 loc) · 3.91 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
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
(in-package #:com.ral.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)
|#
(defun vlimit> (v lim>)
(map 'vector (um:curry #'max lim>) v))
(defun vlimit< (v lim<)
(map 'vector (um:curry #'min lim<) v))
(defun clip (x lim< lim>)
(let ((vmax (max lim< lim>))
(vmin (min lim< lim>)))
(max vmin (min vmax x))))
(defun vclip (v lim< lim>)
(let ((vmax (max lim< lim>))
(vmin (min lim< lim>)))
(map 'vector (um:compose (um:curry #'min vmax)
(um:curry #'max vmin))
v)))
(defun wrap (x lim< lim>)
(let* ((vmax (max lim< lim>))
(vmin (min lim< lim>))
(diff (- vmax vmin)))
(+ vmin (mod (- x vmin) diff))))
(defun vwrap (v lim< lim>)
(let* ((vmax (max lim< lim>))
(vmin (min lim< lim>))
(diff (- vmax vmin)))
(map 'vector (lambda (x)
(+ vmin (mod (- x vmin) diff)))
v)))
(defun extrema (&rest args)
(values (apply #'min args)
(apply #'max args)))
(defun vextrema (v)
(let* ((vmin (aref v 0))
(vmax vmin))
(map nil (lambda (x)
(setf vmin (min vmin x)
vmax (max vmax x)))
v)
(values vmin vmax)))