Main Page   Class Hierarchy   Compound List   File List   Compound Members  

util.h

00001 #ifndef Util_H
00002 #define Util_H
00003 
00004 #include "defs.h"
00005 #include <math.h>
00006 
00007 void printmatrix( TMatrix m, int li, int lf, int ci, int cf ) {
00008    int i, j;
00009    for (i=li; i<=lf; i++) { 
00010            printf("\n");
00011            for (j=ci; j<=cf; j++) printf(" %4.5f ", m[i][j]);
00012    };
00013    printf("\n");
00014 
00015 };
00016 
00017 
00018 Value_Type *create_vector( long nl, long nh )
00019 /* allocate a Value_Type vector with subscript range v[nl..nh] */
00020 {
00021         Value_Type *v;
00022         v = (Value_Type *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(Value_Type)));
00023         if (!v) printf( "allocation failure in vector()");
00024         return v-nl+NR_END;
00025 };
00026 
00027 
00028 int *create_intvector( long nl, long nh )
00029 /* allocate a Value_Type vector with subscript range v[nl..nh] */
00030 {
00031         int *v;
00032         v = (int *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(int)));
00033         if (!v) printf( "allocation failure in vector()");
00034         return v-nl+NR_END;
00035 };
00036 
00037 
00038 void free_vector( Value_Type *v, long nl, long nh )
00039 /* free a Value_Type vector allocated with vector() */
00040 {
00041         free((FREE_ARG)(v+nl-NR_END));
00042 };
00043 
00044 void free_intvector( int *v, long nl, long nh )
00045 /* free a Value_Type vector allocated with vector() */
00046 {
00047         free((FREE_ARG)(v+nl-NR_END));
00048 };
00049 
00050 
00051 Value_Type **create_matrix( long nrl, long nrh, long ncl, long nch ) {
00052         long i, nrow=nrh-nrl+1, ncol=nch-ncl+1;
00053         Value_Type **m;
00054 
00055         /* allocate pointers to rows */
00056         m = (Value_Type**) malloc((size_t) ((nrow+NR_END)*sizeof(Value_Type*)));
00057 
00058         if (!m) { exit(1); };
00059         m += NR_END;
00060         m -= nrl;
00061 
00062         /* allocate rows an set pointers to them */
00063         m[nrl] = (Value_Type*) malloc((size_t) ((nrow*ncol+NR_END)*sizeof(Value_Type)));
00064         if (!m[nrl]) { exit(1); };
00065 
00066         m[nrl] += NR_END;
00067         m[nrl] -= ncl;
00068     
00069         for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol;
00070 
00071         /* return ponter to array of pointers to rows */
00072 
00073 
00074         return m;
00075     
00076 };
00077 
00078 
00079 void free_matrix( Value_Type **m, long nrl, long nrh, long ncl, long nch )
00080 /* free a Value_Type matrix allocated by matrix() */
00081 {
00082         free((FREE_ARG) (m[nrl]+ncl-NR_END));
00083         free((FREE_ARG) (m+nrl-NR_END));
00084 };
00085 
00086 int strtoint( const string str ) 
00087 {       
00088         return atoi(str.c_str());
00089 }
00090 
00091 float strtofloat( const string str ) 
00092 {
00093         return atof( str.c_str() );
00094 }
00095 
00096 
00097 string inttostr( int i ) 
00098 {
00099         char buffer[5];
00100         itoa( i, buffer, 10 );
00101         return buffer;
00102 }
00103 
00104 string flttostr( Value_Type f ) 
00105 {
00106         string str;
00107         char *buffer;
00108         int precision = 5;
00109         int decimal, sign;
00110         
00111         buffer = fcvt (f, precision, &decimal, &sign);
00112 
00113         str = sign?'-':'+';
00114         str += buffer[0];
00115         str += '.';
00116         str += buffer+1;
00117         str += 'e';
00118         str += inttostr( decimal - 1 );
00119 
00120         return str;
00121 }
00122 
00123 
00124 bool isNumeric( const string str ) 
00125 {       
00126     string alpha( "_abcdefghikjlmnopqrstuvxwyzABCDEFGHKIJLMNOPQRSTUVWXZ" );
00127         if (strchr( alpha.c_str(), str[0]) == NULL) 
00128                 return true; 
00129         else 
00130                 return false;   
00131 }
00132 
00133 bool isValid( string str ) {
00134 unsigned _int8 num;
00135 int i;
00136 
00137         if ( str == "#" )
00138                 return false;
00139 
00140         if ( str.empty() )
00141                 return false;
00142 
00143         for (i=0; i< str.length(); i++ )
00144                 if ( (num = str[i]) == 205 )
00145                         return false;
00146 
00147     return true;
00148 }
00149 
00150 string inttolattice( int i ) {
00151         switch(i) {
00152         case 1 : return HEXA_STR; break;
00153         case 2 : return RECT_STR; break;
00154         default: return UNKNOWN; 
00155         }
00156 }
00157 
00158 
00159 string inttoneigh( int i ) {
00160         switch(i) {
00161         case 1 : return GAUSSIAN_STR;  break;
00162         case 2 : return BUBBLE_STR;    break;
00163         case 3 : return CUTGAUSS_STR;  break;
00164         case 4 : return EP_STR;            break;
00165         default: return UNKNOWN;
00166         }
00167 }
00168 
00169 Value_Type dist( Value_Type* vec1, Value_Type* vec2, int d ) {
00170         int i;
00171         Value_Type diff = 0.0;
00172         for (i=1; i<= d; i++)
00173                 diff += (vec1[i] - vec2[i])*(vec1[i] - vec2[i]);
00174         return sqrt( diff );
00175 };
00176 
00177 
00178 void vec_sort( int d[], int s[], int n )
00179 /* Given the eigenvalues d[1..n] and eigenvectors v[1..n][1..n] as output from
00180    jacobi, this routine sorts the eigenvalues into descendenting order, and 
00181    rearranges the columns of v correspondingly. */
00182 {
00183         int k, j, i;
00184         int p, q;
00185 
00186         for (i=1; i<n; i++) {
00187                 p = d[k=i];
00188                 for (j=i+1; j<=n; j++ )
00189                         if ( d[j] >= p) p = d[k=j];
00190                         if (k != i ) {
00191                                 d[k] = d[i];
00192                                 d[i] = p;       
00193                             q = s[i];
00194                                 s[i] = s[k];
00195                                 s[k] = q;
00196                         };
00197         };
00198 };
00199 
00200 #endif

Generated on Thu Apr 8 15:04:11 2004 for SOMCode by doxygen1.2.18