vnl_real_polynomial.h
Go to the documentation of this file.
1 // This is core/vnl/vnl_real_polynomial.h
2 #ifndef vnl_real_polynomial_h_
3 #define vnl_real_polynomial_h_
4 //:
5 // \file
6 // \brief Evaluation of real polynomials
7 // \author Andrew W. Fitzgibbon, Oxford RRG
8 // \date 06 Aug 96
9 //
10 // \verbatim
11 // Modifications
12 // 23 may 97, Peter Vanroose - "NO_COMPLEX" option added (until "complex" type is standardised)
13 // 27/03/2001 Ian Scott and Tim Cootes - Added Binary IO
14 // 27/03/2001 Ian Scott - Comments tidied up
15 // 25/11/2001 Peter Vanroose - added operator==(), derivative(), primitive(), print()
16 // 12/22/2004 Kongbin Kang - add structured comment for operator==()
17 // \endverbatim
18 
19 #include <complex>
20 #include <iosfwd>
21 #include <vnl/vnl_vector.h>
22 #ifdef _MSC_VER
23 # include <vcl_msvc_warnings.h>
24 #endif
25 #include <cassert>
26 #include "vnl/vnl_export.h"
27 
28 //:Evaluation of real polynomials at real and complex points.
29 // vnl_real_polynomial represents a univariate polynomial with real
30 // coefficients, stored as a vector of doubles. This allows
31 // evaluation of the polynomial $p(x)$ at given values of $x$,
32 // or of its derivative $p'(x)$.
33 //
34 // The coefficients (coeffs_) are stored as a vnl_vector, where
35 // coeffs_[n] is the coefficient of the x^(d-n) term,
36 // where d is the degree of the polynomial. Otherwise said,
37 // the coefficients are stored starting with the highest degree term.
38 //
39 // Roots may be extracted using the roots() method.
40 class VNL_EXPORT vnl_real_polynomial
41 {
42  public:
43  //: Initialize polynomial.
44  // The polynomial is $ a[0] x^d + a[1] x^{d-1} + \cdots + a[d] = 0 $.
45  vnl_real_polynomial(vnl_vector<double> const & a): coeffs_(a) {
46  if (a.empty()) { coeffs_.set_size(1); coeffs_(0)=0.0; }
47  }
48 
49  //: Initialize polynomial from C vector.
50  // The parameter len is the number
51  // of coefficients, one greater than the degree.
52  vnl_real_polynomial(double const * a, unsigned len): coeffs_(a, len) {
53  if (len==0) { coeffs_.set_size(1); coeffs_(0)=0.0; }
54  }
55 
56  //: Initialize polynomial from double.
57  // Useful when adding or multiplying a polynomial and a number.
58  vnl_real_polynomial(double a): coeffs_(1u, a) {}
59 
60  //: Initialize polynomial of a given degree.
61  vnl_real_polynomial(int d): coeffs_(static_cast<unsigned int>(d)+1u) { assert (d>=0); }
62  vnl_real_polynomial(unsigned int d): coeffs_(d+1u) { }
63 
64  //: comparison operator
65  bool operator==(vnl_real_polynomial const& p) const { return p.coefficients() == coeffs_; }
66 
67  //: Evaluate polynomial at value x
68  double evaluate(double x) const;
69 
70  //: Evaluate integral at x (assuming constant of integration is zero)
71  double evaluate_integral(double x) const;
72 
73  //: Evaluate integral between x1 and x2
74  double evaluate_integral(double x1, double x2) const;
75 
76  //: Evaluate derivative at value x
77  double devaluate(double x) const;
78 
79  //: Evaluate polynomial at complex value x
80  std::complex<double> evaluate(std::complex<double> const& x) const;
81 
82 
83  //: Evaluate derivative at complex value x
84  std::complex<double> devaluate(std::complex<double> const& x) const;
85 
86  //: Return derivative of this polynomial
87  vnl_real_polynomial derivative() const;
88 
89  //: Return primitive function (inverse derivative) of this polynomial
90  // Since a primitive function is not unique, the one with constant = 0 is returned
91  vnl_real_polynomial primitive() const;
92 
93  //: Add rhs to this and return *this
94  vnl_real_polynomial& operator+=(vnl_real_polynomial const& rhs);
95 
96  //: Subtract rhs from this and return *this
97  vnl_real_polynomial& operator-=(vnl_real_polynomial const& rhs);
98 
99  //: Multiply rhs with this and return *this
100  vnl_real_polynomial& operator*=(vnl_real_polynomial const& rhs);
101 
102  // Data Access---------------------------------------------------------------
103 
104  //: Return the degree (highest power of x) of the polynomial.
105  int degree() const { return int(coeffs_.size()) - 1; }
106 
107  //: Access to the polynomial coefficients
108  double& operator [] (int i) { return coeffs_[i]; }
109  //: Access to the polynomial coefficients
110  double operator [] (int i) const { return coeffs_[i]; }
111 
112  //: Return the vector of coefficients
113  const vnl_vector<double>& coefficients() const { return coeffs_; }
114  //: Return the vector of coefficients
115  vnl_vector<double>& coefficients() { return coeffs_; }
116 
117  void set_coefficients(vnl_vector<double> const& coeffs) {coeffs_ = coeffs;}
118 
119  //: Print this polynomial to stream
120  void print(std::ostream& os) const;
121 
122  protected:
123  //: The coefficients of the polynomial.
124  // coeffs_.back() is the const term.
125  // coeffs_[n] is the coefficient of the x^(d-n) term,
126  // where d=coeffs_.size()-1
127  // \invariant coeffs_size() >= 1;
129 };
130 
131 //: Returns polynomial which is sum of two polynomials f1(x)+f2(x)
132 // \relatesalso vnl_real_polynomial
134 
135 //: Returns polynomial which is different of two polynomials f1(x)-f2(x)
136 // \relatesalso vnl_real_polynomial
138 
139 //: Returns polynomial which is product of two polynomials f1(x)*f2(x)
141 
142 //: Returns RMS difference between f1 and f2 over range [x1,x2]
143 // $\frac1{\sqrt{|x_2-x_1|}}\,\sqrt{\int_{x_1}^{x_2}\left(f_1(x)-f_2(x)\right)^2\,dx}$
144 // \relatesalso vnl_real_polynomial
145 VNL_EXPORT double vnl_rms_difference(const vnl_real_polynomial& f1, const vnl_real_polynomial& f2,
146  double x1, double x2);
147 
148 #endif // vnl_real_polynomial_h_
vnl_bignum operator+(vnl_bignum const &r1, long r2)
Returns the sum of two bignum numbers.
Definition: vnl_bignum.h:279
vnl_vector< double > & coefficients()
Return the vector of coefficients.
vnl_real_polynomial(vnl_vector< double > const &a)
Initialize polynomial.
bool operator==(vnl_real_polynomial const &p) const
comparison operator.
const vnl_vector< double > & coefficients() const
Return the vector of coefficients.
Evaluation of real polynomials at real and complex points.
vnl_real_polynomial(double a)
Initialize polynomial from double.
VNL_EXPORT double vnl_rms_difference(const vnl_real_polynomial &f1, const vnl_real_polynomial &f2, double x1, double x2)
Returns RMS difference between f1 and f2 over range [x1,x2].
vnl_real_polynomial(double const *a, unsigned len)
Initialize polynomial from C vector.
void set_coefficients(vnl_vector< double > const &coeffs)
vnl_real_polynomial(int d)
Initialize polynomial of a given degree.
vnl_bignum operator-(vnl_bignum const &r1, vnl_bignum const &r2)
Returns the difference of two bignum numbers.
Definition: vnl_bignum.h:290
vnl_vector< double > coeffs_
The coefficients of the polynomial.
vnl_bignum operator *(vnl_bignum const &r1, vnl_bignum const &r2)
Returns the product of two bignum numbers.
Definition: vnl_bignum.h:302
bool empty() const
Return true iff the size is zero.
Definition: vnl_vector.h:368
int degree() const
Return the degree (highest power of x) of the polynomial.
vnl_real_polynomial(unsigned int d)