def dot(self, another): """返回矩阵乘法的结果""" if isinstance(another, Vector): assert self.col_num() == len(another), "Error in Matrix-Vector Multiplication, the col num of Matrix and the length of Vector must be same." # return Vector([ # sum([a * b for a, b in zip(self.row_vector(i), another)]) for i in range(self.row_num()) # ]) return Vector([ self.row_vector(i).dot(another) for i in range(self.row_num()) ])
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.dot(another.col_vector(i))]for i in range(another.col_num())]) 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())])
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())])
def col_vector(self, index): """返回矩阵第index个列向量""" return Vector([row[index] for row in self._values])
from playLA.Vector import Vector if __name__ == "__main__": vec = Vector([5, 2]) vec2 = Vector([2, 5]) print(vec) print(len(vec)) print("vec[0] = {}, vec[1] = {}".format(vec[0], vec[1])) print("{} * {} = {}".format(vec, 3, vec * 3)) print("{} * {} = {}".format(4, vec, 4 * vec)) print("{} + {} = {}".format(vec, vec2, vec + vec2)) print("{} - {} = {}".format(vec, vec2, vec - vec2)) print("-{} = {}".format(vec, -vec)) print("+{} = {}".format(vec, +vec)) zero2 = Vector.zero(2) print(zero2) print("normalize {} = {}".format(vec, vec.normalize())) print(vec.normalize().norm()) try: zero2.normalize() except ZeroDivisionError: print("Cannot normalize zero vector {}.".format(zero2)) print("{} . {} = {}".format(vec, vec2, vec.dot(vec2))) print("{} == {}? {}".format(vec, vec2, vec == vec2)) print("{} != {}? {}".format(vec, vec2, vec != vec2))
from playLA.Matrix import Matrix from playLA.Vector import Vector if __name__ == "__main__": matrix = Matrix([[1, 2], [3, 4]]) print(matrix) print("{} shape = {}".format(matrix, matrix.shape())) print("{} size = {}".format(matrix, matrix.size())) print("len(matrix({})) = {}".format(matrix, len(matrix))) print("matrix[0][0] = {}".format(matrix[0, 0])) matrix2 = Matrix([[5, 6], [7, 8]]) print("add {} + {} = {}".format(matrix, matrix2, matrix + matrix2)) print("sub {} - {} = {}".format(matrix, matrix2, matrix - matrix2)) print("zero 2 3 : {}".format(Matrix.zero(2, 3))) T = Matrix([[1.5, 0], [0, 2]]) p = Vector([5, 3]) print(T.dot(p)) P = Matrix([[0, 4, 5], [0, 0, 3]]) print(T.dot(P)) I = Matrix.identity(2) print(I)
if __name__ == '__main__': m1 = Matrix([[1, 2], [3, 4]]) #print(m1) #print(m1.row_num()) #print(len(m1)) #print(m1.col_num()) #print(m1.shap()) #print(m1.size()) #print(m1[1, 2]) #print(m1.row_vec(1)) #print(m1.col_vec(2)) m2 = Matrix([[0, 4, 5], [0, 0, 3]]) #print(m1 + m2) #print(m1 - m2) #print(m2 * 2) #print(2 * m2) #print(m2 / 2) #print(+m2) #print(-m2) #print(m1.dot_mat(m2)) #print(m1.T()) v1 = Vector([3, 2]) #print(v1) #print(m1.dot_vec(v1)) z = Matrix.zero(3, 2) #print(z) m3 = np.array([234, 29]) print(m3)
from playLA.LinearSystem import LinearSystem from playLA.LinearSystem import inv, rank from playLA.Matrix import Matrix from playLA.Vector import Vector if __name__ == '__main__': A1 = Matrix([[1, 2, 4], [3, 7, 2], [2, 3, 3]]) b = Vector([7, -11, 1]) ls = LinearSystem(A1, b) ls.gauss_jordan_elimination() ls.fancy_print() print() # 测试更加一般化的高斯约旦消元法 A2 = Matrix([[1, -1, 2, 0, 3], [-1, 1, 0, 2, -5], [1, -1, 4, 2, 4], [-2, 2, -5, -1, -3]]) b2 = Vector([1, 5, 13, -1]) ls2 = LinearSystem(A2, b2) ls2.gauss_jordan_elimination() ls2.fancy_print() print() A3 = Matrix([[2, 2], [2, 1], [1, 2]]) b3 = Vector([3, 2.5, 7]) ls3 = LinearSystem(A3, b3)
def col_vector(self, index): return Vector([row[index] for row in self._values])
from playLA.Vector import Vector u = Vector([5, 2]) u2 = Vector([3, 4]) print("{} + {} = {}".format(u, u2, u + u2)) print("{} * {} = {}".format(2, u, u * 2)) print("-{} = {}".format(u, -u)) zero3 = Vector.zero(3) print("{}".format(zero3)) print("norm({}) = {}".format(u, u.norm())) print("normzlize({}) = {}".format(u, u / 3)) print("{}*{} = {}".format(u, u2, u.dot(u2))) print(u[1:]) try: zero3.normalize() except ZeroDivisionError: print("Cannot normalize zero vector{}".format(zero3)) vec3 = Vector([0, 0, 0]) print("{} == {} {}".format(vec3, zero3, vec3 == zero3))
from playLA.Vector import Vector if __name__ == "__main__": vec = Vector([5, 2]) print(vec) print("len(vec) = {}".format(len(vec))) print("vec[0] = {}, vec[1] = {}".format(vec[0], vec[1])) vec2 = Vector([3, 1]) print("{} + {} = {}".format(vec, vec2, vec + vec2)) print("{} - {} = {}".format(vec, vec2, vec - vec2)) print("{} * {} = {}".format(vec, 3, vec * 3)) print("{} * {} = {}".format(3, vec, 3 * vec)) print("+{} = {}".format(vec, +vec)) print("-{} = {}".format(vec, -vec))
from playLA.Vector import Vector from playLA.GramSchmidtProcess import gram_schmidt_process 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])) basis2 = [Vector([2, 3]), Vector([4, 5])] res2 = gram_schmidt_process(basis2) for row in res2: print(row) res2 = [row / row.norm() for row in res2] for row in res2: print(row) print(res2[0].dot(res2[1])) basis3 = [Vector([1, 0, 1]), Vector([3, 1, 1]), Vector([-1, -1, -1])] res3 = gram_schmidt_process(basis3) for row in res3: print(row) res3 = [row / row.norm() for row in res3] for row in res3: print(row)
import sys import numpy import scipy from playLA.Vector import Vector if __name__ == "__main__": print(sys.version) print(numpy.__version__) print(scipy.__version__) vec = Vector([1, 3]) print("vec = {}".format(vec)) print("len(vec) = {}".format(len(vec))) print("vec[0] = {}, vec(1) = {}".format(vec[0], vec[1])) vec1 = Vector([2, 3]) print("vec1 = {}".format(vec1)) print("vec + vec1 = {}".format(vec + vec1)) print("vec - vec1 = {}".format(vec - vec1)) print("{} * {} = {}".format(vec, 2, vec * 2)) print("{} * {} = {}".format(2, vec, 2 * vec)) print("+{} = {}".format(vec, +vec)) print("-{} = {}".format(vec, -vec)) zero2 = Vector.zero(2) print(zero2)
from playLA.Vector import Vector if __name__ == '__main__': u = Vector([5, 2]) print(u * 3) print(-u) # 零向量 zero2 = Vector.zero(2) print('零向量:{}'.format(zero2)) norm = u.norm() print('u的模为:{}'.format(norm)) normlize = u.normalize() print('u的归一化:{}'.format(normlize)) try: zero2.normalize() except ZeroDivisionError: print("can not be divided by zero") u1 = Vector([3, 1]) dot = u.dot(u1) print('u和u1的点乘结果:{}'.format(dot))
from playLA.Vector import Vector from playLA.GramSchmidtProcess import gram_schmidt_process 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() basis4 = [ Vector([1, 1, 5, 2]), Vector([-3, 3, 4, -2]), Vector([-1, -2, 2, 5]) ] res4 = gram_schmidt_process(basis4) res4 = [row / row.norm() for row in res4] for row in res4: print(row) print(res4[0].dot(res4[1])) print(res4[0].dot(res4[2])) print(res4[1].dot(res4[2])) print()
from playLA.Vector import Vector if __name__ == "__main__": vec = Vector([2, 5]) print(vec, len(vec)) print("vec[0] = {}, vec[1] = {}".format(vec[0], vec[1])) vec2 = Vector([3, 1]) print("{} + {} = {}".format(vec, vec2, vec + vec2)) print("{} - {} = {}".format(vec, vec2, vec - vec2)) print("{} * {} = {}".format(vec, 3, vec * 3)) print("{} * {} = {}".format(3, vec, 3 * vec)) print("+{} = {}".format(vec, +vec)) print("-{} = {}".format(vec, -vec)) zero = Vector.zero(2) print(zero) print("{} + {} = {}".format(vec, zero, vec + zero)) print("norm({}) = {}".format(vec, vec.norm())) print("normalize {} is {}".format(vec, vec.normalize())) print(vec.normalize().norm()) try: zero.normalize() except ZeroDivisionError: print("Cannot normalize zero vector {}.".format(zero))
from playLA.Vector import Vector if __name__ == "__main__": vec = Vector([5, 2]) vec2 = Vector([3,4]) print(vec + vec2) print(dir(vec)) # print(vec) # print(len(vec)) # print("vec[0]={0}, vec[1]={1}".format(vec[0], vec[1]))
from playLA.Vector import Vector from playLA.GramSchmidtProcess import gram_schmidt_process if __name__ == "__main__": basis1 = [Vector([2, 2]), Vector([3, 5])] res1 = gram_schmidt_process(basis1) for row in res1: print(row) res1 = []
from playLA.Matrix import Matrix from playLA.Vector import Vector from playLA.LinearSystem import LinearSystem if __name__ == "__main__": A1 = Matrix([[1, -3, 5], [2, -1, -3], [3, 1, 4]]) b1 = Vector([-9, 19, -13]) ls1 = LinearSystem(A1, b1) ls1.gauss_jordan_elimination() ls1.fancy_print() print() A2 = Matrix([[1, 1, 1], [1, -1, -1], [2, 1, 5]]) b2 = Vector([3, -1, 8]) 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)
from playLA.Vector import Vector if __name__ == "__main__": vec = Vector([5,-3]) print(vec) print(len(vec)) print(vec[0]) vec2 = Vector([3,1]) print("{} + {} = {}".format(vec, vec2, vec2 + vec)) print("{} - {} = {}".format(vec, vec2, vec - vec2)) print("{} * {} = {}".format(vec, 3, vec * 3)) print("{} * {} = {}".format(3, vec, 3 * vec)) print("+{} = {}".format(vec, +vec)) print("-{} = {}".format(vec, -vec)) zero2 = Vector.zero(2) print(zero2) print("{} + {} = {}".format(vec, zero2, vec+zero2)) print("norm({}) = {}".format(vec, vec.norm())) print("norm({}) = {}".format(vec2, vec2.norm())) print("normalization({})= {}".format(vec, vec.normalize())) print("normalization({})= {}".format(zero2, zero2.normalize()))
from playLA.Matrix import Matrix from playLA.Vector import Vector from playLA.LinearSystem import LinearSystem, inv, rank 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() A7 = Matrix([[1, -1, 2, 0, 3], [-1, 1, 0, 2, -5], [1, -1, 4, 2, 4], [-2, 2, -5, -1, -3]]) b7 = Vector([1, 5, 13, -1]) ls7 = LinearSystem(A7, b7) ls7.gauss_jordan_elimination() ls7.fancy_print() print() A8 = Matrix([[2, 2], [2, 1], [1, 2]]) b8 = Vector([3, 2.5, 7]) ls8 = LinearSystem(A8, b8) if not ls8.gauss_jordan_elimination(): print("No Solution!") ls8.fancy_print() print() A9 = Matrix([[1, 2], [3, 4]]) invA = inv(A9) print(invA)
from playLA.Vector import Vector if __name__ == "__main__": vec = Vector([5, 2]) print(vec) print(type(vec)) print("len(vec) = {}".format(len(vec))) print("vec[0] = {}, vec[1] = {}".format(vec[0], vec[1]))
def row_vector(self, index): return Vector(self._values[index])
from playLA.Vector import Vector if __name__ == "__main__": vec = Vector([5, 2]) print(vec) print(len(vec)) print("vec[0] = {}, vec[1] = {}".format(vec[0], vec[1])) vec2 = Vector([3, 1]) print("{} + {} = {}".format(vec, vec2, vec + vec2)) print("{} - {} = {}".format(vec, vec2, vec - vec2)) print("{} * {} = {}".format(vec, 3, vec * 3)) print("{} * {} = {}".format(3, vec, 3 * vec)) print("+{} = {}".format(vec, +vec)) print("-{} = {}".format(vec, -vec)) zero2 = Vector.zero(2) print(zero2) print("{} + {} = {}".format(vec, zero2, vec + zero2)) print("norm({}) = {}".format(vec, vec.norm())) print("normalize {} is {}".format(vec, vec.normalize())) try: zero2.normalize() except ZeroDivisionError: print("Cannot normalize zero vector {}. ".format(zero2)) print(vec.dot(vec2))
from playLA.Vector import Vector if __name__ == "__main__": vec = Vector([5, 2]) print(vec) print("len(vec) = {}".format(len(vec))) print("vec[0] = {}, vec[1] = {}".format(vec[0], vec[1])) vec2 = Vector([3, 1]) print("{} + {} = {}".format(vec, vec2, vec + vec2)) print("{} - {} = {}".format(vec, vec2, vec - vec2)) print("{} * {} = {}".format(vec, 3, vec * 3)) print("{} * {} = {}".format(3, vec, 3 * vec)) print("+{} = {}".format(vec, +vec)) print("-{} = {}".format(vec, -vec)) zero2 = Vector.zero(2) print(zero2) print("{} + {} = {}".format(vec, zero2, vec + zero2))
from playLA.GramSchmidtProcess import gram_schmidt_process from playLA.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()
from playLA.Matrix import Matrix from playLA.Vector import Vector from playLA.LinearSystem import LinearSystem from playLA.LinearSystem1 import * 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)
from playLA.Matrix import Matrix from playLA.Vector import Vector from playLA.LinearSystem import LinearSystem from playLA.LinearSystem import inv, rank 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, -1, 2, 0, 3], [-1, 1, 0, 2, -5], [1, -1, 4, 2, 4], [-2, 2, -5, -1, -3]]) b3 = Vector([1, 5, 13, -1]) ls3 = LinearSystem(A3, b3) if not ls3.gauss_jordan_elimination(): print("No solution") print("ls3") ls3.fancy_print() print() A4 = Matrix([[2, 2], [2, 1], [1, 2]])
from playLA.LinearSystem import LinearSystem from playLA.Matrix import Matrix from playLA.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() 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()
def row_vector(self, index): """返回矩阵第index个行向量""" return Vector(self._values[index])