| 1 | #include "machdefs.h" | 
|---|
| 2 | #include "poly.h" | 
|---|
| 3 | #include "linfit.h" | 
|---|
| 4 | #include "fioarr.h" | 
|---|
| 5 |  | 
|---|
| 6 | //////////////////////////////////////////////////////////// | 
|---|
| 7 | //////////////////////////////////////////////////////////// | 
|---|
| 8 | //////////////////////////////////////////////////////////// | 
|---|
| 9 | //////////////////////////////////////////////////////////// | 
|---|
| 10 | //////////////////////////////////////////////////////////// | 
|---|
| 11 | /*! | 
|---|
| 12 | \class SOPHYA::Poly | 
|---|
| 13 | \ingroup NTools | 
|---|
| 14 | One dimensional polynomials class. | 
|---|
| 15 | */ | 
|---|
| 16 |  | 
|---|
| 17 | //! Constructor | 
|---|
| 18 | /*! Create a 1D polynomial of degre \b degre */ | 
|---|
| 19 | Poly::Poly(int degre) | 
|---|
| 20 | : TVector<r_8>(degre+1), dirty(0), deg(0) | 
|---|
| 21 | { | 
|---|
| 22 | END_CONSTRUCTOR | 
|---|
| 23 | } | 
|---|
| 24 |  | 
|---|
| 25 | //! Constructor by copy | 
|---|
| 26 | Poly::Poly(Poly const& a) | 
|---|
| 27 | :TVector<r_8>(a), dirty(a.dirty), deg(a.deg) | 
|---|
| 28 | { | 
|---|
| 29 | END_CONSTRUCTOR | 
|---|
| 30 | } | 
|---|
| 31 |  | 
|---|
| 32 | //! update degre | 
|---|
| 33 | /*! update degre (that could be changed after operations) */ | 
|---|
| 34 | void Poly::UpdateDeg() const | 
|---|
| 35 | { | 
|---|
| 36 | int i = NElts()-1; | 
|---|
| 37 | while (Element(i) == 0 && i>0) i--; | 
|---|
| 38 |  | 
|---|
| 39 | (int&) deg = i;          // bientot mutable dans ANSI C++ | 
|---|
| 40 | (int&) dirty = 0; | 
|---|
| 41 | } | 
|---|
| 42 |  | 
|---|
| 43 | //! compute value P(\b x) | 
|---|
| 44 | double Poly::operator()(double x) const | 
|---|
| 45 | { | 
|---|
| 46 | UpdateDegIfDirty(); | 
|---|
| 47 | double res = Element(deg); | 
|---|
| 48 | for (int i=deg-1; i>=0; i--) { | 
|---|
| 49 | res *= x; | 
|---|
| 50 | res += Element(i); | 
|---|
| 51 | } | 
|---|
| 52 | return res; | 
|---|
| 53 | } | 
|---|
| 54 |  | 
|---|
| 55 | //! Replace p(x) by its derivate | 
|---|
| 56 | void Poly::Derivate() | 
|---|
| 57 | { | 
|---|
| 58 | UpdateDegIfDirty(); | 
|---|
| 59 | if (deg == 0) { Element(0) = 0; return;} | 
|---|
| 60 | for (int i=1; i<=deg; i++) | 
|---|
| 61 | Element(i-1) = Element(i)*i; | 
|---|
| 62 | Element(deg) = 0; | 
|---|
| 63 | deg--; | 
|---|
| 64 | } | 
|---|
| 65 |  | 
|---|
| 66 |  | 
|---|
| 67 | //! Return the derivate in \b der(x) | 
|---|
| 68 | void Poly::Derivate(Poly& der) const | 
|---|
| 69 | { | 
|---|
| 70 | UpdateDegIfDirty(); | 
|---|
| 71 | der.Realloc(deg); | 
|---|
| 72 | //  der.Zero();    // on sait que Realloc met a zero le reste. | 
|---|
| 73 | for (int i=1; i<=deg; i++) | 
|---|
| 74 | der.Element(i-1) = Element(i)*i; | 
|---|
| 75 | } | 
|---|
| 76 |  | 
|---|
| 77 |  | 
|---|
| 78 | //! Return the roots of the polynomial into \b roots | 
|---|
| 79 | /*! | 
|---|
| 80 | This works until degre 2 | 
|---|
| 81 | \return the number of roots | 
|---|
| 82 | */ | 
|---|
| 83 | int Poly::Roots(TVector<r_8>& roots) const | 
|---|
| 84 | { | 
|---|
| 85 | UpdateDegIfDirty(); | 
|---|
| 86 |  | 
|---|
| 87 | switch (deg) | 
|---|
| 88 | { | 
|---|
| 89 | case 0 :           // degre 0 | 
|---|
| 90 | return 0; | 
|---|
| 91 | case 1 :           // degre 1 | 
|---|
| 92 | roots.Realloc(1); | 
|---|
| 93 | return Root1(roots(0)); | 
|---|
| 94 | case 2 :           // degre 2 | 
|---|
| 95 | roots.Realloc(2); | 
|---|
| 96 | return Root2(roots(0),roots(1)); | 
|---|
| 97 | default : | 
|---|
| 98 | THROW(parmErr); | 
|---|
| 99 | } | 
|---|
| 100 | } | 
|---|
| 101 |  | 
|---|
| 102 |  | 
|---|
| 103 | //! Return root \b r for a degre 1 polynomial | 
|---|
| 104 | /*! \return return 1 if succes, 0 if not */ | 
|---|
| 105 | int Poly::Root1(double& r) const | 
|---|
| 106 | { | 
|---|
| 107 | UpdateDegIfDirty(); | 
|---|
| 108 | if (deg != 1) THROW(sizeMismatchErr); | 
|---|
| 109 |  | 
|---|
| 110 | if (Element(1) == 0) return 0; | 
|---|
| 111 | r = -Element(0)/Element(1); | 
|---|
| 112 | return 1; | 
|---|
| 113 | } | 
|---|
| 114 |  | 
|---|
| 115 | //! Return roots \b r1 and \b r2 for a degre 2 polynomial | 
|---|
| 116 | /*! \return return the number of roots found */ | 
|---|
| 117 | int Poly::Root2(double& r1, double& r2) const | 
|---|
| 118 | { | 
|---|
| 119 | UpdateDegIfDirty(); | 
|---|
| 120 | if (deg != 2) THROW(sizeMismatchErr); | 
|---|
| 121 |  | 
|---|
| 122 | double delta = Element(1)*Element(1) - 4*Element(0)*Element(2); | 
|---|
| 123 | if (delta < 0) return 0; | 
|---|
| 124 | if (delta == 0) { | 
|---|
| 125 | r1 = r2 = -Element(1)/2/Element(0); | 
|---|
| 126 | return 1; | 
|---|
| 127 | } | 
|---|
| 128 | r1 = (-Element(1) - sqrt(delta))/2/Element(0); | 
|---|
| 129 | r2 = (-Element(1) + sqrt(delta))/2/Element(0); | 
|---|
| 130 | return 2; | 
|---|
| 131 | } | 
|---|
| 132 |  | 
|---|
| 133 | //! Operator P(x) = a(x) | 
|---|
| 134 | Poly& Poly::operator = (Poly const& a) | 
|---|
| 135 | { | 
|---|
| 136 | if (this == &a) return *this; | 
|---|
| 137 | TVector<r_8>::operator=(a); | 
|---|
| 138 |  | 
|---|
| 139 | UpdateDeg(); | 
|---|
| 140 | return *this; | 
|---|
| 141 | } | 
|---|
| 142 |  | 
|---|
| 143 | //! Perform P(x) += b(x) | 
|---|
| 144 | Poly& Poly::operator += (Poly const& b) | 
|---|
| 145 | { | 
|---|
| 146 | UpdateDegIfDirty(); | 
|---|
| 147 | b.UpdateDegIfDirty(); | 
|---|
| 148 |  | 
|---|
| 149 | if (b.deg > deg) Realloc(b.deg); | 
|---|
| 150 |  | 
|---|
| 151 | int n = (deg > b.deg) ? deg : b.deg; | 
|---|
| 152 | for (int i=0; i<=n; i++) Element(i) += b.Element(i); | 
|---|
| 153 |  | 
|---|
| 154 | UpdateDeg(); | 
|---|
| 155 | return *this; | 
|---|
| 156 | } | 
|---|
| 157 |  | 
|---|
| 158 | //! Perform P(x) -= b(x) | 
|---|
| 159 | Poly& Poly::operator -= (Poly const& b) | 
|---|
| 160 | { | 
|---|
| 161 | UpdateDegIfDirty(); | 
|---|
| 162 | b.UpdateDegIfDirty(); | 
|---|
| 163 |  | 
|---|
| 164 | if (b.deg > deg) Realloc(b.deg); | 
|---|
| 165 |  | 
|---|
| 166 | int n = (deg > b.deg) ? deg : b.deg; | 
|---|
| 167 | for (int i=0; i<=n; i++) Element(i) -= b.Element(i); | 
|---|
| 168 |  | 
|---|
| 169 | UpdateDeg(); | 
|---|
| 170 | return *this; | 
|---|
| 171 | } | 
|---|
| 172 |  | 
|---|
| 173 | //! Perform P(x) *= b(x) | 
|---|
| 174 | Poly& Poly::operator *= (double a) | 
|---|
| 175 | { | 
|---|
| 176 | UpdateDegIfDirty(); | 
|---|
| 177 | for (int i=0; i<=deg; i++) Element(i) *= a; | 
|---|
| 178 | return *this; | 
|---|
| 179 | } | 
|---|
| 180 |  | 
|---|
| 181 | //! Return P(x) = *this(x) * b(x) | 
|---|
| 182 | Poly Poly::Mult(Poly const& b) const | 
|---|
| 183 | { | 
|---|
| 184 | Poly c(deg + b.deg); | 
|---|
| 185 | UpdateDegIfDirty(); | 
|---|
| 186 | b.UpdateDegIfDirty(); | 
|---|
| 187 |  | 
|---|
| 188 | c.deg = deg + b.deg; | 
|---|
| 189 |  | 
|---|
| 190 | for (int i=0; i<=c.deg; i++) { | 
|---|
| 191 | c[i] = 0; | 
|---|
| 192 | int kmin = (i <= deg) ? 0 : i - deg; | 
|---|
| 193 | int kmax = (i <= deg) ? i : deg; | 
|---|
| 194 | for (int k=kmin; k<=kmax; k++) | 
|---|
| 195 | c[i] += (*this)[k] * b[i-k]; | 
|---|
| 196 | } | 
|---|
| 197 | return c; | 
|---|
| 198 | } | 
|---|
| 199 |  | 
|---|
| 200 | //! Print on stream \b s | 
|---|
| 201 | void Poly::Print(ostream& s, int_4 , bool ) const | 
|---|
| 202 | { | 
|---|
| 203 | UpdateDegIfDirty(); | 
|---|
| 204 | int nz=0; | 
|---|
| 205 | for (int i = deg; i>=0; i--) { | 
|---|
| 206 | if ((*this)[i] != 0) { | 
|---|
| 207 | nz = 1; | 
|---|
| 208 | if (i < deg && (*this)[i] > 0) s << "+"; | 
|---|
| 209 | s << (*this)[i]; | 
|---|
| 210 | if (i == 1) s << "*X "; | 
|---|
| 211 | if (i > 1) s << "*X^" << i << " "; | 
|---|
| 212 | } | 
|---|
| 213 | } | 
|---|
| 214 | if (!nz) s << " 0 "; | 
|---|
| 215 | } | 
|---|
| 216 |  | 
|---|
| 217 | //! Fit datas by a polynomial | 
|---|
| 218 | /*! | 
|---|
| 219 | Fit y(x) by a polynimial P(x) | 
|---|
| 220 | \param x : x datas | 
|---|
| 221 | \param y : y datas | 
|---|
| 222 | \param degre : degre of the polynomial P(x) to be fitted | 
|---|
| 223 | \warning result is stored in the current object | 
|---|
| 224 | \return return chisquare | 
|---|
| 225 | */ | 
|---|
| 226 | double Poly::Fit(TVector<r_8> const& x, TVector<r_8> const& y, int degre) | 
|---|
| 227 | { | 
|---|
| 228 | int n = x.NElts(); | 
|---|
| 229 | if (n != (int)y.NElts()) THROW(sizeMismatchErr); | 
|---|
| 230 |  | 
|---|
| 231 | Realloc(degre); | 
|---|
| 232 |  | 
|---|
| 233 | TMatrix<r_8> a(degre+1, n); | 
|---|
| 234 |  | 
|---|
| 235 | for (int c=0; c<n; c++) { | 
|---|
| 236 | double xpow = 1.0; | 
|---|
| 237 | for (int l=0; l<=degre; l++) { | 
|---|
| 238 | a(l,c) = xpow; | 
|---|
| 239 | xpow *= x(c); | 
|---|
| 240 | } | 
|---|
| 241 | } | 
|---|
| 242 |  | 
|---|
| 243 | LinFitter<r_8> lf; | 
|---|
| 244 | double rc = lf.LinFit(a,y,(TVector<r_8>&)*this); | 
|---|
| 245 | UpdateDeg(); | 
|---|
| 246 | return rc; | 
|---|
| 247 | } | 
|---|
| 248 |  | 
|---|
| 249 | //! Fit datas with errors by a polynomial | 
|---|
| 250 | /*! | 
|---|
| 251 | Fit y(x) by a polynimial P(x) | 
|---|
| 252 | \param x : x datas | 
|---|
| 253 | \param y : y datas | 
|---|
| 254 | \param erry2 : errors squared on y | 
|---|
| 255 | \param degre : degre of the polynomial P(x) to be fitted | 
|---|
| 256 | \warning result is stored in the current object | 
|---|
| 257 | \return \b errcoeff : errors on the coefficients | 
|---|
| 258 | \return return chisquare | 
|---|
| 259 | */ | 
|---|
| 260 | double Poly::Fit(TVector<r_8> const& x, TVector<r_8> const& y, | 
|---|
| 261 | TVector<r_8> const& erry2, int degre,TVector<r_8>& errCoef) | 
|---|
| 262 | { | 
|---|
| 263 | int n = x.NElts(); | 
|---|
| 264 | if (n != (int)y.NElts()) THROW(sizeMismatchErr); | 
|---|
| 265 | if (n != (int)erry2.NElts()) THROW(sizeMismatchErr); | 
|---|
| 266 |  | 
|---|
| 267 | Realloc(degre); | 
|---|
| 268 | errCoef.Realloc(degre+1); | 
|---|
| 269 |  | 
|---|
| 270 | TMatrix<r_8> a(degre+1, n); | 
|---|
| 271 |  | 
|---|
| 272 | for (int c=0; c<n; c++) { | 
|---|
| 273 | double xpow = 1.0; | 
|---|
| 274 | for (int l=0; l<=degre; l++) { | 
|---|
| 275 | a(l,c) = xpow; | 
|---|
| 276 | xpow *= x(c); | 
|---|
| 277 | } | 
|---|
| 278 | } | 
|---|
| 279 |  | 
|---|
| 280 | LinFitter<r_8> lf; | 
|---|
| 281 | double rc = lf.LinFit(a,y,erry2,(TVector<r_8>&)*this,errCoef); | 
|---|
| 282 | UpdateDeg(); | 
|---|
| 283 | return rc; | 
|---|
| 284 | } | 
|---|
| 285 |  | 
|---|
| 286 |  | 
|---|
| 287 | //! Return the polynomial at power \b n : ( \f$ P(x)^n \f$ ) | 
|---|
| 288 | Poly Poly::power(int n) const // a accelerer !!! | 
|---|
| 289 | { | 
|---|
| 290 | if (n < 0) THROW(rangeCheckErr); | 
|---|
| 291 | if (n == 0) { Poly r(0); r[0] = 1; return r;} | 
|---|
| 292 | if (n == 1) { return *this; } | 
|---|
| 293 | return *this * power(n-1); | 
|---|
| 294 | } | 
|---|
| 295 |  | 
|---|
| 296 | //! Substitue polynomial and return P\f$ (b(x)) \f$ | 
|---|
| 297 | Poly Poly::operator() (Poly const& b) const | 
|---|
| 298 | { | 
|---|
| 299 | Poly c(b.Degre()*Degre()); | 
|---|
| 300 | for (int i=0; i<= Degre(); i++) | 
|---|
| 301 | c += (*this)[i] * b.power(i); | 
|---|
| 302 | return c; | 
|---|
| 303 | } | 
|---|
| 304 |  | 
|---|
| 305 |  | 
|---|
| 306 | ////////////////////////////////////////////////////////////////////////// | 
|---|
| 307 | //! For persistance management | 
|---|
| 308 | void ObjFileIO<Poly>::ReadSelf(PInPersist& is) | 
|---|
| 309 | { | 
|---|
| 310 | if(dobj==NULL) dobj=new Poly; | 
|---|
| 311 | int_4 dg; | 
|---|
| 312 | is >> dg; | 
|---|
| 313 | dobj->Realloc(dg,true); | 
|---|
| 314 | is >> *((TVector<r_8> *) dobj); | 
|---|
| 315 | dobj->UpdateDeg(); | 
|---|
| 316 | } | 
|---|
| 317 |  | 
|---|
| 318 | //! For persistance management | 
|---|
| 319 | void ObjFileIO<Poly>::WriteSelf(POutPersist& os) const | 
|---|
| 320 | { | 
|---|
| 321 | if(dobj == NULL) return; | 
|---|
| 322 | dobj->UpdateDegIfDirty(); | 
|---|
| 323 | dobj->Realloc(dobj->deg,true); | 
|---|
| 324 | os << dobj->deg; | 
|---|
| 325 | os << *((TVector<r_8> *) dobj); | 
|---|
| 326 | } | 
|---|
| 327 |  | 
|---|
| 328 | ////////////////////////////////////////////////////////////////////////// | 
|---|
| 329 | /*! \ingroup NTools | 
|---|
| 330 | \fn binomial(int,int) | 
|---|
| 331 | Return the binomial coefficient \f$ {C_n}^p \f$. | 
|---|
| 332 | */ | 
|---|
| 333 | int binomial(int n, int p) | 
|---|
| 334 | { | 
|---|
| 335 | int c = 1; | 
|---|
| 336 | for (int i=n-p+1; i<=n; i++) c *= i; | 
|---|
| 337 | for (int j=2; j<=p; j++) c /= j; | 
|---|
| 338 | return c; | 
|---|
| 339 | } | 
|---|
| 340 |  | 
|---|
| 341 |  | 
|---|
| 342 | //////////////////////////////////////////////////////////// | 
|---|
| 343 | //////////////////////////////////////////////////////////// | 
|---|
| 344 | //////////////////////////////////////////////////////////// | 
|---|
| 345 | //////////////////////////////////////////////////////////// | 
|---|
| 346 | //////////////////////////////////////////////////////////// | 
|---|
| 347 | /*! | 
|---|
| 348 | \class SOPHYA::Poly2 | 
|---|
| 349 | \ingroup NTools | 
|---|
| 350 | Two dimensional polynomials class. | 
|---|
| 351 | */ | 
|---|
| 352 |  | 
|---|
| 353 | //! Constructor of 2D polynomial of degres \b degreX \b degreY | 
|---|
| 354 | Poly2::Poly2(int degreX, int degreY) | 
|---|
| 355 | :TVector<r_8>((degreX+1)*(degreY+1)), dirty(0), | 
|---|
| 356 | maxDegX(degreX), maxDegY(degreY), degX(0), degY(0), deg(0) | 
|---|
| 357 | { | 
|---|
| 358 | END_CONSTRUCTOR | 
|---|
| 359 | } | 
|---|
| 360 |  | 
|---|
| 361 | //! Constructor of 2D polynomial \f$ P(x,y) = px(x) * py(y) \f$ | 
|---|
| 362 | Poly2::Poly2(Poly const& polX, Poly const& polY) | 
|---|
| 363 | :TVector<r_8>((polX.Degre()+1)*(polY.Degre()+1)), dirty(0), | 
|---|
| 364 | maxDegX(polX.Degre()), maxDegY(polY.Degre()), | 
|---|
| 365 | degX(polX.Degre()), degY(polY.Degre()), deg(degX+degY) | 
|---|
| 366 | { | 
|---|
| 367 | for (int i=0; i<=degX; i++) | 
|---|
| 368 | for (int j=0; j<=degY; j++) | 
|---|
| 369 | Coef(i,j) = polX[i]*polY[j]; | 
|---|
| 370 | END_CONSTRUCTOR | 
|---|
| 371 | } | 
|---|
| 372 |  | 
|---|
| 373 | //! Constructor by copy | 
|---|
| 374 | Poly2::Poly2(Poly2 const& a) | 
|---|
| 375 | :TVector<r_8>(a), dirty(a.dirty), | 
|---|
| 376 | maxDegX(a.maxDegX), maxDegY(a.maxDegY), | 
|---|
| 377 | degX(a.degX), degY(a.degY), deg(a.deg) | 
|---|
| 378 | { | 
|---|
| 379 | END_CONSTRUCTOR | 
|---|
| 380 | } | 
|---|
| 381 |  | 
|---|
| 382 | //! Operator P(x) = a(x) | 
|---|
| 383 | Poly2& Poly2::operator = (Poly2 const& a) | 
|---|
| 384 | { | 
|---|
| 385 | if (this == &a) return *this; | 
|---|
| 386 | if (maxDegX < a.DegX() || maxDegY < a.DegY()) | 
|---|
| 387 | Realloc(a.DegX(), a.DegY()); | 
|---|
| 388 |  | 
|---|
| 389 |  | 
|---|
| 390 | for (int i=0; i<= maxDegX; i++) | 
|---|
| 391 | for (int j=0; j<= maxDegY; j++) | 
|---|
| 392 | Coef(i,j) = a.Coef(i,j); | 
|---|
| 393 |  | 
|---|
| 394 | UpdateDeg(); | 
|---|
| 395 | return *this; | 
|---|
| 396 | } | 
|---|
| 397 |  | 
|---|
| 398 | //! Re-allocate space for 2D polynomial with partial degres \b degreX \b degreY | 
|---|
| 399 | void Poly2::Realloc(int degreX, int degreY) | 
|---|
| 400 | { | 
|---|
| 401 | UpdateDegIfDirty(); | 
|---|
| 402 | Poly2 tmp(*this); | 
|---|
| 403 | TVector<r_8>::Realloc((degreX+1)*(degreY+1)); | 
|---|
| 404 | DataBlock().Reset(); | 
|---|
| 405 | maxDegX = degreX; | 
|---|
| 406 | maxDegY = degreY; | 
|---|
| 407 |  | 
|---|
| 408 | // Attention - Reza 30/09/99 | 
|---|
| 409 | // il faut prendre le min en degre du polynome de depart et le nouveau | 
|---|
| 410 | int cdegx = (tmp.degX < degreX) ? tmp.degX : degreX; | 
|---|
| 411 | int cdegy = (tmp.degY < degreY) ? tmp.degY : degreY; | 
|---|
| 412 | for (int i=0; i<= cdegx; i++) | 
|---|
| 413 | for (int j=0; j<= cdegy; j++) | 
|---|
| 414 | Coef(i,j) = tmp.Coef(i,j); | 
|---|
| 415 | } | 
|---|
| 416 |  | 
|---|
| 417 |  | 
|---|
| 418 | //! update degres | 
|---|
| 419 | /*! update degres (that could be changed after operations) */ | 
|---|
| 420 | void Poly2::UpdateDeg() const | 
|---|
| 421 | { | 
|---|
| 422 | (int&)degX=(int&)degY=(int&)deg=0; | 
|---|
| 423 |  | 
|---|
| 424 | for (int dx=0; dx<=maxDegX; dx++) | 
|---|
| 425 | for (int dy=0; dy<=maxDegY; dy++) | 
|---|
| 426 | if (Coef(dx,dy) != 0) { | 
|---|
| 427 | if (dx > degX) (int&)degX = dx; | 
|---|
| 428 | if (dy > degY) (int&)degY = dy; | 
|---|
| 429 | if (dx+dy > deg) (int&)deg = dx+dy; | 
|---|
| 430 | } | 
|---|
| 431 |  | 
|---|
| 432 | (int&)dirty = 0; | 
|---|
| 433 | } | 
|---|
| 434 |  | 
|---|
| 435 | //! Return P(\b x, \b y) | 
|---|
| 436 | double Poly2::operator()(double x, double y) const | 
|---|
| 437 | { | 
|---|
| 438 | UpdateDegIfDirty(); | 
|---|
| 439 | double res = 0; | 
|---|
| 440 | double xPow = 1; | 
|---|
| 441 | for (int dx=0; dx<=maxDegX; dx++) { | 
|---|
| 442 | double yPow = 1; | 
|---|
| 443 | for (int dy=0; dy<=maxDegY; dy++) { | 
|---|
| 444 | res += Coef(dx,dy) * xPow * yPow; | 
|---|
| 445 | yPow *= y; | 
|---|
| 446 | } | 
|---|
| 447 | xPow *= x; | 
|---|
| 448 | } | 
|---|
| 449 | return res; | 
|---|
| 450 | } | 
|---|
| 451 |  | 
|---|
| 452 | //! Fit datas by a polynomial | 
|---|
| 453 | /*! | 
|---|
| 454 | Fit z(x,y) by a polynimial P(x,y) | 
|---|
| 455 | \param x : x datas | 
|---|
| 456 | \param y : y datas | 
|---|
| 457 | \param z : z datas | 
|---|
| 458 | \param degreX : partial degre on X | 
|---|
| 459 | \param degreY : partial degre on Y | 
|---|
| 460 | \warning result is stored in the current object | 
|---|
| 461 | \return return chisquare | 
|---|
| 462 | */ | 
|---|
| 463 | double Poly2::Fit(TVector<r_8> const& x, TVector<r_8> const& y, | 
|---|
| 464 | TVector<r_8> const& z, int degreX, int degreY) | 
|---|
| 465 | { | 
|---|
| 466 | int n = x.NElts(); | 
|---|
| 467 | if (n != (int)y.NElts()) THROW(sizeMismatchErr); | 
|---|
| 468 | if (n != (int)z.NElts()) THROW(sizeMismatchErr); | 
|---|
| 469 |  | 
|---|
| 470 | Realloc(degreX, degreY); | 
|---|
| 471 |  | 
|---|
| 472 | TMatrix<r_8> a((degreX+1)*(degreY+1), n); | 
|---|
| 473 |  | 
|---|
| 474 | for (int c=0; c<n; c++) { | 
|---|
| 475 | double xPow = 1.0; | 
|---|
| 476 | for (int dx = 0; dx <= degreX; dx++) { | 
|---|
| 477 | double yPow = 1.0; | 
|---|
| 478 | for (int dy = 0; dy <= degreY; dy++) { | 
|---|
| 479 | a(IndCoef(dx,dy), c) = xPow*yPow; | 
|---|
| 480 | yPow *= y(c); | 
|---|
| 481 | } | 
|---|
| 482 | xPow *= x(c); | 
|---|
| 483 | } | 
|---|
| 484 | } | 
|---|
| 485 |  | 
|---|
| 486 | LinFitter<r_8> lf; | 
|---|
| 487 | double rc = lf.LinFit(a,z,(TVector<r_8>&)*this); | 
|---|
| 488 | UpdateDeg(); | 
|---|
| 489 | return rc; | 
|---|
| 490 | } | 
|---|
| 491 |  | 
|---|
| 492 | //! Fit datas with errors by a polynomial | 
|---|
| 493 | /*! | 
|---|
| 494 | Fit z(x,y) by a polynimial P(x,y) | 
|---|
| 495 | \param x : x datas | 
|---|
| 496 | \param y : y datas | 
|---|
| 497 | \param z : z datas | 
|---|
| 498 | \param errz2 : errors squared on z | 
|---|
| 499 | \param degreX : partial degre on X | 
|---|
| 500 | \param degreY : partial degre on Y | 
|---|
| 501 | \warning result is stored in the current object | 
|---|
| 502 | \return \b errcoeff : errors on the coefficients | 
|---|
| 503 | \return return chisquare | 
|---|
| 504 | */ | 
|---|
| 505 | double Poly2::Fit(TVector<r_8> const& x, TVector<r_8> const& y, TVector<r_8> const& z, | 
|---|
| 506 | TVector<r_8> const& errz2, int degreX, int degreY, | 
|---|
| 507 | TVector<r_8>& errCoef) | 
|---|
| 508 | { | 
|---|
| 509 | int n = x.NElts(); | 
|---|
| 510 | if (n != (int)y.NElts()) THROW(sizeMismatchErr); | 
|---|
| 511 | if (n != (int)z.NElts()) THROW(sizeMismatchErr); | 
|---|
| 512 | if (n != (int)errz2.NElts()) THROW(sizeMismatchErr); | 
|---|
| 513 |  | 
|---|
| 514 | Realloc(degreX, degreY); | 
|---|
| 515 | errCoef.Realloc((degreX+1)*(degreY+1)); | 
|---|
| 516 |  | 
|---|
| 517 | TMatrix<r_8> a((degreX+1)*(degreY+1), n); | 
|---|
| 518 |  | 
|---|
| 519 | for (int c=0; c<n; c++) { | 
|---|
| 520 | double xPow = 1.0; | 
|---|
| 521 | for (int dx = 0; dx <= degreX; dx++) { | 
|---|
| 522 | double yPow = 1.0; | 
|---|
| 523 | for (int dy = 0; dy <= degreY; dy++) { | 
|---|
| 524 | a(IndCoef(dx,dy), c) = xPow*yPow; | 
|---|
| 525 | yPow *= y(c); | 
|---|
| 526 | } | 
|---|
| 527 | xPow *= x(c); | 
|---|
| 528 | } | 
|---|
| 529 | } | 
|---|
| 530 |  | 
|---|
| 531 | LinFitter<r_8> lf; | 
|---|
| 532 | double rc = lf.LinFit(a,z,errz2,(TVector<r_8>&)*this,errCoef); | 
|---|
| 533 | UpdateDeg(); | 
|---|
| 534 | return rc; | 
|---|
| 535 | } | 
|---|
| 536 |  | 
|---|
| 537 | //! Fit datas by a polynomial | 
|---|
| 538 | /*! | 
|---|
| 539 | Fit z(x,y) by a polynimial P(x,y) | 
|---|
| 540 | \param x : x datas | 
|---|
| 541 | \param y : y datas | 
|---|
| 542 | \param z : z datas | 
|---|
| 543 | \param degre : total degre | 
|---|
| 544 | \warning result is stored in the current object | 
|---|
| 545 | \return return chisquare | 
|---|
| 546 | */ | 
|---|
| 547 | double Poly2::Fit(TVector<r_8> const& x, TVector<r_8> const& y, | 
|---|
| 548 | TVector<r_8> const& z, int degre) | 
|---|
| 549 | { | 
|---|
| 550 | int n = x.NElts(); | 
|---|
| 551 | if (n != (int)y.NElts()) THROW(sizeMismatchErr); | 
|---|
| 552 | if (n != (int)z.NElts()) THROW(sizeMismatchErr); | 
|---|
| 553 |  | 
|---|
| 554 | Realloc(degre, degre);   // certains vaudront 0, impose. | 
|---|
| 555 |  | 
|---|
| 556 | TMatrix<r_8> a((degre+1)*(degre+2)/2, n); | 
|---|
| 557 | TVector<r_8> cf((degre+1)*(degre+2)/2); | 
|---|
| 558 | #define C_INDEX(i,j) ((i) + (j)*(2*degre+3-(j))/2) | 
|---|
| 559 |  | 
|---|
| 560 | for (int c=0; c<n; c++) { | 
|---|
| 561 | double xPow = 1.0; | 
|---|
| 562 | for (int dx = 0; dx <= degre; dx++) { | 
|---|
| 563 | double yPow = 1.0; | 
|---|
| 564 | for (int dy = 0; dy <= degre; dy++) { | 
|---|
| 565 | if (dy+dx <= degre) | 
|---|
| 566 | a(C_INDEX(dx,dy), c) = xPow*yPow; | 
|---|
| 567 | yPow *= y(c); | 
|---|
| 568 | } | 
|---|
| 569 | xPow *= x(c); | 
|---|
| 570 | } | 
|---|
| 571 | } | 
|---|
| 572 |  | 
|---|
| 573 | LinFitter<r_8> lf; | 
|---|
| 574 | double rc = lf.LinFit(a,z,cf); | 
|---|
| 575 |  | 
|---|
| 576 | for (int dx = 0; dx <= degre; dx++) | 
|---|
| 577 | for (int dy = 0; dy <= degre; dy++) | 
|---|
| 578 | if (dx+dy <= degre) | 
|---|
| 579 | Coef(dx,dy) = cf(C_INDEX(dx,dy)); | 
|---|
| 580 | else | 
|---|
| 581 | Coef(dx,dy) = 0; | 
|---|
| 582 |  | 
|---|
| 583 | UpdateDeg(); | 
|---|
| 584 | return rc; | 
|---|
| 585 | } | 
|---|
| 586 |  | 
|---|
| 587 | //! Fit datas with errors by a polynomial | 
|---|
| 588 | /*! | 
|---|
| 589 | Fit z(x,y) by a polynimial P(x,y) | 
|---|
| 590 | \param x : x datas | 
|---|
| 591 | \param y : y datas | 
|---|
| 592 | \param z : z datas | 
|---|
| 593 | \param errz2 : errors squared on z | 
|---|
| 594 | \param degre : total degre | 
|---|
| 595 | \warning result is stored in the current object | 
|---|
| 596 | \return \b errcoeff : errors on the coefficients | 
|---|
| 597 | \return return chisquare | 
|---|
| 598 | */ | 
|---|
| 599 | double Poly2::Fit(TVector<r_8> const& x, TVector<r_8> const& y, | 
|---|
| 600 | TVector<r_8> const& z,TVector<r_8> const& errz2, | 
|---|
| 601 | int degre, TVector<r_8>& errCoef) | 
|---|
| 602 | { | 
|---|
| 603 | int n = x.NElts(); | 
|---|
| 604 | if (n != (int)y.NElts()) THROW(sizeMismatchErr); | 
|---|
| 605 | if (n != (int)z.NElts()) THROW(sizeMismatchErr); | 
|---|
| 606 | if (n != (int)errz2.NElts()) THROW(sizeMismatchErr); | 
|---|
| 607 |  | 
|---|
| 608 | Realloc(degre, degre); | 
|---|
| 609 | errCoef.Realloc((degre+1)*(degre+1)); | 
|---|
| 610 | #define C_INDEX(i,j) ((i) + (j)*(2*degre+3-(j))/2) | 
|---|
| 611 |  | 
|---|
| 612 | TMatrix<r_8> a((degre+1)*(degre+2)/2, n); | 
|---|
| 613 | TVector<r_8> cf((degre+1)*(degre+2)/2); | 
|---|
| 614 | TVector<r_8> ecf((degre+1)*(degre+2)/2); | 
|---|
| 615 |  | 
|---|
| 616 | for (int c=0; c<n; c++) { | 
|---|
| 617 | double xPow = 1.0; | 
|---|
| 618 | for (int dx = 0; dx <= degre; dx++) { | 
|---|
| 619 | double yPow = 1.0; | 
|---|
| 620 | for (int dy = 0; dy <= degre; dy++) { | 
|---|
| 621 | if (dy+dx <= degre) | 
|---|
| 622 | a(C_INDEX(dx,dy), c) = xPow*yPow; | 
|---|
| 623 | yPow *= y(c); | 
|---|
| 624 | } | 
|---|
| 625 | xPow *= x(c); | 
|---|
| 626 | } | 
|---|
| 627 | } | 
|---|
| 628 |  | 
|---|
| 629 | LinFitter<r_8> lf; | 
|---|
| 630 | double rc = lf.LinFit(a,z,errz2,cf,ecf); | 
|---|
| 631 |  | 
|---|
| 632 |  | 
|---|
| 633 | for (int dx = 0; dx <= degre; dx++) | 
|---|
| 634 | for (int dy = 0; dy <= degre; dy++) | 
|---|
| 635 | if (dx+dy <= degre) { | 
|---|
| 636 | Coef(dx,dy) = cf(C_INDEX(dx,dy)); | 
|---|
| 637 | errCoef(IndCoef(dx,dy)) = ecf(C_INDEX(dx,dy)); | 
|---|
| 638 | } else { | 
|---|
| 639 | Coef(dx,dy) = 0; | 
|---|
| 640 | errCoef(IndCoef(dx,dy)) = 0; | 
|---|
| 641 | } | 
|---|
| 642 | UpdateDeg(); | 
|---|
| 643 | return rc; | 
|---|
| 644 | } | 
|---|
| 645 |  | 
|---|
| 646 | //! Print on stream \b s | 
|---|
| 647 | void Poly2::Print(ostream& s, int_4 , bool ) const | 
|---|
| 648 | { | 
|---|
| 649 | UpdateDegIfDirty(); | 
|---|
| 650 | int nz=0; | 
|---|
| 651 | int notfirst=0; | 
|---|
| 652 | for (int dx = degX; dx>=0; dx--) | 
|---|
| 653 | for (int dy= degY; dy>=0; dy--) { | 
|---|
| 654 | double c = Coef(dx,dy); | 
|---|
| 655 | if (c != 0) { | 
|---|
| 656 | nz = 1; | 
|---|
| 657 | if (notfirst && c > 0) { | 
|---|
| 658 | s << "+"; | 
|---|
| 659 | notfirst = 1; | 
|---|
| 660 | } | 
|---|
| 661 | s << c << " "; | 
|---|
| 662 | if (dx == 1) s << "* X "; | 
|---|
| 663 | if (dx > 1) s << "* X^" << dx << " "; | 
|---|
| 664 | if (dy == 1) s << "* Y "; | 
|---|
| 665 | if (dy > 1) s << "* Y^" << dy << " "; | 
|---|
| 666 | s << endl; | 
|---|
| 667 | } | 
|---|
| 668 | } | 
|---|
| 669 | if (!nz) s << " 0 "; | 
|---|
| 670 | } | 
|---|
| 671 |  | 
|---|
| 672 | //! Operator: return P(x) = *this(x) + b(x) | 
|---|
| 673 | Poly2& Poly2::operator += (Poly2 const& b) | 
|---|
| 674 | { | 
|---|
| 675 | if (maxDegX < b.DegX() || maxDegY < b.DegY()) | 
|---|
| 676 | Realloc(b.DegX(),b.DegY()); | 
|---|
| 677 |  | 
|---|
| 678 | UpdateDegIfDirty(); | 
|---|
| 679 |  | 
|---|
| 680 | int mx = b.DegX(); | 
|---|
| 681 | int my = b.DegY(); | 
|---|
| 682 | for (int i=0; i<= mx; i++) | 
|---|
| 683 | for (int j=0; j<= my; j++) | 
|---|
| 684 | Coef(i,j) += b.Coef(i,j); | 
|---|
| 685 |  | 
|---|
| 686 | UpdateDeg(); | 
|---|
| 687 | return *this; | 
|---|
| 688 | } | 
|---|
| 689 |  | 
|---|
| 690 | //! Operator: return P(x) = *this(x) - b(x) | 
|---|
| 691 | Poly2& Poly2::operator -= (Poly2 const& b) | 
|---|
| 692 | { | 
|---|
| 693 | if (maxDegX < b.DegX() || maxDegY < b.DegY()) | 
|---|
| 694 | Realloc(b.DegX(),b.DegY()); | 
|---|
| 695 |  | 
|---|
| 696 | UpdateDegIfDirty(); | 
|---|
| 697 |  | 
|---|
| 698 | int mx = b.DegX(); | 
|---|
| 699 | int my = b.DegY(); | 
|---|
| 700 | for (int i=0; i<= mx; i++) | 
|---|
| 701 | for (int j=0; j<= my; j++) | 
|---|
| 702 | Coef(i,j) -= b.Coef(i,j); | 
|---|
| 703 |  | 
|---|
| 704 | UpdateDeg(); | 
|---|
| 705 | return *this; | 
|---|
| 706 | } | 
|---|
| 707 |  | 
|---|
| 708 | //! Operator: return P(x) = *this(x) * a | 
|---|
| 709 | Poly2& Poly2::operator *= (double a) | 
|---|
| 710 | { | 
|---|
| 711 | for (uint_4 i=0; i<NElts(); i++) Element(i) *= a; | 
|---|
| 712 | return *this; | 
|---|
| 713 | } | 
|---|
| 714 |  | 
|---|
| 715 | //! Operator: return P(x) = *this(x) * b(x) | 
|---|
| 716 | Poly2 Poly2::Mult(Poly2 const& b) const | 
|---|
| 717 | { | 
|---|
| 718 | Poly2 c(DegX() + b.DegX(), DegY() + b.DegY()); | 
|---|
| 719 | UpdateDegIfDirty(); | 
|---|
| 720 | b.UpdateDegIfDirty(); | 
|---|
| 721 |  | 
|---|
| 722 | for (int i=0; i<=DegX(); i++) | 
|---|
| 723 | for (int j=0; j<=DegY(); j++) | 
|---|
| 724 | for (int k=0; k<=b.DegX(); k++) | 
|---|
| 725 | for (int l=0; l<=b.DegY(); l++) | 
|---|
| 726 | c.Coef(i+k,j+l) += Coef(i,j)*b.Coef(k,l); | 
|---|
| 727 | return c; | 
|---|
| 728 | } | 
|---|
| 729 |  | 
|---|
| 730 | //! Return \f$ P(x,y)^n \f$ | 
|---|
| 731 | Poly2 Poly2::power(int n) const | 
|---|
| 732 | { | 
|---|
| 733 | if (n < 0) THROW(rangeCheckErr); | 
|---|
| 734 | if (n == 0) { Poly2 r(0); r.Coef(0,0) = 1; return r;} | 
|---|
| 735 | if (n == 1) { return *this; } | 
|---|
| 736 | return *this * power(n-1); | 
|---|
| 737 | } | 
|---|
| 738 |  | 
|---|
| 739 |  | 
|---|
| 740 | //! substitute and return \f$ P(a(x),b(x)) \f$ | 
|---|
| 741 | Poly2 Poly2::operator() (Poly const& a, Poly const& b) const | 
|---|
| 742 | { | 
|---|
| 743 | UpdateDegIfDirty(); | 
|---|
| 744 | Poly2 c(maxDegX*a.Degre(), maxDegY*b.Degre()); | 
|---|
| 745 |  | 
|---|
| 746 | for (int i=0; i<= degX; i++) | 
|---|
| 747 | for (int j=0; j<= degY; j++) { | 
|---|
| 748 | Poly2 d(a.power(i), b.power(j)); | 
|---|
| 749 | c += Coef(i,j) * d; | 
|---|
| 750 | } | 
|---|
| 751 |  | 
|---|
| 752 | return c; | 
|---|
| 753 | } | 
|---|
| 754 |  | 
|---|
| 755 | //! substitute and return 2D polynomial \f$ P(a(x,y)) \f$, P is a 1D polynomial | 
|---|
| 756 | Poly2 Poly::operator() (Poly2 const& a) const | 
|---|
| 757 | { | 
|---|
| 758 | Poly2 c(a.MaxDegX()*Degre(), a.MaxDegY()*Degre()); | 
|---|
| 759 |  | 
|---|
| 760 | for (int i=0; i<= Degre(); i++) | 
|---|
| 761 | c += (*this)[i] * a.power(i); | 
|---|
| 762 | return c; | 
|---|
| 763 | } | 
|---|
| 764 |  | 
|---|
| 765 | ////////////////////////////////////////////////////////////////////////// | 
|---|
| 766 | //! For persistance management | 
|---|
| 767 | void ObjFileIO<Poly2>::ReadSelf(PInPersist& is) | 
|---|
| 768 | { | 
|---|
| 769 | if(dobj==NULL) dobj=new Poly2; | 
|---|
| 770 | int_4 dgx, dgy; | 
|---|
| 771 | is >> dgx >> dgy; | 
|---|
| 772 | dobj->Realloc(dgx,dgy); | 
|---|
| 773 | is >> *((TVector<r_8> *) dobj); | 
|---|
| 774 | dobj->UpdateDeg(); | 
|---|
| 775 | } | 
|---|
| 776 |  | 
|---|
| 777 | //! For persistance management | 
|---|
| 778 | void ObjFileIO<Poly2>::WriteSelf(POutPersist& os) const | 
|---|
| 779 | { | 
|---|
| 780 | if(dobj == NULL) return; | 
|---|
| 781 | os << dobj->maxDegX << dobj->maxDegY; | 
|---|
| 782 | os << *((TVector<r_8> *) dobj); | 
|---|
| 783 | } | 
|---|
| 784 |  | 
|---|
| 785 |  | 
|---|
| 786 | ////////////////////////////////////////////////////////////////////////// | 
|---|
| 787 | #ifdef __CXX_PRAGMA_TEMPLATES__ | 
|---|
| 788 | #pragma define_template ObjFileIO<Poly> | 
|---|
| 789 | #pragma define_template ObjFileIO<Poly2> | 
|---|
| 790 | #endif | 
|---|
| 791 |  | 
|---|
| 792 | #if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES) | 
|---|
| 793 | template class ObjFileIO<Poly>; | 
|---|
| 794 | template class ObjFileIO<Poly2>; | 
|---|
| 795 | #endif | 
|---|