|
6 | 6 | #include <stan/math/prim/meta/is_container.hpp> |
7 | 7 | #include <stan/math/prim/meta/is_eigen.hpp> |
8 | 8 | #include <stan/math/prim/meta/require_generics.hpp> |
| 9 | +#include <stan/math/prim/fun/num_elements.hpp> |
9 | 10 | #include <vector> |
10 | 11 |
|
11 | 12 | namespace stan { |
@@ -55,6 +56,116 @@ inline auto apply_scalar_binary(const T1& x, const T2& y, const F& f) { |
55 | 56 | return x.binaryExpr(y, f).eval(); |
56 | 57 | } |
57 | 58 |
|
| 59 | +/** |
| 60 | + * Specialisation for use with one Eigen vector (row or column) and |
| 61 | + * a one-dimensional std::vector of integer types |
| 62 | + * |
| 63 | + * @tparam T1 Type of first argument to which functor is applied. |
| 64 | + * @tparam T2 Type of second argument to which functor is applied. |
| 65 | + * @tparam F Type of functor to apply. |
| 66 | + * @param x Eigen input to which operation is applied. |
| 67 | + * @param y Integer std::vector input to which operation is applied. |
| 68 | + * @param f functor to apply to inputs. |
| 69 | + * @return Eigen object with result of applying functor to inputs. |
| 70 | + */ |
| 71 | +template <typename T1, typename T2, typename F, |
| 72 | + require_eigen_vector_vt<is_stan_scalar, T1>* = nullptr, |
| 73 | + require_std_vector_vt<std::is_integral, T2>* = nullptr> |
| 74 | +inline auto apply_scalar_binary(const T1& x, const T2& y, const F& f) { |
| 75 | + check_matching_sizes("Binary function", "x", x, "y", y); |
| 76 | + using int_vec_t = promote_scalar_t<value_type_t<T2>, plain_type_t<T1>>; |
| 77 | + Eigen::Map<const int_vec_t> y_map(y.data(), y.size()); |
| 78 | + return x.binaryExpr(y_map, f).eval(); |
| 79 | +} |
| 80 | + |
| 81 | +/** |
| 82 | + * Specialisation for use with a one-dimensional std::vector of integer types |
| 83 | + * and one Eigen vector (row or column). |
| 84 | + * |
| 85 | + * @tparam T1 Type of first argument to which functor is applied. |
| 86 | + * @tparam T2 Type of second argument to which functor is applied. |
| 87 | + * @tparam F Type of functor to apply. |
| 88 | + * @param x Integer std::vector input to which operation is applied. |
| 89 | + * @param y Eigen input to which operation is applied. |
| 90 | + * @param f functor to apply to inputs. |
| 91 | + * @return Eigen object with result of applying functor to inputs. |
| 92 | + */ |
| 93 | +template <typename T1, typename T2, typename F, |
| 94 | + require_std_vector_vt<std::is_integral, T1>* = nullptr, |
| 95 | + require_eigen_vector_vt<is_stan_scalar, T2>* = nullptr> |
| 96 | +inline auto apply_scalar_binary(const T1& x, const T2& y, const F& f) { |
| 97 | + check_matching_sizes("Binary function", "x", x, "y", y); |
| 98 | + using int_vec_t = promote_scalar_t<value_type_t<T1>, plain_type_t<T2>>; |
| 99 | + Eigen::Map<const int_vec_t> x_map(x.data(), x.size()); |
| 100 | + return x_map.binaryExpr(y, f).eval(); |
| 101 | +} |
| 102 | + |
| 103 | +/** |
| 104 | + * Specialisation for use with one Eigen matrix and |
| 105 | + * a two-dimensional std::vector of integer types |
| 106 | + * |
| 107 | + * @tparam T1 Type of first argument to which functor is applied. |
| 108 | + * @tparam T2 Type of second argument to which functor is applied. |
| 109 | + * @tparam F Type of functor to apply. |
| 110 | + * @param x Eigen matrix input to which operation is applied. |
| 111 | + * @param y Nested integer std::vector input to which operation is applied. |
| 112 | + * @param f functor to apply to inputs. |
| 113 | + * @return Eigen object with result of applying functor to inputs. |
| 114 | + */ |
| 115 | +template <typename T1, typename T2, typename F, |
| 116 | + require_eigen_matrix_vt<is_stan_scalar, T1>* = nullptr, |
| 117 | + require_std_vector_vt<is_std_vector, T2>* = nullptr, |
| 118 | + require_std_vector_st<std::is_integral, T2>* = nullptr> |
| 119 | +inline auto apply_scalar_binary(const T1& x, const T2& y, const F& f) { |
| 120 | + if (num_elements(x) != num_elements(y)) { |
| 121 | + std::ostringstream msg; |
| 122 | + msg << "Inputs to vectorized binary function must match in" |
| 123 | + << " size if one is not a scalar"; |
| 124 | + throw std::invalid_argument(msg.str()); |
| 125 | + } |
| 126 | + using return_st = decltype(f(x(0), y[0][0])); |
| 127 | + Eigen::Matrix<return_st, Eigen::Dynamic, Eigen::Dynamic> result(x.rows(), |
| 128 | + x.cols()); |
| 129 | + for (size_t i = 0; i < y.size(); ++i) { |
| 130 | + result.row(i) = apply_scalar_binary(x.row(i).transpose(), |
| 131 | + as_column_vector_or_scalar(y[i]), f); |
| 132 | + } |
| 133 | + return result; |
| 134 | +} |
| 135 | + |
| 136 | +/** |
| 137 | + * Specialisation for use with a two-dimensional std::vector of integer types |
| 138 | + * and one Eigen matrix. |
| 139 | + * |
| 140 | + * @tparam T1 Type of first argument to which functor is applied. |
| 141 | + * @tparam T2 Type of second argument to which functor is applied. |
| 142 | + * @tparam F Type of functor to apply. |
| 143 | + * @param x Nested integer std::vector input to which operation is applied. |
| 144 | + * @param y Eigen matrix input to which operation is applied. |
| 145 | + * @param f functor to apply to inputs. |
| 146 | + * @return Eigen object with result of applying functor to inputs. |
| 147 | + */ |
| 148 | +template <typename T1, typename T2, typename F, |
| 149 | + require_std_vector_vt<is_std_vector, T1>* = nullptr, |
| 150 | + require_std_vector_st<std::is_integral, T1>* = nullptr, |
| 151 | + require_eigen_matrix_vt<is_stan_scalar, T2>* = nullptr> |
| 152 | +inline auto apply_scalar_binary(const T1& x, const T2& y, const F& f) { |
| 153 | + if (num_elements(x) != num_elements(y)) { |
| 154 | + std::ostringstream msg; |
| 155 | + msg << "Inputs to vectorized binary function must match in" |
| 156 | + << " size if one is not a scalar"; |
| 157 | + throw std::invalid_argument(msg.str()); |
| 158 | + } |
| 159 | + using return_st = decltype(f(x[0][0], y(0))); |
| 160 | + Eigen::Matrix<return_st, Eigen::Dynamic, Eigen::Dynamic> result(y.rows(), |
| 161 | + y.cols()); |
| 162 | + for (size_t i = 0; i < x.size(); ++i) { |
| 163 | + result.row(i) = apply_scalar_binary(as_column_vector_or_scalar(x[i]), |
| 164 | + y.row(i).transpose(), f); |
| 165 | + } |
| 166 | + return result; |
| 167 | +} |
| 168 | + |
58 | 169 | /** |
59 | 170 | * Specialisation for use when the first input is an Eigen type and the second |
60 | 171 | * is a scalar. Eigen's unaryExpr framework is used for more efficient indexing |
|
0 commit comments