-
-
Notifications
You must be signed in to change notification settings - Fork 193
allows complex abs() to be vectorized #2737
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
Conversation
Thanks so much @SteveBronder. I think given that I've worked on this and don't clearly understand the current math requirements, that @rok-cesnovar or someone else is going to have to review this. |
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.
Looks great! Thanks.
Did this also vectorize the integer signature of abs? |
Yes. std::vector<int> t{1,2,-3,4,-5,6,-1};
EXPECT_EQ(stan::math::abs(t), std::vector<int>({1, 2, 3, 4,5,6, 1})); passes. |
The relevant stanc pr is failing to compile the example model still:
It seems like the return type of the vectorized version may be incorrect? |
I can confirm this PR is broken for vectors of complex values. Adding this test exposes the issue:
Failure:
|
Also fails with a similar error (" |
Here's a version of the test which I think should pass at a minimum. The first two sections are already tested and pass, and the integer section also passes. The middle versions which use containers of complex values fail to compile. TEST(mixFun, absReturnType) {
using stan::math::abs;
// validate return types not overpromoted to complex by assignability
std::complex<stan::math::var> a = 3;
stan::math::var b = abs(a);
std::complex<stan::math::fvar<double>> c = 3;
stan::math::fvar<double> d = abs(c);
std::vector<std::complex<stan::math::var>> vc{3};
std::vector<stan::math::var> vr = abs(vc);
Eigen::MatrixXcd mc(0, 0);
Eigen::MatrixXd mr = abs(mc);
std::vector<Eigen::MatrixXcd> vmc{mc};
std::vector<Eigen::MatrixXd> vmr = abs(vmc);
std::vector<int> t{1, 2, -3, 4, -5, 6, -1};
// does not promote to real
t = abs(t);
SUCCEED();
} It wouldn't hurt to add tests which also check that the correct values are returned for |
Will look into this tomorrow. Thanks for digging all this up! |
I have a fix for this sorted out I'll put up in a little bit |
You can write tests using #include <typetraits>
template <typename E, typename T>
bool is_type(const T& x) {
return std::is_same<T, E>::value;
} and then call it with something like |
Summary
Alternative to #2734 that uses
apply_scalar_unary
to vectorizeabs()
for complex types and containers of complex types. Mostly we just remove the explicit instantiations forstd::vector<>
andEigen::Matrix
types and use theapply_scalar_unary
scheme to do the vectorization.Tests
@bob-carpenter I kept the tests you added in the above PR and added a few more to make sure everything still works with the new matrix type as well.
Side Effects
idt so, but adding as a note @andrjohns we should add a doc page describing how to write vectorized versions of scalar functions.
Release notes
Checklist
Math issue fully vectorize
abs
forint
,real
,complex
#2729Copyright holder: Steve Bronder
The copyright holder is typically you or your assignee, such as a university or company. By submitting this pull request, the copyright holder is agreeing to the license the submitted work under the following licenses:
- Code: BSD 3-clause (https://opensource.org/licenses/BSD-3-Clause)
- Documentation: CC-BY 4.0 (https://creativecommons.org/licenses/by/4.0/)
the basic tests are passing
./runTests.py test/unit
)make test-headers
)make test-math-dependencies
)make doxygen
)make cpplint
)the code is written in idiomatic C++ and changes are documented in the doxygen
the new changes are tested