]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/math/test/test_polynomial.cpp
bump version to 18.2.4-pve3
[ceph.git] / ceph / src / boost / libs / math / test / test_polynomial.cpp
1 // (C) Copyright Jeremy Murphy 2015.
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
6 #include <boost/config.hpp>
7 #define BOOST_TEST_MAIN
8 #include <boost/array.hpp>
9 #include <boost/math/tools/polynomial.hpp>
10 #include <boost/integer/common_factor_rt.hpp>
11 #include <boost/mpl/list.hpp>
12 #include <boost/mpl/joint_view.hpp>
13 #include <boost/test/unit_test.hpp>
14 #include <boost/multiprecision/cpp_int.hpp>
15 #include <boost/multiprecision/cpp_bin_float.hpp>
16 #include <boost/multiprecision/cpp_dec_float.hpp>
17 #include <utility>
18 #include <array>
19 #include <list>
20
21 #if !defined(TEST1) && !defined(TEST2) && !defined(TEST3)
22 # define TEST1
23 # define TEST2
24 # define TEST3
25 #endif
26
27 using namespace boost::math;
28 using boost::integer::gcd;
29 using namespace boost::math::tools;
30 using namespace std;
31 using boost::integer::gcd_detail::Euclid_gcd;
32 using boost::math::tools::subresultant_gcd;
33
34 template <typename T>
35 struct answer
36 {
37 answer(std::pair< polynomial<T>, polynomial<T> > const &x) :
38 quotient(x.first), remainder(x.second) {}
39
40 polynomial<T> quotient;
41 polynomial<T> remainder;
42 };
43
44 std::array<double, 4> const d3a = {{10, -6, -4, 3}};
45 std::array<double, 4> const d3b = {{-7, 5, 6, 1}};
46
47 std::array<double, 2> const d1a = {{-2, 1}};
48 std::array<double, 1> const d0a = {{6}};
49 std::array<double, 2> const d0a1 = {{0, 6}};
50 std::array<double, 6> const d0a5 = {{0, 0, 0, 0, 0, 6}};
51
52
53 std::array<int, 9> const d8 = {{-5, 2, 8, -3, -3, 0, 1, 0, 1}};
54 std::array<int, 9> const d8b = {{0, 2, 8, -3, -3, 0, 1, 0, 1}};
55
56
57
58 BOOST_AUTO_TEST_CASE(trivial)
59 {
60 /* We have one empty test case here, so that there is always something for Boost.Test to do even if the tests below are #if'ed out */
61 }
62
63
64 #ifdef TEST1
65
66 std::array<double, 4> const d3c = {{10.0/3.0, -2.0, -4.0/3.0, 1.0}};
67 std::array<double, 3> const d2a = {{-2, 2, 3}};
68 std::array<double, 3> const d2b = {{-7, 5, 6}};
69 std::array<double, 3> const d2c = {{31, -21, -22}};
70 std::array<double, 1> const d0b = {{3}};
71 std::array<int, 7> const d6 = {{21, -9, -4, 0, 5, 0, 3}};
72 std::array<int, 3> const d2 = {{-6, 0, 9}};
73 std::array<int, 6> const d5 = {{-9, 0, 3, 0, -15}};
74
75
76 BOOST_AUTO_TEST_CASE( test_construction )
77 {
78 polynomial<double> const a(d3a.begin(), d3a.end());
79 polynomial<double> const b(d3a.begin(), 3);
80 BOOST_CHECK_EQUAL(a, b);
81 }
82
83 #ifdef BOOST_MATH_HAS_IS_CONST_ITERABLE
84
85 #include <list>
86 #include <array>
87
88 BOOST_AUTO_TEST_CASE(test_range_construction)
89 {
90 std::list<double> l{ 1, 2, 3, 4 };
91 std::array<double, 4> a{ 3, 4, 5, 6 };
92 polynomial<double> p1{ 1, 2, 3, 4 };
93 polynomial<double> p2{ 3, 4, 5, 6 };
94
95 polynomial<double> p3(l);
96 polynomial<double> p4(a);
97
98 BOOST_CHECK_EQUAL(p1, p3);
99 BOOST_CHECK_EQUAL(p2, p4);
100 }
101 #endif
102
103 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) && !BOOST_WORKAROUND(BOOST_GCC_VERSION, < 40500)
104 BOOST_AUTO_TEST_CASE( test_initializer_list_construction )
105 {
106 polynomial<double> a(begin(d3a), end(d3a));
107 polynomial<double> b = {10, -6, -4, 3};
108 polynomial<double> c{10, -6, -4, 3};
109 polynomial<double> d{10, -6, -4, 3, 0, 0};
110 BOOST_CHECK_EQUAL(a, b);
111 BOOST_CHECK_EQUAL(b, c);
112 BOOST_CHECK_EQUAL(d.degree(), 3u);
113 }
114
115 BOOST_AUTO_TEST_CASE( test_initializer_list_assignment )
116 {
117 polynomial<double> a(begin(d3a), end(d3a));
118 polynomial<double> b;
119 b = {10, -6, -4, 3, 0, 0};
120 BOOST_CHECK_EQUAL(b.degree(), 3u);
121 BOOST_CHECK_EQUAL(a, b);
122 }
123 #endif
124
125
126 BOOST_AUTO_TEST_CASE( test_degree )
127 {
128 polynomial<double> const zero;
129 polynomial<double> const a(d3a.begin(), d3a.end());
130 BOOST_CHECK_THROW(zero.degree(), std::logic_error);
131 BOOST_CHECK_EQUAL(a.degree(), 3u);
132 }
133
134
135 BOOST_AUTO_TEST_CASE( test_division_over_field )
136 {
137 polynomial<double> const a(d3a.begin(), d3a.end());
138 polynomial<double> const b(d1a.begin(), d1a.end());
139 polynomial<double> const q(d2a.begin(), d2a.end());
140 polynomial<double> const r(d0a.begin(), d0a.end());
141 polynomial<double> const c(d3b.begin(), d3b.end());
142 polynomial<double> const d(d2b.begin(), d2b.end());
143 polynomial<double> const e(d2c.begin(), d2c.end());
144 polynomial<double> const f(d0b.begin(), d0b.end());
145 polynomial<double> const g(d3c.begin(), d3c.end());
146 polynomial<double> const zero;
147 polynomial<double> const one(1.0);
148
149 answer<double> result = quotient_remainder(a, b);
150 BOOST_CHECK_EQUAL(result.quotient, q);
151 BOOST_CHECK_EQUAL(result.remainder, r);
152 BOOST_CHECK_EQUAL(a, q * b + r); // Sanity check.
153
154 result = quotient_remainder(a, c);
155 BOOST_CHECK_EQUAL(result.quotient, f);
156 BOOST_CHECK_EQUAL(result.remainder, e);
157 BOOST_CHECK_EQUAL(a, f * c + e); // Sanity check.
158
159 result = quotient_remainder(a, f);
160 BOOST_CHECK_EQUAL(result.quotient, g);
161 BOOST_CHECK_EQUAL(result.remainder, zero);
162 BOOST_CHECK_EQUAL(a, g * f + zero); // Sanity check.
163 // Check that division by a regular number gives the same result.
164 BOOST_CHECK_EQUAL(a / 3.0, g);
165 BOOST_CHECK_EQUAL(a % 3.0, zero);
166
167 // Sanity checks.
168 BOOST_CHECK_EQUAL(a / a, one);
169 BOOST_CHECK_EQUAL(a % a, zero);
170 // BOOST_CHECK_EQUAL(zero / zero, zero); // TODO
171 }
172
173 BOOST_AUTO_TEST_CASE( test_division_over_ufd )
174 {
175 polynomial<int> const zero;
176 polynomial<int> const one(1);
177 polynomial<int> const aa(d8.begin(), d8.end());
178 polynomial<int> const bb(d6.begin(), d6.end());
179 polynomial<int> const q(d2.begin(), d2.end());
180 polynomial<int> const r(d5.begin(), d5.end());
181
182 answer<int> result = quotient_remainder(aa, bb);
183 BOOST_CHECK_EQUAL(result.quotient, q);
184 BOOST_CHECK_EQUAL(result.remainder, r);
185
186 // Sanity checks.
187 BOOST_CHECK_EQUAL(aa / aa, one);
188 BOOST_CHECK_EQUAL(aa % aa, zero);
189 }
190
191 #endif
192
193 template <typename T>
194 struct FM2GP_Ex_8_3__1
195 {
196 polynomial<T> x;
197 polynomial<T> y;
198 polynomial<T> z;
199
200 FM2GP_Ex_8_3__1()
201 {
202 std::array<T, 5> const x_data = {{105, 278, -88, -56, 16}};
203 std::array<T, 5> const y_data = {{70, 232, -44, -64, 16}};
204 std::array<T, 3> const z_data = {{35, -24, 4}};
205 x = polynomial<T>(x_data.begin(), x_data.end());
206 y = polynomial<T>(y_data.begin(), y_data.end());
207 z = polynomial<T>(z_data.begin(), z_data.end());
208 }
209 };
210
211 template <typename T>
212 struct FM2GP_Ex_8_3__2
213 {
214 polynomial<T> x;
215 polynomial<T> y;
216 polynomial<T> z;
217
218 FM2GP_Ex_8_3__2()
219 {
220 std::array<T, 5> const x_data = {{1, -6, -8, 6, 7}};
221 std::array<T, 5> const y_data = {{1, -5, -2, 15, 11}};
222 std::array<T, 3> const z_data = {{1, 2, 1}};
223 x = polynomial<T>(x_data.begin(), x_data.end());
224 y = polynomial<T>(y_data.begin(), y_data.end());
225 z = polynomial<T>(z_data.begin(), z_data.end());
226 }
227 };
228
229
230 template <typename T>
231 struct FM2GP_mixed
232 {
233 polynomial<T> x;
234 polynomial<T> y;
235 polynomial<T> z;
236
237 FM2GP_mixed()
238 {
239 std::array<T, 4> const x_data = {{-2.2, -3.3, 0, 1}};
240 std::array<T, 3> const y_data = {{-4.4, 0, 1}};
241 std::array<T, 2> const z_data= {{-2, 1}};
242 x = polynomial<T>(x_data.begin(), x_data.end());
243 y = polynomial<T>(y_data.begin(), y_data.end());
244 z = polynomial<T>(z_data.begin(), z_data.end());
245 }
246 };
247
248
249 template <typename T>
250 struct FM2GP_trivial
251 {
252 polynomial<T> x;
253 polynomial<T> y;
254 polynomial<T> z;
255
256 FM2GP_trivial()
257 {
258 std::array<T, 4> const x_data = {{-2, -3, 0, 1}};
259 std::array<T, 3> const y_data = {{-4, 0, 1}};
260 std::array<T, 2> const z_data= {{-2, 1}};
261 x = polynomial<T>(x_data.begin(), x_data.end());
262 y = polynomial<T>(y_data.begin(), y_data.end());
263 z = polynomial<T>(z_data.begin(), z_data.end());
264 }
265 };
266
267 // Sanity checks to make sure I didn't break it.
268 #ifdef TEST1
269 typedef boost::mpl::list<signed char, short, int, long> integral_test_types;
270 typedef boost::mpl::list<int, long> large_integral_test_types;
271 typedef boost::mpl::list<> mp_integral_test_types;
272 #elif defined(TEST2)
273 typedef boost::mpl::list<
274 #if !BOOST_WORKAROUND(BOOST_MSVC, <= 1500)
275 boost::multiprecision::cpp_int
276 #endif
277 > integral_test_types;
278 typedef integral_test_types large_integral_test_types;
279 typedef large_integral_test_types mp_integral_test_types;
280 #elif defined(TEST3)
281 typedef boost::mpl::list<> large_integral_test_types;
282 typedef boost::mpl::list<> integral_test_types;
283 typedef large_integral_test_types mp_integral_test_types;
284 #endif
285
286 #ifdef TEST1
287 typedef boost::mpl::list<double, long double> non_integral_test_types;
288 #elif defined(TEST2)
289 typedef boost::mpl::list<
290 #if !BOOST_WORKAROUND(BOOST_MSVC, <= 1500)
291 boost::multiprecision::cpp_rational
292 #endif
293 > non_integral_test_types;
294 #elif defined(TEST3)
295 typedef boost::mpl::list<
296 #if !BOOST_WORKAROUND(BOOST_MSVC, <= 1500)
297 boost::multiprecision::cpp_bin_float_single, boost::multiprecision::cpp_dec_float_50
298 #endif
299 > non_integral_test_types;
300 #endif
301
302 typedef boost::mpl::joint_view<integral_test_types, non_integral_test_types> all_test_types;
303
304
305 template <typename T>
306 void normalize(polynomial<T> &p)
307 {
308 if (leading_coefficient(p) < T(0))
309 std::transform(p.data().begin(), p.data().end(), p.data().begin(), std::negate<T>());
310 }
311
312 /**
313 * Note that we do not expect 'pure' gcd algorithms to normalize the result.
314 * However, the usual public interface function gcd() will do that.
315 */
316
317 BOOST_AUTO_TEST_SUITE(test_subresultant_gcd)
318
319 // This test is just to show that gcd<polynomial<T>>(u, v) is defined (and works) when T is integral and multiprecision.
320 BOOST_FIXTURE_TEST_CASE_TEMPLATE( gcd_interface, T, mp_integral_test_types, FM2GP_Ex_8_3__1<T> )
321 {
322 typedef FM2GP_Ex_8_3__1<T> fixture_type;
323 polynomial<T> w;
324 w = gcd(fixture_type::x, fixture_type::y);
325 normalize(w);
326 BOOST_CHECK_EQUAL(w, fixture_type::z);
327 w = gcd(fixture_type::y, fixture_type::x);
328 normalize(w);
329 BOOST_CHECK_EQUAL(w, fixture_type::z);
330 }
331
332 // This test is just to show that gcd<polynomial<T>>(u, v) is defined (and works) when T is floating point.
333 BOOST_FIXTURE_TEST_CASE_TEMPLATE( gcd_float_interface, T, non_integral_test_types, FM2GP_Ex_8_3__1<T> )
334 {
335 typedef FM2GP_Ex_8_3__1<T> fixture_type;
336 polynomial<T> w;
337 w = gcd(fixture_type::x, fixture_type::y);
338 normalize(w);
339 BOOST_CHECK_EQUAL(w, fixture_type::z);
340 w = gcd(fixture_type::y, fixture_type::x);
341 normalize(w);
342 BOOST_CHECK_EQUAL(w, fixture_type::z);
343 }
344
345 // The following tests call subresultant_gcd explicitly to remove any ambiguity
346 // and to permit testing on single-precision integral types.
347 BOOST_FIXTURE_TEST_CASE_TEMPLATE( Ex_8_3__1, T, large_integral_test_types, FM2GP_Ex_8_3__1<T> )
348 {
349 typedef FM2GP_Ex_8_3__1<T> fixture_type;
350 polynomial<T> w;
351 w = subresultant_gcd(fixture_type::x, fixture_type::y);
352 normalize(w);
353 BOOST_CHECK_EQUAL(w, fixture_type::z);
354 w = subresultant_gcd(fixture_type::y, fixture_type::x);
355 normalize(w);
356 BOOST_CHECK_EQUAL(w, fixture_type::z);
357 }
358
359 BOOST_FIXTURE_TEST_CASE_TEMPLATE( Ex_8_3__2, T, large_integral_test_types, FM2GP_Ex_8_3__2<T> )
360 {
361 typedef FM2GP_Ex_8_3__2<T> fixture_type;
362 polynomial<T> w;
363 w = subresultant_gcd(fixture_type::x, fixture_type::y);
364 normalize(w);
365 BOOST_CHECK_EQUAL(w, fixture_type::z);
366 w = subresultant_gcd(fixture_type::y, fixture_type::x);
367 normalize(w);
368 BOOST_CHECK_EQUAL(w, fixture_type::z);
369 }
370
371 BOOST_FIXTURE_TEST_CASE_TEMPLATE( trivial_int, T, large_integral_test_types, FM2GP_trivial<T> )
372 {
373 typedef FM2GP_trivial<T> fixture_type;
374 polynomial<T> w;
375 w = subresultant_gcd(fixture_type::x, fixture_type::y);
376 normalize(w);
377 BOOST_CHECK_EQUAL(w, fixture_type::z);
378 w = subresultant_gcd(fixture_type::y, fixture_type::x);
379 normalize(w);
380 BOOST_CHECK_EQUAL(w, fixture_type::z);
381 }
382
383 BOOST_AUTO_TEST_SUITE_END()
384
385
386 BOOST_AUTO_TEST_CASE_TEMPLATE( test_addition, T, all_test_types )
387 {
388 polynomial<T> const a(d3a.begin(), d3a.end());
389 polynomial<T> const b(d1a.begin(), d1a.end());
390 polynomial<T> const zero;
391
392 polynomial<T> result = a + b; // different degree
393 std::array<T, 4> tmp = {{8, -5, -4, 3}};
394 polynomial<T> expected(tmp.begin(), tmp.end());
395 BOOST_CHECK_EQUAL(result, expected);
396 BOOST_CHECK_EQUAL(a + zero, a);
397 BOOST_CHECK_EQUAL(a + b, b + a);
398 }
399
400 BOOST_AUTO_TEST_CASE_TEMPLATE( test_subtraction, T, all_test_types )
401 {
402 polynomial<T> const a(d3a.begin(), d3a.end());
403 polynomial<T> const zero;
404
405 BOOST_CHECK_EQUAL(a - T(0), a);
406 BOOST_CHECK_EQUAL(T(0) - a, -a);
407 BOOST_CHECK_EQUAL(a - zero, a);
408 BOOST_CHECK_EQUAL(zero - a, -a);
409 BOOST_CHECK_EQUAL(a - a, zero);
410 }
411
412 BOOST_AUTO_TEST_CASE_TEMPLATE( test_multiplication, T, all_test_types )
413 {
414 polynomial<T> const a(d3a.begin(), d3a.end());
415 polynomial<T> const b(d1a.begin(), d1a.end());
416 polynomial<T> const zero;
417 std::array<T, 7> const d3a_sq = {{100, -120, -44, 108, -20, -24, 9}};
418 polynomial<T> const a_sq(d3a_sq.begin(), d3a_sq.end());
419
420 BOOST_CHECK_EQUAL(a * T(0), zero);
421 BOOST_CHECK_EQUAL(a * zero, zero);
422 BOOST_CHECK_EQUAL(zero * T(0), zero);
423 BOOST_CHECK_EQUAL(zero * zero, zero);
424 BOOST_CHECK_EQUAL(a * b, b * a);
425 polynomial<T> aa(a);
426 aa *= aa;
427 BOOST_CHECK_EQUAL(aa, a_sq);
428 BOOST_CHECK_EQUAL(aa, a * a);
429 }
430
431 BOOST_AUTO_TEST_CASE_TEMPLATE( test_arithmetic_relations, T, all_test_types )
432 {
433 polynomial<T> const a(d8b.begin(), d8b.end());
434 polynomial<T> const b(d1a.begin(), d1a.end());
435
436 BOOST_CHECK_EQUAL(a * T(2), a + a);
437 BOOST_CHECK_EQUAL(a - b, -b + a);
438 BOOST_CHECK_EQUAL(a, (a * a) / a);
439 BOOST_CHECK_EQUAL(a, (a / a) * a);
440 }
441
442
443 BOOST_AUTO_TEST_CASE_TEMPLATE(test_non_integral_arithmetic_relations, T, non_integral_test_types )
444 {
445 polynomial<T> const a(d8b.begin(), d8b.end());
446 polynomial<T> const b(d1a.begin(), d1a.end());
447
448 BOOST_CHECK_EQUAL(a * T(0.5), a / T(2));
449 }
450
451 BOOST_AUTO_TEST_CASE_TEMPLATE(test_cont_and_pp, T, integral_test_types)
452 {
453 std::array<polynomial<T>, 4> const q={{
454 polynomial<T>(d8.begin(), d8.end()),
455 polynomial<T>(d8b.begin(), d8b.end()),
456 polynomial<T>(d3a.begin(), d3a.end()),
457 polynomial<T>(d3b.begin(), d3b.end())
458 }};
459 for (std::size_t i = 0; i < q.size(); i++)
460 {
461 BOOST_CHECK_EQUAL(q[i], content(q[i]) * primitive_part(q[i]));
462 BOOST_CHECK_EQUAL(primitive_part(q[i]), primitive_part(q[i], content(q[i])));
463 }
464
465 polynomial<T> const zero;
466 BOOST_CHECK_EQUAL(primitive_part(zero), zero);
467 BOOST_CHECK_EQUAL(content(zero), T(0));
468 }
469
470 BOOST_AUTO_TEST_CASE_TEMPLATE( test_self_multiply_assign, T, all_test_types )
471 {
472 polynomial<T> a(d3a.begin(), d3a.end());
473 polynomial<T> const b(a);
474 std::array<double, 7> const d3a_sq = {{100, -120, -44, 108, -20, -24, 9}};
475 polynomial<T> const asq(d3a_sq.begin(), d3a_sq.end());
476
477 a *= a;
478
479 BOOST_CHECK_EQUAL(a, b*b);
480 BOOST_CHECK_EQUAL(a, asq);
481
482 a *= a;
483
484 BOOST_CHECK_EQUAL(a, b*b*b*b);
485 }
486
487
488 BOOST_AUTO_TEST_CASE_TEMPLATE(test_right_shift, T, all_test_types )
489 {
490 polynomial<T> a(d8b.begin(), d8b.end());
491 polynomial<T> const aa(a);
492 polynomial<T> const b(d8b.begin() + 1, d8b.end());
493 polynomial<T> const c(d8b.begin() + 5, d8b.end());
494 a >>= 0u;
495 BOOST_CHECK_EQUAL(a, aa);
496 a >>= 1u;
497 BOOST_CHECK_EQUAL(a, b);
498 a = a >> 4u;
499 BOOST_CHECK_EQUAL(a, c);
500 }
501
502
503 BOOST_AUTO_TEST_CASE_TEMPLATE(test_left_shift, T, all_test_types )
504 {
505 polynomial<T> a(d0a.begin(), d0a.end());
506 polynomial<T> const aa(a);
507 polynomial<T> const b(d0a1.begin(), d0a1.end());
508 polynomial<T> const c(d0a5.begin(), d0a5.end());
509 a <<= 0u;
510 BOOST_CHECK_EQUAL(a, aa);
511 a <<= 1u;
512 BOOST_CHECK_EQUAL(a, b);
513 a = a << 4u;
514 BOOST_CHECK_EQUAL(a, c);
515 polynomial<T> zero;
516 // Multiplying zero by x should still be zero.
517 zero <<= 1u;
518 BOOST_CHECK_EQUAL(zero, zero_element(multiplies< polynomial<T> >()));
519 }
520
521
522 BOOST_AUTO_TEST_CASE_TEMPLATE(test_odd_even, T, all_test_types)
523 {
524 polynomial<T> const zero;
525 BOOST_CHECK_EQUAL(odd(zero), false);
526 BOOST_CHECK_EQUAL(even(zero), true);
527 polynomial<T> const a(d0a.begin(), d0a.end());
528 BOOST_CHECK_EQUAL(odd(a), true);
529 BOOST_CHECK_EQUAL(even(a), false);
530 polynomial<T> const b(d0a1.begin(), d0a1.end());
531 BOOST_CHECK_EQUAL(odd(b), false);
532 BOOST_CHECK_EQUAL(even(b), true);
533 }
534
535 // NOTE: Slightly unexpected: this unit test passes even when T = char.
536 BOOST_AUTO_TEST_CASE_TEMPLATE( test_pow, T, all_test_types )
537 {
538 if (std::numeric_limits<T>::digits < 32)
539 return; // Invokes undefined behaviour
540 polynomial<T> a(d3a.begin(), d3a.end());
541 polynomial<T> const one(T(1));
542 std::array<double, 7> const d3a_sqr = {{100, -120, -44, 108, -20, -24, 9}};
543 std::array<double, 10> const d3a_cub =
544 {{1000, -1800, -120, 2124, -1032, -684, 638, -18, -108, 27}};
545 polynomial<T> const asqr(d3a_sqr.begin(), d3a_sqr.end());
546 polynomial<T> const acub(d3a_cub.begin(), d3a_cub.end());
547
548 BOOST_CHECK_EQUAL(pow(a, 0), one);
549 BOOST_CHECK_EQUAL(pow(a, 1), a);
550 BOOST_CHECK_EQUAL(pow(a, 2), asqr);
551 BOOST_CHECK_EQUAL(pow(a, 3), acub);
552 BOOST_CHECK_EQUAL(pow(a, 4), pow(asqr, 2));
553 BOOST_CHECK_EQUAL(pow(a, 5), asqr * acub);
554 BOOST_CHECK_EQUAL(pow(a, 6), pow(acub, 2));
555 BOOST_CHECK_EQUAL(pow(a, 7), acub * acub * a);
556
557 BOOST_CHECK_THROW(pow(a, -1), std::domain_error);
558 BOOST_CHECK_EQUAL(pow(one, 137), one);
559 }
560
561
562 BOOST_AUTO_TEST_CASE_TEMPLATE(test_bool, T, all_test_types)
563 {
564 polynomial<T> const zero;
565 polynomial<T> const a(d0a.begin(), d0a.end());
566 BOOST_CHECK_EQUAL(bool(zero), false);
567 BOOST_CHECK_EQUAL(bool(a), true);
568 }
569
570
571 BOOST_AUTO_TEST_CASE_TEMPLATE(test_set_zero, T, all_test_types)
572 {
573 polynomial<T> const zero;
574 polynomial<T> a(d0a.begin(), d0a.end());
575 a.set_zero();
576 BOOST_CHECK_EQUAL(a, zero);
577 a.set_zero(); // Ensure that setting zero to zero is a no-op.
578 BOOST_CHECK_EQUAL(a, zero);
579 }
580
581
582 BOOST_AUTO_TEST_CASE_TEMPLATE(test_leading_coefficient, T, all_test_types)
583 {
584 polynomial<T> const zero;
585 BOOST_CHECK_EQUAL(leading_coefficient(zero), T(0));
586 polynomial<T> a(d0a.begin(), d0a.end());
587 BOOST_CHECK_EQUAL(leading_coefficient(a), T(d0a.back()));
588 }
589
590 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX)
591 BOOST_AUTO_TEST_CASE_TEMPLATE(test_prime, T, all_test_types)
592 {
593 std::vector<T> d{1,1,1,1,1};
594 polynomial<T> p(std::move(d));
595 polynomial<T> q = p.prime();
596 BOOST_CHECK_EQUAL(q(0), T(1));
597
598 for (size_t i = 0; i < q.size(); ++i)
599 {
600 BOOST_CHECK_EQUAL(q[i], i+1);
601 }
602
603 polynomial<T> P = p.integrate();
604 BOOST_CHECK_EQUAL(P(0), T(0));
605 for (size_t i = 1; i < P.size(); ++i)
606 {
607 BOOST_CHECK_EQUAL(P[i], 1/static_cast<T>(i));
608 }
609
610 polynomial<T> empty;
611 q = empty.prime();
612 BOOST_CHECK_EQUAL(q.size(), 0);
613
614 }
615 #endif