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