numerictools.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. #ifndef _NUMERICTOOLS_FBASICS_H
  2. #define _NUMERICTOOLS_FBASICS_H
  3. /*
  4. * NICE-Core - efficient algebra and computer vision methods
  5. * - libfbasics - library of some basic tools
  6. * See file License for license information.
  7. */
  8. #include <cmath>
  9. #include <math.h>
  10. #include <stdlib.h>
  11. #include <limits>
  12. #include <string>
  13. #ifdef LIMUN_AIBO_MODE
  14. // some functions missing in math.h (NOT in linum namespace!!)
  15. inline float roundf(float x) {
  16. return floor(x + 0.5);
  17. }
  18. inline double round(double x) {
  19. return floor(x + 0.5);
  20. }
  21. inline long double roundl(long double x) {
  22. return floor(x + 0.5);
  23. }
  24. inline long lroundf(float x) {
  25. return (long) floor(x + 0.5);
  26. }
  27. inline long lround(double x) {
  28. return (long) floor(x + 0.5);
  29. }
  30. inline long lroundl(long double x) {
  31. return (long) floor(x + 0.5);
  32. }
  33. #endif // LIMUN_AIBO_MODE
  34. namespace NICE {
  35. /**
  36. * Is a numerical value zero?
  37. */
  38. template<class T>
  39. inline bool isZero(const T& x) {
  40. return x == (T)0;
  41. }
  42. /**
  43. * Is a numerical value zero up to a tolerance?
  44. */
  45. inline bool isZero(float x, float tolerance) {
  46. return fabs(x) < tolerance;
  47. }
  48. /**
  49. * Is a numerical value zero up to a tolerance?
  50. */
  51. inline bool isZero(double x, double tolerance) {
  52. return fabs(x) < tolerance;
  53. }
  54. /**
  55. * Is a numerical value zero?
  56. * Specialization for floating point: Zero up to machine precision?
  57. */
  58. template<>
  59. inline bool isZero(const float& x) {
  60. return isZero(x, std::numeric_limits<float>::epsilon());
  61. }
  62. /**
  63. * Is a numerical value zero?
  64. * Specialization for floating point: Zero up to machine precision?
  65. */
  66. template<>
  67. inline bool isZero(const double& x) {
  68. return isZero(x, std::numeric_limits<double>::epsilon());
  69. }
  70. /**
  71. * Is a numerical value almost zero ? :) up to 1e-15
  72. */
  73. inline bool almostZero(const double a) {
  74. return (fabs(a)<1e-15);
  75. }
  76. /**
  77. * Is \c x = \c y up to a tolerance?
  78. */
  79. inline bool isEqual(float x, float y, float tolerance) {
  80. return fabs(x - y) < tolerance;
  81. }
  82. /**
  83. * Is \c x = \c y up to a tolerance?
  84. */
  85. inline bool isEqual(double x, double y, double tolerance) {
  86. return fabs(x - y) < tolerance;
  87. }
  88. /**
  89. * Is \c x = \c y up to machine precision?
  90. */
  91. inline bool isEqual(float x, float y) {
  92. return isEqual(x, y, std::numeric_limits<float>::epsilon());
  93. }
  94. /**
  95. * Is \c x = \c y up to machine precision?
  96. */
  97. inline bool isEqual(double x, double y) {
  98. return isEqual(x, y, std::numeric_limits<double>::epsilon());
  99. }
  100. /**
  101. * Sign of a numerical value?
  102. * (For floating point values, zero is checked up to machine precision,
  103. * see isZero()).
  104. */
  105. template<class T>
  106. inline T sign(const T& x) {
  107. if (isZero(x)) {
  108. return (T)0;
  109. } else if (x > (T)0) {
  110. return (T)1;
  111. } else {
  112. return (T)(-1);
  113. }
  114. }
  115. /**
  116. * Absolute value (as std::abs() or fabs() for floating point values).
  117. */
  118. template<class T>
  119. inline T absolute(const T& x) {
  120. //return std::abs(x);
  121. if (x >= T(0)) {
  122. return x;
  123. } else {
  124. return -x;
  125. }
  126. }
  127. /**
  128. * Absolute value (as fabs()).
  129. */
  130. template<>
  131. inline float absolute(const float& x) {
  132. return (float) fabs(x);
  133. }
  134. /**
  135. * Absolute value (as fabs()).
  136. */
  137. template<>
  138. inline double absolute(const double& x) {
  139. return (double) fabs(x);
  140. }
  141. /**
  142. * Impose the sign of a numerical value to the sign of another value.
  143. * (For floating point values, zero is checked/handled up to machine precision,
  144. * see isZero()).
  145. * @param x The input value
  146. * @param s The value supplying the sign (its absolute value may be arbitrary)
  147. * @return \c x with the same sign as \c s
  148. */
  149. template<class T>
  150. inline T imposeSign(const T& x, const T& s) {
  151. return absolute(x) * sign(s);
  152. }
  153. /**
  154. * Square.
  155. */
  156. template<class T>
  157. inline T square(const T& t) {
  158. return t * t;
  159. }
  160. /**
  161. * Cube.
  162. */
  163. template<class T>
  164. inline T cube(const T& t) {
  165. return t * t * t;
  166. }
  167. /**
  168. * Cube root: t^(1/3)
  169. */
  170. inline double cubeRoot(const double& t) {
  171. return sign(t) * pow(fabs(t), 1.0 / 3.0);
  172. }
  173. /**
  174. * Check if a floating point value is NaN
  175. */
  176. inline bool isNaN(double x) {
  177. #if (__GNUC__ > 3)
  178. return std::isnan(x);
  179. #else
  180. return x != x;
  181. #endif
  182. }
  183. /**
  184. * Check if a floating point value is NaN
  185. */
  186. inline bool isNaN(float x) {
  187. #if (__GNUC__ > 3)
  188. return std::isnan(x);
  189. #else
  190. return x != x;
  191. #endif
  192. }
  193. /**
  194. * Create NaN
  195. */
  196. inline double doubleNaN() {
  197. double zero = 1.0;
  198. zero -= zero;
  199. return 0.0 / zero;
  200. }
  201. /**
  202. * Create NaN
  203. */
  204. inline float floatNaN() {
  205. float zero = 1.0f;
  206. zero -= zero;
  207. return 0.0f / zero;
  208. }
  209. /**
  210. * Restrict \c value to be within [\c min, \c max].
  211. */
  212. template<class T>
  213. inline const T& limitValue(const T& value, const T& min, const T& max) {
  214. if (value < min) {
  215. return min;
  216. } else if (value > max) {
  217. return max;
  218. } else {
  219. return value;
  220. }
  221. }
  222. /**
  223. * Degree to radian.
  224. */
  225. inline double degreeToRadian(double a) {
  226. return a * M_PI / 180.0;
  227. }
  228. /**
  229. * Radian to degree.
  230. */
  231. inline double radianToDegree(double a) {
  232. return a / M_PI * 180.0;
  233. }
  234. /**
  235. * Normalize an angle to be between 0 and 2 Pi.
  236. */
  237. inline double normalizeAngle(double a) {
  238. const double TWO_PI = 2.0 * M_PI;
  239. return a - floor(a / TWO_PI) * TWO_PI;
  240. }
  241. /**
  242. * Initialize random number generator.
  243. */
  244. void initRand( bool fixedSeed = false, unsigned int seed = 0 );
  245. /**
  246. * A pseudo random number in the range [0,limit), based on \c rand().
  247. * (Initialize generator with \c initRand()).
  248. */
  249. inline int randInt(const int limit) {
  250. if (limit == 0) {
  251. return 0;
  252. } else {
  253. return rand() % limit;
  254. }
  255. }
  256. /**
  257. * A pseudo random number in the range [0,1), based on \c rand().
  258. * (Initialize generator with \c initRand()).
  259. */
  260. inline double randDouble() {
  261. return ((double)rand() / ((double)(RAND_MAX)+1.0));
  262. }
  263. /**
  264. * A pseudo random number in the range [0,limit), based on \c rand().
  265. * (Initialize generator with \c initRand()).
  266. */
  267. inline double randDouble(const double limit) {
  268. return ((double)rand() / ((double)(RAND_MAX)+1.0)) * limit;
  269. }
  270. /**
  271. * A pseudo random number in the range [0,1), based on \c rand().
  272. * (Initialize generator with \c initRand()).
  273. */
  274. inline float randFloat() {
  275. return ((float)rand() / ((float)(RAND_MAX)+1.0f));
  276. }
  277. /**
  278. * A pseudo random number in the range [0,limit), based on \c rand().
  279. * (Initialize generator with \c initRand()).
  280. */
  281. inline float randFloat(const float limit) {
  282. return ((float)rand() / ((float)(RAND_MAX)+1.0f)) * limit;
  283. }
  284. /**
  285. * A pseudo random number generated using a normal distribution
  286. * with an arbitrary standard deviation \c s and zero mean.
  287. */
  288. inline double randGaussDouble ( const double stddev ) {
  289. // adapted from k_reconstruction (Olaf Kähler)
  290. double r1, r2, d;
  291. do {
  292. r1 = 2.0 * randDouble() - 1.0;
  293. r2 = 2.0 * randDouble() - 1.0;
  294. d = r1*r1 + r2*r2;
  295. } while ((d >= 1.0)||(isZero(d)));
  296. d = sqrt((-2.0 * log(d))/d);
  297. double y1 = r1*d;
  298. // y2 = r2*d; this is another random variable
  299. return y1*stddev;
  300. }
  301. /**
  302. * If \c x is NaN, return infinity, x otherwise.
  303. */
  304. inline double nanToInf(double x) {
  305. if (isNaN(x)) {
  306. return std::numeric_limits<double>::infinity();
  307. } else {
  308. return x;
  309. }
  310. }
  311. /**
  312. * Convert string to double, including inf and nan.
  313. * @param s Input string
  314. * @return the double represented by s
  315. * @throw Exception if format not ok
  316. */
  317. double stringToDouble(const char* s);
  318. inline double stringToDouble(std::string s) {
  319. return stringToDouble(s.c_str());
  320. }
  321. /**
  322. * Read double from istream, including inf and nan.
  323. *
  324. * Example1: double x; cin >> x;
  325. *
  326. * Example2: double y; readDouble(y);
  327. *
  328. * If a correct double can be read from the stream,
  329. * both examples do the same. In case of inf and nan,
  330. * only the second version reads correctly.
  331. * Also: errors are signaled differently.
  332. *
  333. * @param s Input string
  334. * @return the double represented by s
  335. * @throw Exception if format not ok
  336. */
  337. inline double readDouble(std::istream& str) {
  338. std::string s;
  339. str >> s;
  340. return stringToDouble(s);
  341. }
  342. /**
  343. * Convert string to int.
  344. * @param s Input string
  345. * @return the int represented by s
  346. * @throw Exception if format not ok
  347. */
  348. long stringToInt(const char* s);
  349. inline long stringToInt(std::string s) {
  350. return stringToInt(s.c_str());
  351. }
  352. /**
  353. * Convert an integer into a string.
  354. */
  355. std::string intToString(const int i);
  356. /**
  357. * Convert a double into a string.
  358. */
  359. std::string doubleToString(const double d, const unsigned int digits = 6,
  360. const bool fixedPoint = false);
  361. /**
  362. * Round a floating point value and convert to int.
  363. */
  364. inline int roundInt(double d) {
  365. return int(round(d));
  366. }
  367. /**
  368. * Round a floating point value and convert to int.
  369. */
  370. inline int roundInt(float d) {
  371. return int(roundf(d));
  372. }
  373. } // namespace
  374. #endif // _NUMERICTOOLS_FBASICS_H