From a368d620ae41dece6382c4f14b3ec1d629502360 Mon Sep 17 00:00:00 2001 From: Christian Bender Date: Mon, 5 Mar 2018 13:27:27 +0100 Subject: [PATCH] Add files via upload --- linear-algebra-python/README.md | 4 ++++ linear-algebra-python/src/lib.py | 34 +++++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/linear-algebra-python/README.md b/linear-algebra-python/README.md index bca0af449..ebfcdab7b 100644 --- a/linear-algebra-python/README.md +++ b/linear-algebra-python/README.md @@ -29,6 +29,8 @@ This module contains some useful classes and functions for dealing with linear a - returns a unit basis vector with a One at index 'pos' (indexing at 0) - function axpy(scalar,vector1,vector2) - computes the axpy operation +- function randomVector(N,a,b) + - returns a random vector of size N, with random integer components between 'a' and 'b'. - class Matrix - This class represents a matrix of arbitrary size and operations on it. @@ -45,6 +47,8 @@ This module contains some useful classes and functions for dealing with linear a - operator - _ implements the matrix-subtraction - function squareZeroMatrix(N) - returns a square zero-matrix of dimension NxN +- function randomMatrix(W,H,a,b) + - returns a random matrix WxH with integer components between 'a' and 'b' --- ## Documentation diff --git a/linear-algebra-python/src/lib.py b/linear-algebra-python/src/lib.py index efded3a2a..66f27ff89 100644 --- a/linear-algebra-python/src/lib.py +++ b/linear-algebra-python/src/lib.py @@ -14,12 +14,15 @@ Overview: - function zeroVector(dimension) - function unitBasisVector(dimension,pos) - function axpy(scalar,vector1,vector2) +- function randomVector(N,a,b) - class Matrix -- squareZeroMatrix(N) +- function squareZeroMatrix(N) +- function randomMatrix(W,H,a,b) """ import math +import random class Vector(object): @@ -196,6 +199,20 @@ def axpy(scalar,x,y): return (x*scalar + y) +def randomVector(N,a,b): + """ + input: size (N) of the vector. + random range (a,b) + output: returns a random vector of size N, with + random integer components between 'a' and 'b'. + """ + ans = zeroVector(N) + random.seed(None) + for i in range(N): + ans.changeComponent(i,random.randint(a,b)) + return ans + + class Matrix(object): """ class: Matrix @@ -328,5 +345,20 @@ def squareZeroMatrix(N): row.append(0) ans.append(row) return Matrix(ans,N,N) + + +def randomMatrix(W,H,a,b): + """ + returns a random matrix WxH with integer components + between 'a' and 'b' + """ + matrix = [] + random.seed(None) + for i in range(H): + row = [] + for j in range(W): + row.append(random.randint(a,b)) + matrix.append(row) + return Matrix(matrix,W,H) \ No newline at end of file