PFAbsExp.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /**
  2. * @file PFAbsExp.h
  3. * @author Erik Rodner, Alexander Freytag
  4. * @brief Parameterized Function: absolute value and exponential operation -- pow(fabs(x), exponent) (Interface + Implementation)
  5. * @date 01/04/2012
  6. */
  7. #ifndef _NICE_PFABSEXPINCLUDE
  8. #define _NICE_PFABSEXPINCLUDE
  9. // STL includes
  10. #include <math.h>
  11. // NICE-core includes
  12. #include <core/vector/VectorT.h>
  13. // NICE-core includes
  14. #include "ParameterizedFunction.h"
  15. namespace NICE {
  16. /**
  17. * @class PFAbsExp
  18. * @brief Parameterized Function: absolute value and exponential operation -- pow(fabs(x), exponent)
  19. * @author Erik Rodner, Alexander Freytag
  20. */
  21. class PFAbsExp : public ParameterizedFunction
  22. {
  23. protected:
  24. double upperBound;
  25. double lowerBound;
  26. public:
  27. /** simple constructor, we only have one parameter */
  28. PFAbsExp( double exponent = 1.0,
  29. double lB = -std::numeric_limits<double>::max(),
  30. double uB = std::numeric_limits<double>::max() ) :
  31. ParameterizedFunction(1)
  32. {
  33. m_parameters[0] = exponent;
  34. upperBound = uB;
  35. lowerBound = lB;
  36. };
  37. ~PFAbsExp(){};
  38. double f ( uint index, double x ) const {
  39. return pow(fabs(x),m_parameters[0]);
  40. }
  41. bool isOrderPreserving() const { return true; };
  42. Vector getParameterUpperBounds() const { return NICE::Vector(1, upperBound); };
  43. Vector getParameterLowerBounds() const { return NICE::Vector(1, lowerBound); };
  44. void setParameterLowerBounds(const NICE::Vector & _newLowerBounds) { if (_newLowerBounds.size() > 0) lowerBound = _newLowerBounds(0);};
  45. void setParameterUpperBounds(const NICE::Vector & _newUpperBounds) { if (_newUpperBounds.size() > 0) upperBound = _newUpperBounds(0);};
  46. /** Persistent interface */
  47. virtual void restore ( std::istream & is, int format = 0 )
  48. {
  49. if (is.good())
  50. {
  51. is.precision (std::numeric_limits<double>::digits10 + 1);
  52. std::string tmp;
  53. bool b_endOfBlock ( false ) ;
  54. while ( !b_endOfBlock )
  55. {
  56. is >> tmp; // start of block
  57. if ( this->isEndTag( tmp, "PFAbsExp" ) )
  58. {
  59. b_endOfBlock = true;
  60. continue;
  61. }
  62. tmp = this->removeStartTag ( tmp );
  63. if ( tmp.compare("upperBound") == 0 )
  64. {
  65. is >> upperBound;
  66. is >> tmp; // end of block
  67. tmp = this->removeEndTag ( tmp );
  68. }
  69. else if ( tmp.compare("lowerBound") == 0 )
  70. {
  71. is >> lowerBound;
  72. is >> tmp; // end of block
  73. tmp = this->removeEndTag ( tmp );
  74. }
  75. else if ( tmp.compare("ParameterizedFunction") == 0 )
  76. {
  77. // restore parent object
  78. ParameterizedFunction::restore(is);
  79. }
  80. else
  81. {
  82. std::cerr << "WARNING -- unexpected PFAbsExp object -- " << tmp << " -- for restoration... aborting" << std::endl;
  83. throw;
  84. }
  85. }
  86. }
  87. else
  88. {
  89. std::cerr << "PFAbsExp::restore -- InStream not initialized - restoring not possible!" << std::endl;
  90. }
  91. };
  92. virtual void store ( std::ostream & os, int format = 0 ) const
  93. {
  94. if (os.good())
  95. {
  96. // show starting point
  97. os << this->createStartTag( "PFAbsExp" ) << std::endl;
  98. os.precision (std::numeric_limits<double>::digits10 + 1);
  99. os << this->createStartTag( "upperBound" ) << std::endl;
  100. os << upperBound << std::endl;
  101. os << this->createEndTag( "upperBound" ) << std::endl;
  102. os << this->createStartTag( "lowerBound" ) << std::endl;
  103. os << lowerBound << std::endl;
  104. os << this->createEndTag( "lowerBound" ) << std::endl;
  105. // store parent object
  106. ParameterizedFunction::store(os);
  107. // done
  108. os << this->createEndTag( "PFAbsExp" ) << std::endl;
  109. }
  110. };
  111. virtual void clear () {};
  112. virtual std::string sayYourName() const {return "absexp";};
  113. };
  114. }
  115. #endif