PFExp.h 3.6 KB

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