Changeset 514 in Sophya for trunk/SophyaLib/NTools/poly.cc


Ignore:
Timestamp:
Oct 25, 1999, 6:43:04 PM (26 years ago)
Author:
ansari
Message:

elimination des OVector/OMatrix au profit des TVector/TMatrix cmv 25/10/99

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/SophyaLib/NTools/poly.cc

    r508 r514  
    1313//++
    1414// Links        Parents
    15 // OVector
     15// Vector
    1616//--
    1717
     
    2121
    2222
     23//////////////////////////////////////////////////////////////////////////
    2324//++
    2425// Poly::Poly(int degre=0)
     
    2930
    3031Poly::Poly(int degre)
    31 : OVector(degre+1), dirty(0), deg(0)
     32: Vector(degre+1), dirty(0), deg(0)
    3233{
    3334  END_CONSTRUCTOR
    3435}
    3536
     37//++
     38Poly::Poly(Poly const& a)
     39//
     40//      Constructeur par copie.
     41//--
     42:Vector(a), dirty(a.dirty), deg(a.deg)
     43{
     44  END_CONSTRUCTOR
     45}
     46
    3647
    3748void Poly::UpdateDeg() const
    3849{
    39   int i = nalloc-1;
    40   while (data[i] == 0 && i>0) i--;
     50  int i = NElts()-1;
     51  while (Element(i) == 0 && i>0) i--;
    4152
    4253  (int&) deg = i;          // bientot mutable dans ANSI C++
     
    6172{
    6273  UpdateDegIfDirty();
    63   double res = data[deg];
     74  double res = Element(deg);
    6475  for (int i=deg-1; i>=0; i--) {
    6576    res *= x;
    66     res += data[i];
     77    res += Element(i);
    6778  }
    6879  return res;
     
    7687{
    7788  UpdateDegIfDirty();
    78   if (deg == 0) { data[0] = 0; return;}
     89  if (deg == 0) { Element(0) = 0; return;}
    7990  for (int i=1; i<=deg; i++)
    80     data[i-1] = data[i]*i;
    81   data[deg] = 0;
     91    Element(i-1) = Element(i)*i;
     92  Element(deg) = 0;
    8293  deg--;
    8394}
     
    94105//  der.Zero();    // on sait que Realloc met a zero le reste.
    95106  for (int i=1; i<=deg; i++)
    96     der.data[i-1] = data[i]*i;
    97 }
    98 
    99 
    100 //++
    101 int Poly::Roots(OVector& roots) const
     107    der.Element(i-1) = Element(i)*i;
     108}
     109
     110
     111//++
     112int Poly::Roots(Vector& roots) const
    102113//
    103114//      Retourne dans roots les racines réelles, si on sait
     
    133144  if (deg != 1) THROW(sizeMismatchErr);
    134145
    135   if (data[1] == 0) return 0;
    136   r = -data[0]/data[1];
     146  if (Element(1) == 0) return 0;
     147  r = -Element(0)/Element(1);
    137148  return 1;
    138149}
     
    149160  if (deg != 2) THROW(sizeMismatchErr);
    150161
    151   double delta = data[1]*data[1] - 4*data[0]*data[2];
     162  double delta = Element(1)*Element(1) - 4*Element(0)*Element(2);
    152163  if (delta < 0) return 0;
    153164  if (delta == 0) {
    154     r1 = r2 = -data[1]/2/data[0];
     165    r1 = r2 = -Element(1)/2/Element(0);
    155166    return 1;
    156167  }
    157   r1 = (-data[1] - sqrt(delta))/2/data[0];
    158   r2 = (-data[1] + sqrt(delta))/2/data[0];
     168  r1 = (-Element(1) - sqrt(delta))/2/Element(0);
     169  r2 = (-Element(1) + sqrt(delta))/2/Element(0);
    159170  return 2;
    160171}
     
    167178{
    168179  if (this == &a) return *this;
    169   OVector::operator=(a);
     180  Vector::operator=(a);
    170181
    171182  UpdateDeg();
     
    185196  b.UpdateDegIfDirty();
    186197
    187   if (b.deg > deg)
    188     Realloc(b.deg);
     198  if (b.deg > deg) Realloc(b.deg);
    189199
    190200  int n = (deg > b.deg) ? deg : b.deg;
    191   for (int i=0; i<=n; i++)
    192     data[i] += b.data[i];
     201  for (int i=0; i<=n; i++) Element(i) += b.Element(i);
    193202
    194203  UpdateDeg();
     
    204213  b.UpdateDegIfDirty();
    205214
    206   if (b.deg > deg)
    207     Realloc(b.deg);
     215  if (b.deg > deg) Realloc(b.deg);
    208216
    209217  int n = (deg > b.deg) ? deg : b.deg;
    210   for (int i=0; i<=n; i++)
    211     data[i] -= b.data[i];
     218  for (int i=0; i<=n; i++) Element(i) -= b.Element(i);
    212219
    213220  UpdateDeg();
     
    215222}
    216223
    217 
    218 //++
    219 Poly operator+ (Poly const& a, Poly const& b)
    220 //
    221 //--
    222 #if HAS_NAMED_RETURN
    223      return c((a.deg > b.deg)?(a.deg+1):(b.deg+1))
    224 #endif
    225 {
    226 #if !HAS_NAMED_RETURN
    227 Poly c((a.deg > b.deg)?(a.deg+1):(b.deg+1));
    228 #endif
    229   c = a;
    230   c += b;
    231 #if !HAS_NAMED_RETURN
    232 return c;
    233 #endif
    234 }
    235 
    236 //++
    237 Poly operator- (Poly const& a, Poly const& b)
    238 //
    239 //--
    240 #if HAS_NAMED_RETURN
    241      return c((a.deg > b.deg)?(a.deg+1):(b.deg+1))
    242 #endif
    243 {
    244 #if !HAS_NAMED_RETURN
    245 Poly c((a.deg > b.deg)?(a.deg+1):(b.deg+1));
    246 #endif
    247   c = a;
    248   c -= b;
    249 #if !HAS_NAMED_RETURN
    250 return c;
    251 #endif
    252 }
    253 
    254 //++
    255 Poly operator* (Poly const& a, Poly const& b)
    256 //
    257 //--
    258 //#if HAS_NAMED_RETURN
    259 //     return c(a.deg + b.deg)
    260 //#endif
    261 {
    262 //#if !HAS_NAMED_RETURN
    263 Poly c(a.deg + b.deg);
    264 //#endif
    265   a.UpdateDegIfDirty();
     224//++
     225Poly& Poly::operator *= (double a)
     226//
     227//--
     228{
     229  UpdateDegIfDirty();
     230  for (int i=0; i<=deg; i++) Element(i) *= a;
     231  return *this;
     232}
     233
     234//++
     235Poly Poly::Mult(Poly const& b) const
     236//
     237//--
     238{
     239Poly c(deg + b.deg);
     240  UpdateDegIfDirty();
    266241  b.UpdateDegIfDirty();
    267242
    268   c.deg = a.deg + b.deg;
     243  c.deg = deg + b.deg;
    269244
    270245  for (int i=0; i<=c.deg; i++) {
    271246    c[i] = 0;
    272     int kmin = (i <= a.deg) ? 0 : i - a.deg;
    273     int kmax = (i <= a.deg) ? i : a.deg;
     247    int kmin = (i <= deg) ? 0 : i - deg;
     248    int kmax = (i <= deg) ? i : deg;
    274249    for (int k=kmin; k<=kmax; k++)
    275       c[i] += a[k] * b[i-k];
    276   }
    277 //#if !HAS_NAMED_RETURN
     250      c[i] += (*this)[k] * b[i-k];
     251  }
    278252return c;
    279 //#endif
    280 }
    281 
    282 //++
    283 Poly& Poly::operator *= (double a)
    284 //
    285 //--
    286 {
    287   UpdateDegIfDirty();
    288 
    289   for (int i=0; i<=deg; i++)
    290     data[i] *= a;
    291 
    292   return *this;
    293 }
    294 
    295 
    296 //++
    297 Poly operator* (double a, Poly const& b)
    298 //
    299 //--
    300 #if HAS_NAMED_RETURN
    301      return c(b)
    302 #endif
    303 {
    304 #if !HAS_NAMED_RETURN
    305   Poly c(b);
    306 #endif
    307 
    308    c *= a;
    309 
    310 #if !HAS_NAMED_RETURN
    311   return c;
    312 #endif
    313 }
    314 
    315 
    316 void Poly::ReadSelf(PInPersist& s)
    317 {
    318   int_4 dg;
    319   s >> dg;
    320   Realloc(dg, true);
    321 
    322 #ifdef DEBUG
    323   int r = nr; int c = nc;
    324 #endif
    325 
    326   OVector::ReadSelf(s);
    327 
    328 #ifdef DEBUG
    329   DBASSERT(r == nr && c == nc);
    330 #endif
    331   UpdateDeg();
    332 
    333   #ifdef DEBUG
    334   DBASSERT(dg == deg);
    335   #endif
    336 }
    337 
    338 void Poly::WriteSelf(POutPersist& s) const
    339 {
    340   UpdateDegIfDirty();
    341   ((Poly*)(this))->Realloc(deg, true);
    342   s << deg;
    343   OVector::WriteSelf(s);
    344253}
    345254
     
    365274
    366275//++
    367 ostream& operator << (ostream& s, const Poly& a)
    368 //
    369 //      Impressions sur le stream "s".
    370 //--
    371 {
    372   a.Print(s);
    373   return s;
    374 }
    375 
    376 //++
    377 double Poly::Fit(OVector const& x, OVector const& y, int degre)
     276double Poly::Fit(Vector const& x, Vector const& y, int degre)
    378277//
    379278//      Ajustement polynomial par moindre carrés. Un polynôme de
     
    387286  Realloc(degre);
    388287
    389   OMatrix a(degre+1, n);
     288  Matrix a(degre+1, n);
    390289
    391290  for (int c=0; c<n; c++) {
     
    397296  }
    398297
    399   double rc = LinFit(a,y,(OVector&)*this);
     298  double rc = LinFit(a,y,(Vector&)*this);
    400299  UpdateDeg();
    401300  return rc;
     
    403302
    404303//++
    405 double Poly::Fit(OVector const& x, OVector const& y, OVector const& erry2, int degre,
    406                  OVector& errCoef)
     304double Poly::Fit(Vector const& x, Vector const& y, Vector const& erry2, int degre,
     305                 Vector& errCoef)
    407306//
    408307//      Ajustement polynomial par moindre carrés. Un polynôme de
     
    419318  errCoef.Realloc(degre+1);
    420319
    421   OMatrix a(degre+1, n);
     320  Matrix a(degre+1, n);
    422321
    423322  for (int c=0; c<n; c++) {
     
    429328  }
    430329
    431   double rc = LinFit(a,y,erry2,(OVector&)*this,errCoef);
     330  double rc = LinFit(a,y,erry2,(Vector&)*this,errCoef);
    432331  UpdateDeg();
    433332  return rc;
    434333}
    435334
     335
     336//++
     337// Poly Poly::power(int n) const
     338//
     339//      Retourne le polynôme à la puissance n
     340//--
     341
     342Poly 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//++
     351Poly 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
     363//////////////////////////////////////////////////////////////////////////
     364void ObjFileIO<Poly>::ReadSelf(PInPersist& is)
     365{
     366if(dobj==NULL) dobj=new Poly;
     367int_4 dg;
     368is >> dg;
     369dobj->Realloc(dg,true);
     370is >> *((Vector *) dobj);
     371dobj->UpdateDeg();
     372}
     373
     374void ObjFileIO<Poly>::WriteSelf(POutPersist& os) const
     375{
     376if(dobj == NULL) return;
     377dobj->UpdateDegIfDirty();
     378dobj->Realloc(dobj->deg,true);
     379os << dobj->deg;
     380os << *((Vector *) dobj);
     381}
     382
     383//////////////////////////////////////////////////////////////////////////
    436384int binomial(int n, int p)
    437385{
     
    442390}
    443391
    444 
    445 //++
    446 // Poly Poly::power(int n) const
    447 //
    448 //      Retourne le polynôme à la puissance n
    449 //--
    450 
    451 Poly Poly::power(int n) const // a accelerer !!!
    452 {
    453   if (n < 0) THROW(rangeCheckErr);
    454   if (n == 0) { Poly r(0); r[0] = 1; return r;}
    455   if (n == 1) { return *this; }
    456   return *this * power(n-1);
    457 }
    458 
    459 //++
    460 Poly Poly::operator() (Poly const& b) const
    461 //
    462 //      Substitution d'un polynôme dans un autre.
    463 //--
    464 {
    465   Poly c(b.Degre()*Degre());
    466   for (int i=0; i<= Degre(); i++)
    467     c += (*this)[i] * b.power(i);
    468   return c;
    469 }
    470 
    471 
     392//////////////////////////////////////////////////////////////////////////
    472393// ******************* POLY 2 VARIABLES ******************
    473 
    474394//++
    475395// Class        Poly2
     
    482402//++
    483403// Links        Parents
    484 // OVector
     404// Vector
    485405//--
    486406
     
    494414//      Crée un polynôme de degrés partiels degreX et degreY.
    495415//--
    496 :OVector((degreX+1)*(degreY+1)), dirty(0),
     416:Vector((degreX+1)*(degreY+1)), dirty(0),
    497417 maxDegX(degreX), maxDegY(degreY), degX(0), degY(0), deg(0)
    498418{
     
    506426//      de deux polynômes à une variable, p2(x,y)=px(x)py(y)
    507427//--
    508 :OVector((polX.Degre()+1)*(polY.Degre()+1)), dirty(0),
     428:Vector((polX.Degre()+1)*(polY.Degre()+1)), dirty(0),
    509429 maxDegX(polX.Degre()), maxDegY(polY.Degre()),
    510430 degX(polX.Degre()), degY(polY.Degre()), deg(degX+degY)
     
    521441//      Constructeur par copie.
    522442//--
    523 :OVector(a), dirty(a.dirty),
     443:Vector(a), dirty(a.dirty),
    524444 maxDegX(a.maxDegX), maxDegY(a.maxDegY),
    525445 degX(a.degX), degY(a.degY), deg(a.deg)
     
    561481  UpdateDegIfDirty();
    562482  Poly2 tmp(*this);
    563   OVector::Realloc((degreX+1)*(degreY+1));
    564   Zero();
     483  Vector::Realloc((degreX+1)*(degreY+1));
     484  Reset();
    565485  maxDegX = degreX;
    566486  maxDegY = degreY;
     
    631551
    632552//++
    633 double Poly2::Fit(OVector const& x, OVector const& y, OVector const& z,
     553double Poly2::Fit(Vector const& x, Vector const& y, Vector const& z,
    634554                  int degreX, int degreY)
    635555//
     
    643563  Realloc(degreX, degreY);
    644564
    645   OMatrix a((degreX+1)*(degreY+1), n);
     565  Matrix a((degreX+1)*(degreY+1), n);
    646566
    647567  for (int c=0; c<n; c++) {
     
    657577  }
    658578
    659   double rc = LinFit(a,z,(OVector&)*this);
     579  double rc = LinFit(a,z,(Vector&)*this);
    660580  UpdateDeg();
    661581  return rc;
     
    664584
    665585//++
    666 double Poly2::Fit(OVector const& x, OVector const& y, OVector const& z,
    667                   OVector const& errz2, int degreX, int degreY,
    668                   OVector& errCoef)
     586double Poly2::Fit(Vector const& x, Vector const& y, Vector const& z,
     587                  Vector const& errz2, int degreX, int degreY,
     588                  Vector& errCoef)
    669589//
    670590//      Ajustement par moindre carrés z = P(x,y), degrés partiels imposés,
     
    680600  errCoef.Realloc((degreX+1)*(degreY+1));
    681601
    682   OMatrix a((degreX+1)*(degreY+1), n);
     602  Matrix a((degreX+1)*(degreY+1), n);
    683603
    684604  for (int c=0; c<n; c++) {
     
    694614  }
    695615
    696   double rc = LinFit(a,z,errz2,(OVector&)*this,errCoef);
     616  double rc = LinFit(a,z,errz2,(Vector&)*this,errCoef);
    697617  UpdateDeg();
    698618  return rc;
     
    700620
    701621//++
    702 double Poly2::Fit(OVector const& x, OVector const& y, OVector const& z,
     622double Poly2::Fit(Vector const& x, Vector const& y, Vector const& z,
    703623                  int degre)
    704624//
     
    712632  Realloc(degre, degre);   // certains vaudront 0, impose.
    713633
    714   OMatrix a((degre+1)*(degre+2)/2, n);
    715   OVector cf((degre+1)*(degre+2)/2);
     634  Matrix a((degre+1)*(degre+2)/2, n);
     635  Vector cf((degre+1)*(degre+2)/2);
    716636#define C_INDEX(i,j) ((i) + (j)*(2*degre+3-(j))/2)
    717637
     
    744664
    745665//++
    746 double Poly2::Fit(OVector const& x, OVector const& y, OVector const& z,
    747                   OVector const& errz2, int degre,
    748                   OVector& errCoef)
     666double Poly2::Fit(Vector const& x, Vector const& y, Vector const& z,
     667                  Vector const& errz2, int degre,
     668                  Vector& errCoef)
    749669//
    750670//      Ajustement par moindre carrés z = P(x,y), degré total imposé,
     
    761681#define C_INDEX(i,j) ((i) + (j)*(2*degre+3-(j))/2)
    762682
    763   OMatrix a((degre+1)*(degre+2)/2, n);
    764   OVector cf((degre+1)*(degre+2)/2);
    765   OVector ecf((degre+1)*(degre+2)/2);
     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);
    766686
    767687  for (int c=0; c<n; c++) {
     
    792712  UpdateDeg();
    793713  return rc;
    794 }
    795 
    796 
    797 void Poly2::ReadSelf(PInPersist& s)
    798 {
    799   int_4 dgx, dgy;
    800   s >> dgx >> dgy;
    801 
    802   Realloc(dgx, dgy);
    803 
    804 #ifdef DEBUG
    805   int r = nr; int c = nc;
    806 #endif
    807 
    808   OVector::ReadSelf(s);
    809 
    810 #ifdef DEBUG
    811   DBASSERT(r == nr && c == nc);
    812 #endif
    813   UpdateDeg();
    814 }
    815 
    816 void Poly2::WriteSelf(POutPersist& s) const
    817 {
    818   s << maxDegX << maxDegY;
    819   OVector::WriteSelf(s);
    820714}
    821715
     
    850744
    851745//++
    852 ostream& operator << (ostream& s, const Poly2& a)
    853 //
    854 //      Impression sur stream s.
    855 //--
    856 {
    857   a.Print(s);
    858   return s;
    859 }
    860 
    861 
    862 //++
    863746// Titre        Opérations
    864747//--
     
    905788
    906789//++
    907 Poly2 operator+ (Poly2 const& a, Poly2 const& b)
    908 //
    909 //--
    910 #if HAS_NAMED_RETURN
    911      return c(a)
    912 #endif
    913 {
    914 #if !HAS_NAMED_RETURN
    915 Poly2 c(a);
    916 #endif
    917   c += b;
    918 #if !HAS_NAMED_RETURN
    919 return c;
    920 #endif
    921 }
    922 
    923 //++
    924 Poly2 operator- (Poly2 const& a, Poly2 const& b)
    925 //
    926 //--
    927 #if HAS_NAMED_RETURN
    928      return c(a)
    929 #endif
    930 {
    931 #if !HAS_NAMED_RETURN
    932 Poly2 c(a);
    933 #endif
    934   c -= b;
    935 #if !HAS_NAMED_RETURN
    936 return c;
    937 #endif
    938 }
    939 
    940 //++
    941790Poly2& Poly2::operator *= (double a)
    942791//
     
    944793{
    945794  for (int i=0; i<NElts(); i++)
    946     data[i] *= a;
     795    Element(i) *= a;
    947796
    948797  return *this;
     
    950799
    951800//++
    952 Poly2 operator * (double a, Poly2 const& b)
    953 //
    954 //--
    955 #if HAS_NAMED_RETURN
    956      return c(b)
    957 #endif
    958 {
    959 #if !HAS_NAMED_RETURN
    960 Poly2 c(b);
    961 #endif
    962   c *= a;
    963 #if !HAS_NAMED_RETURN
    964 return c;
    965 #endif
    966 }
    967 
    968 //++
    969 Poly2 operator* (Poly2 const& a, Poly2 const& b)
    970 //
    971 //--
    972 {
    973   Poly2 c(a.DegX() + b.DegX(), a.DegY() + b.DegY());
    974   a.UpdateDegIfDirty();
     801Poly2 Poly2::Mult(Poly2 const& b) const
     802//
     803//--
     804{
     805  Poly2 c(DegX() + b.DegX(), DegY() + b.DegY());
     806  UpdateDegIfDirty();
    975807  b.UpdateDegIfDirty();
    976808
    977   for (int i=0; i<=a.DegX(); i++)
    978     for (int j=0; j<=a.DegY(); j++)
     809  for (int i=0; i<=DegX(); i++)
     810    for (int j=0; j<=DegY(); j++)
    979811      for (int k=0; k<=b.DegX(); k++)
    980812        for (int l=0; l<=b.DegY(); l++)
    981           c.Coef(i+k,j+l) += a.Coef(i,j)*b.Coef(k,l);
     813          c.Coef(i+k,j+l) += Coef(i,j)*b.Coef(k,l);
    982814  return c;
    983815}
     
    1029861}
    1030862
    1031 
     863//////////////////////////////////////////////////////////////////////////
     864void ObjFileIO<Poly2>::ReadSelf(PInPersist& is)
     865{
     866if(dobj==NULL) dobj=new Poly2;
     867int_4 dgx, dgy;
     868is >> dgx >> dgy;
     869dobj->Realloc(dgx,dgy);
     870is >> *((Vector *) dobj);
     871dobj->UpdateDeg();
     872}
     873
     874void ObjFileIO<Poly2>::WriteSelf(POutPersist& os) const
     875{
     876if(dobj == NULL) return;
     877os << dobj->maxDegX << dobj->maxDegY;
     878os << *((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)
     889template class ObjFileIO<Poly>;
     890template class ObjFileIO<Poly2>;
     891#endif
Note: See TracChangeset for help on using the changeset viewer.