Stan Math Library  2.20.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 namespace math {
10 
20 template <typename T>
21 Eigen::Matrix<T, Eigen::Dynamic, 1> ordered_constrain(
22  const Eigen::Matrix<T, Eigen::Dynamic, 1>& x) {
23  using Eigen::Dynamic;
24  using Eigen::Matrix;
25  using std::exp;
26 
27  typedef typename index_type<Matrix<T, Dynamic, 1> >::type size_type;
28 
29  size_type k = x.size();
30  Matrix<T, Dynamic, 1> y(k);
31  if (k == 0)
32  return y;
33  y[0] = x[0];
34  for (size_type i = 1; i < k; ++i)
35  y[i] = y[i - 1] + exp(x[i]);
36  return y;
37 }
38 
51 template <typename T>
52 inline Eigen::Matrix<T, Eigen::Dynamic, 1> ordered_constrain(
53  const Eigen::Matrix<T, Eigen::Dynamic, 1>& x, T& lp) {
54  using Eigen::Dynamic;
55  using Eigen::Matrix;
56 
57  typedef typename index_type<Matrix<T, Dynamic, 1> >::type size_type;
58 
59  for (size_type i = 1; i < x.size(); ++i)
60  lp += x(i);
61  return ordered_constrain(x);
62 }
63 
64 } // namespace math
65 
66 } // namespace stan
67 
68 #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:11
Primary template class for the metaprogram to compute the index type of a container.
Definition: index_type.hpp:18
fvar< T > exp(const fvar< T > &x)
Definition: exp.hpp:11

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