All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Groups
unit_vector_free.hpp
Go to the documentation of this file.
#ifndef STAN_MATH_PRIM_MAT_FUN_UNIT_VECTOR_FREE_HPP
#define STAN_MATH_PRIM_MAT_FUN_UNIT_VECTOR_FREE_HPP
#include <stan/math/prim/mat/fun/Eigen.hpp>
#include <stan/math/prim/mat/meta/index_type.hpp>
#include <stan/math/prim/mat/err/check_unit_vector.hpp>
#include <cmath>
namespace stan {
namespace math {
template <typename T>
Eigen::Matrix<T, Eigen::Dynamic, 1>
unit_vector_free(const Eigen::Matrix<T, Eigen::Dynamic, 1>& x) {
using Eigen::Matrix;
using Eigen::Dynamic;
stan::math::check_unit_vector("stan::math::unit_vector_free",
"Unit vector variable", x);
int Km1 = x.size() - 1;
Matrix<T, Dynamic, 1> y(Km1);
T sumSq = x(Km1)*x(Km1);
const T half_pi = T(M_PI/2.0);
for (int k = Km1; --k >= 0; ) {
y(k) = atan2(sqrt(sumSq), x(k)) - half_pi;
sumSq += x(k)*x(k);
}
return y;
}
}
}
#endif
stan::math::sqrt
fvar< T > sqrt(const fvar< T > &x)
Definition: sqrt.hpp:15
stan::math::atan2
fvar< T > atan2(const fvar< T > &x1, const fvar< T > &x2)
Definition: atan2.hpp:12
stan::math::check_unit_vector
bool check_unit_vector(const char *function, const char *name, const Eigen::Matrix< T_prob, Dynamic, 1 > &theta)
Return true if the specified vector is unit vector.
Definition: check_unit_vector.hpp:38
check_unit_vector.hpp
index_type.hpp
stan::math::unit_vector_free
Eigen::Matrix< T, Eigen::Dynamic, 1 > unit_vector_free(const Eigen::Matrix< T, Eigen::Dynamic, 1 > &x)
Definition: unit_vector_free.hpp:16
Eigen.hpp

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