Skip to content

Commit

Permalink
Add missing convert methods for Cholesky (JuliaLang#56562)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Karrasch <[email protected]>
  • Loading branch information
timholy and dkarrasch authored Nov 19, 2024
1 parent af9e6e3 commit 0bd8292
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
7 changes: 6 additions & 1 deletion stdlib/LinearAlgebra/src/cholesky.jl
Original file line number Diff line number Diff line change
Expand Up @@ -631,11 +631,16 @@ function Cholesky{T}(C::Cholesky) where T
Cnew = convert(AbstractMatrix{T}, C.factors)
Cholesky{T, typeof(Cnew)}(Cnew, C.uplo, C.info)
end
Cholesky{T,S}(C::Cholesky) where {T,S<:AbstractMatrix} = Cholesky{T,S}(C.factors, C.uplo, C.info)
Factorization{T}(C::Cholesky{T}) where {T} = C
Factorization{T}(C::Cholesky) where {T} = Cholesky{T}(C)
CholeskyPivoted{T}(C::CholeskyPivoted{T}) where {T} = C
CholeskyPivoted{T}(C::CholeskyPivoted) where {T} =
CholeskyPivoted(AbstractMatrix{T}(C.factors),C.uplo,C.piv,C.rank,C.tol,C.info)
CholeskyPivoted(AbstractMatrix{T}(C.factors), C.uplo, C.piv, C.rank, C.tol, C.info)
CholeskyPivoted{T,S}(C::CholeskyPivoted) where {T,S<:AbstractMatrix} =
CholeskyPivoted{T,S,typeof(C.piv)}(C.factors, C.uplo, C.piv, C.rank, C.tol, C.info)
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)
Factorization{T}(C::CholeskyPivoted{T}) where {T} = C
Factorization{T}(C::CholeskyPivoted) where {T} = CholeskyPivoted{T}(C)

Expand Down
18 changes: 18 additions & 0 deletions stdlib/LinearAlgebra/test/cholesky.jl
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,24 @@ 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
@test eltype(convert(CholeskyPivoted{Float32,Matrix{Float32},Vector{Int16}}, capd).piv) === Int16
end
end

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

0 comments on commit 0bd8292

Please sign in to comment.