/tags/rel-0-4-0/FreeSpeech/VQ/include/Cell.h

# · C++ Header · 85 lines · 66 code · 18 blank · 1 comment · 3 complexity · ccac65b4d85e09e02333a6b75eeecbba MD5 · raw file

  1. #include <math.h>
  2. #include <vector>
  3. #include <iostream>
  4. #include "Object.h"
  5. class Cell;
  6. inline double entropy_funct(double x)
  7. {
  8. if (x==0) {
  9. //cerr << "got zero\n";
  10. return 0;
  11. }
  12. return -x*log(x);
  13. }
  14. ostream &operator << (ostream &out, const Cell &cell);
  15. class MutualInformation {
  16. public:
  17. inline static float mmi (vector<int> Nij, int Nj, vector<int> Ai)
  18. {
  19. float ent = 0;
  20. for (int i = 0; i < Nij.size(); i++)
  21. {
  22. float Pc = float(Nij[i]) / Nj;
  23. float Pa = float(Ai[i]) / Nij[i];
  24. ent -= Pc * log(Pc) + Pc * ( Pa * log(Pa) + (1-Pa) * log (1-Pa) );
  25. }
  26. return ent;
  27. }
  28. };
  29. class Cell : public Object {
  30. protected:
  31. int dimension;
  32. int numberClasses;
  33. bool terminal;
  34. Cell *first;
  35. Cell *second;
  36. float threshold;
  37. int splitDimension;
  38. int cellID;
  39. public:
  40. Cell(int _dimension, int _numberClasses)
  41. : dimension(_dimension)
  42. , numberClasses (_numberClasses)
  43. , terminal(true)
  44. , first(NULL)
  45. , second(NULL)
  46. , cellID (-1)
  47. {}
  48. Cell(){}
  49. Cell (const Cell &) {cerr << "don't call the Cell copy constructor\n"; exit(1);}
  50. ~Cell()
  51. {
  52. if (!terminal)
  53. {
  54. delete first;
  55. delete second;
  56. }
  57. }
  58. void recursiveSplit (const vector<pair<int, float *> > &data, int level = 2);
  59. void split(const vector<pair<int, float *> > &data, int &bestDim, float &bestThreshold);
  60. void findThreshold(const vector<pair<int, float *> > &data, int dim, float &thresh, float &score);
  61. int setNumbering(int start=0);
  62. int belongs(float *vect) const;
  63. void calcTemplate (const vector<float *> &features, vector<int> &templ) const;
  64. void printOn(ostream &out) const;
  65. void readFrom (istream &in);
  66. friend istream &operator >> (istream &in, Cell &cell);
  67. };