[244] | 1 | #include "machdefs.h"
|
---|
[220] | 2 | #include "poly.h"
|
---|
| 3 | #include "linfit.h"
|
---|
[805] | 4 | #include "fioarr.h"
|
---|
[220] | 5 |
|
---|
| 6 | //++
|
---|
| 7 | // Class Poly
|
---|
| 8 | // Lib Outils++
|
---|
| 9 | // include poly.h
|
---|
| 10 | //
|
---|
| 11 | // Classe de calcul sur polynômes à une variable.
|
---|
| 12 | //--
|
---|
| 13 |
|
---|
| 14 | //++
|
---|
| 15 | // Links Parents
|
---|
[514] | 16 | // Vector
|
---|
[220] | 17 | //--
|
---|
| 18 |
|
---|
| 19 | //++
|
---|
| 20 | // Titre Constructeurs
|
---|
| 21 | //--
|
---|
| 22 |
|
---|
| 23 |
|
---|
[514] | 24 | //////////////////////////////////////////////////////////////////////////
|
---|
[220] | 25 | //++
|
---|
| 26 | // Poly::Poly(int degre=0)
|
---|
| 27 | //
|
---|
| 28 | // Crée un nouveau polynôme, en allouant de la place pour
|
---|
| 29 | // le degré spécifié.
|
---|
| 30 | //--
|
---|
| 31 |
|
---|
| 32 | Poly::Poly(int degre)
|
---|
[514] | 33 | : Vector(degre+1), dirty(0), deg(0)
|
---|
[220] | 34 | {
|
---|
| 35 | END_CONSTRUCTOR
|
---|
| 36 | }
|
---|
| 37 |
|
---|
[514] | 38 | //++
|
---|
| 39 | Poly::Poly(Poly const& a)
|
---|
| 40 | //
|
---|
| 41 | // Constructeur par copie.
|
---|
| 42 | //--
|
---|
| 43 | :Vector(a), dirty(a.dirty), deg(a.deg)
|
---|
| 44 | {
|
---|
| 45 | END_CONSTRUCTOR
|
---|
| 46 | }
|
---|
[220] | 47 |
|
---|
[514] | 48 |
|
---|
[220] | 49 | void Poly::UpdateDeg() const
|
---|
| 50 | {
|
---|
[514] | 51 | int i = NElts()-1;
|
---|
| 52 | while (Element(i) == 0 && i>0) i--;
|
---|
[220] | 53 |
|
---|
| 54 | (int&) deg = i; // bientot mutable dans ANSI C++
|
---|
| 55 | (int&) dirty = 0;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | //++
|
---|
| 59 | // Titre Méthodes
|
---|
| 60 | //--
|
---|
| 61 |
|
---|
| 62 | //++
|
---|
| 63 | // double& Poly::operator[](int i)
|
---|
| 64 | // Permet d'accéder au coefficient de degré i (avec version
|
---|
| 65 | // const).
|
---|
| 66 | //--
|
---|
| 67 |
|
---|
| 68 | //++
|
---|
| 69 | double Poly::operator()(double x) const
|
---|
| 70 | //
|
---|
| 71 | // Calcule la valeur du polynôme au point x.
|
---|
| 72 | //--
|
---|
| 73 | {
|
---|
| 74 | UpdateDegIfDirty();
|
---|
[514] | 75 | double res = Element(deg);
|
---|
[220] | 76 | for (int i=deg-1; i>=0; i--) {
|
---|
| 77 | res *= x;
|
---|
[514] | 78 | res += Element(i);
|
---|
[220] | 79 | }
|
---|
| 80 | return res;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | //++
|
---|
| 84 | void Poly::Derivate()
|
---|
| 85 | //
|
---|
| 86 | // Remplace le polynôme par le polynôme dérivé.
|
---|
| 87 | //--
|
---|
| 88 | {
|
---|
| 89 | UpdateDegIfDirty();
|
---|
[514] | 90 | if (deg == 0) { Element(0) = 0; return;}
|
---|
[220] | 91 | for (int i=1; i<=deg; i++)
|
---|
[514] | 92 | Element(i-1) = Element(i)*i;
|
---|
| 93 | Element(deg) = 0;
|
---|
[220] | 94 | deg--;
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 |
|
---|
| 98 | //++
|
---|
| 99 | void Poly::Derivate(Poly& der) const
|
---|
| 100 | //
|
---|
| 101 | // Retourne dans der le polynôme dérivé.
|
---|
| 102 | //--
|
---|
| 103 | {
|
---|
| 104 | UpdateDegIfDirty();
|
---|
| 105 | der.Realloc(deg);
|
---|
| 106 | // der.Zero(); // on sait que Realloc met a zero le reste.
|
---|
| 107 | for (int i=1; i<=deg; i++)
|
---|
[514] | 108 | der.Element(i-1) = Element(i)*i;
|
---|
[220] | 109 | }
|
---|
| 110 |
|
---|
| 111 |
|
---|
| 112 | //++
|
---|
[514] | 113 | int Poly::Roots(Vector& roots) const
|
---|
[220] | 114 | //
|
---|
| 115 | // Retourne dans roots les racines réelles, si on sait
|
---|
| 116 | // les calculer. Retourne le nombre de racines.
|
---|
| 117 | //--
|
---|
| 118 | {
|
---|
| 119 | UpdateDegIfDirty();
|
---|
| 120 |
|
---|
| 121 | switch (deg)
|
---|
| 122 | {
|
---|
| 123 | case 0 : // degre 0
|
---|
| 124 | return 0;
|
---|
| 125 | case 1 : // degre 1
|
---|
| 126 | roots.Realloc(1);
|
---|
| 127 | return Root1(roots(0));
|
---|
| 128 | case 2 : // degre 2
|
---|
| 129 | roots.Realloc(2);
|
---|
| 130 | return Root2(roots(0),roots(1));
|
---|
| 131 | default :
|
---|
| 132 | THROW(parmErr);
|
---|
| 133 | }
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 |
|
---|
| 137 | //++
|
---|
| 138 | int Poly::Root1(double& r) const
|
---|
| 139 | //
|
---|
| 140 | // Seulement si le polynôme est de degré 1: retourne
|
---|
| 141 | // la racine dans "r". Retourne 1 (nombre de racines).
|
---|
| 142 | //--
|
---|
| 143 | {
|
---|
| 144 | UpdateDegIfDirty();
|
---|
| 145 | if (deg != 1) THROW(sizeMismatchErr);
|
---|
| 146 |
|
---|
[514] | 147 | if (Element(1) == 0) return 0;
|
---|
| 148 | r = -Element(0)/Element(1);
|
---|
[220] | 149 | return 1;
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | //++
|
---|
| 153 | int Poly::Root2(double& r1, double& r2) const
|
---|
| 154 | //
|
---|
| 155 | // Seulement si le polynôme est de degre 2: retourne
|
---|
| 156 | // les racines dans "r1" et "r2". Retourne 0, 1 ou 2
|
---|
| 157 | // (nombre de racines).
|
---|
| 158 | //--
|
---|
| 159 | {
|
---|
| 160 | UpdateDegIfDirty();
|
---|
| 161 | if (deg != 2) THROW(sizeMismatchErr);
|
---|
| 162 |
|
---|
[514] | 163 | double delta = Element(1)*Element(1) - 4*Element(0)*Element(2);
|
---|
[220] | 164 | if (delta < 0) return 0;
|
---|
| 165 | if (delta == 0) {
|
---|
[514] | 166 | r1 = r2 = -Element(1)/2/Element(0);
|
---|
[220] | 167 | return 1;
|
---|
| 168 | }
|
---|
[514] | 169 | r1 = (-Element(1) - sqrt(delta))/2/Element(0);
|
---|
| 170 | r2 = (-Element(1) + sqrt(delta))/2/Element(0);
|
---|
[220] | 171 | return 2;
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | //++
|
---|
| 175 | Poly& Poly::operator = (Poly const& a)
|
---|
| 176 | //
|
---|
| 177 | // Opérateur d'affectation.
|
---|
| 178 | //--
|
---|
| 179 | {
|
---|
| 180 | if (this == &a) return *this;
|
---|
[514] | 181 | Vector::operator=(a);
|
---|
[220] | 182 |
|
---|
| 183 | UpdateDeg();
|
---|
| 184 | return *this;
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | //++
|
---|
| 188 | // Titres Opérations sur polynômes
|
---|
| 189 | //--
|
---|
| 190 |
|
---|
| 191 | //++
|
---|
| 192 | Poly& Poly::operator += (Poly const& b)
|
---|
| 193 | //
|
---|
| 194 | //--
|
---|
| 195 | {
|
---|
| 196 | UpdateDegIfDirty();
|
---|
| 197 | b.UpdateDegIfDirty();
|
---|
| 198 |
|
---|
[514] | 199 | if (b.deg > deg) Realloc(b.deg);
|
---|
[220] | 200 |
|
---|
| 201 | int n = (deg > b.deg) ? deg : b.deg;
|
---|
[514] | 202 | for (int i=0; i<=n; i++) Element(i) += b.Element(i);
|
---|
[220] | 203 |
|
---|
| 204 | UpdateDeg();
|
---|
| 205 | return *this;
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 | //++
|
---|
| 209 | Poly& Poly::operator -= (Poly const& b)
|
---|
| 210 | //
|
---|
| 211 | //--
|
---|
| 212 | {
|
---|
| 213 | UpdateDegIfDirty();
|
---|
| 214 | b.UpdateDegIfDirty();
|
---|
| 215 |
|
---|
[514] | 216 | if (b.deg > deg) Realloc(b.deg);
|
---|
[220] | 217 |
|
---|
| 218 | int n = (deg > b.deg) ? deg : b.deg;
|
---|
[514] | 219 | for (int i=0; i<=n; i++) Element(i) -= b.Element(i);
|
---|
[220] | 220 |
|
---|
| 221 | UpdateDeg();
|
---|
| 222 | return *this;
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | //++
|
---|
[514] | 226 | Poly& Poly::operator *= (double a)
|
---|
[220] | 227 | //
|
---|
| 228 | //--
|
---|
| 229 | {
|
---|
[514] | 230 | UpdateDegIfDirty();
|
---|
| 231 | for (int i=0; i<=deg; i++) Element(i) *= a;
|
---|
| 232 | return *this;
|
---|
[220] | 233 | }
|
---|
| 234 |
|
---|
| 235 | //++
|
---|
[514] | 236 | Poly Poly::Mult(Poly const& b) const
|
---|
[220] | 237 | //
|
---|
| 238 | //--
|
---|
| 239 | {
|
---|
[514] | 240 | Poly c(deg + b.deg);
|
---|
| 241 | UpdateDegIfDirty();
|
---|
[220] | 242 | b.UpdateDegIfDirty();
|
---|
| 243 |
|
---|
[514] | 244 | c.deg = deg + b.deg;
|
---|
[220] | 245 |
|
---|
| 246 | for (int i=0; i<=c.deg; i++) {
|
---|
| 247 | c[i] = 0;
|
---|
[514] | 248 | int kmin = (i <= deg) ? 0 : i - deg;
|
---|
| 249 | int kmax = (i <= deg) ? i : deg;
|
---|
[220] | 250 | for (int k=kmin; k<=kmax; k++)
|
---|
[514] | 251 | c[i] += (*this)[k] * b[i-k];
|
---|
[220] | 252 | }
|
---|
| 253 | return c;
|
---|
| 254 | }
|
---|
| 255 |
|
---|
| 256 | //++
|
---|
[805] | 257 | void Poly::Print(ostream& s, int_4 , bool ) const
|
---|
[220] | 258 | //
|
---|
| 259 | // Impresssion.
|
---|
| 260 | //--
|
---|
| 261 | {
|
---|
| 262 | UpdateDegIfDirty();
|
---|
| 263 | int nz=0;
|
---|
| 264 | for (int i = deg; i>=0; i--) {
|
---|
| 265 | if ((*this)[i] != 0) {
|
---|
| 266 | nz = 1;
|
---|
| 267 | if (i < deg && (*this)[i] > 0) s << "+";
|
---|
| 268 | s << (*this)[i];
|
---|
| 269 | if (i == 1) s << "*X ";
|
---|
| 270 | if (i > 1) s << "*X^" << i << " ";
|
---|
| 271 | }
|
---|
| 272 | }
|
---|
| 273 | if (!nz) s << " 0 ";
|
---|
| 274 | }
|
---|
| 275 |
|
---|
| 276 | //++
|
---|
[514] | 277 | double Poly::Fit(Vector const& x, Vector const& y, int degre)
|
---|
[220] | 278 | //
|
---|
| 279 | // Ajustement polynomial par moindre carrés. Un polynôme de
|
---|
| 280 | // degré "degre" est ajusté sur les données "x" et "y", et stocké dans
|
---|
| 281 | // l'objet courant. Retourne le chi2.
|
---|
| 282 | //--
|
---|
| 283 | {
|
---|
| 284 | int n = x.NElts();
|
---|
| 285 | if (n != y.NElts()) THROW(sizeMismatchErr);
|
---|
| 286 |
|
---|
| 287 | Realloc(degre);
|
---|
| 288 |
|
---|
[514] | 289 | Matrix a(degre+1, n);
|
---|
[220] | 290 |
|
---|
| 291 | for (int c=0; c<n; c++) {
|
---|
| 292 | double xpow = 1.0;
|
---|
| 293 | for (int l=0; l<=degre; l++) {
|
---|
| 294 | a(l,c) = xpow;
|
---|
| 295 | xpow *= x(c);
|
---|
| 296 | }
|
---|
| 297 | }
|
---|
| 298 |
|
---|
[540] | 299 | LinFitter lf;
|
---|
| 300 | double rc = lf.LinFit(a,y,(Vector&)*this);
|
---|
[220] | 301 | UpdateDeg();
|
---|
| 302 | return rc;
|
---|
| 303 | }
|
---|
| 304 |
|
---|
| 305 | //++
|
---|
[514] | 306 | double Poly::Fit(Vector const& x, Vector const& y, Vector const& erry2, int degre,
|
---|
| 307 | Vector& errCoef)
|
---|
[220] | 308 | //
|
---|
| 309 | // Ajustement polynomial par moindre carrés. Un polynôme de
|
---|
| 310 | // degré est ajusté sur les données x et y, et stocké dans
|
---|
| 311 | // l'objet courant. erry2 contient le carre des erreurs sur y.
|
---|
| 312 | // Retourne le chi2.
|
---|
| 313 | //--
|
---|
| 314 | {
|
---|
| 315 | int n = x.NElts();
|
---|
| 316 | if (n != y.NElts()) THROW(sizeMismatchErr);
|
---|
| 317 | if (n != erry2.NElts()) THROW(sizeMismatchErr);
|
---|
| 318 |
|
---|
| 319 | Realloc(degre);
|
---|
| 320 | errCoef.Realloc(degre+1);
|
---|
| 321 |
|
---|
[514] | 322 | Matrix a(degre+1, n);
|
---|
[220] | 323 |
|
---|
| 324 | for (int c=0; c<n; c++) {
|
---|
| 325 | double xpow = 1.0;
|
---|
| 326 | for (int l=0; l<=degre; l++) {
|
---|
| 327 | a(l,c) = xpow;
|
---|
| 328 | xpow *= x(c);
|
---|
| 329 | }
|
---|
| 330 | }
|
---|
| 331 |
|
---|
[540] | 332 | LinFitter lf;
|
---|
| 333 | double rc = lf.LinFit(a,y,erry2,(Vector&)*this,errCoef);
|
---|
[220] | 334 | UpdateDeg();
|
---|
| 335 | return rc;
|
---|
| 336 | }
|
---|
| 337 |
|
---|
| 338 |
|
---|
| 339 | //++
|
---|
| 340 | // Poly Poly::power(int n) const
|
---|
| 341 | //
|
---|
| 342 | // Retourne le polynôme à la puissance n
|
---|
| 343 | //--
|
---|
| 344 |
|
---|
| 345 | Poly Poly::power(int n) const // a accelerer !!!
|
---|
| 346 | {
|
---|
| 347 | if (n < 0) THROW(rangeCheckErr);
|
---|
| 348 | if (n == 0) { Poly r(0); r[0] = 1; return r;}
|
---|
| 349 | if (n == 1) { return *this; }
|
---|
| 350 | return *this * power(n-1);
|
---|
| 351 | }
|
---|
| 352 |
|
---|
| 353 | //++
|
---|
| 354 | Poly Poly::operator() (Poly const& b) const
|
---|
| 355 | //
|
---|
| 356 | // Substitution d'un polynôme dans un autre.
|
---|
| 357 | //--
|
---|
| 358 | {
|
---|
| 359 | Poly c(b.Degre()*Degre());
|
---|
| 360 | for (int i=0; i<= Degre(); i++)
|
---|
| 361 | c += (*this)[i] * b.power(i);
|
---|
| 362 | return c;
|
---|
| 363 | }
|
---|
| 364 |
|
---|
| 365 |
|
---|
[514] | 366 | //////////////////////////////////////////////////////////////////////////
|
---|
| 367 | void ObjFileIO<Poly>::ReadSelf(PInPersist& is)
|
---|
| 368 | {
|
---|
| 369 | if(dobj==NULL) dobj=new Poly;
|
---|
| 370 | int_4 dg;
|
---|
| 371 | is >> dg;
|
---|
| 372 | dobj->Realloc(dg,true);
|
---|
| 373 | is >> *((Vector *) dobj);
|
---|
| 374 | dobj->UpdateDeg();
|
---|
| 375 | }
|
---|
| 376 |
|
---|
| 377 | void ObjFileIO<Poly>::WriteSelf(POutPersist& os) const
|
---|
| 378 | {
|
---|
| 379 | if(dobj == NULL) return;
|
---|
| 380 | dobj->UpdateDegIfDirty();
|
---|
| 381 | dobj->Realloc(dobj->deg,true);
|
---|
| 382 | os << dobj->deg;
|
---|
| 383 | os << *((Vector *) dobj);
|
---|
| 384 | }
|
---|
| 385 |
|
---|
| 386 | //////////////////////////////////////////////////////////////////////////
|
---|
| 387 | int binomial(int n, int p)
|
---|
| 388 | {
|
---|
| 389 | int c = 1;
|
---|
| 390 | for (int i=n-p+1; i<=n; i++) c *= i;
|
---|
| 391 | for (int j=2; j<=p; j++) c /= j;
|
---|
| 392 | return c;
|
---|
| 393 | }
|
---|
| 394 |
|
---|
| 395 | //////////////////////////////////////////////////////////////////////////
|
---|
[220] | 396 | // ******************* POLY 2 VARIABLES ******************
|
---|
| 397 | //++
|
---|
| 398 | // Class Poly2
|
---|
| 399 | // Lib Outils++
|
---|
| 400 | // include poly.h
|
---|
| 401 | //
|
---|
| 402 | // Classe de calcul sur polynômes à deux variables.
|
---|
| 403 | //--
|
---|
| 404 |
|
---|
| 405 | //++
|
---|
| 406 | // Links Parents
|
---|
[514] | 407 | // Vector
|
---|
[220] | 408 | //--
|
---|
| 409 |
|
---|
| 410 | //++
|
---|
| 411 | // Titre Constructeurs
|
---|
| 412 | //--
|
---|
| 413 |
|
---|
| 414 | //++
|
---|
| 415 | Poly2::Poly2(int degreX, int degreY)
|
---|
| 416 | //
|
---|
| 417 | // Crée un polynôme de degrés partiels degreX et degreY.
|
---|
| 418 | //--
|
---|
[514] | 419 | :Vector((degreX+1)*(degreY+1)), dirty(0),
|
---|
[220] | 420 | maxDegX(degreX), maxDegY(degreY), degX(0), degY(0), deg(0)
|
---|
| 421 | {
|
---|
| 422 | END_CONSTRUCTOR
|
---|
| 423 | }
|
---|
| 424 |
|
---|
| 425 | //++
|
---|
| 426 | Poly2::Poly2(Poly const& polX, Poly const& polY)
|
---|
| 427 | //
|
---|
| 428 | // Crée un polynôme à deux variables comme produit
|
---|
| 429 | // de deux polynômes à une variable, p2(x,y)=px(x)py(y)
|
---|
| 430 | //--
|
---|
[514] | 431 | :Vector((polX.Degre()+1)*(polY.Degre()+1)), dirty(0),
|
---|
[220] | 432 | maxDegX(polX.Degre()), maxDegY(polY.Degre()),
|
---|
| 433 | degX(polX.Degre()), degY(polY.Degre()), deg(degX+degY)
|
---|
| 434 | {
|
---|
| 435 | for (int i=0; i<=degX; i++)
|
---|
| 436 | for (int j=0; j<=degY; j++)
|
---|
| 437 | Coef(i,j) = polX[i]*polY[j];
|
---|
| 438 | END_CONSTRUCTOR
|
---|
| 439 | }
|
---|
| 440 |
|
---|
| 441 | //++
|
---|
| 442 | Poly2::Poly2(Poly2 const& a)
|
---|
| 443 | //
|
---|
| 444 | // Constructeur par copie.
|
---|
| 445 | //--
|
---|
[514] | 446 | :Vector(a), dirty(a.dirty),
|
---|
[220] | 447 | maxDegX(a.maxDegX), maxDegY(a.maxDegY),
|
---|
| 448 | degX(a.degX), degY(a.degY), deg(a.deg)
|
---|
| 449 | {
|
---|
| 450 | END_CONSTRUCTOR
|
---|
| 451 | }
|
---|
| 452 |
|
---|
| 453 |
|
---|
| 454 | //++
|
---|
| 455 | // Titre Méthodes
|
---|
| 456 | //--
|
---|
| 457 |
|
---|
| 458 | //++
|
---|
| 459 | Poly2& Poly2::operator = (Poly2 const& a)
|
---|
| 460 | //
|
---|
| 461 | // Opérateur d'affectation.
|
---|
| 462 | //--
|
---|
| 463 | {
|
---|
| 464 | if (this == &a) return *this;
|
---|
| 465 | if (maxDegX < a.DegX() || maxDegY < a.DegY())
|
---|
| 466 | Realloc(a.DegX(), a.DegY());
|
---|
| 467 |
|
---|
| 468 |
|
---|
| 469 | for (int i=0; i<= maxDegX; i++)
|
---|
| 470 | for (int j=0; j<= maxDegY; j++)
|
---|
| 471 | Coef(i,j) = a.Coef(i,j);
|
---|
| 472 |
|
---|
| 473 | UpdateDeg();
|
---|
| 474 | return *this;
|
---|
| 475 | }
|
---|
| 476 |
|
---|
| 477 | //++
|
---|
| 478 | void Poly2::Realloc(int degreX, int degreY)
|
---|
| 479 | //
|
---|
| 480 | // Redimensionne le polynôme comme etant un
|
---|
| 481 | // polynôme de degrés partiels degreX et degreY.
|
---|
| 482 | //--
|
---|
| 483 | {
|
---|
| 484 | UpdateDegIfDirty();
|
---|
| 485 | Poly2 tmp(*this);
|
---|
[514] | 486 | Vector::Realloc((degreX+1)*(degreY+1));
|
---|
[805] | 487 | DataBlock().Reset();
|
---|
[220] | 488 | maxDegX = degreX;
|
---|
| 489 | maxDegY = degreY;
|
---|
| 490 |
|
---|
[490] | 491 | // Attention - Reza 30/09/99
|
---|
| 492 | // il faut prendre le min en degre du polynome de depart et le nouveau
|
---|
| 493 | int cdegx = (tmp.degX < degreX) ? tmp.degX : degreX;
|
---|
| 494 | int cdegy = (tmp.degY < degreY) ? tmp.degY : degreY;
|
---|
| 495 | for (int i=0; i<= cdegx; i++)
|
---|
| 496 | for (int j=0; j<= cdegy; j++)
|
---|
[220] | 497 | Coef(i,j) = tmp.Coef(i,j);
|
---|
| 498 | }
|
---|
| 499 |
|
---|
| 500 |
|
---|
| 501 | void Poly2::UpdateDeg() const
|
---|
| 502 | {
|
---|
| 503 | (int&)degX=(int&)degY=(int&)deg=0;
|
---|
| 504 |
|
---|
| 505 | for (int dx=0; dx<=maxDegX; dx++)
|
---|
| 506 | for (int dy=0; dy<=maxDegY; dy++)
|
---|
| 507 | if (Coef(dx,dy) != 0) {
|
---|
| 508 | if (dx > degX) (int&)degX = dx;
|
---|
| 509 | if (dy > degY) (int&)degY = dy;
|
---|
| 510 | if (dx+dy > deg) (int&)deg = dx+dy;
|
---|
| 511 | }
|
---|
| 512 |
|
---|
| 513 | (int&)dirty = 0;
|
---|
| 514 | }
|
---|
| 515 |
|
---|
| 516 | //++
|
---|
| 517 | // int Poly2::DegX() const
|
---|
| 518 | // Degré partiel en X.
|
---|
| 519 | // int Poly2::DegY() const
|
---|
| 520 | // Degré partiel en Y
|
---|
| 521 | // int Poly2::MaxDegX() const
|
---|
| 522 | // Degré partiel maximum (alloué) en X
|
---|
| 523 | // int Poly2::MaxDegY() const
|
---|
| 524 | // Degré partiel maximum (alloué) en Y
|
---|
| 525 | // int Poly2::Deg() const
|
---|
| 526 | // Degré total.
|
---|
| 527 | //--
|
---|
| 528 |
|
---|
| 529 | //++
|
---|
| 530 | // double& Poly2::Coef(int dx, int dy)
|
---|
| 531 | // Retourne le coefficient de x^dx y^dy
|
---|
| 532 | // (avec aussi version const).
|
---|
| 533 | //--
|
---|
| 534 |
|
---|
| 535 | //++
|
---|
| 536 | double Poly2::operator()(double x, double y) const
|
---|
| 537 | //
|
---|
| 538 | // Retourne la valeur en (x,y).
|
---|
| 539 | //--
|
---|
| 540 | {
|
---|
| 541 | UpdateDegIfDirty();
|
---|
| 542 | double res = 0;
|
---|
| 543 | double xPow = 1;
|
---|
| 544 | for (int dx=0; dx<=maxDegX; dx++) {
|
---|
| 545 | double yPow = 1;
|
---|
| 546 | for (int dy=0; dy<=maxDegY; dy++) {
|
---|
| 547 | res += Coef(dx,dy) * xPow * yPow;
|
---|
| 548 | yPow *= y;
|
---|
| 549 | }
|
---|
| 550 | xPow *= x;
|
---|
| 551 | }
|
---|
| 552 | return res;
|
---|
| 553 | }
|
---|
| 554 |
|
---|
| 555 | //++
|
---|
[514] | 556 | double Poly2::Fit(Vector const& x, Vector const& y, Vector const& z,
|
---|
[220] | 557 | int degreX, int degreY)
|
---|
| 558 | //
|
---|
| 559 | // Ajustement par moindre carrés z = P(x,y), degrés partiels imposés.
|
---|
| 560 | //--
|
---|
| 561 | {
|
---|
| 562 | int n = x.NElts();
|
---|
| 563 | if (n != y.NElts()) THROW(sizeMismatchErr);
|
---|
| 564 | if (n != z.NElts()) THROW(sizeMismatchErr);
|
---|
| 565 |
|
---|
| 566 | Realloc(degreX, degreY);
|
---|
| 567 |
|
---|
[514] | 568 | Matrix a((degreX+1)*(degreY+1), n);
|
---|
[220] | 569 |
|
---|
| 570 | for (int c=0; c<n; c++) {
|
---|
| 571 | double xPow = 1.0;
|
---|
| 572 | for (int dx = 0; dx <= degreX; dx++) {
|
---|
| 573 | double yPow = 1.0;
|
---|
| 574 | for (int dy = 0; dy <= degreY; dy++) {
|
---|
| 575 | a(IndCoef(dx,dy), c) = xPow*yPow;
|
---|
| 576 | yPow *= y(c);
|
---|
| 577 | }
|
---|
| 578 | xPow *= x(c);
|
---|
| 579 | }
|
---|
| 580 | }
|
---|
| 581 |
|
---|
[540] | 582 | LinFitter lf;
|
---|
| 583 | double rc = lf.LinFit(a,z,(Vector&)*this);
|
---|
[220] | 584 | UpdateDeg();
|
---|
| 585 | return rc;
|
---|
| 586 | }
|
---|
| 587 |
|
---|
| 588 |
|
---|
| 589 | //++
|
---|
[514] | 590 | double Poly2::Fit(Vector const& x, Vector const& y, Vector const& z,
|
---|
| 591 | Vector const& errz2, int degreX, int degreY,
|
---|
| 592 | Vector& errCoef)
|
---|
[220] | 593 | //
|
---|
| 594 | // Ajustement par moindre carrés z = P(x,y), degrés partiels imposés,
|
---|
| 595 | // et erreurs^2 sur z dans errz2.
|
---|
| 596 | //--
|
---|
| 597 | {
|
---|
| 598 | int n = x.NElts();
|
---|
| 599 | if (n != y.NElts()) THROW(sizeMismatchErr);
|
---|
| 600 | if (n != z.NElts()) THROW(sizeMismatchErr);
|
---|
| 601 | if (n != errz2.NElts()) THROW(sizeMismatchErr);
|
---|
| 602 |
|
---|
| 603 | Realloc(degreX, degreY);
|
---|
| 604 | errCoef.Realloc((degreX+1)*(degreY+1));
|
---|
| 605 |
|
---|
[514] | 606 | Matrix a((degreX+1)*(degreY+1), n);
|
---|
[220] | 607 |
|
---|
| 608 | for (int c=0; c<n; c++) {
|
---|
| 609 | double xPow = 1.0;
|
---|
| 610 | for (int dx = 0; dx <= degreX; dx++) {
|
---|
| 611 | double yPow = 1.0;
|
---|
| 612 | for (int dy = 0; dy <= degreY; dy++) {
|
---|
| 613 | a(IndCoef(dx,dy), c) = xPow*yPow;
|
---|
| 614 | yPow *= y(c);
|
---|
| 615 | }
|
---|
| 616 | xPow *= x(c);
|
---|
| 617 | }
|
---|
| 618 | }
|
---|
| 619 |
|
---|
[540] | 620 | LinFitter lf;
|
---|
| 621 | double rc = lf.LinFit(a,z,errz2,(Vector&)*this,errCoef);
|
---|
[220] | 622 | UpdateDeg();
|
---|
| 623 | return rc;
|
---|
| 624 | }
|
---|
| 625 |
|
---|
| 626 | //++
|
---|
[514] | 627 | double Poly2::Fit(Vector const& x, Vector const& y, Vector const& z,
|
---|
[220] | 628 | int degre)
|
---|
| 629 | //
|
---|
| 630 | // Ajustement par moindre carrés z = P(x,y), degré total imposé.
|
---|
| 631 | //--
|
---|
| 632 | {
|
---|
| 633 | int n = x.NElts();
|
---|
| 634 | if (n != y.NElts()) THROW(sizeMismatchErr);
|
---|
| 635 | if (n != z.NElts()) THROW(sizeMismatchErr);
|
---|
| 636 |
|
---|
| 637 | Realloc(degre, degre); // certains vaudront 0, impose.
|
---|
| 638 |
|
---|
[514] | 639 | Matrix a((degre+1)*(degre+2)/2, n);
|
---|
| 640 | Vector cf((degre+1)*(degre+2)/2);
|
---|
[220] | 641 | #define C_INDEX(i,j) ((i) + (j)*(2*degre+3-(j))/2)
|
---|
| 642 |
|
---|
| 643 | for (int c=0; c<n; c++) {
|
---|
| 644 | double xPow = 1.0;
|
---|
| 645 | for (int dx = 0; dx <= degre; dx++) {
|
---|
| 646 | double yPow = 1.0;
|
---|
| 647 | for (int dy = 0; dy <= degre; dy++) {
|
---|
| 648 | if (dy+dx <= degre)
|
---|
| 649 | a(C_INDEX(dx,dy), c) = xPow*yPow;
|
---|
| 650 | yPow *= y(c);
|
---|
| 651 | }
|
---|
| 652 | xPow *= x(c);
|
---|
| 653 | }
|
---|
| 654 | }
|
---|
| 655 |
|
---|
[540] | 656 | LinFitter lf;
|
---|
| 657 | double rc = lf.LinFit(a,z,cf);
|
---|
[220] | 658 |
|
---|
| 659 | for (int dx = 0; dx <= degre; dx++)
|
---|
| 660 | for (int dy = 0; dy <= degre; dy++)
|
---|
| 661 | if (dx+dy <= degre)
|
---|
| 662 | Coef(dx,dy) = cf(C_INDEX(dx,dy));
|
---|
| 663 | else
|
---|
| 664 | Coef(dx,dy) = 0;
|
---|
| 665 |
|
---|
| 666 | UpdateDeg();
|
---|
| 667 | return rc;
|
---|
| 668 | }
|
---|
| 669 |
|
---|
| 670 |
|
---|
| 671 | //++
|
---|
[514] | 672 | double Poly2::Fit(Vector const& x, Vector const& y, Vector const& z,
|
---|
| 673 | Vector const& errz2, int degre,
|
---|
| 674 | Vector& errCoef)
|
---|
[220] | 675 | //
|
---|
| 676 | // Ajustement par moindre carrés z = P(x,y), degré total imposé,
|
---|
| 677 | // et erreurs^2 sur z dans errz2.
|
---|
| 678 | //--
|
---|
| 679 | {
|
---|
| 680 | int n = x.NElts();
|
---|
| 681 | if (n != y.NElts()) THROW(sizeMismatchErr);
|
---|
| 682 | if (n != z.NElts()) THROW(sizeMismatchErr);
|
---|
| 683 | if (n != errz2.NElts()) THROW(sizeMismatchErr);
|
---|
| 684 |
|
---|
| 685 | Realloc(degre, degre);
|
---|
| 686 | errCoef.Realloc((degre+1)*(degre+1));
|
---|
| 687 | #define C_INDEX(i,j) ((i) + (j)*(2*degre+3-(j))/2)
|
---|
| 688 |
|
---|
[514] | 689 | Matrix a((degre+1)*(degre+2)/2, n);
|
---|
| 690 | Vector cf((degre+1)*(degre+2)/2);
|
---|
| 691 | Vector ecf((degre+1)*(degre+2)/2);
|
---|
[220] | 692 |
|
---|
| 693 | for (int c=0; c<n; c++) {
|
---|
| 694 | double xPow = 1.0;
|
---|
| 695 | for (int dx = 0; dx <= degre; dx++) {
|
---|
| 696 | double yPow = 1.0;
|
---|
| 697 | for (int dy = 0; dy <= degre; dy++) {
|
---|
| 698 | if (dy+dx <= degre)
|
---|
| 699 | a(C_INDEX(dx,dy), c) = xPow*yPow;
|
---|
| 700 | yPow *= y(c);
|
---|
| 701 | }
|
---|
| 702 | xPow *= x(c);
|
---|
| 703 | }
|
---|
| 704 | }
|
---|
| 705 |
|
---|
[540] | 706 | LinFitter lf;
|
---|
| 707 | double rc = lf.LinFit(a,z,errz2,cf,ecf);
|
---|
[220] | 708 |
|
---|
| 709 |
|
---|
| 710 | for (int dx = 0; dx <= degre; dx++)
|
---|
| 711 | for (int dy = 0; dy <= degre; dy++)
|
---|
| 712 | if (dx+dy <= degre) {
|
---|
| 713 | Coef(dx,dy) = cf(C_INDEX(dx,dy));
|
---|
| 714 | errCoef(IndCoef(dx,dy)) = ecf(C_INDEX(dx,dy));
|
---|
| 715 | } else {
|
---|
| 716 | Coef(dx,dy) = 0;
|
---|
| 717 | errCoef(IndCoef(dx,dy)) = 0;
|
---|
| 718 | }
|
---|
| 719 | UpdateDeg();
|
---|
| 720 | return rc;
|
---|
| 721 | }
|
---|
| 722 |
|
---|
| 723 | //++
|
---|
[805] | 724 | void Poly2::Print(ostream& s, int_4 , bool ) const
|
---|
[220] | 725 | //
|
---|
| 726 | // Impression sur stream s.
|
---|
| 727 | //--
|
---|
| 728 | {
|
---|
| 729 | UpdateDegIfDirty();
|
---|
| 730 | int nz=0;
|
---|
| 731 | int notfirst=0;
|
---|
| 732 | for (int dx = degX; dx>=0; dx--)
|
---|
| 733 | for (int dy= degY; dy>=0; dy--) {
|
---|
| 734 | double c = Coef(dx,dy);
|
---|
| 735 | if (c != 0) {
|
---|
| 736 | nz = 1;
|
---|
| 737 | if (notfirst && c > 0) {
|
---|
| 738 | s << "+";
|
---|
| 739 | notfirst = 1;
|
---|
| 740 | }
|
---|
| 741 | s << c << " ";
|
---|
| 742 | if (dx == 1) s << "* X ";
|
---|
| 743 | if (dx > 1) s << "* X^" << dx << " ";
|
---|
| 744 | if (dy == 1) s << "* Y ";
|
---|
| 745 | if (dy > 1) s << "* Y^" << dy << " ";
|
---|
| 746 | s << endl;
|
---|
| 747 | }
|
---|
| 748 | }
|
---|
| 749 | if (!nz) s << " 0 ";
|
---|
| 750 | }
|
---|
| 751 |
|
---|
| 752 | //++
|
---|
| 753 | // Titre Opérations
|
---|
| 754 | //--
|
---|
| 755 |
|
---|
| 756 | //++
|
---|
| 757 | Poly2& Poly2::operator += (Poly2 const& b)
|
---|
| 758 | //
|
---|
| 759 | //--
|
---|
| 760 | {
|
---|
| 761 | if (maxDegX < b.DegX() || maxDegY < b.DegY())
|
---|
| 762 | Realloc(b.DegX(),b.DegY());
|
---|
| 763 |
|
---|
| 764 | UpdateDegIfDirty();
|
---|
| 765 |
|
---|
| 766 | int mx = b.DegX();
|
---|
| 767 | int my = b.DegY();
|
---|
| 768 | for (int i=0; i<= mx; i++)
|
---|
| 769 | for (int j=0; j<= my; j++)
|
---|
| 770 | Coef(i,j) += b.Coef(i,j);
|
---|
| 771 |
|
---|
| 772 | UpdateDeg();
|
---|
| 773 | return *this;
|
---|
| 774 | }
|
---|
| 775 |
|
---|
| 776 | //++
|
---|
| 777 | Poly2& Poly2::operator -= (Poly2 const& b)
|
---|
| 778 | //
|
---|
| 779 | //--
|
---|
| 780 | {
|
---|
| 781 | if (maxDegX < b.DegX() || maxDegY < b.DegY())
|
---|
| 782 | Realloc(b.DegX(),b.DegY());
|
---|
| 783 |
|
---|
| 784 | UpdateDegIfDirty();
|
---|
| 785 |
|
---|
| 786 | int mx = b.DegX();
|
---|
| 787 | int my = b.DegY();
|
---|
| 788 | for (int i=0; i<= mx; i++)
|
---|
| 789 | for (int j=0; j<= my; j++)
|
---|
| 790 | Coef(i,j) -= b.Coef(i,j);
|
---|
| 791 |
|
---|
| 792 | UpdateDeg();
|
---|
| 793 | return *this;
|
---|
| 794 | }
|
---|
| 795 |
|
---|
| 796 | //++
|
---|
| 797 | Poly2& Poly2::operator *= (double a)
|
---|
| 798 | //
|
---|
| 799 | //--
|
---|
| 800 | {
|
---|
| 801 | for (int i=0; i<NElts(); i++)
|
---|
[514] | 802 | Element(i) *= a;
|
---|
[220] | 803 |
|
---|
| 804 | return *this;
|
---|
| 805 | }
|
---|
| 806 |
|
---|
| 807 | //++
|
---|
[514] | 808 | Poly2 Poly2::Mult(Poly2 const& b) const
|
---|
[220] | 809 | //
|
---|
| 810 | //--
|
---|
| 811 | {
|
---|
[514] | 812 | Poly2 c(DegX() + b.DegX(), DegY() + b.DegY());
|
---|
| 813 | UpdateDegIfDirty();
|
---|
[220] | 814 | b.UpdateDegIfDirty();
|
---|
| 815 |
|
---|
[514] | 816 | for (int i=0; i<=DegX(); i++)
|
---|
| 817 | for (int j=0; j<=DegY(); j++)
|
---|
[220] | 818 | for (int k=0; k<=b.DegX(); k++)
|
---|
| 819 | for (int l=0; l<=b.DegY(); l++)
|
---|
[514] | 820 | c.Coef(i+k,j+l) += Coef(i,j)*b.Coef(k,l);
|
---|
[220] | 821 | return c;
|
---|
| 822 | }
|
---|
| 823 |
|
---|
| 824 | //++
|
---|
| 825 | Poly2 Poly2::power(int n) const
|
---|
| 826 | //
|
---|
| 827 | // Calcule le polynôme P(x,y)^n
|
---|
| 828 | //--
|
---|
| 829 | {
|
---|
| 830 | if (n < 0) THROW(rangeCheckErr);
|
---|
| 831 | if (n == 0) { Poly2 r(0); r.Coef(0,0) = 1; return r;}
|
---|
| 832 | if (n == 1) { return *this; }
|
---|
| 833 | return *this * power(n-1);
|
---|
| 834 | }
|
---|
| 835 |
|
---|
| 836 |
|
---|
| 837 | //++
|
---|
| 838 | Poly2 Poly2::operator() (Poly const& a, Poly const& b) const
|
---|
| 839 | //
|
---|
| 840 | // Substitution de deux polynômes en X et Y,
|
---|
| 841 | // P2(pa(x), pb(y)).
|
---|
| 842 | //--
|
---|
| 843 | {
|
---|
| 844 | UpdateDegIfDirty();
|
---|
| 845 | Poly2 c(maxDegX*a.Degre(), maxDegY*b.Degre());
|
---|
| 846 |
|
---|
| 847 | for (int i=0; i<= degX; i++)
|
---|
| 848 | for (int j=0; j<= degY; j++) {
|
---|
| 849 | Poly2 d(a.power(i), b.power(j));
|
---|
| 850 | c += Coef(i,j) * d;
|
---|
| 851 | }
|
---|
| 852 |
|
---|
| 853 | return c;
|
---|
| 854 | }
|
---|
| 855 |
|
---|
| 856 | //++
|
---|
| 857 | Poly2 Poly::operator() (Poly2 const& a) const
|
---|
| 858 | //
|
---|
| 859 | // Substitution d'un polynôme à deux variables dans
|
---|
| 860 | // un polynôme à une variable, P(P2(x,y)).
|
---|
| 861 | //--
|
---|
| 862 | {
|
---|
| 863 | Poly2 c(a.MaxDegX()*Degre(), a.MaxDegY()*Degre());
|
---|
| 864 |
|
---|
| 865 | for (int i=0; i<= Degre(); i++)
|
---|
| 866 | c += (*this)[i] * a.power(i);
|
---|
| 867 | return c;
|
---|
| 868 | }
|
---|
| 869 |
|
---|
[514] | 870 | //////////////////////////////////////////////////////////////////////////
|
---|
| 871 | void ObjFileIO<Poly2>::ReadSelf(PInPersist& is)
|
---|
| 872 | {
|
---|
| 873 | if(dobj==NULL) dobj=new Poly2;
|
---|
| 874 | int_4 dgx, dgy;
|
---|
| 875 | is >> dgx >> dgy;
|
---|
| 876 | dobj->Realloc(dgx,dgy);
|
---|
| 877 | is >> *((Vector *) dobj);
|
---|
| 878 | dobj->UpdateDeg();
|
---|
| 879 | }
|
---|
[220] | 880 |
|
---|
[514] | 881 | void ObjFileIO<Poly2>::WriteSelf(POutPersist& os) const
|
---|
| 882 | {
|
---|
| 883 | if(dobj == NULL) return;
|
---|
| 884 | os << dobj->maxDegX << dobj->maxDegY;
|
---|
| 885 | os << *((Vector *) dobj);
|
---|
| 886 | }
|
---|
| 887 |
|
---|
| 888 |
|
---|
| 889 | //////////////////////////////////////////////////////////////////////////
|
---|
| 890 | #ifdef __CXX_PRAGMA_TEMPLATES__
|
---|
| 891 | #pragma define_template ObjFileIO<Poly>
|
---|
| 892 | #pragma define_template ObjFileIO<Poly2>
|
---|
| 893 | #endif
|
---|
| 894 |
|
---|
| 895 | #if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
|
---|
| 896 | template class ObjFileIO<Poly>;
|
---|
| 897 | template class ObjFileIO<Poly2>;
|
---|
| 898 | #endif
|
---|