Automatic Differentiation
 
Loading...
Searching...
No Matches
rows_dot_self.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_REV_FUN_ROWS_DOT_SELF_HPP
2#define STAN_MATH_REV_FUN_ROWS_DOT_SELF_HPP
3
9
10namespace stan {
11namespace math {
12
19template <typename Mat, require_eigen_vt<is_var, Mat>* = nullptr>
20inline Eigen::Matrix<var, Mat::RowsAtCompileTime, 1> rows_dot_self(
21 const Mat& x) {
22 Eigen::Matrix<var, Mat::RowsAtCompileTime, 1> ret(x.rows());
23 for (size_type i = 0; i < x.rows(); i++) {
24 ret(i) = dot_self(x.row(i));
25 }
26 return ret;
27}
28
35template <typename Mat, require_var_matrix_t<Mat>* = nullptr>
36inline auto rows_dot_self(const Mat& x) {
37 using ret_type = var_value<Eigen::VectorXd>;
38 arena_t<ret_type> res = x.val().rowwise().squaredNorm();
39 if (x.size() >= 0) {
40 reverse_pass_callback([res, x]() mutable {
41 x.adj() += (2 * res.adj()).asDiagonal() * x.val();
42 });
43 }
44 return res;
45}
46
47} // namespace math
48} // namespace stan
49#endif
auto rows_dot_self(T_a &&a)
Returns the dot product of each row of a matrix with itself.
void reverse_pass_callback(F &&functor)
Puts a callback on the autodiff stack to be called in reverse pass.
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.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...