All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Groups
dot_product.hpp
Go to the documentation of this file.
#ifndef STAN_MATH_REV_MAT_FUN_DOT_PRODUCT_HPP
#define STAN_MATH_REV_MAT_FUN_DOT_PRODUCT_HPP
#include <stan/math/prim/mat/fun/Eigen.hpp>
#include <stan/math/prim/mat/fun/typedefs.hpp>
#include <stan/math/prim/mat/err/check_vector.hpp>
#include <stan/math/prim/mat/err/check_matching_sizes.hpp>
#include <stan/math/prim/scal/fun/value_of.hpp>
#include <stan/math/rev/core.hpp>
#include <stan/math/rev/mat/fun/typedefs.hpp>
#include <stan/math/rev/scal/fun/value_of.hpp>
#include <boost/utility/enable_if.hpp>
#include <boost/type_traits.hpp>
#include <vector>
namespace stan {
namespace math {
namespace {
template<typename T>
struct dot_product_store_type;
template<>
struct dot_product_store_type<var> {
typedef vari** type;
};
template<>
struct dot_product_store_type<double> {
typedef double* type;
};
template<typename T1, typename T2>
class dot_product_vari : public vari {
protected:
typename dot_product_store_type<T1>::type v1_;
typename dot_product_store_type<T2>::type v2_;
size_t length_;
inline static double var_dot(vari** v1, vari** v2,
size_t length) {
Eigen::VectorXd vd1(length), vd2(length);
for (size_t i = 0; i < length; i++) {
vd1[i] = v1[i]->val_;
vd2[i] = v2[i]->val_;
}
return vd1.dot(vd2);
}
inline static double var_dot(const T1* v1, const T2* v2,
size_t length) {
using stan::math::value_of;
Eigen::VectorXd vd1(length), vd2(length);
for (size_t i = 0; i < length; i++) {
vd1[i] = value_of(v1[i]);
vd2[i] = value_of(v2[i]);
}
return vd1.dot(vd2);
}
template<typename Derived1, typename Derived2>
inline static double var_dot(const Eigen::DenseBase<Derived1> &v1,
const Eigen::DenseBase<Derived2> &v2) {
using stan::math::value_of;
using stan::math::value_of;
Eigen::VectorXd vd1(v1.size()), vd2(v1.size());
for (int i = 0; i < v1.size(); i++) {
vd1[i] = value_of(v1[i]);
vd2[i] = value_of(v2[i]);
}
return vd1.dot(vd2);
}
inline void chain(vari** v1, vari** v2) {
for (size_t i = 0; i < length_; i++) {
v1[i]->adj_ += adj_ * v2_[i]->val_;
v2[i]->adj_ += adj_ * v1_[i]->val_;
}
}
inline void chain(double* v1, vari** v2) {
for (size_t i = 0; i < length_; i++) {
v2[i]->adj_ += adj_ * v1_[i];
}
}
inline void chain(vari** v1, double* v2) {
for (size_t i = 0; i < length_; i++) {
v1[i]->adj_ += adj_ * v2_[i];
}
}
inline void initialize(vari** &mem_v, const var *inv,
vari **shared = NULL) {
if (shared == NULL) {
mem_v = reinterpret_cast<vari**>(ChainableStack::memalloc_
.alloc(length_*sizeof(vari*)));
for (size_t i = 0; i < length_; i++)
mem_v[i] = inv[i].vi_;
} else {
mem_v = shared;
}
}
template<typename Derived>
inline void initialize(vari** &mem_v,
const Eigen::DenseBase<Derived> &inv,
vari **shared = NULL) {
if (shared == NULL) {
mem_v = reinterpret_cast<vari**>(ChainableStack::memalloc_
.alloc(length_*sizeof(vari*)));
for (size_t i = 0; i < length_; i++)
mem_v[i] = inv(i).vi_;
} else {
mem_v = shared;
}
}
inline void initialize(double* &mem_d, const double *ind,
double *shared = NULL) {
if (shared == NULL) {
mem_d = reinterpret_cast<double*>(ChainableStack::memalloc_
.alloc(length_*sizeof(double)));
for (size_t i = 0; i < length_; i++)
mem_d[i] = ind[i];
} else {
mem_d = shared;
}
}
template<typename Derived>
inline void initialize(double* &mem_d,
const Eigen::DenseBase<Derived> &ind,
double *shared = NULL) {
if (shared == NULL) {
mem_d = reinterpret_cast<double*>
(ChainableStack::memalloc_.alloc(length_*sizeof(double)));
for (size_t i = 0; i < length_; i++)
mem_d[i] = ind(i);
} else {
mem_d = shared;
}
}
public:
dot_product_vari(typename dot_product_store_type<T1>::type v1,
typename dot_product_store_type<T2>::type v2,
size_t length)
: vari(var_dot(v1, v2, length)), v1_(v1), v2_(v2), length_(length) {}
dot_product_vari(const T1* v1, const T2* v2, size_t length,
dot_product_vari<T1, T2>* shared_v1 = NULL,
dot_product_vari<T1, T2>* shared_v2 = NULL) :
vari(var_dot(v1, v2, length)), length_(length) {
if (shared_v1 == NULL) {
initialize(v1_, v1);
} else {
initialize(v1_, v1, shared_v1->v1_);
}
if (shared_v2 == NULL) {
initialize(v2_, v2);
} else {
initialize(v2_, v2, shared_v2->v2_);
}
}
template<typename Derived1, typename Derived2>
dot_product_vari(const Eigen::DenseBase<Derived1> &v1,
const Eigen::DenseBase<Derived2> &v2,
dot_product_vari<T1, T2>* shared_v1 = NULL,
dot_product_vari<T1, T2>* shared_v2 = NULL) :
vari(var_dot(v1, v2)), length_(v1.size()) {
if (shared_v1 == NULL) {
initialize(v1_, v1);
} else {
initialize(v1_, v1, shared_v1->v1_);
}
if (shared_v2 == NULL) {
initialize(v2_, v2);
} else {
initialize(v2_, v2, shared_v2->v2_);
}
}
template<int R1, int C1, int R2, int C2>
dot_product_vari(const Eigen::Matrix<T1, R1, C1> &v1,
const Eigen::Matrix<T2, R2, C2> &v2,
dot_product_vari<T1, T2>* shared_v1 = NULL,
dot_product_vari<T1, T2>* shared_v2 = NULL) :
vari(var_dot(v1, v2)), length_(v1.size()) {
if (shared_v1 == NULL) {
initialize(v1_, v1);
} else {
initialize(v1_, v1, shared_v1->v1_);
}
if (shared_v2 == NULL) {
initialize(v2_, v2);
} else {
initialize(v2_, v2, shared_v2->v2_);
}
}
virtual void chain() {
chain(v1_, v2_);
}
};
}
template<typename T1, int R1, int C1, typename T2, int R2, int C2>
inline
typename boost::enable_if_c<boost::is_same<T1, var>::value ||
boost::is_same<T2, var>::value, var>::type
dot_product(const Eigen::Matrix<T1, R1, C1>& v1,
const Eigen::Matrix<T2, R2, C2>& v2) {
stan::math::check_vector("dot_product", "v1", v1);
stan::math::check_vector("dot_product", "v2", v2);
stan::math::check_matching_sizes("dot_product",
"v1", v1,
"v2", v2);
return var(new dot_product_vari<T1, T2>(v1, v2));
}
template<typename T1, typename T2>
inline
typename boost::enable_if_c<boost::is_same<T1, var>::value ||
boost::is_same<T2, var>::value, var>::type
dot_product(const T1* v1, const T2* v2, size_t length) {
return var(new dot_product_vari<T1, T2>(v1, v2, length));
}
template<typename T1, typename T2>
inline
typename boost::enable_if_c<boost::is_same<T1, var>::value ||
boost::is_same<T2, var>::value, var>::type
dot_product(const std::vector<T1>& v1,
const std::vector<T2>& v2) {
stan::math::check_matching_sizes("dot_product",
"v1", v1,
"v2", v2);
return var(new dot_product_vari<T1, T2>(&v1[0], &v2[0], v1.size()));
}
}
}
#endif
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::value_of
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition: value_of.hpp:16
v1_
dot_product_store_type< T1 >::type v1_
Definition: dot_product.hpp:36
stan::math::AutodiffStackStorage::memalloc_
static stack_alloc memalloc_
Definition: autodiffstackstorage.hpp:16
stan::length
size_t length(const std::vector< T > &x)
Definition: length.hpp:10
check_matching_sizes.hpp
stan::math::var
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:32
v2_
dot_product_store_type< T2 >::type v2_
Definition: dot_product.hpp:37
stan::math::initialize
void initialize(T &x, const T &v)
Definition: initialize.hpp:17
typedefs.hpp
stan::math::check_matching_sizes
bool check_matching_sizes(const char *function, const char *name1, const T_y1 &y1, const char *name2, const T_y2 &y2)
Return true if two structures at the same size.
Definition: check_matching_sizes.hpp:29
stan::math::dot_product
fvar< T > dot_product(const Eigen::Matrix< fvar< T >, R1, C1 > &v1, const Eigen::Matrix< fvar< T >, R2, C2 > &v2)
Definition: dot_product.hpp:20
Eigen.hpp
check_vector.hpp
stan::math::size
int size(const std::vector< T > &x)
Return the size of the specified standard vector.
Definition: size.hpp:17
length_
size_t length_
Definition: dot_product.hpp:38
value_of.hpp
value_of.hpp
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
stan::math::inv
fvar< T > inv(const fvar< T > &x)
Definition: inv.hpp:15
typedefs.hpp

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