| [220] | 1 | // This may look like C code, but it is really -*- C++ -*- | 
|---|
|  | 2 | // | 
|---|
| [958] | 3 | // $Id: poly.h,v 1.8 2000-04-18 13:38:36 ansari Exp $ | 
|---|
| [220] | 4 | // | 
|---|
|  | 5 |  | 
|---|
|  | 6 | // Des polynomes avec des operations de bases et surtout des fits. | 
|---|
|  | 7 |  | 
|---|
|  | 8 | #ifndef POLY_SEEN | 
|---|
|  | 9 | #define POLY_SEEN | 
|---|
|  | 10 |  | 
|---|
| [514] | 11 | #include "objfio.h" | 
|---|
| [220] | 12 | #include <stdio.h> | 
|---|
|  | 13 | #include "peida.h" | 
|---|
| [514] | 14 | #include "tvector.h" | 
|---|
|  | 15 | #include "ppersist.h" | 
|---|
|  | 16 | #include "anydataobj.h" | 
|---|
| [220] | 17 |  | 
|---|
| [552] | 18 | namespace SOPHYA { | 
|---|
| [514] | 19 |  | 
|---|
| [220] | 20 | class Poly2; | 
|---|
|  | 21 |  | 
|---|
| [514] | 22 | ////////////////////////////////////////////////////////////////////////// | 
|---|
| [958] | 23 | //! Class of 1 dimension polynomials P(x) | 
|---|
| [938] | 24 | class Poly : public TVector<r_8> { | 
|---|
| [514] | 25 | friend class ObjFileIO<Poly>; | 
|---|
| [220] | 26 | public: | 
|---|
|  | 27 | Poly(int degre = 0); | 
|---|
| [514] | 28 | Poly(Poly const& a); | 
|---|
| [220] | 29 |  | 
|---|
| [958] | 30 | //! Return degre of polynomials | 
|---|
| [220] | 31 | inline int Degre() const {UpdateDegIfDirty(); return deg;} | 
|---|
|  | 32 |  | 
|---|
| [958] | 33 | //! Return space for a \b n degre polynomial | 
|---|
|  | 34 | /*! | 
|---|
|  | 35 | \param \b n : degre of new polynomial | 
|---|
|  | 36 | \param \b force : force re-allocation if true,\ | 
|---|
|  | 37 | if not allocation occurs only | 
|---|
|  | 38 | if space needed is greater than the old one | 
|---|
|  | 39 | */ | 
|---|
| [938] | 40 | inline void Realloc(int n, bool force=false) | 
|---|
|  | 41 | {TVector<r_8>::Realloc(n+1,force);} | 
|---|
| [220] | 42 |  | 
|---|
| [805] | 43 | //  Pour compatibilite PEIDA  - Reza 03/2000 | 
|---|
| [958] | 44 | //! Return coefficient of degre \b i | 
|---|
| [805] | 45 | inline double Element(int i) const { return Elem(i,0,0,0,0); } | 
|---|
| [958] | 46 | //! Return coefficient of degre \b i | 
|---|
| [805] | 47 | inline double & Element(int i)  { return Elem(i,0,0,0,0); } | 
|---|
|  | 48 |  | 
|---|
| [958] | 49 | //! Return coefficient of degre \b i | 
|---|
| [805] | 50 | inline double operator[](int i) const {return Elem(i,0,0,0,0);} | 
|---|
| [958] | 51 | //! Return coefficient of degre \b i | 
|---|
| [805] | 52 | inline double& operator[](int i) {dirty = 1; return Elem(i,0,0,0,0);} | 
|---|
| [220] | 53 | // Retourne le coefficient de degre i | 
|---|
|  | 54 |  | 
|---|
|  | 55 | double operator()(double x) const; | 
|---|
|  | 56 | // Retourne la valeur prise en x. | 
|---|
|  | 57 |  | 
|---|
|  | 58 | void   Derivate(); | 
|---|
|  | 59 | // Derive le polynome en place | 
|---|
|  | 60 |  | 
|---|
|  | 61 | void   Derivate(Poly& der) const; | 
|---|
|  | 62 | // Derive le polynome dans un autre | 
|---|
|  | 63 |  | 
|---|
| [938] | 64 | int    Roots(TVector<r_8>& roots) const; | 
|---|
| [220] | 65 | // retourne les racines si on peut les calculer... | 
|---|
|  | 66 |  | 
|---|
|  | 67 | int    Root1(double& r) const; | 
|---|
|  | 68 | // special degre 1 | 
|---|
|  | 69 |  | 
|---|
|  | 70 | int    Root2(double& r1, double& r2) const; | 
|---|
|  | 71 | // special degre 2 | 
|---|
|  | 72 |  | 
|---|
|  | 73 | Poly& operator = (Poly const& b); | 
|---|
|  | 74 | Poly& operator += (Poly const& b); | 
|---|
|  | 75 | Poly& operator -= (Poly const& b); | 
|---|
|  | 76 |  | 
|---|
|  | 77 | Poly& operator *= (double a); | 
|---|
|  | 78 |  | 
|---|
| [514] | 79 | Poly Mult(Poly const& b) const; | 
|---|
|  | 80 |  | 
|---|
| [220] | 81 | Poly power(int n) const; | 
|---|
|  | 82 | Poly operator() (Poly const& b) const; | 
|---|
|  | 83 | Poly2 operator() (Poly2 const& b) const; | 
|---|
|  | 84 |  | 
|---|
| [805] | 85 | void Print(ostream& s, int_4 maxprt=-1, bool si=false) const; | 
|---|
| [220] | 86 |  | 
|---|
| [938] | 87 | double Fit(TVector<r_8> const& x, TVector<r_8> const& y, int degre); | 
|---|
| [220] | 88 | // Fit d'un polynome de degre donne sur les x et y. | 
|---|
|  | 89 |  | 
|---|
| [938] | 90 | double Fit(TVector<r_8> const& x, TVector<r_8> const& y, | 
|---|
|  | 91 | TVector<r_8> const& erry2, int degre, TVector<r_8>& errCoef); | 
|---|
| [220] | 92 | // En plus, on fournit les carres des erreurs sur y et on a les erreurs | 
|---|
|  | 93 | // sur les coefficients dans un vecteur. | 
|---|
|  | 94 |  | 
|---|
|  | 95 | private: | 
|---|
|  | 96 | int dirty; | 
|---|
|  | 97 | int_4 deg; | 
|---|
|  | 98 | void UpdateDeg() const; | 
|---|
|  | 99 | void UpdateDegIfDirty() const {if (dirty) UpdateDeg();} | 
|---|
|  | 100 | }; | 
|---|
|  | 101 |  | 
|---|
| [958] | 102 | /*! \ingroup NTools \fn operator*(Poly const&,Poly const&) | 
|---|
|  | 103 | \brief perform and return P(X) = a(x) * b(x) */ | 
|---|
| [514] | 104 | inline Poly operator* (Poly const& a, Poly const& b) | 
|---|
|  | 105 | { return a.Mult(b); } | 
|---|
| [220] | 106 |  | 
|---|
| [958] | 107 | /*! \ingroup NTools \fn operator+(Poly const&,Poly const&) | 
|---|
|  | 108 | \brief perform and return P(X) = a(x) + b(x) */ | 
|---|
| [514] | 109 | inline Poly operator+ (Poly const& a, Poly const& b) | 
|---|
|  | 110 | { Poly c((a.Degre() > b.Degre())?(a.Degre()+1):(b.Degre()+1)); | 
|---|
|  | 111 | c = a; c += b; return c; } | 
|---|
| [220] | 112 |  | 
|---|
| [958] | 113 | /*! \ingroup NTools \fn operator-(Poly const&,Poly const&) | 
|---|
|  | 114 | \brief perform and return P(X) = a(x) - b(x) */ | 
|---|
| [514] | 115 | inline Poly operator- (Poly const& a, Poly const& b) | 
|---|
|  | 116 | { Poly c((a.Degre() > b.Degre())?(a.Degre()+1):(b.Degre()+1)); | 
|---|
|  | 117 | c = a; c -= b; return c; } | 
|---|
| [220] | 118 |  | 
|---|
| [958] | 119 | /*! \ingroup NTools \fn operator*(double,Poly const&) | 
|---|
|  | 120 | \brief perform and return P(X) = a * b(x) */ | 
|---|
| [514] | 121 | inline Poly operator* (double a, Poly const& b) | 
|---|
|  | 122 | { Poly c(b); c *= a; return c; } | 
|---|
|  | 123 |  | 
|---|
| [958] | 124 | /*! \ingroup NTools \fn operator<<(ostream&,const Poly&) | 
|---|
|  | 125 | \brief Print a(x) on stream \b s */ | 
|---|
| [514] | 126 | inline ostream& operator << (ostream& s, const Poly& a) | 
|---|
|  | 127 | { a.Print(s); return s; } | 
|---|
|  | 128 |  | 
|---|
|  | 129 | ////////////////////////////////////////////////////////////////////////// | 
|---|
| [958] | 130 | /*! \ingroup NTools \fn operator<<(POutPersist&,Poly&) | 
|---|
|  | 131 | \brief For persistance management */ | 
|---|
| [514] | 132 | inline POutPersist& operator << (POutPersist& os, Poly & obj) | 
|---|
|  | 133 | { ObjFileIO<Poly> fio(&obj);  fio.Write(os);  return(os); } | 
|---|
| [958] | 134 | /*! \ingroup NTools \fn operator<<(PInPersist&,,Poly&) | 
|---|
|  | 135 | \brief For persistance management */ | 
|---|
| [514] | 136 | inline PInPersist& operator >> (PInPersist& is, Poly & obj) | 
|---|
|  | 137 | { ObjFileIO<Poly> fio(&obj);  fio.Read(is);  return(is); } | 
|---|
|  | 138 | // Classe pour la gestion de persistance | 
|---|
|  | 139 | // ObjFileIO<Poly> | 
|---|
|  | 140 |  | 
|---|
|  | 141 | ////////////////////////////////////////////////////////////////////////// | 
|---|
|  | 142 | int binomial(int n, int p); | 
|---|
|  | 143 |  | 
|---|
|  | 144 | ////////////////////////////////////////////////////////////////////////// | 
|---|
| [958] | 145 | //! Class of 2 dimensions polynomials P(x,y) | 
|---|
| [938] | 146 | class Poly2 : public TVector<r_8> { | 
|---|
| [514] | 147 | friend class ObjFileIO<Poly2>; | 
|---|
| [220] | 148 | public: | 
|---|
|  | 149 | Poly2(int degreX=0, int degreY=0); | 
|---|
|  | 150 | // degres alloues. | 
|---|
|  | 151 | Poly2(Poly const& polX, Poly const& polY); | 
|---|
|  | 152 | Poly2(Poly2 const& a); | 
|---|
|  | 153 |  | 
|---|
| [958] | 154 | //! Return polynomial partial degre for X | 
|---|
| [220] | 155 | inline int DegX() const {UpdateDegIfDirty(); return degX;} | 
|---|
| [958] | 156 | //! Return polynomial partial degre for Y | 
|---|
| [220] | 157 | inline int DegY() const {UpdateDegIfDirty(); return degY;} | 
|---|
| [958] | 158 | //! Return polynomial maximum partial degre for X | 
|---|
| [220] | 159 | inline int MaxDegX() const {return maxDegX;} | 
|---|
| [958] | 160 | //! Return polynomial maximum partial degre for Y | 
|---|
| [220] | 161 | inline int MaxDegY() const {return maxDegY;} | 
|---|
| [958] | 162 | //! Return polynomial total degre for X | 
|---|
| [220] | 163 | inline int Deg()  const {UpdateDegIfDirty(); return deg;} | 
|---|
|  | 164 | // les degres partiels en x et y, et totaux. | 
|---|
|  | 165 |  | 
|---|
| [805] | 166 | //  Pour compatibilite PEIDA  - Reza 03/2000 | 
|---|
| [958] | 167 | //! Return coefficient of degre \b i | 
|---|
| [805] | 168 | inline double Element(int i) const { return Elem(i,0,0,0,0); } | 
|---|
| [958] | 169 | //! Return coefficient of degre \b i | 
|---|
| [805] | 170 | inline double & Element(int i)  { return Elem(i,0,0,0,0); } | 
|---|
|  | 171 |  | 
|---|
| [220] | 172 | double operator()(double x, double y) const; | 
|---|
|  | 173 | // retourne la valeur en (x,y) | 
|---|
|  | 174 |  | 
|---|
| [958] | 175 | //! Return index of coefficient of X^dx * Y^dy in the vector | 
|---|
| [220] | 176 | inline int IndCoef(int dx, int dy) const { | 
|---|
|  | 177 | if (dx>maxDegX || dy>maxDegY) THROW(rangeCheckErr); | 
|---|
|  | 178 | return dx + (maxDegX+1)*dy; | 
|---|
|  | 179 | } | 
|---|
|  | 180 | // l'indice du coefficient dans le vecteur. Public uniquement parce | 
|---|
|  | 181 | // que ca sert a recuperer les erreurs sur les coefficients lors | 
|---|
|  | 182 | // d'un fit... | 
|---|
|  | 183 |  | 
|---|
| [958] | 184 | //! Return coefficient of X^dx * Y^dy | 
|---|
| [220] | 185 | inline double Coef(int dx, int dy) const { | 
|---|
| [514] | 186 | return (dx>maxDegX || dy>maxDegY) ? 0 : Element(IndCoef(dx,dy)); | 
|---|
| [220] | 187 | } | 
|---|
| [958] | 188 | //! Return coefficient of X^dx * Y^dy | 
|---|
| [220] | 189 | inline double& Coef(int dx, int dy) { | 
|---|
|  | 190 | if (dx>maxDegX || dy>maxDegY) THROW(rangeCheckErr); | 
|---|
| [514] | 191 | dirty = 1; return Element(IndCoef(dx,dy)); | 
|---|
| [220] | 192 | } | 
|---|
|  | 193 | // retourne le coefficient de degre (dx,dy) | 
|---|
|  | 194 |  | 
|---|
| [938] | 195 | double Fit(TVector<r_8> const& x, TVector<r_8> const& y, TVector<r_8> const& z, | 
|---|
| [220] | 196 | int degreX, int degreY); | 
|---|
| [938] | 197 | double Fit(TVector<r_8> const& x, TVector<r_8> const& y, TVector<r_8> const& z, | 
|---|
|  | 198 | TVector<r_8> const& errz2, int degreX, int degreY, | 
|---|
|  | 199 | TVector<r_8>& errCoef); | 
|---|
| [220] | 200 | // degres partiels imposes. cf Poly::Fit sinon | 
|---|
|  | 201 |  | 
|---|
|  | 202 |  | 
|---|
| [938] | 203 | double Fit(TVector<r_8> const& x, TVector<r_8> const& y, TVector<r_8> const& z, | 
|---|
| [220] | 204 | int degre); | 
|---|
| [938] | 205 | double Fit(TVector<r_8> const& x, TVector<r_8> const& y, TVector<r_8> const& z, | 
|---|
|  | 206 | TVector<r_8> const& errz2, int degre, | 
|---|
|  | 207 | TVector<r_8>& errCoef); | 
|---|
| [220] | 208 | // degre total impose. cf Poly::Fit sinon | 
|---|
|  | 209 |  | 
|---|
|  | 210 | Poly2& operator = (Poly2 const& b); | 
|---|
|  | 211 |  | 
|---|
|  | 212 | Poly2& operator += (Poly2 const& b); | 
|---|
|  | 213 | Poly2& operator -= (Poly2 const& b); | 
|---|
|  | 214 |  | 
|---|
| [514] | 215 | Poly2& operator *= (double a); | 
|---|
| [220] | 216 |  | 
|---|
| [514] | 217 | Poly2 Mult(Poly2 const& b) const; | 
|---|
|  | 218 |  | 
|---|
| [220] | 219 | Poly2 power(int n) const; | 
|---|
|  | 220 |  | 
|---|
|  | 221 | Poly2 operator() (Poly const& px, Poly const& py) const; | 
|---|
|  | 222 | //  Poly2 operator() (Poly2 const& px, Poly2 const& py) const; | 
|---|
|  | 223 |  | 
|---|
|  | 224 | void Realloc(int degreX, int degreY); | 
|---|
|  | 225 |  | 
|---|
| [805] | 226 | void Print(ostream& s, int_4 maxprt=-1, bool si=false) const; | 
|---|
| [220] | 227 |  | 
|---|
|  | 228 | private: | 
|---|
|  | 229 | int dirty; | 
|---|
|  | 230 | int_4 maxDegX; | 
|---|
|  | 231 | int_4 maxDegY; | 
|---|
|  | 232 | int degX; | 
|---|
|  | 233 | int degY; | 
|---|
|  | 234 | int deg; | 
|---|
|  | 235 | void UpdateDeg() const; | 
|---|
|  | 236 | void UpdateDegIfDirty() const {if (dirty) UpdateDeg();} | 
|---|
|  | 237 | }; | 
|---|
|  | 238 |  | 
|---|
| [958] | 239 | /*! \ingroup NTools \fn operator*(Poly2 const&,Poly2 const&) | 
|---|
|  | 240 | \brief Perform P(x,y) = a(x,y) * b(x,y) */ | 
|---|
| [514] | 241 | inline Poly2 operator* (Poly2 const& a, Poly2 const& b) | 
|---|
|  | 242 | { return a.Mult(b); } | 
|---|
|  | 243 |  | 
|---|
| [958] | 244 | /*! \ingroup NTools \fn operator+(Poly2 const&,Poly2 const&) | 
|---|
|  | 245 | \brief Perform P(x,y) = a(x,y) + b(x,y) */ | 
|---|
| [514] | 246 | inline Poly2 operator+ (Poly2 const& a, Poly2 const& b) | 
|---|
|  | 247 | { Poly2 c(a); c += b; return c; } | 
|---|
|  | 248 |  | 
|---|
| [958] | 249 | /*! \ingroup NTools \fn operator-(Poly2 const&,Poly2 const&) | 
|---|
|  | 250 | \brief Perform P(x,y) = a(x,y) - b(x,y) */ | 
|---|
| [514] | 251 | inline Poly2 operator- (Poly2 const& a, Poly2 const& b) | 
|---|
|  | 252 | { Poly2 c(a); c -= b; return c; } | 
|---|
|  | 253 |  | 
|---|
| [958] | 254 | /*! \ingroup NTools \fn operator-(double,Poly2 const&) | 
|---|
|  | 255 | \brief Perform P(x,y) = a * b(x,y) */ | 
|---|
| [514] | 256 | inline Poly2 operator * (double a, Poly2 const& b) | 
|---|
|  | 257 | { Poly2 c(b); c *= a; return c; } | 
|---|
|  | 258 |  | 
|---|
| [958] | 259 | /*! \ingroup NTools \fn operator<<(ostream&,const Poly2&) | 
|---|
|  | 260 | \brief Print a(x,y) on stream \b s */ | 
|---|
| [514] | 261 | inline ostream& operator << (ostream& s, const Poly2& a) | 
|---|
|  | 262 | { a.Print(s); return s; } | 
|---|
|  | 263 |  | 
|---|
|  | 264 | ////////////////////////////////////////////////////////////////////////// | 
|---|
| [958] | 265 | /*! \ingroup NTools \fn operator<<(POutPersist&,Poly2&) | 
|---|
|  | 266 | \brief For persistance management */ | 
|---|
| [514] | 267 | inline POutPersist& operator << (POutPersist& os, Poly2 & obj) | 
|---|
|  | 268 | { ObjFileIO<Poly2> fio(&obj);  fio.Write(os);  return(os); } | 
|---|
| [958] | 269 | /*! \ingroup NTools \fn operator<<(POutPersist&,Poly2&) | 
|---|
|  | 270 | \brief For persistance management */ | 
|---|
| [514] | 271 | inline PInPersist& operator >> (PInPersist& is, Poly2 & obj) | 
|---|
|  | 272 | { ObjFileIO<Poly2> fio(&obj);  fio.Read(is);  return(is); } | 
|---|
|  | 273 | // Classe pour la gestion de persistance | 
|---|
|  | 274 | // ObjFileIO<Poly2> | 
|---|
|  | 275 |  | 
|---|
|  | 276 |  | 
|---|
|  | 277 | } // Fin du namespace | 
|---|
|  | 278 |  | 
|---|
| [220] | 279 | #endif // POLY_SEEN | 
|---|