[220] | 1 | // This may look like C code, but it is really -*- C++ -*-
|
---|
| 2 | //
|
---|
[2506] | 3 | // $Id: poly.h,v 1.11 2004-03-15 16:46:56 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 |
|
---|
[1584] | 85 | void Print(ostream& s, sa_size_t maxprt=-1,
|
---|
[1557] | 86 | bool si=false, bool ascd=false) const ;
|
---|
[220] | 87 |
|
---|
[938] | 88 | double Fit(TVector<r_8> const& x, TVector<r_8> const& y, int degre);
|
---|
[220] | 89 | // Fit d'un polynome de degre donne sur les x et y.
|
---|
| 90 |
|
---|
[938] | 91 | double Fit(TVector<r_8> const& x, TVector<r_8> const& y,
|
---|
| 92 | TVector<r_8> const& erry2, int degre, TVector<r_8>& errCoef);
|
---|
[220] | 93 | // En plus, on fournit les carres des erreurs sur y et on a les erreurs
|
---|
| 94 | // sur les coefficients dans un vecteur.
|
---|
| 95 |
|
---|
| 96 | private:
|
---|
| 97 | int dirty;
|
---|
| 98 | int_4 deg;
|
---|
| 99 | void UpdateDeg() const;
|
---|
| 100 | void UpdateDegIfDirty() const {if (dirty) UpdateDeg();}
|
---|
| 101 | };
|
---|
| 102 |
|
---|
[958] | 103 | /*! \ingroup NTools \fn operator*(Poly const&,Poly const&)
|
---|
| 104 | \brief perform and return P(X) = a(x) * b(x) */
|
---|
[514] | 105 | inline Poly operator* (Poly const& a, Poly const& b)
|
---|
| 106 | { return a.Mult(b); }
|
---|
[220] | 107 |
|
---|
[958] | 108 | /*! \ingroup NTools \fn operator+(Poly const&,Poly const&)
|
---|
| 109 | \brief perform and return P(X) = a(x) + b(x) */
|
---|
[514] | 110 | inline Poly operator+ (Poly const& a, Poly const& b)
|
---|
| 111 | { Poly c((a.Degre() > b.Degre())?(a.Degre()+1):(b.Degre()+1));
|
---|
| 112 | c = a; c += b; return c; }
|
---|
[220] | 113 |
|
---|
[958] | 114 | /*! \ingroup NTools \fn operator-(Poly const&,Poly const&)
|
---|
| 115 | \brief perform and return P(X) = a(x) - b(x) */
|
---|
[514] | 116 | inline Poly operator- (Poly const& a, Poly const& b)
|
---|
| 117 | { Poly c((a.Degre() > b.Degre())?(a.Degre()+1):(b.Degre()+1));
|
---|
| 118 | c = a; c -= b; return c; }
|
---|
[220] | 119 |
|
---|
[958] | 120 | /*! \ingroup NTools \fn operator*(double,Poly const&)
|
---|
| 121 | \brief perform and return P(X) = a * b(x) */
|
---|
[514] | 122 | inline Poly operator* (double a, Poly const& b)
|
---|
| 123 | { Poly c(b); c *= a; return c; }
|
---|
| 124 |
|
---|
[958] | 125 | /*! \ingroup NTools \fn operator<<(ostream&,const Poly&)
|
---|
| 126 | \brief Print a(x) on stream \b s */
|
---|
[514] | 127 | inline ostream& operator << (ostream& s, const Poly& a)
|
---|
| 128 | { a.Print(s); return s; }
|
---|
| 129 |
|
---|
| 130 | //////////////////////////////////////////////////////////////////////////
|
---|
[958] | 131 | /*! \ingroup NTools \fn operator<<(POutPersist&,Poly&)
|
---|
| 132 | \brief For persistance management */
|
---|
[514] | 133 | inline POutPersist& operator << (POutPersist& os, Poly & obj)
|
---|
| 134 | { ObjFileIO<Poly> fio(&obj); fio.Write(os); return(os); }
|
---|
[958] | 135 | /*! \ingroup NTools \fn operator<<(PInPersist&,,Poly&)
|
---|
| 136 | \brief For persistance management */
|
---|
[514] | 137 | inline PInPersist& operator >> (PInPersist& is, Poly & obj)
|
---|
| 138 | { ObjFileIO<Poly> fio(&obj); fio.Read(is); return(is); }
|
---|
| 139 | // Classe pour la gestion de persistance
|
---|
| 140 | // ObjFileIO<Poly>
|
---|
| 141 |
|
---|
| 142 | //////////////////////////////////////////////////////////////////////////
|
---|
| 143 | int binomial(int n, int p);
|
---|
| 144 |
|
---|
| 145 | //////////////////////////////////////////////////////////////////////////
|
---|
[958] | 146 | //! Class of 2 dimensions polynomials P(x,y)
|
---|
[938] | 147 | class Poly2 : public TVector<r_8> {
|
---|
[514] | 148 | friend class ObjFileIO<Poly2>;
|
---|
[220] | 149 | public:
|
---|
| 150 | Poly2(int degreX=0, int degreY=0);
|
---|
| 151 | // degres alloues.
|
---|
| 152 | Poly2(Poly const& polX, Poly const& polY);
|
---|
| 153 | Poly2(Poly2 const& a);
|
---|
| 154 |
|
---|
[958] | 155 | //! Return polynomial partial degre for X
|
---|
[220] | 156 | inline int DegX() const {UpdateDegIfDirty(); return degX;}
|
---|
[958] | 157 | //! Return polynomial partial degre for Y
|
---|
[220] | 158 | inline int DegY() const {UpdateDegIfDirty(); return degY;}
|
---|
[958] | 159 | //! Return polynomial maximum partial degre for X
|
---|
[220] | 160 | inline int MaxDegX() const {return maxDegX;}
|
---|
[958] | 161 | //! Return polynomial maximum partial degre for Y
|
---|
[220] | 162 | inline int MaxDegY() const {return maxDegY;}
|
---|
[958] | 163 | //! Return polynomial total degre for X
|
---|
[220] | 164 | inline int Deg() const {UpdateDegIfDirty(); return deg;}
|
---|
| 165 | // les degres partiels en x et y, et totaux.
|
---|
| 166 |
|
---|
[805] | 167 | // Pour compatibilite PEIDA - Reza 03/2000
|
---|
[958] | 168 | //! Return coefficient of degre \b i
|
---|
[805] | 169 | inline double Element(int i) const { return Elem(i,0,0,0,0); }
|
---|
[958] | 170 | //! Return coefficient of degre \b i
|
---|
[805] | 171 | inline double & Element(int i) { return Elem(i,0,0,0,0); }
|
---|
| 172 |
|
---|
[220] | 173 | double operator()(double x, double y) const;
|
---|
| 174 | // retourne la valeur en (x,y)
|
---|
| 175 |
|
---|
[958] | 176 | //! Return index of coefficient of X^dx * Y^dy in the vector
|
---|
[220] | 177 | inline int IndCoef(int dx, int dy) const {
|
---|
[2506] | 178 | if (dx>maxDegX || dy>maxDegY) throw RangeCheckError("Poly2::IndCoef()");
|
---|
[220] | 179 | return dx + (maxDegX+1)*dy;
|
---|
| 180 | }
|
---|
| 181 | // l'indice du coefficient dans le vecteur. Public uniquement parce
|
---|
| 182 | // que ca sert a recuperer les erreurs sur les coefficients lors
|
---|
| 183 | // d'un fit...
|
---|
| 184 |
|
---|
[958] | 185 | //! Return coefficient of X^dx * Y^dy
|
---|
[220] | 186 | inline double Coef(int dx, int dy) const {
|
---|
[514] | 187 | return (dx>maxDegX || dy>maxDegY) ? 0 : Element(IndCoef(dx,dy));
|
---|
[220] | 188 | }
|
---|
[958] | 189 | //! Return coefficient of X^dx * Y^dy
|
---|
[220] | 190 | inline double& Coef(int dx, int dy) {
|
---|
[2506] | 191 | if (dx>maxDegX || dy>maxDegY) throw RangeCheckError("Poly2::Coef() ");
|
---|
[514] | 192 | dirty = 1; return Element(IndCoef(dx,dy));
|
---|
[220] | 193 | }
|
---|
| 194 | // retourne le coefficient de degre (dx,dy)
|
---|
| 195 |
|
---|
[938] | 196 | double Fit(TVector<r_8> const& x, TVector<r_8> const& y, TVector<r_8> const& z,
|
---|
[220] | 197 | int degreX, int degreY);
|
---|
[938] | 198 | double Fit(TVector<r_8> const& x, TVector<r_8> const& y, TVector<r_8> const& z,
|
---|
| 199 | TVector<r_8> const& errz2, int degreX, int degreY,
|
---|
| 200 | TVector<r_8>& errCoef);
|
---|
[220] | 201 | // degres partiels imposes. cf Poly::Fit sinon
|
---|
| 202 |
|
---|
| 203 |
|
---|
[938] | 204 | double Fit(TVector<r_8> const& x, TVector<r_8> const& y, TVector<r_8> const& z,
|
---|
[220] | 205 | int degre);
|
---|
[938] | 206 | double Fit(TVector<r_8> const& x, TVector<r_8> const& y, TVector<r_8> const& z,
|
---|
| 207 | TVector<r_8> const& errz2, int degre,
|
---|
| 208 | TVector<r_8>& errCoef);
|
---|
[220] | 209 | // degre total impose. cf Poly::Fit sinon
|
---|
| 210 |
|
---|
| 211 | Poly2& operator = (Poly2 const& b);
|
---|
| 212 |
|
---|
| 213 | Poly2& operator += (Poly2 const& b);
|
---|
| 214 | Poly2& operator -= (Poly2 const& b);
|
---|
| 215 |
|
---|
[514] | 216 | Poly2& operator *= (double a);
|
---|
[220] | 217 |
|
---|
[514] | 218 | Poly2 Mult(Poly2 const& b) const;
|
---|
| 219 |
|
---|
[220] | 220 | Poly2 power(int n) const;
|
---|
| 221 |
|
---|
| 222 | Poly2 operator() (Poly const& px, Poly const& py) const;
|
---|
| 223 | // Poly2 operator() (Poly2 const& px, Poly2 const& py) const;
|
---|
| 224 |
|
---|
| 225 | void Realloc(int degreX, int degreY);
|
---|
| 226 |
|
---|
[1584] | 227 | void Print(ostream& s, sa_size_t maxprt=-1,
|
---|
[1557] | 228 | bool si=false, bool ascd=false) const ;
|
---|
[220] | 229 |
|
---|
| 230 | private:
|
---|
| 231 | int dirty;
|
---|
| 232 | int_4 maxDegX;
|
---|
| 233 | int_4 maxDegY;
|
---|
| 234 | int degX;
|
---|
| 235 | int degY;
|
---|
| 236 | int deg;
|
---|
| 237 | void UpdateDeg() const;
|
---|
| 238 | void UpdateDegIfDirty() const {if (dirty) UpdateDeg();}
|
---|
| 239 | };
|
---|
| 240 |
|
---|
[958] | 241 | /*! \ingroup NTools \fn operator*(Poly2 const&,Poly2 const&)
|
---|
| 242 | \brief Perform P(x,y) = a(x,y) * b(x,y) */
|
---|
[514] | 243 | inline Poly2 operator* (Poly2 const& a, Poly2 const& b)
|
---|
| 244 | { return a.Mult(b); }
|
---|
| 245 |
|
---|
[958] | 246 | /*! \ingroup NTools \fn operator+(Poly2 const&,Poly2 const&)
|
---|
| 247 | \brief Perform P(x,y) = a(x,y) + b(x,y) */
|
---|
[514] | 248 | inline Poly2 operator+ (Poly2 const& a, Poly2 const& b)
|
---|
| 249 | { Poly2 c(a); c += b; return c; }
|
---|
| 250 |
|
---|
[958] | 251 | /*! \ingroup NTools \fn operator-(Poly2 const&,Poly2 const&)
|
---|
| 252 | \brief Perform P(x,y) = a(x,y) - b(x,y) */
|
---|
[514] | 253 | inline Poly2 operator- (Poly2 const& a, Poly2 const& b)
|
---|
| 254 | { Poly2 c(a); c -= b; return c; }
|
---|
| 255 |
|
---|
[958] | 256 | /*! \ingroup NTools \fn operator-(double,Poly2 const&)
|
---|
| 257 | \brief Perform P(x,y) = a * b(x,y) */
|
---|
[514] | 258 | inline Poly2 operator * (double a, Poly2 const& b)
|
---|
| 259 | { Poly2 c(b); c *= a; return c; }
|
---|
| 260 |
|
---|
[958] | 261 | /*! \ingroup NTools \fn operator<<(ostream&,const Poly2&)
|
---|
| 262 | \brief Print a(x,y) on stream \b s */
|
---|
[514] | 263 | inline ostream& operator << (ostream& s, const Poly2& a)
|
---|
| 264 | { a.Print(s); return s; }
|
---|
| 265 |
|
---|
| 266 | //////////////////////////////////////////////////////////////////////////
|
---|
[958] | 267 | /*! \ingroup NTools \fn operator<<(POutPersist&,Poly2&)
|
---|
| 268 | \brief For persistance management */
|
---|
[514] | 269 | inline POutPersist& operator << (POutPersist& os, Poly2 & obj)
|
---|
| 270 | { ObjFileIO<Poly2> fio(&obj); fio.Write(os); return(os); }
|
---|
[958] | 271 | /*! \ingroup NTools \fn operator<<(POutPersist&,Poly2&)
|
---|
| 272 | \brief For persistance management */
|
---|
[514] | 273 | inline PInPersist& operator >> (PInPersist& is, Poly2 & obj)
|
---|
| 274 | { ObjFileIO<Poly2> fio(&obj); fio.Read(is); return(is); }
|
---|
| 275 | // Classe pour la gestion de persistance
|
---|
| 276 | // ObjFileIO<Poly2>
|
---|
| 277 |
|
---|
| 278 |
|
---|
| 279 | } // Fin du namespace
|
---|
| 280 |
|
---|
[220] | 281 | #endif // POLY_SEEN
|
---|