14
14
- function zeroVector(dimension)
15
15
- function unitBasisVector(dimension,pos)
16
16
- function axpy(scalar,vector1,vector2)
17
+ - function randomVector(N,a,b)
17
18
- class Matrix
18
- - squareZeroMatrix(N)
19
+ - function squareZeroMatrix(N)
20
+ - function randomMatrix(W,H,a,b)
19
21
"""
20
22
21
23
22
24
import math
25
+ import random
23
26
24
27
25
28
class Vector (object ):
@@ -196,6 +199,20 @@ def axpy(scalar,x,y):
196
199
return (x * scalar + y )
197
200
198
201
202
+ def randomVector (N ,a ,b ):
203
+ """
204
+ input: size (N) of the vector.
205
+ random range (a,b)
206
+ output: returns a random vector of size N, with
207
+ random integer components between 'a' and 'b'.
208
+ """
209
+ ans = zeroVector (N )
210
+ random .seed (None )
211
+ for i in range (N ):
212
+ ans .changeComponent (i ,random .randint (a ,b ))
213
+ return ans
214
+
215
+
199
216
class Matrix (object ):
200
217
"""
201
218
class: Matrix
@@ -328,5 +345,20 @@ def squareZeroMatrix(N):
328
345
row .append (0 )
329
346
ans .append (row )
330
347
return Matrix (ans ,N ,N )
348
+
349
+
350
+ def randomMatrix (W ,H ,a ,b ):
351
+ """
352
+ returns a random matrix WxH with integer components
353
+ between 'a' and 'b'
354
+ """
355
+ matrix = []
356
+ 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 )
362
+ return Matrix (matrix ,W ,H )
331
363
332
364
0 commit comments