util.h

00001 #ifndef Util_H
00002 #define Util_H
00003 
00004 #include "defs.h"
00005 #include <math.h>
00006 #include <stdexcept>
00007 #include <vector>
00008 #include <ctime>
00009 
00010 using std::vector;
00011 using std::string;
00012 
00013 typedef unsigned char _uint8;
00014 
00015 
00016 void printClusters(int* clusters, int dimL, int dimC){
00017 
00018    for( int i = 1; i <= dimL; i++) {
00019      for (int j = 1; j <= dimC; j++)
00020        printf("%i ",clusters[(j - 1)*dimL + i]);
00021      printf("\n");
00022    }
00023 }
00024 
00025 void printmatrix( TMatrix m, int li, int lf, int ci, int cf ) {
00026    int i, j;
00027    for (i=li; i<=lf; i++) { 
00028            printf("\n");
00029            for (j=ci; j<=cf; j++) printf(" %4.5f ", m[i][j]);
00030    };
00031    printf("\n");
00032 
00033 };
00034 
00035 
00036 Value_Type *create_vector( long nl, long nh )
00037 /* allocate a Value_Type vector with subscript range v[nl..nh] */
00038 {
00039         Value_Type *v;
00040         v = (Value_Type *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(Value_Type)));
00041         if (!v) printf( "allocation failure in vector()");
00042         return v-nl+NR_END;
00043 };
00044 
00045 
00046 int *create_intvector( long nl, long nh )
00047 /* allocate a Value_Type vector with subscript range v[nl..nh] */
00048 {
00049         int *v;
00050         v = (int *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(int)));
00051         if (!v) printf( "allocation failure in vector()");
00052         return v-nl+NR_END;
00053 };
00054 
00055 
00056 void free_vector( Value_Type *v, long nl, long nh )
00057 /* free a Value_Type vector allocated with vector() */
00058 {
00059         free((FREE_ARG)(v+nl-NR_END));
00060 };
00061 
00062 void free_intvector( int *v, long nl, long nh )
00063 /* free a Value_Type vector allocated with vector() */
00064 {
00065         free((FREE_ARG)(v+nl-NR_END));
00066 };
00067 
00068 
00069 Value_Type **create_matrix( long nrl, long nrh, long ncl, long nch ) {
00070         long i, nrow=nrh-nrl+1, ncol=nch-ncl+1;
00071         Value_Type **m;
00072 
00073         /* allocate pointers to rows */
00074         m = (Value_Type**) malloc((size_t) ((nrow+NR_END)*sizeof(Value_Type*)));
00075 
00076         if (!m) { exit(1); };
00077         m += NR_END;
00078         m -= nrl;
00079 
00080         /* allocate rows an set pointers to them */
00081         m[nrl] = (Value_Type*) malloc((size_t) ((nrow*ncol+NR_END)*sizeof(Value_Type)));
00082         if (!m[nrl]) { exit(1); };
00083 
00084         m[nrl] += NR_END;
00085         m[nrl] -= ncl;
00086     
00087         for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol;
00088 
00089         /* return ponter to array of pointers to rows */
00090 
00091 
00092         return m;
00093     
00094 };
00095 
00096 
00097 void free_matrix( Value_Type **m, long nrl, long nrh, long ncl, long nch )
00098 /* free a Value_Type matrix allocated by matrix() */
00099 {
00100         free((FREE_ARG) (m[nrl]+ncl-NR_END));
00101         free((FREE_ARG) (m+nrl-NR_END));
00102 };
00103 
00104 int strtoint( const string str ) 
00105 {       
00106         return atoi(str.c_str());
00107 }
00108 
00109 
00110 float strtofloat( const string str ) 
00111 {
00112         return atof( str.c_str() );
00113 }
00114 
00115 
00116 string inttostr( int i ) 
00117 {
00118         char buffer[5];
00119         sprintf(buffer,"%d",i);
00120         //itoa( i, buffer, 10 );
00121         return buffer;
00122 }
00123 
00124 string flttostr( Value_Type f ) 
00125 {
00126         string str;
00127         char *buffer;
00128         int precision = 5;
00129         int decimal, sign;
00130         
00131         buffer = fcvt (f, precision, &decimal, &sign);
00132 
00133         str = sign?'-':'+';
00134         str += buffer[0];
00135         str += '.';
00136         str += buffer+1;
00137         str += 'e';
00138         str += inttostr( decimal - 1 );
00139 
00140         return str;
00141 }
00142 
00143 
00144 bool isNumeric( const string str ) 
00145 {       
00146     string alpha( "_abcdefghikjlmnopqrstuvxwyzABCDEFGHKIJLMNOPQRSTUVWXZ" );
00147         if (strchr( alpha.c_str(), str[0]) == NULL) 
00148                 return true; 
00149         else 
00150                 return false;   
00151 }
00152 
00153 bool isValid( string str ) {
00154 _uint8 num;
00155 int i;
00156 
00157         if ( str == "#" )
00158                 return false;
00159 
00160         if ( str.empty() )
00161                 return false;
00162 
00163         for (i=0; i< str.length(); i++ )
00164                 if ( (num = str[i]) == 205 )
00165                         return false;
00166 
00167     return true;
00168 }
00169 
00170 string inttolattice( int i ) {
00171         switch(i) {
00172           case 1 : return HEXA_STR; break;
00173           case 2 : return RECT_STR; break;
00174           default: return UNKNOWN; 
00175         }
00176 }
00177 
00178 
00179 string inttoneigh( int i ) {
00180         switch(i) {
00181           case 1 : return GAUSSIAN_STR;  break;
00182           case 2 : return BUBBLE_STR;    break;
00183           case 3 : return CUTGAUSS_STR;  break;
00184           case 4 : return EP_STR;                  break;
00185           default: return UNKNOWN;
00186         }
00187 }
00188 
00189 Value_Type dist( Value_Type* vec1, Value_Type* vec2, int d ) {
00190         int i;
00191         Value_Type diff = 0.0;
00192         for (i=1; i<= d; i++)
00193                 diff += (vec1[i] - vec2[i])*(vec1[i] - vec2[i]);
00194         return sqrt( diff );
00195 };
00196 
00197 
00198 
00199 void vec_sort( int d[], int s[], int n ) {
00200 /* Given the eigenvalues d[1..n] and eigenvectors v[1..n][1..n] as output from
00201    jacobi, this routine sorts the eigenvalues into descendenting order, and 
00202    rearranges the columns of v correspondingly. */
00203 
00204         int k, j, i;
00205         int p, q;
00206 
00207         for (i=1; i<n; i++) {
00208                 p = d[k=i];
00209                 for (j=i+1; j<=n; j++ )
00210                         if ( d[j] >= p) p = d[k=j];
00211                         if (k != i ) {
00212                                 d[k] = d[i];
00213                                 d[i] = p;       
00214                             q = s[i];
00215                                 s[i] = s[k];
00216                                 s[k] = q;
00217                         };
00218         };
00219 };
00220 
00221 
00222 
00223 //included by marcio
00224 Value_Type manhattan(const Value_Type* v1, const Value_Type* v2, const int& dimension) {
00225   
00226   register Value_Type sum = 0;
00227 
00228   for(register int i  = 0 ; i < dimension ; i++) 
00229     sum += fabs(v1[i] - v2[i]); //warning, estava sendo usada a funcao abs, que retornava um int e n um float. perda de precisao
00230 
00231   return sum;
00232 
00233 };
00234 
00235 
00236 
00237 //included by marcio
00238 Value_Type euclideanDistance(const Value_Type* v1, const Value_Type* v2, const int& dimension) {
00239   
00240   register Value_Type sum = 0;
00241   register Value_Type diff;
00242 
00243 
00244   for(register int i  = 0 ; i < dimension ; i++) {
00245     diff = v1[i] - v2[i];
00246     sum += diff * diff;
00247   }
00248 
00249   return sqrt(sum);
00250 }
00251 
00252 
00253 
00254 //included by marcio
00255 void randomVector(Value_Type* vec, const int& dimension) {
00256 
00257   // Set initial seed
00258   srand( (unsigned)time( NULL ) );
00259 
00260   for(register int i = 0 ; i < dimension ; i++) 
00261     vec[i] = rand() / (RAND_MAX + 1.0);
00262 };
00263 
00264 
00265 
00266 //included by marcio
00267 void zeroVector(Value_Type* vec, const int& dimension) {
00268 
00269   for(register int i = 0 ; i < dimension ; i++) 
00270     vec[i] = 0.0;
00271 };
00272 
00273 
00274 
00275 //included by marcio
00276 void sumVectors(Value_Type* v1, const Value_Type* v2, const int& dimension) {
00277   
00278   for(register int i  = 0 ; i < dimension ; i++) 
00279     v1[i] += v2[i];
00280 
00281 };
00282 
00283 
00284 
00285 //included by marcio
00286 void subtractVectors(Value_Type* v1, const Value_Type* v2, const int& dimension) {
00287   
00288   for(register int i  = 0 ; i < dimension ; i++) 
00289     v1[i] -= v2[i];
00290 
00291 };
00292 
00293 
00294 
00295 //included by marcio
00296 void multiplyVector(Value_Type* vec, const Value_Type value, const int& dimension) {
00297   
00298   for(register int i  = 0 ; i < dimension ; i++) 
00299     vec[i] = value * vec[i];
00300 
00301 };
00302 
00303 
00304 
00305 //included by marcio
00306 void copyVectors(Value_Type* dest, const Value_Type* source, const int& dimension) {
00307   
00308   for(register int i  = 0 ; i < dimension ; i++) 
00309     dest[i] = source[i];
00310 
00311 };
00312 
00313 
00314 
00315 template<class T>
00316 void insertVector(vector<T>& dst, vector<T> src) {
00317   
00318   int size = src.size();
00319 
00320   for(register int i = 0 ; i < size ; i++)
00321     dst.push_back(src[i]);
00322 
00323 };
00324 
00327 string rem_id(string label)
00328 {
00329 string result = "" ;
00330 for (int i = 2 ; i<label.length(); i++ )
00331         result += label[i];
00332 return result ;
00333 }
00334 
00335 #endif
00336 

Generated on Tue Aug 7 16:03:33 2007 for SOMCode by  doxygen 1.5.3