polynomial.qbk 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. [section:polynomials Polynomials]
  2. [h4 Synopsis]
  3. ``
  4. #include <boost/math/tools/polynomial.hpp>
  5. ``
  6. namespace boost { namespace math {
  7. namespace tools {
  8. template <class T>
  9. class polynomial
  10. {
  11. public:
  12. // typedefs:
  13. typedef typename std::vector<T>::value_type value_type;
  14. typedef typename std::vector<T>::size_type size_type;
  15. // construct:
  16. polynomial(){}
  17. template <class U>
  18. polynomial(const U* data, unsigned order);
  19. template <class I>
  20. polynomial(I first, I last);
  21. template <class U>
  22. explicit polynomial(const U& point,
  23. typename boost::enable_if<boost::is_convertible<U, T> >::type* = 0);
  24. template <class Range>
  25. explicit polynomial(const Range& r,
  26. typename boost::enable_if<boost::math::tools::detail::is_const_iterable<Range> >::type* = 0); // C++14
  27. polynomial(std::initializer_list<T> l); // C++11
  28. polynomial(std::vector<T>&& p);
  29. // access:
  30. size_type size()const;
  31. size_type degree()const;
  32. value_type& operator[](size_type i);
  33. const value_type& operator[](size_type i)const;
  34. std::vector<T> const& data() const;
  35. std::vector<T>& data();
  36. explicit operator bool() const;
  37. polynomial<T> prime() const;
  38. polynomial<T> integrate() const;
  39. T operator()(T z) const;
  40. // modify:
  41. void set_zero();
  42. // operators:
  43. template <class U>
  44. polynomial& operator +=(const U& value);
  45. template <class U>
  46. polynomial& operator -=(const U& value);
  47. template <class U>
  48. polynomial& operator *=(const U& value);
  49. template <class U>
  50. polynomial& operator /=(const U& value);
  51. template <class U>
  52. polynomial& operator %=(const U& value);
  53. template <class U>
  54. polynomial& operator +=(const polynomial<U>& value);
  55. template <class U>
  56. polynomial& operator -=(const polynomial<U>& value);
  57. template <class U>
  58. polynomial& operator *=(const polynomial<U>& value);
  59. template <class U>
  60. polynomial& operator /=(const polynomial<U>& value);
  61. template <class U>
  62. polynomial& operator %=(const polynomial<U>& value);
  63. };
  64. template <class T>
  65. polynomial<T> operator + (const polynomial<T>& a, const polynomial<T>& b);
  66. template <class T>
  67. polynomial<T> operator - (const polynomial<T>& a, const polynomial<T>& b);
  68. template <class T>
  69. polynomial<T> operator * (const polynomial<T>& a, const polynomial<T>& b);
  70. template <class T>
  71. polynomial<T> operator / (const polynomial<T>& a, const polynomial<T>& b);
  72. template <class T>
  73. polynomial<T> operator % (const polynomial<T>& a, const polynomial<T>& b);
  74. template <class T, class U>
  75. polynomial<T> operator + (const polynomial<T>& a, const U& b);
  76. template <class T, class U>
  77. polynomial<T> operator - (const polynomial<T>& a, const U& b);
  78. template <class T, class U>
  79. polynomial<T> operator * (const polynomial<T>& a, const U& b);
  80. template <class T, class U>
  81. polynomial<T> operator / (const polynomial<T>& a, const U& b);
  82. template <class T, class U>
  83. polynomial<T> operator % (const polynomial<T>& a, const U& b);
  84. template <class U, class T>
  85. polynomial<T> operator + (const U& a, const polynomial<T>& b);
  86. template <class U, class T>
  87. polynomial<T> operator - (const U& a, const polynomial<T>& b);
  88. template <class U, class T>
  89. polynomial<T> operator * (const U& a, const polynomial<T>& b);
  90. template <class T>
  91. polynomial<T> operator - (const polynomial<T>& a);
  92. template <class T>
  93. polynomial<T> operator >>= (const U& a);
  94. template <class T>
  95. polynomial<T> operator >> (polynomial<T> const &a, const U& b);
  96. template <class T>
  97. polynomial<T> operator <<= (const U& a);
  98. template <class T>
  99. polynomial<T> operator << (polynomial<T> const &a, const U& b);
  100. template <class T>
  101. bool operator == (const polynomial<T> &a, const polynomial<T> &b);
  102. template <class T>
  103. bool operator != (const polynomial<T> &a, const polynomial<T> &b);
  104. template <class T>
  105. polynomial<T> pow(polynomial<T> base, int exp);
  106. template <class charT, class traits, class T>
  107. std::basic_ostream<charT, traits>& operator <<
  108. (std::basic_ostream<charT, traits>& os, const polynomial<T>& poly);
  109. template <typename T>
  110. std::pair< polynomial<T>, polynomial<T> >
  111. quotient_remainder(const polynomial<T>& a, const polynomial<T>& b);
  112. } // namespace tools
  113. }} // namespace boost { namespace math
  114. [h4 Description]
  115. This is a somewhat trivial class for polynomial manipulation.
  116. See
  117. * [@https://en.wikipedia.org/wiki/Polynomial Polynomial] and
  118. * Donald E. Knuth, The Art of Computer Programming: Volume 2, Third edition, (1998)
  119. Chapter 4.6.1, Algorithm D: Division of polynomials over a field.
  120. Implementation is currently of the "naive" variety, with [bigo](N[super 2])
  121. multiplication, for example. This class should not be used in
  122. high-performance computing environments: it is intended for the
  123. simple manipulation of small polynomials, typically generated
  124. for special function approximation.
  125. It does has division for polynomials over a [@https://en.wikipedia.org/wiki/Field_%28mathematics%29 field]
  126. (here floating point, complex, etc)
  127. and over a unique factorization domain (integers).
  128. Division of polynomials over a field is compatible with
  129. [@https://en.wikipedia.org/wiki/Euclidean_algorithm Euclidean GCD].
  130. Division of polynomials over a UFD is compatible with the subresultant algorithm for GCD (implemented as subresultant_gcd), but a serious word of warning is required: the intermediate value swell of that algorithm will cause single-precision integral types to overflow very easily. So although the algorithm will work on single-precision integral types, an overload of the gcd function is only provided for polynomials with multi-precision integral types, to prevent nasty surprises. This is done somewhat crudely by disabling the overload for non-POD integral types.
  131. Advanced manipulations: the FFT, factorisation etc are
  132. not currently provided. Submissions for these are of course welcome :-)
  133. [h4:polynomial_examples Polynomial Arithmetic Examples]
  134. [import ../../example/polynomial_arithmetic.cpp]
  135. [polynomial_arithmetic_0]
  136. [polynomial_arithmetic_1]
  137. [polynomial_arithmetic_2]
  138. for output:
  139. [polynomial_output_1]
  140. [polynomial_arithmetic_3]
  141. for output
  142. [polynomial_output_2]
  143. [h5 Division, Quotient and Remainder]
  144. [polynomial_arithmetic_4]
  145. The source code is at [@../../example/polynomial_arithmetic.cpp polynomial_arithmetic.cpp]
  146. [endsect] [/section:polynomials Polynomials]
  147. [/
  148. Copyright 2006 John Maddock and Paul A. Bristow.
  149. Copyright 2015 Jeremy William Murphy.
  150. Distributed under the Boost Software License, Version 1.0.
  151. (See accompanying file LICENSE_1_0.txt or copy at
  152. http://www.boost.org/LICENSE_1_0.txt).
  153. ]