polynomial.hpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  1. // (C) Copyright John Maddock 2006.
  2. // (C) Copyright Jeremy William Murphy 2015.
  3. // Use, modification and distribution are subject to the
  4. // Boost Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_MATH_TOOLS_POLYNOMIAL_HPP
  7. #define BOOST_MATH_TOOLS_POLYNOMIAL_HPP
  8. #ifdef _MSC_VER
  9. #pragma once
  10. #endif
  11. #include <boost/assert.hpp>
  12. #include <boost/config.hpp>
  13. #ifdef BOOST_NO_CXX11_LAMBDAS
  14. #include <boost/lambda/lambda.hpp>
  15. #endif
  16. #include <boost/math/tools/rational.hpp>
  17. #include <boost/math/tools/real_cast.hpp>
  18. #include <boost/math/policies/error_handling.hpp>
  19. #include <boost/math/special_functions/binomial.hpp>
  20. #include <boost/core/enable_if.hpp>
  21. #include <vector>
  22. #include <ostream>
  23. #include <algorithm>
  24. #ifndef BOOST_NO_CXX11_HDR_INITIALIZER_LIST
  25. #include <initializer_list>
  26. #endif
  27. namespace boost{ namespace math{ namespace tools{
  28. template <class T>
  29. T chebyshev_coefficient(unsigned n, unsigned m)
  30. {
  31. BOOST_MATH_STD_USING
  32. if(m > n)
  33. return 0;
  34. if((n & 1) != (m & 1))
  35. return 0;
  36. if(n == 0)
  37. return 1;
  38. T result = T(n) / 2;
  39. unsigned r = n - m;
  40. r /= 2;
  41. BOOST_ASSERT(n - 2 * r == m);
  42. if(r & 1)
  43. result = -result;
  44. result /= n - r;
  45. result *= boost::math::binomial_coefficient<T>(n - r, r);
  46. result *= ldexp(1.0f, m);
  47. return result;
  48. }
  49. template <class Seq>
  50. Seq polynomial_to_chebyshev(const Seq& s)
  51. {
  52. // Converts a Polynomial into Chebyshev form:
  53. typedef typename Seq::value_type value_type;
  54. typedef typename Seq::difference_type difference_type;
  55. Seq result(s);
  56. difference_type order = s.size() - 1;
  57. difference_type even_order = order & 1 ? order - 1 : order;
  58. difference_type odd_order = order & 1 ? order : order - 1;
  59. for(difference_type i = even_order; i >= 0; i -= 2)
  60. {
  61. value_type val = s[i];
  62. for(difference_type k = even_order; k > i; k -= 2)
  63. {
  64. val -= result[k] * chebyshev_coefficient<value_type>(static_cast<unsigned>(k), static_cast<unsigned>(i));
  65. }
  66. val /= chebyshev_coefficient<value_type>(static_cast<unsigned>(i), static_cast<unsigned>(i));
  67. result[i] = val;
  68. }
  69. result[0] *= 2;
  70. for(difference_type i = odd_order; i >= 0; i -= 2)
  71. {
  72. value_type val = s[i];
  73. for(difference_type k = odd_order; k > i; k -= 2)
  74. {
  75. val -= result[k] * chebyshev_coefficient<value_type>(static_cast<unsigned>(k), static_cast<unsigned>(i));
  76. }
  77. val /= chebyshev_coefficient<value_type>(static_cast<unsigned>(i), static_cast<unsigned>(i));
  78. result[i] = val;
  79. }
  80. return result;
  81. }
  82. template <class Seq, class T>
  83. T evaluate_chebyshev(const Seq& a, const T& x)
  84. {
  85. // Clenshaw's formula:
  86. typedef typename Seq::difference_type difference_type;
  87. T yk2 = 0;
  88. T yk1 = 0;
  89. T yk = 0;
  90. for(difference_type i = a.size() - 1; i >= 1; --i)
  91. {
  92. yk2 = yk1;
  93. yk1 = yk;
  94. yk = 2 * x * yk1 - yk2 + a[i];
  95. }
  96. return a[0] / 2 + yk * x - yk1;
  97. }
  98. template <typename T>
  99. class polynomial;
  100. namespace detail {
  101. /**
  102. * Knuth, The Art of Computer Programming: Volume 2, Third edition, 1998
  103. * Chapter 4.6.1, Algorithm D: Division of polynomials over a field.
  104. *
  105. * @tparam T Coefficient type, must be not be an integer.
  106. *
  107. * Template-parameter T actually must be a field but we don't currently have that
  108. * subtlety of distinction.
  109. */
  110. template <typename T, typename N>
  111. BOOST_DEDUCED_TYPENAME disable_if_c<std::numeric_limits<T>::is_integer, void >::type
  112. division_impl(polynomial<T> &q, polynomial<T> &u, const polynomial<T>& v, N n, N k)
  113. {
  114. q[k] = u[n + k] / v[n];
  115. for (N j = n + k; j > k;)
  116. {
  117. j--;
  118. u[j] -= q[k] * v[j - k];
  119. }
  120. }
  121. template <class T, class N>
  122. T integer_power(T t, N n)
  123. {
  124. switch(n)
  125. {
  126. case 0:
  127. return static_cast<T>(1u);
  128. case 1:
  129. return t;
  130. case 2:
  131. return t * t;
  132. case 3:
  133. return t * t * t;
  134. }
  135. T result = integer_power(t, n / 2);
  136. result *= result;
  137. if(n & 1)
  138. result *= t;
  139. return result;
  140. }
  141. /**
  142. * Knuth, The Art of Computer Programming: Volume 2, Third edition, 1998
  143. * Chapter 4.6.1, Algorithm R: Pseudo-division of polynomials.
  144. *
  145. * @tparam T Coefficient type, must be an integer.
  146. *
  147. * Template-parameter T actually must be a unique factorization domain but we
  148. * don't currently have that subtlety of distinction.
  149. */
  150. template <typename T, typename N>
  151. BOOST_DEDUCED_TYPENAME enable_if_c<std::numeric_limits<T>::is_integer, void >::type
  152. division_impl(polynomial<T> &q, polynomial<T> &u, const polynomial<T>& v, N n, N k)
  153. {
  154. q[k] = u[n + k] * integer_power(v[n], k);
  155. for (N j = n + k; j > 0;)
  156. {
  157. j--;
  158. u[j] = v[n] * u[j] - (j < k ? T(0) : u[n + k] * v[j - k]);
  159. }
  160. }
  161. /**
  162. * Knuth, The Art of Computer Programming: Volume 2, Third edition, 1998
  163. * Chapter 4.6.1, Algorithm D and R: Main loop.
  164. *
  165. * @param u Dividend.
  166. * @param v Divisor.
  167. */
  168. template <typename T>
  169. std::pair< polynomial<T>, polynomial<T> >
  170. division(polynomial<T> u, const polynomial<T>& v)
  171. {
  172. BOOST_ASSERT(v.size() <= u.size());
  173. BOOST_ASSERT(v);
  174. BOOST_ASSERT(u);
  175. typedef typename polynomial<T>::size_type N;
  176. N const m = u.size() - 1, n = v.size() - 1;
  177. N k = m - n;
  178. polynomial<T> q;
  179. q.data().resize(m - n + 1);
  180. do
  181. {
  182. division_impl(q, u, v, n, k);
  183. }
  184. while (k-- != 0);
  185. u.data().resize(n);
  186. u.normalize(); // Occasionally, the remainder is zeroes.
  187. return std::make_pair(q, u);
  188. }
  189. //
  190. // These structures are the same as the void specializations of the functors of the same name
  191. // in the std lib from C++14 onwards:
  192. //
  193. struct negate
  194. {
  195. template <class T>
  196. T operator()(T const &x) const
  197. {
  198. return -x;
  199. }
  200. };
  201. struct plus
  202. {
  203. template <class T, class U>
  204. T operator()(T const &x, U const& y) const
  205. {
  206. return x + y;
  207. }
  208. };
  209. struct minus
  210. {
  211. template <class T, class U>
  212. T operator()(T const &x, U const& y) const
  213. {
  214. return x - y;
  215. }
  216. };
  217. } // namespace detail
  218. /**
  219. * Returns the zero element for multiplication of polynomials.
  220. */
  221. template <class T>
  222. polynomial<T> zero_element(std::multiplies< polynomial<T> >)
  223. {
  224. return polynomial<T>();
  225. }
  226. template <class T>
  227. polynomial<T> identity_element(std::multiplies< polynomial<T> >)
  228. {
  229. return polynomial<T>(T(1));
  230. }
  231. /* Calculates a / b and a % b, returning the pair (quotient, remainder) together
  232. * because the same amount of computation yields both.
  233. * This function is not defined for division by zero: user beware.
  234. */
  235. template <typename T>
  236. std::pair< polynomial<T>, polynomial<T> >
  237. quotient_remainder(const polynomial<T>& dividend, const polynomial<T>& divisor)
  238. {
  239. BOOST_ASSERT(divisor);
  240. if (dividend.size() < divisor.size())
  241. return std::make_pair(polynomial<T>(), dividend);
  242. return detail::division(dividend, divisor);
  243. }
  244. template <class T>
  245. class polynomial
  246. {
  247. public:
  248. // typedefs:
  249. typedef typename std::vector<T>::value_type value_type;
  250. typedef typename std::vector<T>::size_type size_type;
  251. // construct:
  252. polynomial(){}
  253. template <class U>
  254. polynomial(const U* data, unsigned order)
  255. : m_data(data, data + order + 1)
  256. {
  257. normalize();
  258. }
  259. template <class I>
  260. polynomial(I first, I last)
  261. : m_data(first, last)
  262. {
  263. normalize();
  264. }
  265. template <class U>
  266. explicit polynomial(const U& point)
  267. {
  268. if (point != U(0))
  269. m_data.push_back(point);
  270. }
  271. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  272. // move:
  273. polynomial(polynomial&& p) BOOST_NOEXCEPT
  274. : m_data(std::move(p.m_data)) { }
  275. #endif
  276. // copy:
  277. polynomial(const polynomial& p)
  278. : m_data(p.m_data) { }
  279. template <class U>
  280. polynomial(const polynomial<U>& p)
  281. {
  282. for(unsigned i = 0; i < p.size(); ++i)
  283. {
  284. m_data.push_back(boost::math::tools::real_cast<T>(p[i]));
  285. }
  286. }
  287. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) && !BOOST_WORKAROUND(BOOST_GCC_VERSION, < 40500)
  288. polynomial(std::initializer_list<T> l) : polynomial(std::begin(l), std::end(l))
  289. {
  290. }
  291. polynomial&
  292. operator=(std::initializer_list<T> l)
  293. {
  294. m_data.assign(std::begin(l), std::end(l));
  295. normalize();
  296. return *this;
  297. }
  298. #endif
  299. // access:
  300. size_type size() const { return m_data.size(); }
  301. size_type degree() const
  302. {
  303. if (size() == 0)
  304. throw std::logic_error("degree() is undefined for the zero polynomial.");
  305. return m_data.size() - 1;
  306. }
  307. value_type& operator[](size_type i)
  308. {
  309. return m_data[i];
  310. }
  311. const value_type& operator[](size_type i) const
  312. {
  313. return m_data[i];
  314. }
  315. T evaluate(T z) const
  316. {
  317. return m_data.size() > 0 ? boost::math::tools::evaluate_polynomial(&m_data[0], z, m_data.size()) : 0;
  318. }
  319. std::vector<T> chebyshev() const
  320. {
  321. return polynomial_to_chebyshev(m_data);
  322. }
  323. std::vector<T> const& data() const
  324. {
  325. return m_data;
  326. }
  327. std::vector<T> & data()
  328. {
  329. return m_data;
  330. }
  331. // operators:
  332. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  333. polynomial& operator =(polynomial&& p) BOOST_NOEXCEPT
  334. {
  335. m_data = std::move(p.m_data);
  336. return *this;
  337. }
  338. #endif
  339. polynomial& operator =(const polynomial& p)
  340. {
  341. m_data = p.m_data;
  342. return *this;
  343. }
  344. template <class U>
  345. typename boost::enable_if_c<boost::is_constructible<T, U>::value, polynomial&>::type operator +=(const U& value)
  346. {
  347. addition(value);
  348. normalize();
  349. return *this;
  350. }
  351. template <class U>
  352. typename boost::enable_if_c<boost::is_constructible<T, U>::value, polynomial&>::type operator -=(const U& value)
  353. {
  354. subtraction(value);
  355. normalize();
  356. return *this;
  357. }
  358. template <class U>
  359. typename boost::enable_if_c<boost::is_constructible<T, U>::value, polynomial&>::type operator *=(const U& value)
  360. {
  361. multiplication(value);
  362. normalize();
  363. return *this;
  364. }
  365. template <class U>
  366. typename boost::enable_if_c<boost::is_constructible<T, U>::value, polynomial&>::type operator /=(const U& value)
  367. {
  368. division(value);
  369. normalize();
  370. return *this;
  371. }
  372. template <class U>
  373. typename boost::enable_if_c<boost::is_constructible<T, U>::value, polynomial&>::type operator %=(const U& /*value*/)
  374. {
  375. // We can always divide by a scalar, so there is no remainder:
  376. this->set_zero();
  377. return *this;
  378. }
  379. template <class U>
  380. polynomial& operator +=(const polynomial<U>& value)
  381. {
  382. addition(value);
  383. normalize();
  384. return *this;
  385. }
  386. template <class U>
  387. polynomial& operator -=(const polynomial<U>& value)
  388. {
  389. subtraction(value);
  390. normalize();
  391. return *this;
  392. }
  393. template <typename U, typename V>
  394. void multiply(const polynomial<U>& a, const polynomial<V>& b) {
  395. if (!a || !b)
  396. {
  397. this->set_zero();
  398. return;
  399. }
  400. std::vector<T> prod(a.size() + b.size() - 1, T(0));
  401. for (unsigned i = 0; i < a.size(); ++i)
  402. for (unsigned j = 0; j < b.size(); ++j)
  403. prod[i+j] += a.m_data[i] * b.m_data[j];
  404. m_data.swap(prod);
  405. }
  406. template <class U>
  407. polynomial& operator *=(const polynomial<U>& value)
  408. {
  409. this->multiply(*this, value);
  410. return *this;
  411. }
  412. template <typename U>
  413. polynomial& operator /=(const polynomial<U>& value)
  414. {
  415. *this = quotient_remainder(*this, value).first;
  416. return *this;
  417. }
  418. template <typename U>
  419. polynomial& operator %=(const polynomial<U>& value)
  420. {
  421. *this = quotient_remainder(*this, value).second;
  422. return *this;
  423. }
  424. template <typename U>
  425. polynomial& operator >>=(U const &n)
  426. {
  427. BOOST_ASSERT(n <= m_data.size());
  428. m_data.erase(m_data.begin(), m_data.begin() + n);
  429. return *this;
  430. }
  431. template <typename U>
  432. polynomial& operator <<=(U const &n)
  433. {
  434. m_data.insert(m_data.begin(), n, static_cast<T>(0));
  435. normalize();
  436. return *this;
  437. }
  438. // Convenient and efficient query for zero.
  439. bool is_zero() const
  440. {
  441. return m_data.empty();
  442. }
  443. // Conversion to bool.
  444. #ifdef BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS
  445. typedef bool (polynomial::*unmentionable_type)() const;
  446. BOOST_FORCEINLINE operator unmentionable_type() const
  447. {
  448. return is_zero() ? false : &polynomial::is_zero;
  449. }
  450. #else
  451. BOOST_FORCEINLINE explicit operator bool() const
  452. {
  453. return !m_data.empty();
  454. }
  455. #endif
  456. // Fast way to set a polynomial to zero.
  457. void set_zero()
  458. {
  459. m_data.clear();
  460. }
  461. /** Remove zero coefficients 'from the top', that is for which there are no
  462. * non-zero coefficients of higher degree. */
  463. void normalize()
  464. {
  465. #ifndef BOOST_NO_CXX11_LAMBDAS
  466. m_data.erase(std::find_if(m_data.rbegin(), m_data.rend(), [](const T& x)->bool { return x != 0; }).base(), m_data.end());
  467. #else
  468. using namespace boost::lambda;
  469. m_data.erase(std::find_if(m_data.rbegin(), m_data.rend(), _1 != T(0)).base(), m_data.end());
  470. #endif
  471. }
  472. private:
  473. template <class U, class R>
  474. polynomial& addition(const U& value, R op)
  475. {
  476. if(m_data.size() == 0)
  477. m_data.resize(1, 0);
  478. m_data[0] = op(m_data[0], value);
  479. return *this;
  480. }
  481. template <class U>
  482. polynomial& addition(const U& value)
  483. {
  484. return addition(value, detail::plus());
  485. }
  486. template <class U>
  487. polynomial& subtraction(const U& value)
  488. {
  489. return addition(value, detail::minus());
  490. }
  491. template <class U, class R>
  492. polynomial& addition(const polynomial<U>& value, R op)
  493. {
  494. if (m_data.size() < value.size())
  495. m_data.resize(value.size(), 0);
  496. for(size_type i = 0; i < value.size(); ++i)
  497. m_data[i] = op(m_data[i], value[i]);
  498. return *this;
  499. }
  500. template <class U>
  501. polynomial& addition(const polynomial<U>& value)
  502. {
  503. return addition(value, detail::plus());
  504. }
  505. template <class U>
  506. polynomial& subtraction(const polynomial<U>& value)
  507. {
  508. return addition(value, detail::minus());
  509. }
  510. template <class U>
  511. polynomial& multiplication(const U& value)
  512. {
  513. #ifndef BOOST_NO_CXX11_LAMBDAS
  514. std::transform(m_data.begin(), m_data.end(), m_data.begin(), [&](const T& x)->T { return x * value; });
  515. #else
  516. using namespace boost::lambda;
  517. std::transform(m_data.begin(), m_data.end(), m_data.begin(), ret<T>(_1 * value));
  518. #endif
  519. return *this;
  520. }
  521. template <class U>
  522. polynomial& division(const U& value)
  523. {
  524. #ifndef BOOST_NO_CXX11_LAMBDAS
  525. std::transform(m_data.begin(), m_data.end(), m_data.begin(), [&](const T& x)->T { return x / value; });
  526. #else
  527. using namespace boost::lambda;
  528. std::transform(m_data.begin(), m_data.end(), m_data.begin(), ret<T>(_1 / value));
  529. #endif
  530. return *this;
  531. }
  532. std::vector<T> m_data;
  533. };
  534. template <class T>
  535. inline polynomial<T> operator + (const polynomial<T>& a, const polynomial<T>& b)
  536. {
  537. polynomial<T> result(a);
  538. result += b;
  539. return result;
  540. }
  541. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  542. template <class T>
  543. inline polynomial<T> operator + (polynomial<T>&& a, const polynomial<T>& b)
  544. {
  545. a += b;
  546. return a;
  547. }
  548. template <class T>
  549. inline polynomial<T> operator + (const polynomial<T>& a, polynomial<T>&& b)
  550. {
  551. b += a;
  552. return b;
  553. }
  554. template <class T>
  555. inline polynomial<T> operator + (polynomial<T>&& a, polynomial<T>&& b)
  556. {
  557. a += b;
  558. return a;
  559. }
  560. #endif
  561. template <class T>
  562. inline polynomial<T> operator - (const polynomial<T>& a, const polynomial<T>& b)
  563. {
  564. polynomial<T> result(a);
  565. result -= b;
  566. return result;
  567. }
  568. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  569. template <class T>
  570. inline polynomial<T> operator - (polynomial<T>&& a, const polynomial<T>& b)
  571. {
  572. a -= b;
  573. return a;
  574. }
  575. template <class T>
  576. inline polynomial<T> operator - (const polynomial<T>& a, polynomial<T>&& b)
  577. {
  578. b -= a;
  579. return -b;
  580. }
  581. template <class T>
  582. inline polynomial<T> operator - (polynomial<T>&& a, polynomial<T>&& b)
  583. {
  584. a -= b;
  585. return a;
  586. }
  587. #endif
  588. template <class T>
  589. inline polynomial<T> operator * (const polynomial<T>& a, const polynomial<T>& b)
  590. {
  591. polynomial<T> result;
  592. result.multiply(a, b);
  593. return result;
  594. }
  595. template <class T>
  596. inline polynomial<T> operator / (const polynomial<T>& a, const polynomial<T>& b)
  597. {
  598. return quotient_remainder(a, b).first;
  599. }
  600. template <class T>
  601. inline polynomial<T> operator % (const polynomial<T>& a, const polynomial<T>& b)
  602. {
  603. return quotient_remainder(a, b).second;
  604. }
  605. template <class T, class U>
  606. inline typename boost::enable_if_c<boost::is_constructible<T, U>::value, polynomial<T> >::type operator + (polynomial<T> a, const U& b)
  607. {
  608. a += b;
  609. return a;
  610. }
  611. template <class T, class U>
  612. inline typename boost::enable_if_c<boost::is_constructible<T, U>::value, polynomial<T> >::type operator - (polynomial<T> a, const U& b)
  613. {
  614. a -= b;
  615. return a;
  616. }
  617. template <class T, class U>
  618. inline typename boost::enable_if_c<boost::is_constructible<T, U>::value, polynomial<T> >::type operator * (polynomial<T> a, const U& b)
  619. {
  620. a *= b;
  621. return a;
  622. }
  623. template <class T, class U>
  624. inline typename boost::enable_if_c<boost::is_constructible<T, U>::value, polynomial<T> >::type operator / (polynomial<T> a, const U& b)
  625. {
  626. a /= b;
  627. return a;
  628. }
  629. template <class T, class U>
  630. inline typename boost::enable_if_c<boost::is_constructible<T, U>::value, polynomial<T> >::type operator % (const polynomial<T>&, const U&)
  631. {
  632. // Since we can always divide by a scalar, result is always an empty polynomial:
  633. return polynomial<T>();
  634. }
  635. template <class U, class T>
  636. inline typename boost::enable_if_c<boost::is_constructible<T, U>::value, polynomial<T> >::type operator + (const U& a, polynomial<T> b)
  637. {
  638. b += a;
  639. return b;
  640. }
  641. template <class U, class T>
  642. inline typename boost::enable_if_c<boost::is_constructible<T, U>::value, polynomial<T> >::type operator - (const U& a, polynomial<T> b)
  643. {
  644. b -= a;
  645. return -b;
  646. }
  647. template <class U, class T>
  648. inline typename boost::enable_if_c<boost::is_constructible<T, U>::value, polynomial<T> >::type operator * (const U& a, polynomial<T> b)
  649. {
  650. b *= a;
  651. return b;
  652. }
  653. template <class T>
  654. bool operator == (const polynomial<T> &a, const polynomial<T> &b)
  655. {
  656. return a.data() == b.data();
  657. }
  658. template <class T>
  659. bool operator != (const polynomial<T> &a, const polynomial<T> &b)
  660. {
  661. return a.data() != b.data();
  662. }
  663. template <typename T, typename U>
  664. polynomial<T> operator >> (polynomial<T> a, const U& b)
  665. {
  666. a >>= b;
  667. return a;
  668. }
  669. template <typename T, typename U>
  670. polynomial<T> operator << (polynomial<T> a, const U& b)
  671. {
  672. a <<= b;
  673. return a;
  674. }
  675. // Unary minus (negate).
  676. template <class T>
  677. polynomial<T> operator - (polynomial<T> a)
  678. {
  679. std::transform(a.data().begin(), a.data().end(), a.data().begin(), detail::negate());
  680. return a;
  681. }
  682. template <class T>
  683. bool odd(polynomial<T> const &a)
  684. {
  685. return a.size() > 0 && a[0] != static_cast<T>(0);
  686. }
  687. template <class T>
  688. bool even(polynomial<T> const &a)
  689. {
  690. return !odd(a);
  691. }
  692. template <class T>
  693. polynomial<T> pow(polynomial<T> base, int exp)
  694. {
  695. if (exp < 0)
  696. return policies::raise_domain_error(
  697. "boost::math::tools::pow<%1%>",
  698. "Negative powers are not supported for polynomials.",
  699. base, policies::policy<>());
  700. // if the policy is ignore_error or errno_on_error, raise_domain_error
  701. // will return std::numeric_limits<polynomial<T>>::quiet_NaN(), which
  702. // defaults to polynomial<T>(), which is the zero polynomial
  703. polynomial<T> result(T(1));
  704. if (exp & 1)
  705. result = base;
  706. /* "Exponentiation by squaring" */
  707. while (exp >>= 1)
  708. {
  709. base *= base;
  710. if (exp & 1)
  711. result *= base;
  712. }
  713. return result;
  714. }
  715. template <class charT, class traits, class T>
  716. inline std::basic_ostream<charT, traits>& operator << (std::basic_ostream<charT, traits>& os, const polynomial<T>& poly)
  717. {
  718. os << "{ ";
  719. for(unsigned i = 0; i < poly.size(); ++i)
  720. {
  721. if(i) os << ", ";
  722. os << poly[i];
  723. }
  724. os << " }";
  725. return os;
  726. }
  727. } // namespace tools
  728. } // namespace math
  729. } // namespace boost
  730. //
  731. // Polynomial specific overload of gcd algorithm:
  732. //
  733. #include <boost/math/tools/polynomial_gcd.hpp>
  734. #endif // BOOST_MATH_TOOLS_POLYNOMIAL_HPP