Stan Math Library  2.15.0
reverse mode automatic differentiation
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 
9 #include <stan/math/rev/core.hpp>
11 #include <cmath>
12 
13 namespace stan {
14  namespace math {
15 
16  namespace {
17  class unit_vector_elt_vari : public vari {
18  private:
19  vari** y_;
20  const double* unit_vector_y_;
21  const int size_;
22  const int idx_;
23  const double norm_;
24 
25  public:
26  unit_vector_elt_vari(double val,
27  vari** y,
28  const double* unit_vector_y,
29  int size,
30  int idx,
31  double norm)
32  : vari(val),
33  y_(y),
34  unit_vector_y_(unit_vector_y),
35  size_(size),
36  idx_(idx),
37  norm_(norm) {
38  }
39  void chain() {
40  const double cubed_norm = norm_ * norm_ * norm_;
41  for (int m = 0; m < size_; ++m) {
42  y_[m]->adj_
43  -= adj_ * unit_vector_y_[m] * unit_vector_y_[idx_] / cubed_norm;
44  if (m == idx_)
45  y_[m]->adj_ += adj_ / norm_;
46  }
47  }
48  };
49  }
50 
59  template <int R, int C>
60  Eigen::Matrix<var, R, C>
61  unit_vector_constrain(const Eigen::Matrix<var, R, C>& y) {
62  check_vector("unit_vector", "y", y);
63  check_nonzero_size("unit_vector", "y", y);
64 
65  vari** y_vi_array
66  = reinterpret_cast<vari**>(ChainableStack::memalloc_
67  .alloc(sizeof(vari*) * y.size()));
68  for (int i = 0; i < y.size(); ++i)
69  y_vi_array[i] = y.coeff(i).vi_;
70 
71  Eigen::VectorXd y_d(y.size());
72  for (int i = 0; i < y.size(); ++i)
73  y_d.coeffRef(i) = y.coeff(i).val();
74 
75  const double norm = y_d.norm();
76  check_positive_finite("unit_vector", "norm", norm);
77  Eigen::VectorXd unit_vector_d = y_d / norm;
78 
79  double* unit_vector_y_d_array
80  = reinterpret_cast<double*>(ChainableStack::memalloc_
81  .alloc(sizeof(double) * y_d.size()));
82  for (int i = 0; i < y_d.size(); ++i)
83  unit_vector_y_d_array[i] = unit_vector_d.coeff(i);
84 
85  Eigen::Matrix<var, R, C> unit_vector_y(y.size());
86  for (int k = 0; k < y.size(); ++k)
87  unit_vector_y.coeffRef(k)
88  = var(new unit_vector_elt_vari(unit_vector_d[k],
89  y_vi_array,
90  unit_vector_y_d_array,
91  y.size(),
92  k,
93  norm));
94  return unit_vector_y;
95  }
96 
106  template <int R, int C>
107  Eigen::Matrix<var, R, C>
108  unit_vector_constrain(const Eigen::Matrix<var, R, C>& y, var &lp) {
109  Eigen::Matrix<var, R, C> x = unit_vector_constrain(y);
110  lp -= 0.5 * dot_self(y);
111  return x;
112  }
113 
114  }
115 }
116 #endif
void check_nonzero_size(const char *function, const char *name, const T_y &y)
Check if the specified matrix/vector is of non-zero size.
The variable implementation base class.
Definition: vari.hpp:30
void check_vector(const char *function, const char *name, const Eigen::Matrix< T, R, C > &x)
Check if the matrix is either a row vector or column vector.
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:30
fvar< T > dot_self(const Eigen::Matrix< fvar< T >, R, C > &v)
Definition: dot_self.hpp:16
Eigen::Matrix< fvar< T >, R, C > unit_vector_constrain(const Eigen::Matrix< fvar< T >, R, C > &y)
void check_positive_finite(const char *function, const char *name, const T_y &y)
Check if y is positive and finite.
size_t size_
Definition: dot_self.hpp:18
int size(const std::vector< T > &x)
Return the size of the specified standard vector.
Definition: size.hpp:17
void * alloc(size_t len)
Return a newly allocated block of memory of the appropriate size managed by the stack allocator...

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