This repository was archived by the owner on Jul 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector.py
More file actions
410 lines (334 loc) · 6.86 KB
/
Vector.py
File metadata and controls
410 lines (334 loc) · 6.86 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
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
#!/usr/bin/env python
#
# a python Vector class
# A. Pletzer 5 Jan 00/11 April 2002
#
import math
"""
A list based Vector class that supports elementwise mathematical operations
In this version, the Vector call inherits from list; this
requires Python 2.2 or later.
"""
class Vector(list):
"""
A list based Vector class
"""
# no c'tor
def __getslice__(self, i, j):
try:
# use the list __getslice__ method and convert
# result to Vector
return Vector(super(Vector, self).__getslice__(i,j))
except:
raise TypeError, 'Vector::FAILURE in __getslice__'
def __add__(self, other):
return Vector(map(lambda x,y: x+y, self, other))
def __neg__(self):
return Vector(map(lambda x: -x, self))
def __sub__(self, other):
return Vector(map(lambda x,y: x-y, self, other))
def __mul__(self, other):
"""
Element by element multiplication
"""
try:
return Vector(map(lambda x,y: x*y, self,other))
except:
# other is a const
return Vector(map(lambda x: x*other, self))
def __rmul__(self, other):
return (self*other)
def __div__(self, other):
"""
Element by element division.
"""
try:
return Vector(map(lambda x,y: x/y, self, other))
except:
return Vector(map(lambda x: x/other, self))
def __rdiv__(self, other):
"""
The same as __div__
"""
try:
return Vector(map(lambda x,y: x/y, other, self))
except:
# other is a const
return Vector(map(lambda x: other/x, self))
def size(self): return len(self)
def conjugate(self):
return Vector(map(lambda x: x.conjugate(), self))
def ReIm(self):
"""
Return the real and imaginary parts
"""
return [
Vector(map(lambda x: x.real, self)),
Vector(map(lambda x: x.imag, self)),
]
def AbsArg(self):
"""
Return modulus and phase parts
"""
return [
Vector(map(lambda x: abs(x), self)),
Vector(map(lambda x: math.atan2(x.imag,x.real), self)),
]
def out(self):
"""
Prints out the Vector.
"""
print self
###############################################################################
def isVector(x):
"""
Determines if the argument is a Vector class object.
"""
return hasattr(x,'__class__') and x.__class__ is Vector
def zeros(n):
"""
Returns a zero Vector of length n.
"""
return Vector(map(lambda x: 0., range(n)))
def ones(n):
"""
Returns a Vector of length n with all ones.
"""
return Vector(map(lambda x: 1., range(n)))
def random(n, lmin=0.0, lmax=1.0):
"""
Returns a random Vector of length n.
"""
import whrandom
new = Vector([])
gen = whrandom.whrandom()
dl = lmax-lmin
return Vector(map(lambda x: dl*gen.random(),
range(n)))
def dot(a, b):
"""
dot product of two Vectors.
"""
try:
return reduce(lambda x, y: x+y, a*b, 0.)
except:
raise TypeError, 'Vector::FAILURE in dot'
def cross3d(v1,v2):
"""
cross product of two Vectors.
"""
try:
#(1 2 - 2 1) i + (2 0 - 0 2) j + (0 1 - 1 0) k
return Vector([ (v1[1]*v2[2] - v1[2]*v2[1]),
(v1[2]*v2[0] - v1[0]*v2[2]),
(v1[0]*v2[1] - v1[1]*v2[0])
])
except:
raise TypeError, 'Vector::FAILURE in cross'
def norm(a):
"""
Computes the norm of Vector a.
"""
try:
return math.sqrt(abs(dot(a,a)))
except:
raise TypeError, 'Vector::FAILURE in norm'
def sum(a):
"""
Returns the sum of the elements of a.
"""
try:
return reduce(lambda x, y: x+y, a, 0)
except:
raise TypeError, 'Vector::FAILURE in sum'
# elementwise operations
def log10(a):
"""
log10 of each element of a.
"""
try:
return Vector(map(math.log10, a))
except:
raise TypeError, 'Vector::FAILURE in log10'
def log(a):
"""
log of each element of a.
"""
try:
return Vector(map(math.log, a))
except:
raise TypeError, 'Vector::FAILURE in log'
def exp(a):
"""
Elementwise exponential.
"""
try:
return Vector(map(math.exp, a))
except:
raise TypeError, 'Vector::FAILURE in exp'
def sin(a):
"""
Elementwise sine.
"""
try:
return Vector(map(math.sin, a))
except:
raise TypeError, 'Vector::FAILURE in sin'
def tan(a):
"""
Elementwise tangent.
"""
try:
return Vector(map(math.tan, a))
except:
raise TypeError, 'Vector::FAILURE in tan'
def cos(a):
"""
Elementwise cosine.
"""
try:
return Vector(map(math.cos, a))
except:
raise TypeError, 'Vector::FAILURE in cos'
def asin(a):
"""
Elementwise inverse sine.
"""
try:
return Vector(map(math.asin, a))
except:
raise TypeError, 'Vector::FAILURE in asin'
def atan(a):
"""
Elementwise inverse tangent.
"""
try:
return Vector(map(math.atan, a))
except:
raise TypeError, 'Vector::FAILURE in atan'
def acos(a):
"""
Elementwise inverse cosine.
"""
try:
return Vector(map(math.acos, a))
except:
raise TypeError, 'Vector::FAILURE in acos'
def sqrt(a):
"""
Elementwise sqrt.
"""
try:
return Vector(map(math.sqrt, a))
except:
raise TypeError, 'Vector::FAILURE in sqrt'
def sinh(a):
"""
Elementwise hyperbolic sine.
"""
try:
return Vector(map(math.sinh, a))
except:
raise TypeError, 'Vector::FAILURE in sinh'
def tanh(a):
"""
Elementwise hyperbolic tangent.
"""
try:
return Vector(map(math.tanh, a))
except:
raise TypeError, 'Vector::FAILURE in tanh'
def cosh(a):
"""
Elementwise hyperbolic cosine.
"""
try:
return Vector(map(math.cosh, a))
except:
raise TypeError, 'Vector::FAILURE in cosh'
def pow(a,b):
"""
Takes the elements of a and raises them to the b-th power
"""
try:
return Vector(map(lambda x: x**b, a))
except:
try:
return Vector(map(lambda x,y: x**y, a, b))
except:
raise TypeError, 'Vector::FAILURE in pow'
def atan2(a,b):
"""
Arc tangent
"""
try:
return Vector(map(math.atan2, a, b))
except:
raise TypeError, 'Vector::FAILURE in atan2'
###############################################################################
if __name__ == "__main__":
print 'a = zeros(4)'
a = zeros(4)
print 'a.__doc__=',a.__doc__
print 'a[0] = 1.0'
a[0] = 1.0
print 'a[3] = 3.0'
a[3] = 3.0
print 'a[0]=', a[0]
print 'a[1]=', a[1]
print 'len(a)=',len(a)
print 'a.size()=', a.size()
b = Vector([1, 2, 3, 4])
print 'a=', a
print 'b=', b
print 'a+b'
c = a + b
c.out()
print '-a'
c = -a
c.out()
a.out()
print 'a-b'
c = a - b
c.out()
print 'a*1.2'
c = a*1.2
c.out()
print '1.2*a'
c = 1.2*a
c.out()
print 'a=', a
print 'dot(a,b) = ', dot(a,b)
print 'dot(b,a) = ', dot(b,a)
print 'a*b'
c = a*b
c.out()
print 'a/1.2'
c = a/1.2
c.out()
print 'a[0:2]'
c = a[0:2]
c.out()
print 'a[2:5] = [9.0, 4.0, 5.0]'
a[2:5] = [9.0, 4.0, 5.0]
a.out()
print 'sqrt(a)=',sqrt(a)
print 'pow(a, 2*ones(len(a)))=',pow(a, 2*ones(len(a)))
print 'pow(a, 2)=',pow(a, 2*ones(len(a)))
print 'ones(10)'
c = ones(10)
c.out()
print 'zeros(10)'
c = zeros(10)
c.out()
print 'del a'
del a
try:
a = random(11, 0., 2.)
a.out()
except: pass
a = Vector([3, -3, 1])
b = Vector([4,9,2])
a = Vector([3, -2, -2])
b = Vector([-1,0,5])
print 'Cross(a,b)',cross3d(a,b)