using LinearAlgebra
Copyright By PowCoder代写 加微信 powcoder
A = randn(n, n)
b = randn(n)
A[1,1] = 0.0001
100×100 Matrix{Float64}:
0.0001 2.05786 0.642939 … -0.937449 -0.436383 1.00274
0.857705 -0.0512408 0.704025 -0.623807 -0.796627 -0.0620374
0.468861 1.77821 0.676941 0.50701 -0.785322 -1.3562
1.44907 -0.908546 -2.20951 0.0937038 0.39236 -1.38619
-0.826472 -0.102076 -1.22126 -0.707755 0.356802 -0.0522832
-0.0481242 0.95191 -0.72975 … -0.726723 0.358165 0.812479
0.9543 1.66796 -0.0221621 0.0784163 -1.32475 0.557905
0.902898 -0.641115 0.264211 0.297853 0.44419 0.8591
1.10412 1.67546 -0.612531 -2.36997 1.03513 0.758801
-0.385931 0.787924 -1.23427 0.453515 -1.16654 -0.0348245
1.5289 -0.010966 1.35357 … -0.168868 -0.329057 0.61933
0.279126 -0.0662927 0.0151347 -0.0180698 -0.558747 0.441856
0.74122 -0.486221 0.493939 0.371204 0.47901 0.273129
⋮ ⋱
-2.2635 -0.115231 0.621221 1.21392 -0.846812 1.14172
1.8773 -0.533425 1.47308 -0.942231 -1.24062 0.753151
0.539997 1.70058 1.70723 … -1.45577 -1.35289 0.725339
-0.0417826 0.223788 -0.747316 -1.84453 1.13227 -0.775587
0.115233 0.178672 -0.254488 -0.468145 -0.344758 -0.598009
2.02165 0.141459 0.410795 1.33229 -0.0330919 -0.282807
-1.7683 -0.714073 1.32415 0.85207 0.816893 -1.19536
-1.36606 -0.189776 0.347993 … 1.24798 -0.428436 1.52507
-0.359039 1.29851 -0.0781375 0.0688121 1.59158 0.711133
-1.36238 -1.41046 1.83152 -0.00224417 -0.999825 0.847363
0.994962 -1.09873 0.335537 -0.977363 1.1815 -0.691121
-1.31725 -0.499813 0.534151 0.268023 -1.57036 0.621978
Q,R = qr(A)
norm(A\b – R\(Q’b))
3.149114569438207e-13
L,U = lu(A, Val(false))
norm((A \ b) – U\(L\b))
9.729096760892749e-10
L,U,p = lu(A)
P = I(n)[p,:]
A ≈ P’*L*U
norm(A\b – U\(L\b[p]))
norm(big.(A)\big.(b) – U\(L\b[p]))
2.881741908145771098896252118296898722966373362604164972714149997600724007398575e-13
norm(big.(A)\big.(b) – R\(Q’b))
5.172026616813518715703398297589574904140902327877671894015938794961047494916807e-14
LU Decomposition¶
L₁ = Matrix(1.0I, n, n)
L₁[2:end,1] = -A[2:end,1]/A[1,1]
L₂ = Matrix(1.0I, n, n)
L₂[3:end,2] = -A₁[3:end,2]/A₁[2,2]
Ls = Vector{Matrix{Float64}}(undef, n-1)
à = copy(A)
for j = 1:n-1
Ls[j] = Matrix(1.0I, n, n)
Ls[j][j+1:end,j] = -Ã[j+1:end,j]/Ã[j,j]
à = Ls[j]*Ã
U = UpperTriangular(Ã)
# U = Ls[n-1]…Ls[2]Ls[1]A ⇒ L = inv(Ls[1])…inv(Ls[n])
L = LowerTriangular(Matrix(1.0I, n, n))
for j = 1:n-1
L[j+1:end,j] = -Ls[j][j+1:end,j]
100×100 Matrix{Float64}:
0.0001 2.05786 0.642939 … -0.937449 -0.436383 1.00274
0.857705 -0.0512408 0.704025 -0.623807 -0.796627 -0.0620374
0.468861 1.77821 0.676941 0.50701 -0.785322 -1.3562
1.44907 -0.908546 -2.20951 0.0937038 0.39236 -1.38619
-0.826472 -0.102076 -1.22126 -0.707755 0.356802 -0.0522832
-0.0481242 0.95191 -0.72975 … -0.726723 0.358165 0.812479
0.9543 1.66796 -0.0221621 0.0784163 -1.32475 0.557905
0.902898 -0.641115 0.264211 0.297853 0.44419 0.8591
1.10412 1.67546 -0.612531 -2.36997 1.03513 0.758801
-0.385931 0.787924 -1.23427 0.453515 -1.16654 -0.0348245
1.5289 -0.010966 1.35357 … -0.168868 -0.329057 0.61933
0.279126 -0.0662927 0.0151347 -0.0180698 -0.558747 0.441856
0.74122 -0.486221 0.493939 0.371204 0.47901 0.273129
⋮ ⋱
-2.2635 -0.115231 0.621221 1.21392 -0.846812 1.14172
1.8773 -0.533425 1.47308 -0.942231 -1.24062 0.753151
0.539997 1.70058 1.70723 … -1.45577 -1.35289 0.725339
-0.0417826 0.223788 -0.747316 -1.84453 1.13227 -0.775587
0.115233 0.178672 -0.254488 -0.468145 -0.344758 -0.598009
2.02165 0.141459 0.410795 1.33229 -0.0330919 -0.282807
-1.7683 -0.714073 1.32415 0.85207 0.816893 -1.19536
-1.36606 -0.189776 0.347993 … 1.24798 -0.428436 1.52507
-0.359039 1.29851 -0.0781375 0.0688121 1.59158 0.711133
-1.36238 -1.41046 1.83152 -0.00224417 -0.999825 0.847363
0.994962 -1.09873 0.335537 -0.977363 1.1815 -0.691121
-1.31725 -0.499813 0.534151 0.268023 -1.57036 0.621978
inv(Ls[1])inv(Ls[2])
100×100 Matrix{Float64}:
1.0 0.0 0.0 0.0 0.0 … 0.0 0.0 0.0 0.0 0.0 0.0 0.0
8577.05 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
4688.61 0.546543 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
14490.7 1.68952 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
-8264.72 -0.963576 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
-481.242 -0.0561619 0.0 0.0 0.0 … 0.0 0.0 0.0 0.0 0.0 0.0 0.0
9543.0 1.11252 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
9028.98 1.05272 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
11041.2 1.2872 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
-3859.31 -0.450001 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
15289.0 1.78254 0.0 0.0 0.0 … 0.0 0.0 0.0 0.0 0.0 0.0 0.0
2791.26 0.325436 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
7412.2 0.864214 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
⋮ ⋱ ⋮
-22635.0 -2.639 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
18773.0 2.18877 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
5399.97 0.629486 0.0 0.0 0.0 … 0.0 0.0 0.0 0.0 0.0 0.0 0.0
-417.826 -0.0487269 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
1152.33 0.134339 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
20216.5 2.35703 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0
-17683.0 -2.06162 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0
-13660.6 -1.59268 0.0 0.0 0.0 … 0.0 0.0 1.0 0.0 0.0 0.0 0.0
-3590.39 -0.418677 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0
-13623.8 -1.58831 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0
9949.62 1.16009 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0
-13172.5 -1.53575 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0
100×100 Matrix{Float64}:
1.0 0.0 0.0 0.0 0.0 0.0 … 0.0 0.0 0.0 0.0 0.0 0.0 0.0
8577.05 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
4688.61 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
14490.7 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
-8264.72 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
-481.242 0.0 0.0 0.0 0.0 1.0 … 0.0 0.0 0.0 0.0 0.0 0.0 0.0
9543.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0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