# Using Symbolics to compute Hessian of a large objective function

**URL:** <https://discourse.julialang.org/t/using-symbolics-to-compute-hessian-of-a-large-objective-function/93805>\
**Category:** Modelling & Simulations\
**Created:** [January 31, 2023, 3:18am UTC](https://discourse.julialang.org/t/using-symbolics-to-compute-hessian-of-a-large-objective-function/93805 "2023-01-31T03:18:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![zornsllama](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zornsllama/32/24636_2.png) [@zornsllama](https://discourse.julialang.org/u/zornsllama)\
**Post date:** [January 31, 2023, 3:18am UTC](https://discourse.julialang.org/t/using-symbolics-to-compute-hessian-of-a-large-objective-function/93805/1 "2023-01-31T03:18:01Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![alisdairmckay](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@alisdairmckay](https://discourse.julialang.org/u/alisdairmckay)\
**Post date:** [May 28, 2024, 8:17pm UTC](https://discourse.julialang.org/t/using-symbolics-to-compute-hessian-of-a-large-objective-function/93805/2 "2024-05-28T20:17:50Z")

</div>

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