1 #ifndef STAN_MATH_REV_MAT_FUN_DOT_SELF_HPP 2 #define STAN_MATH_REV_MAT_FUN_DOT_SELF_HPP 15 class dot_self_vari :
public vari {
21 dot_self_vari(vari** v,
size_t size)
22 : vari(var_dot_self(v, size)),
26 template<
typename Derived>
27 explicit dot_self_vari(
const Eigen::DenseBase<Derived> &v) :
28 vari(var_dot_self(v)),
size_(v.size()) {
30 .
alloc(size_*
sizeof(vari*)));
31 for (
size_t i = 0; i <
size_; i++)
34 template <
int R,
int C>
35 explicit dot_self_vari(
const Eigen::Matrix<var, R, C>& v) :
36 vari(var_dot_self(v)),
size_(v.size()) {
37 v_ =
reinterpret_cast<vari**
> 39 for (
size_t i = 0; i <
size_; ++i)
42 inline static double square(
double x) {
return x * x; }
43 inline static double var_dot_self(vari** v,
size_t size) {
45 for (
size_t i = 0; i <
size; ++i)
49 template<
typename Derived>
50 double var_dot_self(
const Eigen::DenseBase<Derived> &v) {
52 for (
int i = 0; i < v.size(); ++i)
53 sum +=
square(v(i).vi_->val_);
56 template <
int R,
int C>
57 inline static double var_dot_self(
const Eigen::Matrix<var, R, C> &v) {
59 for (
int i = 0; i < v.size(); ++i)
60 sum +=
square(v(i).vi_->val_);
63 virtual void chain() {
64 for (
size_t i = 0; i <
size_; ++i)
65 v_[i]->adj_ += adj_ * 2.0 * v_[i]->val_;
79 template<
int R,
int C>
82 return var(
new dot_self_vari(v));
fvar< T > sum(const std::vector< fvar< T > > &m)
Return the sum of the entries of the specified standard vector.
static stack_alloc memalloc_
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.
fvar< T > dot_self(const Eigen::Matrix< fvar< T >, R, C > &v)
fvar< T > square(const fvar< T > &x)
int size(const std::vector< T > &x)
Return the size of the specified standard vector.
void * alloc(size_t len)
Return a newly allocated block of memory of the appropriate size managed by the stack allocator...