# Vector-valued functions with manual gradients in external C++

**URL:** <https://discourse.mc-stan.org/t/vector-valued-functions-with-manual-gradients-in-external-c/32297>\
**Category:** General\
**Tags:** cmdstan, ode\
**Created:** [July 31, 2023, 8:27pm UTC](https://discourse.mc-stan.org/t/vector-valued-functions-with-manual-gradients-in-external-c/32297 "2023-07-31T20:27:12Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![alphillips](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/alphillips/32/11656_2.png) [@alphillips](https://discourse.mc-stan.org/u/alphillips)\
**Post date:** [August 1, 2023, 7:47am UTC](https://discourse.mc-stan.org/t/vector-valued-functions-with-manual-gradients-in-external-c/32297/4 "2023-08-01T07:47:44Z")

</div>

Thanks Bob. The version that was compiling was before I’d added the jacobian, but the compiler wasn’t complaining about those lines.

I think I’ve confused myself looking at [other examples](https://discourse.mc-stan.org/t/external-c-code-with-precomputed-gradients/26232) of precomputed gradients where the result of make\_callback\_var is returned (albeit for scalar returns).

With your suggested edits and returning `dydt` I’m now getting a different error:

```plaintext
/home/alecks/git/MinimalODEExample/odefunc.hpp:37: error: invalid use of incomplete type ‘class stan::math::var_value<Eigen::Matrix<stan::math::var_value<double>, -1, 1>, void>’
   37 | stan::math::make_callback_var(dydt, [y_arena, jacobian](auto& res){
      | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   38 | y_arena.adj() += jacobian.transpose() * res.adj();
      | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   39 | });
      | 
In file included from stan/lib/stan_math/stan/math/rev/core/vari.hpp:4,
                 from stan/lib/stan_math/stan/math/rev/core/var.hpp:8,
                 from stan/lib/stan_math/stan/math/rev/meta/conditional_var_value.hpp:4,
                 from stan/lib/stan_math/stan/math/rev/meta.hpp:6,
                 from stan/lib/stan_math/stan/math/rev/core/accumulate_adjoints.hpp:5,
                 from stan/lib/stan_math/stan/math/rev/core.hpp:4,
                 from stan/lib/stan_math/stan/math/rev.hpp:8:
stan/lib/stan_math/stan/math/rev/core/var_value_fwd_declare.hpp:8:7: note: declaration of ‘class stan::math::var_value<Eigen::Matrix<stan::math::var_value<double>, -1, 1>, void>’
    8 | class var_value;
      | ^ ~~~~~~~~

```

Apologies if I’m missing something obvious! Current version and log attached.

[error.txt](https://discourse.mc-stan.org/uploads/short-url/otCZDLX1bQtKyi7svRGXQGUMzja.txt) (23.9 KB)  
[odefunc.hpp](https://discourse.mc-stan.org/uploads/short-url/wkoIVPEfNaRH3SeLTFTNZrShNsH.hpp) (2.3 KB)

---

_[View the full topic](https://discourse.mc-stan.org/t/vector-valued-functions-with-manual-gradients-in-external-c/32297)._
