Skip to content
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

Permit Bidiagonal represents empty matrix #38392

Merged
merged 1 commit into from
Dec 9, 2020
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
6 changes: 3 additions & 3 deletions stdlib/LinearAlgebra/src/bidiag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ struct Bidiagonal{T,V<:AbstractVector{T}} <: AbstractMatrix{T}
uplo::Char # upper bidiagonal ('U') or lower ('L')
function Bidiagonal{T,V}(dv, ev, uplo::AbstractChar) where {T,V<:AbstractVector{T}}
require_one_based_indexing(dv, ev)
if length(ev) != length(dv)-1
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
new{T,V}(dv, ev, uplo)
Expand Down Expand Up @@ -352,7 +352,7 @@ function diag(M::Bidiagonal, n::Integer=0)
end

function +(A::Bidiagonal, B::Bidiagonal)
if A.uplo == B.uplo
if A.uplo == B.uplo || length(A.dv) == 0
Bidiagonal(A.dv+B.dv, A.ev+B.ev, A.uplo)
else
newdv = A.dv+B.dv
Expand All @@ -361,7 +361,7 @@ function +(A::Bidiagonal, B::Bidiagonal)
end

function -(A::Bidiagonal, B::Bidiagonal)
if A.uplo == B.uplo
if A.uplo == B.uplo || length(A.dv) == 0
Bidiagonal(A.dv-B.dv, A.ev-B.ev, A.uplo)
else
newdv = A.dv-B.dv
Expand Down
7 changes: 4 additions & 3 deletions stdlib/LinearAlgebra/src/structuredbroadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,12 @@ find_uplo(a) = nothing
find_uplo(bc::Broadcasted) = mapreduce(find_uplo, merge_uplos, bc.args, init=nothing)

function structured_broadcast_alloc(bc, ::Type{<:Bidiagonal}, ::Type{ElType}, n) where {ElType}
uplo = find_uplo(bc)
uplo = n > 0 ? find_uplo(bc) : 'U'
n1 = max(n - 1, 0)
if uplo == 'T'
return Tridiagonal(Array{ElType}(undef, n-1), Array{ElType}(undef, n), Array{ElType}(undef, n-1))
return Tridiagonal(Array{ElType}(undef, n1), Array{ElType}(undef, n), Array{ElType}(undef, n1))
end
return Bidiagonal(Array{ElType}(undef, n),Array{ElType}(undef, n-1), uplo)
return Bidiagonal(Array{ElType}(undef, n),Array{ElType}(undef, n1), uplo)
end
structured_broadcast_alloc(bc, ::Type{<:SymTridiagonal}, ::Type{ElType}, n) where {ElType} =
SymTridiagonal(Array{ElType}(undef, n),Array{ElType}(undef, n-1))
Expand Down
48 changes: 45 additions & 3 deletions stdlib/LinearAlgebra/test/bidiag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,19 @@ Random.seed!(1)
ev += im*convert(Vector{elty}, rand(1:10, n-1))
end
end
dv0 = zeros(elty, 0)
ev0 = zeros(elty, 0)

@testset "Constructors" begin
for (x, y) in ((dv, ev), (GenericArray(dv), GenericArray(ev)))
for (x, y) in ((dv0, ev0), (dv, ev), (GenericArray(dv), GenericArray(ev)))
# from vectors
ubd = Bidiagonal(x, y, :U)
lbd = Bidiagonal(x, y, :L)
@test ubd != lbd
@test ubd != lbd || x === dv0
@test ubd.dv === x
@test lbd.ev === y
@test_throws ArgumentError Bidiagonal(x, y, :R)
@test_throws DimensionMismatch Bidiagonal(x, x, :U)
x == dv0 || @test_throws DimensionMismatch Bidiagonal(x, x, :U)
@test_throws MethodError Bidiagonal(x, y)
# from matrix
@test Bidiagonal(ubd, :U) == Bidiagonal(Matrix(ubd), :U) == ubd
Expand Down Expand Up @@ -593,4 +595,44 @@ Base.transpose(n::MyNotANumberType) = n
@test transpose(copy(tB)) == B
end

@testset "empty bidiagonal matrices" begin
dv0 = zeros(0)
ev0 = zeros(0)
zm = zeros(0, 0)
ubd = Bidiagonal(dv0, ev0, :U)
lbd = Bidiagonal(dv0, ev0, :L)
@test size(ubd) == (0, 0)
@test_throws BoundsError getindex(ubd, 1, 1)
@test_throws BoundsError setindex!(ubd, 0.0, 1, 1)
@test similar(ubd) == ubd
@test similar(lbd, Int) == zeros(Int, 0, 0)
@test ubd == zm
@test lbd == zm
@test ubd == lbd
@test ubd * ubd == ubd
@test lbd + lbd == lbd
@test lbd' == ubd
@test ubd' == lbd
@test triu(ubd, 1) == ubd
@test triu(lbd, 1) == ubd
@test tril(ubd, -1) == ubd
@test tril(lbd, -1) == ubd
@test_throws ArgumentError triu(ubd)
@test_throws ArgumentError tril(ubd)
@test sum(ubd) == 0.0
@test reduce(+, ubd) == 0.0
@test reduce(+, ubd, dims=1) == zeros(1, 0)
@test reduce(+, ubd, dims=2) == zeros(0, 1)
@test hcat(ubd, ubd) == zm
@test vcat(ubd, lbd) == zm
@test hcat(lbd, ones(0, 3)) == ones(0, 3)
@test fill!(copy(ubd), 1.0) == ubd
@test map(abs, ubd) == zm
@test lbd .+ 1 == zm
@test lbd + ubd isa Bidiagonal
@test lbd .+ ubd isa Bidiagonal
@test ubd * 5 == ubd
@test ubd .* 3 == ubd
end

end # module TestBidiagonal
1 change: 1 addition & 0 deletions stdlib/SparseArrays/src/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,7 @@ end
SparseMatrixCSC(B::Bidiagonal{Tv}) where Tv = SparseMatrixCSC{Tv,Int}(B)
function SparseMatrixCSC{Tv,Ti}(B::Bidiagonal) where {Tv,Ti}
m = length(B.dv)
m == 0 && return SparseMatrixCSC{Tv,Ti}(zeros(Tv, 0, 0))

colptr = Vector{Ti}(undef, m+1)
colptr[1] = 1
Expand Down