core/vnl/vnl_real_polynomial.h

Go to the documentation of this file.
00001 // This is core/vnl/vnl_real_polynomial.h
00002 #ifndef vnl_real_polynomial_h_
00003 #define vnl_real_polynomial_h_
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007 //:
00008 // \file
00009 // \brief Evaluation of real polynomials
00010 // \author Andrew W. Fitzgibbon, Oxford RRG
00011 // \date   06 Aug 96
00012 //
00013 // \verbatim
00014 // Modifications
00015 // 23 may 97, Peter Vanroose - "NO_COMPLEX" option added (until "complex" type is standardised)
00016 // 27/03/2001 Ian Scott and Tim Cootes - Added Binary IO
00017 // 27/03/2001 Ian Scott - Comments tidied up
00018 // 25/11/2001 Peter Vanroose - added operator==(), derivative(), primitive(), print()
00019 // 12/22/2004 Kongbin Kang - add structured comment for operator==()
00020 // \endverbatim
00021 
00022 #include <vnl/vnl_vector.h>
00023 #include <vcl_complex.h>
00024 #include <vcl_iosfwd.h>
00025 #include <vcl_cassert.h>
00026 
00027 //:Evaluation of real polynomials at real and complex points.
00028 //    vnl_real_polynomial represents a univariate polynomial with real
00029 //    coefficients, stored as a vector of doubles.  This allows
00030 //    evaluation of the polynomial $p(x)$ at given values of $x$,
00031 //    or of its derivative $p'(x)$.
00032 //
00033 //    Roots may be extracted using the roots() method.
00034 class vnl_real_polynomial
00035 {
00036  public:
00037   //: Initialize polynomial.
00038   // The polynomial is $ a[0] x^d + a[1] x^{d-1} + \cdots + a[d] = 0 $.
00039   vnl_real_polynomial(vnl_vector<double> const & a): coeffs_(a) {
00040     if (a.empty()) { coeffs_.set_size(1); coeffs_(0)=0.0; }
00041   }
00042 
00043   //: Initialize polynomial from C vector.
00044   // The parameter len is the number
00045   // of coefficients, one greater than the degree.
00046   vnl_real_polynomial(double const * a, unsigned len): coeffs_(a, len) {
00047     if (len==0) { coeffs_.set_size(1); coeffs_(0)=0.0; }
00048   }
00049 
00050   //: Initialize polynomial from double.
00051   // Useful when adding or multiplying a polynomial and a number.
00052   vnl_real_polynomial(double a): coeffs_(1u, a) {}
00053 
00054   //: Initialize polynomial of a given degree.
00055   vnl_real_polynomial(int d): coeffs_(d+1) { assert (d>=0); }
00056 
00057   //: comparison operator
00058   bool operator==(vnl_real_polynomial const& p) const { return p.coefficients() == coeffs_; }
00059 
00060   //: Evaluate polynomial at value x
00061   double evaluate(double x) const;
00062 
00063   //: Evaluate integral at x (assuming constant of integration is zero)
00064   double evaluate_integral(double x) const;
00065 
00066   //: Evaluate integral between x1 and x2
00067   double evaluate_integral(double x1, double x2) const;
00068 
00069   //: Evaluate derivative at value x
00070   double devaluate(double x) const;
00071 
00072   //: Evaluate polynomial at complex value x
00073   vcl_complex<double> evaluate(vcl_complex<double> const& x) const;
00074 
00075 
00076   //: Evaluate derivative at complex value x
00077   vcl_complex<double> devaluate(vcl_complex<double> const& x) const;
00078 
00079   //: Return derivative of this polynomial
00080   vnl_real_polynomial derivative() const;
00081 
00082   //: Return primitive function (inverse derivative) of this polynomial
00083   // Since a primitive function is not unique, the one with constant = 0 is returned
00084   vnl_real_polynomial primitive() const;
00085 
00086   // Data Access---------------------------------------------------------------
00087 
00088   //: Return the degree (highest power of x) of the polynomial.
00089   int     degree() const { return int(coeffs_.size()) - 1; }
00090 
00091   //: Access to the polynomial coefficients
00092   double& operator [] (int i)       { return coeffs_[i]; }
00093   //: Access to the polynomial coefficients
00094   double  operator [] (int i) const { return coeffs_[i]; }
00095 
00096   //: Return the vector of coefficients
00097   const vnl_vector<double>& coefficients() const { return coeffs_; }
00098   //: Return the vector of coefficients
00099         vnl_vector<double>& coefficients()       { return coeffs_; }
00100 
00101   void set_coefficients(vnl_vector<double> const& coeffs) {coeffs_ = coeffs;}
00102 
00103   //: Print this polynomial to stream
00104   void print(vcl_ostream& os) const;
00105 
00106  protected:
00107   //: The coefficients of the polynomial.
00108   // coeffs_.back() is the const term.
00109   // coeffs_[n] is the coefficient of the x^(d-n) term,
00110   //    where d=coeffs_.size()-1
00111   // \invariant coeffs_size() >= 1;
00112   vnl_vector<double> coeffs_;
00113 };
00114 
00115 //: Returns polynomial which is sum of two polynomials f1(x)+f2(x)
00116 // \relates vnl_real_polynomial
00117 vnl_real_polynomial operator+(const vnl_real_polynomial& f1, const vnl_real_polynomial& f2);
00118 
00119 //: Returns polynomial which is different of two polynomials f1(x)-f2(x)
00120 // \relates vnl_real_polynomial
00121 vnl_real_polynomial operator-(const vnl_real_polynomial& f1, const vnl_real_polynomial& f2);
00122 
00123 //: Returns polynomial which is product of two polynomials f1(x)*f2(x)
00124 vnl_real_polynomial operator*(const vnl_real_polynomial& f1, const vnl_real_polynomial& f2);
00125 
00126 //: Returns RMS difference between f1 and f2 over range [x1,x2]
00127 // $\frac1{\sqrt{|x_2-x_1|}}\,\sqrt{\int_{x_1}^{x_2}\left(f_1(x)-f_2(x)\right)^2\,dx}$
00128 // \relates vnl_real_polynomial
00129 double vnl_rms_difference(const vnl_real_polynomial& f1, const vnl_real_polynomial& f2,
00130                           double x1, double x2);
00131 
00132 #endif // vnl_real_polynomial_h_

Generated on Sat Nov 22 05:06:22 2008 for core/vnl by  doxygen 1.5.1