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

hessian_sparsity errors for functions with > 2 arguments #1452

Closed
devmotion opened this issue Feb 26, 2025 · 0 comments · Fixed by #1453
Closed

hessian_sparsity errors for functions with > 2 arguments #1452

devmotion opened this issue Feb 26, 2025 · 0 comments · Fixed by #1453

Comments

@devmotion
Copy link
Contributor

As expected, for known traceable functions with e.g. 3 arguments the sparsity pattern is inferred correctly:

julia> using Symbolics

julia> @variables x y z;

julia> f(x, y, z) = x * y^2 + z;

julia> Symbolics.hessian_sparsity(f(x, y, z), [x, y, z])
3×3 SparseArrays.SparseMatrixCSC{Bool, Int64} with 3 stored entries:
   1  
 1  1  
     

However, for unknown functions with >= 3 arguments hessian_sparsity errors:

julia> g(x, y, z) = x * y^2 + z
g (generic function with 1 method)

julia> @register_symbolic g(x, y, z)

julia> Symbolics.hessian_sparsity(g(x, y, z), [x, y, z])
ERROR: MethodError: no method matching _sparse(::SymbolicUtils.BasicSymbolic{Any}, ::Int64)
The function `_sparse` exists, but no method is defined for this combination of argument types.

Closest candidates are:
  _sparse(::Symbolics.TermCombination, ::Any)
   @ Symbolics ~/.julia/packages/Symbolics/CjPtz/src/linearity.jl:131
  _sparse(::Number, ::Any)
   @ Symbolics ~/.julia/packages/Symbolics/CjPtz/src/linearity.jl:154
  _sparse(::Type{Tv}, ::Type{Ti}, ::Any) where {Tv, Ti}
   @ Symbolics ~/.julia/packages/Symbolics/CjPtz/src/linear_algebra.jl:379

Stacktrace:
 [1] hessian_sparsity(expr::Num, vars::Vector{Num}; full::Bool)
   @ Symbolics ~/.julia/packages/Symbolics/CjPtz/src/diff.jl:709
 [2] hessian_sparsity(expr::Num, vars::Vector{Num})
   @ Symbolics ~/.julia/packages/Symbolics/CjPtz/src/diff.jl:702
 [3] top-level scope
   @ REPL[16]:1

Compare this with what happens for functions with 2 arguments:

As expected, for known traceable functions the sparsity pattern is inferred correctly:

julia> f2(x, y) = x * y^2;

julia> Symbolics.hessian_sparsity(f2(x, y), [x, y])
2×2 SparseArrays.SparseMatrixCSC{Bool, Int64} with 3 stored entries:
   1
 1  1

For unknown functions, the default fallback (non-sparse) is used (was added in #1384):

julia> g2(x, y) = x * y^2;

julia> @register_symbolic g2(x, y)

julia> Symbolics.hessian_sparsity(g2(x, y), [x, y])
2×2 SparseArrays.SparseMatrixCSC{Bool, Int64} with 4 stored entries:
 1  1
 1  1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant