import numpy as np import matrixstitcher as mats from matrixstitcher.method import GramSchmidt v = mats.Matrix([[0, -20, -14], [3, 27, -4], [4, 11, -2]], dtype=np.float) print('-> Origin Matrix:') print(v) print('*' * 80) Q1, R1 = GramSchmidt()(v) print('-> GramSchmidt Reduction:') print('-> Q:') print(Q1) print('-> R:') print(R1) print('-> Q * R:') print(Q1 * R1)
import matrixstitcher as mats from matrixstitcher.method import LUFactorization # Edit your matrix to be factorized, here are two examples # A = [[0, 0, 9, 1], [3, 0, 5, 2], [6, 1, 0, 2], [1, 1, 1, 1]] A = [[1, 2, -3, 4], [4, 8, 12, -8], [2, 3, 2, 1], [-3, -1, 1, -4]] display_execution = True if __name__ == '__main__': A = mats.Matrix(A) with mats.TransformTape(): P, L, U = LUFactorization()(A) print('The second column of A:\n', A[:, 2], sep='') print('\n', '*' * 80, sep='') print('Execution Path:') result = A.forward(display=display_execution) print('\n', '*'*80, sep='') print('The LU Factorization result:') print('P:\n', P, sep='') print('L:\n', L, sep='') print('U:\n', U, sep='') print('\n', '*'*80, sep='') print('Proof P * A = L * U:') print('P * A:\n', P * A, sep='') print('L * U:\n', L * U, sep='')
import matplotlib.pyplot as plt import numpy as np import matrixstitcher as mats from matrixstitcher.method import LeastSquareTech x = np.array(list(range(-5, 6))) y = np.array([2, 7, 9, 12, 13, 14, 14, 13, 10, 8, 4]) plt.plot(x, y, 'kx', markersize=10) X1 = np.stack([np.ones(x.shape), x], axis=1) X1 = mats.Matrix(X1) X2 = np.stack([np.ones(x.shape), x, x * x], axis=1) X2 = mats.Matrix(X2) y = mats.Matrix(y) tech1 = LeastSquareTech() alpha1 = tech1(X1, y) print(alpha1[0], alpha1[1]) plt.plot(x, tech1.predict(X1).numpy(), c='r') tech2 = LeastSquareTech() alpha2 = tech2(X2, y) print(alpha2[0], alpha2[1]) plt.plot(x, tech2.predict(X2).numpy(), c='b') plt.grid(True) plt.show()