-
-
Notifications
You must be signed in to change notification settings - Fork 187
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
var matrix specializations for a-c unary functions in rev #2256
Changes from 21 commits
0c52267
17b853d
2f8da9f
fe2354c
afda49a
91d1472
da21330
86d6f8a
13974c1
77f77e8
1768e1e
198bec3
010ebaf
fac9b65
11e3459
3a02c30
237f6a0
0e610cf
37d61ac
b0738f7
ba7dae7
8129f72
88c35d9
4fa4816
0db6223
a7deb36
7e41553
b26159f
812ae25
c5caa59
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,21 +3,57 @@ | |
|
||
#include <stan/math/prim/meta.hpp> | ||
#include <stan/math/prim/fun/hypot.hpp> | ||
#include <stan/math/prim/functor/apply_scalar_unary.hpp> | ||
#include <stan/math/prim/functor/apply_vector_unary.hpp> | ||
#include <cmath> | ||
|
||
namespace stan { | ||
namespace math { | ||
|
||
/** | ||
* Return floating-point absolute value. | ||
* Structure to wrap `abs()` so it can be vectorized. | ||
* | ||
* Delegates to <code>fabs(double)</code> rather than | ||
* <code>std::abs(int)</code>. | ||
* @tparam T type of variable | ||
* @param x argument | ||
* @return Arc cosine of variable in radians. | ||
*/ | ||
struct abs_fun { | ||
template <typename T> | ||
static inline T fun(const T& x) { | ||
using std::fabs; | ||
return fabs(x); | ||
} | ||
}; | ||
|
||
/** | ||
* Returns the elementwise `abs()` of the input, | ||
* which may be a scalar or any Stan container of numeric scalars. | ||
* | ||
* @param x scalar | ||
* @return absolute value of scalar | ||
* @tparam Container type of container | ||
* @param x argument | ||
* @return Arc cosine of each variable in the container, in radians. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Absolute value |
||
*/ | ||
inline double abs(double x) { return std::fabs(x); } | ||
template <typename Container, | ||
require_not_container_st<std::is_arithmetic, Container>* = nullptr, | ||
require_not_var_matrix_t<Container>* = nullptr> | ||
inline auto abs(const Container& x) { | ||
return apply_scalar_unary<abs_fun, Container>::apply(x); | ||
} | ||
|
||
/** | ||
* Version of `abs()` that accepts std::vectors, Eigen Matrix/Array objects | ||
* or expressions, and containers of these. | ||
* | ||
* @tparam Container Type of x | ||
* @param x argument | ||
* @return Arc cosine of each variable in the container, in radians. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Absolute value |
||
*/ | ||
template <typename Container, | ||
require_container_st<std::is_arithmetic, Container>* = nullptr> | ||
inline auto abs(const Container& x) { | ||
return apply_vector_unary<Container>::apply( | ||
x, [](const auto& v) { return v.array().abs(); }); | ||
} | ||
|
||
namespace internal { | ||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -83,8 +83,9 @@ struct acosh_fun { | |
* @param x container | ||
* @return Elementwise acosh of members of container. | ||
*/ | ||
template <typename T, require_all_not_nonscalar_prim_or_rev_kernel_expression_t< | ||
T>* = nullptr> | ||
template < | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not require_container_st? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess this is the other thing I'd expect: template <typename Container,
require_container_st<std::is_arithmetic, Container>* = nullptr>
inline auto acosh(const Container& x) {
return apply_vector_unary<Container>::apply(
x, [](const auto& v) { return v.array().acosh(); });
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually this can just be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There were some functions here that didn't have Edit: didn't exist at the time these functions were written but do exist now There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (But if you want to clean things, feel free, happy to review) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ups actually ignore that, this needs to be able to take in scalar types so that's why we can't use |
||
typename T, require_not_var_matrix_t<T>* = nullptr, | ||
require_all_not_nonscalar_prim_or_rev_kernel_expression_t<T>* = nullptr> | ||
inline auto acosh(const T& x) { | ||
return apply_scalar_unary<acosh_fun, T>::apply(x); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Absolute value