Skip to content
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

Expose hypergeometric_2F1 function #2792

Merged
merged 31 commits into from
Jul 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
332dd04
Add functions initial
andrjohns Jul 12, 2022
ccb2d8a
Update tests and cpplint
andrjohns Jul 12, 2022
1398625
[Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1
stan-buildbot Jul 12, 2022
aca78c9
Update template docs and inlining
andrjohns Jul 13, 2022
0a5e5f5
Merge branch 'develop' into add-hyper-2f1
andrjohns Jul 13, 2022
b529fd1
[Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1
stan-buildbot Jul 13, 2022
64141a5
Ensure same type scalar arguments for vector inp
andrjohns Jul 14, 2022
e054c78
[Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1
stan-buildbot Jul 14, 2022
b8518d5
Add special case rules and prim tests
andrjohns Jul 14, 2022
5139069
Begin updating grad_2f1
andrjohns Jul 14, 2022
e6f5e96
grad_2f1 only calculate for requested
andrjohns Jul 14, 2022
c461bce
Begin updating gradients for special cases
andrjohns Jul 14, 2022
30ab661
Update grad and hyper funs with Euler transform
andrjohns Jul 14, 2022
0e702b2
Finish doc and grad funs
andrjohns Jul 14, 2022
580508a
cpplint
andrjohns Jul 14, 2022
f662efa
Merge commit 'd3b129e6783ce86b094dab62409c4eaa329d506a' into HEAD
yashikno Jul 14, 2022
208196f
[Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1
stan-buildbot Jul 14, 2022
118a6df
Fix pFq 2F1 tests and doc special-cases
andrjohns Jul 14, 2022
f1b94cd
[Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1
stan-buildbot Jul 14, 2022
1b8c171
[Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1
stan-buildbot Jul 14, 2022
8c3b188
Update prim tests
andrjohns Jul 14, 2022
3503067
[Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1
stan-buildbot Jul 14, 2022
884491b
Review comments
andrjohns Jul 18, 2022
63cd400
[Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1
stan-buildbot Jul 18, 2022
58497c1
Update grad_pFq test args
andrjohns Jul 18, 2022
74b2347
[Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1
stan-buildbot Jul 18, 2022
067ec23
Missed 2F1 prim tests
andrjohns Jul 18, 2022
3ebb1a6
[Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1
stan-buildbot Jul 18, 2022
26e2a9a
Simplify grad_inc assignments, split euler transform logic
andrjohns Jul 19, 2022
99bf6f3
[Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1
stan-buildbot Jul 19, 2022
1475b99
Fix test failure due to undefined behaviour
andrjohns Jul 19, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions stan/math/fwd/fun.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include <stan/math/fwd/fun/gamma_p.hpp>
#include <stan/math/fwd/fun/gamma_q.hpp>
#include <stan/math/fwd/fun/grad_inc_beta.hpp>
#include <stan/math/fwd/fun/hypergeometric_2F1.hpp>
#include <stan/math/fwd/fun/hypergeometric_pFq.hpp>
#include <stan/math/fwd/fun/hypot.hpp>
#include <stan/math/fwd/fun/inc_beta.hpp>
Expand Down
6 changes: 4 additions & 2 deletions stan/math/fwd/fun/grad_inc_beta.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ void grad_inc_beta(fvar<T>& g1, fvar<T>& g2, fvar<T> a, fvar<T> b, fvar<T> z) {
fvar<T> dF1 = 0;
fvar<T> dF2 = 0;
fvar<T> dF3 = 0;
fvar<T> dFz = 0;

if (value_of(value_of(C))) {
grad_2F1(dF1, dF2, dF3, a + b, fvar<T>(1.0), a + 1, z);
if (value_of_rec(C)) {
std::forward_as_tuple(dF1, dF2, dF3, dFz)
= grad_2F1<true>(a + b, fvar<T>(1.0), a + 1, z);
}

g1 = (c1 - 1.0 / a) * c3 + C * (dF1 + dF3);
Expand Down
65 changes: 65 additions & 0 deletions stan/math/fwd/fun/hypergeometric_2F1.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#ifndef STAN_MATH_FWD_FUN_HYPERGEOMETRIC_2F1_HPP
#define STAN_MATH_FWD_FUN_HYPERGEOMETRIC_2F1_HPP

#include <stan/math/prim/meta.hpp>
#include <stan/math/fwd/core.hpp>
#include <stan/math/prim/fun/hypergeometric_2F1.hpp>
#include <stan/math/prim/fun/grad_2F1.hpp>

namespace stan {
namespace math {

/**
* Returns the Gauss hypergeometric function applied to the
* input arguments:
* \f$_2F_1(a_1,a_2;b;z)\f$
*
* See 'grad_2F1.hpp' for the derivatives wrt each parameter
*
* @tparam Ta1 Type of scalar first 'a' argument
* @tparam Ta2 Type of scalar second 'a' argument
* @tparam Tb Type of scalar 'b' argument
* @tparam Tz Type of scalar 'z' argument
* @param[in] a1 First of 'a' arguments to function
* @param[in] a2 Second of 'a' arguments to function
* @param[in] b 'b' argument to function
* @param[in] z Scalar z argument
* @return Gauss hypergeometric function
*/
template <typename Ta1, typename Ta2, typename Tb, typename Tz,
require_all_stan_scalar_t<Ta1, Ta2, Tb, Tz>* = nullptr,
require_any_fvar_t<Ta1, Ta2, Tb, Tz>* = nullptr>
inline return_type_t<Ta1, Ta1, Tb, Tz> hypergeometric_2F1(const Ta1& a1,
const Ta2& a2,
const Tb& b,
const Tz& z) {
using fvar_t = return_type_t<Ta1, Ta1, Tb, Tz>;

auto a1_val = value_of(a1);
auto a2_val = value_of(a2);
auto b_val = value_of(b);
auto z_val = value_of(z);

auto grad_tuple = grad_2F1(a1, a2, b, z);

typename fvar_t::Scalar grad = 0;

if (!is_constant<Ta1>::value) {
grad += forward_as<fvar_t>(a1).d() * std::get<0>(grad_tuple);
}
if (!is_constant<Ta2>::value) {
grad += forward_as<fvar_t>(a2).d() * std::get<1>(grad_tuple);
}
if (!is_constant<Tb>::value) {
grad += forward_as<fvar_t>(b).d() * std::get<2>(grad_tuple);
}
if (!is_constant<Tz>::value) {
grad += forward_as<fvar_t>(z).d() * std::get<3>(grad_tuple);
}

return fvar_t(hypergeometric_2F1(a1_val, a2_val, b_val, z_val), grad);
}

} // namespace math
} // namespace stan
#endif
3 changes: 2 additions & 1 deletion stan/math/prim/fun.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,9 @@
#include <stan/math/prim/fun/grad_reg_inc_gamma.hpp>
#include <stan/math/prim/fun/grad_reg_lower_inc_gamma.hpp>
#include <stan/math/prim/fun/head.hpp>
#include <stan/math/prim/fun/hypergeometric_pFq.hpp>
#include <stan/math/prim/fun/hypergeometric_2F1.hpp>
#include <stan/math/prim/fun/hypergeometric_2F2.hpp>
#include <stan/math/prim/fun/hypergeometric_pFq.hpp>
#include <stan/math/prim/fun/hypot.hpp>
#include <stan/math/prim/fun/identity_constrain.hpp>
#include <stan/math/prim/fun/identity_free.hpp>
Expand Down
Loading