Stan Math Library  2.11.0
reverse mode automatic differentiation
ordered_constrain.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_FUN_ORDERED_CONSTRAIN_HPP
2 #define STAN_MATH_PRIM_MAT_FUN_ORDERED_CONSTRAIN_HPP
3 
6 #include <cmath>
7 
8 namespace stan {
9 
10  namespace math {
11 
21  template <typename T>
22  Eigen::Matrix<T, Eigen::Dynamic, 1>
23  ordered_constrain(const Eigen::Matrix<T, Eigen::Dynamic, 1>& x) {
24  using Eigen::Matrix;
25  using Eigen::Dynamic;
27  using std::exp;
28 
29  typedef typename index_type<Matrix<T, Dynamic, 1> >::type size_type;
30 
31  size_type k = x.size();
32  Matrix<T, Dynamic, 1> y(k);
33  if (k == 0)
34  return y;
35  y[0] = x[0];
36  for (size_type i = 1; i < k; ++i)
37  y[i] = y[i-1] + exp(x[i]);
38  return y;
39  }
40 
53  template <typename T>
54  inline
55  Eigen::Matrix<T, Eigen::Dynamic, 1>
56  ordered_constrain(const Eigen::Matrix<T, Eigen::Dynamic, 1>& x, T& lp) {
57  using Eigen::Matrix;
58  using Eigen::Dynamic;
60 
61  typedef typename index_type<Matrix<T, Dynamic, 1> >::type size_type;
62 
63  for (size_type i = 1; i < x.size(); ++i)
64  lp += x(i);
65  return ordered_constrain(x);
66  }
67 
68  }
69 
70 }
71 
72 #endif
Eigen::Matrix< T, Eigen::Dynamic, 1 > ordered_constrain(const Eigen::Matrix< T, Eigen::Dynamic, 1 > &x)
Return an increasing ordered vector derived from the specified free vector.
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic >::Index size_type
Type for sizes and indexes in an Eigen matrix with double e.
Definition: typedefs.hpp:13
Primary template class for the metaprogram to compute the index type of a container.
Definition: index_type.hpp:19
fvar< T > exp(const fvar< T > &x)
Definition: exp.hpp:10

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