polynomial.hpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  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. if (m_data.size() == 0)
  355. {
  356. return polynomial<T>({});
  357. }
  358. std::vector<T> p_data(m_data.size() - 1);
  359. for (size_t i = 0; i < p_data.size(); ++i) {
  360. p_data[i] = m_data[i+1]*static_cast<T>(i+1);
  361. }
  362. return polynomial<T>(std::move(p_data));
  363. }
  364. polynomial<T> integrate() const
  365. {
  366. std::vector<T> i_data(m_data.size() + 1);
  367. // Choose integration constant such that P(0) = 0.
  368. i_data[0] = T(0);
  369. for (size_t i = 1; i < i_data.size(); ++i)
  370. {
  371. i_data[i] = m_data[i-1]/static_cast<T>(i);
  372. }
  373. return polynomial<T>(std::move(i_data));
  374. }
  375. // operators:
  376. polynomial& operator =(polynomial&& p) BOOST_NOEXCEPT
  377. {
  378. m_data = std::move(p.m_data);
  379. return *this;
  380. }
  381. #endif
  382. polynomial& operator =(const polynomial& p)
  383. {
  384. m_data = p.m_data;
  385. return *this;
  386. }
  387. template <class U>
  388. typename boost::enable_if_c<boost::is_constructible<T, U>::value, polynomial&>::type operator +=(const U& value)
  389. {
  390. addition(value);
  391. normalize();
  392. return *this;
  393. }
  394. template <class U>
  395. typename boost::enable_if_c<boost::is_constructible<T, U>::value, polynomial&>::type operator -=(const U& value)
  396. {
  397. subtraction(value);
  398. normalize();
  399. return *this;
  400. }
  401. template <class U>
  402. typename boost::enable_if_c<boost::is_constructible<T, U>::value, polynomial&>::type operator *=(const U& value)
  403. {
  404. multiplication(value);
  405. normalize();
  406. return *this;
  407. }
  408. template <class U>
  409. typename boost::enable_if_c<boost::is_constructible<T, U>::value, polynomial&>::type operator /=(const U& value)
  410. {
  411. division(value);
  412. normalize();
  413. return *this;
  414. }
  415. template <class U>
  416. typename boost::enable_if_c<boost::is_constructible<T, U>::value, polynomial&>::type operator %=(const U& /*value*/)
  417. {
  418. // We can always divide by a scalar, so there is no remainder:
  419. this->set_zero();
  420. return *this;
  421. }
  422. template <class U>
  423. polynomial& operator +=(const polynomial<U>& value)
  424. {
  425. addition(value);
  426. normalize();
  427. return *this;
  428. }
  429. template <class U>
  430. polynomial& operator -=(const polynomial<U>& value)
  431. {
  432. subtraction(value);
  433. normalize();
  434. return *this;
  435. }
  436. template <typename U, typename V>
  437. void multiply(const polynomial<U>& a, const polynomial<V>& b) {
  438. if (!a || !b)
  439. {
  440. this->set_zero();
  441. return;
  442. }
  443. std::vector<T> prod(a.size() + b.size() - 1, T(0));
  444. for (unsigned i = 0; i < a.size(); ++i)
  445. for (unsigned j = 0; j < b.size(); ++j)
  446. prod[i+j] += a.m_data[i] * b.m_data[j];
  447. m_data.swap(prod);
  448. }
  449. template <class U>
  450. polynomial& operator *=(const polynomial<U>& value)
  451. {
  452. this->multiply(*this, value);
  453. return *this;
  454. }
  455. template <typename U>
  456. polynomial& operator /=(const polynomial<U>& value)
  457. {
  458. *this = quotient_remainder(*this, value).first;
  459. return *this;
  460. }
  461. template <typename U>
  462. polynomial& operator %=(const polynomial<U>& value)
  463. {
  464. *this = quotient_remainder(*this, value).second;
  465. return *this;
  466. }
  467. template <typename U>
  468. polynomial& operator >>=(U const &n)
  469. {
  470. BOOST_ASSERT(n <= m_data.size());
  471. m_data.erase(m_data.begin(), m_data.begin() + n);
  472. return *this;
  473. }
  474. template <typename U>
  475. polynomial& operator <<=(U const &n)
  476. {
  477. m_data.insert(m_data.begin(), n, static_cast<T>(0));
  478. normalize();
  479. return *this;
  480. }
  481. // Convenient and efficient query for zero.
  482. bool is_zero() const
  483. {
  484. return m_data.empty();
  485. }
  486. // Conversion to bool.
  487. #ifdef BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS
  488. typedef bool (polynomial::*unmentionable_type)() const;
  489. BOOST_FORCEINLINE operator unmentionable_type() const
  490. {
  491. return is_zero() ? false : &polynomial::is_zero;
  492. }
  493. #else
  494. BOOST_FORCEINLINE explicit operator bool() const
  495. {
  496. return !m_data.empty();
  497. }
  498. #endif
  499. // Fast way to set a polynomial to zero.
  500. void set_zero()
  501. {
  502. m_data.clear();
  503. }
  504. /** Remove zero coefficients 'from the top', that is for which there are no
  505. * non-zero coefficients of higher degree. */
  506. void normalize()
  507. {
  508. #ifndef BOOST_NO_CXX11_LAMBDAS
  509. m_data.erase(std::find_if(m_data.rbegin(), m_data.rend(), [](const T& x)->bool { return x != T(0); }).base(), m_data.end());
  510. #else
  511. using namespace boost::lambda;
  512. m_data.erase(std::find_if(m_data.rbegin(), m_data.rend(), _1 != T(0)).base(), m_data.end());
  513. #endif
  514. }
  515. private:
  516. template <class U, class R>
  517. polynomial& addition(const U& value, R op)
  518. {
  519. if(m_data.size() == 0)
  520. m_data.resize(1, 0);
  521. m_data[0] = op(m_data[0], value);
  522. return *this;
  523. }
  524. template <class U>
  525. polynomial& addition(const U& value)
  526. {
  527. return addition(value, detail::plus());
  528. }
  529. template <class U>
  530. polynomial& subtraction(const U& value)
  531. {
  532. return addition(value, detail::minus());
  533. }
  534. template <class U, class R>
  535. polynomial& addition(const polynomial<U>& value, R op)
  536. {
  537. if (m_data.size() < value.size())
  538. m_data.resize(value.size(), 0);
  539. for(size_type i = 0; i < value.size(); ++i)
  540. m_data[i] = op(m_data[i], value[i]);
  541. return *this;
  542. }
  543. template <class U>
  544. polynomial& addition(const polynomial<U>& value)
  545. {
  546. return addition(value, detail::plus());
  547. }
  548. template <class U>
  549. polynomial& subtraction(const polynomial<U>& value)
  550. {
  551. return addition(value, detail::minus());
  552. }
  553. template <class U>
  554. polynomial& multiplication(const U& value)
  555. {
  556. #ifndef BOOST_NO_CXX11_LAMBDAS
  557. std::transform(m_data.begin(), m_data.end(), m_data.begin(), [&](const T& x)->T { return x * value; });
  558. #else
  559. using namespace boost::lambda;
  560. std::transform(m_data.begin(), m_data.end(), m_data.begin(), ret<T>(_1 * value));
  561. #endif
  562. return *this;
  563. }
  564. template <class U>
  565. polynomial& division(const U& value)
  566. {
  567. #ifndef BOOST_NO_CXX11_LAMBDAS
  568. std::transform(m_data.begin(), m_data.end(), m_data.begin(), [&](const T& x)->T { return x / value; });
  569. #else
  570. using namespace boost::lambda;
  571. std::transform(m_data.begin(), m_data.end(), m_data.begin(), ret<T>(_1 / value));
  572. #endif
  573. return *this;
  574. }
  575. std::vector<T> m_data;
  576. };
  577. template <class T>
  578. inline polynomial<T> operator + (const polynomial<T>& a, const polynomial<T>& b)
  579. {
  580. polynomial<T> result(a);
  581. result += b;
  582. return result;
  583. }
  584. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  585. template <class T>
  586. inline polynomial<T> operator + (polynomial<T>&& a, const polynomial<T>& b)
  587. {
  588. a += b;
  589. return a;
  590. }
  591. template <class T>
  592. inline polynomial<T> operator + (const polynomial<T>& a, polynomial<T>&& b)
  593. {
  594. b += a;
  595. return b;
  596. }
  597. template <class T>
  598. inline polynomial<T> operator + (polynomial<T>&& a, polynomial<T>&& b)
  599. {
  600. a += b;
  601. return a;
  602. }
  603. #endif
  604. template <class T>
  605. inline polynomial<T> operator - (const polynomial<T>& a, const polynomial<T>& b)
  606. {
  607. polynomial<T> result(a);
  608. result -= b;
  609. return result;
  610. }
  611. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  612. template <class T>
  613. inline polynomial<T> operator - (polynomial<T>&& a, const polynomial<T>& b)
  614. {
  615. a -= b;
  616. return a;
  617. }
  618. template <class T>
  619. inline polynomial<T> operator - (const polynomial<T>& a, polynomial<T>&& b)
  620. {
  621. b -= a;
  622. return -b;
  623. }
  624. template <class T>
  625. inline polynomial<T> operator - (polynomial<T>&& a, polynomial<T>&& b)
  626. {
  627. a -= b;
  628. return a;
  629. }
  630. #endif
  631. template <class T>
  632. inline polynomial<T> operator * (const polynomial<T>& a, const polynomial<T>& b)
  633. {
  634. polynomial<T> result;
  635. result.multiply(a, b);
  636. return result;
  637. }
  638. template <class T>
  639. inline polynomial<T> operator / (const polynomial<T>& a, const polynomial<T>& b)
  640. {
  641. return quotient_remainder(a, b).first;
  642. }
  643. template <class T>
  644. inline polynomial<T> operator % (const polynomial<T>& a, const polynomial<T>& b)
  645. {
  646. return quotient_remainder(a, b).second;
  647. }
  648. template <class T, class U>
  649. inline typename boost::enable_if_c<boost::is_constructible<T, U>::value, polynomial<T> >::type operator + (polynomial<T> a, const U& b)
  650. {
  651. a += b;
  652. return a;
  653. }
  654. template <class T, class U>
  655. inline typename boost::enable_if_c<boost::is_constructible<T, U>::value, polynomial<T> >::type operator - (polynomial<T> a, const U& b)
  656. {
  657. a -= b;
  658. return a;
  659. }
  660. template <class T, class U>
  661. inline typename boost::enable_if_c<boost::is_constructible<T, U>::value, polynomial<T> >::type operator * (polynomial<T> a, const U& b)
  662. {
  663. a *= b;
  664. return a;
  665. }
  666. template <class T, class U>
  667. inline typename boost::enable_if_c<boost::is_constructible<T, U>::value, polynomial<T> >::type operator / (polynomial<T> a, const U& b)
  668. {
  669. a /= b;
  670. return a;
  671. }
  672. template <class T, class U>
  673. inline typename boost::enable_if_c<boost::is_constructible<T, U>::value, polynomial<T> >::type operator % (const polynomial<T>&, const U&)
  674. {
  675. // Since we can always divide by a scalar, result is always an empty polynomial:
  676. return polynomial<T>();
  677. }
  678. template <class U, class T>
  679. inline typename boost::enable_if_c<boost::is_constructible<T, U>::value, polynomial<T> >::type operator + (const U& a, polynomial<T> b)
  680. {
  681. b += a;
  682. return b;
  683. }
  684. template <class U, class T>
  685. inline typename boost::enable_if_c<boost::is_constructible<T, U>::value, polynomial<T> >::type operator - (const U& a, polynomial<T> b)
  686. {
  687. b -= a;
  688. return -b;
  689. }
  690. template <class U, class T>
  691. inline typename boost::enable_if_c<boost::is_constructible<T, U>::value, polynomial<T> >::type operator * (const U& a, polynomial<T> b)
  692. {
  693. b *= a;
  694. return b;
  695. }
  696. template <class T>
  697. bool operator == (const polynomial<T> &a, const polynomial<T> &b)
  698. {
  699. return a.data() == b.data();
  700. }
  701. template <class T>
  702. bool operator != (const polynomial<T> &a, const polynomial<T> &b)
  703. {
  704. return a.data() != b.data();
  705. }
  706. template <typename T, typename U>
  707. polynomial<T> operator >> (polynomial<T> a, const U& b)
  708. {
  709. a >>= b;
  710. return a;
  711. }
  712. template <typename T, typename U>
  713. polynomial<T> operator << (polynomial<T> a, const U& b)
  714. {
  715. a <<= b;
  716. return a;
  717. }
  718. // Unary minus (negate).
  719. template <class T>
  720. polynomial<T> operator - (polynomial<T> a)
  721. {
  722. std::transform(a.data().begin(), a.data().end(), a.data().begin(), detail::negate());
  723. return a;
  724. }
  725. template <class T>
  726. bool odd(polynomial<T> const &a)
  727. {
  728. return a.size() > 0 && a[0] != static_cast<T>(0);
  729. }
  730. template <class T>
  731. bool even(polynomial<T> const &a)
  732. {
  733. return !odd(a);
  734. }
  735. template <class T>
  736. polynomial<T> pow(polynomial<T> base, int exp)
  737. {
  738. if (exp < 0)
  739. return policies::raise_domain_error(
  740. "boost::math::tools::pow<%1%>",
  741. "Negative powers are not supported for polynomials.",
  742. base, policies::policy<>());
  743. // if the policy is ignore_error or errno_on_error, raise_domain_error
  744. // will return std::numeric_limits<polynomial<T>>::quiet_NaN(), which
  745. // defaults to polynomial<T>(), which is the zero polynomial
  746. polynomial<T> result(T(1));
  747. if (exp & 1)
  748. result = base;
  749. /* "Exponentiation by squaring" */
  750. while (exp >>= 1)
  751. {
  752. base *= base;
  753. if (exp & 1)
  754. result *= base;
  755. }
  756. return result;
  757. }
  758. template <class charT, class traits, class T>
  759. inline std::basic_ostream<charT, traits>& operator << (std::basic_ostream<charT, traits>& os, const polynomial<T>& poly)
  760. {
  761. os << "{ ";
  762. for(unsigned i = 0; i < poly.size(); ++i)
  763. {
  764. if(i) os << ", ";
  765. os << poly[i];
  766. }
  767. os << " }";
  768. return os;
  769. }
  770. } // namespace tools
  771. } // namespace math
  772. } // namespace boost
  773. //
  774. // Polynomial specific overload of gcd algorithm:
  775. //
  776. #include <boost/math/tools/polynomial_gcd.hpp>
  777. #endif // BOOST_MATH_TOOLS_POLYNOMIAL_HPP