Stan Math Library  2.20.0
reverse mode automatic differentiation
jacobian.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_FWD_MAT_FUNCTOR_JACOBIAN_HPP
2 #define STAN_MATH_FWD_MAT_FUNCTOR_JACOBIAN_HPP
3 
4 #include <stan/math/fwd/core.hpp>
6 
7 namespace stan {
8 namespace math {
9 
10 template <typename T, typename F>
11 void 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  for (int i = 0; i < x.size(); ++i) {
18  for (int k = 0; k < x.size(); ++k)
19  x_fvar(k) = fvar<T>(x(k), i == k);
20  Matrix<fvar<T>, Dynamic, 1> fx_fvar = f(x_fvar);
21  if (i == 0) {
22  J.resize(fx_fvar.size(), x.size());
23  fx.resize(fx_fvar.size());
24  for (int k = 0; k < fx_fvar.size(); ++k)
25  fx(k) = fx_fvar(k).val_;
26  }
27  for (int k = 0; k < fx_fvar.size(); ++k) {
28  J(k, i) = fx_fvar(k).d_;
29  }
30  }
31 }
32 
33 } // namespace math
34 } // namespace stan
35 #endif
T d_
The tangent (derivative) of this variable.
Definition: fvar.hpp:50
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
T val_
The value of this variable.
Definition: fvar.hpp:45
This template class represents scalars used in forward-mode automatic differentiation, which consist of values and directional derivatives of the specified template type.
Definition: fvar.hpp:41

     [ Stan Home Page ] © 2011–2018, Stan Development Team.