Changeset bc1dd97 in tol


Ignore:
Timestamp:
Apr 24, 2012, 5:33:49 PM (15 years ago)
Author:
Víctor de Buen <vdebuen@…>
Branches:
1941-docker-image-to-test, devel, master
Children:
1466af1
Parents:
736be83
Message:

Refs #1511

git-svn-id: ​https://www.tol-project.org/svn/tolp/trunk@4587 be4846ae-f051-483a-831f-b64e7d08527f

Location:
tol
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • tol/bmath/bstat/ar.cpp

    r736be83 rbc1dd97  
    298298    IdentifyAutoRegIntegrated(z,err,s,sw,w,dif,ar,period);
    299299}
     300
     301//--------------------------------------------------------------------
     302BArray<BDat> NextSchur(const BArray<BDat>& pol)
     303//--------------------------------------------------------------------
     304{
     305  int k, s = pol.Size();
     306  BArray<BDat> b(s-1);
     307  b.AllocBuffer(s-1);
     308  double* B  = (double*)b.GetBuffer();
     309  const double* A  = (const double*)pol.Buffer();
     310  const double* A_ = (const double*)pol.Buffer()+s-1;
     311  double a = *A;
     312  double a_ = *A_;
     313  double c = a / a_;
     314/* * /
     315  Std(BText("\nNextSchur a0=")+a+" an="+a_+" c="+c);
     316  if(BDat(c).IsUnknown())
     317   printf("");
     318/* */
     319  A++;
     320  A_--;
     321  for(k=0; k<s-1; k++, A++, A_--, B++)
     322  {
     323    *B = (*A - c * *A_);
     324  }
     325  return(b);
     326}
     327
     328//--------------------------------------------------------------------
     329BDat SchurValue(const BArray<BDat>& a, BDat toleranceBase, BDat& tolerance, bool &isSchur)
     330//--------------------------------------------------------------------
     331{
     332  int sld = sizeof(long double);
     333  int s = a.Size();
     334  BDat a0 = Abs(a[0]);
     335  BDat an = Abs(a[s-1]);
     336  BDat sv = (2*an)/(an+a0);
     337  isSchur = an-a0 > tolerance;
     338/* * /
     339//Std(BText("\nSchurValue a=")); for(int k=0; k<s; k++) { Std(BText("|")+a[k]); }
     340  Std(BText("\nSchurValue tolerance=")+tolerance+" a0=")+a0+" an="+an+" sv="+sv+" isSchur="+isSchur);
     341/* */
     342  if(!isSchur || (s==2))
     343  {
     344    return(sv);
     345  }
     346  else
     347  {
     348    tolerance += toleranceBase*(1+2*(s-1));
     349    return(SchurValue(NextSchur(a),toleranceBase,tolerance,isSchur));
     350  }
     351}
     352
     353
     354//--------------------------------------------------------------------
     355BDat StationaryValue(
     356  const BPolyn<BDat>& pol,
     357  BDat toleranceBase,
     358  BDat& tolerance,
     359  bool& isStationary)
     360//--------------------------------------------------------------------
     361{
     362 
     363  if(toleranceBase.IsUnknown() || toleranceBase<0) { toleranceBase = DEpsilon(); }
     364  tolerance = toleranceBase;
     365  int i,j, deg;
     366  BDat coef;
     367  int s = pol.Size();
     368  int n = pol.Degree();
     369  if(s==1)
     370  {
     371    if(n==0) { isStationary=true;  return(2); }
     372    else     { isStationary=false; return(0); }
     373  }
     374  int gcd = pol.Period();
     375  int m = n/gcd;
     376  BArray<BDat> a(BDat(0),m+1);
     377  for(i=0; i<s; i++)
     378  {
     379    j = s-1-i;
     380    deg  = pol(i).Degree();
     381    coef = pol(i).Coef();
     382    a[m-deg/gcd] = coef;
     383  }
     384  BDat sv = SchurValue(a,toleranceBase,tolerance,isStationary);
     385  if(sv.IsUnknown())
     386  {
     387    Warning("StationaryValue returns unknown value.");
     388  }
     389  return(sv); 
     390}
     391
     392//--------------------------------------------------------------------
     393bool IsStationary(const BPolyn<BDat>& pol, BDat& tolerance)
     394//--------------------------------------------------------------------
     395{
     396  bool isStationary;
     397  BDat toleranceBase = tolerance;
     398  BDat sv = StationaryValue(pol,toleranceBase,tolerance,isStationary);
     399  return(isStationary);
     400}
     401
     402//--------------------------------------------------------------------
     403BDat StationaryValue(const BPolyn<BDat>& pol)
     404//--------------------------------------------------------------------
     405{
     406  BDat tolerance, toleranceBase = BDat::Unknown();
     407  bool isStationary;
     408  return(StationaryValue(pol,toleranceBase,tolerance,isStationary));
     409}
     410
     411//--------------------------------------------------------------------
     412bool IsStationary(const BPolyn<BDat>& pol)
     413//--------------------------------------------------------------------
     414{
     415  BDat tolerance = BDat::Unknown();
     416  return(IsStationary(pol,tolerance));
     417}
  • tol/bmath/bstat/arma.cpp

    r736be83 rbc1dd97  
    3535#include <tol/tol_bvmat.h>
    3636#include <tol/tol_bratgra.h>
     37#include <tol/tol_bar.h>
    3738
    3839//--------------------------------------------------------------------
    … …  
    330331  MatBackwardDifEq (R, ZEROp, ZB<<Z, ZEROq, A0A);
    331332
    332   if(ar.IsStationary() && ma.IsStationary())
     333  if(IsStationary(ar) && IsStationary(ma))
    333334  {
    334335    BMatrix<BDat> oldA0A;
    … …  
    700701  int i, j;
    701702  BArray<BDat> gn;
    702 //BBool isStationary = ar.IsStationary() && ma.IsStationary();
     703//BBool isStationary = IsStationary(ar) && IsStationary(ma);
    703704  ARMAAutoCovarianzeVector(gn, ar, ma, n0, sigma);
    704705  BSymMatrix<BDat> G(n0);
    … …  
    12831284    BPolyn<BDat> ar_i = factor_(i).ar_; ar_i.Aggregate();
    12841285    BPolyn<BDat> ma_i = factor_(i).ma_; ma_i.Aggregate();
    1285     arOk = arOk && ar_i.IsStationary();
    1286     maOk = maOk && ma_i.IsStationary();
     1286    arOk = arOk && IsStationary(ar_i);
     1287    maOk = maOk && IsStationary(ma_i);
    12871288  }
    12881289  bool ok = arOk && maOk;
    … …  
    12971298      ar_i.Aggregate();
    12981299      ma_i.Aggregate();
    1299       bool arOk = ar_i.IsStationary()!=0;
    1300       bool maOk = ma_i.IsStationary()!=0;
     1300      bool arOk = IsStationary(ar_i)!=0;
     1301      bool maOk = IsStationary(ma_i)!=0;
    13011302      Std(BText("\nStationary?[")<<(arOk?"YES":"NOT")<<"] AR("<<factor_(i).s_<<")="<<factor_(i).ar_.Name());
    13021303      Std(BText("\nStationary?[")<<(maOk?"YES":"NOT")<<"] MA("<<factor_(i).s_<<")="<<factor_(i).ma_.Name());
  • tol/bmath/bstat/tol_bar.h

    r736be83 rbc1dd97  
    6767                               BInt          period=1);
    6868
     69BDat StationaryValue(
     70  const BPolyn<BDat>& pol,
     71  BDat toleranceBase,
     72  BDat& tolerance,
     73  bool& isStationary);
     74bool IsStationary(const BPolyn<BDat>& pol, BDat& tolerance);
     75BDat StationaryValue(const BPolyn<BDat>& pol);
     76bool IsStationary(const BPolyn<BDat>& pol);
     77
     78
    6979
    7080#endif  // TOL_BAR_H
  • tol/bmath/bstat/unitroot.cpp

    r736be83 rbc1dd97  
    104104    else
    105105    {
    106         ok = p.IsStationary();
     106        ok = IsStationary(p);
    107107    }
    108108    if(changed && name.HasName())
    … …  
    180180        rAr[0].PutCoef(1);
    181181        for(i=0; i<q-d; i++) { rAr[i+1].PutCoef(-(rNU(i,0)+NU(i,0))); }
    182         if(rAr.IsStationary()) { prob+=1; }
     182        if(IsStationary(rAr)) { prob+=1; }
    183183    }
    184184    detChoCOV = 1;
  • tol/bmath/mathobjects/tol_bpolyn.h

    r736be83 rbc1dd97  
    9898  void ChangeBF(const BPolyn<Any>& pol);
    9999  void InverseRoots(const BPolyn<Any>& pol);
    100   BPolyn<Any> NextSchur() const;
    101   Any    Schur(BBool complete) const;
    102   Any    StationaryValue(BBool complete) const;
    103   BBool IsStationary() const;
    104100
    105101  BBool IsKnown() const {
    … …  
    348344}
    349345
    350 //--------------------------------------------------------------------
    351 template <class Any>
    352 BPolyn<Any> BPolyn<Any>::NextSchur() const
    353 //--------------------------------------------------------------------
    354 {
    355 //Std(BText("\nTRACE  NextSchur 1 this=")+Name());
    356   BInt n = Degree();
    357   BArray<Any> a(n+1);
    358   int k;
    359   for(k=0; k<=n; k++) { a[k] = Coef(k); }
    360   Any a0  = a[0];
    361   Any an  = a[n];
    362   Any c;
    363 //Std(BText("\na[0]: ")+a0.Name()+"; a[n]:"+an.Name());
    364   BPolyn<Any> next;
    365   next.ReallocBuffer(n);
    366   for(k=0; k<n; k++)
    367   {
    368     c = an*a[k+1] - a0*a[n-k-1];
    369     next(k).PutCoef(c);
    370     next(k).PutDegree(k);
    371   //Std(BText("\na[k+1]: ")+a[k+1].Name()+"; a[n-k-1]:"+a[n-k-1].Name()+"; next(i-1):"+c.Name());
    372   }
    373 //Std(BText("\nTRACE NextSchur 2 next=")+next.Name());
    374   next.Aggregate();
    375 //Std(BText("\nNextSchur 3 next=")+next.Name());
    376 //if(next.Degree()>0) { next /= next.Coef(next.Degree()); }
    377 //Std(BText("\nTRACE NextSchur 4 next=")+next.Name());
    378   return(next);
    379 }
    380 
    381 
    382 //--------------------------------------------------------------------
    383 template <class Any>
    384 Any BPolyn<Any>::Schur(BBool complete) const
    385 
    386 /*! Returns a value great than 1 if there are no roots out of
    387  *  the unit circle
    388  */
    389 //--------------------------------------------------------------------
    390 {
    391 //Std(BText("\nTRACE  Schur : ")+Name());
    392   int s = this->Size();
    393   int n = this->Degree();
    394   if(s==1) { return(2); }
    395   Any a0 = Abs(this->Coef(0));
    396   Any an = Abs(this->Coef(n));
    397   Any v = 1.0+(an-a0)/(an+a0);
    398   if((!complete && (v<=1)) || (s==2))
    399   {
    400     return(v);
    401   }
    402   else
    403   {
    404     return(NextSchur().Schur(complete));
    405   }
    406 }
    407346
    408347//--------------------------------------------------------------------
    … …  
    423362}
    424363
    425 //--------------------------------------------------------------------
    426 template <class Any>
    427 Any BPolyn<Any>::StationaryValue(BBool complete) const
    428 
    429 /*! Returns a value great than 1 if there are no roots inside
    430  *  the unit circle
    431  */
    432 //--------------------------------------------------------------------
    433 {
    434 //Std(BText("\nTRACE  StationaryValue : ")+Name());
    435   int s = this->Size();
    436   int n = this->Degree();
    437   if(s==1)
    438   {
    439     if(n==0) { return(2); }
    440     else     { return(0); }
    441   }
    442   int i, j;
    443   int deg;
    444   Any coef;
    445   BPolyn sym;
    446   int gcd = Period();
    447 //Std(BText("\nTRACE  StationaryValue  gcd=")+gcd);
    448   int m = n/gcd;
    449   sym.ReallocBuffer(s);
    450   for(i=0; i<s; i++)
    451   {
    452     j = s-1-i;
    453     deg  = (*this)(i).Degree();
    454     coef = (*this)(i).Coef();
    455     sym(j).PutDegree(m-deg/gcd);
    456     sym(j).PutCoef(coef);
    457   }
    458   return(sym.Schur(complete));
    459 }
    460 
    461 
    462 //--------------------------------------------------------------------
    463 template <class Any>
    464 BBool BPolyn<Any>::IsStationary() const
    465 
    466 /*! Returns true if all roots in this polyn are outside of the unit
    467  *  circle
    468  */
    469 //--------------------------------------------------------------------
    470 {
    471   if(this->Size  ()==0) { return(BFALSE); }
    472   if(this->Degree()==0) { return(BTRUE); }
    473   else { return(this->StationaryValue(BFALSE)>1); }
    474 }
    475 
    476364
    477365//--------------------------------------------------------------------
  • tol/btol/bmodel/modcalc.cpp

    r736be83 rbc1dd97  
    3333#include <tol/tol_bsparse.h>
    3434#include <tol/tol_bsvdsep.h>
     35#include <tol/tol_bar.h>
    3536
    3637#include <gsl/gsl_math.h>
    … …  
    10031004  for(i=0; i<arma_.factor_.Size(); i++)
    10041005  {
    1005     bool arOk = arma_.factor_(i).ar_.IsStationary();
    1006     bool maOk = arma_.factor_(i).ma_.IsStationary();
     1006    bool arOk = IsStationary(arma_.factor_(i).ar_);
     1007    bool maOk = IsStationary(arma_.factor_(i).ma_);
    10071008    Std(BText("\nStationary?[")<<(arOk?"YES":"NOT")<<"] AR("<<arma_.factor_(i).s_<<")="<<arma_.factor_(i).ar_.Name());
    10081009    Std(BText("\nStationary?[")<<(maOk?"YES":"NOT")<<"] MA("<<arma_.factor_(i).s_<<")="<<arma_.factor_(i).ma_.Name());
    … …  
    11041105  for(i=0; i<arma_.factor_.Size(); i++)
    11051106  {
    1106     bool arOk = arma_.factor_(i).ar_.IsStationary();
    1107     bool maOk = arma_.factor_(i).ma_.IsStationary();
     1107    bool arOk = IsStationary(arma_.factor_(i).ar_);
     1108    bool maOk = IsStationary(arma_.factor_(i).ma_);
    11081109    Std(BText("\nStationary?[")<<(arOk?"YES":"NOT")<<"] AR("<<arma_.factor_(i).s_<<")="<<arma_.factor_(i).ar_.Name());
    11091110    Std(BText("\nStationary?[")<<(maOk?"YES":"NOT")<<"] MA("<<arma_.factor_(i).s_<<")="<<arma_.factor_(i).ma_.Name());
    … …  
    12971298//--------------------------------------------------------------------
    12981299{
    1299   BDat y = p_.StationaryValue(BTRUE);
     1300  BDat y = StationaryValue(p_);
    13001301//Std(BText("\nS(")+p_.Name()+")="+(y-1));
    13011302  return(y-1);
  • tol/btol/bmodel/moddiag.cpp

    r736be83 rbc1dd97  
    3333#include <tol/tol_bmultvar.h>
    3434#include <tol/tol_bsetgra.h>
     35#include <tol/tol_bar.h>
    3536
    3637//--------------------------------------------------------------------
    … …  
    468469//        Std(BText("\nFactor[")+j+"]"+factors[j].Name() +
    469470//                " Stationary = " + factors[j].Stationary());
    470         if(!(factors[j].IsStationary())) { unarys[j]+=1; }
     471        if(!(IsStationary(factors[j]))) { unarys[j]+=1; }
    471472      }
    472473    }
  • tol/btol/real_type/datgrav.cpp

    r736be83 rbc1dd97  
    4040#include <tol/tol_bdtegra.h>
    4141#include <tol/tol_bnameblock.h>
     42#include <tol/tol_bar.h>
    4243#ifdef __USE_TC__
    4344#  include <tol/tol_bctmigra.h>
    … …  
    13111312//--------------------------------------------------------------------
    13121313  DeclareContensClass(BDat, BDatTemporary, BDatIsStationary);
    1313   DefExtOpr(1, BDatIsStationary, "IsStationary", 1, 1, "Polyn",
    1314   "(Polyn pol)",
     1314  DefExtOpr(1, BDatIsStationary, "IsStationary", 1, 2, "Polyn Real",
     1315  "(Polyn pol [, Real tolerance=MachineEpsilon])",
    13151316  I2("Returns true if all the roots of the polynomial are would be of the "
    13161317     "unit circle.",
    … …  
    13211322//--------------------------------------------------------------------
    13221323{
    1323   BPol p = Pol(Arg(1));
    1324   p.Aggregate();
    1325   contens_ = p.IsStationary();
     1324  BPol& p = Pol(Arg(1));
     1325  BDat t;
     1326  if(Arg(2)) { t = Dat(Arg(2)); }
     1327  contens_ = IsStationary(p,t);
    13261328}
    13271329
    … …  
    13391341//--------------------------------------------------------------------
    13401342{
    1341   contens_ = Pol(Arg(1)).Period();
     1343  BPol& p = Pol(Arg(1));
     1344  contens_ = p.Period();
    13421345}
    13431346
    13441347//--------------------------------------------------------------------
    13451348  DeclareContensClass(BDat, BDatTemporary, BDatStationaryValue);
    1346   DefExtOpr(1, BDatStationaryValue, "StationaryValue", 1, 1, "Polyn",
    1347   "(Polyn pol)",
     1349  DefExtOpr(1, BDatStationaryValue, "StationaryValue", 1, 2, "Polyn Real",
     1350  "(Polyn pol [, Real tolerance=MachineEpsilon])",
    13481351  I2("Returns a measure of the distence to the unit circle of the "
    13491352     "polynomial roots .",
    … …  
    13541357//--------------------------------------------------------------------
    13551358{
    1356   contens_ = Pol(Arg(1)).StationaryValue(BFALSE);
     1359  BPol& p = Pol(Arg(1));
     1360  BDat toleranceBase;
     1361  BDat tolerance;
     1362  if(Arg(2)) { toleranceBase = Dat(Arg(2)); }
     1363  bool isStationary;
     1364  contens_ = StationaryValue(p,toleranceBase,tolerance,isStationary);
    13571365}
    13581366
    … …  
    14131421    }
    14141422    pk.Aggregate();
    1415     BInt ok = pk.IsStationary();
     1423    BInt ok = IsStationary(pk);
    14161424//  Std(BText("\n")+(ok?" OK   ":" FAIL ")+pk.Name());
    14171425    sumOk += ok;
Note: See TracChangeset for help on using the changeset viewer.