Automatic Differentiation
 
Loading...
Searching...
No Matches
jacobian.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_REV_FUNCTOR_JACOBIAN_HPP
2#define STAN_MATH_REV_FUNCTOR_JACOBIAN_HPP
3
7#include <stdexcept>
8#include <vector>
9
10namespace stan {
11namespace math {
12
13template <typename F>
14void jacobian(const F& f, const Eigen::Matrix<double, Eigen::Dynamic, 1>& x,
15 Eigen::Matrix<double, Eigen::Dynamic, 1>& fx,
16 Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>& J) {
17 using Eigen::Dynamic;
18 using Eigen::Matrix;
19 // Run nested autodiff in this scope
21
22 Matrix<var, Dynamic, 1> x_var(x);
23 Matrix<var, Dynamic, 1> fx_var = f(x_var);
24 fx.resize(fx_var.size());
25 J.resize(x.size(), fx_var.size());
26 fx = fx_var.val();
27 grad(fx_var(0).vi_);
28 J.col(0) = x_var.adj();
29 for (int i = 1; i < fx_var.size(); ++i) {
30 nested.set_zero_all_adjoints();
31 grad(fx_var(i).vi_);
32 J.col(i) = x_var.adj();
33 }
34 J.transposeInPlace();
35}
36
37} // namespace math
38} // namespace stan
39#endif
void set_zero_all_adjoints()
Reset all adjoint values in this nested stack to zero.
A class following the RAII idiom to start and recover nested autodiff scopes.
void jacobian(const F &f, const Eigen::Matrix< T, Eigen::Dynamic, 1 > &x, Eigen::Matrix< T, Eigen::Dynamic, 1 > &fx, Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &J)
Definition jacobian.hpp:11
static void grad()
Compute the gradient for all variables starting from the end of the AD tape.
Definition grad.hpp:26
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...