Stan Math Library: stan/math/fwd/mat/functor/jacobian.hpp Source File

| Stan Math Library 2.10.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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>
5 #include <stan/math/prim/mat/fun/Eigen.hpp>
6 #include <vector>
7
8 namespace stan {
9
10 namespace math {
11
12 template <typename T, typename F>
13 void
14 jacobian(const F& f,
15 const Eigen::Matrix<T, Eigen::Dynamic, 1>& x,
16 Eigen::Matrix<T, Eigen::Dynamic, 1>& fx,
17 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& J) {
18 using Eigen::Matrix;
19 using Eigen::Dynamic;
20 using stan::math::fvar;
21 Matrix<fvar<T>, Dynamic, 1> x_fvar(x.size());
22 for (int i = 0; i < x.size(); ++i) {
23 for (int k = 0; k < x.size(); ++k)
24 x_fvar(k) = fvar<T>(x(k), i == k);
25 Matrix<fvar<T>, Dynamic, 1> fx_fvar
26 = f(x_fvar);
27 if (i == 0) {
28 J.resize(fx_fvar.size(), x.size());
29 fx.resize(fx_fvar.size());
30 for (int k = 0; k < fx_fvar.size(); ++k)
31 fx(k) = fx_fvar(k).val_;
32 }
33 for (int k = 0; k < fx_fvar.size(); ++k) {
34 J(k, i) = fx_fvar(k).d_;
35 }
36 }
37 }
38
39 } // namespace math
40 } // namespace stan
41 #endif
core.hpp
stan::math::fvar::d_
T d_
Definition: fvar.hpp:15
stan
Definition: log_sum_exp.hpp:8
stan::math::jacobian
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:14
stan::math::fvar::val_
T val_
Definition: fvar.hpp:14
Eigen.hpp
stan::math::fvar
Definition: fvar.hpp:13

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