# matrix3: [[1, 2, 0], [2, 1, 5], [1, 1, 2], [1, 0, 0], [4, 1, 2]] # matrix4: [[2, 0], [1, 3], [0, 1]] # matrix5: [[2, 0, 1], [1, 2, 2], [0, 3, 3]] # zero_2_2: [[0, 0], [0, 0]] # zero_2_3: [[0, 0, 0], [0, 0, 0]] # identity_3: [[1, 0, 0], [0, 1, 0], [0, 0, 1]] # vector: [2, 1, 0] matrix = Matrix([[1, 2], [3, 4]]) matrix2 = Matrix([[10, 20], [30, 40]]) matrix3 = Matrix([[1, 2, 0], [2, 1, 5], [1, 1, 2], [1, 0, 0], [4, 1, 2]]) matrix4 = Matrix([[2, 0], [1, 3], [0, 1]]) matrix5 = Matrix([[2, 0, 1], [1, 2, 2], [0, 3, 3]]) zero_2_2 = Matrix.zero(2, 2) zero_2_3 = Matrix.zero(2, 3) identity_3 = Matrix.identity(3) vector = Vector([2, 1, 0]) print("matrix:", matrix) print("matrix2:", matrix2) print("matrix3:", matrix3) print("matrix4:", matrix4) print("matrix5:", matrix5) print("zero_2_2:", zero_2_2) print("zero_2_3:", zero_2_3) print("identity_3:", identity_3) print("vector:", vector) print("------") # matrix[0]: [1, 2] # matrix[0, 1]: 2 # matrix.shape(): (2, 2)
from la.vector import Vector if __name__ == "__main__": # vector = [5, 2] # vector2 = [3, 1] # zero_2 = [0, 0] # zero_3 = [0, 0, 0] vector = Vector([5, 2]) vector2 = Vector([3, 1]) zero_2 = Vector.zero(2) zero_3 = Vector.zero(3) print("vector = {}".format(vector)) print("vector2 = {}".format(vector2)) print("zero_2 = {}".format(zero_2)) print("zero_3 = {}".format(zero_3)) print("------") # len(vector) = 2 # vector[0] = 5, vector[1] = 2 print("len(vector) = {}".format(len(vector))) print("vector[0] = {}, vector[1] = {}".format(vector[0], vector[1])) print("------") # [5, 2] + [3, 1] = [8, 3] # [5, 2] + [0, 0] = [5, 2] # [5, 2] - [3, 1] = [2, 1] # [5, 2] - [0, 0] = [5, 2] # [5, 2] * 3 = [15, 6] # 3 * [5, 2] = [15, 6]
from la.linear_system import LinearSystem from la.matrix import Matrix from la.vector import Vector if __name__ == "__main__": # 1.0 0.0 0.0 | 2.0 # -0.0 1.0 0.0 | -1.5 # 0.0 0.0 1.0 | 1.0 matrix = Matrix([[0, 4, 8], [2, 2, 1], [4, 8, 8]]) vector = Vector([2, 2, 4]) linear_system = LinearSystem(matrix, vector) linear_system.gauss_jordan_elimination() linear_system.augmented_matrix_fancy_print() print("------") # 1.0 0.0 0.0 | -1.0 # 0.0 1.0 0.0 | -2.0 # -0.0 -0.0 1.0 | 3.0 matrix = Matrix([[1, 2, 4], [3, 7, 2], [2, 3, 3]]) vector = Vector([7, -11, 1]) linear_system = LinearSystem(matrix, vector) linear_system.gauss_jordan_elimination() linear_system.augmented_matrix_fancy_print() print("------") # 1.0 0.0 0.0 | 2.0 # 0.0 1.0 0.0 | -3.0 # 0.0 0.0 1.0 | -4.0 matrix = Matrix([[1, -3, 5], [2, -1, -3], [3, 1, 4]])
from la.vector import Vector if __name__ == "__main__": # vector = [5, 2] # vector2 = [3, 1] vector = Vector([5, 2]) vector2 = Vector([3, 1]) print("vector = {}".format(vector)) print("vector2 = {}".format(vector2)) print("------") # len(vector) = 2 # vector[0] = 5, vector[1] = 2 print("len(vector) = {}".format(len(vector))) print("vector[0] = {}, vector[1] = {}".format(vector[0], vector[1]))
from la.linear_system import LinearSystem from la.matrix import Matrix from la.vector import Vector if __name__ == "__main__": # 1.0 0.0 0.0 | 2.0 # -0.0 1.0 0.0 | -1.5 # 0.0 0.0 1.0 | 1.0 # ONE # [0, 1, 2] matrix = Matrix([[0, 4, 8], [2, 2, 1], [4, 8, 8]]) vector = Vector([2, 2, 4]) linear_system = LinearSystem(matrix, vector) linear_system.gauss_jordan_elimination() linear_system.augmented_matrix_fancy_print() print(linear_system.determine_solution()) print(linear_system.pivots) print("------") # 1.0 0.0 0.0 | -1.0 # 0.0 1.0 0.0 | -2.0 # -0.0 -0.0 1.0 | 3.0 # ONE # [0, 1, 2] matrix = Matrix([[1, 2, 4], [3, 7, 2], [2, 3, 3]]) vector = Vector([7, -11, 1]) linear_system = LinearSystem(matrix, vector) linear_system.gauss_jordan_elimination() linear_system.augmented_matrix_fancy_print() print(linear_system.determine_solution())
# coding: utf-8 ''' @author: jessezhu @file: main_vector.py @time: 2019/8/15 2:46 PM @desc: ''' from la.vector import Vector if __name__ == '__main__': vector = Vector([1, 2, 3]) print("vector={}".format(vector)) print('vector[0]={},vector[1]={}'.format(vector[0], vector[1])) vector2 = Vector([1, 2, 3]) print("{}+{}={}".format(vector, vector2, vector + vector2)) print("{}-{}={}".format(vector, vector2, vector - vector2)) print("{}*{}={}".format(vector, 3, vector * 3)) print("{}*{}={}".format(3, vector, 3 * vector)) print("+{}={}".format(vector, +vector)) print("-{}={}".format(vector, -vector)) zero = Vector.zero(2) print("zero 2 = {} ".format(zero)) norm_vec = Vector([3, 4]) print('norm({}) = {}'.format(norm_vec, norm_vec.norm())) print('normalize({}) = {} and its norm = {}'.format(