mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-24 13:31:07 +00:00
Merge pull request #261 from christianbender/added_linearAlgebra
added two new functions
This commit is contained in:
commit
95a5331162
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
@ -330,3 +347,18 @@ def squareZeroMatrix(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)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user