| Stan Math Library 2.6.3 probability, sampling & optimization

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros
dot_self.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_REV_MAT_FUN_DOT_SELF_HPP
2 #define STAN_MATH_REV_MAT_FUN_DOT_SELF_HPP
3
4 #include <stan/math/prim/mat/fun/Eigen.hpp>
5 #include <stan/math/prim/mat/fun/typedefs.hpp>
6 #include <stan/math/prim/mat/err/check_vector.hpp>
7 #include <stan/math/rev/core.hpp>
8 #include <stan/math/rev/mat/fun/typedefs.hpp>
9 #include <vector>
10
11 namespace stan {
12 namespace math {
13
14 namespace {
15 class dot_self_vari : public vari {
16 protected:
17 vari** v_;
18 size_t size_;
19
20 public:
21 dot_self_vari(vari** v, size_t size)
22 : vari(var_dot_self(v, size)),
23 v_(v),
24 size_(size) {
25 }
26 template<typename Derived>
27 explicit dot_self_vari(const Eigen::DenseBase<Derived> &v) :
28 vari(var_dot_self(v)), size_(v.size()) {
29 v_ = reinterpret_cast<vari**>(ChainableStack::memalloc_
30 .alloc(size_*sizeof(vari*)));
31 for (size_t i = 0; i < size_; i++)
32 v_[i] = v[i].vi_;
33 }
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**>
38 (ChainableStack::memalloc_.alloc(size_ * sizeof(vari*)));
39 for (size_t i = 0; i < size_; ++i)
40 v_[i] = v(i).vi_;
41 }
42 inline static double square(double x) { return x * x; }
43 inline static double var_dot_self(vari** v, size_t size) {
44 double sum = 0.0;
45 for (size_t i = 0; i < size; ++i)
46 sum += square(v[i]->val_);
47 return sum;
48 }
49 template<typename Derived>
50 double var_dot_self(const Eigen::DenseBase<Derived> &v) {
51 double sum = 0.0;
52 for (int i = 0; i < v.size(); ++i)
53 sum += square(v(i).vi_->val_);
54 return sum;
55 }
56 template <int R, int C>
57 inline static double var_dot_self(const Eigen::Matrix<var, R, C> &v) {
58 double sum = 0.0;
59 for (int i = 0; i < v.size(); ++i)
60 sum += square(v(i).vi_->val_);
61 return sum;
62 }
63 virtual void chain() {
64 for (size_t i = 0; i < size_; ++i)
65 v_[i]->adj_ += adj_ * 2.0 * v_[i]->val_;
66 }
67 };
68 }
79 template<int R, int C>
80 inline var dot_self(const Eigen::Matrix<var, R, C>& v) {
81 stan::math::check_vector("dot_self", "v", v);
82 return var(new dot_self_vari(v));
83 }
84
85 }
86 }
87 #endif
stan::math::sum
fvar< T > sum(const std::vector< fvar< T > > &m)
Return the sum of the entries of the specified standard vector.
Definition: sum.hpp:20
core.hpp
stan::math::check_vector
bool check_vector(const char *function, const char *name, const Eigen::Matrix< T, R, C > &x)
Return true if the matrix is either a row vector or column vector.
Definition: check_vector.hpp:34
stan::math::AutodiffStackStorage::memalloc_
static stack_alloc memalloc_
Definition: autodiffstackstorage.hpp:16
stan::math::var
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:32
stan::math::dot_self
fvar< T > dot_self(const Eigen::Matrix< fvar< T >, R, C > &v)
Definition: dot_self.hpp:16
stan::math::square
fvar< T > square(const fvar< T > &x)
Definition: square.hpp:15
typedefs.hpp
size_
size_t size_
Definition: dot_self.hpp:18
v_
vari ** v_
Definition: dot_self.hpp:17
Eigen.hpp
check_vector.hpp
stan::math::size
int size(const std::vector< T > &x)
Definition: size.hpp:11
stan::math::stack_alloc::alloc
void * alloc(size_t len)
Return a newly allocated block of memory of the appropriate size managed by the stack allocator...
Definition: stack_alloc.hpp:170
typedefs.hpp

[ Stan Home Page ] © 2011–2015, Stan Development Team.