julia
julia copied to clipboard
Missing Bidiagonal constructor
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}})
Same for Tridiagonal
. However, both Diagonal
and SymTridiagonal
have the respective constructor.
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
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}}
.
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)
This looks fine.
It would be good to have respective test cases as well.
Hi, is this open for new contributors to work on?
Hi. I opened a minimam PR for the modification today. I'm sorry for the late response.