Skip to content

Add uplo check to Bidiagonal, Symmetric, and Hermitian base constructors #42466

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
Oct 5, 2021
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
3 changes: 1 addition & 2 deletions stdlib/LinearAlgebra/src/LinearAlgebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,7 @@ function sym_uplo(uplo::Char)
end
end


@noinline throw_uplo() = throw(ArgumentError("uplo argument must be either :U (upper) or :L (lower)"))
@noinline throw_uplo() = throw(ArgumentError("uplo argument must be either :U/'U' (upper) or :L/'L' (lower)"))
Comment on lines -266 to +265
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@andreasnoack I guess this change here should be reverted according to your comment that the Char representation is internal? The additional checks in this PR should be fine, though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. I'd think so. At least temporarily until a decision has been made.



"""
Expand Down
4 changes: 3 additions & 1 deletion stdlib/LinearAlgebra/src/bidiag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ struct Bidiagonal{T,V<:AbstractVector{T}} <: AbstractMatrix{T}
if length(ev) != max(length(dv)-1, 0)
throw(DimensionMismatch("length of diagonal vector is $(length(dv)), length of off-diagonal vector is $(length(ev))"))
end
(uplo != 'U' && uplo != 'L') && throw_uplo()
new{T,V}(dv, ev, uplo)
end
end
Expand Down Expand Up @@ -62,7 +63,7 @@ julia> Bl = Bidiagonal(dv, ev, :L) # ev is on the first subdiagonal
```
"""
function Bidiagonal(dv::V, ev::V, uplo::Symbol) where {T,V<:AbstractVector{T}}
Bidiagonal{T,V}(dv, ev, char_uplo(uplo))
Bidiagonal{T,V}(dv, ev, uplo)
end
function Bidiagonal(dv::V, ev::V, uplo::AbstractChar) where {T,V<:AbstractVector{T}}
Bidiagonal{T,V}(dv, ev, uplo)
Expand Down Expand Up @@ -109,6 +110,7 @@ function Bidiagonal(A::AbstractMatrix, uplo::Symbol)
Bidiagonal(diag(A, 0), diag(A, uplo === :U ? 1 : -1), uplo)
end


Bidiagonal(A::Bidiagonal) = A
Bidiagonal{T}(A::Bidiagonal{T}) where {T} = A
Bidiagonal{T}(A::Bidiagonal) where {T} = Bidiagonal{T}(A.dv, A.ev, A.uplo)
Expand Down
2 changes: 2 additions & 0 deletions stdlib/LinearAlgebra/src/symmetric.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ struct Symmetric{T,S<:AbstractMatrix{<:T}} <: AbstractMatrix{T}

function Symmetric{T,S}(data, uplo) where {T,S<:AbstractMatrix{<:T}}
require_one_based_indexing(data)
(uplo != 'U' && uplo != 'L') && throw_uplo()
new{T,S}(data, uplo)
end
end
Expand Down Expand Up @@ -88,6 +89,7 @@ struct Hermitian{T,S<:AbstractMatrix{<:T}} <: AbstractMatrix{T}

function Hermitian{T,S}(data, uplo) where {T,S<:AbstractMatrix{<:T}}
require_one_based_indexing(data)
(uplo != 'U' && uplo != 'L') && throw_uplo()
new{T,S}(data, uplo)
end
end
Expand Down
1 change: 1 addition & 0 deletions stdlib/LinearAlgebra/test/bidiag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Random.seed!(1)
@test ubd.dv === x
@test lbd.ev === y
@test_throws ArgumentError Bidiagonal(x, y, :R)
@test_throws ArgumentError Bidiagonal(x, y, 'R')
x == dv0 || @test_throws DimensionMismatch Bidiagonal(x, x, :U)
@test_throws MethodError Bidiagonal(x, y)
# from matrix
Expand Down
4 changes: 4 additions & 0 deletions stdlib/LinearAlgebra/test/symmetric.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ end
@test Hermitian(Hermitian(aherm, :U), :U) === Hermitian(aherm, :U)
@test_throws ArgumentError Symmetric(Symmetric(asym, :U), :L)
@test_throws ArgumentError Hermitian(Hermitian(aherm, :U), :L)

@test_throws ArgumentError Symmetric(asym, :R)
@test_throws ArgumentError Hermitian(asym, :R)

# mixed cases with Hermitian/Symmetric
if eltya <: Real
@test Symmetric(Hermitian(aherm, :U)) === Symmetric(aherm, :U)
Expand Down