| Stan Math Library 2.12.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

subtract.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_FUN_SUBTRACT_HPP
2 #define STAN_MATH_PRIM_MAT_FUN_SUBTRACT_HPP
3
4 #include <boost/math/tools/promotion.hpp>
5 #include <stan/math/prim/mat/fun/Eigen.hpp>
6 #include <stan/math/prim/mat/err/check_matching_dims.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 subtract(const Eigen::Matrix<T1, R, C>& m1,
28 const Eigen::Matrix<T2, R, C>& m2) {
29 check_matching_dims("subtract", "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
38 template <typename T1, typename T2, int R, int C>
39 inline
40 Eigen::Matrix<typename boost::math::tools::promote_args<T1, T2>::type, R, C>
41 subtract(const T1& c,
42 const Eigen::Matrix<T2, R, C>& m) {
43 Eigen::Matrix<typename boost::math::tools::promote_args<T1, T2>::type,
44 R, C>
45 result(m.rows(), m.cols());
46 for (int i = 0; i < m.size(); ++i)
47 result(i) = c - m(i);
48 return result;
49 }
50
51 template <typename T1, typename T2, int R, int C>
52 inline
53 Eigen::Matrix<typename boost::math::tools::promote_args<T1, T2>::type, R, C>
54 subtract(const Eigen::Matrix<T1, R, C>& m,
55 const T2& c) {
56 Eigen::Matrix<typename boost::math::tools::promote_args<T1, T2>::type,
57 R, C>
58 result(m.rows(), m.cols());
59 for (int i = 0; i < m.size(); ++i)
60 result(i) = m(i) - c;
61 return result;
62 }
63
64 }
65 }
66 #endif
check_matching_dims.hpp
stan
Definition: log_sum_exp.hpp:8
stan::math::subtract
Eigen::Matrix< typename boost::math::tools::promote_args< T1, T2 >::type, R, C > subtract(const Eigen::Matrix< T1, R, C > &m1, const Eigen::Matrix< T2, R, C > &m2)
Return the result of subtracting the second specified matrix from the first specified matrix...
Definition: subtract.hpp:27
stan::math::check_matching_dims
bool 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)
Return true if the two matrices are of the same size.
Definition: check_matching_dims.hpp:37
Eigen.hpp

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