Skip to content

Commit

Permalink
Updated test code to work with c++14 (Issue #993)
Browse files Browse the repository at this point in the history
  • Loading branch information
bbbales2 committed Sep 4, 2018
1 parent 1b3cc0c commit 1b3c059
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 43 deletions.
3 changes: 2 additions & 1 deletion stan/math/prim/mat/fun/promote_double_to_T.hpp
Expand Up @@ -105,7 +105,8 @@ auto promote_double_to_T_impl_impl(std::tuple<T_output...> output,
template <typename T, std::size_t... I, typename... T_inputs>
auto promote_double_to_T_impl(std::index_sequence<I...>,
const std::tuple<T_inputs...>& input) {
return promote_double_to_T_impl_impl<T>(std::tuple(), std::get<I>(input)...);
return promote_double_to_T_impl_impl<T>(std::tuple<>(),
std::get<I>(input)...);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion stan/math/prim/mat/fun/variable_adapter.hpp
@@ -1,6 +1,7 @@
#ifndef STAN_MATH_PRIM_MAT_FUN_VARIABLE_ADAPTER_HPP
#define STAN_MATH_PRIM_MAT_FUN_VARIABLE_ADAPTER_HPP

#include <stan/math/prim/scal/functor/apply.hpp>
#include <stan/math/prim/scal/err/check_less.hpp>
#include <vector>
#include <cstddef>
Expand Down Expand Up @@ -172,7 +173,7 @@ class variable_adapter {
auto& operator()(size_t i) {
check_less("variable_adapter::operator()", "i", i, size_);

return std::apply(
return apply(
[ i, this ](auto&... args) -> auto& { return this->get(i, args...); },
args_);
}
Expand Down
1 change: 1 addition & 0 deletions stan/math/prim/scal.hpp
Expand Up @@ -177,6 +177,7 @@
#include <stan/math/prim/scal/fun/value_of.hpp>
#include <stan/math/prim/scal/fun/value_of_rec.hpp>

#include <stan/math/prim/scal/functor/apply.hpp>
#include <stan/math/prim/scal/functor/call_all_argument_combos.hpp>

#include <stan/math/prim/scal/prob/bernoulli_ccdf_log.hpp>
Expand Down
50 changes: 50 additions & 0 deletions stan/math/prim/scal/functor/apply.hpp
@@ -0,0 +1,50 @@
#ifndef STAN_MATH_PRIM_SCAL_FUNCTOR_APPLY_HPP
#define STAN_MATH_PRIM_SCAL_FUNCTOR_APPLY_HPP

#include <functional>
#include <tuple>
#include <utility>

namespace stan {
namespace math {
/*
* Invoke the functor f with arguments given in t and indexed in the index
* sequence I
*
* @tparam F Type of functor
* @tparam Tuple Type of tuple containing arguments
* @tparam I Index sequence going from 0 to std::tuple_size<T>::value - 1
* inclusive
* @param f functor callable
* @param t tuple of arguments
* @param i placeholder variable for index sequence
*/
template <class F, class Tuple, std::size_t... I>
constexpr decltype(auto) apply_impl(F&& f, Tuple&& t,
std::index_sequence<I...> i) {
return f(std::forward<decltype(std::get<I>(t))>(std::get<I>(t))...);
}

/*
* Call the functor f with the tuple of arguments t, like:
*
* f(std::get<0>(t), std::get<1>(t), ...)
*
* TODO: replace this with implementation in C++ std when C++17 is available
*
* @tparam F Type of functor
* @tparam Tuple Type of tuple containing arguments
* @param f functor callable
* @param t tuple of arguments
*/
template <class F, class Tuple>
constexpr decltype(auto) apply(F&& f, Tuple&& t) {
return apply_impl(std::forward<F>(f), std::forward<Tuple>(t),
std::make_index_sequence<
std::tuple_size<std::remove_reference_t<Tuple>>{}>{});
}

} // namespace math
} // namespace stan

#endif
35 changes: 0 additions & 35 deletions stan/math/rev/mat/fun/adj_jac_apply.hpp
Expand Up @@ -12,41 +12,6 @@ namespace stan {
namespace math {

namespace {
/*
* Invoke the functor f with arguments given in t and indexed in the index
* sequence I
*
* @tparam F Type of functor
* @tparam Tuple Type of tuple containing arguments
* @tparam I Index sequence going from 0 to std::tuple_size<T>::value - 1
* inclusive
* @param f functor callable
* @param t tuple of arguments
* @param i placeholder variable for index sequence
*/
template <class F, class Tuple, std::size_t... I>
constexpr auto apply_impl(const F& f, const Tuple& t,
std::index_sequence<I...> i) {
return f(std::get<I>(t)...);
}

/*
* Call the functor f with the tuple of arguments t, like:
*
* f(std::get<0>(t), std::get<1>(t), ...)
*
* TODO: replace this with implementation in C++ std when C++17 is available
*
* @tparam F Type of functor
* @tparam Tuple Type of tuple containing arguments
* @param f functor callable
* @param t tuple of arguments
*/
template <class F, class Tuple>
constexpr auto apply(const F& f, const Tuple& t) {
return apply_impl(f, t, std::make_index_sequence<std::tuple_size<Tuple>{}>{});
}

/**
* build_y_adj takes the adjoint from the vari pointed to
* by y_vi_[0] and stores it in y_adj
Expand Down
10 changes: 5 additions & 5 deletions test/unit/math/rev/mat/util/test_autodiff.hpp
Expand Up @@ -203,14 +203,14 @@ void test_var(F f, double dx, const Targs&... args) {
return;
}

auto output = make_variable_adapter<var>(std::apply(f, input.get_args()));
auto output = make_variable_adapter<var>(apply(f, input.get_args()));

if (output.size() == 0) {
throw std::runtime_error("The function produced no output");
}

auto output_double
= make_variable_adapter<double>(std::apply(fd, input.get_args()));
= make_variable_adapter<double>(apply(fd, input.get_args()));

if (output_double.size() != output.size()) {
std::cout << "The non-autodiff output has " << output_double.size()
Expand All @@ -231,13 +231,13 @@ void test_var(F f, double dx, const Targs&... args) {

Eigen::MatrixXd fd_jac(output.size(), input.size());
for (size_t j = 0; j < input.size(); ++j) {
variable_adapter fd_input = input;
auto fd_input = input;
fd_input(j) += dx;
auto output1
= make_variable_adapter<double>(std::apply(fd, fd_input.get_args()));
= make_variable_adapter<double>(apply(fd, fd_input.get_args()));
fd_input(j) -= 2 * dx;
auto output2
= make_variable_adapter<double>(std::apply(fd, fd_input.get_args()));
= make_variable_adapter<double>(apply(fd, fd_input.get_args()));
for (size_t i = 0; i < output.size(); ++i) {
fd_jac(i, j) = (output1(i) - output2(i)) / (2.0 * dx);
}
Expand Down
2 changes: 1 addition & 1 deletion test/unit/math/rev/mat/util/test_autodiff_test.cpp
Expand Up @@ -121,7 +121,7 @@ struct bad_std_vector_func {
}

std::vector<double> operator()(const std::vector<double>& t) {
std::vector o(t);
std::vector<double> o(t);
o[0] = o[0] + 1.0;
return o;
}
Expand Down

0 comments on commit 1b3c059

Please sign in to comment.