| [6bb72a] | 1 | /*
 | 
|---|
 | 2 |  * helpers.hpp
 | 
|---|
 | 3 |  *
 | 
|---|
 | 4 |  *  Created on: Sep 26, 2012
 | 
|---|
 | 5 |  *      Author: heber
 | 
|---|
 | 6 |  */
 | 
|---|
 | 7 | 
 | 
|---|
 | 8 | #ifndef POTENTIALS_HELPERS_HPP_
 | 
|---|
 | 9 | #define POTENTIALS_HELPERS_HPP_
 | 
|---|
 | 10 | 
 | 
|---|
 | 11 | 
 | 
|---|
 | 12 | // include config.h
 | 
|---|
 | 13 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 14 | #include <config.h>
 | 
|---|
 | 15 | #endif
 | 
|---|
 | 16 | 
 | 
|---|
| [dda3e8] | 17 | #include <cmath>
 | 
|---|
 | 18 | //#include <iomanip>
 | 
|---|
 | 19 | #include <limits>
 | 
|---|
 | 20 | //#include <sstream>
 | 
|---|
 | 21 | 
 | 
|---|
| [775dd1a] | 22 | #include "FunctionApproximation/FunctionModel.hpp"
 | 
|---|
 | 23 | #include "FunctionApproximation/FunctionArgument.hpp"
 | 
|---|
 | 24 | 
 | 
|---|
| [dda3e8] | 25 | //#include "CodePatterns/Log.hpp"
 | 
|---|
 | 26 | 
 | 
|---|
| [6bb72a] | 27 | namespace Helpers
 | 
|---|
 | 28 | {
 | 
|---|
 | 29 | 
 | 
|---|
 | 30 |   /** Integer-optimized version for power of n.
 | 
|---|
 | 31 |    *
 | 
|---|
 | 32 |    * This is taken from Julian Iseringhausen's VMG project which is under
 | 
|---|
 | 33 |    * GPL.
 | 
|---|
 | 34 |    *
 | 
|---|
 | 35 |    * @param base base
 | 
|---|
 | 36 |    * @param power power to take \a base to
 | 
|---|
 | 37 |    * @return \a base to the power of \a power
 | 
|---|
 | 38 |    */
 | 
|---|
 | 39 |   inline int intpow(int base, unsigned int power)
 | 
|---|
 | 40 |   {
 | 
|---|
 | 41 |     int result = 1;
 | 
|---|
 | 42 |     while (power != 0) {
 | 
|---|
 | 43 |       if (power & 1)
 | 
|---|
 | 44 |         result *= base;
 | 
|---|
 | 45 |       base *= base;
 | 
|---|
 | 46 |       power >>= 1;
 | 
|---|
 | 47 |     }
 | 
|---|
 | 48 |     return result;
 | 
|---|
 | 49 |   }
 | 
|---|
 | 50 | 
 | 
|---|
 | 51 |   /** Double-optimized version for power of n.
 | 
|---|
 | 52 |    *
 | 
|---|
 | 53 |    * This is taken from Julian Iseringhausen's VMG project which is under
 | 
|---|
 | 54 |    * GPL.
 | 
|---|
 | 55 |    *
 | 
|---|
 | 56 |    * @param base base
 | 
|---|
 | 57 |    * @param power power to take \a base to
 | 
|---|
 | 58 |    * @return \a base to the power of \a power
 | 
|---|
 | 59 |    */
 | 
|---|
 | 60 |   inline double pow(double base, unsigned int power)
 | 
|---|
 | 61 |   {
 | 
|---|
 | 62 |     double result = 1.0;
 | 
|---|
 | 63 |     while (power != 0) {
 | 
|---|
 | 64 |       if (power & 1)
 | 
|---|
 | 65 |         result *= base;
 | 
|---|
 | 66 |       base *= base;
 | 
|---|
 | 67 |       power >>= 1;
 | 
|---|
 | 68 |     }
 | 
|---|
 | 69 |     return result;
 | 
|---|
 | 70 |   }
 | 
|---|
 | 71 | 
 | 
|---|
| [dda3e8] | 72 |   /** Equality operator that takes numerical precision into account.
 | 
|---|
 | 73 |    *
 | 
|---|
 | 74 |    * \param first first operand
 | 
|---|
 | 75 |    * \param second second operand
 | 
|---|
 | 76 |    * \param factor factor to influence numeric threshold
 | 
|---|
 | 77 |    */
 | 
|---|
 | 78 |   template <typename T>
 | 
|---|
 | 79 |   inline bool isEqual(const T &first, const T &second, const double factor=1.) {
 | 
|---|
 | 80 | //    std::stringstream stream;
 | 
|---|
 | 81 | //    stream << std::setprecision(10)
 | 
|---|
 | 82 | //    << "Comparing " << first << " to " << second << ": "
 | 
|---|
 | 83 | //    << fabs(first-second) << "<" << std::numeric_limits<T>::epsilon()*factor << "?";
 | 
|---|
 | 84 | //    LOG(1, stream.str());
 | 
|---|
 | 85 |     return (fabs(first-second) < std::numeric_limits<T>::epsilon()*factor);
 | 
|---|
 | 86 |   }
 | 
|---|
 | 87 | 
 | 
|---|
| [775dd1a] | 88 |   inline std::vector< FunctionModel::arguments_t >
 | 
|---|
 | 89 |   NoOp_Triplefunction(
 | 
|---|
 | 90 |       const argument_t &,
 | 
|---|
 | 91 |       const double)
 | 
|---|
 | 92 |   {
 | 
|---|
 | 93 |     return std::vector< FunctionModel::arguments_t >();
 | 
|---|
 | 94 |   }
 | 
|---|
 | 95 | 
 | 
|---|
| [0f5d38] | 96 |   inline FunctionModel::arguments_t
 | 
|---|
 | 97 |   NoOp_Filterfunction(
 | 
|---|
 | 98 |       const FunctionModel::arguments_t &args
 | 
|---|
 | 99 |       )
 | 
|---|
 | 100 |   {
 | 
|---|
 | 101 |     return args;
 | 
|---|
 | 102 |   }
 | 
|---|
 | 103 | 
 | 
|---|
 | 104 |   inline FunctionModel::arguments_t
 | 
|---|
 | 105 |   returnEmptyArguments()
 | 
|---|
 | 106 |   {
 | 
|---|
 | 107 |     return FunctionModel::arguments_t();
 | 
|---|
 | 108 |   }
 | 
|---|
 | 109 | 
 | 
|---|
| [e1fe7e] | 110 |   inline FunctionModel::list_of_arguments_t
 | 
|---|
 | 111 |   returnEmptyListArguments()
 | 
|---|
 | 112 |   {
 | 
|---|
 | 113 |     return FunctionModel::list_of_arguments_t();
 | 
|---|
 | 114 |   }
 | 
|---|
 | 115 | 
 | 
|---|
| [6bb72a] | 116 | }; /* namespace Helpers */
 | 
|---|
 | 117 | 
 | 
|---|
 | 118 | 
 | 
|---|
 | 119 | #endif /* POTENTIALS_HELPERS_HPP_ */
 | 
|---|