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>
14inline void jacobian(const F& f,
15 const Eigen::Matrix<double, Eigen::Dynamic, 1>& x,
16 Eigen::Matrix<double, Eigen::Dynamic, 1>& fx,
17 Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>& J) {
18 using Eigen::Dynamic;
19 using Eigen::Matrix;
20 // Run nested autodiff in this scope
22
23 Matrix<var, Dynamic, 1> x_var(x);
24 Matrix<var, Dynamic, 1> fx_var = f(x_var);
25 fx.resize(fx_var.size());
26 J.resize(x.size(), fx_var.size());
27 fx = fx_var.val();
28 grad(fx_var(0).vi_);
29 J.col(0) = x_var.adj();
30 for (int i = 1; i < fx_var.size(); ++i) {
31 nested.set_zero_all_adjoints();
32 grad(fx_var(i).vi_);
33 J.col(i) = x_var.adj();
34 }
35 J.transposeInPlace();
36}
37
38} // namespace math
39} // namespace stan
40#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 ...