def __init__(self, A, b=None): assert b is None or A.row_num() == len( b), "row number of A must be equal to the length of b" self._m = A.row_num() self._n = A.col_num() if b is None: self.Ab = [A.row_vector(i) for i in range(self._m)] if isinstance(b, Vector): self.Ab = [ Vector(A.row_vector(i).underlying_list() + [b[i]]) for i in range(self._m) ] if isinstance(b, Matrix): self.Ab = [ Vector( A.row_vector(i).underlying_list() + b.row_vector(i).underlying_list()) for i in range(self._m) ] self.pivots = []
def dot(self, another): if isinstance(another, Vector): assert self.col_num() == len( another), "Error in Matrix-Vector Multiplication." return Vector([ self.row_vector(i).dot(another) for i in range(self.row_num()) ]) if isinstance(another, Matrix): assert self.col_num() == another.row_num( ), "Error in Matrix-Matrix Multiplication." return Matrix([[ self.row_vector(i).dot(another.col_vector(j)) for j in range(another.col_num()) ] for i in range(self.row_num())])
matrix = Matrix([[1, 2], [3, 4]]) print(matrix) print("matrix.shape = {}".format(matrix.shape())) print("matrix.size = {}".format(matrix.size())) print("len(matrix) = {}".format(len(matrix))) print("matrix[0][0] = {}".format(matrix[0, 0])) matrix2 = Matrix([[5, 6], [7, 8]]) print(matrix2) print("add: {}".format(matrix + matrix2)) print("subtract: {}".format(matrix - matrix2)) print("scalar-mul: {}".format(2 * matrix)) print("scalar-mul: {}".format(matrix * 2)) print("zero_2_3: {}".format(Matrix.zero(2, 3))) T = Matrix([[1.5, 0], [0, 2]]) p = Vector([5, 3]) print("T.dot(p) = {}".format(T.dot(p))) P = Matrix([[0, 4, 5], [0, 0, 3]]) print("T.dot(P) = {}".format(T.dot(P))) print("A.dot(B) = {}".format(matrix.dot(matrix2))) print("B.dot(A) = {}".format(matrix2.dot(matrix))) print("P.T = {}".format(P.T())) I = Matrix.identity(2) print(I) print("A.dot(I) = {}".format(matrix.dot(I))) print("I.dot(A) = {}".format(I.dot(matrix)))
from src.linear_system.LinearSystem import LinearSystem, inv, rank from src.matrix.matrix import Matrix from src.vectors.vector import Vector if __name__ == "__main__": A = Matrix([[1, 2, 4], [3, 7, 2], [2, 3, 3]]) b = Vector([7, -11, 1]) ls = LinearSystem(A, b) ls.gauss_jordan_elimination() ls.fancy_print() print() A2 = Matrix([[1, -3, 5], [2, -1, -3], [3, 1, 4]]) b2 = Vector([-9, 19, -13]) ls2 = LinearSystem(A2, b2) ls2.gauss_jordan_elimination() ls2.fancy_print() print() A3 = Matrix([[1, 2, -2], [2, -3, 1], [3, -1, 3]]) b3 = Vector([6, -10, -16]) ls3 = LinearSystem(A3, b3) ls3.gauss_jordan_elimination() ls3.fancy_print() print() A4 = Matrix([[3, 1, -2], [5, -3, 10], [7, 4, 16]]) b4 = Vector([4, 32, 13]) ls4 = LinearSystem(A4, b4) ls4.gauss_jordan_elimination() ls4.fancy_print()
def col_vector(self, index): return Vector([row[index] for row in self._values])
def row_vector(self, index): return Vector(self._values[index])
from itertools import product from src.matrix.matrix import Matrix from src.orthonormal.gram_schmidt import gram_schmidt_process, qr from src.vectors.vector import Vector if __name__ == "__main__": basis1 = [Vector([2, 1]), Vector([1, 1])] res1 = gram_schmidt_process(basis1) for row in res1: print(row) res1 = [row / row.norm() for row in res1] for row in res1: print(row) print(res1[0].dot(res1[1])) print() basis2 = [Vector([2, 3]), Vector([4, 5])] res2 = gram_schmidt_process(basis2) res2 = [row / row.norm() for row in res2] for row in res2: print(row) print(res2[0].dot(res2[1])) print() basis3 = [Vector([1, 0, 1]), Vector([3, 1, 1]), Vector([-1, -1, -1])] res3 = gram_schmidt_process(basis3) res3 = [row / row.norm() for row in res3] for row in res3:
def rank(A): ls = LinearSystem(A) ls.gauss_jordan_elimination() zero = Vector.zero(A.col_num()) return sum([row != zero for row in ls.Ab])