1 #ifndef STAN_MATH_REV_MAT_FUN_DOT_PRODUCT_HPP
2 #define STAN_MATH_REV_MAT_FUN_DOT_PRODUCT_HPP
12 #include <boost/utility/enable_if.hpp>
13 #include <boost/type_traits.hpp>
21 struct dot_product_store_type;
24 struct dot_product_store_type<var> {
29 struct dot_product_store_type<double> {
33 template<
typename T1,
typename T2>
34 class dot_product_vari :
public vari {
36 typename dot_product_store_type<T1>::type
v1_;
37 typename dot_product_store_type<T2>::type
v2_;
40 inline static double var_dot(vari** v1, vari** v2,
42 Eigen::VectorXd vd1(length), vd2(length);
43 for (
size_t i = 0; i <
length; i++) {
50 inline static double var_dot(
const T1* v1,
const T2* v2,
53 Eigen::VectorXd vd1(length), vd2(length);
54 for (
size_t i = 0; i <
length; i++) {
61 template<
typename Derived1,
typename Derived2>
62 inline static double var_dot(
const Eigen::DenseBase<Derived1> &v1,
63 const Eigen::DenseBase<Derived2> &v2) {
66 Eigen::VectorXd vd1(v1.size()), vd2(v1.size());
67 for (
int i = 0; i < v1.size(); i++) {
73 inline void chain(vari** v1, vari** v2) {
74 for (
size_t i = 0; i <
length_; i++) {
75 v1[i]->adj_ += adj_ *
v2_[i]->val_;
76 v2[i]->adj_ += adj_ *
v1_[i]->val_;
79 inline void chain(
double* v1, vari** v2) {
80 for (
size_t i = 0; i <
length_; i++) {
81 v2[i]->adj_ += adj_ *
v1_[i];
84 inline void chain(vari** v1,
double* v2) {
85 for (
size_t i = 0; i <
length_; i++) {
86 v1[i]->adj_ += adj_ *
v2_[i];
90 vari **shared = NULL) {
94 for (
size_t i = 0; i <
length_; i++)
95 mem_v[i] = inv[i].vi_;
100 template<
typename Derived>
102 const Eigen::DenseBase<Derived> &inv,
103 vari **shared = NULL) {
104 if (shared == NULL) {
107 for (
size_t i = 0; i <
length_; i++)
108 mem_v[i] =
inv(i).vi_;
114 inline void initialize(
double* &mem_d,
const double *ind,
115 double *shared = NULL) {
116 if (shared == NULL) {
119 for (
size_t i = 0; i <
length_; i++)
125 template<
typename Derived>
127 const Eigen::DenseBase<Derived> &ind,
128 double *shared = NULL) {
129 if (shared == NULL) {
130 mem_d =
reinterpret_cast<double*
>
132 for (
size_t i = 0; i <
length_; i++)
140 dot_product_vari(
typename dot_product_store_type<T1>::type v1,
141 typename dot_product_store_type<T2>::type v2,
143 : vari(var_dot(v1, v2, length)),
v1_(v1),
v2_(v2),
length_(length) {}
145 dot_product_vari(
const T1* v1,
const T2* v2,
size_t length,
146 dot_product_vari<T1, T2>* shared_v1 = NULL,
147 dot_product_vari<T1, T2>* shared_v2 = NULL) :
148 vari(var_dot(v1, v2, length)),
length_(length) {
149 if (shared_v1 == NULL) {
154 if (shared_v2 == NULL) {
160 template<
typename Derived1,
typename Derived2>
161 dot_product_vari(
const Eigen::DenseBase<Derived1> &v1,
162 const Eigen::DenseBase<Derived2> &v2,
163 dot_product_vari<T1, T2>* shared_v1 = NULL,
164 dot_product_vari<T1, T2>* shared_v2 = NULL) :
166 if (shared_v1 == NULL) {
171 if (shared_v2 == NULL) {
177 template<
int R1,
int C1,
int R2,
int C2>
178 dot_product_vari(
const Eigen::Matrix<T1, R1, C1> &v1,
179 const Eigen::Matrix<T2, R2, C2> &v2,
180 dot_product_vari<T1, T2>* shared_v1 = NULL,
181 dot_product_vari<T1, T2>* shared_v2 = NULL) :
183 if (shared_v1 == NULL) {
188 if (shared_v2 == NULL) {
194 virtual void chain() {
208 template<
typename T1,
int R1,
int C1,
typename T2,
int R2,
int C2>
210 typename boost::enable_if_c<boost::is_same<T1, var>::value ||
211 boost::is_same<T2, var>::value, var>::type
213 const Eigen::Matrix<T2, R2, C2>& v2) {
219 return var(
new dot_product_vari<T1, T2>(v1, v2));
229 template<
typename T1,
typename T2>
231 typename boost::enable_if_c<boost::is_same<T1, var>::value ||
232 boost::is_same<T2, var>::value, var>::type
234 return var(
new dot_product_vari<T1, T2>(v1, v2, length));
245 template<
typename T1,
typename T2>
247 typename boost::enable_if_c<boost::is_same<T1, var>::value ||
248 boost::is_same<T2, var>::value, var>::type
250 const std::vector<T2>& v2) {
254 return var(
new dot_product_vari<T1, T2>(&v1[0], &v2[0], v1.size()));
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.
T value_of(const fvar< T > &v)
Return the value of the specified variable.
dot_product_store_type< T1 >::type v1_
static stack_alloc memalloc_
size_t length(const std::vector< T > &x)
Independent (input) and dependent (output) variables for gradients.
dot_product_store_type< T2 >::type v2_
void initialize(T &x, const T &v)
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.
fvar< T > dot_product(const Eigen::Matrix< fvar< T >, R1, C1 > &v1, const Eigen::Matrix< fvar< T >, R2, C2 > &v2)
int size(const std::vector< T > &x)
void * alloc(size_t len)
Return a newly allocated block of memory of the appropriate size managed by the stack allocator...
fvar< T > inv(const fvar< T > &x)