Add files via upload

This commit is contained in:
Christian Bender 2018-03-05 13:27:27 +01:00 committed by GitHub
parent 0b85929188
commit a368d620ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 1 deletions

View File

@ -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) - returns a unit basis vector with a One at index 'pos' (indexing at 0)
- function axpy(scalar,vector1,vector2) - function axpy(scalar,vector1,vector2)
- computes the axpy operation - 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 - class Matrix
- This class represents a matrix of arbitrary size and operations on it. - 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 - operator - _ implements the matrix-subtraction
- function squareZeroMatrix(N) - function squareZeroMatrix(N)
- returns a square zero-matrix of dimension NxN - 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 ## Documentation

View File

@ -14,12 +14,15 @@ Overview:
- function zeroVector(dimension) - function zeroVector(dimension)
- function unitBasisVector(dimension,pos) - function unitBasisVector(dimension,pos)
- function axpy(scalar,vector1,vector2) - function axpy(scalar,vector1,vector2)
- function randomVector(N,a,b)
- class Matrix - class Matrix
- squareZeroMatrix(N) - function squareZeroMatrix(N)
- function randomMatrix(W,H,a,b)
""" """
import math import math
import random
class Vector(object): class Vector(object):
@ -196,6 +199,20 @@ def axpy(scalar,x,y):
return (x*scalar + 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(object):
""" """
class: Matrix class: Matrix
@ -330,3 +347,18 @@ def squareZeroMatrix(N):
return Matrix(ans,N,N) 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)