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