polynomial.hpp 21 KB

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