22
22
23
23
import math
24
24
import random
25
+ from typing import Collection , Optional , Union , overload
25
26
26
27
27
28
class Vector :
@@ -45,7 +46,7 @@ class Vector:
45
46
TODO: compare-operator
46
47
"""
47
48
48
- def __init__ (self , components = None ):
49
+ def __init__ (self , components : Optional [ Collection [ float ]] = None ) -> None :
49
50
"""
50
51
input: components or nothing
51
52
simple constructor for init the vector
@@ -54,7 +55,7 @@ def __init__(self, components=None):
54
55
components = []
55
56
self .__components = list (components )
56
57
57
- def set (self , components ) :
58
+ def set (self , components : Collection [ float ]) -> None :
58
59
"""
59
60
input: new components
60
61
changes the components of the vector.
@@ -65,13 +66,13 @@ def set(self, components):
65
66
else :
66
67
raise Exception ("please give any vector" )
67
68
68
- def __str__ (self ):
69
+ def __str__ (self ) -> str :
69
70
"""
70
71
returns a string representation of the vector
71
72
"""
72
73
return "(" + "," .join (map (str , self .__components )) + ")"
73
74
74
- def component (self , i ) :
75
+ def component (self , i : int ) -> float :
75
76
"""
76
77
input: index (start at 0)
77
78
output: the i-th component of the vector.
@@ -81,22 +82,22 @@ def component(self, i):
81
82
else :
82
83
raise Exception ("index out of range" )
83
84
84
- def __len__ (self ):
85
+ def __len__ (self ) -> int :
85
86
"""
86
87
returns the size of the vector
87
88
"""
88
89
return len (self .__components )
89
90
90
- def euclidLength (self ):
91
+ def euclidLength (self ) -> float :
91
92
"""
92
93
returns the euclidean length of the vector
93
94
"""
94
- summe = 0
95
+ summe : float = 0
95
96
for c in self .__components :
96
97
summe += c ** 2
97
98
return math .sqrt (summe )
98
99
99
- def __add__ (self , other ) :
100
+ def __add__ (self , other : "Vector" ) -> "Vector" :
100
101
"""
101
102
input: other vector
102
103
assumes: other vector has the same size
@@ -109,7 +110,7 @@ def __add__(self, other):
109
110
else :
110
111
raise Exception ("must have the same size" )
111
112
112
- def __sub__ (self , other ) :
113
+ def __sub__ (self , other : "Vector" ) -> "Vector" :
113
114
"""
114
115
input: other vector
115
116
assumes: other vector has the same size
@@ -122,7 +123,15 @@ def __sub__(self, other):
122
123
else : # error case
123
124
raise Exception ("must have the same size" )
124
125
125
- def __mul__ (self , other ):
126
+ @overload
127
+ def __mul__ (self , other : float ) -> "Vector" :
128
+ ...
129
+
130
+ @overload
131
+ def __mul__ (self , other : "Vector" ) -> float :
132
+ ...
133
+
134
+ def __mul__ (self , other : Union [float , "Vector" ]) -> Union [float , "Vector" ]:
126
135
"""
127
136
mul implements the scalar multiplication
128
137
and the dot-product
@@ -132,20 +141,20 @@ def __mul__(self, other):
132
141
return Vector (ans )
133
142
elif isinstance (other , Vector ) and (len (self ) == len (other )):
134
143
size = len (self )
135
- summe = 0
144
+ summe : float = 0
136
145
for i in range (size ):
137
146
summe += self .__components [i ] * other .component (i )
138
147
return summe
139
148
else : # error case
140
149
raise Exception ("invalid operand!" )
141
150
142
- def copy (self ):
151
+ def copy (self ) -> "Vector" :
143
152
"""
144
153
copies this vector and returns it.
145
154
"""
146
155
return Vector (self .__components )
147
156
148
- def changeComponent (self , pos , value ) :
157
+ def changeComponent (self , pos : int , value : float ) -> None :
149
158
"""
150
159
input: an index (pos) and a value
151
160
changes the specified component (pos) with the
@@ -156,7 +165,7 @@ def changeComponent(self, pos, value):
156
165
self .__components [pos ] = value
157
166
158
167
159
- def zeroVector (dimension ) :
168
+ def zeroVector (dimension : int ) -> Vector :
160
169
"""
161
170
returns a zero-vector of size 'dimension'
162
171
"""
@@ -165,7 +174,7 @@ def zeroVector(dimension):
165
174
return Vector ([0 ] * dimension )
166
175
167
176
168
- def unitBasisVector (dimension , pos ) :
177
+ def unitBasisVector (dimension : int , pos : int ) -> Vector :
169
178
"""
170
179
returns a unit basis vector with a One
171
180
at index 'pos' (indexing at 0)
@@ -177,7 +186,7 @@ def unitBasisVector(dimension, pos):
177
186
return Vector (ans )
178
187
179
188
180
- def axpy (scalar , x , y ) :
189
+ def axpy (scalar : float , x : Vector , y : Vector ) -> Vector :
181
190
"""
182
191
input: a 'scalar' and two vectors 'x' and 'y'
183
192
output: a vector
@@ -192,15 +201,15 @@ def axpy(scalar, x, y):
192
201
return x * scalar + y
193
202
194
203
195
- def randomVector (N , a , b ) :
204
+ def randomVector (N : int , a : int , b : int ) -> Vector :
196
205
"""
197
206
input: size (N) of the vector.
198
207
random range (a,b)
199
208
output: returns a random vector of size N, with
200
209
random integer components between 'a' and 'b'.
201
210
"""
202
211
random .seed (None )
203
- ans = [random .randint (a , b ) for i in range (N )]
212
+ ans = [random .randint (a , b ) for _ in range (N )]
204
213
return Vector (ans )
205
214
206
215
@@ -222,7 +231,7 @@ class Matrix:
222
231
operator - _ implements the matrix-subtraction
223
232
"""
224
233
225
- def __init__ (self , matrix , w , h ) :
234
+ def __init__ (self , matrix : list [ list [ float ]] , w : int , h : int ) -> None :
226
235
"""
227
236
simple constructor for initializing
228
237
the matrix with components.
@@ -231,7 +240,7 @@ def __init__(self, matrix, w, h):
231
240
self .__width = w
232
241
self .__height = h
233
242
234
- def __str__ (self ):
243
+ def __str__ (self ) -> str :
235
244
"""
236
245
returns a string representation of this
237
246
matrix.
@@ -246,7 +255,7 @@ def __str__(self):
246
255
ans += str (self .__matrix [i ][j ]) + "|\n "
247
256
return ans
248
257
249
- def changeComponent (self , x , y , value ) :
258
+ def changeComponent (self , x : int , y : int , value : float ) -> None :
250
259
"""
251
260
changes the x-y component of this matrix
252
261
"""
@@ -255,7 +264,7 @@ def changeComponent(self, x, y, value):
255
264
else :
256
265
raise Exception ("changeComponent: indices out of bounds" )
257
266
258
- def component (self , x , y ) :
267
+ def component (self , x : int , y : int ) -> float :
259
268
"""
260
269
returns the specified (x,y) component
261
270
"""
@@ -264,13 +273,13 @@ def component(self, x, y):
264
273
else :
265
274
raise Exception ("changeComponent: indices out of bounds" )
266
275
267
- def width (self ):
276
+ def width (self ) -> int :
268
277
"""
269
278
getter for the width
270
279
"""
271
280
return self .__width
272
281
273
- def height (self ):
282
+ def height (self ) -> int :
274
283
"""
275
284
getter for the height
276
285
"""
@@ -303,7 +312,15 @@ def determinate(self) -> float:
303
312
else :
304
313
raise Exception ("matrix is not square" )
305
314
306
- def __mul__ (self , other ):
315
+ @overload
316
+ def __mul__ (self , other : float ) -> "Matrix" :
317
+ ...
318
+
319
+ @overload
320
+ def __mul__ (self , other : Vector ) -> Vector :
321
+ ...
322
+
323
+ def __mul__ (self , other : Union [float , Vector ]) -> Union [Vector , "Matrix" ]:
307
324
"""
308
325
implements the matrix-vector multiplication.
309
326
implements the matrix-scalar multiplication
@@ -312,7 +329,7 @@ def __mul__(self, other):
312
329
if len (other ) == self .__width :
313
330
ans = zeroVector (self .__height )
314
331
for i in range (self .__height ):
315
- summe = 0
332
+ summe : float = 0
316
333
for j in range (self .__width ):
317
334
summe += other .component (j ) * self .__matrix [i ][j ]
318
335
ans .changeComponent (i , summe )
@@ -330,7 +347,7 @@ def __mul__(self, other):
330
347
]
331
348
return Matrix (matrix , self .__width , self .__height )
332
349
333
- def __add__ (self , other ) :
350
+ def __add__ (self , other : "Matrix" ) -> "Matrix" :
334
351
"""
335
352
implements the matrix-addition.
336
353
"""
@@ -345,7 +362,7 @@ def __add__(self, other):
345
362
else :
346
363
raise Exception ("matrix must have the same dimension!" )
347
364
348
- def __sub__ (self , other ) :
365
+ def __sub__ (self , other : "Matrix" ) -> "Matrix" :
349
366
"""
350
367
implements the matrix-subtraction.
351
368
"""
@@ -361,19 +378,21 @@ def __sub__(self, other):
361
378
raise Exception ("matrix must have the same dimension!" )
362
379
363
380
364
- def squareZeroMatrix (N ) :
381
+ def squareZeroMatrix (N : int ) -> Matrix :
365
382
"""
366
383
returns a square zero-matrix of dimension NxN
367
384
"""
368
- ans = [[0 ] * N for i in range (N )]
385
+ ans : list [ list [ float ]] = [[0 ] * N for _ in range (N )]
369
386
return Matrix (ans , N , N )
370
387
371
388
372
- def randomMatrix (W , H , a , b ) :
389
+ def randomMatrix (W : int , H : int , a : int , b : int ) -> Matrix :
373
390
"""
374
391
returns a random matrix WxH with integer components
375
392
between 'a' and 'b'
376
393
"""
377
394
random .seed (None )
378
- matrix = [[random .randint (a , b ) for j in range (W )] for i in range (H )]
395
+ matrix : list [list [float ]] = [
396
+ [random .randint (a , b ) for _ in range (W )] for _ in range (H )
397
+ ]
379
398
return Matrix (matrix , W , H )
0 commit comments