Stan Math Library  2.14.0
reverse mode automatic differentiation
add.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_FUN_ADD_HPP
2 #define STAN_MATH_PRIM_MAT_FUN_ADD_HPP
3 
4 #include <boost/math/tools/promotion.hpp>
7 
8 namespace stan {
9  namespace math {
10 
24  template <typename T1, typename T2, int R, int C>
25  inline
26  Eigen::Matrix<typename boost::math::tools::promote_args<T1, T2>::type, R, C>
27  add(const Eigen::Matrix<T1, R, C>& m1,
28  const Eigen::Matrix<T2, R, C>& m2) {
29  check_matching_dims("add", "m1", m1, "m2", m2);
30  Eigen::Matrix<typename boost::math::tools::promote_args<T1, T2>::type,
31  R, C>
32  result(m1.rows(), m1.cols());
33  for (int i = 0; i < result.size(); ++i)
34  result(i) = m1(i) + m2(i);
35  return result;
36  }
37 
47  template <typename T1, typename T2, int R, int C>
48  inline
49  Eigen::Matrix<typename boost::math::tools::promote_args<T1, T2>::type, R, C>
50  add(const Eigen::Matrix<T1, R, C>& m,
51  const T2& c) {
52  Eigen::Matrix<typename boost::math::tools::promote_args<T1, T2>::type,
53  R, C>
54  result(m.rows(), m.cols());
55  for (int i = 0; i < result.size(); ++i)
56  result(i) = m(i) + c;
57  return result;
58  }
59 
69  template <typename T1, typename T2, int R, int C>
70  inline
71  Eigen::Matrix<typename boost::math::tools::promote_args<T1, T2>::type, R, C>
72  add(const T1& c,
73  const Eigen::Matrix<T2, R, C>& m) {
74  Eigen::Matrix<typename boost::math::tools::promote_args<T1, T2>::type,
75  R, C>
76  result(m.rows(), m.cols());
77  for (int i = 0; i < result.size(); ++i)
78  result(i) = c + m(i);
79  return result;
80  }
81 
82  }
83 }
84 #endif
void check_matching_dims(const char *function, const char *name1, const Eigen::Matrix< T1, R1, C1 > &y1, const char *name2, const Eigen::Matrix< T2, R2, C2 > &y2)
Check if the two matrices are of the same size.
Eigen::Matrix< typename boost::math::tools::promote_args< T1, T2 >::type, R, C > add(const Eigen::Matrix< T1, R, C > &m1, const Eigen::Matrix< T2, R, C > &m2)
Return the sum of the specified matrices.
Definition: add.hpp:27

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