| 1 | // This may look like C code, but it is really -*- C++ -*- | 
|---|
| 2 | // | 
|---|
| 3 | // $Id: poly.h,v 1.1.1.1 1999-11-26 16:37:12 ansari Exp $ | 
|---|
| 4 | // | 
|---|
| 5 |  | 
|---|
| 6 | // Des polynomes avec des operations de bases et surtout des fits. | 
|---|
| 7 |  | 
|---|
| 8 | #ifndef POLY_SEEN | 
|---|
| 9 | #define POLY_SEEN | 
|---|
| 10 |  | 
|---|
| 11 | #include "objfio.h" | 
|---|
| 12 | #include <stdio.h> | 
|---|
| 13 | #include "peida.h" | 
|---|
| 14 | #include "tvector.h" | 
|---|
| 15 | #include "ppersist.h" | 
|---|
| 16 | #include "anydataobj.h" | 
|---|
| 17 |  | 
|---|
| 18 | namespace SOPHYA { | 
|---|
| 19 |  | 
|---|
| 20 | class Poly2; | 
|---|
| 21 |  | 
|---|
| 22 | ////////////////////////////////////////////////////////////////////////// | 
|---|
| 23 | class Poly : public Vector { | 
|---|
| 24 | friend class ObjFileIO<Poly>; | 
|---|
| 25 | public: | 
|---|
| 26 | Poly(int degre = 0); | 
|---|
| 27 | Poly(Poly const& a); | 
|---|
| 28 |  | 
|---|
| 29 | enum {classId = ClassId_Poly1}; | 
|---|
| 30 | int_4  ClassId() const         { return classId; } | 
|---|
| 31 |  | 
|---|
| 32 | inline int Degre() const {UpdateDegIfDirty(); return deg;} | 
|---|
| 33 |  | 
|---|
| 34 | inline void Realloc(int n, bool force=false) {Vector::Realloc(n+1,force);} | 
|---|
| 35 |  | 
|---|
| 36 | inline double operator[](int i) const {return Element(i);} | 
|---|
| 37 | inline double& operator[](int i) {dirty = 1; return Element(i);} | 
|---|
| 38 | // Retourne le coefficient de degre i | 
|---|
| 39 |  | 
|---|
| 40 | double operator()(double x) const; | 
|---|
| 41 | // Retourne la valeur prise en x. | 
|---|
| 42 |  | 
|---|
| 43 | void   Derivate(); | 
|---|
| 44 | // Derive le polynome en place | 
|---|
| 45 |  | 
|---|
| 46 | void   Derivate(Poly& der) const; | 
|---|
| 47 | // Derive le polynome dans un autre | 
|---|
| 48 |  | 
|---|
| 49 | int    Roots(Vector& roots) const; | 
|---|
| 50 | // retourne les racines si on peut les calculer... | 
|---|
| 51 |  | 
|---|
| 52 | int    Root1(double& r) const; | 
|---|
| 53 | // special degre 1 | 
|---|
| 54 |  | 
|---|
| 55 | int    Root2(double& r1, double& r2) const; | 
|---|
| 56 | // special degre 2 | 
|---|
| 57 |  | 
|---|
| 58 | Poly& operator = (Poly const& b); | 
|---|
| 59 | Poly& operator += (Poly const& b); | 
|---|
| 60 | Poly& operator -= (Poly const& b); | 
|---|
| 61 |  | 
|---|
| 62 | Poly& operator *= (double a); | 
|---|
| 63 |  | 
|---|
| 64 | Poly Mult(Poly const& b) const; | 
|---|
| 65 |  | 
|---|
| 66 | Poly power(int n) const; | 
|---|
| 67 | Poly operator() (Poly const& b) const; | 
|---|
| 68 | Poly2 operator() (Poly2 const& b) const; | 
|---|
| 69 |  | 
|---|
| 70 | void Print(ostream& s) const; | 
|---|
| 71 |  | 
|---|
| 72 | double Fit(Vector const& x, Vector const& y, int degre); | 
|---|
| 73 | // Fit d'un polynome de degre donne sur les x et y. | 
|---|
| 74 |  | 
|---|
| 75 | double Fit(Vector const& x, Vector const& y, Vector const& erry2, int degre, | 
|---|
| 76 | Vector& errCoef); | 
|---|
| 77 | // En plus, on fournit les carres des erreurs sur y et on a les erreurs | 
|---|
| 78 | // sur les coefficients dans un vecteur. | 
|---|
| 79 |  | 
|---|
| 80 | private: | 
|---|
| 81 | int dirty; | 
|---|
| 82 | int_4 deg; | 
|---|
| 83 | void UpdateDeg() const; | 
|---|
| 84 | void UpdateDegIfDirty() const {if (dirty) UpdateDeg();} | 
|---|
| 85 | }; | 
|---|
| 86 |  | 
|---|
| 87 | inline Poly operator* (Poly const& a, Poly const& b) | 
|---|
| 88 | { return a.Mult(b); } | 
|---|
| 89 |  | 
|---|
| 90 | inline Poly operator+ (Poly const& a, Poly const& b) | 
|---|
| 91 | { Poly c((a.Degre() > b.Degre())?(a.Degre()+1):(b.Degre()+1)); | 
|---|
| 92 | c = a; c += b; return c; } | 
|---|
| 93 |  | 
|---|
| 94 | inline Poly operator- (Poly const& a, Poly const& b) | 
|---|
| 95 | { Poly c((a.Degre() > b.Degre())?(a.Degre()+1):(b.Degre()+1)); | 
|---|
| 96 | c = a; c -= b; return c; } | 
|---|
| 97 |  | 
|---|
| 98 | inline Poly operator* (double a, Poly const& b) | 
|---|
| 99 | { Poly c(b); c *= a; return c; } | 
|---|
| 100 |  | 
|---|
| 101 | inline ostream& operator << (ostream& s, const Poly& a) | 
|---|
| 102 | { a.Print(s); return s; } | 
|---|
| 103 |  | 
|---|
| 104 | ////////////////////////////////////////////////////////////////////////// | 
|---|
| 105 | inline POutPersist& operator << (POutPersist& os, Poly & obj) | 
|---|
| 106 | { ObjFileIO<Poly> fio(&obj);  fio.Write(os);  return(os); } | 
|---|
| 107 | inline PInPersist& operator >> (PInPersist& is, Poly & obj) | 
|---|
| 108 | { ObjFileIO<Poly> fio(&obj);  fio.Read(is);  return(is); } | 
|---|
| 109 | // Classe pour la gestion de persistance | 
|---|
| 110 | // ObjFileIO<Poly> | 
|---|
| 111 |  | 
|---|
| 112 | ////////////////////////////////////////////////////////////////////////// | 
|---|
| 113 | int binomial(int n, int p); | 
|---|
| 114 |  | 
|---|
| 115 | ////////////////////////////////////////////////////////////////////////// | 
|---|
| 116 | class Poly2 : public Vector { | 
|---|
| 117 | friend class ObjFileIO<Poly2>; | 
|---|
| 118 | public: | 
|---|
| 119 | Poly2(int degreX=0, int degreY=0); | 
|---|
| 120 | // degres alloues. | 
|---|
| 121 | Poly2(Poly const& polX, Poly const& polY); | 
|---|
| 122 | Poly2(Poly2 const& a); | 
|---|
| 123 |  | 
|---|
| 124 | enum {classId = ClassId_Poly2}; | 
|---|
| 125 | int_4 ClassId() const         { return classId; } | 
|---|
| 126 |  | 
|---|
| 127 | inline int DegX() const {UpdateDegIfDirty(); return degX;} | 
|---|
| 128 | inline int DegY() const {UpdateDegIfDirty(); return degY;} | 
|---|
| 129 | inline int MaxDegX() const {return maxDegX;} | 
|---|
| 130 | inline int MaxDegY() const {return maxDegY;} | 
|---|
| 131 | inline int Deg()  const {UpdateDegIfDirty(); return deg;} | 
|---|
| 132 | // les degres partiels en x et y, et totaux. | 
|---|
| 133 |  | 
|---|
| 134 | double operator()(double x, double y) const; | 
|---|
| 135 | // retourne la valeur en (x,y) | 
|---|
| 136 |  | 
|---|
| 137 | inline int IndCoef(int dx, int dy) const { | 
|---|
| 138 | if (dx>maxDegX || dy>maxDegY) THROW(rangeCheckErr); | 
|---|
| 139 | return dx + (maxDegX+1)*dy; | 
|---|
| 140 | } | 
|---|
| 141 | // l'indice du coefficient dans le vecteur. Public uniquement parce | 
|---|
| 142 | // que ca sert a recuperer les erreurs sur les coefficients lors | 
|---|
| 143 | // d'un fit... | 
|---|
| 144 |  | 
|---|
| 145 | inline double Coef(int dx, int dy) const { | 
|---|
| 146 | return (dx>maxDegX || dy>maxDegY) ? 0 : Element(IndCoef(dx,dy)); | 
|---|
| 147 | } | 
|---|
| 148 | inline double& Coef(int dx, int dy) { | 
|---|
| 149 | if (dx>maxDegX || dy>maxDegY) THROW(rangeCheckErr); | 
|---|
| 150 | dirty = 1; return Element(IndCoef(dx,dy)); | 
|---|
| 151 | } | 
|---|
| 152 | // retourne le coefficient de degre (dx,dy) | 
|---|
| 153 |  | 
|---|
| 154 | double Fit(Vector const& x, Vector const& y, Vector const& z, | 
|---|
| 155 | int degreX, int degreY); | 
|---|
| 156 | double Fit(Vector const& x, Vector const& y, Vector const& z, | 
|---|
| 157 | Vector const& errz2, int degreX, int degreY, | 
|---|
| 158 | Vector& errCoef); | 
|---|
| 159 | // degres partiels imposes. cf Poly::Fit sinon | 
|---|
| 160 |  | 
|---|
| 161 |  | 
|---|
| 162 | double Fit(Vector const& x, Vector const& y, Vector const& z, | 
|---|
| 163 | int degre); | 
|---|
| 164 | double Fit(Vector const& x, Vector const& y, Vector const& z, | 
|---|
| 165 | Vector const& errz2, int degre, | 
|---|
| 166 | Vector& errCoef); | 
|---|
| 167 | // degre total impose. cf Poly::Fit sinon | 
|---|
| 168 |  | 
|---|
| 169 | Poly2& operator = (Poly2 const& b); | 
|---|
| 170 |  | 
|---|
| 171 | Poly2& operator += (Poly2 const& b); | 
|---|
| 172 | Poly2& operator -= (Poly2 const& b); | 
|---|
| 173 |  | 
|---|
| 174 | Poly2& operator *= (double a); | 
|---|
| 175 |  | 
|---|
| 176 | Poly2 Mult(Poly2 const& b) const; | 
|---|
| 177 |  | 
|---|
| 178 | Poly2 power(int n) const; | 
|---|
| 179 |  | 
|---|
| 180 | Poly2 operator() (Poly const& px, Poly const& py) const; | 
|---|
| 181 | //  Poly2 operator() (Poly2 const& px, Poly2 const& py) const; | 
|---|
| 182 |  | 
|---|
| 183 | void Realloc(int degreX, int degreY); | 
|---|
| 184 |  | 
|---|
| 185 | void Print(ostream& s) const; | 
|---|
| 186 |  | 
|---|
| 187 | private: | 
|---|
| 188 | int dirty; | 
|---|
| 189 | int_4 maxDegX; | 
|---|
| 190 | int_4 maxDegY; | 
|---|
| 191 | int degX; | 
|---|
| 192 | int degY; | 
|---|
| 193 | int deg; | 
|---|
| 194 | void UpdateDeg() const; | 
|---|
| 195 | void UpdateDegIfDirty() const {if (dirty) UpdateDeg();} | 
|---|
| 196 | }; | 
|---|
| 197 |  | 
|---|
| 198 | inline Poly2 operator* (Poly2 const& a, Poly2 const& b) | 
|---|
| 199 | { return a.Mult(b); } | 
|---|
| 200 |  | 
|---|
| 201 | inline Poly2 operator+ (Poly2 const& a, Poly2 const& b) | 
|---|
| 202 | { Poly2 c(a); c += b; return c; } | 
|---|
| 203 |  | 
|---|
| 204 | inline Poly2 operator- (Poly2 const& a, Poly2 const& b) | 
|---|
| 205 | { Poly2 c(a); c -= b; return c; } | 
|---|
| 206 |  | 
|---|
| 207 | inline Poly2 operator * (double a, Poly2 const& b) | 
|---|
| 208 | { Poly2 c(b); c *= a; return c; } | 
|---|
| 209 |  | 
|---|
| 210 | inline ostream& operator << (ostream& s, const Poly2& a) | 
|---|
| 211 | { a.Print(s); return s; } | 
|---|
| 212 |  | 
|---|
| 213 | ////////////////////////////////////////////////////////////////////////// | 
|---|
| 214 | inline POutPersist& operator << (POutPersist& os, Poly2 & obj) | 
|---|
| 215 | { ObjFileIO<Poly2> fio(&obj);  fio.Write(os);  return(os); } | 
|---|
| 216 | inline PInPersist& operator >> (PInPersist& is, Poly2 & obj) | 
|---|
| 217 | { ObjFileIO<Poly2> fio(&obj);  fio.Read(is);  return(is); } | 
|---|
| 218 | // Classe pour la gestion de persistance | 
|---|
| 219 | // ObjFileIO<Poly2> | 
|---|
| 220 |  | 
|---|
| 221 |  | 
|---|
| 222 | } // Fin du namespace | 
|---|
| 223 |  | 
|---|
| 224 | #endif // POLY_SEEN | 
|---|