def qr_gs_inverse(A: matrix): B = matrix(A.size1, A.size2) I = matrix(A.size1, A.size2) e_i = vector(I.size1) c = vector(I.size1) for ii in range(I.size1): for jj in range(I.size2): if ii == jj: I[ii, jj] = 1 else: I[ii, jj] = 0 for ii in range(A.size1): for jj in range(A.size1): e_i[jj] = matrix.get(I, jj, ii) c = qr_gs_solve(A, e_i) for jj in range(A.size2): B[jj, ii] = vector.get(c, jj) return B
def outer(a: vector, b: vector): A = matrix(a.size, b.size) for ii in range(a.size): for jj in range(b.size): A[ii,jj] = a[ii] * b[jj] return A
def qr_gs_decomp(A: matrix): n = A.size1 m = A.size2 Q = matrix(n, m) R = matrix(m, m) A_new = mt_copy(A) for ii in range(m): #iteration over columns R[ii, ii] = matrix.dot_prod(A_new.get_col(ii), A_new.get_col(ii))**(1. / 2) for jj in range(n): #iteration over rows Q[jj, ii] = A_new[jj, ii] / R[ii, ii] for kk in range(ii + 1, m): R[ii, kk] = matrix.dot_prod(Q.get_col(ii), A_new.get_col(kk)) for ll in range(n): A_new[ll, kk] = A_new[ll, kk] - Q[ll, ii] * R[ii, kk] return Q, R
import numpy as np import qr_decomp from eigen import * from matrix_codes import matrix, trans, matrix_mult import random from vector_codes import vector, mt_vt_mult n = 3 A = matrix(n, n) N = matrix(n, n) for ii in range(0, n): A[ii, ii] = np.random.random() N[ii, ii] = np.random.random() for jj in range(ii, n): A[ii, jj] = np.random.random() A[jj, ii] = A[ii, jj] N[ii, jj] = np.random.random() N[jj, ii] = N[ii, jj] NtN = matrix_mult(trans(N), N) for ii in range(NtN.size1): N[ii, ii] = NtN[ii, ii] #Make N real positive definite print("Printing matrix A") matrix.printing(A) print("\n\nPrinting matrix N") matrix.printing(N)
import numpy as np import qr_decomp from matrix_codes import matrix, trans, matrix_mult import random from vector_codes import vector, mt_vt_mult n = 5 m = 3 A = matrix(n, m) for ii in range(0, n): for jj in range(0, m): A[ii, jj] = np.random.random() print("\n----------Part A.1----------\n") print("\nTesting decomposition:\n") Q, R = qr_decomp.qr_gs_decomp(A) print("My Q matrix: \n") matrix.printing(Q) print("My R matrix: \n") matrix.printing(R) print("My QTQ matrix: \n") QT = trans(Q) QTQ = matrix_mult(QT, Q) matrix.printing(QTQ)