Merge pull request #261 from christianbender/added_linearAlgebra

added two new functions
This commit is contained in:
Harshil 2018-03-05 18:52:43 +05:30 committed by GitHub
commit 95a5331162
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
@ -328,5 +345,20 @@ def squareZeroMatrix(N):
row.append(0) row.append(0)
ans.append(row) ans.append(row)
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)