autodiff_cpp11.hpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. // Copyright Matthew Pulver 2018 - 2019.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // https://www.boost.org/LICENSE_1_0.txt)
  5. // Contributors:
  6. // * Kedar R. Bhat - C++11 compatibility.
  7. // Notes:
  8. // * Any changes to this file should always be downstream from autodiff.cpp.
  9. // C++17 is a higher-level language and is easier to maintain. For example, a number of functions which are
  10. // lucidly read in autodiff.cpp are forced to be split into multiple structs/functions in this file for
  11. // C++11.
  12. // * Use of typename RootType and SizeType is a hack to prevent Visual Studio 2015 from compiling functions
  13. // that are never called, that would otherwise produce compiler errors. Also forces functions to be inline.
  14. #ifndef BOOST_MATH_DIFFERENTIATION_AUTODIFF_HPP
  15. #error \
  16. "Do not #include this file directly. This should only be #included by autodiff.hpp for C++11 compatibility."
  17. #endif
  18. #include <type_traits>
  19. #include <boost/math/tools/mp.hpp>
  20. namespace mp = boost::math::tools::meta_programming;
  21. namespace boost {
  22. namespace math {
  23. namespace differentiation {
  24. inline namespace autodiff_v1 {
  25. namespace detail {
  26. template <typename RealType, size_t Order>
  27. fvar<RealType, Order>::fvar(root_type const& ca, bool const is_variable) {
  28. fvar_cpp11(is_fvar<RealType>{}, ca, is_variable);
  29. }
  30. template <typename RealType, size_t Order>
  31. template <typename RootType>
  32. void fvar<RealType, Order>::fvar_cpp11(std::true_type, RootType const& ca, bool const is_variable) {
  33. v.front() = RealType(ca, is_variable);
  34. if (0 < Order)
  35. std::fill(v.begin() + 1, v.end(), static_cast<RealType>(0));
  36. }
  37. template <typename RealType, size_t Order>
  38. template <typename RootType>
  39. void fvar<RealType, Order>::fvar_cpp11(std::false_type, RootType const& ca, bool const is_variable) {
  40. v.front() = ca;
  41. if (0 < Order) {
  42. v[1] = static_cast<root_type>(static_cast<int>(is_variable));
  43. if (1 < Order)
  44. std::fill(v.begin() + 2, v.end(), static_cast<RealType>(0));
  45. }
  46. }
  47. template <typename RealType, size_t Order>
  48. template <typename... Orders>
  49. get_type_at<RealType, sizeof...(Orders)> fvar<RealType, Order>::at_cpp11(std::true_type,
  50. size_t order,
  51. Orders...) const {
  52. return v.at(order);
  53. }
  54. template <typename RealType, size_t Order>
  55. template <typename... Orders>
  56. get_type_at<RealType, sizeof...(Orders)> fvar<RealType, Order>::at_cpp11(std::false_type,
  57. size_t order,
  58. Orders... orders) const {
  59. return v.at(order).at(orders...);
  60. }
  61. // Can throw "std::out_of_range: array::at: __n (which is 7) >= _Nm (which is 7)"
  62. template <typename RealType, size_t Order>
  63. template <typename... Orders>
  64. get_type_at<RealType, sizeof...(Orders)> fvar<RealType, Order>::at(size_t order, Orders... orders) const {
  65. return at_cpp11(std::integral_constant<bool, sizeof...(orders) == 0>{}, order, orders...);
  66. }
  67. template <typename T, typename... Ts>
  68. constexpr T product(Ts...) {
  69. return static_cast<T>(1);
  70. }
  71. template <typename T, typename... Ts>
  72. constexpr T product(T factor, Ts... factors) {
  73. return factor * product<T>(factors...);
  74. }
  75. // Can throw "std::out_of_range: array::at: __n (which is 7) >= _Nm (which is 7)"
  76. template <typename RealType, size_t Order>
  77. template <typename... Orders>
  78. get_type_at<fvar<RealType, Order>, sizeof...(Orders)> fvar<RealType, Order>::derivative(
  79. Orders... orders) const {
  80. static_assert(sizeof...(Orders) <= depth,
  81. "Number of parameters to derivative(...) cannot exceed fvar::depth.");
  82. return at(static_cast<size_t>(orders)...) *
  83. product(boost::math::factorial<root_type>(static_cast<unsigned>(orders))...);
  84. }
  85. template <typename RootType, typename Func>
  86. class Curry {
  87. Func const& f_;
  88. size_t const i_;
  89. public:
  90. template <typename SizeType> // typename SizeType to force inline constructor.
  91. Curry(Func const& f, SizeType i) : f_(f), i_(static_cast<std::size_t>(i)) {}
  92. template <typename... Indices>
  93. RootType operator()(Indices... indices) const {
  94. using unsigned_t = typename std::make_unsigned<typename std::common_type<Indices>::type...>::type;
  95. return f_(i_, static_cast<unsigned_t>(indices)...);
  96. }
  97. };
  98. template <typename RealType, size_t Order>
  99. template <typename Func, typename Fvar, typename... Fvars>
  100. promote<fvar<RealType, Order>, Fvar, Fvars...> fvar<RealType, Order>::apply_coefficients(
  101. size_t const order,
  102. Func const& f,
  103. Fvar const& cr,
  104. Fvars&&... fvars) const {
  105. fvar<RealType, Order> const epsilon = fvar<RealType, Order>(*this).set_root(0);
  106. size_t i = order < order_sum ? order : order_sum;
  107. using return_type = promote<fvar<RealType, Order>, Fvar, Fvars...>;
  108. return_type accumulator = cr.apply_coefficients(
  109. order - i, Curry<typename return_type::root_type, Func>(f, i), std::forward<Fvars>(fvars)...);
  110. while (i--)
  111. (accumulator *= epsilon) += cr.apply_coefficients(
  112. order - i, Curry<typename return_type::root_type, Func>(f, i), std::forward<Fvars>(fvars)...);
  113. return accumulator;
  114. }
  115. template <typename RealType, size_t Order>
  116. template <typename Func, typename Fvar, typename... Fvars>
  117. promote<fvar<RealType, Order>, Fvar, Fvars...> fvar<RealType, Order>::apply_coefficients_nonhorner(
  118. size_t const order,
  119. Func const& f,
  120. Fvar const& cr,
  121. Fvars&&... fvars) const {
  122. fvar<RealType, Order> const epsilon = fvar<RealType, Order>(*this).set_root(0);
  123. fvar<RealType, Order> epsilon_i = fvar<RealType, Order>(1); // epsilon to the power of i
  124. using return_type = promote<fvar<RealType, Order>, Fvar, Fvars...>;
  125. return_type accumulator = cr.apply_coefficients_nonhorner(
  126. order, Curry<typename return_type::root_type, Func>(f, 0), std::forward<Fvars>(fvars)...);
  127. size_t const i_max = order < order_sum ? order : order_sum;
  128. for (size_t i = 1; i <= i_max; ++i) {
  129. epsilon_i = epsilon_i.epsilon_multiply(i - 1, 0, epsilon, 1, 0);
  130. accumulator += epsilon_i.epsilon_multiply(
  131. i,
  132. 0,
  133. cr.apply_coefficients_nonhorner(
  134. order - i, Curry<typename return_type::root_type, Func>(f, i), std::forward<Fvars>(fvars)...),
  135. 0,
  136. 0);
  137. }
  138. return accumulator;
  139. }
  140. template <typename RealType, size_t Order>
  141. template <typename Func, typename Fvar, typename... Fvars>
  142. promote<fvar<RealType, Order>, Fvar, Fvars...> fvar<RealType, Order>::apply_derivatives(
  143. size_t const order,
  144. Func const& f,
  145. Fvar const& cr,
  146. Fvars&&... fvars) const {
  147. fvar<RealType, Order> const epsilon = fvar<RealType, Order>(*this).set_root(0);
  148. size_t i = order < order_sum ? order : order_sum;
  149. using return_type = promote<fvar<RealType, Order>, Fvar, Fvars...>;
  150. return_type accumulator =
  151. cr.apply_derivatives(
  152. order - i, Curry<typename return_type::root_type, Func>(f, i), std::forward<Fvars>(fvars)...) /
  153. factorial<root_type>(static_cast<unsigned>(i));
  154. while (i--)
  155. (accumulator *= epsilon) +=
  156. cr.apply_derivatives(
  157. order - i, Curry<typename return_type::root_type, Func>(f, i), std::forward<Fvars>(fvars)...) /
  158. factorial<root_type>(static_cast<unsigned>(i));
  159. return accumulator;
  160. }
  161. template <typename RealType, size_t Order>
  162. template <typename Func, typename Fvar, typename... Fvars>
  163. promote<fvar<RealType, Order>, Fvar, Fvars...> fvar<RealType, Order>::apply_derivatives_nonhorner(
  164. size_t const order,
  165. Func const& f,
  166. Fvar const& cr,
  167. Fvars&&... fvars) const {
  168. fvar<RealType, Order> const epsilon = fvar<RealType, Order>(*this).set_root(0);
  169. fvar<RealType, Order> epsilon_i = fvar<RealType, Order>(1); // epsilon to the power of i
  170. using return_type = promote<fvar<RealType, Order>, Fvar, Fvars...>;
  171. return_type accumulator = cr.apply_derivatives_nonhorner(
  172. order, Curry<typename return_type::root_type, Func>(f, 0), std::forward<Fvars>(fvars)...);
  173. size_t const i_max = order < order_sum ? order : order_sum;
  174. for (size_t i = 1; i <= i_max; ++i) {
  175. epsilon_i = epsilon_i.epsilon_multiply(i - 1, 0, epsilon, 1, 0);
  176. accumulator += epsilon_i.epsilon_multiply(
  177. i,
  178. 0,
  179. cr.apply_derivatives_nonhorner(
  180. order - i, Curry<typename return_type::root_type, Func>(f, i), std::forward<Fvars>(fvars)...) /
  181. factorial<root_type>(static_cast<unsigned>(i)),
  182. 0,
  183. 0);
  184. }
  185. return accumulator;
  186. }
  187. template <typename RealType, size_t Order>
  188. template <typename SizeType>
  189. fvar<RealType, Order> fvar<RealType, Order>::epsilon_multiply_cpp11(std::true_type,
  190. SizeType z0,
  191. size_t isum0,
  192. fvar<RealType, Order> const& cr,
  193. size_t z1,
  194. size_t isum1) const {
  195. size_t const m0 = order_sum + isum0 < Order + z0 ? Order + z0 - (order_sum + isum0) : 0;
  196. size_t const m1 = order_sum + isum1 < Order + z1 ? Order + z1 - (order_sum + isum1) : 0;
  197. size_t const i_max = m0 + m1 < Order ? Order - (m0 + m1) : 0;
  198. fvar<RealType, Order> retval = fvar<RealType, Order>();
  199. for (size_t i = 0, j = Order; i <= i_max; ++i, --j)
  200. retval.v[j] = epsilon_inner_product(z0, isum0, m0, cr, z1, isum1, m1, j);
  201. return retval;
  202. }
  203. template <typename RealType, size_t Order>
  204. template <typename SizeType>
  205. fvar<RealType, Order> fvar<RealType, Order>::epsilon_multiply_cpp11(std::false_type,
  206. SizeType z0,
  207. size_t isum0,
  208. fvar<RealType, Order> const& cr,
  209. size_t z1,
  210. size_t isum1) const {
  211. using ssize_t = typename std::make_signed<std::size_t>::type;
  212. RealType const zero(0);
  213. size_t const m0 = order_sum + isum0 < Order + z0 ? Order + z0 - (order_sum + isum0) : 0;
  214. size_t const m1 = order_sum + isum1 < Order + z1 ? Order + z1 - (order_sum + isum1) : 0;
  215. size_t const i_max = m0 + m1 < Order ? Order - (m0 + m1) : 0;
  216. fvar<RealType, Order> retval = fvar<RealType, Order>();
  217. for (size_t i = 0, j = Order; i <= i_max; ++i, --j)
  218. retval.v[j] = std::inner_product(
  219. v.cbegin() + ssize_t(m0), v.cend() - ssize_t(i + m1), cr.v.crbegin() + ssize_t(i + m0), zero);
  220. return retval;
  221. }
  222. template <typename RealType, size_t Order>
  223. fvar<RealType, Order> fvar<RealType, Order>::epsilon_multiply(size_t z0,
  224. size_t isum0,
  225. fvar<RealType, Order> const& cr,
  226. size_t z1,
  227. size_t isum1) const {
  228. return epsilon_multiply_cpp11(is_fvar<RealType>{}, z0, isum0, cr, z1, isum1);
  229. }
  230. template <typename RealType, size_t Order>
  231. template <typename SizeType>
  232. fvar<RealType, Order> fvar<RealType, Order>::epsilon_multiply_cpp11(std::true_type,
  233. SizeType z0,
  234. size_t isum0,
  235. root_type const& ca) const {
  236. fvar<RealType, Order> retval(*this);
  237. size_t const m0 = order_sum + isum0 < Order + z0 ? Order + z0 - (order_sum + isum0) : 0;
  238. for (size_t i = m0; i <= Order; ++i)
  239. retval.v[i] = retval.v[i].epsilon_multiply(z0, isum0 + i, ca);
  240. return retval;
  241. }
  242. template <typename RealType, size_t Order>
  243. template <typename SizeType>
  244. fvar<RealType, Order> fvar<RealType, Order>::epsilon_multiply_cpp11(std::false_type,
  245. SizeType z0,
  246. size_t isum0,
  247. root_type const& ca) const {
  248. fvar<RealType, Order> retval(*this);
  249. size_t const m0 = order_sum + isum0 < Order + z0 ? Order + z0 - (order_sum + isum0) : 0;
  250. for (size_t i = m0; i <= Order; ++i)
  251. if (retval.v[i] != static_cast<RealType>(0))
  252. retval.v[i] *= ca;
  253. return retval;
  254. }
  255. template <typename RealType, size_t Order>
  256. fvar<RealType, Order> fvar<RealType, Order>::epsilon_multiply(size_t z0,
  257. size_t isum0,
  258. root_type const& ca) const {
  259. return epsilon_multiply_cpp11(is_fvar<RealType>{}, z0, isum0, ca);
  260. }
  261. template <typename RealType, size_t Order>
  262. template <typename RootType>
  263. fvar<RealType, Order>& fvar<RealType, Order>::multiply_assign_by_root_type_cpp11(std::true_type,
  264. bool is_root,
  265. RootType const& ca) {
  266. auto itr = v.begin();
  267. itr->multiply_assign_by_root_type(is_root, ca);
  268. for (++itr; itr != v.end(); ++itr)
  269. itr->multiply_assign_by_root_type(false, ca);
  270. return *this;
  271. }
  272. template <typename RealType, size_t Order>
  273. template <typename RootType>
  274. fvar<RealType, Order>& fvar<RealType, Order>::multiply_assign_by_root_type_cpp11(std::false_type,
  275. bool is_root,
  276. RootType const& ca) {
  277. auto itr = v.begin();
  278. if (is_root || *itr != 0)
  279. *itr *= ca; // Skip multiplication of 0 by ca=inf to avoid nan, except when is_root.
  280. for (++itr; itr != v.end(); ++itr)
  281. if (*itr != 0)
  282. *itr *= ca;
  283. return *this;
  284. }
  285. template <typename RealType, size_t Order>
  286. fvar<RealType, Order>& fvar<RealType, Order>::multiply_assign_by_root_type(bool is_root,
  287. root_type const& ca) {
  288. return multiply_assign_by_root_type_cpp11(is_fvar<RealType>{}, is_root, ca);
  289. }
  290. template <typename RealType, size_t Order>
  291. template <typename RootType>
  292. fvar<RealType, Order>& fvar<RealType, Order>::negate_cpp11(std::true_type, RootType const&) {
  293. std::for_each(v.begin(), v.end(), [](RealType& r) { r.negate(); });
  294. return *this;
  295. }
  296. template <typename RealType, size_t Order>
  297. template <typename RootType>
  298. fvar<RealType, Order>& fvar<RealType, Order>::negate_cpp11(std::false_type, RootType const&) {
  299. std::for_each(v.begin(), v.end(), [](RealType& a) { a = -a; });
  300. return *this;
  301. }
  302. template <typename RealType, size_t Order>
  303. fvar<RealType, Order>& fvar<RealType, Order>::negate() {
  304. return negate_cpp11(is_fvar<RealType>{}, static_cast<root_type>(*this));
  305. }
  306. template <typename RealType, size_t Order>
  307. template <typename RootType>
  308. fvar<RealType, Order>& fvar<RealType, Order>::set_root_cpp11(std::true_type, RootType const& root) {
  309. v.front().set_root(root);
  310. return *this;
  311. }
  312. template <typename RealType, size_t Order>
  313. template <typename RootType>
  314. fvar<RealType, Order>& fvar<RealType, Order>::set_root_cpp11(std::false_type, RootType const& root) {
  315. v.front() = root;
  316. return *this;
  317. }
  318. template <typename RealType, size_t Order>
  319. fvar<RealType, Order>& fvar<RealType, Order>::set_root(root_type const& root) {
  320. return set_root_cpp11(is_fvar<RealType>{}, root);
  321. }
  322. template <typename RealType, size_t Order, size_t... Is>
  323. auto make_fvar_for_tuple(mp::index_sequence<Is...>, RealType const& ca)
  324. -> decltype(make_fvar<RealType, zero<Is>::value..., Order>(ca)) {
  325. return make_fvar<RealType, zero<Is>::value..., Order>(ca);
  326. }
  327. template <typename RealType, size_t... Orders, size_t... Is, typename... RealTypes>
  328. auto make_ftuple_impl(mp::index_sequence<Is...>, RealTypes const&... ca)
  329. -> decltype(std::make_tuple(make_fvar_for_tuple<RealType, Orders>(mp::make_index_sequence<Is>{},
  330. ca)...)) {
  331. return std::make_tuple(make_fvar_for_tuple<RealType, Orders>(mp::make_index_sequence<Is>{}, ca)...);
  332. }
  333. } // namespace detail
  334. template <typename RealType, size_t... Orders, typename... RealTypes>
  335. auto make_ftuple(RealTypes const&... ca)
  336. -> decltype(detail::make_ftuple_impl<RealType, Orders...>(mp::index_sequence_for<RealTypes...>{},
  337. ca...)) {
  338. static_assert(sizeof...(Orders) == sizeof...(RealTypes),
  339. "Number of Orders must match number of function parameters.");
  340. return detail::make_ftuple_impl<RealType, Orders...>(mp::index_sequence_for<RealTypes...>{}, ca...);
  341. }
  342. } // namespace autodiff_v1
  343. } // namespace differentiation
  344. } // namespace math
  345. } // namespace boost