forked from Jutho/KrylovKit.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsvdsolve.jl
52 lines (46 loc) · 2.14 KB
/
svdsolve.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
@testset "GKL - svdsolve full ($mode)" for mode in (:vector, :inplace, :outplace, :mixed)
scalartypes = mode === :vector ? (Float32, Float64, ComplexF32, ComplexF64) :
(ComplexF64,)
orths = mode === :vector ? (cgs2, mgs2, cgsr, mgsr) : (mgsr,)
@testset for T in scalartypes
@testset for orth in orths
A = rand(T, (n, n))
alg = GKL(; orth=orth, krylovdim=2 * n, maxiter=1, tol=tolerance(T))
S, lvecs, rvecs, info = @constinferred svdsolve(wrapop(A, Val(mode)),
wrapvec(A[:, 1], Val(mode)), n,
:LR, alg)
@test S ≈ svdvals(A)
U = stack(unwrapvec, lvecs)
V = stack(unwrapvec, rvecs)
@test U' * U ≈ I
@test V' * V ≈ I
@test A * V ≈ U * Diagonal(S)
end
end
end
@testset "GKL - svdsolve iteratively ($mode)" for mode in
(:vector, :inplace, :outplace, :mixed)
scalartypes = mode === :vector ? (Float32, Float64, ComplexF32, ComplexF64) :
(ComplexF64,)
orths = mode === :vector ? (cgs2, mgs2, cgsr, mgsr) : (mgsr,)
@testset for T in scalartypes
@testset for orth in orths
A = rand(T, (2 * N, N))
v = rand(T, (2 * N,))
n₁ = div(n, 2)
alg = GKL(; orth=orth, krylovdim=n, maxiter=10, tol=tolerance(T), eager=true)
S, lvecs, rvecs, info = @constinferred svdsolve(wrapop(A, Val(mode)),
wrapvec(v, Val(mode)),
n₁, :LR, alg)
l = info.converged
@test S[1:l] ≈ svdvals(A)[1:l]
U = stack(unwrapvec, lvecs)
V = stack(unwrapvec, rvecs)
@test U[:, 1:l]' * U[:, 1:l] ≈ I
@test V[:, 1:l]' * V[:, 1:l] ≈ I
R = stack(unwrapvec, info.residual)
@test A' * U ≈ V * Diagonal(S)
@test A * V ≈ U * Diagonal(S) + R
end
end
end