2 #ifndef vnl_polynomial_hxx_ 3 #define vnl_polynomial_hxx_ 16 # include <vcl_msvc_warnings.h> 24 typename std::vector<T>::const_iterator i = coeffs_.begin();
25 if (i == coeffs_.end())
return T(0);
28 for (++i; i!=coeffs_.end(); ++i) {
39 std::vector<T> neg = coeffs_;
40 typename std::vector<T>::iterator i = neg.begin();
41 for (; i!=neg.end(); ++i) *i = - *i;
50 int d=degree(), d2=f.
degree();
51 std::vector<T> sum = coeffs_;
52 for (
int i=0;i<=d&&i<=d2;++i) sum[i]+=f[i];
53 for (
int i=d+1;i<=d2;++i) sum.push_back(f[i]);
55 while (sum.end() != sum.begin() && sum.back() == T(0)) sum.pop_back();
63 int d1=degree(), d2=f.
degree(), d = d1+d2;
65 std::vector<T> prod(d+1, T(0));
66 for (
int i=0;i<=d1;++i)
67 for (
int j=0;j<=d2;++j)
68 prod[i+j] += coeffs_[i]*f[j];
78 int d1=degree(), d2=f.
degree(), d=d1-d2;
79 assert (d2 >= 0 && f[d2] != T(0));
82 for (
int i=0;i<=d;++i) {
83 T acc = coeffs_[d1-i];
84 for (
int j=0;j<d2&&j<i;++j) acc -= quot[j] * f[d2-j-1];
85 quot.insert(quot.begin(), 1, acc/f[d2]);
97 if (quot.
degree() < 0)
return *
this;
101 for (
int i=0; i<n; ++i) diff.push_back(coeffs_[i] - prod[i]);
103 while (diff.end() != diff.begin() && diff.back() == T(0)) diff.pop_back();
112 typename std::vector<T>::const_iterator i = coeffs_.begin();
114 for (++i; i!=coeffs_.end(); ++i,++n)
115 cd.push_back(*i * n);
128 typename std::vector<T>::const_iterator i = coeffs_.begin();
129 for (++n; i!=coeffs_.end(); ++i,++n)
130 cd.push_back(*i / n);
137 bool first_coeff =
true;
139 for (
int i=degree(); i >= 0; --i) {
140 if (coeffs_[i] == T(0))
continue;
142 if (coeffs_[i] > T(0) && !first_coeff) os <<
'+';
143 if (i==0) os << coeffs_[i];
144 else if (coeffs_[i] == -T(1)) os <<
'-';
145 else if (coeffs_[i] != T(1)) os << coeffs_[i] <<
' ';
146 if (i == 1) os <<
'X';
147 else if (i != 0) os <<
"X^" << i;
150 if (first_coeff) os <<
" 0";
153 #undef VNL_POLYNOMIAL_INSTANTIATE 154 #define VNL_POLYNOMIAL_INSTANTIATE(T) \ 155 template class VNL_EXPORT vnl_polynomial<T >; \ 156 template VNL_EXPORT std::ostream& operator<<(std::ostream& os, vnl_polynomial<T > const&) 158 #endif // vnl_polynomial_hxx_ vnl_polynomial< T > operator+(vnl_polynomial< T > const &f) const
Returns polynomial which is sum of this with polynomial f.
vnl_polynomial< T > operator/(vnl_polynomial< T > const &f) const
Returns polynomial which is the result of the long division by polynomial f.
vnl_polynomial< T > operator%(vnl_polynomial< T > const &f) const
Returns polynomial which is the remainder after a long division by polynomial f.
void print(std::ostream &os) const
Print this polynomial to stream.
Evaluation of univariate polynomials Templated class (on the data type of the coefficients),...
vnl_polynomial< T > derivative() const
Return derivative of this polynomial.
vnl_polynomial< T > operator-() const
Returns negative of this polynomial.
vnl_polynomial< T > operator *(vnl_polynomial< T > const &f) const
Returns polynomial which is product of this with polynomial f.
vnl_polynomial< T > primitive() const
Return primitive function (inverse derivative) of this polynomial.
T evaluate(T const &x) const
Evaluate polynomial at value x.
vnl_bignum operator/(vnl_bignum const &r1, vnl_bignum const &r2)
Returns the division of two bignum numbers.
int degree() const
Return the degree (highest power of X) of the polynomial.
Evaluation of polynomials.