All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Groups
unit_vector_constrain.hpp
Go to the documentation of this file.
#ifndef STAN_MATH_PRIM_MAT_FUN_UNIT_VECTOR_CONSTRAIN_HPP
#define STAN_MATH_PRIM_MAT_FUN_UNIT_VECTOR_CONSTRAIN_HPP
#include <stan/math/prim/mat/fun/Eigen.hpp>
#include <stan/math/prim/mat/meta/index_type.hpp>
#include <cmath>
namespace stan {
namespace math {
// Unit vector
template <typename T>
Eigen::Matrix<T, Eigen::Dynamic, 1>
unit_vector_constrain(const Eigen::Matrix<T, Eigen::Dynamic, 1>& y) {
using Eigen::Matrix;
using Eigen::Dynamic;
using stan::math::index_type;
typedef typename index_type<Matrix<T, Dynamic, 1> >::type size_type;
int Km1 = y.size();
Matrix<T, Dynamic, 1> x(Km1 + 1);
x(0) = 1.0;
const T half_pi = T(M_PI/2.0);
for (size_type k = 1; k <= Km1; ++k) {
T yk_1 = y(k-1) + half_pi;
T sin_yk_1 = sin(yk_1);
x(k) = x(k-1)*sin_yk_1;
x(k-1) *= cos(yk_1);
}
return x;
}
template <typename T>
Eigen::Matrix<T, Eigen::Dynamic, 1>
unit_vector_constrain(const Eigen::Matrix<T, Eigen::Dynamic, 1>& y, T &lp) {
using Eigen::Matrix;
using Eigen::Dynamic;
using stan::math::index_type;
typedef typename index_type<Matrix<T, Dynamic, 1> >::type size_type;
int Km1 = y.size();
Matrix<T, Dynamic, 1> x(Km1 + 1);
x(0) = 1.0;
const T half_pi = T(0.5 * M_PI);
for (size_type k = 1; k <= Km1; ++k) {
T yk_1 = y(k-1) + half_pi;
T sin_yk_1 = sin(yk_1);
x(k) = x(k-1) * sin_yk_1;
x(k-1) *= cos(yk_1);
if (k < Km1)
lp += (Km1 - k) * log(fabs(sin_yk_1));
}
return x;
}
}
}
#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.