1 #ifndef STAN__AGRAD__REV__MATRIX__DOT_SELF_HPP
2 #define STAN__AGRAD__REV__MATRIX__DOT_SELF_HPP
15 class dot_self_vari :
public vari {
20 dot_self_vari(vari** v,
size_t size)
21 : vari(var_dot_self(v,size)),
25 template<
typename Derived>
26 dot_self_vari(
const Eigen::DenseBase<Derived> &v) :
29 for (
size_t i = 0; i <
size_; i++)
32 template <
int R,
int C>
33 dot_self_vari(
const Eigen::Matrix<var,R,C>& v) :
36 for (
size_t i = 0; i <
size_; ++i)
39 inline static double square(
double x) {
return x * x; }
40 inline static double var_dot_self(vari** v,
size_t size) {
42 for (
size_t i = 0; i <
size; ++i)
46 template<
typename Derived>
47 double var_dot_self(
const Eigen::DenseBase<Derived> &v) {
49 for (
int i = 0; i < v.size(); ++i)
50 sum +=
square(v(i).vi_->val_);
53 template <
int R,
int C>
54 inline static double var_dot_self(
const Eigen::Matrix<var,R,C> &v) {
56 for (
int i = 0; i < v.size(); ++i)
57 sum +=
square(v(i).vi_->val_);
60 virtual void chain() {
61 for (
size_t i = 0; i <
size_; ++i)
62 v_[i]->adj_ += adj_ * 2.0 *
v_[i]->val_;
76 template<
int R,
int C>
79 return var(
new dot_self_vari(v));
88 inline Eigen::Matrix<var,1,C>
90 Eigen::Matrix<var,1,C> ret(1,x.cols());
91 for (
size_type i = 0; i < x.cols(); i++) {
92 ret(i) =
var(
new dot_self_vari(x.col(i)));
Eigen::Matrix< fvar< T >, 1, C > columns_dot_self(const Eigen::Matrix< fvar< T >, R, C > &x)
fvar< T > dot_self(const Eigen::Matrix< fvar< T >, R, C > &v)
fvar< T > square(const fvar< T > &x)
memory::stack_alloc memalloc_
bool check_vector(const char *function, const Eigen::Matrix< T, R, C > &x, const char *name, T_result *result)
fvar< T > sum(const Eigen::Matrix< fvar< T >, R, C > &m)
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic >::Index size_type
void * alloc(size_t len)
Return a newly allocated block of memory of the appropriate size managed by the stack allocator...
Independent (input) and dependent (output) variables for gradients.
int size(const std::vector< T > &x)