From 7a5c3e9378fd5d1e818dea73dfed985116d502ef Mon Sep 17 00:00:00 2001 From: tmigot Date: Tue, 13 Aug 2024 07:44:03 -0400 Subject: [PATCH] update sparse matrix --- docs/src/{sparsepattern.md => sparsity_pattern.md} | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) rename docs/src/{sparsepattern.md => sparsity_pattern.md} (92%) diff --git a/docs/src/sparsepattern.md b/docs/src/sparsity_pattern.md similarity index 92% rename from docs/src/sparsepattern.md rename to docs/src/sparsity_pattern.md index a5cd8624..3027dd3d 100644 --- a/docs/src/sparsepattern.md +++ b/docs/src/sparsity_pattern.md @@ -88,13 +88,15 @@ xi = vcat(ones(T, N), zeros(T, N)) lcon = ucon = vcat(one(T), zeros(T, N - 1)) @elapsed begin - J = spzeros(Bool, N - 1, n) - for i =1:(N - 1) - J[i, i] = true - J[i, i + 1] = true - J[i, N + i] = true - J[i, N + i + 1] = true + Is = Vector{Int}(undef, 4 * (N - 1)) + Js = Vector{Int}(undef, 4 * (N - 1)) + Vs = ones(Bool, 4 * (N - 1)) + for i = 1:(N - 1) + Is[((i - 1) * 4 + 1):(i * 4)] = [i; i; i; i] + Js[((i - 1) * 4 + 1):(i * 4)] = [i; i + 1; N + i; N + i + 1] end + J = sparse(Is, Js, Vs, N - 1, n) + jac_back = ADNLPModels.SparseADJacobian(n, f, N - 1, c!, J) nlp = ADNLPModel!(f, xi, lvar, uvar, [1], [1], T[1], c!, lcon, ucon; hessian_backend = ADNLPModels.EmptyADbackend, jacobian_backend = jac_back) end