Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

Numerical Methods Homework

Views: 706
Kernel: SageMath 8.2

Q-R Factorization

import numpy as np A = np.array([[1,1],[2,1],[1,2],[0,3]]) b = [[3],[5],[5],[5]] show("A is: ", A) Q,R = np.linalg.qr(A) show("Q is: ", Q) show("R is: ", R) right_hand_side = np.dot(Q.T,b) x = var('x') show(R,x, ' = ', right_hand_side) show (x,' = ',np.linalg.lstsq(R,right_hand_side)[0]) show ('2-norm',' = ',np.linalg.lstsq(R,right_hand_side)[1]) show ('rank',' = ',np.linalg.lstsq(R,right_hand_side)[2])
# import numpy as np A = np.array([[1,2,2],[2,-1,2],[3,1,1],[1,1,-1]]) b = [[10],[5],[10],[3]] show("A is: ", A) Q,R = np.linalg.qr(A) show("Q is: ", Q) show("R is: ", R) right_hand_side = np.dot(Q.T,b) x = var('x') show(R,x, ' = ', right_hand_side) show (x,' = ',np.linalg.lstsq(R,right_hand_side)[0]) show ('2-norm',' = ',np.linalg.lstsq(R,right_hand_side)[1]) show ('rank',' = ',np.linalg.lstsq(R,right_hand_side)[2]) #returns x, 2-norm for each column, rank