source: Sophya/trunk/SophyaLib/NTools/poly.h@ 4029

Last change on this file since 4029 was 2506, checked in by ansari, 22 years ago

Remplacement THROW par throw - Reza 15/03/2004

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