Skip to content

Commit 18fe185

Browse files
committed
Define adjoint for SVD
1 parent 32c1435 commit 18fe185

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

stdlib/LinearAlgebra/src/svd.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,10 @@ end
252252
size(A::SVD, dim::Integer) = dim == 1 ? size(A.U, dim) : size(A.Vt, dim)
253253
size(A::SVD) = (size(A, 1), size(A, 2))
254254

255+
function adjoint(F::SVD)
256+
return SVD(F.Vt', F.S, F.U')
257+
end
258+
255259
function show(io::IO, mime::MIME{Symbol("text/plain")}, F::SVD{<:Any,<:Any,<:AbstractArray})
256260
summary(io, F); println(io)
257261
println(io, "U factor:")

stdlib/LinearAlgebra/test/svd.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,4 +217,24 @@ end
217217
@test Uc * diagm(0=>Sc) * transpose(V) complex.(A) rtol=1e-3
218218
end
219219

220+
@testset "adjoint of SVD" begin
221+
n = 5
222+
B = randn(5, 2)
223+
224+
@testset "size(b)=$(size(b))" for b in (B[:, 1], B)
225+
@testset "size(A)=$(size(A))" for A in (
226+
randn(n, n),
227+
# Wide problems become minimum norm (in x) problems similarly to LQ
228+
randn(n + 2, n),
229+
randn(n - 2, n),
230+
complex.(randn(n, n), randn(n, n)))
231+
232+
F = svd(A)
233+
x = F'\b
234+
@test x A'\b
235+
@test length(size(x)) == length(size(b))
236+
end
237+
end
238+
end
239+
220240
end # module TestSVD

0 commit comments

Comments
 (0)