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