JuliaDiff / SparseDiffTools.jl

Fast jacobian computation through sparsity exploitation and matrix coloring
MIT License
237 stars 41 forks source link

Sparsity Detection not working in combination with OffsetArrays #154

Open boriskaus opened 2 years ago

boriskaus commented 2 years ago

In some cases, it is quite handy to use OffsetArrays within the residual routine. Yet it seems that the sparsity pattern cannot be detected in that case.

Following the example in the Readme:

fcalls = 0
function f_offset(y,x_in) # in-place
  global fcalls += 1
  x = OffsetArray(x_in,1:length(x_in))
  for i in 2:length(x)-1
    y[i] = x[i-1] - 2x[i] + x[i+1]
  end
  y[1] = -2x[1] + x[2]
  y[end] = x[end-1] - 2x[end]
  nothing
end

julia> using SparsityDetection, SparseArrays, OffsetArrays
julia> input = rand(30)
julia> output = similar(input)
julia> sparsity_pattern = jacobian_sparsity(f_offset,output,input)
Explored path: SparsityDetection.Path(Bool[], 1)
30×30 SparseMatrixCSC{Bool, Int64} with 0 stored entries:⠀⠀⠀⠀⠀⠀⠀⠀⠀

I realize that the SparsityDetection package is deprecated in favor of ModelingToolkit.jl, but within the documentation of the last package I did not find an example that replaces the lines above (and can thus not check if it has been fixed). An example would be highly appreciated!