@@ -36,7 +36,7 @@ class Vector(object):
36
36
set(components : list) : changes the vector components.
37
37
__str__() : toString method
38
38
component(i : int): gets the i-th component (start by 0)
39
- size () : gets the size of the vector (number of components)
39
+ __len__ () : gets the size of the vector (number of components)
40
40
euclidLength() : returns the eulidean length of the vector.
41
41
operator + : vector addition
42
42
operator - : vector subtraction
@@ -50,41 +50,32 @@ def __init__(self,components):
50
50
input: components or nothing
51
51
simple constructor for init the vector
52
52
"""
53
- self .__components = components
53
+ self .__components = list ( components )
54
54
def set (self ,components ):
55
55
"""
56
56
input: new components
57
57
changes the components of the vector.
58
58
replace the components with newer one.
59
59
"""
60
60
if len (components ) > 0 :
61
- self .__components = components
61
+ self .__components = list ( components )
62
62
else :
63
63
raise Exception ("please give any vector" )
64
64
def __str__ (self ):
65
65
"""
66
66
returns a string representation of the vector
67
67
"""
68
- ans = "("
69
- length = len (self .__components )
70
- for i in range (length ):
71
- if i != length - 1 :
72
- ans += str (self .__components [i ]) + ","
73
- else :
74
- ans += str (self .__components [i ]) + ")"
75
- if len (ans ) == 1 :
76
- ans += ")"
77
- return ans
68
+ return "(" + "," .join (map (str , self .__components )) + ")"
78
69
def component (self ,i ):
79
70
"""
80
71
input: index (start at 0)
81
72
output: the i-th component of the vector.
82
73
"""
83
- if i < len (self .__components ) and i >= 0 :
74
+ if type ( i ) is int and - len (self .__components ) <= i < len ( self . __components ) :
84
75
return self .__components [i ]
85
76
else :
86
77
raise Exception ("index out of range" )
87
- def size (self ):
78
+ def __len__ (self ):
88
79
"""
89
80
returns the size of the vector
90
81
"""
@@ -103,9 +94,9 @@ def __add__(self,other):
103
94
assumes: other vector has the same size
104
95
returns a new vector that represents the sum.
105
96
"""
106
- size = self . size ( )
97
+ size = len ( self )
107
98
result = []
108
- if size == other . size ( ):
99
+ if size == len ( other ):
109
100
for i in range (size ):
110
101
result .append (self .__components [i ] + other .component (i ))
111
102
else :
@@ -117,9 +108,9 @@ def __sub__(self,other):
117
108
assumes: other vector has the same size
118
109
returns a new vector that represents the differenz.
119
110
"""
120
- size = self . size ( )
111
+ size = len ( self )
121
112
result = []
122
- if size == other . size ( ):
113
+ if size == len ( other ):
123
114
for i in range (size ):
124
115
result .append (self .__components [i ] - other .component (i ))
125
116
else : # error case
@@ -134,8 +125,8 @@ def __mul__(self,other):
134
125
if isinstance (other ,float ) or isinstance (other ,int ):
135
126
for c in self .__components :
136
127
ans .append (c * other )
137
- elif (isinstance (other ,Vector ) and (self . size ( ) == other . size ( ))):
138
- size = self . size ( )
128
+ elif (isinstance (other ,Vector ) and (len ( self ) == len ( other ))):
129
+ size = len ( self )
139
130
summe = 0
140
131
for i in range (size ):
141
132
summe += self .__components [i ] * other .component (i )
@@ -147,16 +138,15 @@ def copy(self):
147
138
"""
148
139
copies this vector and returns it.
149
140
"""
150
- components = [x for x in self .__components ]
151
- return Vector (components )
141
+ return Vector (self .__components )
152
142
def changeComponent (self ,pos ,value ):
153
143
"""
154
144
input: an index (pos) and a value
155
145
changes the specified component (pos) with the
156
146
'value'
157
147
"""
158
148
#precondition
159
- assert (pos >= 0 and pos < len (self .__components ))
149
+ assert (- len ( self . __components ) <= pos < len (self .__components ))
160
150
self .__components [pos ] = value
161
151
162
152
def zeroVector (dimension ):
@@ -165,10 +155,7 @@ def zeroVector(dimension):
165
155
"""
166
156
#precondition
167
157
assert (isinstance (dimension ,int ))
168
- ans = []
169
- for i in range (dimension ):
170
- ans .append (0 )
171
- return Vector (ans )
158
+ return Vector ([0 ]* dimension )
172
159
173
160
174
161
def unitBasisVector (dimension ,pos ):
@@ -178,12 +165,8 @@ def unitBasisVector(dimension,pos):
178
165
"""
179
166
#precondition
180
167
assert (isinstance (dimension ,int ) and (isinstance (pos ,int )))
181
- ans = []
182
- for i in range (dimension ):
183
- if i != pos :
184
- ans .append (0 )
185
- else :
186
- ans .append (1 )
168
+ ans = [0 ]* dimension
169
+ ans [pos ] = 1
187
170
return Vector (ans )
188
171
189
172
@@ -206,11 +189,9 @@ def randomVector(N,a,b):
206
189
output: returns a random vector of size N, with
207
190
random integer components between 'a' and 'b'.
208
191
"""
209
- ans = zeroVector (N )
210
192
random .seed (None )
211
- for i in range (N ):
212
- ans .changeComponent (i ,random .randint (a ,b ))
213
- return ans
193
+ ans = [random .randint (a ,b ) for i in range (N )]
194
+ return Vector (ans )
214
195
215
196
216
197
class Matrix (object ):
@@ -220,7 +201,7 @@ class Matrix(object):
220
201
221
202
Overview about the methods:
222
203
223
- __str__() : returns a string representation
204
+ __str__() : returns a string representation
224
205
operator * : implements the matrix vector multiplication
225
206
implements the matrix-scalar multiplication.
226
207
changeComponent(x,y,value) : changes the specified component.
@@ -284,7 +265,7 @@ def __mul__(self,other):
284
265
implements the matrix-scalar multiplication
285
266
"""
286
267
if isinstance (other , Vector ): # vector-matrix
287
- if (other . size ( ) == self .__width ):
268
+ if (len ( other ) == self .__width ):
288
269
ans = zeroVector (self .__height )
289
270
for i in range (self .__height ):
290
271
summe = 0
@@ -294,15 +275,9 @@ def __mul__(self,other):
294
275
summe = 0
295
276
return ans
296
277
else :
297
- raise Exception ("vector must have the same size as the "
298
- + "number of columns of the matrix!" )
278
+ raise Exception ("vector must have the same size as the " + "number of columns of the matrix!" )
299
279
elif isinstance (other ,int ) or isinstance (other ,float ): # matrix-scalar
300
- matrix = []
301
- for i in range (self .__height ):
302
- row = []
303
- for j in range (self .__width ):
304
- row .append (self .__matrix [i ][j ] * other )
305
- matrix .append (row )
280
+ matrix = [[self .__matrix [i ][j ] * other for j in range (self .__width )] for i in range (self .__height )]
306
281
return Matrix (matrix ,self .__width ,self .__height )
307
282
def __add__ (self ,other ):
308
283
"""
@@ -338,12 +313,7 @@ def squareZeroMatrix(N):
338
313
"""
339
314
returns a square zero-matrix of dimension NxN
340
315
"""
341
- ans = []
342
- for i in range (N ):
343
- row = []
344
- for j in range (N ):
345
- row .append (0 )
346
- ans .append (row )
316
+ ans = [[0 ]* N for i in range (N )]
347
317
return Matrix (ans ,N ,N )
348
318
349
319
@@ -352,13 +322,8 @@ def randomMatrix(W,H,a,b):
352
322
returns a random matrix WxH with integer components
353
323
between 'a' and 'b'
354
324
"""
355
- matrix = []
356
325
random .seed (None )
357
- for i in range (H ):
358
- row = []
359
- for j in range (W ):
360
- row .append (random .randint (a ,b ))
361
- matrix .append (row )
326
+ matrix = [[random .randint (a ,b ) for j in range (W )] for i in range (H )]
362
327
return Matrix (matrix ,W ,H )
363
328
364
-
329
+
0 commit comments