-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsvd.jl
More file actions
174 lines (168 loc) · 6.09 KB
/
svd.jl
File metadata and controls
174 lines (168 loc) · 6.09 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# Inputs
# ------
function copy_input(::typeof(svd_full), A::AbstractMatrix)
return copy!(similar(A, float(eltype(A))), A)
end
copy_input(::typeof(svd_compact), A::AbstractMatrix) = copy_input(svd_full, A)
copy_input(::typeof(svd_vals), A::AbstractMatrix) = copy_input(svd_full, A)
copy_input(::typeof(svd_trunc), A) = copy_input(svd_compact, A)
# TODO: many of these checks are happening again in the LAPACK routines
function check_input(::typeof(svd_full!), A::AbstractMatrix, USVᴴ)
m, n = size(A)
U, S, Vᴴ = USVᴴ
@assert U isa AbstractMatrix && S isa AbstractMatrix && Vᴴ isa AbstractMatrix
@check_size(U, (m, m))
@check_scalar(U, A)
@check_size(S, (m, n))
@check_scalar(S, A, real)
@check_size(Vᴴ, (n, n))
@check_scalar(Vᴴ, A)
return nothing
end
function check_input(::typeof(svd_compact!), A::AbstractMatrix, USVᴴ)
m, n = size(A)
minmn = min(m, n)
U, S, Vᴴ = USVᴴ
@assert U isa AbstractMatrix && S isa Diagonal && Vᴴ isa AbstractMatrix
@check_size(U, (m, minmn))
@check_scalar(U, A)
@check_size(S, (minmn, minmn))
@check_scalar(S, A, real)
@check_size(Vᴴ, (minmn, n))
@check_scalar(Vᴴ, A)
return nothing
end
function check_input(::typeof(svd_vals!), A::AbstractMatrix, S)
m, n = size(A)
minmn = min(m, n)
@assert S isa AbstractVector
@check_size(S, (minmn,))
@check_scalar(S, A, real)
return nothing
end
# Outputs
# -------
function initialize_output(::typeof(svd_full!), A::AbstractMatrix, ::LAPACK_SVDAlgorithm)
m, n = size(A)
U = similar(A, (m, m))
S = similar(A, real(eltype(A)), (m, n)) # TODO: Rectangular diagonal type?
Vᴴ = similar(A, (n, n))
return (U, S, Vᴴ)
end
function initialize_output(::typeof(svd_compact!), A::AbstractMatrix, ::LAPACK_SVDAlgorithm)
m, n = size(A)
minmn = min(m, n)
U = similar(A, (m, minmn))
S = Diagonal(similar(A, real(eltype(A)), (minmn,)))
Vᴴ = similar(A, (minmn, n))
return (U, S, Vᴴ)
end
function initialize_output(::typeof(svd_vals!), A::AbstractMatrix, ::LAPACK_SVDAlgorithm)
return similar(A, real(eltype(A)), (min(size(A)...),))
end
function initialize_output(::typeof(svd_trunc!), A::AbstractMatrix, alg::TruncatedAlgorithm)
return initialize_output(svd_compact!, A, alg.alg)
end
# Implementation
# --------------
function svd_full!(A::AbstractMatrix, USVᴴ, alg::LAPACK_SVDAlgorithm)
check_input(svd_full!, A, USVᴴ)
U, S, Vᴴ = USVᴴ
fill!(S, zero(eltype(S)))
m, n = size(A)
minmn = min(m, n)
if alg isa LAPACK_QRIteration
isempty(alg.kwargs) ||
throw(ArgumentError("LAPACK_QRIteration does not accept any keyword arguments"))
YALAPACK.gesvd!(A, view(S, 1:minmn, 1), U, Vᴴ)
elseif alg isa LAPACK_DivideAndConquer
isempty(alg.kwargs) ||
throw(ArgumentError("LAPACK_DivideAndConquer does not accept any keyword arguments"))
YALAPACK.gesdd!(A, view(S, 1:minmn, 1), U, Vᴴ)
elseif alg isa LAPACK_Bisection
throw(ArgumentError("LAPACK_Bisection is not supported for full SVD"))
elseif alg isa LAPACK_Jacobi
throw(ArgumentError("LAPACK_Bisection is not supported for full SVD"))
else
throw(ArgumentError("Unsupported SVD algorithm"))
end
for i in 2:minmn
S[i, i] = S[i, 1]
S[i, 1] = zero(eltype(S))
end
# TODO: make this controllable using a `gaugefix` keyword argument
for j in 1:max(m, n)
if j <= minmn
u = view(U, :, j)
v = view(Vᴴ, j, :)
s = conj(sign(argmax(abs, u)))
u .*= s
v .*= conj(s)
elseif j <= m
u = view(U, :, j)
s = conj(sign(argmax(abs, u)))
u .*= s
else
v = view(Vᴴ, j, :)
s = conj(sign(argmax(abs, v)))
v .*= s
end
end
return USVᴴ
end
function svd_compact!(A::AbstractMatrix, USVᴴ, alg::LAPACK_SVDAlgorithm)
check_input(svd_compact!, A, USVᴴ)
U, S, Vᴴ = USVᴴ
if alg isa LAPACK_QRIteration
isempty(alg.kwargs) ||
throw(ArgumentError("LAPACK_QRIteration does not accept any keyword arguments"))
YALAPACK.gesvd!(A, S.diag, U, Vᴴ)
elseif alg isa LAPACK_DivideAndConquer
isempty(alg.kwargs) ||
throw(ArgumentError("LAPACK_DivideAndConquer does not accept any keyword arguments"))
YALAPACK.gesdd!(A, S.diag, U, Vᴴ)
elseif alg isa LAPACK_Bisection
YALAPACK.gesvdx!(A, S.diag, U, Vᴴ; alg.kwargs...)
elseif alg isa LAPACK_Jacobi
isempty(alg.kwargs) ||
throw(ArgumentError("LAPACK_Jacobi does not accept any keyword arguments"))
YALAPACK.gesvj!(A, S.diag, U, Vᴴ)
else
throw(ArgumentError("Unsupported SVD algorithm"))
end
# TODO: make this controllable using a `gaugefix` keyword argument
for j in 1:size(U, 2)
u = view(U, :, j)
v = view(Vᴴ, j, :)
s = conj(sign(argmax(abs, u)))
u .*= s
v .*= conj(s)
end
return USVᴴ
end
function svd_vals!(A::AbstractMatrix, S, alg::LAPACK_SVDAlgorithm)
check_input(svd_vals!, A, S)
U, Vᴴ = similar(A, (0, 0)), similar(A, (0, 0))
if alg isa LAPACK_QRIteration
isempty(alg.kwargs) ||
throw(ArgumentError("LAPACK_QRIteration does not accept any keyword arguments"))
YALAPACK.gesvd!(A, S, U, Vᴴ)
elseif alg isa LAPACK_DivideAndConquer
isempty(alg.kwargs) ||
throw(ArgumentError("LAPACK_DivideAndConquer does not accept any keyword arguments"))
YALAPACK.gesdd!(A, S, U, Vᴴ)
elseif alg isa LAPACK_Bisection
YALAPACK.gesvdx!(A, S, U, Vᴴ; alg.kwargs...)
elseif alg isa LAPACK_Jacobi
isempty(alg.kwargs) ||
throw(ArgumentError("LAPACK_Jacobi does not accept any keyword arguments"))
YALAPACK.gesvj!(A, S, U, Vᴴ)
else
throw(ArgumentError("Unsupported SVD algorithm"))
end
return S
end
function svd_trunc!(A::AbstractMatrix, USVᴴ, alg::TruncatedAlgorithm)
USVᴴ′ = svd_compact!(A, USVᴴ, alg.alg)
return truncate!(svd_trunc!, USVᴴ′, alg)
end