File: test_poly_method.cpp

package info (click to toggle)
boost1.83 1.83.0-4.2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 545,456 kB
  • sloc: cpp: 3,857,086; xml: 125,552; ansic: 34,414; python: 25,887; asm: 5,276; sh: 4,799; ada: 1,681; makefile: 1,629; perl: 1,212; pascal: 1,139; sql: 810; yacc: 478; ruby: 102; lisp: 24; csh: 6
file content (371 lines) | stat: -rw-r--r-- 14,526 bytes parent folder | download | duplicates (13)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
//  Copyright John Maddock 2015.
//  Use, modification and distribution are subject to the
//  Boost Software License, Version 1.0. (See accompanying file
//  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#ifdef _MSC_VER
#  pragma warning (disable : 4224)
#endif

#include <boost/array.hpp>
#include <boost/lexical_cast.hpp>
#include "../../test/table_type.hpp"
#include "table_helper.hpp"
#include "performance.hpp"
#include <iostream>

#define evaluate_polynomial_c_imp evaluate_polynomial_c_imp_1
#undef BOOST_MATH_TOOLS_POLY_EVAL_20_HPP
#include <boost/math/tools/detail/polynomial_horner1_20.hpp>
#undef evaluate_polynomial_c_imp
#undef BOOST_MATH_TOOLS_POLY_EVAL_20_HPP
#define evaluate_polynomial_c_imp evaluate_polynomial_c_imp_2
#include <boost/math/tools/detail/polynomial_horner2_20.hpp>
#undef evaluate_polynomial_c_imp
#undef BOOST_MATH_TOOLS_POLY_EVAL_20_HPP
#define evaluate_polynomial_c_imp evaluate_polynomial_c_imp_3
#include <boost/math/tools/detail/polynomial_horner3_20.hpp>
#undef evaluate_polynomial_c_imp

#undef BOOST_MATH_TOOLS_POLY_RAT_20_HPP
#define evaluate_rational_c_imp evaluate_rational_c_imp_1
#include <boost/math/tools/detail/rational_horner1_20.hpp>
#undef evaluate_rational_c_imp
#undef BOOST_MATH_TOOLS_POLY_RAT_20_HPP
#define evaluate_rational_c_imp evaluate_rational_c_imp_2
#include <boost/math/tools/detail/rational_horner2_20.hpp>
#undef evaluate_rational_c_imp
#undef BOOST_MATH_TOOLS_RAT_EVAL_20_HPP
#define evaluate_rational_c_imp evaluate_rational_c_imp_3
#include <boost/math/tools/detail/rational_horner3_20.hpp>
#undef evaluate_rational_c_imp
#undef BOOST_MATH_TOOLS_POLY_RAT_20_HPP

static const double num[21] = {
   static_cast<double>(56906521.91347156388090791033559122686859L),
   static_cast<double>(103794043.1163445451906271053616070238554L),
   static_cast<double>(86363131.28813859145546927288977868422342L),
   static_cast<double>(43338889.32467613834773723740590533316085L),
   static_cast<double>(14605578.08768506808414169982791359218571L),
   static_cast<double>(3481712.15498064590882071018964774556468L),
   static_cast<double>(601859.6171681098786670226533699352302507L),
   static_cast<double>(75999.29304014542649875303443598909137092L),
   static_cast<double>(6955.999602515376140356310115515198987526L),
   static_cast<double>(449.9445569063168119446858607650988409623L),
   static_cast<double>(19.51992788247617482847860966235652136208L),
   static_cast<double>(0.5098416655656676188125178644804694509993L),
   static_cast<double>(0.006061842346248906525783753964555936883222L),
   0.0
};
static const double denom[20] = {
   static_cast<double>(0u),
   static_cast<double>(39916800u),
   static_cast<double>(120543840u),
   static_cast<double>(150917976u),
   static_cast<double>(105258076u),
   static_cast<double>(45995730u),
   static_cast<double>(13339535u),
   static_cast<double>(2637558u),
   static_cast<double>(357423u),
   static_cast<double>(32670u),
   static_cast<double>(1925u),
   static_cast<double>(66u),
   static_cast<double>(1u),
   0.0
};
static const std::uint32_t denom_int[20] = {
   static_cast<std::uint32_t>(0u),
   static_cast<std::uint32_t>(39916800u),
   static_cast<std::uint32_t>(120543840u),
   static_cast<std::uint32_t>(150917976u),
   static_cast<std::uint32_t>(105258076u),
   static_cast<std::uint32_t>(45995730u),
   static_cast<std::uint32_t>(13339535u),
   static_cast<std::uint32_t>(2637558u),
   static_cast<std::uint32_t>(357423u),
   static_cast<std::uint32_t>(32670u),
   static_cast<std::uint32_t>(1925u),
   static_cast<std::uint32_t>(66u),
   static_cast<std::uint32_t>(1u),
   0
};

std::string make_order_string(int n)
{
   std::string result = boost::lexical_cast<std::string>(n);
   if (result.size() < 2)
      result.insert(result.begin(), ' ');
   return result;
}

void test_poly_1(const std::integral_constant<int, 1>&)
{
}

template <int N>
void test_poly_1(const std::integral_constant<int, N>&)
{
   test_poly_1(std::integral_constant<int, N - 1>());

   double time = exec_timed_test([](const std::vector<double>& v) 
   {
      double result = 0;
      for (unsigned i = 0; i < 10; ++i)
         result += boost::math::tools::detail::evaluate_polynomial_c_imp_1(denom, v[0] + i, static_cast<std::integral_constant<int, N>*>(0));
      return result;
   });
   report_execution_time(time, std::string("Polynomial Method Comparison with ") + compiler_name() + std::string(" on ") + platform_name(), "Order " + make_order_string(N), "Method 1[br](Double Coefficients)");

   time = exec_timed_test([](const std::vector<double>& v)
   {
      double result = 0;
      for (unsigned i = 0; i < 10; ++i)
         result += boost::math::tools::detail::evaluate_polynomial_c_imp_1(denom_int, v[0] + i, static_cast<std::integral_constant<int, N>*>(0));
      return result;
   });
   report_execution_time(time, std::string("Polynomial Method Comparison with ") + compiler_name() + std::string(" on ") + platform_name(), "Order " + make_order_string(N), "Method 1[br](Integer Coefficients)");
}


void test_poly_2(const std::integral_constant<int, 1>&)
{
}

template <int N>
void test_poly_2(const std::integral_constant<int, N>&)
{
   test_poly_2(std::integral_constant<int, N - 1>());

   double time = exec_timed_test([](const std::vector<double>& v) 
   {  
      double result = 0; 
      for (unsigned i = 0; i < 10; ++i)
         result += boost::math::tools::detail::evaluate_polynomial_c_imp_2(denom, v[0] + i, static_cast<std::integral_constant<int, N>*>(0));
      return result;
   });
   report_execution_time(time, std::string("Polynomial Method Comparison with ") + compiler_name() + std::string(" on ") + platform_name(), "Order " + make_order_string(N), "Method 2[br](Double Coefficients)");

   time = exec_timed_test([](const std::vector<double>& v) 
   {  
      double result = 0; 
      for (unsigned i = 0; i < 10; ++i)
         result += boost::math::tools::detail::evaluate_polynomial_c_imp_2(denom_int, v[0] + i, static_cast<std::integral_constant<int, N>*>(0));
      return result;
   });
   report_execution_time(time, std::string("Polynomial Method Comparison with ") + compiler_name() + std::string(" on ") + platform_name(), "Order " + make_order_string(N), "Method 2[br](Integer Coefficients)");
}

void test_poly_3(const std::integral_constant<int, 1>&)
{
}

template <int N>
void test_poly_3(const std::integral_constant<int, N>&)
{
   test_poly_3(std::integral_constant<int, N - 1>());

   double time = exec_timed_test([](const std::vector<double>& v) {  double result = 0;
   for (unsigned i = 0; i < 10; ++i)
      result += boost::math::tools::detail::evaluate_polynomial_c_imp_3(denom, v[0] + i, static_cast<std::integral_constant<int, N>*>(0));
   return result;
   });
   report_execution_time(time, std::string("Polynomial Method Comparison with ") + compiler_name() + std::string(" on ") + platform_name(), "Order " + make_order_string(N), "Method 3[br](Double Coefficients)");

   time = exec_timed_test([](const std::vector<double>& v) {  double result = 0;
   for (unsigned i = 0; i < 10; ++i)
      result += boost::math::tools::detail::evaluate_polynomial_c_imp_3(denom_int, v[0] + i, static_cast<std::integral_constant<int, N>*>(0));
   return result;
   });
   report_execution_time(time, std::string("Polynomial Method Comparison with ") + compiler_name() + std::string(" on ") + platform_name(), "Order " + make_order_string(N), "Method 3[br](Integer Coefficients)");
}

template <class T, class U>
U evaluate_polynomial_0(const T* poly, U const& z, std::size_t count)
{
   U sum = static_cast<U>(poly[count - 1]);
   for (int i = static_cast<int>(count) - 2; i >= 0; --i)
   {
      sum *= z;
      sum += static_cast<U>(poly[i]);
   }
   return sum;
}

void test_rat_1(const std::integral_constant<int, 1>&)
{
}

template <int N>
void test_rat_1(const std::integral_constant<int, N>&)
{
   test_rat_1(std::integral_constant<int, N - 1>());

   double time = exec_timed_test([](const std::vector<double>& v)
   {
      double result = 0;
      for (unsigned i = 0; i < 10; ++i)
         result += boost::math::tools::detail::evaluate_rational_c_imp_1(num, denom, v[0] + i, static_cast<std::integral_constant<int, N>*>(0));
      return result;
   });
   report_execution_time(time, std::string("Rational Method Comparison with ") + compiler_name() + std::string(" on ") + platform_name(), "Order " + make_order_string(N), "Method 1[br](Double Coefficients)");

   time = exec_timed_test([](const std::vector<double>& v)
   {
      double result = 0;
      for (unsigned i = 0; i < 10; ++i)
         result += boost::math::tools::detail::evaluate_rational_c_imp_1(num, denom_int, v[0] + i, static_cast<std::integral_constant<int, N>*>(0));
      return result;
   });
   report_execution_time(time, std::string("Rational Method Comparison with ") + compiler_name() + std::string(" on ") + platform_name(), "Order " + make_order_string(N), "Method 1[br](Integer Coefficients)");
}

void test_rat_2(const std::integral_constant<int, 1>&)
{
}

template <int N>
void test_rat_2(const std::integral_constant<int, N>&)
{
   test_rat_2(std::integral_constant<int, N - 1>());

   double time = exec_timed_test([](const std::vector<double>& v)
   {
      double result = 0;
      for (unsigned i = 0; i < 10; ++i)
         result += boost::math::tools::detail::evaluate_rational_c_imp_2(num, denom, v[0] + i, static_cast<std::integral_constant<int, N>*>(0));
      return result;
   });
   report_execution_time(time, std::string("Rational Method Comparison with ") + compiler_name() + std::string(" on ") + platform_name(), "Order " + make_order_string(N), "Method 2[br](Double Coefficients)");

   time = exec_timed_test([](const std::vector<double>& v)
   {
      double result = 0;
      for (unsigned i = 0; i < 10; ++i)
         result += boost::math::tools::detail::evaluate_rational_c_imp_2(num, denom_int, v[0] + i, static_cast<std::integral_constant<int, N>*>(0));
      return result;
   });
   report_execution_time(time, std::string("Rational Method Comparison with ") + compiler_name() + std::string(" on ") + platform_name(), "Order " + make_order_string(N), "Method 2[br](Integer Coefficients)");
}

void test_rat_3(const std::integral_constant<int, 1>&)
{
}

template <int N>
void test_rat_3(const std::integral_constant<int, N>&)
{
   test_rat_3(std::integral_constant<int, N - 1>());

   double time = exec_timed_test([](const std::vector<double>& v)
   {
      double result = 0;
      for (unsigned i = 0; i < 10; ++i)
         result += boost::math::tools::detail::evaluate_rational_c_imp_3(num, denom, v[0] + i, static_cast<std::integral_constant<int, N>*>(0));
      return result;
   });
   report_execution_time(time, std::string("Rational Method Comparison with ") + compiler_name() + std::string(" on ") + platform_name(), "Order " + make_order_string(N), "Method 3[br](Double Coefficients)");

   time = exec_timed_test([](const std::vector<double>& v)
   {
      double result = 0;
      for (unsigned i = 0; i < 10; ++i)
         result += boost::math::tools::detail::evaluate_rational_c_imp_3(num, denom_int, v[0] + i, static_cast<std::integral_constant<int, N>*>(0));
      return result;
   });
   report_execution_time(time, std::string("Rational Method Comparison with ") + compiler_name() + std::string(" on ") + platform_name(), "Order " + make_order_string(N), "Method 3[br](Integer Coefficients)");
}

template <class T, class U, class V>
V evaluate_rational_0(const T* num, const U* denom, const V& z_, std::size_t count)
{
   V z(z_);
   V s1, s2;
   if (z <= 1)
   {
      s1 = static_cast<V>(num[count - 1]);
      s2 = static_cast<V>(denom[count - 1]);
      for (int i = (int)count - 2; i >= 0; --i)
      {
         s1 *= z;
         s2 *= z;
         s1 += num[i];
         s2 += denom[i];
      }
   }
   else
   {
      z = 1 / z;
      s1 = static_cast<V>(num[0]);
      s2 = static_cast<V>(denom[0]);
      for (unsigned i = 1; i < count; ++i)
      {
         s1 *= z;
         s2 *= z;
         s1 += num[i];
         s2 += denom[i];
      }
   }
   return s1 / s2;
}


int main()
{
   double val = 0.001;
   while (val < 1)
   {
      std::vector<double> v;
      v.push_back(val);
      data.push_back(v);
      val *= 1.1;
   }

   for (unsigned i = 3; i <= 20; ++i)
   {
      double time = exec_timed_test([&](const std::vector<double>& v) {  
         double result = 0;
         for (unsigned j = 0; j < 10; ++j)
            result += evaluate_polynomial_0(denom, v[0] + j, i);
         return result;
      });
      report_execution_time(time, std::string("Polynomial Method Comparison with ") + compiler_name() + std::string(" on ") + platform_name(), "Order " + make_order_string(i), "Method 0[br](Double Coefficients)");

      time = exec_timed_test([&](const std::vector<double>& v) {
         double result = 0;
         for (unsigned j = 0; j < 10; ++j)
            result += evaluate_polynomial_0(denom_int, v[0] + j, i);
         return result;
      });
      report_execution_time(time, std::string("Polynomial Method Comparison with ") + compiler_name() + std::string(" on ") + platform_name(), "Order " + make_order_string(i), "Method 0[br](Integer Coefficients)");
   }

   test_poly_1(std::integral_constant<int, 20>());
   test_poly_2(std::integral_constant<int, 20>());
   test_poly_3(std::integral_constant<int, 20>());

   for (unsigned i = 3; i <= 20; ++i)
   {
      double time = exec_timed_test([&](const std::vector<double>& v) {
         double result = 0;
         for (unsigned j = 0; j < 10; ++j)
            result += evaluate_rational_0(num, denom, v[0] + j, i);
         return result;
      });
      report_execution_time(time, std::string("Rational Method Comparison with ") + compiler_name() + std::string(" on ") + platform_name(), "Order " + make_order_string(i), "Method 0[br](Double Coefficients)");

      time = exec_timed_test([&](const std::vector<double>& v) {
         double result = 0;
         for (unsigned j = 0; j < 10; ++j)
            result += evaluate_rational_0(num, denom_int, v[0] + j, i);
         return result;
      });
      report_execution_time(time, std::string("Rational Method Comparison with ") + compiler_name() + std::string(" on ") + platform_name(), "Order " + make_order_string(i), "Method 0[br](Integer Coefficients)");
   }

   test_rat_1(std::integral_constant<int, 20>());
   test_rat_2(std::integral_constant<int, 20>());
   test_rat_3(std::integral_constant<int, 20>());

   return 0;
}