Using Symbolics to compute Hessian of a large objective function

Hi all,

I’m trying to use Symbolics.jl to compute the Hessian of a large sum-of-squares type objective function, e.g. something like \sum_{i=i}^{N-2} f(x_i, x_{i+1}, x_{i+2})^2 where f is some nonlinear function. I have two questions:

  1. For large N, this symbolic computation seems infeasibly slow, even if just doing something simple

@variables x[1:100000]
obj = sum(map(s → s^2, x))
Symbolics.jacobian([obj], x)

Is there a way to speed up this calculation by describing the input differently, or utilizing sparsity somehow? I can of course differentiate each term symbolically and then add them by hand, but if possible I’d like to avoid doing this when computing the sparse Hessian, since I’d then have to keep track of the sparsity pattern myself.

  1. The following behavior is unexpected:

@variables x[1:3]
obj = sum(map(s → s^2, x))
Symbolics.hessian(obj, x)

gives an output of
3×3 Matrix{Num}:
0 0 0
0 0 0
0 0 0

which is incorrect. Am I missing something here?

Any help is greatly appreciated – thanks!

On point 2, replace your last line with Symbolics.hessian(obj, [x...])