diff --git a/src/sparsity_pattern.jl b/src/sparsity_pattern.jl index ac83be98..ba16683c 100644 --- a/src/sparsity_pattern.jl +++ b/src/sparsity_pattern.jl @@ -1,8 +1,16 @@ """ + compute_jacobian_sparsity(c, x0) compute_jacobian_sparsity(c!, cx, x0) Return a sparse matrix. """ +function compute_jacobian_sparsity end + +function compute_jacobian_sparsity(c, x0) + S = SparseConnectivityTracer.jacobian_pattern(c, x0) + return S +end + function compute_jacobian_sparsity(c!, cx, x0) S = SparseConnectivityTracer.jacobian_pattern(c!, cx, x0) return S @@ -21,8 +29,10 @@ function compute_hessian_sparsity(f, nvar, c!, ncon) else x0 = rand(nvar) y0 = rand(ncon) - cx = similar(y0) - ℓ(x) = f(x) + sum(c!(cx, x)[i] * y0[i] for i = 1:ncon) + function ℓ(x) + cx = similar(x, ncon) + return f(x) + sum(c!(cx, x)[i] * y0[i] for i = 1:ncon) + end S = SparseConnectivityTracer.hessian_pattern(ℓ, x0) return S end diff --git a/test/runtests.jl b/test/runtests.jl index 4bb8ec4e..abf66f40 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -5,13 +5,12 @@ using ADNLPModels: @testset "Test sparsity pattern of Jacobian and Hessian" begin f(x) = sum(x) - c!(cx, x) = begin - cx .= 1 - return x - end - nvar, ncon = 2, 1 + c(x) = x + c!(cx, x) = copyto!(cx, x) + nvar, ncon = 2, 2 x0 = ones(nvar) cx = rand(ncon) + S = ADNLPModels.compute_jacobian_sparsity(c, x0) S = ADNLPModels.compute_jacobian_sparsity(c!, cx, x0) S = ADNLPModels.compute_hessian_sparsity(f, nvar, c!, ncon) end @@ -41,13 +40,11 @@ push!( ADNLPModels.predefined_backend, :zygote_backend => Dict( :gradient_backend => ADNLPModels.ZygoteADGradient, - :hprod_backend => ADNLPModels.SDTForwardDiffADHvprod, :jprod_backend => ADNLPModels.ZygoteADJprod, :jtprod_backend => ADNLPModels.ZygoteADJtprod, :jacobian_backend => ADNLPModels.ZygoteADJacobian, :hessian_backend => ADNLPModels.ZygoteADHessian, :ghjvprod_backend => ADNLPModels.ForwardDiffADGHjvprod, - :hprod_residual_backend => ADNLPModels.SDTForwardDiffADHvprod, :jprod_residual_backend => ADNLPModels.ZygoteADJprod, :jtprod_residual_backend => ADNLPModels.ZygoteADJtprod, :jacobian_residual_backend => ADNLPModels.ZygoteADJacobian,