COOPY » Guide  version 0.6.5
/home/paulfitz/cvs/coopy_scm/coopy/src/libcoopy_core/SparseSheet.cpp
Go to the documentation of this file.
00001 #include <coopy/SparseSheet.h>
00002 
00003 #include <math.h>
00004 
00005 using namespace std;
00006 using namespace coopy::store;
00007 
00008 Stat SparseFloatSheet::normalize(int first, int last, float sc, bool modify) {
00009   Stat s;
00010   //int hh = height();
00011   int ww = width();
00012   if (first==-1) first = 0;
00013   if (last==-1) last = ww-1;
00014   float tot = 0;
00015   float tot2 = 0;
00016   int ct = 0;
00017   for (efficient_map<long long,float>::iterator it=data.begin(); it!=data.end(); it++) {
00018     long long col = it->first % ww;
00019     if (col>=first&&col<=last) {
00020       float r = it->second;
00021       tot += r;
00022       tot2 += r*r;
00023       ct++;
00024     }
00025   }
00026   if (ct==0) return s;
00027   float mean = tot;
00028   float dev = 1;
00029   mean /= ct;
00030   dev = tot2 / ct - mean*mean;
00031   if (dev<0) { 
00032     dev = 0; 
00033   } else {
00034     dev = sqrt(dev);
00035   }
00036   //printf("mean %g, dev %g\n", mean, dev);
00037   if (dev<sc) dev = sc;
00038   if (modify) {
00039     for (efficient_map<long long,float>::iterator it=data.begin(); it!=data.end(); it++) {
00040       long long col = it->first % ww;
00041       if (col>=first&&col<=last) {
00042         float r = it->second;
00043         r = (r-mean)/dev;
00044         it->second = r;
00045       }
00046     }
00047   }
00048   s.mean = mean;
00049   s.stddev = dev;
00050   s.valid = true;
00051   return s;
00052 }
00053 
00054 
00055 void SparseFloatSheet::findBest(IntSheet& bestIndex, FloatSheet& bestValue, FloatSheet& bestInc) {
00056   bestIndex.resize(1,h,-1);
00057   bestValue.resize(1,h,0);
00058   bestInc.resize(1,h,0);
00059   for (efficient_map<long long,float>::iterator it=data.begin(); it!=data.end(); it++) {
00060     long long x = it->first % w;
00061     long long y = it->first / w;
00062     if (x>=w || y>=h) {
00063       fprintf(stderr,"SparseSheet - out of range: %ld %ld : (%dx%d)\n", 
00064               (long)x, (long)y, w, h);
00065       exit(1);
00066     }
00067     float val = it->second;
00068     float& best = bestValue.cell(0,y);
00069     if (val>best) {
00070       bestIndex.cell(0,y) = x;
00071       bestInc.cell(0,y) = val - best;
00072       best = val;
00073     }
00074   }
00075 }
00076 
00077 
00078 
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines