Automatic Differentiation
 
Loading...
Searching...
No Matches
jacobian.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_FWD_FUNCTOR_JACOBIAN_HPP
2#define STAN_MATH_FWD_FUNCTOR_JACOBIAN_HPP
3
6
7namespace stan {
8namespace math {
9
10template <typename T, typename F>
11void jacobian(const F& f, const Eigen::Matrix<T, Eigen::Dynamic, 1>& x,
12 Eigen::Matrix<T, Eigen::Dynamic, 1>& fx,
13 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& J) {
14 using Eigen::Dynamic;
15 using Eigen::Matrix;
16 Matrix<fvar<T>, Dynamic, 1> x_fvar(x.size());
17 J.resize(x_fvar.size(), x.size());
18 fx.resize(x_fvar.size());
19 for (int k = 0; k < x.size(); ++k) {
20 x_fvar(k) = fvar<T>(x(k), 0);
21 }
22 x_fvar(0) = fvar<T>(x(0), 1);
23 Matrix<fvar<T>, Dynamic, 1> fx_fvar = f(x_fvar);
24 fx = fx_fvar.val();
25 J.col(0) = fx_fvar.d();
26 const fvar<T> switch_fvar(0, 1); // flips the tangents on and off
27 for (int i = 1; i < x.size(); ++i) {
28 x_fvar(i - 1) -= switch_fvar;
29 x_fvar(i) += switch_fvar;
30 Matrix<fvar<T>, Dynamic, 1> fx_fvar = f(x_fvar);
31 J.col(i) = fx_fvar.d();
32 }
33}
34
35} // namespace math
36} // namespace stan
37#endif
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
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
This template class represents scalars used in forward-mode automatic differentiation,...
Definition fvar.hpp:40