Automatic Differentiation
 
Loading...
Searching...
No Matches
unit_vector_constrain.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_FWD_CONSTRAINT_UNIT_VECTOR_CONSTRAIN_HPP
2#define STAN_MATH_FWD_CONSTRAINT_UNIT_VECTOR_CONSTRAIN_HPP
3
13#include <cmath>
14
15namespace stan {
16namespace math {
17
18template <typename EigMat,
20inline auto unit_vector_constrain(const EigMat& y) {
21 using eig_partial = partials_type_t<value_type_t<EigMat>>;
23 plain_type_t<EigMat> unit_vector_y(y_val.size());
24 unit_vector_y.val() = unit_vector_constrain(y_val);
25
26 eig_partial squared_norm = dot_self(y_val);
27 eig_partial norm = sqrt(squared_norm);
28 eig_partial inv_norm = inv(norm);
29 Eigen::Matrix<eig_partial, Eigen::Dynamic, Eigen::Dynamic> J
30 = divide(tcrossprod(y_val), -norm * squared_norm);
31
32 for (Eigen::Index m = 0; m < y_val.size(); ++m) {
33 J.coeffRef(m, m) += inv_norm;
34 for (Eigen::Index k = 0; k < y_val.size(); ++k) {
35 unit_vector_y.coeffRef(k).d_ = J.coeff(k, m);
36 }
37 }
38 return unit_vector_y;
39}
40
41template <typename EigMat, typename T,
43 require_stan_scalar_t<T>* = nullptr>
44inline auto unit_vector_constrain(const EigMat& y, T& lp) {
45 const auto& y_ref = to_ref(y);
46 const value_type_t<EigMat> squared_norm = dot_self(y_ref);
47 lp -= 0.5 * squared_norm;
48 return unit_vector_constrain(y_ref);
49}
50
51} // namespace math
52} // namespace stan
53#endif
require_t< container_type_check_base< is_eigen_col_vector, value_type_t, TypeCheck, Check... > > require_eigen_col_vector_vt
Require type satisfies is_eigen_col_vector.
require_t< container_type_check_base< is_eigen, value_type_t, TypeCheck, Check... > > require_eigen_vt
Require type satisfies is_eigen.
Definition is_eigen.hpp:97
auto divide(T_a &&a, double d)
Returns the elementwise division of the kernel generator expression.
Definition divide.hpp:20
require_t< is_stan_scalar< std::decay_t< T > > > require_stan_scalar_t
Require type satisfies is_stan_scalar.
typename partials_type< T >::type partials_type_t
Helper alias for accessing the partial type.
typename value_type< T >::type value_type_t
Helper function for accessing underlying type.
Eigen::Matrix< value_type_t< EigMat >, EigMat::RowsAtCompileTime, EigMat::RowsAtCompileTime > tcrossprod(const EigMat &m)
fvar< T > norm(const std::complex< fvar< T > > &z)
Return the squared magnitude of the complex argument.
Definition norm.hpp:19
typename promote_scalar_type< std::decay_t< T >, std::decay_t< S > >::type promote_scalar_t
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition value_of.hpp:18
auto unit_vector_constrain(const EigMat &y)
fvar< T > sqrt(const fvar< T > &x)
Definition sqrt.hpp:17
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:17
auto dot_self(const T &a)
Returns squared norm of a vector or matrix.
Definition dot_self.hpp:21
fvar< T > inv(const fvar< T > &x)
Definition inv.hpp:12
typename plain_type< T >::type plain_type_t
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...