@@ -27,39 +27,30 @@ struct fabs_fun {
2727 * Returns the elementwise `fabs()` of the input,
2828 * which may be a scalar or any Stan container of numeric scalars.
2929 *
30- * @tparam T type of container
30+ * @tparam Container type of container
3131 * @param x container
3232 * @return Absolute value of each value in x.
3333 */
34- template <typename T, typename = require_not_eigen_vt<std::is_arithmetic, T>>
35- inline typename apply_scalar_unary<fabs_fun, T>::return_t fabs (const T& x) {
36- return apply_scalar_unary<fabs_fun, T>::apply (x);
34+ template <
35+ typename Container,
36+ require_not_container_st<is_container, std::is_arithmetic, Container>...>
37+ inline auto fabs (const Container& x) {
38+ return apply_scalar_unary<fabs_fun, Container>::apply (x);
3739}
3840
3941/* *
40- * Version of `fabs()` that accepts Eigen Matrix or matrix expressions.
42+ * Version of `fabs()` that accepts std::vectors, Eigen Matrix/Array objects
43+ * or expressions, and containers of these.
4144 *
42- * @tparam Derived derived type of x
43- * @param x Matrix or matrix expression
45+ * @tparam Container Type of x
46+ * @param x Container
4447 * @return Absolute value of each value in x.
4548 */
46- template <typename Derived,
47- typename = require_eigen_vt<std::is_arithmetic, Derived>>
48- inline auto fabs (const Eigen::MatrixBase<Derived>& x) {
49- return x.derived ().array ().abs ().matrix ().eval ();
50- }
51-
52- /* *
53- * Version of \c fabs() that accepts Eigen Array or array expressions.
54- *
55- * @tparam Derived derived type of x
56- * @param x Matrix or matrix expression
57- * @return Absolute value of each value in x.
58- */
59- template <typename Derived,
60- typename = require_eigen_vt<std::is_arithmetic, Derived>>
61- inline auto fabs (const Eigen::ArrayBase<Derived>& x) {
62- return x.derived ().abs ().eval ();
49+ template <typename Container,
50+ require_container_st<is_container, std::is_arithmetic, Container>...>
51+ inline auto fabs (const Container& x) {
52+ return apply_vector_unary<Container>::apply (
53+ x, [](const auto & v) { return v.array ().abs (); });
6354}
6455
6556} // namespace math
0 commit comments