[220] | 1 | // This may look like C code, but it is really -*- C++ -*-
|
---|
| 2 | //
|
---|
| 3 | // $Id: poly.h,v 1.1.1.1 1999-04-09 17:57:58 ansari Exp $
|
---|
| 4 | //
|
---|
| 5 |
|
---|
| 6 |
|
---|
| 7 | // Des polynomes avec des operations de bases et surtout des fits.
|
---|
| 8 |
|
---|
| 9 | #ifndef POLY_SEEN
|
---|
| 10 | #define POLY_SEEN
|
---|
| 11 |
|
---|
| 12 | #include <stdio.h>
|
---|
| 13 | #include "peida.h"
|
---|
| 14 | #include "cvector.h"
|
---|
| 15 |
|
---|
| 16 | class Poly2;
|
---|
| 17 |
|
---|
| 18 | class Poly : public Vector {
|
---|
| 19 | public:
|
---|
| 20 | Poly(int degre = 0);
|
---|
| 21 |
|
---|
| 22 | enum {classId = ClassId_Poly1};
|
---|
| 23 | int_4 ClassId() const { return classId; }
|
---|
| 24 | static PPersist* Create() {return new Poly;}
|
---|
| 25 |
|
---|
| 26 | inline int Degre() const {UpdateDegIfDirty(); return deg;}
|
---|
| 27 |
|
---|
| 28 | inline void Realloc(int n, bool force=false) {Vector::Realloc(n+1,force);}
|
---|
| 29 |
|
---|
| 30 | inline double operator[](int i) const {return data[i];}
|
---|
| 31 | inline double& operator[](int i) {dirty = 1; return data[i];}
|
---|
| 32 | // Retourne le coefficient de degre i
|
---|
| 33 |
|
---|
| 34 | double operator()(double x) const;
|
---|
| 35 | // Retourne la valeur prise en x.
|
---|
| 36 |
|
---|
| 37 | void Derivate();
|
---|
| 38 | // Derive le polynome en place
|
---|
| 39 |
|
---|
| 40 | void Derivate(Poly& der) const;
|
---|
| 41 | // Derive le polynome dans un autre
|
---|
| 42 |
|
---|
| 43 | int Roots(Vector& roots) const;
|
---|
| 44 | // retourne les racines si on peut les calculer...
|
---|
| 45 |
|
---|
| 46 | int Root1(double& r) const;
|
---|
| 47 | // special degre 1
|
---|
| 48 |
|
---|
| 49 | int Root2(double& r1, double& r2) const;
|
---|
| 50 | // special degre 2
|
---|
| 51 |
|
---|
| 52 | friend Poly operator* (Poly const& a, Poly const& b);
|
---|
| 53 | friend Poly operator+ (Poly const& a, Poly const& b);
|
---|
| 54 | friend Poly operator- (Poly const& a, Poly const& b);
|
---|
| 55 |
|
---|
| 56 | Poly& operator = (Poly const& b);
|
---|
| 57 | Poly& operator += (Poly const& b);
|
---|
| 58 | Poly& operator -= (Poly const& b);
|
---|
| 59 |
|
---|
| 60 | friend Poly operator* (double a, Poly const& b);
|
---|
| 61 |
|
---|
| 62 | Poly& operator *= (double a);
|
---|
| 63 |
|
---|
| 64 | Poly power(int n) const;
|
---|
| 65 | Poly operator() (Poly const& b) const;
|
---|
| 66 | Poly2 operator() (Poly2 const& b) const;
|
---|
| 67 |
|
---|
| 68 | void ReadSelf(PInPersist&);
|
---|
| 69 | void WriteSelf(POutPersist&) const;
|
---|
| 70 |
|
---|
| 71 | void Print(ostream& s) const;
|
---|
| 72 | friend ostream& operator << (ostream& s, const Poly& a);
|
---|
| 73 |
|
---|
| 74 | double Fit(Vector const& x, Vector const& y, int degre);
|
---|
| 75 | // Fit d'un polynome de degre donne sur les x et y.
|
---|
| 76 |
|
---|
| 77 | double Fit(Vector const& x, Vector const& y, Vector const& erry2, int degre,
|
---|
| 78 | Vector& errCoef);
|
---|
| 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 |
|
---|
| 89 | int binomial(int n, int p);
|
---|
| 90 |
|
---|
| 91 |
|
---|
| 92 | class Poly2 : public Vector { // Ca pourrait etre une matrice mais
|
---|
| 93 | // la encore, un vecteur est utile pour les
|
---|
| 94 | // fits.
|
---|
| 95 |
|
---|
| 96 | public:
|
---|
| 97 | Poly2(int degreX=0, int degreY=0);
|
---|
| 98 | // degres alloues.
|
---|
| 99 | Poly2(Poly const& polX, Poly const& polY);
|
---|
| 100 | Poly2(Poly2 const& a);
|
---|
| 101 |
|
---|
| 102 | enum {classId = ClassId_Poly2};
|
---|
| 103 | int_4 ClassId() const { return classId; }
|
---|
| 104 | static PPersist* Create() {return new Poly2;}
|
---|
| 105 |
|
---|
| 106 | inline int DegX() const {UpdateDegIfDirty(); return degX;}
|
---|
| 107 | inline int DegY() const {UpdateDegIfDirty(); return degY;}
|
---|
| 108 | inline int MaxDegX() const {return maxDegX;}
|
---|
| 109 | inline int MaxDegY() const {return maxDegY;}
|
---|
| 110 | inline int Deg() const {UpdateDegIfDirty(); return deg;}
|
---|
| 111 | // les degres partiels en x et y, et totaux.
|
---|
| 112 |
|
---|
| 113 | double operator()(double x, double y) const;
|
---|
| 114 | // retourne la valeur en (x,y)
|
---|
| 115 |
|
---|
| 116 | inline int IndCoef(int dx, int dy) const {
|
---|
| 117 | if (dx>maxDegX || dy>maxDegY) THROW(rangeCheckErr);
|
---|
| 118 | return dx + (maxDegX+1)*dy;
|
---|
| 119 | }
|
---|
| 120 | // l'indice du coefficient dans le vecteur. Public uniquement parce
|
---|
| 121 | // que ca sert a recuperer les erreurs sur les coefficients lors
|
---|
| 122 | // d'un fit...
|
---|
| 123 |
|
---|
| 124 | inline double Coef(int dx, int dy) const {
|
---|
| 125 | return (dx>maxDegX || dy>maxDegY) ? 0 : data[IndCoef(dx,dy)];
|
---|
| 126 | }
|
---|
| 127 | inline double& Coef(int dx, int dy) {
|
---|
| 128 | if (dx>maxDegX || dy>maxDegY) THROW(rangeCheckErr);
|
---|
| 129 | dirty = 1; return data[IndCoef(dx,dy)];
|
---|
| 130 | }
|
---|
| 131 | // retourne le coefficient de degre (dx,dy)
|
---|
| 132 |
|
---|
| 133 | double Fit(Vector const& x, Vector const& y, Vector const& z,
|
---|
| 134 | int degreX, int degreY);
|
---|
| 135 | double Fit(Vector const& x, Vector const& y, Vector const& z,
|
---|
| 136 | Vector const& errz2, int degreX, int degreY,
|
---|
| 137 | Vector& errCoef);
|
---|
| 138 | // degres partiels imposes. cf Poly::Fit sinon
|
---|
| 139 |
|
---|
| 140 |
|
---|
| 141 | double Fit(Vector const& x, Vector const& y, Vector const& z,
|
---|
| 142 | int degre);
|
---|
| 143 | double Fit(Vector const& x, Vector const& y, Vector const& z,
|
---|
| 144 | Vector const& errz2, int degre,
|
---|
| 145 | Vector& errCoef);
|
---|
| 146 | // degre total impose. cf Poly::Fit sinon
|
---|
| 147 |
|
---|
| 148 | Poly2& operator = (Poly2 const& b);
|
---|
| 149 |
|
---|
| 150 | Poly2& operator += (Poly2 const& b);
|
---|
| 151 | Poly2& operator -= (Poly2 const& b);
|
---|
| 152 |
|
---|
| 153 | friend Poly2 operator + (Poly2 const& a, Poly2 const& b);
|
---|
| 154 | friend Poly2 operator - (Poly2 const& a, Poly2 const& b);
|
---|
| 155 | friend Poly2 operator * (Poly2 const& a, Poly2 const& b);
|
---|
| 156 |
|
---|
| 157 | Poly2 power(int n) const;
|
---|
| 158 |
|
---|
| 159 | Poly2& operator *= (double a);
|
---|
| 160 | friend Poly2 operator * (double a, Poly2 const& b);
|
---|
| 161 |
|
---|
| 162 | Poly2 operator() (Poly const& px, Poly const& py) const;
|
---|
| 163 | // Poly2 operator() (Poly2 const& px, Poly2 const& py) const;
|
---|
| 164 |
|
---|
| 165 | void Realloc(int degreX, int degreY);
|
---|
| 166 |
|
---|
| 167 | void ReadSelf(PInPersist&);
|
---|
| 168 | void WriteSelf(POutPersist&) const;
|
---|
| 169 |
|
---|
| 170 | void Print(ostream& s) const;
|
---|
| 171 | friend ostream& operator << (ostream& s, const Poly2& a);
|
---|
| 172 |
|
---|
| 173 | private:
|
---|
| 174 | int dirty;
|
---|
| 175 | int_4 maxDegX;
|
---|
| 176 | int_4 maxDegY;
|
---|
| 177 | int degX;
|
---|
| 178 | int degY;
|
---|
| 179 | int deg;
|
---|
| 180 | void UpdateDeg() const;
|
---|
| 181 | void UpdateDegIfDirty() const {if (dirty) UpdateDeg();}
|
---|
| 182 | };
|
---|
| 183 |
|
---|
| 184 | #endif // POLY_SEEN
|
---|