Stan Math Library  2.9.0
reverse mode automatic differentiation
positive_ordered_constrain.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_FUN_POSITIVE_ORDERED_CONSTRAIN_HPP
2 #define STAN_MATH_PRIM_MAT_FUN_POSITIVE_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  positive_ordered_constrain(const Eigen::Matrix<T, Eigen::Dynamic, 1>& x) {
24  using Eigen::Matrix;
25  using Eigen::Dynamic;
27  using std::exp;
28  typedef typename index_type<Matrix<T, Dynamic, 1> >::type size_type;
29 
30  size_type k = x.size();
31  Matrix<T, Dynamic, 1> y(k);
32  if (k == 0)
33  return y;
34  y[0] = exp(x[0]);
35  for (size_type i = 1; i < k; ++i)
36  y[i] = y[i-1] + exp(x[i]);
37  return y;
38  }
39 
52  template <typename T>
53  inline
54  Eigen::Matrix<T, Eigen::Dynamic, 1>
55  positive_ordered_constrain(const Eigen::Matrix<T, Eigen::Dynamic, 1>& x,
56  T& lp) {
57  using Eigen::Matrix;
58  using Eigen::Dynamic;
60  typedef typename index_type<Matrix<T, Dynamic, 1> >::type size_type;
61 
62  for (size_type i = 0; i < x.size(); ++i)
63  lp += x(i);
65  }
66 
67  }
68 
69 }
70 
71 #endif
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
Eigen::Matrix< T, Eigen::Dynamic, 1 > positive_ordered_constrain(const Eigen::Matrix< T, Eigen::Dynamic, 1 > &x)
Return an increasing positive ordered vector derived from the specified free vector.
fvar< T > exp(const fvar< T > &x)
Definition: exp.hpp:10

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