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