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