functor.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. // (C) Copyright John Maddock 2007.
  2. // Use, modification and distribution are subject to the
  3. // Boost Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #include <boost/math/special_functions/trunc.hpp>
  6. #ifndef BOOST_MATH_TEST_FUNCTOR_HPP
  7. #define BOOST_MATH_TEST_FUNCTOR_HPP
  8. template <class Real>
  9. struct extract_result_type
  10. {
  11. extract_result_type(unsigned i) : m_location(i){}
  12. template <class S>
  13. Real operator()(const S& row)
  14. {
  15. return row[m_location];
  16. }
  17. private:
  18. unsigned m_location;
  19. };
  20. template <class Real>
  21. inline extract_result_type<Real> extract_result(unsigned i)
  22. {
  23. return extract_result_type<Real>(i);
  24. }
  25. template <class Real, class F>
  26. struct row_binder1
  27. {
  28. row_binder1(F _f, unsigned i) : f(_f), m_i(i) {}
  29. template <class S>
  30. Real operator()(const S& row)
  31. {
  32. return f(row[m_i]);
  33. }
  34. private:
  35. F f;
  36. unsigned m_i;
  37. };
  38. template<class Real, class F>
  39. inline row_binder1<Real, F> bind_func(F f, unsigned i)
  40. {
  41. return row_binder1<Real, F>(f, i);
  42. }
  43. template <class Real, class F>
  44. struct row_binder2
  45. {
  46. row_binder2(F _f, unsigned i, unsigned j) : f(_f), m_i(i), m_j(j) {}
  47. template <class S>
  48. Real operator()(const S& row)
  49. {
  50. return f(row[m_i], row[m_j]);
  51. }
  52. private:
  53. F f;
  54. unsigned m_i, m_j;
  55. };
  56. template<class Real, class F>
  57. inline row_binder2<Real, F> bind_func(F f, unsigned i, unsigned j)
  58. {
  59. return row_binder2<Real, F>(f, i, j);
  60. }
  61. template <class Real, class F>
  62. struct row_binder3
  63. {
  64. row_binder3(F _f, unsigned i, unsigned j, unsigned k) : f(_f), m_i(i), m_j(j), m_k(k) {}
  65. template <class S>
  66. Real operator()(const S& row)
  67. {
  68. return f(row[m_i], row[m_j], row[m_k]);
  69. }
  70. private:
  71. F f;
  72. unsigned m_i, m_j, m_k;
  73. };
  74. template<class Real, class F>
  75. inline row_binder3<Real, F> bind_func(F f, unsigned i, unsigned j, unsigned k)
  76. {
  77. return row_binder3<Real, F>(f, i, j, k);
  78. }
  79. template <class Real, class F>
  80. struct row_binder4
  81. {
  82. row_binder4(F _f, unsigned i, unsigned j, unsigned k, unsigned l) : f(_f), m_i(i), m_j(j), m_k(k), m_l(l) {}
  83. template <class S>
  84. Real operator()(const S& row)
  85. {
  86. return f(row[m_i], row[m_j], row[m_k], row[m_l]);
  87. }
  88. private:
  89. F f;
  90. unsigned m_i, m_j, m_k, m_l;
  91. };
  92. template<class Real, class F>
  93. inline row_binder4<Real, F> bind_func(F f, unsigned i, unsigned j, unsigned k, unsigned l)
  94. {
  95. return row_binder4<Real, F>(f, i, j, k, l);
  96. }
  97. template <class Real, class F>
  98. struct row_binder2_i1
  99. {
  100. row_binder2_i1(F _f, unsigned i, unsigned j) : f(_f), m_i(i), m_j(j) {}
  101. template <class S>
  102. Real operator()(const S& row)
  103. {
  104. return f(boost::math::itrunc(Real(row[m_i])), row[m_j]);
  105. }
  106. private:
  107. F f;
  108. unsigned m_i, m_j;
  109. };
  110. template<class Real, class F>
  111. inline row_binder2_i1<Real, F> bind_func_int1(F f, unsigned i, unsigned j)
  112. {
  113. return row_binder2_i1<Real, F>(f, i, j);
  114. }
  115. template <class Real, class F>
  116. struct row_binder3_i2
  117. {
  118. row_binder3_i2(F _f, unsigned i, unsigned j, unsigned k) : f(_f), m_i(i), m_j(j), m_k(k) {}
  119. template <class S>
  120. Real operator()(const S& row)
  121. {
  122. return f(
  123. boost::math::itrunc(Real(row[m_i])),
  124. boost::math::itrunc(Real(row[m_j])),
  125. row[m_k]);
  126. }
  127. private:
  128. F f;
  129. unsigned m_i, m_j, m_k;
  130. };
  131. template<class Real, class F>
  132. inline row_binder3_i2<Real, F> bind_func_int2(F f, unsigned i, unsigned j, unsigned k)
  133. {
  134. return row_binder3_i2<Real, F>(f, i, j, k);
  135. }
  136. template <class Real, class F>
  137. struct row_binder4_i2
  138. {
  139. row_binder4_i2(F _f, unsigned i, unsigned j, unsigned k, unsigned l) : f(_f), m_i(i), m_j(j), m_k(k), m_l(l) {}
  140. template <class S>
  141. Real operator()(const S& row)
  142. {
  143. return f(
  144. boost::math::itrunc(Real(row[m_i])),
  145. boost::math::itrunc(Real(row[m_j])),
  146. row[m_k],
  147. row[m_l]);
  148. }
  149. private:
  150. F f;
  151. unsigned m_i, m_j, m_k, m_l;
  152. };
  153. template<class Real, class F>
  154. inline row_binder4_i2<Real, F> bind_func_int2(F f, unsigned i, unsigned j, unsigned k, unsigned l)
  155. {
  156. return row_binder4_i2<Real, F>(f, i, j, k, l);
  157. }
  158. template <class Real, class F>
  159. struct negate_type
  160. {
  161. negate_type(F f) : m_f(f){}
  162. template <class S>
  163. Real operator()(const S& row)
  164. {
  165. return -Real(m_f(row));
  166. }
  167. private:
  168. F m_f;
  169. };
  170. template <class Real, class F>
  171. inline negate_type<Real, F> negate(F f)
  172. {
  173. return negate_type<Real, F>(f);
  174. }
  175. #endif