[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 |
|
---|
[514] | 298 | double rc = LinFit(a,y,(Vector&)*this);
|
---|
[220] | 299 | UpdateDeg();
|
---|
| 300 | return rc;
|
---|
| 301 | }
|
---|
| 302 |
|
---|
| 303 | //++
|
---|
[514] | 304 | double Poly::Fit(Vector const& x, Vector const& y, Vector const& erry2, int degre,
|
---|
| 305 | Vector& errCoef)
|
---|
[220] | 306 | //
|
---|
| 307 | // Ajustement polynomial par moindre carrés. Un polynôme de
|
---|
| 308 | // degré est ajusté sur les données x et y, et stocké dans
|
---|
| 309 | // l'objet courant. erry2 contient le carre des erreurs sur y.
|
---|
| 310 | // Retourne le chi2.
|
---|
| 311 | //--
|
---|
| 312 | {
|
---|
| 313 | int n = x.NElts();
|
---|
| 314 | if (n != y.NElts()) THROW(sizeMismatchErr);
|
---|
| 315 | if (n != erry2.NElts()) THROW(sizeMismatchErr);
|
---|
| 316 |
|
---|
| 317 | Realloc(degre);
|
---|
| 318 | errCoef.Realloc(degre+1);
|
---|
| 319 |
|
---|
[514] | 320 | Matrix a(degre+1, n);
|
---|
[220] | 321 |
|
---|
| 322 | for (int c=0; c<n; c++) {
|
---|
| 323 | double xpow = 1.0;
|
---|
| 324 | for (int l=0; l<=degre; l++) {
|
---|
| 325 | a(l,c) = xpow;
|
---|
| 326 | xpow *= x(c);
|
---|
| 327 | }
|
---|
| 328 | }
|
---|
| 329 |
|
---|
[514] | 330 | double rc = LinFit(a,y,erry2,(Vector&)*this,errCoef);
|
---|
[220] | 331 | UpdateDeg();
|
---|
| 332 | return rc;
|
---|
| 333 | }
|
---|
| 334 |
|
---|
| 335 |
|
---|
| 336 | //++
|
---|
| 337 | // Poly Poly::power(int n) const
|
---|
| 338 | //
|
---|
| 339 | // Retourne le polynôme à la puissance n
|
---|
| 340 | //--
|
---|
| 341 |
|
---|
| 342 | Poly Poly::power(int n) const // a accelerer !!!
|
---|
| 343 | {
|
---|
| 344 | if (n < 0) THROW(rangeCheckErr);
|
---|
| 345 | if (n == 0) { Poly r(0); r[0] = 1; return r;}
|
---|
| 346 | if (n == 1) { return *this; }
|
---|
| 347 | return *this * power(n-1);
|
---|
| 348 | }
|
---|
| 349 |
|
---|
| 350 | //++
|
---|
| 351 | Poly Poly::operator() (Poly const& b) const
|
---|
| 352 | //
|
---|
| 353 | // Substitution d'un polynôme dans un autre.
|
---|
| 354 | //--
|
---|
| 355 | {
|
---|
| 356 | Poly c(b.Degre()*Degre());
|
---|
| 357 | for (int i=0; i<= Degre(); i++)
|
---|
| 358 | c += (*this)[i] * b.power(i);
|
---|
| 359 | return c;
|
---|
| 360 | }
|
---|
| 361 |
|
---|
| 362 |
|
---|
[514] | 363 | //////////////////////////////////////////////////////////////////////////
|
---|
| 364 | void ObjFileIO<Poly>::ReadSelf(PInPersist& is)
|
---|
| 365 | {
|
---|
| 366 | if(dobj==NULL) dobj=new Poly;
|
---|
| 367 | int_4 dg;
|
---|
| 368 | is >> dg;
|
---|
| 369 | dobj->Realloc(dg,true);
|
---|
| 370 | is >> *((Vector *) dobj);
|
---|
| 371 | dobj->UpdateDeg();
|
---|
| 372 | }
|
---|
| 373 |
|
---|
| 374 | void ObjFileIO<Poly>::WriteSelf(POutPersist& os) const
|
---|
| 375 | {
|
---|
| 376 | if(dobj == NULL) return;
|
---|
| 377 | dobj->UpdateDegIfDirty();
|
---|
| 378 | dobj->Realloc(dobj->deg,true);
|
---|
| 379 | os << dobj->deg;
|
---|
| 380 | os << *((Vector *) dobj);
|
---|
| 381 | }
|
---|
| 382 |
|
---|
| 383 | //////////////////////////////////////////////////////////////////////////
|
---|
| 384 | int binomial(int n, int p)
|
---|
| 385 | {
|
---|
| 386 | int c = 1;
|
---|
| 387 | for (int i=n-p+1; i<=n; i++) c *= i;
|
---|
| 388 | for (int j=2; j<=p; j++) c /= j;
|
---|
| 389 | return c;
|
---|
| 390 | }
|
---|
| 391 |
|
---|
| 392 | //////////////////////////////////////////////////////////////////////////
|
---|
[220] | 393 | // ******************* POLY 2 VARIABLES ******************
|
---|
| 394 | //++
|
---|
| 395 | // Class Poly2
|
---|
| 396 | // Lib Outils++
|
---|
| 397 | // include poly.h
|
---|
| 398 | //
|
---|
| 399 | // Classe de calcul sur polynômes à deux variables.
|
---|
| 400 | //--
|
---|
| 401 |
|
---|
| 402 | //++
|
---|
| 403 | // Links Parents
|
---|
[514] | 404 | // Vector
|
---|
[220] | 405 | //--
|
---|
| 406 |
|
---|
| 407 | //++
|
---|
| 408 | // Titre Constructeurs
|
---|
| 409 | //--
|
---|
| 410 |
|
---|
| 411 | //++
|
---|
| 412 | Poly2::Poly2(int degreX, int degreY)
|
---|
| 413 | //
|
---|
| 414 | // Crée un polynôme de degrés partiels degreX et degreY.
|
---|
| 415 | //--
|
---|
[514] | 416 | :Vector((degreX+1)*(degreY+1)), dirty(0),
|
---|
[220] | 417 | maxDegX(degreX), maxDegY(degreY), degX(0), degY(0), deg(0)
|
---|
| 418 | {
|
---|
| 419 | END_CONSTRUCTOR
|
---|
| 420 | }
|
---|
| 421 |
|
---|
| 422 | //++
|
---|
| 423 | Poly2::Poly2(Poly const& polX, Poly const& polY)
|
---|
| 424 | //
|
---|
| 425 | // Crée un polynôme à deux variables comme produit
|
---|
| 426 | // de deux polynômes à une variable, p2(x,y)=px(x)py(y)
|
---|
| 427 | //--
|
---|
[514] | 428 | :Vector((polX.Degre()+1)*(polY.Degre()+1)), dirty(0),
|
---|
[220] | 429 | maxDegX(polX.Degre()), maxDegY(polY.Degre()),
|
---|
| 430 | degX(polX.Degre()), degY(polY.Degre()), deg(degX+degY)
|
---|
| 431 | {
|
---|
| 432 | for (int i=0; i<=degX; i++)
|
---|
| 433 | for (int j=0; j<=degY; j++)
|
---|
| 434 | Coef(i,j) = polX[i]*polY[j];
|
---|
| 435 | END_CONSTRUCTOR
|
---|
| 436 | }
|
---|
| 437 |
|
---|
| 438 | //++
|
---|
| 439 | Poly2::Poly2(Poly2 const& a)
|
---|
| 440 | //
|
---|
| 441 | // Constructeur par copie.
|
---|
| 442 | //--
|
---|
[514] | 443 | :Vector(a), dirty(a.dirty),
|
---|
[220] | 444 | maxDegX(a.maxDegX), maxDegY(a.maxDegY),
|
---|
| 445 | degX(a.degX), degY(a.degY), deg(a.deg)
|
---|
| 446 | {
|
---|
| 447 | END_CONSTRUCTOR
|
---|
| 448 | }
|
---|
| 449 |
|
---|
| 450 |
|
---|
| 451 | //++
|
---|
| 452 | // Titre Méthodes
|
---|
| 453 | //--
|
---|
| 454 |
|
---|
| 455 | //++
|
---|
| 456 | Poly2& Poly2::operator = (Poly2 const& a)
|
---|
| 457 | //
|
---|
| 458 | // Opérateur d'affectation.
|
---|
| 459 | //--
|
---|
| 460 | {
|
---|
| 461 | if (this == &a) return *this;
|
---|
| 462 | if (maxDegX < a.DegX() || maxDegY < a.DegY())
|
---|
| 463 | Realloc(a.DegX(), a.DegY());
|
---|
| 464 |
|
---|
| 465 |
|
---|
| 466 | for (int i=0; i<= maxDegX; i++)
|
---|
| 467 | for (int j=0; j<= maxDegY; j++)
|
---|
| 468 | Coef(i,j) = a.Coef(i,j);
|
---|
| 469 |
|
---|
| 470 | UpdateDeg();
|
---|
| 471 | return *this;
|
---|
| 472 | }
|
---|
| 473 |
|
---|
| 474 | //++
|
---|
| 475 | void Poly2::Realloc(int degreX, int degreY)
|
---|
| 476 | //
|
---|
| 477 | // Redimensionne le polynôme comme etant un
|
---|
| 478 | // polynôme de degrés partiels degreX et degreY.
|
---|
| 479 | //--
|
---|
| 480 | {
|
---|
| 481 | UpdateDegIfDirty();
|
---|
| 482 | Poly2 tmp(*this);
|
---|
[514] | 483 | Vector::Realloc((degreX+1)*(degreY+1));
|
---|
| 484 | Reset();
|
---|
[220] | 485 | maxDegX = degreX;
|
---|
| 486 | maxDegY = degreY;
|
---|
| 487 |
|
---|
[490] | 488 | // Attention - Reza 30/09/99
|
---|
| 489 | // il faut prendre le min en degre du polynome de depart et le nouveau
|
---|
| 490 | int cdegx = (tmp.degX < degreX) ? tmp.degX : degreX;
|
---|
| 491 | int cdegy = (tmp.degY < degreY) ? tmp.degY : degreY;
|
---|
| 492 | for (int i=0; i<= cdegx; i++)
|
---|
| 493 | for (int j=0; j<= cdegy; j++)
|
---|
[220] | 494 | Coef(i,j) = tmp.Coef(i,j);
|
---|
| 495 | }
|
---|
| 496 |
|
---|
| 497 |
|
---|
| 498 | void Poly2::UpdateDeg() const
|
---|
| 499 | {
|
---|
| 500 | (int&)degX=(int&)degY=(int&)deg=0;
|
---|
| 501 |
|
---|
| 502 | for (int dx=0; dx<=maxDegX; dx++)
|
---|
| 503 | for (int dy=0; dy<=maxDegY; dy++)
|
---|
| 504 | if (Coef(dx,dy) != 0) {
|
---|
| 505 | if (dx > degX) (int&)degX = dx;
|
---|
| 506 | if (dy > degY) (int&)degY = dy;
|
---|
| 507 | if (dx+dy > deg) (int&)deg = dx+dy;
|
---|
| 508 | }
|
---|
| 509 |
|
---|
| 510 | (int&)dirty = 0;
|
---|
| 511 | }
|
---|
| 512 |
|
---|
| 513 | //++
|
---|
| 514 | // int Poly2::DegX() const
|
---|
| 515 | // Degré partiel en X.
|
---|
| 516 | // int Poly2::DegY() const
|
---|
| 517 | // Degré partiel en Y
|
---|
| 518 | // int Poly2::MaxDegX() const
|
---|
| 519 | // Degré partiel maximum (alloué) en X
|
---|
| 520 | // int Poly2::MaxDegY() const
|
---|
| 521 | // Degré partiel maximum (alloué) en Y
|
---|
| 522 | // int Poly2::Deg() const
|
---|
| 523 | // Degré total.
|
---|
| 524 | //--
|
---|
| 525 |
|
---|
| 526 | //++
|
---|
| 527 | // double& Poly2::Coef(int dx, int dy)
|
---|
| 528 | // Retourne le coefficient de x^dx y^dy
|
---|
| 529 | // (avec aussi version const).
|
---|
| 530 | //--
|
---|
| 531 |
|
---|
| 532 | //++
|
---|
| 533 | double Poly2::operator()(double x, double y) const
|
---|
| 534 | //
|
---|
| 535 | // Retourne la valeur en (x,y).
|
---|
| 536 | //--
|
---|
| 537 | {
|
---|
| 538 | UpdateDegIfDirty();
|
---|
| 539 | double res = 0;
|
---|
| 540 | double xPow = 1;
|
---|
| 541 | for (int dx=0; dx<=maxDegX; dx++) {
|
---|
| 542 | double yPow = 1;
|
---|
| 543 | for (int dy=0; dy<=maxDegY; dy++) {
|
---|
| 544 | res += Coef(dx,dy) * xPow * yPow;
|
---|
| 545 | yPow *= y;
|
---|
| 546 | }
|
---|
| 547 | xPow *= x;
|
---|
| 548 | }
|
---|
| 549 | return res;
|
---|
| 550 | }
|
---|
| 551 |
|
---|
| 552 | //++
|
---|
[514] | 553 | double Poly2::Fit(Vector const& x, Vector const& y, Vector const& z,
|
---|
[220] | 554 | int degreX, int degreY)
|
---|
| 555 | //
|
---|
| 556 | // Ajustement par moindre carrés z = P(x,y), degrés partiels imposés.
|
---|
| 557 | //--
|
---|
| 558 | {
|
---|
| 559 | int n = x.NElts();
|
---|
| 560 | if (n != y.NElts()) THROW(sizeMismatchErr);
|
---|
| 561 | if (n != z.NElts()) THROW(sizeMismatchErr);
|
---|
| 562 |
|
---|
| 563 | Realloc(degreX, degreY);
|
---|
| 564 |
|
---|
[514] | 565 | Matrix a((degreX+1)*(degreY+1), n);
|
---|
[220] | 566 |
|
---|
| 567 | for (int c=0; c<n; c++) {
|
---|
| 568 | double xPow = 1.0;
|
---|
| 569 | for (int dx = 0; dx <= degreX; dx++) {
|
---|
| 570 | double yPow = 1.0;
|
---|
| 571 | for (int dy = 0; dy <= degreY; dy++) {
|
---|
| 572 | a(IndCoef(dx,dy), c) = xPow*yPow;
|
---|
| 573 | yPow *= y(c);
|
---|
| 574 | }
|
---|
| 575 | xPow *= x(c);
|
---|
| 576 | }
|
---|
| 577 | }
|
---|
| 578 |
|
---|
[514] | 579 | double rc = LinFit(a,z,(Vector&)*this);
|
---|
[220] | 580 | UpdateDeg();
|
---|
| 581 | return rc;
|
---|
| 582 | }
|
---|
| 583 |
|
---|
| 584 |
|
---|
| 585 | //++
|
---|
[514] | 586 | double Poly2::Fit(Vector const& x, Vector const& y, Vector const& z,
|
---|
| 587 | Vector const& errz2, int degreX, int degreY,
|
---|
| 588 | Vector& errCoef)
|
---|
[220] | 589 | //
|
---|
| 590 | // Ajustement par moindre carrés z = P(x,y), degrés partiels imposés,
|
---|
| 591 | // et erreurs^2 sur z dans errz2.
|
---|
| 592 | //--
|
---|
| 593 | {
|
---|
| 594 | int n = x.NElts();
|
---|
| 595 | if (n != y.NElts()) THROW(sizeMismatchErr);
|
---|
| 596 | if (n != z.NElts()) THROW(sizeMismatchErr);
|
---|
| 597 | if (n != errz2.NElts()) THROW(sizeMismatchErr);
|
---|
| 598 |
|
---|
| 599 | Realloc(degreX, degreY);
|
---|
| 600 | errCoef.Realloc((degreX+1)*(degreY+1));
|
---|
| 601 |
|
---|
[514] | 602 | Matrix a((degreX+1)*(degreY+1), n);
|
---|
[220] | 603 |
|
---|
| 604 | for (int c=0; c<n; c++) {
|
---|
| 605 | double xPow = 1.0;
|
---|
| 606 | for (int dx = 0; dx <= degreX; dx++) {
|
---|
| 607 | double yPow = 1.0;
|
---|
| 608 | for (int dy = 0; dy <= degreY; dy++) {
|
---|
| 609 | a(IndCoef(dx,dy), c) = xPow*yPow;
|
---|
| 610 | yPow *= y(c);
|
---|
| 611 | }
|
---|
| 612 | xPow *= x(c);
|
---|
| 613 | }
|
---|
| 614 | }
|
---|
| 615 |
|
---|
[514] | 616 | double rc = LinFit(a,z,errz2,(Vector&)*this,errCoef);
|
---|
[220] | 617 | UpdateDeg();
|
---|
| 618 | return rc;
|
---|
| 619 | }
|
---|
| 620 |
|
---|
| 621 | //++
|
---|
[514] | 622 | double Poly2::Fit(Vector const& x, Vector const& y, Vector const& z,
|
---|
[220] | 623 | int degre)
|
---|
| 624 | //
|
---|
| 625 | // Ajustement par moindre carrés z = P(x,y), degré total imposé.
|
---|
| 626 | //--
|
---|
| 627 | {
|
---|
| 628 | int n = x.NElts();
|
---|
| 629 | if (n != y.NElts()) THROW(sizeMismatchErr);
|
---|
| 630 | if (n != z.NElts()) THROW(sizeMismatchErr);
|
---|
| 631 |
|
---|
| 632 | Realloc(degre, degre); // certains vaudront 0, impose.
|
---|
| 633 |
|
---|
[514] | 634 | Matrix a((degre+1)*(degre+2)/2, n);
|
---|
| 635 | Vector cf((degre+1)*(degre+2)/2);
|
---|
[220] | 636 | #define C_INDEX(i,j) ((i) + (j)*(2*degre+3-(j))/2)
|
---|
| 637 |
|
---|
| 638 | for (int c=0; c<n; c++) {
|
---|
| 639 | double xPow = 1.0;
|
---|
| 640 | for (int dx = 0; dx <= degre; dx++) {
|
---|
| 641 | double yPow = 1.0;
|
---|
| 642 | for (int dy = 0; dy <= degre; dy++) {
|
---|
| 643 | if (dy+dx <= degre)
|
---|
| 644 | a(C_INDEX(dx,dy), c) = xPow*yPow;
|
---|
| 645 | yPow *= y(c);
|
---|
| 646 | }
|
---|
| 647 | xPow *= x(c);
|
---|
| 648 | }
|
---|
| 649 | }
|
---|
| 650 |
|
---|
| 651 | double rc = LinFit(a,z,cf);
|
---|
| 652 |
|
---|
| 653 | for (int dx = 0; dx <= degre; dx++)
|
---|
| 654 | for (int dy = 0; dy <= degre; dy++)
|
---|
| 655 | if (dx+dy <= degre)
|
---|
| 656 | Coef(dx,dy) = cf(C_INDEX(dx,dy));
|
---|
| 657 | else
|
---|
| 658 | Coef(dx,dy) = 0;
|
---|
| 659 |
|
---|
| 660 | UpdateDeg();
|
---|
| 661 | return rc;
|
---|
| 662 | }
|
---|
| 663 |
|
---|
| 664 |
|
---|
| 665 | //++
|
---|
[514] | 666 | double Poly2::Fit(Vector const& x, Vector const& y, Vector const& z,
|
---|
| 667 | Vector const& errz2, int degre,
|
---|
| 668 | Vector& errCoef)
|
---|
[220] | 669 | //
|
---|
| 670 | // Ajustement par moindre carrés z = P(x,y), degré total imposé,
|
---|
| 671 | // et erreurs^2 sur z dans errz2.
|
---|
| 672 | //--
|
---|
| 673 | {
|
---|
| 674 | int n = x.NElts();
|
---|
| 675 | if (n != y.NElts()) THROW(sizeMismatchErr);
|
---|
| 676 | if (n != z.NElts()) THROW(sizeMismatchErr);
|
---|
| 677 | if (n != errz2.NElts()) THROW(sizeMismatchErr);
|
---|
| 678 |
|
---|
| 679 | Realloc(degre, degre);
|
---|
| 680 | errCoef.Realloc((degre+1)*(degre+1));
|
---|
| 681 | #define C_INDEX(i,j) ((i) + (j)*(2*degre+3-(j))/2)
|
---|
| 682 |
|
---|
[514] | 683 | Matrix a((degre+1)*(degre+2)/2, n);
|
---|
| 684 | Vector cf((degre+1)*(degre+2)/2);
|
---|
| 685 | Vector ecf((degre+1)*(degre+2)/2);
|
---|
[220] | 686 |
|
---|
| 687 | for (int c=0; c<n; c++) {
|
---|
| 688 | double xPow = 1.0;
|
---|
| 689 | for (int dx = 0; dx <= degre; dx++) {
|
---|
| 690 | double yPow = 1.0;
|
---|
| 691 | for (int dy = 0; dy <= degre; dy++) {
|
---|
| 692 | if (dy+dx <= degre)
|
---|
| 693 | a(C_INDEX(dx,dy), c) = xPow*yPow;
|
---|
| 694 | yPow *= y(c);
|
---|
| 695 | }
|
---|
| 696 | xPow *= x(c);
|
---|
| 697 | }
|
---|
| 698 | }
|
---|
| 699 |
|
---|
| 700 | double rc = LinFit(a,z,errz2,cf,ecf);
|
---|
| 701 |
|
---|
| 702 |
|
---|
| 703 | for (int dx = 0; dx <= degre; dx++)
|
---|
| 704 | for (int dy = 0; dy <= degre; dy++)
|
---|
| 705 | if (dx+dy <= degre) {
|
---|
| 706 | Coef(dx,dy) = cf(C_INDEX(dx,dy));
|
---|
| 707 | errCoef(IndCoef(dx,dy)) = ecf(C_INDEX(dx,dy));
|
---|
| 708 | } else {
|
---|
| 709 | Coef(dx,dy) = 0;
|
---|
| 710 | errCoef(IndCoef(dx,dy)) = 0;
|
---|
| 711 | }
|
---|
| 712 | UpdateDeg();
|
---|
| 713 | return rc;
|
---|
| 714 | }
|
---|
| 715 |
|
---|
| 716 | //++
|
---|
| 717 | void Poly2::Print(ostream& s) const
|
---|
| 718 | //
|
---|
| 719 | // Impression sur stream s.
|
---|
| 720 | //--
|
---|
| 721 | {
|
---|
| 722 | UpdateDegIfDirty();
|
---|
| 723 | int nz=0;
|
---|
| 724 | int notfirst=0;
|
---|
| 725 | for (int dx = degX; dx>=0; dx--)
|
---|
| 726 | for (int dy= degY; dy>=0; dy--) {
|
---|
| 727 | double c = Coef(dx,dy);
|
---|
| 728 | if (c != 0) {
|
---|
| 729 | nz = 1;
|
---|
| 730 | if (notfirst && c > 0) {
|
---|
| 731 | s << "+";
|
---|
| 732 | notfirst = 1;
|
---|
| 733 | }
|
---|
| 734 | s << c << " ";
|
---|
| 735 | if (dx == 1) s << "* X ";
|
---|
| 736 | if (dx > 1) s << "* X^" << dx << " ";
|
---|
| 737 | if (dy == 1) s << "* Y ";
|
---|
| 738 | if (dy > 1) s << "* Y^" << dy << " ";
|
---|
| 739 | s << endl;
|
---|
| 740 | }
|
---|
| 741 | }
|
---|
| 742 | if (!nz) s << " 0 ";
|
---|
| 743 | }
|
---|
| 744 |
|
---|
| 745 | //++
|
---|
| 746 | // Titre Opérations
|
---|
| 747 | //--
|
---|
| 748 |
|
---|
| 749 | //++
|
---|
| 750 | Poly2& Poly2::operator += (Poly2 const& b)
|
---|
| 751 | //
|
---|
| 752 | //--
|
---|
| 753 | {
|
---|
| 754 | if (maxDegX < b.DegX() || maxDegY < b.DegY())
|
---|
| 755 | Realloc(b.DegX(),b.DegY());
|
---|
| 756 |
|
---|
| 757 | UpdateDegIfDirty();
|
---|
| 758 |
|
---|
| 759 | int mx = b.DegX();
|
---|
| 760 | int my = b.DegY();
|
---|
| 761 | for (int i=0; i<= mx; i++)
|
---|
| 762 | for (int j=0; j<= my; j++)
|
---|
| 763 | Coef(i,j) += b.Coef(i,j);
|
---|
| 764 |
|
---|
| 765 | UpdateDeg();
|
---|
| 766 | return *this;
|
---|
| 767 | }
|
---|
| 768 |
|
---|
| 769 | //++
|
---|
| 770 | Poly2& Poly2::operator -= (Poly2 const& b)
|
---|
| 771 | //
|
---|
| 772 | //--
|
---|
| 773 | {
|
---|
| 774 | if (maxDegX < b.DegX() || maxDegY < b.DegY())
|
---|
| 775 | Realloc(b.DegX(),b.DegY());
|
---|
| 776 |
|
---|
| 777 | UpdateDegIfDirty();
|
---|
| 778 |
|
---|
| 779 | int mx = b.DegX();
|
---|
| 780 | int my = b.DegY();
|
---|
| 781 | for (int i=0; i<= mx; i++)
|
---|
| 782 | for (int j=0; j<= my; j++)
|
---|
| 783 | Coef(i,j) -= b.Coef(i,j);
|
---|
| 784 |
|
---|
| 785 | UpdateDeg();
|
---|
| 786 | return *this;
|
---|
| 787 | }
|
---|
| 788 |
|
---|
| 789 | //++
|
---|
| 790 | Poly2& Poly2::operator *= (double a)
|
---|
| 791 | //
|
---|
| 792 | //--
|
---|
| 793 | {
|
---|
| 794 | for (int i=0; i<NElts(); i++)
|
---|
[514] | 795 | Element(i) *= a;
|
---|
[220] | 796 |
|
---|
| 797 | return *this;
|
---|
| 798 | }
|
---|
| 799 |
|
---|
| 800 | //++
|
---|
[514] | 801 | Poly2 Poly2::Mult(Poly2 const& b) const
|
---|
[220] | 802 | //
|
---|
| 803 | //--
|
---|
| 804 | {
|
---|
[514] | 805 | Poly2 c(DegX() + b.DegX(), DegY() + b.DegY());
|
---|
| 806 | UpdateDegIfDirty();
|
---|
[220] | 807 | b.UpdateDegIfDirty();
|
---|
| 808 |
|
---|
[514] | 809 | for (int i=0; i<=DegX(); i++)
|
---|
| 810 | for (int j=0; j<=DegY(); j++)
|
---|
[220] | 811 | for (int k=0; k<=b.DegX(); k++)
|
---|
| 812 | for (int l=0; l<=b.DegY(); l++)
|
---|
[514] | 813 | c.Coef(i+k,j+l) += Coef(i,j)*b.Coef(k,l);
|
---|
[220] | 814 | return c;
|
---|
| 815 | }
|
---|
| 816 |
|
---|
| 817 | //++
|
---|
| 818 | Poly2 Poly2::power(int n) const
|
---|
| 819 | //
|
---|
| 820 | // Calcule le polynôme P(x,y)^n
|
---|
| 821 | //--
|
---|
| 822 | {
|
---|
| 823 | if (n < 0) THROW(rangeCheckErr);
|
---|
| 824 | if (n == 0) { Poly2 r(0); r.Coef(0,0) = 1; return r;}
|
---|
| 825 | if (n == 1) { return *this; }
|
---|
| 826 | return *this * power(n-1);
|
---|
| 827 | }
|
---|
| 828 |
|
---|
| 829 |
|
---|
| 830 | //++
|
---|
| 831 | Poly2 Poly2::operator() (Poly const& a, Poly const& b) const
|
---|
| 832 | //
|
---|
| 833 | // Substitution de deux polynômes en X et Y,
|
---|
| 834 | // P2(pa(x), pb(y)).
|
---|
| 835 | //--
|
---|
| 836 | {
|
---|
| 837 | UpdateDegIfDirty();
|
---|
| 838 | Poly2 c(maxDegX*a.Degre(), maxDegY*b.Degre());
|
---|
| 839 |
|
---|
| 840 | for (int i=0; i<= degX; i++)
|
---|
| 841 | for (int j=0; j<= degY; j++) {
|
---|
| 842 | Poly2 d(a.power(i), b.power(j));
|
---|
| 843 | c += Coef(i,j) * d;
|
---|
| 844 | }
|
---|
| 845 |
|
---|
| 846 | return c;
|
---|
| 847 | }
|
---|
| 848 |
|
---|
| 849 | //++
|
---|
| 850 | Poly2 Poly::operator() (Poly2 const& a) const
|
---|
| 851 | //
|
---|
| 852 | // Substitution d'un polynôme à deux variables dans
|
---|
| 853 | // un polynôme à une variable, P(P2(x,y)).
|
---|
| 854 | //--
|
---|
| 855 | {
|
---|
| 856 | Poly2 c(a.MaxDegX()*Degre(), a.MaxDegY()*Degre());
|
---|
| 857 |
|
---|
| 858 | for (int i=0; i<= Degre(); i++)
|
---|
| 859 | c += (*this)[i] * a.power(i);
|
---|
| 860 | return c;
|
---|
| 861 | }
|
---|
| 862 |
|
---|
[514] | 863 | //////////////////////////////////////////////////////////////////////////
|
---|
| 864 | void ObjFileIO<Poly2>::ReadSelf(PInPersist& is)
|
---|
| 865 | {
|
---|
| 866 | if(dobj==NULL) dobj=new Poly2;
|
---|
| 867 | int_4 dgx, dgy;
|
---|
| 868 | is >> dgx >> dgy;
|
---|
| 869 | dobj->Realloc(dgx,dgy);
|
---|
| 870 | is >> *((Vector *) dobj);
|
---|
| 871 | dobj->UpdateDeg();
|
---|
| 872 | }
|
---|
[220] | 873 |
|
---|
[514] | 874 | void ObjFileIO<Poly2>::WriteSelf(POutPersist& os) const
|
---|
| 875 | {
|
---|
| 876 | if(dobj == NULL) return;
|
---|
| 877 | os << dobj->maxDegX << dobj->maxDegY;
|
---|
| 878 | os << *((Vector *) dobj);
|
---|
| 879 | }
|
---|
| 880 |
|
---|
| 881 |
|
---|
| 882 | //////////////////////////////////////////////////////////////////////////
|
---|
| 883 | #ifdef __CXX_PRAGMA_TEMPLATES__
|
---|
| 884 | #pragma define_template ObjFileIO<Poly>
|
---|
| 885 | #pragma define_template ObjFileIO<Poly2>
|
---|
| 886 | #endif
|
---|
| 887 |
|
---|
| 888 | #if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
|
---|
| 889 | template class ObjFileIO<Poly>;
|
---|
| 890 | template class ObjFileIO<Poly2>;
|
---|
| 891 | #endif
|
---|