Automatic Differentiation
 
Loading...
Searching...
No Matches
columns_dot_self.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_REV_FUN_COLUMNS_DOT_SELF_HPP
2#define STAN_MATH_REV_FUN_COLUMNS_DOT_SELF_HPP
3
10
11namespace stan {
12namespace math {
13
20template <typename Mat, require_eigen_vt<is_var, Mat>* = nullptr>
21inline Eigen::Matrix<var, 1, Mat::ColsAtCompileTime> columns_dot_self(
22 const Mat& x) {
23 Eigen::Matrix<var, 1, Mat::ColsAtCompileTime> ret(1, x.cols());
24 for (size_type i = 0; i < x.cols(); i++) {
25 ret(i) = dot_self(x.col(i));
26 }
27 return ret;
28}
29
36template <typename Mat, require_var_matrix_t<Mat>* = nullptr>
37inline auto columns_dot_self(const Mat& x) {
38 using ret_type
39 = return_var_matrix_t<decltype(x.val().colwise().squaredNorm()), Mat>;
40 arena_t<ret_type> res = x.val().colwise().squaredNorm();
41 if (x.size() >= 0) {
42 reverse_pass_callback([res, x]() mutable {
43 x.adj() += x.val() * (2 * res.adj()).asDiagonal();
44 });
45 }
46 return res;
47}
48
49} // namespace math
50} // namespace stan
51#endif
void reverse_pass_callback(F &&functor)
Puts a callback on the autodiff stack to be called in reverse pass.
auto columns_dot_self(const T &a)
Returns the dot product of each column of a matrix with itself.
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic >::Index size_type
Type for sizes and indexes in an Eigen matrix with double elements.
Definition typedefs.hpp:11
auto dot_self(const T &a)
Returns squared norm of a vector or matrix.
Definition dot_self.hpp:21
typename internal::arena_type_impl< std::decay_t< T > >::type arena_t
Determines a type that can be used in place of T that does any dynamic allocations on the AD stack.
std::conditional_t< is_any_var_matrix< ReturnType, Types... >::value, stan::math::var_value< stan::math::promote_scalar_t< double, plain_type_t< ReturnType > > >, stan::math::promote_scalar_t< stan::math::var_value< double >, plain_type_t< ReturnType > > > return_var_matrix_t
Given an Eigen type and several inputs, determine if a matrix should be var<Matrix> or Matrix<var>.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...