import numpy as np
import scipy
import matplotlib.pyplot as plt
# This next line sets the default output precission for Numpy stuff.
np.set_printoptions(precision=4, suppress=True)
A = np.array([[1.,2,3],[4,5,6],[7,8,1]])
print(A)
[[1. 2. 3.] [4. 5. 6.] [7. 8. 1.]]
P, L, U = scipy.linalg.lu(A)
print(P,'\n')
print(L,'\n')
print(U)
[[0. 1. 0.] [0. 0. 1.] [1. 0. 0.]] [[1. 0. 0. ] [0.1429 1. 0. ] [0.5714 0.5 1. ]] [[7. 8. 1. ] [0. 0.8571 2.8571] [0. 0. 4. ]]
A
array([[1., 2., 3.], [4., 5., 6.], [7., 8., 1.]])
P @ L @ U
array([[1., 2., 3.], [4., 5., 6.], [7., 8., 1.]])
U
array([[7. , 8. , 1. ], [0. , 0.8571, 2.8571], [0. , 0. , 4. ]])
L
array([[1. , 0. , 0. ], [0.1429, 1. , 0. ], [0.5714, 0.5 , 1. ]])
myL = np.array([[1,0,0],[4,1.,0],[7,2,1]])
myU = np.array([[1.,2,3],[0,-3,-6],[0,0,-8]])
print(myL,'\n')
print(myU,'\n')
print(myL@myU)
[[1. 0. 0.] [4. 1. 0.] [7. 2. 1.]] [[ 1. 2. 3.] [ 0. -3. -6.] [ 0. 0. -8.]] [[1. 2. 3.] [4. 5. 6.] [7. 8. 1.]]
A = np.random.rand(7,4)
#A[:,2] = A[:,0]
print(A)
[[0.5027 0.9672 0.0954 0.9943] [0.196 0.8704 0.0372 0.4618] [0.6588 0.4099 0.1037 0.0735] [0.0973 0.4428 0.8303 0.4044] [0.9057 0.9722 0.2053 0.6316] [0.53 0.8138 0.3113 0.2674] [0.8983 0.919 0.1748 0.2062]]
Q, R = np.linalg.qr(A, 'reduced')
print(Q, '\n')
print(R)
[[-0.3093 -0.4066 0.233 -0.6765] [-0.1206 -0.6918 0.3272 0.3701] [-0.4053 0.3944 -0.0774 0.0098] [-0.0598 -0.3551 -0.9028 -0.0906] [-0.5571 0.1002 0.0068 -0.3112] [-0.326 -0.2062 -0.1308 0.3915] [-0.5526 0.1483 0.023 0.3832]] [[-1.6256 -1.9114 -0.4381 -0.9702] [ 0. -0.925 -0.3361 -0.7996] [ 0. 0. -0.7585 -0.0139] [ 0. 0. 0. -0.5505]]
Q, R = np.linalg.qr(A, 'complete')
print(Q, '\n')
print(R)
[[-0.3093 -0.4066 0.233 -0.6765 -0.4636 0.0981 0.0506] [-0.1206 -0.6918 0.3272 0.3701 0.1997 -0.4412 -0.1682] [-0.4053 0.3944 -0.0774 0.0098 -0.2922 -0.4529 -0.6194] [-0.0598 -0.3551 -0.9028 -0.0906 -0.005 -0.2035 0.0756] [-0.5571 0.1002 0.0068 -0.3112 0.759 0.0806 0.0055] [-0.326 -0.2062 -0.1308 0.3915 -0.125 0.7276 -0.3685] [-0.5526 0.1483 0.023 0.3832 -0.2608 -0.115 0.6663]] [[-1.6256 -1.9114 -0.4381 -0.9702] [ 0. -0.925 -0.3361 -0.7996] [ 0. 0. -0.7585 -0.0139] [ 0. 0. 0. -0.5505] [ 0. 0. 0. 0. ] [ 0. 0. 0. 0. ] [ 0. 0. 0. 0. ]]
print(A,'\n')
print(Q@R)
[[0.5027 0.9672 0.0954 0.9943] [0.196 0.8704 0.0372 0.4618] [0.6588 0.4099 0.1037 0.0735] [0.0973 0.4428 0.8303 0.4044] [0.9057 0.9722 0.2053 0.6316] [0.53 0.8138 0.3113 0.2674] [0.8983 0.919 0.1748 0.2062]] [[0.5027 0.9672 0.0954 0.9943] [0.196 0.8704 0.0372 0.4618] [0.6588 0.4099 0.1037 0.0735] [0.0973 0.4428 0.8303 0.4044] [0.9057 0.9722 0.2053 0.6316] [0.53 0.8138 0.3113 0.2674] [0.8983 0.919 0.1748 0.2062]]
print(Q.T@Q)
[[ 1. 0. -0. 0. 0. -0. 0.] [ 0. 1. -0. 0. 0. -0. 0.] [-0. -0. 1. 0. -0. -0. -0.] [ 0. 0. 0. 1. 0. 0. 0.] [ 0. 0. -0. 0. 1. 0. -0.] [-0. -0. -0. 0. 0. 1. -0.] [ 0. 0. -0. 0. -0. -0. 1.]]
plt.spy(Q.T@Q, precision=0.0001);