Polynomial.h

00001 #ifndef POLYNOMIAL_H_JAN_6_2007
00002 #define POLYNOMIAL_H_JAN_6_2007
00003 
00004 #include <iostream>
00005 #include <map>
00006 #include <string>
00007 
00008 
00009 #include <sstream>
00010 #include "array.h"
00011 
00012 
00013 using namespace std;
00014 
00015 typedef map<int, double> VarCont;
00016 
00017 struct DoubleRange {
00018         DoubleRange() {
00019                 low = 0;
00020                 high = 0;
00021                 iType = 0;
00022         }
00023         double low;
00024         double high;
00025         // Type of the double range:
00026         // 0 - inward range, 1 - outward range, 2 - 1 point range, 3 - empty range.
00027         int iType; 
00028 };
00029 
00030 template <typename Type>
00031 bool ArrayCompare(Array<Type> const & a1, Array<Type> const & a2);
00032 
00033 class PolyNomial 
00034 {
00035 public: 
00036         PolyNomial();
00037         PolyNomial(const PolyNomial& pl);
00038         PolyNomial(PolyNomial& pl);
00039         ~PolyNomial();
00040         
00041         void Clear();
00042         
00043         // Explicit copy function.
00044         void Copy(PolyNomial& pl);
00045         void Copy(const PolyNomial& pl);
00046 
00047         // operator=
00048         void operator=(PolyNomial& pl);
00049         void operator=(const PolyNomial& pl);
00050 
00051         // Compute the value of the polynomial, given values for the variables.
00052         double ComputePlValue(const Array<double>& vars);
00053         double ComputePlValue();
00054 
00055         // Compute the polynomial's first-order derivative (a polynomial) at the given variable.
00056         PolyNomial GetGradient(int varInIdx);
00057         // Compute the polynomial's first-order derivative values for each variable.
00058         Array<double> GetGradient();
00059 
00060         // Normalize the polynomial to canonical base form, merge items with similar bases, etc.
00061         void Normalize();
00062         // Normalize the polynomial to log Gaussian format (x-a)^2 and return true, if not single-var quadratic, return false.
00063         bool NormalizeGaussian();
00064 
00065         // i.o functions.
00066         void ReadFrom(istream& is);  // Read polynomial from input stream.
00067         void PrintVars(ostream& os) const;  // Print the polynomial variables to stream.
00068         void PrintTo(ostream& os) const;  // Print the polynomial to stream.
00069         void PrintCoef(ostream& os, double coef) const;  // Print the coefficient value, dealing with the two number sign cases.
00070         
00071         // Polynomial computation functions.
00072         void MultiplyConst(double coef);  // Multiply the polynomial with a constant value.
00073         // Assume the polynomial is representing a single variable Gaussian
00074         // this function returns the paramters to the Gaussian distribution represented by the polynomial.
00075         void GetGaussianPara(double* miu, double* stdev);
00076         void AddPl(const PolyNomial& pl);
00077 
00078         // Return the highest order of the given variable.
00079         double GetHighestOrder(int varInIdx);
00080 
00081         // Add variable to polynomial and keep the polynomial unchanged.
00082         bool AddVar(const string& var) {
00083                 if (varsearch_.find(var) != varsearch_.end())
00084                 {
00085                         return false;
00086                 }
00087                 varsearch_[var] = numVar_;
00088                 numVar_++;              
00089                 var_.append(var);
00090                 varValue_.append(0.0);  // Default value for the new variable is set to 0.
00091                 return true;
00092         }
00093 
00094         bool AddItem(const VarCont& item, double coef) {
00095                 VarCont::const_iterator cit = item.begin();
00096                 for (; cit != item.end(); ++cit) {
00097                         int var_id = cit->first;
00098                         if (var_id >= numVar_) return false;
00099                 }
00100                 numItems_ ++;
00101                 coef_.append(coef);
00102                 items_.append(item);
00103                 strItems_.append(GenerateItemString(item));
00104                 Normalize();
00105                 return true;
00106         }
00107 
00108         void SetVarName(const Array<string>& varName) {
00109                 assert(varName.size()== numVar_);
00110                 var_.clear();
00111                 var_.append(varName);
00112 
00113                 varsearch_.clear();
00114                 for(int i = 0; i < var_.size(); ++i) {
00115                         varsearch_[var_[i]] = i;
00116                 }
00117         }
00118 
00119         // Set values of variables.
00120         void SetVarValue(const Array<double>& varValue) {
00121                 assert(varValue.size()== numVar_);
00122                 varValue_.clear();
00123                 varValue_.append(varValue);
00124         }
00125 
00126         void SetConstantValue(double val) {
00127                 constValue_ = val;
00128         }
00129         double GetConstantValue() {
00130                 return constValue_;
00131         }
00132         // Reduce the polynomial to the polynomial over one specific variable, given the values of the rest.
00133         void ReduceToOneVar(const Array<double>& vars, int varIdx);
00134         void ReduceToOneVar(int varIdx);
00135 
00136     // Generate the string of specific item in the polynomial. 
00137         string GenerateItemString(const VarCont& vc);
00138         
00139         // Solve quadratic polynomial constraint.
00140         DoubleRange SolvePoly2(double d);  //assume it's a GREATER THAN (">=") polynomial constraints
00141         DoubleRange SolvePoly2(double a, double b, double c, double d);  //c != 0, constraint type a + b*x +c*x^2 >= d
00142         
00143         // Do quadratic optimization of the quadratic polynomials, and return the value of optimized polynomial.
00144         double QuadraticOptimization();
00145 
00146         const Array<double>& GetVarValue() const {return varValue_;}
00147         double GetVarValue(int varIdx) {return varValue_[varIdx];}
00148         void ClearVarValue() {varValue_.clear();}
00149         void AppendVarValue(double value) {varValue_.append(value);}
00150         const int GetVarNum() const {return numVar_;}
00151         
00152         const string& GetVarAt(int varIdx) const {return var_[varIdx];}
00153 
00154         int GetVarIdx(string& var) const {
00155                 map<string, int>::const_iterator citer = varsearch_.find(var);
00156                 if (citer != varsearch_.end()) 
00157                         return citer->second;
00158                 else return -1;
00159         }
00160 
00161         const double& GetCoef(int varIdx) const { return coef_[varIdx];}
00162         const Array<string> & GetVar() const {return var_;}
00163         const map<string, int>& GetVarSearch() const{ return varsearch_;}
00164 
00165         // Compute the value of the given item, given the values of variables.
00166         double ComputeItem(int itemIdx);
00167         bool IsQuadratic();  // Check if the polynomial is a log Gaussian term, i.e., if it's a quadratic polynomial.
00168         int Optimize2(Array<double>* vars, double* optValue);  // Optimizing the polynomial when it's a quadratic one. Values returned: assignment to variables and the optimum value of the polynomial.
00169         // General optimization interface for arbitrary polynomial is implemented in function minimize in lbfgsp.h
00170 
00171         // Parameters for quadratic polynomials.
00172         struct QuadraticPolyPara {
00173                 double a; //constant
00174                 double b; // coefficient of 1 order term
00175                 double c; // coefficient of 2 order term
00176         };
00177         Array<double> varValue_;  // Holder for value assignment to variables.
00178 public: 
00179         int numVar_;
00180         int numItems_;
00181         double constValue_;     
00182         QuadraticPolyPara gpara_;  // Parameters for quadratic polynomial representations.
00183         Array<string> strItems_;  // Holder for each item's string representation.      
00184         Array<double> coef_;  // From constant to higher orders, coef_[0] is always 1. ??
00185         Array<VarCont> items_;  // Coefficient descriptors of different terms in a polynomial, each element in this vector is a map from variable index to its order value in the term.
00186         Array<string> var_;  // We assume within a polynomial different variables have different names.
00187         map<string, int> varsearch_;  // Map from variable string to its index.
00188 };
00189 
00190 #endif

Generated on Sun Jun 7 11:55:17 2009 for Alchemy by  doxygen 1.5.1