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

Last change on this file since 938 was 938, checked in by ansari, 25 years ago

Vector,Matrix -> TVector,TMatrix<r_8> cmv 14/4/00

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