-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.el
255 lines (191 loc) · 6.26 KB
/
vector.el
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
(require 'list)
(require 'complex)
(defconst vector/default-number-of-elements 5)
(defun vector/make-linspace-real (start stop &optional n accu)
"Return linspace range from START to STOP, both included.
The optional parameter N specifies the number of elements in the range.
By default N is set to `vector/default-number-of-elements'.
(see `push->')"
(let ((acc accu)
num
step newstop)
(when (eq nil accu)
(setq acc (list stop)))
(if (eq nil n)
(setq num (float vector/default-number-of-elements))
;; be careful by the true division:
;; (/ 1 2) returns 0 but (/ 1 2.0) return 0.5
(setq num (float n)))
(setq step (/ (- stop start) (- num 1)))
(setq newstop (- stop step))
(if (>= start stop)
acc
(vector/make-linspace-real start newstop (- num 1) (push-> newstop acc)))
))
(defun vector/make-linspace (start stop &optional n)
"Return complexified linspace range.
(see `vector/make-linspace-real')
(see `complex/ify')
(see `map-reverse')"
(map-reverse 'complex/ify (vector/make-linspace-real start stop n)))
(defun vector/make-value-real (&optional n value accumulate)
"Return vector filled by N VALUEs elements
By default N is set to `vector/default-number-of-elements',
and VAL is set to 1.
(see `push->')"
(let ((acc accumulate)
(num n)
(val value)
w)
(when (eq val nil)
(setq val 1.0))
(when (eq num nil)
(setq num vector/default-number-of-elements))
(when (eq acc nil)
(setq acc (list val)))
(setq w val)
(if (eq num 1)
acc
(vector/make-value-real (- num 1) w (push-> w acc)))
))
(defun vector/make-value (&optional n value)
"Return complexified VALUEs.
(see `vector/make-value-real')
(see `complex/ify')
(see `map')"
;; `map-reverse' should be used, as in `vector/make-linspace'
;; but, since it is only VAL, it is an useless extra cost.
(map 'complex/ify (vector/make-value-real n value)))
;; (defun vector/make-ones (&optional n value)
;; (let ((val value))
;; (when (eq value nil)
;; (setq val 1))
;; (map '(lambda (x) (complex/mul value x)) (vector/make-value-real n))
;; ))
(defun vector/make-ones (&optional n)
"Return complexified vector filled by 1..
(see `vector/make-value-real')
(see `complex/ify')
(see `map')"
;; `map-reverse' should be used, as in `vector/make-linspace'
;; but, since it is only VAL, it is an useless extra cost.
(map 'complex/ify (vector/make-value-real n)))
(defun vector/make-ith-real (i &optional n accumulate)
"Return a vector of length N filled by zeros,
except at the Ith which is filled by 1.
Index I starts at one.
N is set by default to `vector/default-number-of-elements'
ACCUMULATE is optionnal and set by default to `nil'.
Example:
(vector/make-ith-real 2 7)
--> (0 1 0 0 0 0 0)
(see `push->')"
(let ((acc accumulate)
(num n)
(val 0))
(when (eq num nil)
(setq num vector/default-number-of-elements))
(when (eq i num)
(setq val 1))
(when (eq acc nil)
(setq acc '()))
(setq acc (push-> val acc))
(if (eq num 1)
acc
(vector/make-ith-real i (- num 1) acc))
))
(defun vector/make-ith (i &optional n)
(map-reverse 'complex/ify (vector/make-ith-real i n)))
(defun vector/make-ith-val-real (i val &optional n accumulate)
"Return a vector of length N filled by zeros,
except at the Ith which is filled by 1.
Index I starts at one.
N is set by default to `vector/default-number-of-elements'
ACCUMULATE is optionnal and set by default to `nil'.
Example:
(vector/make-ith-real 2 7)
--> (0 1 0 0 0 0 0)
(see `push->')"
(let ((acc accumulate)
(num n)
(vall 0))
(when (eq num nil)
(setq num vector/default-number-of-elements))
(when (eq i num)
(setq vall val))
(when (eq acc nil)
(setq acc '()))
(setq acc (push-> vall acc))
(if (eq num 1)
acc
(vector/make-ith-val-real i val (- num 1) acc))
))
(defun vector/make-ith-val (i val &optional n)
(map-reverse 'complex/ify (vector/make-ith-val-real i val n) t))
(defun vector/dot-ouch (x y &optional accu)
"Compute the inner product and return it as `complex'.
Work for all datatypes supported by `complex'.
This function walks recursively through the nil-terminated lists X and Y.
If the lists X and Y does not have the same length (see `get-length'),
then no one warning is raised,
and the returned value corresponds to the inner prodcut of the common length."
(let ((acc accu)
(xtail (cdr x))
(ytail (cdr y))
(xhead (car x))
(yhead (car y))
val)
(when (eq nil accu)
(setq acc 0))
(setq val (complex/add acc (complex/mul xhead yhead)))
(if (or
(eq nil xtail)
(eq nil ytail))
val
(vector/dot-ouch xtail ytail val))
))
(defun vector/dot (x y)
"Compute by Map/Reduce the inner product and return it as `complex'.
Work for all datatypes supported by `complex'.
If the lists X and Y does not have the same length (see `get-length'),
then no one warning is raised,
and the returned value corresponds to the inner prodcut of the common length.
(see `map' and `reduce')
Example:
(setq x (vector/make-linspace 1 5))
(setq y (vector/make-linspace -5 -1))
(vector/dot x y)"
(reduce 'complex/add
(map 'complex/mul (combine-reversed x y))))
(defun vector/abs (vec)
(map-reverse 'complex/abs vec t))
(defun vector/norm2 (x)
(vector/dot x x))
(defun vector/norm (x)
(math/sqrt (complex/real (vector/norm2 x))))
(defun vector/set-i (vec i value &optional accumulate current)
(let ((acc accumulate)
(cur current)
(val (car vec)))
(when (eq cur nil)
(setq cur 1))
(when (eq i cur)
(setq val value))
(setq acc (push-> val acc))
(if (eq (cdr vec) nil)
(map 'complex/ify acc t)
(vector/set-i (cdr vec) i value acc (+ cur 1)))
))
(defun vector/get-i (vec index)
(get-ith vec index))
(defun vector/map (func x)
;; incosistent between list.el and here.
;; Still because map is a better name ?
(reverse-map func x t))
(defun vector/map-binary (func x y)
(reverse-map-binary func x y t))
(defun vector/add (x y)
(vector/map-binary 'complex/add x y))
(defun vector/sub (x y)
(vector/map-binary 'complex/sub x y))
(provide 'vector)