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