2222
2323import math
2424import random
25+ from typing import Collection , Optional , Union , overload
2526
2627
2728class Vector :
@@ -45,7 +46,7 @@ class Vector:
4546 TODO: compare-operator
4647 """
4748
48- def __init__ (self , components = None ):
49+ def __init__ (self , components : Optional [ Collection [ float ]] = None ) -> None :
4950 """
5051 input: components or nothing
5152 simple constructor for init the vector
@@ -54,7 +55,7 @@ def __init__(self, components=None):
5455 components = []
5556 self .__components = list (components )
5657
57- def set (self , components ) :
58+ def set (self , components : Collection [ float ]) -> None :
5859 """
5960 input: new components
6061 changes the components of the vector.
@@ -65,13 +66,13 @@ def set(self, components):
6566 else :
6667 raise Exception ("please give any vector" )
6768
68- def __str__ (self ):
69+ def __str__ (self ) -> str :
6970 """
7071 returns a string representation of the vector
7172 """
7273 return "(" + "," .join (map (str , self .__components )) + ")"
7374
74- def component (self , i ) :
75+ def component (self , i : int ) -> float :
7576 """
7677 input: index (start at 0)
7778 output: the i-th component of the vector.
@@ -81,22 +82,22 @@ def component(self, i):
8182 else :
8283 raise Exception ("index out of range" )
8384
84- def __len__ (self ):
85+ def __len__ (self ) -> int :
8586 """
8687 returns the size of the vector
8788 """
8889 return len (self .__components )
8990
90- def euclidLength (self ):
91+ def euclidLength (self ) -> float :
9192 """
9293 returns the euclidean length of the vector
9394 """
94- summe = 0
95+ summe : float = 0
9596 for c in self .__components :
9697 summe += c ** 2
9798 return math .sqrt (summe )
9899
99- def __add__ (self , other ) :
100+ def __add__ (self , other : "Vector" ) -> "Vector" :
100101 """
101102 input: other vector
102103 assumes: other vector has the same size
@@ -109,7 +110,7 @@ def __add__(self, other):
109110 else :
110111 raise Exception ("must have the same size" )
111112
112- def __sub__ (self , other ) :
113+ def __sub__ (self , other : "Vector" ) -> "Vector" :
113114 """
114115 input: other vector
115116 assumes: other vector has the same size
@@ -122,7 +123,15 @@ def __sub__(self, other):
122123 else : # error case
123124 raise Exception ("must have the same size" )
124125
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" ]:
126135 """
127136 mul implements the scalar multiplication
128137 and the dot-product
@@ -132,20 +141,20 @@ def __mul__(self, other):
132141 return Vector (ans )
133142 elif isinstance (other , Vector ) and (len (self ) == len (other )):
134143 size = len (self )
135- summe = 0
144+ summe : float = 0
136145 for i in range (size ):
137146 summe += self .__components [i ] * other .component (i )
138147 return summe
139148 else : # error case
140149 raise Exception ("invalid operand!" )
141150
142- def copy (self ):
151+ def copy (self ) -> "Vector" :
143152 """
144153 copies this vector and returns it.
145154 """
146155 return Vector (self .__components )
147156
148- def changeComponent (self , pos , value ) :
157+ def changeComponent (self , pos : int , value : float ) -> None :
149158 """
150159 input: an index (pos) and a value
151160 changes the specified component (pos) with the
@@ -156,7 +165,7 @@ def changeComponent(self, pos, value):
156165 self .__components [pos ] = value
157166
158167
159- def zeroVector (dimension ) :
168+ def zeroVector (dimension : int ) -> Vector :
160169 """
161170 returns a zero-vector of size 'dimension'
162171 """
@@ -165,7 +174,7 @@ def zeroVector(dimension):
165174 return Vector ([0 ] * dimension )
166175
167176
168- def unitBasisVector (dimension , pos ) :
177+ def unitBasisVector (dimension : int , pos : int ) -> Vector :
169178 """
170179 returns a unit basis vector with a One
171180 at index 'pos' (indexing at 0)
@@ -177,7 +186,7 @@ def unitBasisVector(dimension, pos):
177186 return Vector (ans )
178187
179188
180- def axpy (scalar , x , y ) :
189+ def axpy (scalar : float , x : Vector , y : Vector ) -> Vector :
181190 """
182191 input: a 'scalar' and two vectors 'x' and 'y'
183192 output: a vector
@@ -192,15 +201,15 @@ def axpy(scalar, x, y):
192201 return x * scalar + y
193202
194203
195- def randomVector (N , a , b ) :
204+ def randomVector (N : int , a : int , b : int ) -> Vector :
196205 """
197206 input: size (N) of the vector.
198207 random range (a,b)
199208 output: returns a random vector of size N, with
200209 random integer components between 'a' and 'b'.
201210 """
202211 random .seed (None )
203- ans = [random .randint (a , b ) for i in range (N )]
212+ ans = [random .randint (a , b ) for _ in range (N )]
204213 return Vector (ans )
205214
206215
@@ -222,7 +231,7 @@ class Matrix:
222231 operator - _ implements the matrix-subtraction
223232 """
224233
225- def __init__ (self , matrix , w , h ) :
234+ def __init__ (self , matrix : list [ list [ float ]] , w : int , h : int ) -> None :
226235 """
227236 simple constructor for initializing
228237 the matrix with components.
@@ -231,7 +240,7 @@ def __init__(self, matrix, w, h):
231240 self .__width = w
232241 self .__height = h
233242
234- def __str__ (self ):
243+ def __str__ (self ) -> str :
235244 """
236245 returns a string representation of this
237246 matrix.
@@ -246,7 +255,7 @@ def __str__(self):
246255 ans += str (self .__matrix [i ][j ]) + "|\n "
247256 return ans
248257
249- def changeComponent (self , x , y , value ) :
258+ def changeComponent (self , x : int , y : int , value : float ) -> None :
250259 """
251260 changes the x-y component of this matrix
252261 """
@@ -255,7 +264,7 @@ def changeComponent(self, x, y, value):
255264 else :
256265 raise Exception ("changeComponent: indices out of bounds" )
257266
258- def component (self , x , y ) :
267+ def component (self , x : int , y : int ) -> float :
259268 """
260269 returns the specified (x,y) component
261270 """
@@ -264,13 +273,13 @@ def component(self, x, y):
264273 else :
265274 raise Exception ("changeComponent: indices out of bounds" )
266275
267- def width (self ):
276+ def width (self ) -> int :
268277 """
269278 getter for the width
270279 """
271280 return self .__width
272281
273- def height (self ):
282+ def height (self ) -> int :
274283 """
275284 getter for the height
276285 """
@@ -303,7 +312,15 @@ def determinate(self) -> float:
303312 else :
304313 raise Exception ("matrix is not square" )
305314
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" ]:
307324 """
308325 implements the matrix-vector multiplication.
309326 implements the matrix-scalar multiplication
@@ -312,7 +329,7 @@ def __mul__(self, other):
312329 if len (other ) == self .__width :
313330 ans = zeroVector (self .__height )
314331 for i in range (self .__height ):
315- summe = 0
332+ summe : float = 0
316333 for j in range (self .__width ):
317334 summe += other .component (j ) * self .__matrix [i ][j ]
318335 ans .changeComponent (i , summe )
@@ -330,7 +347,7 @@ def __mul__(self, other):
330347 ]
331348 return Matrix (matrix , self .__width , self .__height )
332349
333- def __add__ (self , other ) :
350+ def __add__ (self , other : "Matrix" ) -> "Matrix" :
334351 """
335352 implements the matrix-addition.
336353 """
@@ -345,7 +362,7 @@ def __add__(self, other):
345362 else :
346363 raise Exception ("matrix must have the same dimension!" )
347364
348- def __sub__ (self , other ) :
365+ def __sub__ (self , other : "Matrix" ) -> "Matrix" :
349366 """
350367 implements the matrix-subtraction.
351368 """
@@ -361,19 +378,21 @@ def __sub__(self, other):
361378 raise Exception ("matrix must have the same dimension!" )
362379
363380
364- def squareZeroMatrix (N ) :
381+ def squareZeroMatrix (N : int ) -> Matrix :
365382 """
366383 returns a square zero-matrix of dimension NxN
367384 """
368- ans = [[0 ] * N for i in range (N )]
385+ ans : list [ list [ float ]] = [[0 ] * N for _ in range (N )]
369386 return Matrix (ans , N , N )
370387
371388
372- def randomMatrix (W , H , a , b ) :
389+ def randomMatrix (W : int , H : int , a : int , b : int ) -> Matrix :
373390 """
374391 returns a random matrix WxH with integer components
375392 between 'a' and 'b'
376393 """
377394 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+ ]
379398 return Matrix (matrix , W , H )
0 commit comments