test_polynomial.cpp 19 KB

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