Automatic Differentiation
 
Loading...
Searching...
No Matches
multiply.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_FWD_FUN_MULTIPLY_HPP
2#define STAN_MATH_FWD_FUN_MULTIPLY_HPP
3
11
12namespace stan {
13namespace math {
14
15template <typename Mat1, typename Mat2,
16 require_all_eigen_vt<is_fvar, Mat1, Mat2>* = nullptr,
17 require_vt_same<Mat1, Mat2>* = nullptr,
18 require_not_eigen_row_and_col_t<Mat1, Mat2>* = nullptr>
19inline auto multiply(const Mat1& m1, const Mat2& m2) {
20 check_multiplicable("multiply", "m1", m1, "m2", m2);
21 return (m1 * m2).eval();
22}
23
24template <typename Mat1, typename Mat2,
28inline auto multiply(const Mat1& m1, const Mat2& m2) {
29 check_multiplicable("multiply", "m1", m1, "m2", m2);
30 Eigen::Matrix<value_type_t<Mat1>, Mat1::RowsAtCompileTime,
31 Mat2::ColsAtCompileTime>
32 result(m1.rows(), m2.cols());
33 for (size_type i = 0; i < m1.rows(); i++) {
34 Eigen::Matrix<value_type_t<Mat1>, 1, Mat1::ColsAtCompileTime> crow
35 = m1.row(i);
36 for (size_type j = 0; j < m2.cols(); j++) {
37 result(i, j) = dot_product(crow, m2.col(j));
38 }
39 }
40 return result;
41}
42
43template <typename Mat1, typename Mat2,
44 require_eigen_vt<std::is_floating_point, Mat1>* = nullptr,
45 require_eigen_vt<is_fvar, Mat2>* = nullptr,
46 require_not_eigen_row_and_col_t<Mat1, Mat2>* = nullptr>
47inline auto multiply(const Mat1& m1, const Mat2& m2) {
48 check_multiplicable("multiply", "m1", m1, "m2", m2);
49 Eigen::Matrix<value_type_t<Mat2>, Mat1::RowsAtCompileTime,
50 Mat2::ColsAtCompileTime>
51 result(m1.rows(), m2.cols());
52 for (size_type i = 0; i < m1.rows(); i++) {
53 Eigen::Matrix<double, 1, Mat1::ColsAtCompileTime> crow = m1.row(i);
54 for (size_type j = 0; j < m2.cols(); j++) {
55 auto ccol = m2.col(j);
56 result(i, j) = dot_product(crow, ccol);
57 }
58 }
59 return result;
60}
61
62} // namespace math
63} // namespace stan
64#endif
require_t< container_type_check_base< is_eigen, value_type_t, TypeCheck, Check... > > require_eigen_vt
Require type satisfies is_eigen.
Definition is_eigen.hpp:152
void check_multiplicable(const char *function, const char *name1, const T1 &y1, const char *name2, const T2 &y2)
Check if the matrices can be multiplied.
auto multiply(const Mat1 &m1, const Mat2 &m2)
Return the product of the specified matrices.
Definition multiply.hpp:19
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic >::Index size_type
Type for sizes and indexes in an Eigen matrix with double elements.
Definition typedefs.hpp:11
auto dot_product(const T_a &a, const T_b &b)
Returns the dot product of the specified vectors.
require_not_t< math::conjunction< is_eigen_row_vector< Row >, is_eigen_col_vector< Col > > > require_not_eigen_row_and_col_t
Require Row is not a row vector and Col is not a column vector.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...