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
10
11namespace stan {
12namespace math {
13
14template <typename Mat1, typename Mat2,
15 require_all_eigen_vt<is_fvar, Mat1, Mat2>* = nullptr,
16 require_vt_same<Mat1, Mat2>* = nullptr,
17 require_not_eigen_row_and_col_t<Mat1, Mat2>* = nullptr>
18inline auto multiply(const Mat1& m1, const Mat2& m2) {
19 check_multiplicable("multiply", "m1", m1, "m2", m2);
20 return (m1 * m2).eval();
21}
22
23template <typename Mat1, typename Mat2,
27inline auto multiply(const Mat1& m1, const Mat2& m2) {
28 check_multiplicable("multiply", "m1", m1, "m2", m2);
29 Eigen::Matrix<value_type_t<Mat1>, Mat1::RowsAtCompileTime,
30 Mat2::ColsAtCompileTime>
31 result(m1.rows(), m2.cols());
32 for (size_type i = 0; i < m1.rows(); i++) {
33 Eigen::Matrix<value_type_t<Mat1>, 1, Mat1::ColsAtCompileTime> crow
34 = m1.row(i);
35 for (size_type j = 0; j < m2.cols(); j++) {
36 result(i, j) = dot_product(crow, m2.col(j));
37 }
38 }
39 return result;
40}
41
42template <typename Mat1, typename Mat2,
43 require_eigen_vt<std::is_floating_point, Mat1>* = nullptr,
44 require_eigen_vt<is_fvar, Mat2>* = nullptr,
45 require_not_eigen_row_and_col_t<Mat1, Mat2>* = nullptr>
46inline auto multiply(const Mat1& m1, const Mat2& m2) {
47 check_multiplicable("multiply", "m1", m1, "m2", m2);
48 Eigen::Matrix<value_type_t<Mat2>, Mat1::RowsAtCompileTime,
49 Mat2::ColsAtCompileTime>
50 result(m1.rows(), m2.cols());
51 for (size_type i = 0; i < m1.rows(); i++) {
52 Eigen::Matrix<double, 1, Mat1::ColsAtCompileTime> crow = m1.row(i);
53 for (size_type j = 0; j < m2.cols(); j++) {
54 auto ccol = m2.col(j);
55 result(i, j) = dot_product(crow, ccol);
56 }
57 }
58 return result;
59}
60
61} // namespace math
62} // namespace stan
63#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:97
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:18
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 ...
Definition fvar.hpp:9