Changeset 7db6dd2 in tol


Ignore:
Timestamp:
Oct 22, 2008, 2:29:52 PM (18 years ago)
Author:
Víctor de Buen <vdebuen@…>
Branches:
1941-docker-image-to-test, devel, master
Children:
53020ad
Parents:
baa9d61
Message:

New function GetBoundsInPolytope: Calculates minimum and maximum values of j-th component of vector z inside the polytope D*z <= d

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

Location:
tol/btol/vmatrix_type
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • tol/btol/vmatrix_type/tol_bvmat.h

    rbaa9d61 r7db6dd2  
    498498            ECode c=ESC_blasRdense, int nzmax=-1);
    499499  void Gaussian(int nrow, int ncol, double nu, double sigma);
     500  static int GetBoundsInPolytope(const BVMat&  D,
     501                                 const BVMat&  d,
     502                                       BVMat&  z,
     503                                       int     j,
     504                                       double& lower,
     505                                       double& upper);
    500506  void TruncStdGaussian(const BVMat& D, const BVMat& d_,
    501507                        const BVMat& z0_, int ncol, int burnin);
  • tol/btol/vmatrix_type/vmat_stats.cpp

    rbaa9d61 r7db6dd2  
    431431#define USE_BTruncatedNormalDist
    432432
     433
     434////////////////////////////////////////////////////////////////////////////////
     435  int BVMat::GetBoundsInPolytope(const BVMat&  D,
     436                                 const BVMat&  d_,
     437                                       BVMat&  z_,
     438                                       int     j,
     439                                       double& lower,
     440                                       double& upper)
     441// Calculates minimum and maximum values of z[j] matching
     442//   D*z <= d 
     443////////////////////////////////////////////////////////////////////////////////
     444{
     445  static char* fName = "GetBoundsInPolytope";
     446  if(!D .CheckDefined(fName)) { return(-1); }
     447  if(!d_.CheckDefined(fName)) { return(-1); }
     448  if(!z_.CheckDefined(fName)) { return(-1); }
     449  int Dr = D.Rows();
     450  int Dc = D.Columns();
     451  int dr = d_.Rows();
     452  int dc = d_.Columns();
     453  int zr = z_.Rows();
     454  int zc = z_.Columns();
     455  if((Dr!=dr)||(dc!=1)||
     456     (Dc!=zr)||(zc!=1)||
     457     !Dc||!Dr||!dc||!dr||!zc||!zr)
     458  {
     459    err_invalid_dimensions(fName,D,d_,z_);
     460    return(-1);
     461  }
     462  BVMat d(d_,ESC_blasRdense);
     463  BVMat z(z_,ESC_blasRdense);
     464  BVMat  b = d-D*z;
     465  int i, k, h;
     466  double  aux;
     467  bool    sp = D.code_ == ESC_chlmRsparse;
     468  int     n  = z.s_.blasRdense_->nrow;
     469  int     r  = d.s_.blasRdense_->nrow;
     470  int     k0 = 0;
     471  int     k1 = r;
     472  double* zx = (double*)z.s_.blasRdense_->x;
     473  double* dx = (double*)d.s_.blasRdense_->x;
     474  double* bx = (double*)b.s_.blasRdense_->x;
     475  double* Dx = (double*)(sp?D.s_.chlmRsparse_->x:D.s_.blasRdense_->x);
     476  int*    Di = (int*   )(sp?D.s_.chlmRsparse_->i:NULL);
     477  int*    Dp = (int*   )(sp?D.s_.chlmRsparse_->p:NULL);
     478  double  negInf = BDat::NegInf();
     479  double  posInf = BDat::PosInf();
     480  int     nnz    = D.NonNullCells();
     481  double  drop   = nnz*DEpsilon();
     482 
     483  double& zj = zx[j];
     484//Std(BText("\nj=")<<j);
     485  lower=negInf;
     486  upper=posInf;
     487  if(sp)
     488  {
     489    k0 = Dp[j];
     490    k1 = Dp[j+1];
     491  }
     492  else
     493  {
     494    h = j*Dr;
     495  }
     496  for(k=k0; k<k1; k++)
     497  {
     498    i = sp?Di[k]:k;
     499    const double& Dij = sp?Dx[k]:Dx[h++];
     500    int Dij_sign = gsl_fcmp(Dij+1.0,1.0,DBL_EPSILON);
     501    if(Dij_sign!=0)
     502    {
     503      bx[i] += Dij*zj;
     504    //Std(BText("\nD[")+i+","+j+"]="+Dij+"\tz["+j+"]="+zj+"\tb["+i+"]="+bx[i]+"\td["+i+"]="+dx[i]);
     505      if(Dij_sign<0)
     506      {
     507        aux = bx[i]/Dij;
     508        if(lower<aux) { lower = aux; }
     509      //Std(BText("\naux=")+aux+"\tlower="<<lower);
     510      }
     511      else //if(Dij_sign>0)
     512      {
     513        aux = bx[i]/Dij;
     514        if(upper>aux) { upper = aux; }
     515      //Std(BText("\naux=")+aux+"\tupper="<<upper);
     516      }
     517    }
     518  }
     519//Std(BText("\nlower=")<<lower<<"\tupper="<<upper);
     520  double length = lower-upper;
     521  int length_sign = gsl_fcmp(length+1.0,1.0,DBL_EPSILON);
     522  if(length_sign>=0)
     523  {
     524    if(length_sign==0)
     525    {
     526      lower = upper;
     527    }
     528    else
     529    {
     530      err_cannot_apply("GetBoundsInPolytope",
     531        I2("(empty interval",
     532           "(intervalo vacio")+" ["+lower+","+upper+"])",z);
     533      return(-1);
     534    }
     535  }
     536  return(0);
     537}
     538
    433539////////////////////////////////////////////////////////////////////////////////
    434540  int BVMat::trunc_std_gaussian(const BVMat& D,
    … …  
    436542                                      BVMat& b,
    437543                                      BVMat& z)
    438 // Generates a set of standarized multibormal z constrained to
     544// Generates a set of standarized multinormal z constrained to
    439545//   D*z <= d 
    440546// Value od d-D*z is stored in b and must be initialized by the caller function
    … …  
    563669////////////////////////////////////////////////////////////////////////////////
    564670{
    565   if(!D .CheckDefined("TruncStdGaussian")) { return; }
    566   if(!d_.CheckDefined("TruncStdGaussian")) { return; }
    567   if(!z0.CheckDefined("TruncStdGaussian")) { return; }
    568671  static char* fName = "TruncStdGaussian";
     672  if(!D .CheckDefined(fName)) { return; }
     673  if(!d_.CheckDefined(fName)) { return; }
     674  if(!z0.CheckDefined(fName)) { return; }
    569675  int Dr = D.Rows();
    570676  int Dc = D.Columns();
  • tol/btol/vmatrix_type/vmatgra.cpp

    rbaa9d61 r7db6dd2  
    20342034  assert(contens_.Check());
    20352035}
     2036
     2037//--------------------------------------------------------------------
     2038DeclareContensClass(BSet, BSetTemporary, BSetGetBoundsInPolytope);
     2039DefExtOpr(1, BSetGetBoundsInPolytope, "GetBoundsInPolytope", 4, 4,
     2040  "VMatrix VMatrix VMatrix Real",
     2041  "(VMatrix D, VMatrix d, VMatrix z, Real j)",
     2042  I2("Calculates minimum and maximum values of j-th component of "
     2043     "vector z inside the polytope D*z <= d",
     2044     "Calcula los valores mínimo y máximo de la j-ésima componente "
     2045     "del vector z dentro del politopo D*z <= d"),
     2046BOperClassify::MatrixAlgebra_);
     2047//--------------------------------------------------------------------
     2048void BSetGetBoundsInPolytope::CalcContens()
     2049//--------------------------------------------------------------------
     2050{
     2051  BVMat& D = VMat(Arg(1));
     2052  BVMat& d = VMat(Arg(2));
     2053  BVMat& z = VMat(Arg(3));
     2054  int j =  (int)Real(Arg(4))-1;
     2055  double lower;
     2056  double upper;
     2057  int ok = BVMat::GetBoundsInPolytope(D, d, z, j, lower, upper)==0;
     2058  if(ok)
     2059  {
     2060    contens_.PrepareStore(2);
     2061    BGrammar::IncLevel();
     2062    contens_.AddElement(BContensDat ::New("Lower",lower,""));
     2063    contens_.AddElement(BContensDat ::New("Upper",upper,""));
     2064    contens_.SetIndexByName();
     2065    BGrammar::DecLevel();
     2066  }
     2067}
     2068
    20362069/*
    20372070//--------------------------------------------------------------------
Note: See TracChangeset for help on using the changeset viewer.