def qr_house(A): """ computes QR decomposition using Householder relections (Q,R) = qr_house(A) such that 0 = Q R - A 0 = dot(Q.T,Q) - eye(M) R upper triangular Parameters ---------- A: array_like shape(A) = (M, N), M >= N overwritten on exit Returns ------- R: array_like strict lower triangular part contains the Householder vectors v upper triangular matrix R Q: array_like orthogonal matrix """ M,N = A.shape Q = algopy.zeros((M,M),dtype=A) Q += numpy.eye(M) H = algopy.zeros((M,M),dtype=A) for n in range(N): v,beta = house(A[n:,n:n+1]) A[n:,n:] -= beta * algopy.dot(v, algopy.dot(v.T,A[n:,n:])) H[...] = numpy.eye(M) H[n:,n:] -= beta * algopy.dot(v,v.T) Q = algopy.dot(Q,H) return Q, algopy.triu(A)
res_1_list = [] res_2_list = [] res_3_list = [] betas = range(0, 200, 10) for beta in betas: print "perform QR decomposition" a = alpha(beta) A = a[0] * dot(x1, x1.T) + a[1] * dot(x2, x2.T) + a[2] * dot(x3, x3.T) Q, R = qr(A) res_1 = numpy.abs((dot(Q, R) - A).data).max() res_2 = numpy.abs((dot(Q.T, Q) - numpy.eye(N)).data).max() res_3 = numpy.abs((triu(R) - R).data).max() res_1_list.append(res_1) res_2_list.append(res_2) res_3_list.append(res_3) pyplot.figure() pyplot.semilogy(betas, res_1_list, "kd", label=r"max $ (| QR - A |)$") pyplot.semilogy(betas, res_2_list, "ko", label=r"max $ (| Q^T Q - I |)$") pyplot.semilogy(betas, res_3_list, "k.", label=r"max $ (| P_R \circ R - R |)$") pyplot.xlabel(r"$\beta$") pyplot.legend(loc="best") pyplot.savefig("qr_stability.eps")
# create matrix A by lowrank updates, alpha triggeres what the rank is res_1_list = [] res_2_list = [] res_3_list = [] betas = list(range(0, 200, 10)) for beta in betas: print('perform QR decomposition') a = alpha(beta) A = a[0] * dot(x1, x1.T) + a[1] * dot(x2, x2.T) + a[2] * dot(x3, x3.T) Q, R = qr(A) res_1 = numpy.abs((dot(Q, R) - A).data).max() res_2 = numpy.abs((dot(Q.T, Q) - numpy.eye(N)).data).max() res_3 = numpy.abs((triu(R) - R).data).max() res_1_list.append(res_1) res_2_list.append(res_2) res_3_list.append(res_3) pyplot.figure() pyplot.semilogy(betas, res_1_list, 'kd', label=r'max $ (| QR - A |)$') pyplot.semilogy(betas, res_2_list, 'ko', label=r'max $ (| Q^T Q - I |)$') pyplot.semilogy(betas, res_3_list, 'k.', label=r'max $ (| P_R \circ R - R |)$') pyplot.xlabel(r'$\beta$') pyplot.legend(loc='best') pyplot.savefig('qr_stability.eps') pyplot.show()