vnl_real_npolynomial.h
Go to the documentation of this file.
1 // This is core/vnl/vnl_real_npolynomial.h
2 #ifndef vnl_real_npolynomial_h_
3 #define vnl_real_npolynomial_h_
4 //:
5 // \file
6 // \brief contains class for polynomials with N variables
7 //
8 // Implements a polynomial with N variables
9 //
10 // \author Marc Pollefeys, ESAT-VISICS, K.U.Leuven
11 // \date 1997-08-12
12 //
13 // \verbatim
14 // Modifications
15 // Peter Vanroose 1999-10-10 added simplify();
16 // determine nterms_ nvar_ ideg_ automatically
17 // Peter Vanroose 1999-10-20 Added operator+(), - * and std::ostream <<
18 // dac, Manchester 2001-03-15 Tidied up the documentation + added binary_io
19 // Lee Worden, Berkeley 2006-06-22 Minor fixes to simplify() and operator<<()
20 // Peter Vanroose 2006-06-24 Added method asString implementing oper<<()
21 // Peter Vanroose 2006-06-24 Bug fix in degree(), and added degrees() & maxdegree()
22 // Marcus Brubaker 2007-10-15 Added deval() and deriv() functions
23 // \endverbatim
24 
25 
26 //-----------------------------------------------------------------------------
27 
28 #include <vector>
29 #include <string>
30 #include <iosfwd>
31 #include <vnl/vnl_vector.h>
32 #include <vnl/vnl_matrix.h>
33 #ifdef _MSC_VER
34 # include <vcl_msvc_warnings.h>
35 #endif
36 #include "vnl/vnl_export.h"
37 
38 //: real polynomial in N variables.
39 // vnl_real_npolynomial represents a polynomial in multiple variables.
40 // Used by vnl_rnpoly_solve which solves systems of polynomial equations.
41 // Representation: an N-omial (N terms) is represented by (1) a vector
42 // with the N coefficients (vnl_vector<double>), and (2) a matrix with
43 // N rows, the i-th row representing the exponents of term i, as follows:
44 // (vnl_matrix<int>) column k contains the (integer) exponent of variable
45 // k. Example: the polynomial $A X^3 + B XY + C Y^2 + D XY^2$ is
46 // represented by the coefficients vector [A B C D] and the exponents
47 // matrix
48 // \verbatim
49 // [3 0]
50 // [1 1]
51 // [0 2]
52 // [1 2].
53 // \endverbatim
54 
55 class VNL_EXPORT vnl_real_npolynomial
56 {
57  private:
58  //: coefficients
60  //: degrees of every term for every variable
62  //: number of variables = # columns of polyn_
63  unsigned int nvar_;
64  //: number of terms of polynomial
65  unsigned int nterms_;
66  //: max. degree of polynomial
67  unsigned int ideg_;
68 
69  friend class vnl_rnpoly_solve;
70 
71  public:
72 
73  // Constructor-----------------------------------------------------------------
74  vnl_real_npolynomial() : coeffs_(), polyn_(), nvar_(0), nterms_(0), ideg_(0) {} // don't use this: only here for the STL vector class.
75 
76  //: Construct the polynomial with coefficients vector c and with exponents matrix p
78 
79  // Computations--------------------------------------------------------------
80 
81  //: Evaluate the polynomial at x.
82  double eval(const vnl_vector<double>& x);
83  //: Evaluate the derivative of the polynomial at x with respect to the ith variable.
84  double deval(const vnl_vector<double>& x, unsigned int i);
85  //: Evaluate the gradient of the polynomial at x.
86  vnl_vector<double> deval(const vnl_vector<double>& x);
87  //: Differentiate the polynomial with respect to the ith variable.
88  vnl_real_npolynomial deriv(unsigned int i);
89 
90  vnl_real_npolynomial operator-() const; // unary minus
94  vnl_real_npolynomial& operator+=(vnl_real_npolynomial const& rhs);
95  vnl_real_npolynomial& operator-=(vnl_real_npolynomial const& rhs);
96  vnl_real_npolynomial& operator*=(vnl_real_npolynomial const& rhs);
97  vnl_real_npolynomial operator+(double ) const;
98  vnl_real_npolynomial operator-(double P) const { return operator+(-P); }
99  vnl_real_npolynomial operator*(double ) const;
100  vnl_real_npolynomial& operator*=(double P) { coeffs_ *= P; return *this; }
101  vnl_real_npolynomial operator/(double P) const { return operator*(1.0/P); }
102  vnl_real_npolynomial& operator/=(double P) { return operator*=(1.0/P); }
103  friend VNL_EXPORT std::ostream& operator<<(std::ostream& , vnl_real_npolynomial const& );
104 
105  // nb also added functions to access the coeffs_ member variable
106 
107  //--- Data Access------------------------------------------------------------
108 
109  //: Return the degree (highest total power of all terms) of the polynomial.
110  unsigned int degree() const;
111 
112  //: Return the highest degree of the polynomial in an individual variable.
113  unsigned int maxdegree() const { return ideg_; }
114 
115  //: Return the degrees (highest power of all terms) in each of the variables.
116  std::vector<unsigned int> degrees() const;
117 
118  //: Access to the polynomial coefficients
119  double& operator [] (unsigned int i) { return coeffs_[i]; }
120  //: Access to the polynomial coefficients
121  double operator [] (unsigned int i) const { return coeffs_[i]; }
122 
123  //: Return the vector of coefficients
124  const vnl_vector<double>& coefficients() const { return coeffs_; }
125  //: Return the vector of coefficients
126  vnl_vector<double>& coefficients() { return coeffs_; }
127 
128  //: Set vector of coefficients of each product
129  void set(const vnl_vector<double> & c, const vnl_matrix<unsigned int> & p);
130 
131  //: Return the polynomial matrix
132  // (ie specifying the variables in each product)
133  const vnl_matrix<unsigned int>& polyn() const { return polyn_; }
134 
135  //: Return the vector of coefficients
136  vnl_matrix<unsigned int>& polyn() { return polyn_; }
137 
138  //: Return the textual representation of this polynomial
139  std::string asString() const;
140 
141  private:
142  void simplify();
143  double eval(const vnl_matrix<double>& xn);
144 };
145 
146 VNL_EXPORT std::ostream& operator<<(std::ostream& , vnl_real_npolynomial const& );
147 
148 #endif // vnl_real_npolynomial_h_
unsigned int nterms_
number of terms of polynomial.
unsigned int nvar_
number of variables = # columns of polyn_.
vnl_real_npolynomial & operator/=(double P)
An ordinary mathematical matrix.
vnl_vector< double > & coefficients()
Return the vector of coefficients.
const vnl_vector< double > & coefficients() const
Return the vector of coefficients.
vnl_real_npolynomial operator/(double P) const
vnl_vector< T > operator *(vnl_matrix_inverse< T > const &i, vnl_vector< T > const &B)
Solves for roots of system of real polynomials.
std::ostream & operator<<(std::ostream &s, vnl_decnum const &r)
decimal output.
Definition: vnl_decnum.h:393
unsigned int maxdegree() const
Return the highest degree of the polynomial in an individual variable.
vnl_matrix< unsigned int > & polyn()
Return the vector of coefficients.
vnl_bignum operator-(vnl_bignum const &r1, vnl_bignum const &r2)
Returns the difference of two bignum numbers.
Definition: vnl_bignum.h:290
unsigned int ideg_
max. degree of polynomial.
real polynomial in N variables.
vnl_real_npolynomial operator-(double P) const
vnl_bignum operator+(vnl_bignum const &r1, long r2)
Returns the sum of two bignum numbers.
Definition: vnl_bignum.h:279
const vnl_matrix< unsigned int > & polyn() const
Return the polynomial matrix.
vnl_matrix< unsigned int > polyn_
degrees of every term for every variable.
vnl_vector< double > coeffs_
coefficients.
VNL_EXPORT std::ostream & operator<<(std::ostream &, vnl_real_npolynomial const &)