-
Notifications
You must be signed in to change notification settings - Fork 11
/
vector.py
459 lines (423 loc) · 14 KB
/
vector.py
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
from rpython.rtyper.lltypesystem import rffi
from rpython.rlib.rarithmetic import r_uint, r_ulonglong
from rpython.rlib import debug, jit, rerased
from space import *
#import vectormath
from rpython.rlib.objectmodel import import_from_mixin, specialize
class Numeric(Object):
_immutable_fields_ = ['strategy', 'storage']
def __init__(self, strategy, storage):
self.strategy = strategy
self.storage = storage
def getattr(self, name):
return self.strategy.getattr(self, name)
def getitem(self, index):
return self.strategy.getitem(self, index)
def iter(self):
return self.strategy.iter(self)
# This code doesn't need rerased, so it can be removed.
class FormStrategy(object):
pass
# The rationale in this weird system is that we would like to
# reuse the structures here for symbolic computation as well.
class CommonFormStrategy(object):
def getattr(self, numeric, name):
if name == u"length":
return Integer(rffi.r_long(len(self.unerase(numeric.storage))))
if name == u"strategy_name":
return self.name
return Object.getattr(numeric, name)
def getitem(self, numeric, index):
if isinstance(index, Integer):
ix = index.value
data = self.unerase(numeric.storage)
if ix < len(data):
return self.wrap(data[index.value])
else:
raise unwind(LError(u"out of bounds")) # TODO: change correct error in.
return Object.getitem(numeric, index)
def iter(self, numeric): # TODO: See if this has to be optimized.
out = []
for item in self.unerase(numeric.storage):
out.append(self.wrap(item))
return List(out).iter()
def make_from_list(self, sequence):
newlist = []
for item in sequence:
newlist.append( self.unwrap(item) )
return Numeric(self, self.erase(newlist[:]))
class ObjectFormStrategy(FormStrategy):
import_from_mixin(CommonFormStrategy)
# I am planning to add shape of the numeric into the strategy and
# store a table of used strategies with their associated shape.
# That should allow JIT to optimize common shapes encountered?
def __init__(self):
self.name = String(u"object")
erase, unerase = rerased.new_erasing_pair("object_form")
erase = staticmethod(erase)
unerase = staticmethod(unerase)
def wrap(self, raw):
return raw
def unwrap(self, obj):
return obj
def add(self, a_n, b_n):
a = self.unerase(a_n.storage)
b = self.unerase(b_n.storage)
if len(a) != len(b):
raise unwind(LError(u"incompatible numerics for arithmetic"))
c = []
for i in range(len(a)):
c.append(operators.add.call([a[i], b[i]]))
return Numeric(self, self.erase(c[:]))
class DoubleFormStrategy(FormStrategy):
import_from_mixin(CommonFormStrategy)
def __init__(self):
self.name = String(u"double")
erase, unerase = rerased.new_erasing_pair("double_form")
erase = staticmethod(erase)
unerase = staticmethod(unerase)
def wrap(self, raw):
return Float(raw)
def unwrap(self, obj):
return cast(obj, Float, u"numeric (double form strategy)").number
def add(self, a_n, b_n):
a = self.unerase(a_n.storage)
b = self.unerase(b_n.storage)
if len(a) != len(b):
raise unwind(LError(u"incompatible numerics for arithmetic"))
c = []
for i in range(len(a)):
c.append(a[i] + b[i])
return Numeric(self, self.erase(c[:]))
double_form_strategy = DoubleFormStrategy()
object_form_strategy = ObjectFormStrategy()
@Numeric.instantiator
@jit.unroll_safe
def Numeric_init(argv):
selected_strategy = double_form_strategy
for arg in argv:
if isinstance(arg, Integer) or isinstance(arg, Float):
continue
else:
selected_strategy = object_form_strategy
return selected_strategy.make_from_list(argv)
@operators.add.multimethod_s(Numeric, Numeric)
@jit.unroll_safe
def Numeric_add(self, other):
if self.strategy is other.strategy:
return self.strategy.add(self, other)
#if self.strategy is double_form_strategy and other.strategy is object_form_strategy:
# self = promote(self.strategy, object_form_strategy)
#if other.strategy is double_form_strategy and self.strategy is object_form_strategy:
# other = promote(other.strategy, object_form_strategy)
raise unwind(LError(u"Mixed add not implemented yet"))
# L = self.match_length(other)
# interface = self.match_interface(other)
# if isinstance(self, FVec) and isinstance(other, FVec):
# f_result = [0.0] * L
# for i in range(L):
# f_result[i] = self.fetch_f(i) + other.fetch_f(i)
# return compact_f(f_result)
# result = [null] * L
# for i in range(L):
# result[i] = operators.add.call([self.fetch(i), other.fetch(i)])
# return GVec(result, interface)
#class Vec(Object):
# def fetch(self, index):
# assert False, "abstract method"
# return null
#
# def get_length(self):
# assert False, "abstract method"
# return 0
#
# def match_length(self, other):
# L1 = self.get_length()
# L2 = other.get_length()
# if L1 != L2:
# raise OldError(u"vector size mismatch")
# return jit.promote(L1)
#
# def get_item_type(self):
# assert False, "abstract method"
# return null
#
# def match_interface(self, other):
# i1 = self.get_item_type()
# i2 = other.get_item_type()
# if i1 is i2:
# return i1
# raise OldError(u"vector element type mismatch")
#
# def getattr(self, name):
# if name == u"x":
# return self.fetch(0)
# if name == u"y":
# return self.fetch(1)
# if name == u"z":
# return self.fetch(2)
# if name == u"w":
# return self.fetch(3)
# if 2 <= len(name) <= 4:
# return letter_swizzle(self, name)
# if name == u"length":
# return Integer(rffi.r_long(self.get_length()))
# return Object.getattr(self, name)
#
# def iter(self): # TODO: See if this has to be optimized.
# out = []
# for i in range(self.get_length()):
# out.append(self.fetch(i))
# return List(out).iter()
#
#class FVec(Vec):
# interface = Vec.interface
# def fetch_f(self, index):
# assert False, "abstract method"
# return 0.0
#
# def fetch(self, index):
# return Float(self.fetch_f(index))
#
# def get_item_type(self):
# return Float.interface
#
#class FVec2(FVec):
# _immutable_fields_ = ['f0', 'f1']
# interface = Vec.interface
# def __init__(self, f0, f1):
# self.f0 = f0
# self.f1 = f1
#
# def fetch_f(self, index):
# if index == 0:
# return self.f0
# elif index == 1:
# return self.f1
# raise OldError(u"float vector access out of bounds")
#
# def get_length(self):
# return 2
#
#class FVec3(FVec):
# _immutable_fields_ = ['f0', 'f1', 'f2']
# interface = Vec.interface
# def __init__(self, f0, f1, f2):
# self.f0 = f0
# self.f1 = f1
# self.f2 = f2
#
# def fetch_f(self, index):
# if index == 0:
# return self.f0
# elif index == 1:
# return self.f1
# elif index == 2:
# return self.f2
# raise OldError(u"float vector access out of bounds")
#
# def get_length(self):
# return 3
#
#class FVec4(FVec):
# _immutable_fields_ = ['f0', 'f1', 'f2', 'f3']
# interface = Vec.interface
# def __init__(self, f0, f1, f2, f3):
# self.f0 = f0
# self.f1 = f1
# self.f2 = f2
# self.f3 = f3
#
# def fetch_f(self, index):
# if index == 0:
# return self.f0
# elif index == 1:
# return self.f1
# elif index == 2:
# return self.f2
# elif index == 3:
# return self.f3
# raise OldError(u"float vector access out of bounds")
#
# def get_length(self):
# return 4
#
#class FVecN(FVec):
# _immutable_fields_ = ['f_scalars[*]']
# interface = Vec.interface
# def __init__(self, f_scalars):
# assert len(f_scalars) >= 2
# debug.make_sure_not_resized(f_scalars)
# self.f_scalars = f_scalars
#
# def fetch_f(self, index):
# if index < len(self.f_scalars):
# return self.f_scalars[index]
# raise OldError(u"float vector access out of bounds")
#
# def get_length(self):
# return len(self.f_scalars)
#
#class GVec(Vec):
# _immutable_fields_ = ['g_scalars[*]']
# interface = Vec.interface
# def __init__(self, g_scalars, item_type):
# assert len(g_scalars) >= 2
# debug.make_sure_not_resized(g_scalars)
# self.g_scalars = g_scalars
# self.item_type = item_type
#
# def fetch(self, index):
# if index < len(self.g_scalars):
# return self.g_scalars[index]
# raise OldError(u"generic vector access out of bounds")
#
# def get_length(self):
# return len(self.g_scalars)
#
# def get_item_type(self):
# return self.item_type
#
#@jit.unroll_safe
#def compact(g_scalars):
# if isinstance(g_scalars[0], Float):
# f_scalars = [0.0] * len(g_scalars)
# for i, val in enumerate(g_scalars):
# f_scalars[i] = to_float(val)
# return compact_f(f_scalars)
# interface = get_interface(g_scalars[0])
# for scalar in g_scalars:
# if get_interface(scalar) != interface:
# raise OldError(u"every element in vector must have same interface")
# return GVec(g_scalars[:], interface)
#
#def compact_f(f_scalars):
# if len(f_scalars) == 2:
# return FVec2(f_scalars[0], f_scalars[1])
# elif len(f_scalars) == 3:
# return FVec3(f_scalars[0], f_scalars[1], f_scalars[2])
# elif len(f_scalars) == 4:
# return FVec4(f_scalars[0], f_scalars[1], f_scalars[2], f_scalars[3])
# else:
# return FVecN(f_scalars[:])
#
#@Vec.instantiator # TODO: put it to use instantiator.
#@jit.unroll_safe
#def Vec_init(argv):
# if len(argv) < 2:
# raise OldError(u"Too few arguments to vec()")
# return compact(argv)
#
#@jit.unroll_safe
#def letter_swizzle(self, name):
# name = jit.promote(name)
# result = [null] * len(name)
# for i, a in enumerate(name):
# if a == 'x':
# result[i] = self.fetch(0)
# elif a == 'y':
# result[i] = self.fetch(1)
# elif a == 'z':
# result[i] = self.fetch(2)
# elif a == 'w':
# result[i] = self.fetch(3)
# else:
# return Object.getattr(self, name)
# return compact(result)
#
#@operators.add.multimethod_s(Vec, Vec)
#@jit.unroll_safe
#def Vec_add(self, other):
# L = self.match_length(other)
# interface = self.match_interface(other)
# if isinstance(self, FVec) and isinstance(other, FVec):
# f_result = [0.0] * L
# for i in range(L):
# f_result[i] = self.fetch_f(i) + other.fetch_f(i)
# return compact_f(f_result)
# result = [null] * L
# for i in range(L):
# result[i] = operators.add.call([self.fetch(i), other.fetch(i)])
# return GVec(result, interface)
#
#@operators.sub.multimethod_s(Vec, Vec)
#@jit.unroll_safe
#def Vec_sub(self, other):
# L = self.match_length(other)
# interface = self.match_interface(other)
# if isinstance(self, FVec) and isinstance(other, FVec):
# f_result = [0.0] * L
# for i in range(L):
# f_result[i] = self.fetch_f(i) - other.fetch_f(i)
# return compact_f(f_result)
# result = [null] * L
# for i in range(L):
# result[i] = operators.sub.call([self.fetch(i), other.fetch(i)])
# return GVec(result, interface)
#
#@operators.mul.multimethod_s(Vec, Vec)
#@jit.unroll_safe
#def Vec_mul(self, other):
# L = self.match_length(other)
# interface = self.match_interface(other)
# if isinstance(self, FVec) and isinstance(other, FVec):
# f_result = [0.0] * L
# for i in range(L):
# f_result[i] = self.fetch_f(i) * other.fetch_f(i)
# return compact_f(f_result)
# result = [null] * L
# for i in range(L):
# result[i] = operators.mul.call([self.fetch(i), other.fetch(i)])
# return GVec(result, interface)
#
#@operators.div.multimethod_s(Vec, Vec)
#@jit.unroll_safe
#def Vec_div(self, other):
# interface = self.match_interface(other)
# L = self.match_length(other)
# if isinstance(self, FVec) and isinstance(other, FVec):
# f_result = [0.0] * L
# for i in range(L):
# f_result[i] = self.fetch_f(i) / other.fetch_f(i)
# return compact_f(f_result)
# result = [null] * L
# for i in range(L):
# result[i] = operators.div.call([self.fetch(i), other.fetch(i)])
# return GVec(result, interface)
#
#@vectormath.length.multimethod_s(Vec)
#@jit.unroll_safe
#def Vec_length(self):
# result = operators.mul.call([self.fetch(0), self.fetch(0)])
# for i in range(1, jit.promote(self.get_length())):
# result = operators.add.call([
# result,
# operators.mul.call([self.fetch(i), self.fetch(i)])
# ])
# return vectormath.sqrt_.call([result])
#
#@vectormath.dot.multimethod_s(Vec, Vec)
#@jit.unroll_safe
#def Vec_dot(self, other):
# L = self.match_length(other)
# result = operators.mul.call([self.fetch(0), self.fetch(0)])
# for i in range(1, L):
# result = operators.add.call([
# result,
# operators.mul.call([self.fetch(i), other.fetch(i)])
# ])
# return result
#
## improve to include Int,Float X Vec
## you can wrap the stuff into a function, then implement lots of scalar behavior at once, eg.
## binary_arithmetic(Vec, operators.div, (lambda a, b: a / b))
## There's an example of that in runtime/space/operators.py
#
## improve to cross product (2D, 3D)
## improve to include normalize
## improve to include reflect, refract
## neg
## pos
#
## clamp with vec, float, float
## vec, vec, vec