| Stan Math Library 2.6.3 probability, sampling & optimization

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros
unit_vector_constrain.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_FUN_UNIT_VECTOR_CONSTRAIN_HPP
2 #define STAN_MATH_PRIM_MAT_FUN_UNIT_VECTOR_CONSTRAIN_HPP
3
4 #include <stan/math/prim/mat/fun/Eigen.hpp>
5 #include <stan/math/prim/mat/meta/index_type.hpp>
6 #include <cmath>
7
8 namespace stan {
9
10 namespace math {
11
12 // Unit vector
13
22 template <typename T>
23 Eigen::Matrix<T, Eigen::Dynamic, 1>
24 unit_vector_constrain(const Eigen::Matrix<T, Eigen::Dynamic, 1>& y) {
25 using Eigen::Matrix;
26 using Eigen::Dynamic;
27 using stan::math::index_type;
28 typedef typename index_type<Matrix<T, Dynamic, 1> >::type size_type;
29 int Km1 = y.size();
30 Matrix<T, Dynamic, 1> x(Km1 + 1);
31 x(0) = 1.0;
32 const T half_pi = T(M_PI/2.0);
33 for (size_type k = 1; k <= Km1; ++k) {
34 T yk_1 = y(k-1) + half_pi;
35 T sin_yk_1 = sin(yk_1);
36 x(k) = x(k-1)*sin_yk_1;
37 x(k-1) *= cos(yk_1);
38 }
39 return x;
40 }
41
51 template <typename T>
52 Eigen::Matrix<T, Eigen::Dynamic, 1>
53 unit_vector_constrain(const Eigen::Matrix<T, Eigen::Dynamic, 1>& y, T &lp) {
54 using Eigen::Matrix;
55 using Eigen::Dynamic;
56 using stan::math::index_type;
57 typedef typename index_type<Matrix<T, Dynamic, 1> >::type size_type;
58
59 int Km1 = y.size();
60 Matrix<T, Dynamic, 1> x(Km1 + 1);
61 x(0) = 1.0;
62 const T half_pi = T(0.5 * M_PI);
63 for (size_type k = 1; k <= Km1; ++k) {
64 T yk_1 = y(k-1) + half_pi;
65 T sin_yk_1 = sin(yk_1);
66 x(k) = x(k-1) * sin_yk_1;
67 x(k-1) *= cos(yk_1);
68 if (k < Km1)
69 lp += (Km1 - k) * log(fabs(sin_yk_1));
70 }
71 return x;
72 }
73
74 }
75
76 }
77
78 #endif
stan::math::cos
fvar< T > cos(const fvar< T > &x)
Definition: cos.hpp:13
stan::math::fabs
fvar< T > fabs(const fvar< T > &x)
Definition: fabs.hpp:14
stan::math::log
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:15
stan::math::unit_vector_constrain
Eigen::Matrix< T, Eigen::Dynamic, 1 > unit_vector_constrain(const Eigen::Matrix< T, Eigen::Dynamic, 1 > &y)
Return the unit length vector corresponding to the free vector y.
Definition: unit_vector_constrain.hpp:24
stan::math::size_type
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
stan::math::index_type
Primary template class for the metaprogram to compute the index type of a container.
Definition: index_type.hpp:19
index_type.hpp
stan::math::sin
fvar< T > sin(const fvar< T > &x)
Definition: sin.hpp:14
Eigen.hpp

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