5 #ifndef MC__POLYMODEL_H
6 #define MC__POLYMODEL_H
23 #undef MC__POLYMODEL_DEBUG
24 #define MC__POLYMODEL_CHECK
29 typedef unsigned long long poly_size;
46 (
const unsigned int nvar,
const unsigned int nord )
47 { _size( nvar, nord ); }
80 (
const unsigned int *iexp )
92 (
const unsigned int n,
const unsigned int k )
114 return "mc::PolyModel\t Inconsistent polynomial model dimension";
116 return "mc::PolyModel\t Maximum size in polynomial model reached";
118 return "mc::PolyModel\t Feature not yet implemented in mc::PolyModel class";
120 return "mc::PolyModel\t Undocumented error";
132 (
const unsigned int nvar,
const unsigned int nord );
173 (
const unsigned int nord );
177 (
const unsigned int maxord );
181 (
const unsigned int nord );
185 (
const unsigned int maxord,
const bool full=
false );
189 (
unsigned int *iexp,
const unsigned int iord )
const;
193 (
const unsigned int *iexp )
const;
197 (
const unsigned int nord );
201 (
const unsigned int nord );
205 (
const unsigned int n,
const unsigned int k )
const;
212 (
const unsigned int nvar,
const unsigned int nord )
218 _binom =
new poly_size[nord?(nvar+nord-1)*(nord+1):nvar];
219 _binom_size = std::make_pair( nvar+nord-1, nord+1 );
221 _posord =
new unsigned int[nord+2];
233 (
const unsigned int nord )
237 for(
unsigned int i=1; i<=nord; i++ ){
238 poly_size _posord_next = _posord[i] + _get_binom( _nvar+i-1, i );
239 if( _posord_next > UINT_MAX )
241 _posord[i+1] = _posord_next;
244 #ifdef MC__POLYMODEL_DEBUG
245 mc::display( 1, nord+2, _posord, 1,
"_posord", std::cout );
251 (
const unsigned int maxord )
253 if( maxord < _posord_size )
return;
255 _posord =
new unsigned int[maxord+2];
256 _posord_size = maxord;
257 _set_posord( maxord );
262 (
const unsigned int nord )
264 unsigned int *iexp =
new unsigned int[_nvar] ;
265 for(
unsigned int k=0; k<_nvar; k++ ) _expmon[k] = 0;
266 for(
unsigned int i=1; i<=nord; i++ ){
267 for(
unsigned int j=0; j<_nvar; j++ ) iexp[j] = 0;
268 for(
unsigned int j=_posord[i]; j<_posord[i+1]; j++ ){
269 _next_expmon( iexp, i );
270 for(
unsigned int k=0; k<_nvar; k++ )
271 _expmon[j*_nvar+k] = iexp[k];
276 #ifdef MC__POLYMODEL_DEBUG
277 mc::display( _nvar, _expmon_size, _expmon, _nvar,
"_expmon", std::cout );
283 (
unsigned int *iexp,
const unsigned int iord )
const
287 iexp[_nvar-1] += iord;
288 unsigned int j = _nvar;
289 while( j > 0 && iexp[j-1] > iord ){
290 iexp[j-1] -= iord + 1;
295 for(
unsigned int i=0; i<_nvar; i++ ) curord += iexp[i];
296 }
while( curord != iord );
301 (
const unsigned int maxord,
const bool full )
303 _ext_binom( maxord );
304 _ext_posord( maxord );
306 if( full ) _expmon_size = std::pow(maxord+1,_nvar);
307 else _expmon_size = _posord[maxord+1];
308 _expmon =
new unsigned int[ _expmon_size*_nvar];
309 _set_expmon( maxord );
312 unsigned int *iexp =
new unsigned int[_nvar];
313 for(
unsigned int iord=maxord+1, jmon=_posord[maxord+1];
314 jmon<_expmon_size; iord++ ){
317 if( _posord[iord] >= _posord[iord+1] )
319 for(
unsigned int ivar=0; ivar<_nvar; ivar++ ) iexp[ivar] = 0;
320 for(
unsigned int kmon=_posord[iord]; kmon<_posord[iord+1]; kmon++ ){
321 _next_expmon( iexp, iord );
322 bool lt_maxord =
true;
323 for(
unsigned int ivar=0; ivar<_nvar; ivar++ ){
324 if( iexp[ivar] > maxord ){ lt_maxord =
false;
break; }
325 _expmon[jmon*_nvar+ivar] = iexp[ivar];
328 #ifdef MC__POLYMODEL_DEBUG
329 std::cout << jmon <<
":";
330 for(
unsigned int ivar=0; ivar<_nvar; ivar++ )
331 std::cout <<
" " << _expmon[jmon*_nvar+ivar];
332 std::cout << std::endl;
335 if( jmon >= _expmon_size )
break;
345 (
const unsigned int *iexp )
const
347 unsigned int ord = 0;
348 for(
unsigned int i=0; i<_nvar; i++ ) ord += iexp[i];
349 #ifdef MC__POLYMODEL_CHECK
350 assert( ord<_nord+2 );
352 unsigned int pos = _posord[ord];
354 unsigned int p = _nvar ;
355 for(
unsigned int i=0; i<_nvar-1; i++ ){
357 for(
unsigned int j=0; j<iexp[i]; j++ )
358 pos += _get_binom( p-1+ord-j, ord-j );
367 (
const unsigned int nord )
371 for(
unsigned int i=0; i<_nvar+nord-1; i++ ){
372 p = &_binom[i*(nord+1)];
377 k = ( i+1<nord? i+1: nord );
378 for(
unsigned int j=2; j<=k; j++, p++ ) *p = *(p-1) * (i+2-j)/j;
379 for(
unsigned int j=k+1; j<=nord; j++, p++ ) *p = 0.;
381 #ifdef MC__POLYMODEL_DEBUG
382 std::cout <<
"nvar: " << _nvar <<
" nord: " << nord << std::endl;
383 mc::display( _binom_size.second, _binom_size.first, _binom,
384 _binom_size.second,
"_binom", std::cout );
390 (
const unsigned int maxord )
392 if( maxord < _binom_size.second )
return;
394 _binom =
new poly_size[(_nvar+maxord-1)*(maxord+1)];
395 _binom_size = std::make_pair( _nvar+maxord-1, maxord+1 );
396 _set_binom( maxord );
401 (
const unsigned int n,
const unsigned int k )
const
403 #ifdef MC__POLYMODEL_CHECK
404 assert( n<=_binom_size.first );
405 assert( k<=_binom_size.second );
408 return( n? _binom[(n-1)*_binom_size.second+k]: 1 );
412 PolyModel::_cleanup()
424 template <
typename T>
470 (
const unsigned int iord )
476 (
const unsigned int imon )
482 (
const unsigned int* iexp )
488 (
const unsigned int n,
const unsigned int k )
561 for(
unsigned int i=0; i<nord()+2; i++)
_bndord[i] = var.
_bndord[i];
582 {
for(
unsigned int imon=0; imon<(
_PM?
nmon():1); imon++ )
589 ( std::pair<unsigned int, const double*>& coefmon )
590 {
for(
unsigned int imon=0; imon<(
_PM?
nmon():1); imon++ )
591 _coefmon[imon] = ( imon<coefmon.first && coefmon.second?
592 coefmon.second[imon]: 0. );
604 (
const PolyVar<T>& var,
const bool reset=
true );
616 (
const int type )
const
630 (
const unsigned int iord )
632 {
if( !iord || (
_PM && iord<=
nord()) )
647 {
return bound( type ); }
663 (
const unsigned int*iexp )
667 std::pair<unsigned int, const double*>
coefmon
672 std::pair<unsigned int, const unsigned int*>
expmon
684 template <
typename T>
inline PolyVar<T>&
688 if( !_PM || ( var.
_PM && nvar() < var.
nvar() ) )
return *
this;
691 for(
unsigned int imon=0; reset && imon<nmon(); imon++ ) _coefmon[imon] = 0.;
692 if( reset ) *_bndrem = 0.;
696 if( !var.
_PM ){ *_bndrem = *var.
_bndrem; _bndord_uptd =
false;
return *
this; }
697 unsigned int*iexp =
new unsigned int[nvar()];
698 for(
unsigned int jmon=1; jmon<var.
nmon() && jmon<var.
_posord(nord()+1); jmon++ ){
699 for(
unsigned int ivar=0; ivar<nvar(); ivar++ )
700 iexp[ivar] = ( ivar<var.
nvar()? var.
_expmon(jmon)[ivar]: 0 );
701 _coefmon[_loc_expmon(iexp)] = var.
_coefmon[jmon];
704 for(
unsigned int iord=nord()+1; iord<=var.
nord(); iord++ )
705 *_bndrem += var.
bndord(iord);
708 _bndord_uptd =
false;
719 if( reset ) _coefmon[0] = 0.;
722 if( !var.
_PM || nvar() < var.
nvar() || nord() < var.
nord() )
726 unsigned int*iexp =
new unsigned int[nvar()];
727 for(
unsigned int jmon=0; jmon<(var.
_PM?var.
nmon():1); jmon++ ){
728 for(
unsigned int ivar=0; ivar<nvar(); ivar++ )
729 iexp[ivar] = (ivar<var.
nvar()? var.
_expmon(jmon)[ivar]: 0);
730 var.
_coefmon[jmon] = _coefmon[_loc_expmon(iexp)];
732 if( reset ) _coefmon[_loc_expmon(iexp)] = 0.;
741 _bndord_uptd =
false;
746 template <
typename T>
inline void
752 _coefmon =
new double[1];
757 _coefmon =
new double[nmon()];
758 _bndord =
new T[nord()+2];
759 _bndrem = _bndord + nord()+1;
761 _bndord_uptd =
false;
766 template <
typename T>
inline void
769 delete [] _coefmon;
delete [] _bndord;
delete _bndpol;
770 _coefmon = 0; _bndord = _bndrem = _bndpol = 0;
773 template <
typename T>
inline void
777 if( _PM == env )
return;
794 if(
this == &var )
return *
this;
806 for(
unsigned int i=0; i<nmon(); i++ ) _coefmon[i] = var.
_coefmon[i];
817 template <
typename T>
inline void
825 template <
typename T>
inline void
830 if( _bndpol )
delete _bndpol;
834 _bndpol =
new T( *bndpol );
839 template <
typename T>
inline void
844 _bndpol =
new T( bndpol );
849 template <
typename T>
inline void
853 _coefmon[0] += remmid;
854 if( _PM ) _bndord[0] = _coefmon[0];
856 if( _bndpol ) *_bndpol += remmid;
859 template <
typename T>
inline double
861 (
const unsigned int*iexp )
865 const unsigned int imon = _PM->loc_expmon( iexp );
866 return( imon<nmon()? _coefmon[imon]: 0. );
869 template <
typename T>
inline std::pair<unsigned int, const double*>
873 return std::make_pair( (_PM?nmon():1), _coefmon );
876 template <
typename T>
inline std::pair<unsigned int, const unsigned int*>
880 return std::make_pair( (_PM?nmon()*nvar():1), _PM->expmon );
virtual PolyModel * env() const
Get pointer to associated polynomial model environment.
Definition: polymodel.hpp:611
unsigned _posord_size
Order used to size _posord.
Definition: polymodel.hpp:151
bool _modvar
Have any of the model variables been modified?
Definition: polymodel.hpp:166
unsigned int nmon() const
Total number of monomial terms in polynomial model.
Definition: polymodel.hpp:463
const unsigned int * expmon() const
Const pointer to array of size nmon()*nvar() with variable exponents on each monomial term...
Definition: polymodel.hpp:74
unsigned int _nvar
Number of variables in polynomial model.
Definition: polymodel.hpp:142
C++ base class for the computation of polynomial models for factorable functions: Environment...
Definition: polymodel.hpp:36
void _size(PolyModel *env)
Initialize private/protected members of variable.
Definition: polymodel.hpp:748
unsigned int _nord
Order of polynomial model.
Definition: polymodel.hpp:139
std::pair< unsigned int, const double * > coefmon() const
Get pair of size of, and const pointer to, array of (possibly scaled) monomial coefficients in multiv...
Definition: polymodel.hpp:870
virtual void _update_bndord() const =0
Update private/protected members of variable after manipulation the coefficients (pure virtual) ...
void _set_bndmon()
Populate array _bndmon
virtual ~PolyVar()
Destructor of variable.
Definition: polymodel.hpp:568
virtual void _center()
Center remainder error term _bndrem
Definition: polymodel.hpp:850
T remainder() const
Return remainder term of variable.
Definition: polymodel.hpp:639
void _set_expmon(const unsigned int nord)
Populate array _expmon up to order nord
Definition: polymodel.hpp:262
unsigned int * _posord
Array of size _nord with indices of first monomial term of order iord=1,...,_nord in polynomial model...
Definition: polymodel.hpp:148
unsigned int nmon() const
Total number of monomial terms in polynomial model.
Definition: polymodel.hpp:68
T * _bndrem
Pointer to remainder bound of variable (possibly NULL if not computed)
Definition: polymodel.hpp:443
virtual T _polybound() const =0
Polynomial range bounder using specified bounder type (pure virtual)
std::string what()
Error description.
Definition: polymodel.hpp:111
void _ext_binom(const unsigned int nord)
Extend array _binom with binomial coefficients up to order nord
Definition: polymodel.hpp:390
PolyModel * _PM
Pointer to corresponding polynomial model environment.
Definition: polymodel.hpp:431
poly_size * _binom
Array of (_nvar+_nord-1)*(_nord+1) contining binomial coefficients.
Definition: polymodel.hpp:160
std::pair< unsigned int, const unsigned int * > expmon() const
Get pair of size of, and const pointer to, array of monomial exponents in multivariate polynomial of ...
Definition: polymodel.hpp:877
const unsigned int _loc_expmon(const unsigned int *iexp) const
Index of monomial term whose variable exponents are the same as those in array iexp (of size nvar()) ...
Definition: polymodel.hpp:482
T * _bndpol
Pointer to polynomial bound of variable.
Definition: polymodel.hpp:446
unsigned int _loc_expmon(const unsigned int *iexp) const
Get index of monomial term with variable exponents iexp in 1,...,_nmon
Definition: polymodel.hpp:345
poly_size _get_binom(const unsigned int n, const unsigned int k) const
Get binomial coefficient .
Definition: polymodel.hpp:488
unsigned int loc_expmon(const unsigned int *iexp) const
Index of monomial term whose variable exponents are the same as those in array iexp (of size nvar()) ...
Definition: polymodel.hpp:80
void _resize(PolyModel *env)
Reinitialize private/protected members of variable.
Definition: polymodel.hpp:775
Exceptions(TYPE ierr)
Constructor for error ierr
Definition: polymodel.hpp:107
std::pair< unsigned int, unsigned int > _binom_size
Maximum binomial coefficients in array _binom.
Definition: polymodel.hpp:163
Exceptions of mc::PolyModel.
Definition: polymodel.hpp:97
TYPE
Enumeration type for TModel exception handling.
Definition: polymodel.hpp:101
void _cleanup()
Clean up private/protected members of variable.
Definition: polymodel.hpp:767
poly_size _get_binom(const unsigned int n, const unsigned int k) const
Get binomial coefficient .
Definition: polymodel.hpp:401
Number of variable in polynomial model must be nonzero.
Definition: polymodel.hpp:102
unsigned int _posord(const unsigned int iord) const
Index of first monomial term of order iord in polynomial model.
Definition: polymodel.hpp:470
virtual T B() const
Shortcut to mc::PolyVar::bound.
Definition: polymodel.hpp:651
Feature not yet implemented in mc::PolyModel.
Definition: polymodel.hpp:104
virtual PolyVar< T > & set(PolyModel *env)
Set polynomial model environment in variable as env
Definition: polymodel.hpp:573
const unsigned int * _expmon(const unsigned int imon) const
Const pointer to array of size nvar of variable exponents in monomial term imon of polynomial model...
Definition: polymodel.hpp:476
void _ext_posord(const unsigned int maxord)
Extend array _posord up to maximum order maxord
Definition: polymodel.hpp:251
poly_size get_binom(const unsigned int n, const unsigned int k) const
Get binomial coefficient .
Definition: polymodel.hpp:92
int ierr()
Error flag.
Definition: polymodel.hpp:109
C++ base class for the computation of polynomial models for factorable functions: Variable...
Definition: polymodel.hpp:425
unsigned int * _expmon
Array of size _nmon*_nvar with variable exponents in monomial terms. The exponent for variable ivar i...
Definition: polymodel.hpp:154
T bndord(const unsigned int iord) const
Retreive bound on all terms with (total) order iord in polynomial model.
Definition: polymodel.hpp:630
T bndpol() const
Retreive bound on multivariate polynomial using default bounder.
Definition: polymodel.hpp:624
Maximum size of polynomial model reached (monomials indexed as unsigned int)
Definition: polymodel.hpp:103
unsigned int nvar() const
Number of variables in polynomial model.
Definition: polymodel.hpp:457
unsigned int nord() const
Order of polynomial model.
Definition: polymodel.hpp:451
virtual void _set_bndpol(const T &bndpol)
Set polynomial bound in variable as bndpol
Definition: polymodel.hpp:841
T R() const
Shortcut to mc::PolyVar::remainder.
Definition: polymodel.hpp:657
bool _bndord_uptd
Whether the bounds in bndord are up-to-date.
Definition: polymodel.hpp:440
unsigned int nord() const
Order of polynomial model environment.
Definition: polymodel.hpp:62
T bound() const
Retreive bound on variable using default bounder.
Definition: polymodel.hpp:620
unsigned int _nmon
Total number of monomial terms in polynomial model.
Definition: polymodel.hpp:145
PolyModel(const unsigned int nvar, const unsigned int nord)
Constructor of polynomial model environment for nvar variables and order nord
Definition: polymodel.hpp:46
PolyVar(PolyModel *env=0)
Constructor of variable linked to polynomial model environment env
Definition: polymodel.hpp:546
const unsigned int * posord() const
Array of size _nord with indices of first monomial term of order iord=1,...,_nord in polynomial model...
Definition: polymodel.hpp:86
virtual PolyVar< T > & get(PolyVar< T > &var, const bool reset=false)
Copy multivariate polynomial coefficients from current variable into variable var, possibly defined in another polynomial model environment with less variables or with a lower expansion order. Copied coefficients are reset to 0 in current Taylor variable if reset=true, otherwise they are left unmodified (default).
Definition: polymodel.hpp:714
double * _coefmon
Array of size _nmon with monomial coefficients of variable.
Definition: polymodel.hpp:434
virtual void _unset_bndpol()
Unset polynomial bound in variable.
Definition: polymodel.hpp:819
void _set_binom(const unsigned int nord)
Populate array _binom with binomial coefficients up to order nord
Definition: polymodel.hpp:367
T * _bndord
Array of size _nord+2 with bounds for all terms of degrees iord=0,...,_nord as well as the remainder ...
Definition: polymodel.hpp:437
void _next_expmon(unsigned int *iexp, const unsigned int iord) const
Generate variable exponents iexp for subsequent monomial order iord
Definition: polymodel.hpp:283
virtual ~PolyModel()
Destructor of polynomial model environment.
Definition: polymodel.hpp:51
unsigned int nvar() const
Number of variables in polynomial model environment.
Definition: polymodel.hpp:56
PolyVar< T > & _set(const PolyVar< T > &var)
Set variable equal to var
Definition: polymodel.hpp:791
void _set_posord(const unsigned int nord)
Populate array _posord up to order nord
Definition: polymodel.hpp:233
void _ext_expmon(const unsigned int maxord, const bool full=false)
Extend array _expmon up to order maxord to accomodate maxmon coefficients.
Definition: polymodel.hpp:301
unsigned _expmon_size
Number of monomial coefficients used to size _expmon.
Definition: polymodel.hpp:157
C++ structure to allow usage of various template parameters in the types mc::McCormick, mc::TModel, mc::TVar, and mc::SpecBnd of MC++ _ Specialization of this structure is required for the template parameters can be found in the header files defining the types mc::McCormick, mc::TModel, mc::TVar, and mc::SpecBnd.
Definition: mcop.hpp:13