diff --git a/stdlib/LinearAlgebra/src/cholesky.jl b/stdlib/LinearAlgebra/src/cholesky.jl index 545d92ec1704d..03f7c273ccbef 100644 --- a/stdlib/LinearAlgebra/src/cholesky.jl +++ b/stdlib/LinearAlgebra/src/cholesky.jl @@ -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) diff --git a/stdlib/LinearAlgebra/test/cholesky.jl b/stdlib/LinearAlgebra/test/cholesky.jl index 00bfc18a21638..6ba72432048a9 100644 --- a/stdlib/LinearAlgebra/test/cholesky.jl +++ b/stdlib/LinearAlgebra/test/cholesky.jl @@ -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)