-
-
Notifications
You must be signed in to change notification settings - Fork 190
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
Adds simple vectorized functions for var<matrix> #2461
Conversation
…4.1 (tags/RELEASE_600/final)
I can take the review for this one, if nobody else has started yet. Did you have any more changes to add Steve? |
Much appreciated! No more changes! |
Jenkins Console Log Machine informationProductName: Mac OS X ProductVersion: 10.11.6 BuildVersion: 15G22010CPU: G++: Clang: |
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.
Looking good! Just some minor queries.
My big question would be these specialisations only get called if none of the varmat
inputs are nested. What happens if a user calls beta(double, std::vector<varmat>)
(if that's possible)?
@@ -62,7 +62,7 @@ namespace math { | |||
*/ | |||
template <typename T, require_arithmetic_t<T>* = nullptr> | |||
inline return_type_t<T> falling_factorial(const T& x, int n) { | |||
static const char* function = "falling_factorial"; | |||
constexpr const char* function = "falling_factorial"; |
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.
Should probably save this for a separate PR
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.
Yeah I can, though it's a teeny change that shouldn't effect much if you don't mind leaving it. In general we should sweep through the math library looking for static const char* function =
and make them constexpr
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.
I think probably best to pull it out for now, just so that all functions are consistent. But I'm not super tied to it, so feel free to ignore
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.
Let's leave it for now and I'll open up another PR this week just making a bunch of these constexpr
/* | ||
if (y == 0) { | ||
return make_callback_var(-log1p(-y_hat.val()), [y_hat](auto& vi) mutable { | ||
y_hat.adj().array() += vi.adj().array() / (1.0 - y_hat.val().array()); | ||
}); | ||
} else { | ||
return make_callback_var(-std::log(y_hat.val()), [y_hat](auto& vi) mutable { | ||
y_hat.adj().array() -= vi.adj().array() / y_hat.val().array(); | ||
}); | ||
} | ||
*/ | ||
} | ||
|
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.
Should be removed?
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.
absolutely
stan/math/rev/fun/exp2.hpp
Outdated
template <typename T, require_eigen_t<T>* = nullptr> | ||
inline auto exp2(const var_value<T>& a) { | ||
return make_callback_var( | ||
a.val().unaryExpr([](auto&& x) { return std::exp2(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.
Can probably just use the Stan Math exp2
here since that's vectorised as well
stan/math/rev/fun/expm1.hpp
Outdated
@@ -41,6 +41,13 @@ inline var expm1(const var& a) { | |||
}); | |||
} | |||
|
|||
template <typename T, require_matrix_t<T>* = nullptr> | |||
inline auto expm1(const var_value<T>& a) { | |||
return make_callback_var(expm1(a.val()).eval(), [a](auto& vi) mutable { |
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.
Why the eval
here when the others don't have it?
} | ||
|
||
template <typename T, typename StdVec, require_eigen_t<T>* = nullptr, | ||
require_vector_like_vt<std::is_integral, StdVec>* = nullptr> |
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.
I noticed the other functions use std::vector<int>
rather the require_
, any reason for the preference here?
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.
Honestly idt so, but this is all cleaned up with your suggestions above
Much appreciated for the review! I'll ping you when the tests pass |
Jenkins Console Log Machine informationProductName: Mac OS X ProductVersion: 10.11.6 BuildVersion: 15G22010CPU: G++: Clang: |
Pinging @andrjohns |
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.
Ack, sorry I missed the first ping! This all looks good, one minor comment around the the constexpr char
, but happy to leave it as well
Thanks!
Sorry I totally missed this. |
Summary
This adds a bunch of var versions of functions that were in #2422 . I stopped about halfway though so the PR didn't get too big.
Tests
Tests added for var equivalent functions.
Side Effects
One thing I wanted some guidance on, how should we doc the
var<matrix>
functions when the base version already has documentation? Should we just add a little doc about how this is the function specialized for var matrix types and to see the docs for<function>
for further details?Release notes
Adds
var<Matrix>
functions for bessel first and second kind, beta, binary log loss, ceil, erf, erfc, exp2, expm1, falling_factorial, floorChecklist
Math issue How to add static matrix? #1805
Copyright 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