polynomial.hpp 21 KB

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