Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatic Jacobian sparsity detection fails when concatenating interpolations #317

Closed
SouthEndMusic opened this issue Jul 29, 2024 · 1 comment · Fixed by #318
Closed
Labels
bug Something isn't working

Comments

@SouthEndMusic
Copy link
Member

SouthEndMusic commented Jul 29, 2024

Describe the bug 🐞

There is an error when trying to obtain the Jacobian sparsity of a function with Symbolics.jacobian_sparsity if that function concatenates interpolations.

Minimal Reproducible Example 👇

using Symbolics: jacobian_sparsity
using DataInterpolations

u = [1.0, 2.0]
t = [0.0, 2.0]
A = LinearInterpolation(u, t)
B = LinearInterpolation(u.^2, t; extrapolate = true)

out0 = zeros(5)
x0 = rand(5)

function f!(out, x)
    for (i, x_) in enumerate(x)
        out[i] = (B  A)(x_)
    end
end

jacobian_sparsity((out, x) -> f!(out, x), out0, x0)

Error & Stacktrace ⚠️

ERROR: MethodError: no method matching (::LinearInterpolation{…})(::SymbolicUtils.BasicSymbolic{…})

Closest candidates are:
  (::DataInterpolations.AbstractInterpolation)(::Symbolics.Num)
   @ DataInterpolationsSymbolicsExt path\to\DataInterpolations\ext\DataInterpolationsSymbolicsExt.jl:15
  (::DataInterpolations.AbstractInterpolation)(::Number)
   @ DataInterpolations path\to\DataInterpolations\src\DataInterpolations.jl:25
  (::DataInterpolations.AbstractInterpolation)(::Number, ::Integer)
   @ DataInterpolations path\to\DataInterpolations\src\DataInterpolations.jl:26
  ...

Stacktrace:
 [1] f!(out::Vector{Symbolics.Num}, x::Vector{Symbolics.Num})
   @ Main path\to\runner.jl:15
 [2] (::var"#13#14")(out::Vector{Symbolics.Num}, x::Vector{Symbolics.Num})
   @ Main path\to\runner.jl:20
 [3] jacobian_sparsity(::Function, ::Vector{Float64}, ::Vector{Float64}; kwargs::@Kwargs{})
   @ Symbolics path\to\.julia\packages\Symbolics\8tggF\src\diff.jl:598
 [4] jacobian_sparsity(::Function, ::Vector{Float64}, ::Vector{Float64})
   @ Symbolics path\to\.julia\packages\Symbolics\8tggF\src\diff.jl:593
 [5] top-level scope
   @ path\to\runner.jl:20

Environment

DataInterpolations v5.3.1
Symbolics v5.30.1

@sathvikbhagavan
Copy link
Member

With this

function f!(out, x)
    for (i, x_) in enumerate(x)
        t = A(x_)
        out[i] = B(wrap(t))
    end
end

it works,

julia> jacobian_sparsity((out, x) -> f!(out, x), out0, x0)
5×5 SparseArrays.SparseMatrixCSC{Bool, Int64} with 5 stored entries:
 1        
   1      
     1    
       1  
         1

This can be fixed with a missing dispatch or just registering interpolation. I will make the PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants