julia icon indicating copy to clipboard operation
julia copied to clipboard

Missing Bidiagonal constructor

Open eschnett opened this issue 2 years ago • 5 comments

The Bidiagonal matrix type is missing a constructor from its own type:

julia> using LinearAlgebra
julia> A = Bidiagonal([1,2], [3], :U)
julia> typeof(A)(A)
ERROR: MethodError: no method matching Bidiagonal{Int64, Vector{Int64}}(::Bidiagonal{Int64, Vector{Int64}})

eschnett avatar Aug 30 '22 18:08 eschnett

Same for Tridiagonal. However, both Diagonal and SymTridiagonal have the respective constructor.

eschnett avatar Aug 30 '22 18:08 eschnett

Maybe just add these constructors?

using LinearAlgebra

function Bidiagonal{T,V}(A::AbstractMatrix) where {T,V<:AbstractVector{T}}
    if A.uplo == 'U'
        return Bidiagonal(A, :U)
    else
        return Bidiagonal(A, :L)
    end
end

A = Bidiagonal([1,2], [3], :U)
typeof(A)(A)
# 2×2 Bidiagonal{Int64, Vector{Int64}}:
# 1  3
# ⋅  2

typeof(A)(A) == A # true
using LinearAlgebra

function Tridiagonal{T,V}(A::AbstractMatrix) where {T,V}
    Tridiagonal(A)
end

A = Tridiagonal([1 2 3 4; 1 2 3 4; 1 2 3 4; 1 2 3 4])
typeof(A)(A)
# 4×4 Tridiagonal{Int64, Vector{Int64}}:
# 1  2  ⋅  ⋅
# 1  2  3  ⋅
# ⋅  2  3  4
# ⋅  ⋅  3  4

typeof(A)(A) == A # true

sosuts avatar Sep 01 '22 15:09 sosuts

This doesn't check whether the returned object actually has the type Bidiagonal{T,V} – it simply ignores the type parameters. That is, it would allow e.g. calling Bidiagonal{Complex,SparseVector{Int}}, and the return value might have the type Bidiagonal{Float64,Vector{Float64}}.

eschnett avatar Sep 08 '22 16:09 eschnett

If I'm not misunderstainding the comment, is this ok?

function Bidiagonal{T,V}(A::Bidiagonal) where {T,V<:AbstractVector{T}}
    Bidiagonal{T,V}(A.dv, A.ev, A.uplo)
end

function Tridiagonal{T,V}(A::Tridiagonal) where {T,V<:AbstractVector{T}}
    Tridiagonal{T,V}(A.dl, A.d, A.du)
end

Almost identical to the one of Diagonal.

Diagonal{T,V}(d::Diagonal) where {T,V<:AbstractVector{T}} = Diagonal{T,V}(d.diag)

sosuts avatar Sep 18 '22 15:09 sosuts

This looks fine.

It would be good to have respective test cases as well.

eschnett avatar Sep 19 '22 12:09 eschnett

Hi, is this open for new contributors to work on?

shngt avatar Oct 08 '22 17:10 shngt

Hi. I opened a minimam PR for the modification today. I'm sorry for the late response.

sosuts avatar Oct 09 '22 17:10 sosuts