Skip to content

Commit

Permalink
Add missing convert methods for Cholesky
Browse files Browse the repository at this point in the history
  • Loading branch information
timholy committed Nov 14, 2024
1 parent cf36497 commit ecdf3a1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
7 changes: 7 additions & 0 deletions stdlib/LinearAlgebra/src/cholesky.jl
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ Cholesky(A::AbstractMatrix{T}, uplo::AbstractChar, info::Integer) where {T} =
Cholesky(U::UpperTriangular{T}) where {T} = Cholesky{T,typeof(U.data)}(U.data, 'U', 0)
Cholesky(L::LowerTriangular{T}) where {T} = Cholesky{T,typeof(L.data)}(L.data, 'L', 0)

Base.convert(::Type{Cholesky{T,S}}, C::Cholesky{T,S}) where {T,S<:AbstractMatrix} = C
Base.convert(::Type{Cholesky{T,S}}, C::Cholesky) where {T,S<:AbstractMatrix} = Cholesky{T,S}(C.factors, C.uplo, C.info)

# iteration for destructuring into components
Base.iterate(C::Cholesky) = (C.L, Val(:U))
Base.iterate(C::Cholesky, ::Val{:U}) = (C.U, Val(:done))
Expand Down Expand Up @@ -170,6 +173,10 @@ CholeskyPivoted(A::AbstractMatrix{T}, uplo::AbstractChar, piv::AbstractVector{<:
@deprecate(CholeskyPivoted{T,S}(factors, uplo, piv, rank, tol, info) where {T,S<:AbstractMatrix},
CholeskyPivoted{T,S,typeof(piv)}(factors, uplo, piv, rank, tol, info), false)

Base.convert(::Type{CholeskyPivoted{T,S}}, C::CholeskyPivoted{T,S}) where {T,S<:AbstractMatrix} = C
Base.convert(::Type{CholeskyPivoted{T,S}}, C::CholeskyPivoted) where {T,S<:AbstractMatrix} = CholeskyPivoted{T,S}(C.factors, C.uplo, C.piv, C.rank, C.tol, C.info)
Base.convert(::Type{CholeskyPivoted{T,S,P}}, C::CholeskyPivoted{T,S,P}) where {T,S<:AbstractMatrix,P<:AbstractVector{<:Integer}} = C
Base.convert(::Type{CholeskyPivoted{T,S,P}}, C::CholeskyPivoted) where {T,S<:AbstractMatrix,P<:AbstractVector{<:Integer}} = CholeskyPivoted{T,S,P}(C.factors, C.uplo, C.piv, C.rank, C.tol, C.info)

# iteration for destructuring into components
Base.iterate(C::CholeskyPivoted) = (C.L, Val(:U))
Expand Down
17 changes: 17 additions & 0 deletions stdlib/LinearAlgebra/test/cholesky.jl
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,23 @@ end
end
end
end

@testset "eltype/matrixtype conversions" begin
apd = Matrix(Hermitian(areal'*areal))
capd = cholesky(apd)
@test convert(Cholesky{Float64}, capd) === capd
@test convert(Cholesky{Float64,Matrix{Float64}}, capd) === convert(typeof(capd), capd) === capd
@test eltype(convert(Cholesky{Float32}, capd)) === Float32
@test eltype(convert(Cholesky{Float32,Matrix{Float32}}, capd)) === Float32

capd = cholesky(apd, RowMaximum())
@test convert(CholeskyPivoted{Float64}, capd) === capd
@test convert(CholeskyPivoted{Float64,Matrix{Float64}}, capd) == capd
@test convert(CholeskyPivoted{Float64,Matrix{Float64},Vector{Int}}, capd) === convert(typeof(capd), capd) === capd
@test eltype(convert(CholeskyPivoted{Float32}, capd)) === Float32
@test eltype(convert(CholeskyPivoted{Float32,Matrix{Float32}}, capd)) === Float32
@test eltype(convert(CholeskyPivoted{Float32,Matrix{Float32},Vector{Int}}, capd)) === Float32
end
end

@testset "behavior for non-positive definite matrices" for T in (Float64, ComplexF64, BigFloat)
Expand Down

0 comments on commit ecdf3a1

Please sign in to comment.