Thank you for your help. Do you know where I can find an example that use NLsolve and SparseDiffTools?
I provide now the Jacobian as a diagonal matrix but I don’t know how to use the package SparseDiffTools.jl
using NLsolve
using SparseArrays
using SparseDiffTools
using SparsityDetection
Ne = 200
function f!(F, x)
@inbounds for (i, xi) in enumerate(x)
F[i] = exp(-xi^2/(2*i))*erf(xi)
end
nothing
end
function g_diag!(J, x)
@inbounds for (i, xi) in enumerate(x)
J[i,i] = (-xi/i)*exp(-xi^2/(2*i))*erf(xi) + exp(-xi^2/(2*i))*(2/√π)*exp(-xi^2)
end
J = Diagonal(J)
nothing
end
function g_sparse!(J, x)
@inbounds for (i, xi) in enumerate(x)
J[i,i] = (-xi/i)*exp(-xi^2/(2*i))*erf(xi) + exp(-xi^2/(2*i))*(2/√π)*exp(-xi^2)
end
nothing
end
df_diag = OnceDifferentiable(f!, g_diag!, randn(Ne), rand(Ne), Diagonal(zeros(Ne)))
df_sparse = OnceDifferentiable(f!, g_sparse!, randn(Ne), rand(Ne), spzeros(Ne,Ne))
F = rand(Ne);
@btime r_diag = nlsolve($df_diag, $F, method = :newton);
@btime r_sparse = nlsolve($df_sparse, $F, method = :newton);
124.516 μs (91 allocations: 95.00 KiB)
237.630 μs (511 allocations: 312.55 KiB)
I don’t know how to set the sparsity pattern to Diagonal
, I got the following error message
input = zeros(Ne)
output = similar(input)
sparsity_pattern = jacobian_sparsity(f!,output,input)
jac = Float64.(sparse(sparsity_pattern));
ArgumentError: invalid index: Tagged(Tag{nametype(JacobianSparsityContext),1031646979481733957,Nothing}(), 2, _) of type Cassette.Tagged{Cassette.Tag{nametype(JacobianSparsityContext),0x0e512576419d0b45,Nothing},Int64,SparsityDetection.ProvinanceSet,Cassette.NoMetaMeta,Cassette.Context{nametype(JacobianSparsityContext),Tuple{Sparsity,SparsityDetection.Path},Cassette.Tag{nametype(JacobianSparsityContext),0x0e512576419d0b45,Nothing},SparsityDetection.var"##PassType#253",IdDict{Module,Dict{Symbol,Cassette.BindingMeta}},Cassette.DisableHooks}}
Stacktrace:
[1] to_index(::Cassette.Tagged{Cassette.Tag{nametype(JacobianSparsityContext),0x0e512576419d0b45,Nothing},Int64,SparsityDetection.ProvinanceSet,Cassette.NoMetaMeta,Cassette.Context{nametype(JacobianSparsityContext),Tuple{Sparsity,SparsityDetection.Path},Cassette.Tag{nametype(JacobianSparsityContext),0x0e512576419d0b45,Nothing},SparsityDetection.var"##PassType#253",IdDict{Module,Dict{Symbol,Cassette.BindingMeta}},Cassette.DisableHooks}}) at ./indices.jl:297
[2] to_index(::LinearIndices{1,Tuple{Base.OneTo{Int64}}}, ::Cassette.Tagged{Cassette.Tag{nametype(JacobianSparsityContext),0x0e512576419d0b45,Nothing},Int64,SparsityDetection.ProvinanceSet,Cassette.NoMetaMeta,Cassette.Context{nametype(JacobianSparsityContext),Tuple{Sparsity,SparsityDetection.Path},Cassette.Tag{nametype(JacobianSparsityContext),0x0e512576419d0b45,Nothing},SparsityDetection.var"##PassType#253",IdDict{Module,Dict{Symbol,Cassette.BindingMeta}},Cassette.DisableHooks}}) at ./indices.jl:274
[3] to_indices at ./indices.jl:325 [inlined]
[4] to_indices at ./indices.jl:322 [inlined]
[5] getindex at ./abstractarray.jl:980 [inlined]
[6] overdub at /home/mat/.julia/packages/SparsityDetection/PbKzm/src/jacobian.jl:98 [inlined]
[7] iterate at ./array.jl:763 [inlined]
[8] recurse(::Cassette.Context{nametype(JacobianSparsityContext),Tuple{Sparsity,SparsityDetection.Path},Cassette.Tag{nametype(JacobianSparsityContext),0x0e512576419d0b45,Nothing},SparsityDetection.var"##PassType#253",IdDict{Module,Dict{Symbol,Cassette.BindingMeta}},Cassette.DisableHooks}, ::typeof(iterate), ::Cassette.Tagged{Cassette.Tag{nametype(JacobianSparsityContext),0x0e512576419d0b45,Nothing},Array{Float64,1},Union{SparsityDetection.JacInput, SparsityDetection.JacOutput, SparsityDetection.ProvinanceSet},Array{Cassette.Meta{SparsityDetection.ProvinanceSet,Cassette.NoMetaMeta},1},Cassette.Context{nametype(JacobianSparsityContext),Tuple{Sparsity,SparsityDetection.Path},Cassette.Tag{nametype(JacobianSparsityContext),0x0e512576419d0b45,Nothing},SparsityDetection.var"##PassType#253",IdDict{Module,Dict{Symbol,Cassette.BindingMeta}},Cassette.DisableHooks}}, ::Cassette.Tagged{Cassette.Tag{nametype(JacobianSparsityContext),0x0e512576419d0b45,Nothing},Int64,SparsityDetection.ProvinanceSet,Cassette.NoMetaMeta,Cassette.Context{nametype(JacobianSparsityContext),Tuple{Sparsity,SparsityDetection.Path},Cassette.Tag{nametype(JacobianSparsityContext),0x0e512576419d0b45,Nothing},SparsityDetection.var"##PassType#253",IdDict{Module,Dict{Symbol,Cassette.BindingMeta}},Cassette.DisableHooks}}) at /home/mat/.julia/packages/Cassette/158rp/src/overdub.jl:0
[9] overdub(::Cassette.Context{nametype(JacobianSparsityContext),Tuple{Sparsity,SparsityDetection.Path},Cassette.Tag{nametype(JacobianSparsityContext),0x0e512576419d0b45,Nothing},SparsityDetection.var"##PassType#253",IdDict{Module,Dict{Symbol,Cassette.BindingMeta}},Cassette.DisableHooks}, ::Function, ::Cassette.Tagged{Cassette.Tag{nametype(JacobianSparsityContext),0x0e512576419d0b45,Nothing},Array{Float64,1},Union{SparsityDetection.JacInput, SparsityDetection.JacOutput, SparsityDetection.ProvinanceSet},Array{Cassette.Meta{SparsityDetection.ProvinanceSet,Cassette.NoMetaMeta},1},Cassette.Context{nametype(JacobianSparsityContext),Tuple{Sparsity,SparsityDetection.Path},Cassette.Tag{nametype(JacobianSparsityContext),0x0e512576419d0b45,Nothing},SparsityDetection.var"##PassType#253",IdDict{Module,Dict{Symbol,Cassette.BindingMeta}},Cassette.DisableHooks}}, ::Cassette.Tagged{Cassette.Tag{nametype(JacobianSparsityContext),0x0e512576419d0b45,Nothing},Int64,SparsityDetection.ProvinanceSet,Cassette.NoMetaMeta,Cassette.Context{nametype(JacobianSparsityContext),Tuple{Sparsity,SparsityDetection.Path},Cassette.Tag{nametype(JacobianSparsityContext),0x0e512576419d0b45,Nothing},SparsityDetection.var"##PassType#253",IdDict{Module,Dict{Symbol,Cassette.BindingMeta}},Cassette.DisableHooks}}) at /home/mat/.julia/packages/SparsityDetection/PbKzm/src/propagate_tags.jl:45
[10] overdub(::Cassette.Context{nametype(JacobianSparsityContext),Tuple{Sparsity,SparsityDetection.Path},Cassette.Tag{nametype(JacobianSparsityContext),0x0e512576419d0b45,Nothing},SparsityDetection.var"##PassType#253",IdDict{Module,Dict{Symbol,Cassette.BindingMeta}},Cassette.DisableHooks}, ::typeof(Core._apply_iterate), ::Function, ::Function, ::Cassette.Tagged{Cassette.Tag{nametype(JacobianSparsityContext),0x0e512576419d0b45,Nothing},Tuple{Array{Float64,1}},Union{SparsityDetection.JacInput, SparsityDetection.JacOutput, SparsityDetection.ProvinanceSet},Tuple{Cassette.Immutable{Cassette.Meta{Union{SparsityDetection.JacInput, SparsityDetection.JacOutput, SparsityDetection.ProvinanceSet},Array{Cassette.Meta{SparsityDetection.ProvinanceSet,Cassette.NoMetaMeta},1}}}},Cassette.Context{nametype(JacobianSparsityContext),Tuple{Sparsity,SparsityDetection.Path},Cassette.Tag{nametype(JacobianSparsityContext),0x0e512576419d0b45,Nothing},SparsityDetection.var"##PassType#253",IdDict{Module,Dict{Symbol,Cassette.BindingMeta}},Cassette.DisableHooks}}, ::Cassette.Tagged{Cassette.Tag{nametype(JacobianSparsityContext),0x0e512576419d0b45,Nothing},Tuple{Int64},Union{SparsityDetection.JacInput, SparsityDetection.JacOutput, SparsityDetection.ProvinanceSet},Tuple{Cassette.Immutable{Cassette.Meta{SparsityDetection.ProvinanceSet,Cassette.NoMetaMeta}}},Cassette.Context{nametype(JacobianSparsityContext),Tuple{Sparsity,SparsityDetection.Path},Cassette.Tag{nametype(JacobianSparsityContext),0x0e512576419d0b45,Nothing},SparsityDetection.var"##PassType#253",IdDict{Module,Dict{Symbol,Cassette.BindingMeta}},Cassette.DisableHooks}}) at /home/mat/.julia/packages/Cassette/158rp/src/context.jl:266
[11] iterate at ./iterators.jl:139 [inlined]
[12] recurse(::Cassette.Context{nametype(JacobianSparsityContext),Tuple{Sparsity,SparsityDetection.Path},Cassette.Tag{nametype(JacobianSparsityContext),0x0e512576419d0b45,Nothing},SparsityDetection.var"##PassType#253",IdDict{Module,Dict{Symbol,Cassette.BindingMeta}},Cassette.DisableHooks}, ::typeof(iterate), ::Cassette.Tagged{Cassette.Tag{nametype(JacobianSparsityContext),0x0e512576419d0b45,Nothing},Base.Iterators.Enumerate{Array{Float64,1}},Union{SparsityDetection.JacInput, SparsityDetection.JacOutput, SparsityDetection.ProvinanceSet},NamedTuple{(:itr,),Tuple{Cassette.Immutable{Cassette.Meta{Union{SparsityDetection.JacInput, SparsityDetection.JacOutput, SparsityDetection.ProvinanceSet},Array{Cassette.Meta{SparsityDetection.ProvinanceSet,Cassette.NoMetaMeta},1}}}}},Cassette.Context{nametype(JacobianSparsityContext),Tuple{Sparsity,SparsityDetection.Path},Cassette.Tag{nametype(JacobianSparsityContext),0x0e512576419d0b45,Nothing},SparsityDetection.var"##PassType#253",IdDict{Module,Dict{Symbol,Cassette.BindingMeta}},Cassette.DisableHooks}}, ::Cassette.Tagged{Cassette.Tag{nametype(JacobianSparsityContext),0x0e512576419d0b45,Nothing},Tuple{Int64,Int64},Union{SparsityDetection.JacInput, SparsityDetection.JacOutput, SparsityDetection.ProvinanceSet},Tuple{Cassette.Immutable{Cassette.Meta{SparsityDetection.ProvinanceSet,Cassette.NoMetaMeta}},Cassette.Immutable{Cassette.Meta{SparsityDetection.ProvinanceSet,Cassette.NoMetaMeta}}},Cassette.Context{nametype(JacobianSparsityContext),Tuple{Sparsity,SparsityDetection.Path},Cassette.Tag{nametype(JacobianSparsityContext),0x0e512576419d0b45,Nothing},SparsityDetection.var"##PassType#253",IdDict{Module,Dict{Symbol,Cassette.BindingMeta}},Cassette.DisableHooks}}) at /home/mat/.julia/packages/Cassette/158rp/src/overdub.jl:0
[13] overdub(::Cassette.Context{nametype(JacobianSparsityContext),Tuple{Sparsity,SparsityDetection.Path},Cassette.Tag{nametype(JacobianSparsityContext),0x0e512576419d0b45,Nothing},SparsityDetection.var"##PassType#253",IdDict{Module,Dict{Symbol,Cassette.BindingMeta}},Cassette.DisableHooks}, ::Function, ::Cassette.Tagged{Cassette.Tag{nametype(JacobianSparsityContext),0x0e512576419d0b45,Nothing},Base.Iterators.Enumerate{Array{Float64,1}},Union{SparsityDetection.JacInput, SparsityDetection.JacOutput, SparsityDetection.ProvinanceSet},NamedTuple{(:itr,),Tuple{Cassette.Immutable{Cassette.Meta{Union{SparsityDetection.JacInput, SparsityDetection.JacOutput, SparsityDetection.ProvinanceSet},Array{Cassette.Meta{SparsityDetection.ProvinanceSet,Cassette.NoMetaMeta},1}}}}},Cassette.Context{nametype(JacobianSparsityContext),Tuple{Sparsity,SparsityDetection.Path},Cassette.Tag{nametype(JacobianSparsityContext),0x0e512576419d0b45,Nothing},SparsityDetection.var"##PassType#253",IdDict{Module,Dict{Symbol,Cassette.BindingMeta}},Cassette.DisableHooks}}, ::Cassette.Tagged{Cassette.Tag{nametype(JacobianSparsityContext),0x0e512576419d0b45,Nothing},Tuple{Int64,Int64},Union{SparsityDetection.JacInput, SparsityDetection.JacOutput, SparsityDetection.ProvinanceSet},Tuple{Cassette.Immutable{Cassette.Meta{SparsityDetection.ProvinanceSet,Cassette.NoMetaMeta}},Cassette.Immutable{Cassette.Meta{SparsityDetection.ProvinanceSet,Cassette.NoMetaMeta}}},Cassette.Context{nametype(JacobianSparsityContext),Tuple{Sparsity,SparsityDetection.Path},Cassette.Tag{nametype(JacobianSparsityContext),0x0e512576419d0b45,Nothing},SparsityDetection.var"##PassType#253",IdDict{Module,Dict{Symbol,Cassette.BindingMeta}},Cassette.DisableHooks}}) at /home/mat/.julia/packages/SparsityDetection/PbKzm/src/propagate_tags.jl:37
[14] f! at ./In[113]:9 [inlined]
[15] recurse(::Cassette.Context{nametype(JacobianSparsityContext),Tuple{Sparsity,SparsityDetection.Path},Cassette.Tag{nametype(JacobianSparsityContext),0x0e512576419d0b45,Nothing},SparsityDetection.var"##PassType#253",IdDict{Module,Dict{Symbol,Cassette.BindingMeta}},Cassette.DisableHooks}, ::typeof(f!), ::Cassette.Tagged{Cassette.Tag{nametype(JacobianSparsityContext),0x0e512576419d0b45,Nothing},Array{Float64,1},Union{SparsityDetection.JacInput, SparsityDetection.JacOutput, SparsityDetection.ProvinanceSet},Array{Cassette.Meta{SparsityDetection.ProvinanceSet,Cassette.NoMetaMeta},1},Cassette.Context{nametype(JacobianSparsityContext),Sparsity,Cassette.Tag{nametype(JacobianSparsityContext),0x0e512576419d0b45,Nothing},Cassette.var"##PassType#255",IdDict{Module,Dict{Symbol,Cassette.BindingMeta}},Cassette.DisableHooks}}, ::Cassette.Tagged{Cassette.Tag{nametype(JacobianSparsityContext),0x0e512576419d0b45,Nothing},Array{Float64,1},Union{SparsityDetection.JacInput, SparsityDetection.JacOutput, SparsityDetection.ProvinanceSet},Array{Cassette.Meta{SparsityDetection.ProvinanceSet,Cassette.NoMetaMeta},1},Cassette.Context{nametype(JacobianSparsityContext),Sparsity,Cassette.Tag{nametype(JacobianSparsityContext),0x0e512576419d0b45,Nothing},Cassette.var"##PassType#255",IdDict{Module,Dict{Symbol,Cassette.BindingMeta}},Cassette.DisableHooks}}) at /home/mat/.julia/packages/Cassette/158rp/src/overdub.jl:0
[16] overdub(::Cassette.Context{nametype(JacobianSparsityContext),Tuple{Sparsity,SparsityDetection.Path},Cassette.Tag{nametype(JacobianSparsityContext),0x0e512576419d0b45,Nothing},SparsityDetection.var"##PassType#253",IdDict{Module,Dict{Symbol,Cassette.BindingMeta}},Cassette.DisableHooks}, ::Function, ::Cassette.Tagged{Cassette.Tag{nametype(JacobianSparsityContext),0x0e512576419d0b45,Nothing},Array{Float64,1},Union{SparsityDetection.JacInput, SparsityDetection.JacOutput, SparsityDetection.ProvinanceSet},Array{Cassette.Meta{SparsityDetection.ProvinanceSet,Cassette.NoMetaMeta},1},Cassette.Context{nametype(JacobianSparsityContext),Sparsity,Cassette.Tag{nametype(JacobianSparsityContext),0x0e512576419d0b45,Nothing},Cassette.var"##PassType#255",IdDict{Module,Dict{Symbol,Cassette.BindingMeta}},Cassette.DisableHooks}}, ::Cassette.Tagged{Cassette.Tag{nametype(JacobianSparsityContext),0x0e512576419d0b45,Nothing},Array{Float64,1},Union{SparsityDetection.JacInput, SparsityDetection.JacOutput, SparsityDetection.ProvinanceSet},Array{Cassette.Meta{SparsityDetection.ProvinanceSet,Cassette.NoMetaMeta},1},Cassette.Context{nametype(JacobianSparsityContext),Sparsity,Cassette.Tag{nametype(JacobianSparsityContext),0x0e512576419d0b45,Nothing},Cassette.var"##PassType#255",IdDict{Module,Dict{Symbol,Cassette.BindingMeta}},Cassette.DisableHooks}}) at /home/mat/.julia/packages/SparsityDetection/PbKzm/src/propagate_tags.jl:45
[17] overdub(::Cassette.Context{nametype(JacobianSparsityContext),Tuple{Sparsity,SparsityDetection.Path},Cassette.Tag{nametype(JacobianSparsityContext),0x0e512576419d0b45,Nothing},SparsityDetection.var"##PassType#253",IdDict{Module,Dict{Symbol,Cassette.BindingMeta}},Cassette.DisableHooks}, ::typeof(Core._apply_iterate), ::Function, ::Function, ::Tuple{Cassette.Tagged{Cassette.Tag{nametype(JacobianSparsityContext),0x0e512576419d0b45,Nothing},Array{Float64,1},Union{SparsityDetection.JacInput, SparsityDetection.JacOutput, SparsityDetection.ProvinanceSet},Array{Cassette.Meta{SparsityDetection.ProvinanceSet,Cassette.NoMetaMeta},1},Cassette.Context{nametype(JacobianSparsityContext),Sparsity,Cassette.Tag{nametype(JacobianSparsityContext),0x0e512576419d0b45,Nothing},Cassette.var"##PassType#255",IdDict{Module,Dict{Symbol,Cassette.BindingMeta}},Cassette.DisableHooks}},Cassette.Tagged{Cassette.Tag{nametype(JacobianSparsityContext),0x0e512576419d0b45,Nothing},Array{Float64,1},Union{SparsityDetection.JacInput, SparsityDetection.JacOutput, SparsityDetection.ProvinanceSet},Array{Cassette.Meta{SparsityDetection.ProvinanceSet,Cassette.NoMetaMeta},1},Cassette.Context{nametype(JacobianSparsityContext),Sparsity,Cassette.Tag{nametype(JacobianSparsityContext),0x0e512576419d0b45,Nothing},Cassette.var"##PassType#255",IdDict{Module,Dict{Symbol,Cassette.BindingMeta}},Cassette.DisableHooks}}}) at /home/mat/.julia/packages/Cassette/158rp/src/context.jl:266
[18] #2 at /home/mat/.julia/packages/SparsityDetection/PbKzm/src/controlflow.jl:148 [inlined]
[19] recurse(::Cassette.Context{nametype(JacobianSparsityContext),Tuple{Sparsity,SparsityDetection.Path},Cassette.Tag{nametype(JacobianSparsityContext),0x0e512576419d0b45,Nothing},SparsityDetection.var"##PassType#253",IdDict{Module,Dict{Symbol,Cassette.BindingMeta}},Cassette.DisableHooks}, ::SparsityDetection.var"#2#3"{typeof(f!),Tuple{Cassette.Tagged{Cassette.Tag{nametype(JacobianSparsityContext),0x0e512576419d0b45,Nothing},Array{Float64,1},Union{SparsityDetection.JacInput, SparsityDetection.JacOutput, SparsityDetection.ProvinanceSet},Array{Cassette.Meta{SparsityDetection.ProvinanceSet,Cassette.NoMetaMeta},1},Cassette.Context{nametype(JacobianSparsityContext),Sparsity,Cassette.Tag{nametype(JacobianSparsityContext),0x0e512576419d0b45,Nothing},Cassette.var"##PassType#255",IdDict{Module,Dict{Symbol,Cassette.BindingMeta}},Cassette.DisableHooks}},Cassette.Tagged{Cassette.Tag{nametype(JacobianSparsityContext),0x0e512576419d0b45,Nothing},Array{Float64,1},Union{SparsityDetection.JacInput, SparsityDetection.JacOutput, SparsityDetection.ProvinanceSet},Array{Cassette.Meta{SparsityDetection.ProvinanceSet,Cassette.NoMetaMeta},1},Cassette.Context{nametype(JacobianSparsityContext),Sparsity,Cassette.Tag{nametype(JacobianSparsityContext),0x0e512576419d0b45,Nothing},Cassette.var"##PassType#255",IdDict{Module,Dict{Symbol,Cassette.BindingMeta}},Cassette.DisableHooks}}}}) at /home/mat/.julia/packages/Cassette/158rp/src/overdub.jl:0
[20] abstract_run(::SparsityDetection.var"#22#24", ::Cassette.Context{nametype(JacobianSparsityContext),Sparsity,Cassette.Tag{nametype(JacobianSparsityContext),0x0e512576419d0b45,Nothing},Cassette.var"##PassType#255",IdDict{Module,Dict{Symbol,Cassette.BindingMeta}},Cassette.DisableHooks}, ::Function, ::Cassette.Tagged{Cassette.Tag{nametype(JacobianSparsityContext),0x0e512576419d0b45,Nothing},Array{Float64,1},Union{SparsityDetection.JacInput, SparsityDetection.JacOutput, SparsityDetection.ProvinanceSet},Array{Cassette.Meta{SparsityDetection.ProvinanceSet,Cassette.NoMetaMeta},1},Cassette.Context{nametype(JacobianSparsityContext),Sparsity,Cassette.Tag{nametype(JacobianSparsityContext),0x0e512576419d0b45,Nothing},Cassette.var"##PassType#255",IdDict{Module,Dict{Symbol,Cassette.BindingMeta}},Cassette.DisableHooks}}, ::Vararg{Cassette.Tagged{Cassette.Tag{nametype(JacobianSparsityContext),0x0e512576419d0b45,Nothing},Array{Float64,1},Union{SparsityDetection.JacInput, SparsityDetection.JacOutput, SparsityDetection.ProvinanceSet},Array{Cassette.Meta{SparsityDetection.ProvinanceSet,Cassette.NoMetaMeta},1},Cassette.Context{nametype(JacobianSparsityContext),Sparsity,Cassette.Tag{nametype(JacobianSparsityContext),0x0e512576419d0b45,Nothing},Cassette.var"##PassType#255",IdDict{Module,Dict{Symbol,Cassette.BindingMeta}},Cassette.DisableHooks}},N} where N; verbose::Bool) at /home/mat/.julia/packages/SparsityDetection/PbKzm/src/controlflow.jl:148
[21] jacobian_sparsity(::Function, ::Array{Float64,1}, ::Array{Float64,1}; sparsity::Sparsity, verbose::Bool, raw::Bool) at /home/mat/.julia/packages/SparsityDetection/PbKzm/src/jacobian.jl:135
[22] jacobian_sparsity(::Function, ::Array{Float64,1}, ::Array{Float64,1}) at /home/mat/.julia/packages/SparsityDetection/PbKzm/src/jacobian.jl:130
[23] top-level scope at In[115]:3