All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Groups
sum.hpp
Go to the documentation of this file.
#ifndef STAN_MATH_REV_ARR_FUN_SUM_HPP
#define STAN_MATH_REV_ARR_FUN_SUM_HPP
#include <stan/math/rev/core.hpp>
#include <vector>
namespace stan {
namespace math {
class sum_v_vari : public vari {
protected:
vari** v_;
size_t length_;
inline static double sum_of_val(const std::vector<var>& v) {
double result = 0;
for (size_t i = 0; i < v.size(); i++)
result += v[i].val();
return result;
}
public:
explicit sum_v_vari(double value, vari** v, size_t length)
: vari(value), v_(v), length_(length) {
}
explicit sum_v_vari(const std::vector<var> &v1)
: vari(sum_of_val(v1)),
v_(reinterpret_cast<vari**>(ChainableStack::memalloc_
.alloc(v1.size() * sizeof(vari*)))),
length_(v1.size()) {
for (size_t i = 0; i < length_; i++)
v_[i] = v1[i].vi_;
}
virtual void chain() {
for (size_t i = 0; i < length_; i++) {
v_[i]->adj_ += adj_;
}
}
};
inline var sum(const std::vector<var>& m) {
if (m.size() == 0)
return 0.0;
return var(new sum_v_vari(m));
}
}
}
#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
stan::math::sum_v_vari::sum_v_vari
sum_v_vari(double value, vari **v, size_t length)
Definition: sum.hpp:27
stan::math::sum_v_vari::v_
vari ** v_
Definition: sum.hpp:16
core.hpp
stan::math::vari
The variable implementation base class.
Definition: vari.hpp:28
stan::length
size_t length(const std::vector< T > &x)
Definition: length.hpp:10
stan::math::var
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:32
stan::math::sum_v_vari::length_
size_t length_
Definition: sum.hpp:17
stan::math::sum_v_vari::chain
virtual void chain()
Apply the chain rule to this variable based on the variables on which it depends. ...
Definition: sum.hpp:40
stan::math::size
int size(const std::vector< T > &x)
Return the size of the specified standard vector.
Definition: size.hpp:17
stan::math::AutodiffStackStorage
Definition: autodiffstackstorage.hpp:12
stan::math::vari::adj_
double adj_
The adjoint of this variable, which is the partial derivative of this variable with respect to the ro...
Definition: vari.hpp:42
stan::math::sum_v_vari::sum_of_val
static double sum_of_val(const std::vector< var > &v)
Definition: sum.hpp:19
stan::math::sum_v_vari
Class for sums of variables constructed with standard vectors.
Definition: sum.hpp:14
stan::math::sum_v_vari::sum_v_vari
sum_v_vari(const std::vector< var > &v1)
Definition: sum.hpp:31

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