1 #ifndef STAN_MATH_PRIM_FUN_SUBTRACT_HPP
2 #define STAN_MATH_PRIM_FUN_SUBTRACT_HPP
3
4 #include <stan/math/prim/meta.hpp>
5 #include <stan/math/prim/err.hpp>
6 #include <stan/math/prim/fun/Eigen.hpp>
7
8 namespace stan {
9 namespace math {
10
11 /**
12 * Return the result of subtracting the second scalar from the first
13 * scalar.
14 *
15 * @tparam ScalarA type of the first scalar
16 * @tparam ScalarB type of the second scalar
17 * @param a first scalar
18 * @param b second scalar
19 * @return difference between first scalar and second scalar
20 */
21 template <typename ScalarA, typename ScalarB,
22 require_all_stan_scalar_t<ScalarA, ScalarB>* = nullptr,
23 require_all_not_var_t<ScalarA, ScalarB>* = nullptr>
subtract(const ScalarA & a,const ScalarB & b)24 inline return_type_t<ScalarA, ScalarB> subtract(const ScalarA& a,
25 const ScalarB& b) {
26 return a - b;
27 }
28
29 /**
30 * Return the result of subtracting the second specified matrix
31 * from the first specified matrix.
32 *
33 * @tparam Mat1 type of the first matrix or expression
34 * @tparam Mat2 type of the second matrix or expression
35 *
36 * @param m1 First matrix or expression.
37 * @param m2 Second matrix or expression.
38 * @return Difference between first matrix and second matrix.
39 */
40 template <typename Mat1, typename Mat2,
41 require_all_eigen_t<Mat1, Mat2>* = nullptr,
42 require_all_not_st_var<Mat1, Mat2>* = nullptr>
subtract(const Mat1 & m1,const Mat2 & m2)43 inline auto subtract(const Mat1& m1, const Mat2& m2) {
44 check_matching_dims("subtract", "m1", m1, "m2", m2);
45 return m1 - m2;
46 }
47
48 /**
49 * Return the result of subtracting the specified matrix from the specified
50 * scalar.
51 *
52 * @tparam Scal type of the scalar
53 * @tparam Mat type of the matrix or expression
54 * @param c Scalar.
55 * @param m Matrix or expression.
56 * @return The scalar minus the matrix.
57 */
58 template <typename Scal, typename Mat, require_stan_scalar_t<Scal>* = nullptr,
59 require_eigen_t<Mat>* = nullptr,
60 require_all_not_st_var<Mat, Scal>* = nullptr>
subtract(const Scal c,const Mat & m)61 inline auto subtract(const Scal c, const Mat& m) {
62 return (c - m.array()).matrix();
63 }
64
65 /**
66 * Return the result of subtracting the specified scalar from the specified
67 * matrix.
68 *
69 * @tparam Mat type of the matrix or expression
70 * @tparam Scal type of the scalar
71 * @param m Matrix or expression.
72 * @param c Scalar.
73 * @return The matrix minus the scalar.
74 */
75 template <typename Mat, typename Scal, require_eigen_t<Mat>* = nullptr,
76 require_stan_scalar_t<Scal>* = nullptr,
77 require_all_not_st_var<Scal, Mat>* = nullptr>
subtract(const Mat & m,const Scal c)78 inline auto subtract(const Mat& m, const Scal c) {
79 return (m.array() - c).matrix();
80 }
81
82 } // namespace math
83 } // namespace stan
84
85 #endif
86