程序代写 3D Householder

3D Householder

Copyright By PowCoder代写 加微信 powcoder

using Plots, LinearAlgebra, Interact

1. Householder reflections¶
2. Nullspace & relationship to QR¶
3. Unitary matrices¶
4. Spectral theorem for normal matrices (e.g symmetric)¶

# use Interact.jl for sliders via @manipulate
@manipulate for x=-1.9:0.1:2, y=-1.9:0.1:2, z=-1.9:0.1:2
𝐱 = [x,y,z]
𝐲 = sign(x)norm(𝐱)*[1,0,0] + 𝐱

scatter3d([𝐱[1]], [𝐱[2]], [𝐱[3]]; label=”x”, legend=:bottomleft, xlims=(-2,2), ylims=(-2,2), zlims=(-2,2))
scatter3d!([𝐲[1]], [𝐲[2]], [𝐲[3]]; label=”y”)
plot!([0,𝐱[1]], [0,𝐱[2]], [0,𝐱[3]]; arrow=true, label=nothing, linecolor=:blue)
plot!([0,𝐲[1]], [0,𝐲[2]], [0,𝐲[3]]; arrow=true, label=nothing, linecolor=:red)

xs, ys = [-2,2], [-2,2]

Q = nullspace(𝐲’)
a,b = svd(Q[1:2,:]’) \ Q[3,:]
p = (x,y) -> a*x + b*y

surface!(xs, ys, p.(xs’, ys); alpha=0.5)

2. nullspace & QR¶

x = randn(3)
Q = nullspace(x’)

3×2 Matrix{Float64}:
-0.312579 -0.435032
0.947026 -0.0737261
-0.0737261 0.897392

1×2 adjoint(::Vector{Float64}) with eltype Float64:
4.85723e-17 5.55112e-17

A = randn(3, 5)

3×5 Matrix{Float64}:
-1.92751 0.805431 0.0607317 -0.429444 0.276918
0.497713 0.335423 -1.35182 0.0984736 1.21255
-0.297309 0.858589 0.853053 1.51698 0.512603

Q = nullspace(A)

3×2 Matrix{Float64}:
1.11765e-17 8.64739e-17
2.26358e-16 7.77359e-17
-8.2989e-17 -8.80754e-17

At_ex = [A’ Matrix(I, 5, 2)]

5×5 Matrix{Float64}:
-1.92751 0.497713 -0.297309 1.0 0.0
0.805431 0.335423 0.858589 0.0 1.0
0.0607317 -1.35182 0.853053 0.0 0.0
-0.429444 0.0984736 1.51698 0.0 0.0
0.276918 1.21255 0.512603 0.0 0.0

Q,R = qr(At_ex)

LinearAlgebra.QRCompactWY{Float64, Matrix{Float64}}
5×5 LinearAlgebra.QRCompactWYQ{Float64, Matrix{Float64}}:
-0.895905 -0.157057 -0.0260085 0.414742 2.77556e-17
0.374364 -0.220043 -0.369692 0.702172 -0.426281
0.0282281 0.707373 -0.393178 0.304193 0.501701
-0.199606 -0.0284649 -0.800154 -0.492135 -0.277316
0.128712 -0.652478 -0.260414 0.0146214 0.699769
5×5 Matrix{Float64}:
2.15146 -0.222079 0.375046 -0.895905 0.374364
0.0 -1.90218 0.0835515 -0.157057 -0.220043
0.0 0.0 -1.99239 -0.0260085 -0.369692
0.0 0.0 0.0 0.414742 0.702172
0.0 0.0 0.0 0.0 -0.426281

Q[:,1:3] # Span same as columns of A’ / rows of A
K = Q[:,4:5] # is orthogonal to Q[:,1:3] i.e., orthogonal to rows of A

5×2 Matrix{Float64}:
0.414742 2.77556e-17
0.702172 -0.426281
0.304193 0.501701
-0.492135 -0.277316
0.0146214 0.699769

A*K # K is an orthogonal basis for the kernel of A

3×2 Matrix{Float64}:
-3.63088e-17 3.01987e-17
-1.39276e-16 1.64954e-16
1.27883e-16 1.50969e-16

3. global variables in functions¶

struct Foo

function f(f::Foo)
x + 1 # forgot to do f.x so uses global x

f (generic function with 1 method)

function f(f::Foo)
x = f.x # new x is not related to global x
x + 1 # forgot to do f.x

f (generic function with 1 method)

6. PLU Decomposition¶

A = randn(5,5)
b = randn(5)

5-element Vector{Float64}:
-0.5256166438064742
1.0020975225799644
-1.7309511100809838
-0.06733853550112741
2.225089334126922

x_qr = qr(A) \ b
x_lu = lu(A) \ b

5-element Vector{Float64}:
1.5090645860069503
-0.5331464422627641
1.5403999380850075
0.2538660344520536
-1.0911567571174723

x_lu – x_qr

5-element Vector{Float64}:
4.440892098500626e-16
2.220446049250313e-16
2.220446049250313e-16
2.220446049250313e-16

A \ b # uses LU. Why?

5-element Vector{Float64}:
1.5090645860069503
-0.5331464422627641
1.5403999380850075
0.2538660344520536
-1.0911567571174723

A = randn(n,n)
b = randn(n)

@btime qr(A);
@btime lu(A);

203.275 μs (7 allocations: 134.55 KiB)
56.187 μs (4 allocations: 79.08 KiB)

A = randn(n,n)
b = randn(n)

x_lu = A \ b;
x_qr = qr(A) \ b;

norm((big.(A) \ b) – x_lu)

2.385770822771597243032090589811276883678781897541718389444591191101242625733643e-12

norm((big.(A) \ b) – x_qr)

2.611309765393680819091862494941147315514755797015178945885595298091839016252224e-13

function badmatrix(n)
A = Matrix(1I, n, n)
A[:,end] .= 1
for j = 1:n-1
A[j+1:end,j] .= -1
A = badmatrix(5)

5×5 Matrix{Int64}:
1 0 0 0 1
-1 1 0 0 1
-1 -1 1 0 1
-1 -1 -1 1 1
-1 -1 -1 -1 1

LU{Float64, Matrix{Float64}}
5×5 Matrix{Float64}:
1.0 0.0 0.0 0.0 0.0
-1.0 1.0 0.0 0.0 0.0
-1.0 -1.0 1.0 0.0 0.0
-1.0 -1.0 -1.0 1.0 0.0
-1.0 -1.0 -1.0 -1.0 1.0
5×5 Matrix{Float64}:
1.0 0.0 0.0 0.0 1.0
0.0 1.0 0.0 0.0 2.0
0.0 0.0 1.0 0.0 4.0
0.0 0.0 0.0 1.0 8.0
0.0 0.0 0.0 0.0 16.0

A = badmatrix(n)
b = randn(n)
norm(A \ b – qr(A) \ b) # lu fails on this matrix

7.433517009924015

A_ϵ = A .+ 0.0001randn.();

norm(A_ϵ \b – qr(A_ϵ)\b) # perturbed LU is fine

5.087866217629122e-12

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com