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

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

nbrandom-> SysTools/srandgen.h .c + suppr. pclassid.h Reza 10/4/2000

File size: 6.7 KB
RevLine 
[220]1// This may look like C code, but it is really -*- C++ -*-
2//
[852]3// $Id: poly.h,v 1.6 2000-04-10 12:59:45 ansari Exp $
[220]4//
5
6// Des polynomes avec des operations de bases et surtout des fits.
7
8#ifndef POLY_SEEN
9#define POLY_SEEN
10
[514]11#include "objfio.h"
[220]12#include <stdio.h>
13#include "peida.h"
[514]14#include "tvector.h"
15#include "ppersist.h"
16#include "anydataobj.h"
[220]17
[552]18namespace SOPHYA {
[514]19
[220]20class Poly2;
21
[514]22//////////////////////////////////////////////////////////////////////////
23class Poly : public Vector {
24 friend class ObjFileIO<Poly>;
[220]25public:
26 Poly(int degre = 0);
[514]27 Poly(Poly const& a);
[220]28
29 inline int Degre() const {UpdateDegIfDirty(); return deg;}
30
[514]31 inline void Realloc(int n, bool force=false) {Vector::Realloc(n+1,force);}
[220]32
[805]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);}
[220]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
[514]50 int Roots(Vector& roots) const;
[220]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
[514]65 Poly Mult(Poly const& b) const;
66
[220]67 Poly power(int n) const;
68 Poly operator() (Poly const& b) const;
69 Poly2 operator() (Poly2 const& b) const;
70
[805]71 void Print(ostream& s, int_4 maxprt=-1, bool si=false) const;
[220]72
[514]73 double Fit(Vector const& x, Vector const& y, int degre);
[220]74 // Fit d'un polynome de degre donne sur les x et y.
75
[514]76 double Fit(Vector const& x, Vector const& y, Vector const& erry2, int degre,
77 Vector& errCoef);
[220]78 // En plus, on fournit les carres des erreurs sur y et on a les erreurs
79 // sur les coefficients dans un vecteur.
80
81private:
82 int dirty;
83 int_4 deg;
84 void UpdateDeg() const;
85 void UpdateDegIfDirty() const {if (dirty) UpdateDeg();}
86};
87
[514]88inline Poly operator* (Poly const& a, Poly const& b)
89 { return a.Mult(b); }
[220]90
[514]91inline 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; }
[220]94
[514]95inline 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; }
[220]98
[514]99inline Poly operator* (double a, Poly const& b)
100 { Poly c(b); c *= a; return c; }
101
102inline ostream& operator << (ostream& s, const Poly& a)
103 { a.Print(s); return s; }
104
105//////////////////////////////////////////////////////////////////////////
106inline POutPersist& operator << (POutPersist& os, Poly & obj)
107 { ObjFileIO<Poly> fio(&obj); fio.Write(os); return(os); }
108inline 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//////////////////////////////////////////////////////////////////////////
114int binomial(int n, int p);
115
116//////////////////////////////////////////////////////////////////////////
117class Poly2 : public Vector {
118 friend class ObjFileIO<Poly2>;
[220]119public:
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
[805]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
[220]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 {
[514]148 return (dx>maxDegX || dy>maxDegY) ? 0 : Element(IndCoef(dx,dy));
[220]149 }
150 inline double& Coef(int dx, int dy) {
151 if (dx>maxDegX || dy>maxDegY) THROW(rangeCheckErr);
[514]152 dirty = 1; return Element(IndCoef(dx,dy));
[220]153 }
154 // retourne le coefficient de degre (dx,dy)
155
[514]156 double Fit(Vector const& x, Vector const& y, Vector const& z,
[220]157 int degreX, int degreY);
[514]158 double Fit(Vector const& x, Vector const& y, Vector const& z,
159 Vector const& errz2, int degreX, int degreY,
160 Vector& errCoef);
[220]161 // degres partiels imposes. cf Poly::Fit sinon
162
163
[514]164 double Fit(Vector const& x, Vector const& y, Vector const& z,
[220]165 int degre);
[514]166 double Fit(Vector const& x, Vector const& y, Vector const& z,
167 Vector const& errz2, int degre,
168 Vector& errCoef);
[220]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
[514]176 Poly2& operator *= (double a);
[220]177
[514]178 Poly2 Mult(Poly2 const& b) const;
179
[220]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
[805]187 void Print(ostream& s, int_4 maxprt=-1, bool si=false) const;
[220]188
189private:
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
[514]200inline Poly2 operator* (Poly2 const& a, Poly2 const& b)
201 { return a.Mult(b); }
202
203inline Poly2 operator+ (Poly2 const& a, Poly2 const& b)
204 { Poly2 c(a); c += b; return c; }
205
206inline Poly2 operator- (Poly2 const& a, Poly2 const& b)
207 { Poly2 c(a); c -= b; return c; }
208
209inline Poly2 operator * (double a, Poly2 const& b)
210 { Poly2 c(b); c *= a; return c; }
211
212inline ostream& operator << (ostream& s, const Poly2& a)
213 { a.Print(s); return s; }
214
215//////////////////////////////////////////////////////////////////////////
216inline POutPersist& operator << (POutPersist& os, Poly2 & obj)
217 { ObjFileIO<Poly2> fio(&obj); fio.Write(os); return(os); }
218inline 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
[220]226#endif // POLY_SEEN
Note: See TracBrowser for help on using the repository browser.