Skip to content

Convert lower to upper bidgiagonal in _svd! and _svdvals! #158

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "GenericLinearAlgebra"
uuid = "14197337-ba66-59df-a3e3-ca00e7dcff7a"
version = "0.3.17"
version = "0.3.18"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand Down
22 changes: 21 additions & 1 deletion src/svd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ function svdIter!(
d[n2] = -G.s * ei1 + G.c * di1

else
throw(ArgumentError("lower bidiagonal version not implemented yet"))
throw(ArgumentError("please convert to upper bidiagonal"))
end

return B
Expand Down Expand Up @@ -254,6 +254,7 @@ function __svd!(
end

function _svdvals!(B::Bidiagonal{T}; tol = eps(T)) where {T<:Real}
B = Bidiagonal(B.dv, B.ev, :U)
__svd!(B, tol = tol)
return sort(abs.(diag(B)), rev = true)
end
Expand Down Expand Up @@ -283,12 +284,31 @@ function _sort_and_adjust!(U, s, Vᴴ)
return nothing
end

# Convert a lower Bidiagonal to an upper by running a single pass
# of givens rotations from the left. A noop if the Bidiagnoal is
# already upper.
function _lower_to_upper!(B::Bidiagonal, U::Matrix)
if istriu(B)
return B
end
for i in 1:length(B.ev)
G, r = givens(B.dv[i], B.ev[i], i, i + 1)
B.dv[i] = r
B.ev[i] = G.s * B.dv[i + 1]
B.dv[i + 1] = G.c * B.dv[i + 1]
rmul!(U, G')
end
return Bidiagonal(B.dv, B.ev, :U)
end

function _svd!(B::Bidiagonal{T}; tol = eps(T)) where {T<:Real}
n = size(B, 1)

U = Matrix{T}(I, n, n)
Vᴴ = Matrix{T}(I, n, n)

B = _lower_to_upper!(B, U)

__svd!(B, U, Vᴴ, tol = tol)

s = diag(B)
Expand Down
8 changes: 8 additions & 0 deletions test/svd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -266,4 +266,12 @@ using Test, GenericLinearAlgebra, LinearAlgebra, Quaternions, DoubleFloats
@test svdvals(BigFloat[0 0; 1 -1]) ≈ [sqrt(2), 0]
@test svdvals(BigFloat[1 0 0; 0 0 0; 0 1 -1]) ≈ [sqrt(2), 1, 0]
end

@testset "Lower Bidiagonal matrices. Issue 157" begin
B = Bidiagonal(randn(4), randn(3), :L)
@test GenericLinearAlgebra._svdvals!(copy(B)) ≈ GenericLinearAlgebra._svdvals!(copy(B'))
U, s, V = GenericLinearAlgebra._svd!(copy(B))
@test B ≈ U * Diagonal(s) * V'
@test_throws ArgumentError("please convert to upper bidiagonal") GenericLinearAlgebra.svdIter!(B, 1, size(B, 1), 1.0)
end
end