NLToolbox
|
00001 /* 00002 * PolynomialSystem.h 00003 * 00004 * Created on: Oct 14, 2010 00005 * Author: romain 00006 */ 00007 #include "time.h" 00008 #include "stdio.h" 00009 #include "stdlib.h" 00010 #include "math.h" 00011 #include "ostream" 00012 #include "vector" 00013 #include "iostream" 00014 #include "assert.h" 00015 #include "utils.h" 00016 //#include "Bbox.h" 00017 #include "Box.h" 00018 #include "DynamicalSystem.h" 00019 00020 #ifndef POLYNOMIALSYSTEM_H_ 00021 #define POLYNOMIALSYSTEM_H_ 00022 00023 namespace nltool { 00024 00025 using namespace std; 00026 00027 typedef std::vector<int> multiIndice; 00028 00029 struct Monom { 00030 Number scalar; 00031 multiIndice coef; 00032 00033 Monom(); 00034 Monom(int dim, Number s, multiIndice c); 00035 Monom(int dim, string strEq, bool sign); 00036 virtual ~Monom(); 00037 int dimension; 00038 Number eval(const Vector &x); 00039 Monom derivate(const int dx); 00040 00041 }; 00042 00043 struct Polynom { 00044 std::vector<Monom> monomials; 00045 00046 Polynom(); 00047 Polynom(int dim, std::string strEq); 00048 Polynom(int dim, const std::vector<Monom> monoms); 00049 00050 virtual ~Polynom(); 00051 00052 void clean(); 00053 00054 virtual Number eval(const Vector &x); 00055 00056 void linearInterpollation(stdVector point, stdVector &a, Number b); 00057 00058 Polynom derivate(const int dx); 00059 00060 Polynom replaceVariable(std::vector<Polynom> expression); 00061 00062 multiIndice computeMaxDegrees(); 00063 00064 bool containsMultiIndice(multiIndice i, int& position); 00065 00066 Number computeBernsteinCoefficient(multiIndice i, multiIndice l); 00067 00068 stdVector getBernsteinCoefficients(); 00069 00070 void computeAffinebound(stdVector al, Number bl, stdVector au, Number bu); 00071 00072 int dimension; 00073 00074 }; 00075 00076 class PolynomialSystem: public DynamicalSystem { 00077 public: 00086 PolynomialSystem(int dim, int deg, int nbMon, const bool stable, int s); 00087 00093 PolynomialSystem(int dim, std::vector<Polynom> polys); 00094 00101 PolynomialSystem(int dim, string polysys); 00102 00106 PolynomialSystem(); 00107 00111 virtual ~PolynomialSystem(); 00112 00118 Vector eval(const Vector &x); 00119 00125 DynamicalSystem& derivate(const unsigned dx); 00126 00136 Number maximize(const unsigned indice, const Template& constraintsA, 00137 const Vector& constraintsB, Vector& pt); 00138 00148 Number minimize(const unsigned indice, const Template& constraintsA, 00149 const Vector& constraintsB, Vector& pt); 00150 00157 void linearInterpollation(Vector point, Matrix &A, Vector &b); 00158 00165 Number eval(const Vector &x, const int indice); 00166 00173 Vector evalDiscretised(const Vector &x, Number dt); 00174 00183 Number curvature(const Vector &x, const int indice, const Vector &d1, 00184 const Vector &d2); 00185 00192 PolynomialSystem getDiscretizedSystem(Number dt); 00193 00200 PolynomialSystem replaceVariable(std::vector<Polynom> expression); 00201 00207 PolynomialSystem derivate(const int dx); 00208 00209 //################################# 00210 // Bernstein part 00211 //################################# 00212 00213 //Matrix computeBernsteinCoefficient(); 00214 // std::vector<Polynom> computeUnitBoxMap(Bbox box); 00215 multiIndice getMaxDegree(); 00216 00217 std::vector<Polynom> polynomials; 00218 multiIndice maxDegrees; 00219 00220 private: 00221 00222 const char* toString(); 00223 bool inline equal(PolynomialSystem s2) { 00224 return (std::string(toString()) == std::string(s2.toString())); 00225 } 00226 00227 static Number evalFunc(const int N, const Vector &x, 00228 const int indiceParameter, const Number sign, void *data) { 00229 PolynomialSystem *t = static_cast<PolynomialSystem*>(data); 00230 return t->eval(x, indiceParameter); 00231 } 00232 00233 static Number evalNumberDerivatedFunc(stdVector &pt, int d, int x, int y, 00234 void *data) { 00235 return 0.0; 00236 // TODO 00237 } 00238 00239 static Number curvatureFunc(const int systemId, const Vector &x, 00240 const Vector &v1, const Vector &v2, void* data = NULL) { 00241 PolynomialSystem *t = static_cast<PolynomialSystem*>(data); 00242 return t->curvature(x, systemId, v1, v2); 00243 } 00244 00245 SystemType findSystemType(); 00246 00247 int degree; 00248 int nbMonom; 00249 std::vector<multiIndice> multiIndices; 00250 //Matrix coefficients; 00251 stdVector domainU; 00252 stdVector domainL; 00253 00254 }; 00255 00256 //################################# 00257 // Operator 00258 //################################# 00259 00260 inline std::ostream& operator<<(std::ostream& out, const multiIndice multiI) { 00261 for (unsigned int i = 0; i < multiI.size(); i++) 00262 out << multiI[i] << " "; 00263 out << " "; 00264 return out; 00265 } 00266 00267 inline std::ostream& operator<<(std::ostream& out, const Monom &monom) { 00268 bool prev = false; 00269 //if(fabs(monom.scalar)>1) 00270 out << monom.scalar << "*"; 00271 //else return out<<0; 00272 for (unsigned int i = 0; i < monom.coef.size(); i++) { 00273 if (prev && monom.coef[i] >= 1) 00274 out << "*"; 00275 if (monom.coef[i] > 1) 00276 out << "x" << i << "^" << monom.coef[i]; 00277 if (monom.coef[i] == 1) 00278 out << "x" << i; 00279 if (monom.coef[i] >= 1) 00280 prev = true; 00281 } 00282 return out; 00283 } 00284 00285 inline std::ostream& operator<<(std::ostream& out, const Polynom &poly) { 00286 for (unsigned int i = 0; i < poly.monomials.size(); i++) { 00287 out << poly.monomials[i]; 00288 if (i != poly.monomials.size() - 1) 00289 out << " + "; 00290 } 00291 return out; 00292 } 00293 00294 inline std::ostream& operator<<(std::ostream& out, const PolynomialSystem sys) { 00295 for (unsigned int i = 0; i < (sys.polynomials).size(); i++) { 00296 out << "x" << i << "': " << (sys.polynomials[i]) << std::endl; 00297 } 00298 return out; 00299 } 00300 00301 // Monom and polynomilas operations 00302 00303 inline Monom operator*(const Monom m1, const Monom m2) { 00304 std::vector<int> coef(m1.coef.size(), 0); 00305 Number scalar = 0.0; 00306 for (unsigned int i = 0; i < coef.size(); i++) { 00307 coef[i] = m1.coef[i] + m2.coef[i]; 00308 } 00309 scalar = m1.scalar * m2.scalar; 00310 Monom res(m1.dimension, scalar, coef); 00311 return res; 00312 } 00313 00314 inline Polynom operator*(const Polynom poly1, const Polynom poly2) { 00315 std::vector<Monom> monoms; 00316 for (unsigned int i = 0; i < poly1.monomials.size(); i++) { 00317 for (unsigned int j = 0; j < poly2.monomials.size(); j++) { 00318 monoms.push_back(poly1.monomials[i] * poly2.monomials[j]); 00319 } 00320 } 00321 Polynom res(poly1.dimension, monoms); 00322 res.clean(); 00323 return res; 00324 } 00325 00326 inline Polynom operator*(Number d, const Polynom poly) { 00327 std::vector<Monom> monoms = poly.monomials; 00328 00329 for (unsigned int i = 0; i < poly.monomials.size(); i++) { 00330 monoms[i].scalar *= d; 00331 } 00332 Polynom res(poly.dimension, monoms); 00333 res.clean(); 00334 return res; 00335 } 00336 00337 inline Polynom operator+(const Polynom p1, const Polynom p2) { 00338 Polynom res = p1; 00339 for (unsigned int i = 0; i < p2.monomials.size(); i++) { 00340 bool notPresent = true; 00341 for (unsigned int j = 0; j < res.monomials.size(); j++) { 00342 if (res.monomials[j].coef == p2.monomials[i].coef) { 00343 notPresent = false; 00344 res.monomials[j].scalar += p2.monomials[i].scalar; 00345 } 00346 } 00347 if (notPresent) 00348 res.monomials.push_back(p2.monomials[i]); 00349 } 00350 res.clean(); 00351 return res; 00352 } 00353 00354 inline Polynom operator*(stdVector v, const PolynomialSystem polySys) { 00355 assert(v.size() == polySys.getDimension()); 00356 // cout << "vector:" << v << endl; 00357 Polynom res = v[0] * polySys.polynomials[0]; 00358 //cout << "i=" << 0 << " res="<<res<<endl; 00359 for (unsigned int i = 1; i < polySys.polynomials.size(); i++) { 00360 res = res + (v[i] * polySys.polynomials[i]); 00361 // cout << "i=" << i << " res="<<res<<endl; 00362 } 00363 res.clean(); 00364 return res; 00365 } 00366 00367 inline Polynom operator*(Vector v, const PolynomialSystem polySys) { 00368 assert(v.n_elem == polySys.getDimension()); 00369 // cout << "vector:" << v << endl; 00370 Polynom res = v[0] * polySys.polynomials[0]; 00371 //cout << "i=" << 0 << " res="<<res<<endl; 00372 for (unsigned int i = 1; i < polySys.polynomials.size(); i++) { 00373 res = res + (v[i] * polySys.polynomials[i]); 00374 // cout << "i=" << i << " res="<<res<<endl; 00375 } 00376 res.clean(); 00377 return res; 00378 } 00379 00380 inline bool operator==(const multiIndice ind1, const multiIndice ind2) { 00381 assert(ind1.size() == ind2.size()); 00382 for (unsigned int i = 0; i < ind1.size(); i++) 00383 if (!(ind1[i] == ind2[i])) 00384 return false; 00385 return true; 00386 } 00387 00388 inline stdVector operator/(const multiIndice ind1, const multiIndice ind2) { 00389 assert(ind1.size() == ind2.size()); 00390 stdVector result(ind1.size()); 00391 for (unsigned int i = 0; i < ind1.size(); i++) { 00392 if (ind2[i] != 0) 00393 result[i] = ((Number) ind1[i] / (Number) ind2[i]); 00394 else 00395 result[i] = 0; 00396 } 00397 00398 return result; 00399 } 00400 00401 inline bool operator<=(const multiIndice ind1, const multiIndice ind2) { 00402 assert(ind1.size() == ind2.size()); 00403 for (unsigned int i = 0; i < ind1.size(); i++) 00404 if (ind1[i] > ind2[i]) 00405 return false; 00406 return true; 00407 } 00408 00409 inline int sum(const multiIndice ind) { 00410 int res = 0; 00411 for (unsigned int i = 0; i < ind.size(); i++) { 00412 res += ind[i]; 00413 } 00414 return res; 00415 } 00416 00417 } // namespace nltool 00418 00419 #endif /* POLYNOMIALSYSTEM_H_ */