core/vnl/vnl_real_npolynomial.h
Go to the documentation of this file.
00001 // This is core/vnl/vnl_real_npolynomial.h
00002 #ifndef vnl_real_npolynomial_h_
00003 #define vnl_real_npolynomial_h_
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007 //:
00008 // \file
00009 // \brief contains class for polynomials with N variables
00010 //
00011 // Implements a polynomial with N variables
00012 //
00013 // \author Marc Pollefeys, ESAT-VISICS, K.U.Leuven
00014 // \date   1997-08-12
00015 //
00016 // \verbatim
00017 //  Modifications
00018 //   Peter Vanroose   1999-10-10  added simplify();
00019 //                                determine nterms_ nvar_ ideg_ automatically
00020 //   Peter Vanroose   1999-10-20  Added operator+(), - * and vcl_ostream <<
00021 //   dac, Manchester  2001-03-15  Tidied up the documentation + added binary_io
00022 //   Lee Worden, Berkeley 2006-06-22 Minor fixes to simplify() and operator<<()
00023 //   Peter Vanroose   2006-06-24  Added method asString implementing oper<<()
00024 //   Peter Vanroose   2006-06-24  Bug fix in degree(), and added degrees() & maxdegree()
00025 //   Marcus Brubaker  2007-10-15  Added deval() and deriv() functions
00026 // \endverbatim
00027 
00028 
00029 //-----------------------------------------------------------------------------
00030 
00031 #include <vnl/vnl_vector.h>
00032 #include <vnl/vnl_matrix.h>
00033 #include <vcl_vector.h>
00034 #include <vcl_string.h>
00035 #include <vcl_iosfwd.h>
00036 
00037 //: real polynomial in N variables.
00038 //    vnl_real_npolynomial represents a polynomial in multiple variables.
00039 //    Used by vnl_rnpoly_solve which solves systems of polynomial equations.
00040 //    Representation:  an N-omial (N terms) is represented by (1) a vector
00041 //    with the N coefficients (vnl_vector<double>), and (2) a matrix with
00042 //    N rows, the i-th row representing the exponents of term i, as follows:
00043 //    (vnl_matrix<int>) column k contains the (integer) exponent of variable
00044 //    k.  Example: the polynomial $A X^3 + B XY + C Y^2 + D XY^2$ is
00045 //    represented by the coefficients vector [A B C D] and the exponents
00046 //    matrix
00047 //  \verbatim
00048 //    [3 0]
00049 //    [1 1]
00050 //    [0 2]
00051 //    [1 2].
00052 //  \endverbatim
00053 
00054 class vnl_real_npolynomial
00055 {
00056   //: coefficients
00057   vnl_vector<double>       coeffs_;
00058   //: degrees of every term for every variable
00059   vnl_matrix<unsigned int> polyn_;
00060   //: number of variables = # columns of polyn_
00061   unsigned int             nvar_;
00062   //: number of terms of polynomial
00063   unsigned int             nterms_;
00064   //: max. degree of polynomial
00065   unsigned int             ideg_;
00066 
00067   friend class vnl_rnpoly_solve;
00068 
00069  public:
00070 
00071   // Constructor-----------------------------------------------------------------
00072   vnl_real_npolynomial() : coeffs_(), polyn_(), nvar_(0), nterms_(0), ideg_(0) {} // don't use this: only here for the STL vector class.
00073 
00074   //: Construct the polynomial with coefficients vector c and with exponents matrix p
00075   vnl_real_npolynomial(const vnl_vector<double>& c, const vnl_matrix<unsigned int>& p);
00076 
00077   // Computations--------------------------------------------------------------
00078 
00079   //: Evaluate the polynomial at x.
00080   double eval(const vnl_vector<double>& x);
00081   //: Evaluate the derivative of the polynomial at x with respect to the ith variable.
00082   double deval(const vnl_vector<double>& x, unsigned int i);
00083   //: Evaluate the gradient of the polynomial at x.
00084   vnl_vector<double> deval(const vnl_vector<double>& x);
00085   //: Differentiate the polynomial with respect to the ith variable.
00086   vnl_real_npolynomial deriv(unsigned int i);
00087 
00088   vnl_real_npolynomial operator-() const; // unary minus
00089   vnl_real_npolynomial operator+(vnl_real_npolynomial const& ) const;
00090   vnl_real_npolynomial operator-(vnl_real_npolynomial const& ) const;
00091   vnl_real_npolynomial operator*(vnl_real_npolynomial const& ) const;
00092   vnl_real_npolynomial& operator+=(vnl_real_npolynomial const& rhs);
00093   vnl_real_npolynomial& operator-=(vnl_real_npolynomial const& rhs);
00094   vnl_real_npolynomial& operator*=(vnl_real_npolynomial const& rhs);
00095   vnl_real_npolynomial operator+(double ) const;
00096   vnl_real_npolynomial operator-(double P) const { return operator+(-P); }
00097   vnl_real_npolynomial operator*(double ) const;
00098   vnl_real_npolynomial& operator*=(double P) { coeffs_ *= P; return *this; }
00099   vnl_real_npolynomial operator/(double P) const { return operator*(1.0/P); }
00100   vnl_real_npolynomial& operator/=(double P) { return operator*=(1.0/P); }
00101   friend vcl_ostream& operator<<(vcl_ostream& , vnl_real_npolynomial const& );
00102 
00103   // nb also added functions to access the coeffs_ member variable
00104 
00105   //--- Data Access------------------------------------------------------------
00106 
00107   //: Return the degree (highest total power of all terms) of the polynomial.
00108   unsigned int degree() const;
00109 
00110   //: Return the highest degree of the polynomial in an individual variable.
00111   unsigned int maxdegree() const { return ideg_; }
00112 
00113   //: Return the degrees (highest power of all terms) in each of the variables.
00114   vcl_vector<unsigned int> degrees() const;
00115 
00116   //: Access to the polynomial coefficients
00117   double& operator [] (unsigned int i)       { return coeffs_[i]; }
00118   //: Access to the polynomial coefficients
00119   double  operator [] (unsigned int i) const { return coeffs_[i]; }
00120 
00121   //: Return the vector of coefficients
00122   const vnl_vector<double>& coefficients() const { return coeffs_; }
00123   //: Return the vector of coefficients
00124   vnl_vector<double>& coefficients()       { return coeffs_; }
00125 
00126   //: Set vector of coefficients of each product
00127   void set(const vnl_vector<double> & c, const vnl_matrix<unsigned int> & p);
00128 
00129   //: Return the polynomial matrix
00130   // (ie specifying the variables in each product)
00131   const vnl_matrix<unsigned int>& polyn() const { return polyn_; }
00132 
00133   //: Return the vector of coefficients
00134   vnl_matrix<unsigned int>& polyn() { return polyn_; }
00135 
00136   //: Return the textual representation of this polynomial
00137   vcl_string asString() const;
00138 
00139  private:
00140   void simplify();
00141   double eval(const vnl_matrix<double>& xn);
00142 };
00143 
00144 #endif // vnl_real_npolynomial_h_