1 #ifndef STAN_MATH_PRIM_MAT_FUN_UNIT_VECTOR_CONSTRAIN_HPP 2 #define STAN_MATH_PRIM_MAT_FUN_UNIT_VECTOR_CONSTRAIN_HPP 21 const double* unit_vector_y_;
28 int size,
int idx,
double norm)
31 unit_vector_y_(unit_vector_y),
36 const double cubed_norm = norm_ * norm_ * norm_;
37 for (
int m = 0; m < size_; ++m) {
39 -=
adj_ * unit_vector_y_[m] * unit_vector_y_[idx_] / cubed_norm;
55 template <
int R,
int C>
57 const Eigen::Matrix<var, R, C>& y) {
61 vari** y_vi_array =
reinterpret_cast<vari**
>(
63 for (
int i = 0; i < y.size(); ++i)
64 y_vi_array[i] = y.coeff(i).vi_;
66 Eigen::VectorXd y_d(y.size());
67 for (
int i = 0; i < y.size(); ++i)
68 y_d.coeffRef(i) = y.coeff(i).val();
70 const double norm = y_d.norm();
72 Eigen::VectorXd unit_vector_d = y_d / norm;
74 double* unit_vector_y_d_array =
reinterpret_cast<double*
>(
76 for (
int i = 0; i < y_d.size(); ++i)
77 unit_vector_y_d_array[i] = unit_vector_d.coeff(i);
79 Eigen::Matrix<var, R, C> unit_vector_y(y.size());
80 for (
int k = 0; k < y.size(); ++k)
82 unit_vector_d[k], y_vi_array, unit_vector_y_d_array, y.size(), k,
96 template <
int R,
int C>
98 const Eigen::Matrix<var, R, C>& y,
var& lp) {
unit_vector_elt_vari(double val, vari **y, const double *unit_vector_y, int size, int idx, double norm)
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.
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.
static STAN_THREADS_DEF AutodiffStackStorage * instance_
Independent (input) and dependent (output) variables for gradients.
fvar< T > dot_self(const Eigen::Matrix< fvar< T >, R, C > &v)
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.
int size(const std::vector< T > &x)
Return the size of the specified standard vector.
void chain()
Apply the chain rule to this variable based on the variables on which it depends. ...
double adj_
The adjoint of this variable, which is the partial derivative of this variable with respect to the ro...
void * alloc(size_t len)
Return a newly allocated block of memory of the appropriate size managed by the stack allocator...