tools.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /**
  2. * @file tools.h
  3. * @brief Some very basic methods (e.g. generation of random numbers) (Interface and Implementation)
  4. * @author Alexander Freytag
  5. * @date 12/06/2011
  6. */
  7. #ifndef FASTHIK_TOOLSINCLUDE
  8. #define FASTHIK_TOOLSINCLUDE
  9. #include "core/vector/MatrixT.h"
  10. #include "core/vector/VectorT.h"
  11. #include <cstdlib>
  12. #include <vector>
  13. #include <algorithm>
  14. #include "core/vector/MatrixT.h"
  15. #include <iostream>
  16. #include <fstream>
  17. using namespace std;
  18. /**
  19. * @brief float extension of rand
  20. * @author Alexander Freytag
  21. * @date 12/06/2011
  22. */
  23. inline float frand() { return (float)rand() / (float)(RAND_MAX); }; //returns from 0 to 1
  24. /**
  25. * @brief double extension of rand
  26. * @author Alexander Freytag
  27. * @date 12/06/2011
  28. */
  29. inline double drand() { return (double)rand() / (double)(RAND_MAX); }; //returns from 0 to 1
  30. /**
  31. * @brief generates a random matrix of size n x d with values between zero and one
  32. * @author Alexander Freytag
  33. * @date 12/06/2011
  34. */
  35. inline void generateRandomFeatures(const int & n, const int & d , NICE::Matrix & features)
  36. {
  37. features.resize(n,d);
  38. for (int i = 0; i < n; i++)
  39. {
  40. for (int j = 0; j < d; j++)
  41. {
  42. features(i,j) = drand();
  43. }
  44. }
  45. }
  46. /**
  47. * @brief generates a std::vector of NICE::Vector ( size n x d ) with values between zero and one
  48. * @author Alexander Freytag
  49. * @date 12/06/2011
  50. */
  51. inline void generateRandomFeatures(const int & n, const int & d , std::vector<NICE::Vector> & features)
  52. {
  53. features.clear();
  54. for (int i = 0; i < n; i++)
  55. {
  56. NICE::Vector feature(d);
  57. for (int j = 0; j < d; j++)
  58. {
  59. feature[i] = drand();
  60. }
  61. features.push_back(feature);
  62. }
  63. }
  64. /**
  65. * @brief generates a std::vector of NICE::Vector ( size n x d ) with values between zero and one
  66. * @author Alexander Freytag
  67. * @date 12/06/2011
  68. */
  69. inline void generateRandomFeatures(const int & n, const int & d , std::vector<std::vector<double> > & features)
  70. {
  71. features.clear();
  72. for (int i = 0; i < n; i++)
  73. {
  74. std::vector<double> feature;
  75. feature.clear();
  76. for (int j = 0; j < d; j++)
  77. {
  78. feature.push_back(drand());
  79. }
  80. features.push_back(feature);
  81. }
  82. }
  83. /**
  84. * @brief generates a std::vector of std::vector with size n and values between zero and one
  85. * @author Alexander Freytag
  86. * @date 12/06/2011
  87. */
  88. inline void generateRandomFeatures(const int & n, std::vector<double> & features)
  89. {
  90. features.clear();
  91. for (int i = 0; i < n; i++)
  92. {
  93. features.push_back(drand());
  94. }
  95. }
  96. /**
  97. * @brief generates a std::vector of std::vector with size n and values between zero and one
  98. * @author Alexander Freytag
  99. * @date 12/06/2011
  100. */
  101. inline void generateRandomFeatures(const int & n, NICE::Vector & features)
  102. {
  103. features.resize(n);
  104. for (int i = 0; i < n; i++)
  105. {
  106. features[i] = drand48();
  107. }
  108. }
  109. /**
  110. * @brief generates a NICE::Matrix with random entries between zero and range
  111. * @author Alexander Freytag
  112. * @date 05-01-2012 (dd-mm-yyyy)
  113. */
  114. inline NICE::Matrix generateRandomMatrix(const int & rows, const int & cols, const bool & symmetric = false, const int & range=1)
  115. {
  116. NICE::Matrix M(rows,cols);
  117. if (symmetric)
  118. {
  119. //TODO check, wether is is more efficient to run first over j and then over i
  120. for (int i = 0; i < rows; i++)
  121. for (int j = i; j < cols; j++)
  122. {
  123. M(i,j) = drand()*range;
  124. M(j,i) = M(i,j);
  125. }
  126. }
  127. else
  128. {
  129. //TODO check, wether is is more efficient to run first over j and then over i
  130. for (int i = 0; i < rows; i++)
  131. for (int j = 0; j < cols; j++)
  132. M(i,j) = drand()*range;
  133. }
  134. return M;
  135. }
  136. /**
  137. * @brief generates a NICE::Matrix with random entries between zero and range
  138. * @author Alexander Freytag
  139. * @date 05-01-2012 (dd-mm-yyyy)
  140. */
  141. inline void generateRandomMatrix(NICE::Matrix M, const int & rows, const int & cols, const bool & symmetric = false, const int & range=1)
  142. {
  143. M.resize(rows,cols);
  144. if (symmetric)
  145. {
  146. //TODO check, wether is is more efficient to run first over j and then over i
  147. for (int i = 0; i < rows; i++)
  148. for (int j = i; j < cols; j++)
  149. {
  150. M(i,j) = drand()*range;
  151. M(j,i) = M(i,j);
  152. }
  153. }
  154. else
  155. {
  156. //TODO check, wether is is more efficient to run first over j and then over i
  157. for (int i = 0; i < rows; i++)
  158. for (int j = 0; j < cols; j++)
  159. M(i,j) = drand()*range;
  160. }
  161. }
  162. /**
  163. * @brief computes arbitrary Lp-Norm of a double-vector. Standard is L2-norm
  164. * @author Alexander Freytag
  165. * @date 12/08/2011
  166. */
  167. inline double vectorNorm(const std::vector<double> & a, const int & p=2)
  168. {
  169. double norm(0.0);
  170. for (int i = 0; i < (int) a.size(); i++)
  171. {
  172. norm += pow(fabs(a[i]),p);
  173. }
  174. norm = pow(norm, 1.0/p);
  175. return norm;
  176. }
  177. /**
  178. * @brief Transposes a vector of vectors, assuming all inner vectors having the same length. Allocates space as much as already needed for the current data
  179. * @author Alexander Freytag
  180. * @date 12/08/2011
  181. */
  182. template<class ElementType>
  183. inline void transposeVectorOfVectors(std::vector<std::vector<ElementType> > & features)
  184. {
  185. //unsave! did not check wether all dimensions are equally filled
  186. int d (features.size());
  187. int n ( (features[0]).size() );
  188. std::vector<std::vector<ElementType> > old_features(features);
  189. int tmp(n);
  190. n = d;
  191. d = tmp;
  192. features.resize(d);
  193. for (int dim = 0; dim < d; dim++)
  194. {
  195. features[dim].resize(n);
  196. for (int feat = 0; feat < n; feat++)
  197. {
  198. (features[dim])[feat] = (old_features[feat])[dim];
  199. }
  200. }
  201. }
  202. /**
  203. * @brief Prints the whole Matrix (outer loop over dimension, inner loop over features)
  204. * @author Alexander Freytag
  205. * @date 12/07/2011
  206. */
  207. inline void printMatrix(NICE::Matrix K)
  208. {
  209. for (int row = 0; row < (int)K.rows(); row++)
  210. {
  211. for (int col = 0; col < (int)K.cols(); col++)
  212. {
  213. std::cerr << K(row,col) << " ";
  214. }
  215. std::cerr << std::endl;
  216. }
  217. };
  218. /**
  219. * @brief Prints the whole Matrix (outer loop over dimension, inner loop over features)
  220. * @author Alexander Freytag
  221. * @date 12/07/2011
  222. */
  223. template <typename T>
  224. inline void printMatrix(const std::vector< std::vector<T> > & K)
  225. {
  226. for (int row = 0; row < (int)K.size(); row++)
  227. {
  228. for (int col = 0; col < (int)K[row].size(); col++)
  229. {
  230. std::cerr << K[row][col] << " ";
  231. }
  232. std::cerr << std::endl;
  233. }
  234. };
  235. template <class T>
  236. inline void read_values(const std::string & file_of_values, std::vector<T> & values)
  237. {
  238. std::cerr << "read from file " << file_of_values << std::endl;
  239. values.clear();
  240. ifstream iss(file_of_values.c_str());
  241. if ( !iss.good() )
  242. {
  243. std::cerr << "read_images::Unable to read the data!" << std::endl;
  244. return;
  245. }
  246. while ( ! iss.eof())
  247. {
  248. string str_float;
  249. T val;
  250. iss >> val; //str_float;
  251. // str_float >> val;
  252. values.push_back(val);
  253. std::cerr << val << " ";
  254. }
  255. std::cerr << std::endl;
  256. iss.close();
  257. }
  258. template <class T>
  259. inline void write_values(const std::string & destination, const std::vector<T> & values)
  260. {
  261. ofstream oss(destination.c_str());
  262. if ( !oss.good() )
  263. {
  264. std::cerr << "read_images::Unable to write the data!" << std::endl;
  265. return;
  266. }
  267. for (uint i = 0; i < values.size(); i++)
  268. // for (typename std::vector<T>::const_iterator it = values.begin; it != values.end(); it++)
  269. {
  270. oss << values[i] << std::endl;
  271. }
  272. oss.close();
  273. }
  274. template <class T>
  275. inline void calculating_mean(const std::vector<T> numbers, T & mean)
  276. {
  277. mean = 0.0;
  278. if (numbers.size() == 0)
  279. {
  280. #ifndef NO_PROMPTS
  281. cerr << "calculating_mean: No numbers given." << endl;
  282. #endif
  283. return;
  284. }
  285. for (typename std::vector<T>::const_iterator it = numbers.begin(); it != numbers.end(); it++)
  286. mean += *it;
  287. if (numbers.size() > 0)
  288. mean /= (T) ((double) numbers.size());
  289. else
  290. mean = (T) 0;
  291. }
  292. template <class T>
  293. inline void calculating_mean(const std::vector<T> numbers, std::vector<T> & means, const int & stepSize)
  294. {
  295. means.clear();
  296. double mean(0.0);
  297. if (numbers.size() == 0)
  298. {
  299. #ifndef NO_PROMPTS
  300. cerr << "calculating_mean: No numbers given." << endl;
  301. #endif
  302. return;
  303. }
  304. int cnt(0);
  305. for (typename std::vector<T>::const_iterator it = numbers.begin(); it != numbers.end(); it++, cnt++)
  306. {
  307. mean += *it;
  308. if ( cnt == (stepSize-1))
  309. {
  310. mean /= (T) ((double) stepSize);
  311. means.push_back(mean);
  312. mean = 0.0;
  313. cnt = -1;
  314. }
  315. }
  316. }
  317. inline double calculating_mean(const std::vector<double> numbers)
  318. {
  319. double mean(0.0);
  320. if (numbers.size() == 0)
  321. {
  322. #ifndef NO_PROMPTS
  323. cerr << "calculating_mean: No numbers given." << endl;
  324. #endif
  325. return mean;
  326. }
  327. for (std::vector<double>::const_iterator it = numbers.begin(); it != numbers.end(); it++)
  328. mean += *it;
  329. mean /= ((double) numbers.size());
  330. return mean;
  331. }
  332. inline void calculateMeanPerDimension(const std::vector<std::vector<double> > & numbers, std::vector<double> & meanValues)
  333. {
  334. if (numbers.size() == 0)
  335. return;
  336. meanValues.resize(numbers[0].size());
  337. for (uint dim = 0; dim < numbers[0].size(); dim++)
  338. {
  339. meanValues[dim] = 0.0;
  340. }
  341. for (uint i = 0; i < numbers.size(); i++)
  342. {
  343. for (uint dim = 0; dim < numbers[i].size(); dim++)
  344. {
  345. meanValues[dim] += numbers[i][dim];
  346. }
  347. }
  348. for (uint dim = 0; dim < numbers[0].size(); dim++)
  349. {
  350. meanValues[dim] /= (double) numbers.size();
  351. }
  352. }
  353. inline void calculating_std_dev(const std::vector<double> numbers, const double & mean, double & std_dev)
  354. {
  355. std_dev = 0.0;
  356. if (numbers.size() == 0)
  357. {
  358. #ifndef NO_PROMPTS
  359. cerr << "calculating_mean: No numbers given." << endl;
  360. #endif
  361. return;
  362. }
  363. for (std::vector<double>::const_iterator it = numbers.begin(); it != numbers.end(); it++)
  364. std_dev += pow((*it) - mean,2);
  365. std_dev /= ((double) numbers.size());
  366. std_dev = sqrt(std_dev);
  367. }
  368. inline double calculating_std_dev(const std::vector<double> numbers, const double & mean)
  369. {
  370. double std_dev (0.0);
  371. if (numbers.size() == 0)
  372. {
  373. #ifndef NO_PROMPTS
  374. cerr << "calculating_mean: No numbers given." << endl;
  375. #endif
  376. return std_dev;
  377. }
  378. for (std::vector<double>::const_iterator it = numbers.begin(); it != numbers.end(); it++)
  379. std_dev += pow((*it) - mean,2);
  380. std_dev /= ((double) numbers.size());
  381. std_dev = sqrt(std_dev);
  382. return std_dev;
  383. }
  384. #endif