1 #ifndef STAN_MATH_REV_MAT_FUN_DOT_PRODUCT_HPP 2 #define STAN_MATH_REV_MAT_FUN_DOT_PRODUCT_HPP 11 #include <type_traits> 31 template <
typename T1,
typename T2>
39 Eigen::VectorXd vd1(length), vd2(length);
40 for (
size_t i = 0; i <
length; i++) {
47 inline static double var_dot(
const T1* v1,
const T2* v2,
size_t length) {
48 Eigen::VectorXd vd1(length), vd2(length);
49 for (
size_t i = 0; i <
length; i++) {
56 template <
typename Derived1,
typename Derived2>
57 inline static double var_dot(
const Eigen::DenseBase<Derived1>& v1,
58 const Eigen::DenseBase<Derived2>& v2) {
59 Eigen::VectorXd vd1(v1.size()), vd2(v1.size());
60 for (
int i = 0; i < v1.size(); i++) {
67 for (
size_t i = 0; i < length_; i++) {
68 v1[i]->
adj_ += adj_ * v2_[i]->val_;
69 v2[i]->
adj_ += adj_ * v1_[i]->val_;
73 for (
size_t i = 0; i < length_; i++) {
74 v2[i]->
adj_ += adj_ * v1_[i];
78 for (
size_t i = 0; i < length_; i++) {
79 v1[i]->
adj_ += adj_ * v2_[i];
83 vari** shared =
nullptr) {
84 if (shared ==
nullptr) {
85 mem_v =
reinterpret_cast<vari**
>(
87 for (
size_t i = 0; i < length_; i++)
88 mem_v[i] = inv[i].vi_;
93 template <
typename Derived>
95 vari** shared =
nullptr) {
96 if (shared ==
nullptr) {
97 mem_v =
reinterpret_cast<vari**
>(
99 for (
size_t i = 0; i < length_; i++)
100 mem_v[i] =
inv(i).vi_;
107 double* shared =
nullptr) {
108 if (shared ==
nullptr) {
109 mem_d =
reinterpret_cast<double*
>(
111 for (
size_t i = 0; i < length_; i++)
117 template <
typename Derived>
118 inline void initialize(
double*& mem_d,
const Eigen::DenseBase<Derived>& ind,
119 double* shared =
nullptr) {
120 if (shared ==
nullptr) {
121 mem_d =
reinterpret_cast<double*
>(
123 for (
size_t i = 0; i < length_; i++)
133 :
vari(var_dot(v1, v2, length)), v1_(v1), v2_(v2), length_(length) {}
138 :
vari(var_dot(v1, v2, length)), length_(length) {
139 if (shared_v1 == NULL) {
144 if (shared_v2 == NULL) {
150 template <
typename Derived1,
typename Derived2>
152 const Eigen::DenseBase<Derived2>& v2,
155 :
vari(var_dot(v1, v2)), length_(v1.
size()) {
156 if (shared_v1 == NULL) {
161 if (shared_v2 == NULL) {
167 template <
int R1,
int C1,
int R2,
int C2>
169 const Eigen::Matrix<T2, R2, C2>& v2,
172 :
vari(var_dot(v1, v2)), length_(v1.
size()) {
173 if (shared_v1 == NULL) {
178 if (shared_v2 == NULL) {
196 template <
typename T1,
int R1,
int C1,
typename T2,
int R2,
int C2>
197 inline typename std::enable_if<
198 std::is_same<T1, var>::value || std::is_same<T2, var>::value,
var>::type
200 const Eigen::Matrix<T2, R2, C2>& v2) {
214 template <
typename T1,
typename T2>
215 inline typename std::enable_if<
216 std::is_same<T1, var>::value || std::is_same<T2, var>::value,
var>::type
229 template <
typename T1,
typename T2>
230 inline typename std::enable_if<
231 std::is_same<T1, var>::value || std::is_same<T2, var>::value,
var>::type
232 dot_product(
const std::vector<T1>& v1,
const std::vector<T2>& v2) {
void chain(double *v1, vari **v2)
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)
void initialize(vari **&mem_v, const Eigen::DenseBase< Derived > &inv, vari **shared=nullptr)
void initialize(double *&mem_d, const Eigen::DenseBase< Derived > &ind, double *shared=nullptr)
T value_of(const fvar< T > &v)
Return the value of the specified variable.
void initialize(vari **&mem_v, const var *inv, vari **shared=nullptr)
void chain(vari **v1, vari **v2)
The variable implementation base class.
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.
size_t length(const std::vector< T > &x)
Returns the length of the provided std::vector.
static double var_dot(const T1 *v1, const T2 *v2, size_t length)
static double var_dot(vari **v1, vari **v2, size_t length)
static STAN_THREADS_DEF AutodiffStackStorage * instance_
Independent (input) and dependent (output) variables for gradients.
dot_product_vari(typename dot_product_store_type< T1 >::type v1, typename dot_product_store_type< T2 >::type v2, size_t length)
dot_product_store_type< T1 >::type v1_
const double val_
The value of this variable.
void initialize(T &x, const T &v)
virtual void chain()
Apply the chain rule to this variable based on the variables on which it depends. ...
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)
fvar< T > dot_product(const Eigen::Matrix< fvar< T >, R1, C1 > &v1, const Eigen::Matrix< fvar< T >, R2, C2 > &v2)
void chain(vari **v1, double *v2)
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)
dot_product_store_type< T2 >::type v2_
int size(const std::vector< T > &x)
Return the size of the specified standard vector.
void check_matching_sizes(const char *function, const char *name1, const T_y1 &y1, const char *name2, const T_y2 &y2)
Check if two structures at the same size.
void initialize(double *&mem_d, const double *ind, double *shared=nullptr)
double adj_
The adjoint of this variable, which is the partial derivative of this variable with respect to the ro...
static double var_dot(const Eigen::DenseBase< Derived1 > &v1, const Eigen::DenseBase< Derived2 > &v2)
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)