diff --git a/src/solvers/cholmod.jl b/src/solvers/cholmod.jl index 2e7d2be8..faae83ab 100644 --- a/src/solvers/cholmod.jl +++ b/src/solvers/cholmod.jl @@ -1671,6 +1671,11 @@ function check_hermitian_stype(A::Sparse) throw(ArgumentError("sparse matrix is not symmetric/Hermitian")) return nothing end +function check_refactor_size(F::Factor, A::Sparse) + size(A) == size(F) || throw(DimensionMismatch( + "matrix has size $(size(A)), but the factorization has size $(size(F))")) + return nothing +end ## Compute that symbolic factorization only function symbolic(A::Sparse{<:VTypes, Ti}; @@ -1710,6 +1715,7 @@ function cholesky!(F::Factor{Tv}, A::Sparse{Tv}; shift::Real=0.0, check::Bool = true) where Tv check_hermitian_stype(A) @lock F.lock begin + check_refactor_size(F, A) @cholmod_param final_ll = true begin factorize_p!(A, shift, F) end @@ -1901,6 +1907,7 @@ function ldlt!(F::Factor{Tv}, A::Sparse{Tv}; shift::Real=0.0, check::Bool = true) where Tv check_hermitian_stype(A) @lock F.lock begin + check_refactor_size(F, A) change_factor!(F, false, false, true, false) factorize_p!(A, shift, F) check && (issuccess(F) || throw(factorization_exception(F))) diff --git a/test/solvers/issues.jl b/test/solvers/issues.jl index 588c8acb..4d218bc3 100644 --- a/test/solvers/issues.jl +++ b/test/solvers/issues.jl @@ -214,6 +214,14 @@ end @test cholesky!(F, SparseMatrixCSC{Float64,Int16}(2A)) \ [1.0, 2.0, 3.0] ≈ (2A) \ [1.0, 2.0, 3.0] end +@testset "ldlt! with a matrix of another size leaves an LLt factor unchanged" begin + A = sparse([4.0 1 0; 1 4 1; 0 1 4]) + F = cholesky(A) + @test_throws DimensionMismatch ldlt!(F, sparse(1.0I, 4, 4)) + @test :D ∉ propertynames(F) + @test F \ ones(3) ≈ A \ ones(3) +end + @testset "Issues #26367 and #26368" begin A = sparse([0.0 1 0 0; 0 0 0 0]) F = qr(A)