Skip to content

Commit e5cfa3e

Browse files
committed
rework gesdvd! to accept two copies of input but only modify one
1 parent 0d9989c commit e5cfa3e

2 files changed

Lines changed: 6 additions & 8 deletions

File tree

src/implementations/svd.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ function svd_full!(A::AbstractMatrix, USVᴴ, alg::LAPACK_SVDAlgorithm)
134134
elseif alg isa LAPACK_SafeDivideAndConquer
135135
isempty(alg_kwargs) ||
136136
throw(ArgumentError("invalid keyword arguments for LAPACK_SafeDivideAndConquer"))
137-
YALAPACK.gesdvd!(A, view(S, 1:minmn, 1), U, Vᴴ)
137+
YALAPACK.gesdvd!(A, copy_input(svd_full, A), view(S, 1:minmn, 1), U, Vᴴ)
138138
elseif alg isa LAPACK_Bisection
139139
throw(ArgumentError("LAPACK_Bisection is not supported for full SVD"))
140140
elseif alg isa LAPACK_Jacobi
@@ -179,7 +179,7 @@ function svd_compact!(A::AbstractMatrix, USVᴴ, alg::LAPACK_SVDAlgorithm)
179179
elseif alg isa LAPACK_SafeDivideAndConquer
180180
isempty(alg_kwargs) ||
181181
throw(ArgumentError("invalid keyword arguments for LAPACK_SafeDivideAndConquer"))
182-
YALAPACK.gesdvd!(A, diagview(S), U, Vᴴ)
182+
YALAPACK.gesdvd!(A, copy_input(svd_compact, A), diagview(S), U, Vᴴ)
183183
elseif alg isa LAPACK_Bisection
184184
YALAPACK.gesvdx!(A, diagview(S), U, Vᴴ; alg_kwargs...)
185185
elseif alg isa LAPACK_Jacobi
@@ -218,7 +218,7 @@ function svd_vals!(A::AbstractMatrix, S, alg::LAPACK_SVDAlgorithm)
218218
elseif alg isa LAPACK_SafeDivideAndConquer
219219
isempty(alg_kwargs) ||
220220
throw(ArgumentError("invalid keyword arguments for LAPACK_SafeDivideAndConquer"))
221-
YALAPACK.gesdvd!(A, S, U, Vᴴ)
221+
YALAPACK.gesdvd!(A, copy_input(svd_vals, A), S, U, Vᴴ)
222222
elseif alg isa LAPACK_Bisection
223223
YALAPACK.gesvdx!(A, S, U, Vᴴ; alg_kwargs...)
224224
elseif alg isa LAPACK_Jacobi

src/yalapack.jl

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2169,14 +2169,12 @@ for (gesvd, gesdd, gesvdx, gejsv, gesvj, elty, relty) in
21692169
end
21702170
return (S, U, Vᴴ), info[]
21712171
end
2172-
#! format: off
21732172
function gesdvd!( # SafeSVD implementation
2174-
A::AbstractMatrix{$elty},
2173+
A::AbstractMatrix{$elty}, Ac::AbstractMatrix{$elty} = copy(A), # only modifies Ac!
21752174
S::AbstractVector{$relty} = similar(A, $relty, min(size(A)...)),
21762175
U::AbstractMatrix{$elty} = similar(A, $elty, size(A, 1), min(size(A)...)),
21772176
Vᴴ::AbstractMatrix{$elty} = similar(A, $elty, min(size(A)...), size(A, 2))
21782177
)
2179-
#! format: on
21802178
require_one_based_indexing(A, U, Vᴴ, S)
21812179
chkstride1(A, U, Vᴴ, S)
21822180
m, n = size(A)
@@ -2192,10 +2190,10 @@ for (gesvd, gesdd, gesvdx, gejsv, gesvj, elty, relty) in
21922190
else
21932191
rwork = nothing
21942192
end
2195-
Ac = copy(A)
21962193
(S, U, Vᴴ), info = _gesdd_body!(Ac, S, U, Vᴴ, work, rwork)
21972194
if info > 0
2198-
(S, U, Vᴴ), info = _gesvd_body!(A, S, U, Vᴴ, work, rwork)
2195+
copy!(Ac, A)
2196+
(S, U, Vᴴ), info = _gesvd_body!(Ac, S, U, Vᴴ, work, rwork)
21992197
end
22002198
chklapackerror(info)
22012199
return S, U, Vᴴ

0 commit comments

Comments
 (0)