Utilities.cpp

Go to the documentation of this file.
00001 /* Import from Gabor API
00002 
00003 Copyright (c) 2002-3 Adriaan Tijsseling
00004 
00005 
00006                              All Rights Reserved
00007 
00008     This program is free software; you can redistribute it and/or modify
00009     it under the terms of the GNU General Public License as published by
00010     the Free Software Foundation; either version 2 of the License, or
00011     (at your option) any later version.
00012 
00013     This program is distributed in the hope that it will be useful,
00014     but WITHOUT ANY WARRANTY; without even the implied warranty of
00015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016     GNU General Public License for more details.
00017 
00018     You should have received a copy of the GNU General Public License
00019     along with this program. If not, see <http://www.gnu.org/licenses/>.
00020 */
00021 
00022 /*
00023         Author:                 Adriaan Tijsseling (AGT)
00024         Copyright:              (c) Copyright 2002 Adriaan Tijsseling. All rights reserved.
00025         Description:    Generic utilities
00026 */
00027 
00028 #include <math.h>
00029 #include <stdlib.h>
00030 #include "Utilities.h"
00031 
00032 using namespace std; 
00033 
00034 namespace celeste
00035 {
00036 long    gPrecision;
00037 long    gWidth;
00038 
00039 // for Permute
00040 struct  tmp
00041 {
00042         int p;          /* permutation          */
00043         int r;          /* random number        */
00044 };
00045 
00046 
00047 // Permutes an array
00048 void Permute( int* array, size_t size )
00049 {
00050         struct tmp      *t;
00051         size_t          i;
00052         
00053         t = new tmp[size];
00054         for( i = 0; i < size; i++ )     // load up struct with data
00055         {
00056                 t[i].r = rand();
00057                 t[i].p = array[i];
00058         }       
00059         qsort( t, size, sizeof(struct tmp), cmp );      // shuffle
00060         
00061         // data back to original array
00062         for( i = 0; i < size; i++ ) array[i] = t[i].p;
00063         
00064         delete[] t;
00065 }
00066 
00067 // use for permuted qsort
00068 int cmp( const void *s1, const void *s2 )
00069 {
00070         struct tmp *a1 = (struct tmp *)s1;
00071         struct tmp *a2 = (struct tmp *)s2;
00072         
00073         return((a1->r) - (a2->r));
00074 }
00075 
00076 
00077 float Heavyside( float a ) 
00078 {
00079         // if a is larger than 0.5, return 1.0, else return 0.0
00080         return (float)( ( a > 0.5 ) ? 1.0 : 0.0 );
00081 }
00082 
00083 
00084 float Sigmoid( float act )
00085 {
00086         return (float)( 1.0 / ( 1.0 + exp( -1.0 * act ) ) );
00087 }
00088 
00089 
00090 // beta must be negative
00091 // untested
00092 float Sigmoid( float beta, float a_pot ) 
00093 {
00094         return (float)( 1.0 / ( 1.0 + exp( beta * a_pot ) ) );
00095 }
00096 
00097 float Sigmoid( float beta, float a_pot, float thresh ) 
00098 {
00099         return (float)( 1.0 / ( 1.0 + exp( beta * a_pot + thresh ) ) );
00100 }
00101 
00102 // Create a matrix and fill it with constant given in parameter
00103 int **CreateMatrix( int val, int row, int col ) 
00104 {
00105         int **matrix = new int*[row];
00106         
00107         for ( int i = 0; i < row; i++ ) 
00108         {
00109                 matrix[i] = new int[col];               
00110                 for ( int j = 0; j < col; j++ ) matrix[i][j] = val;
00111         }
00112         return matrix;
00113 }
00114 
00115 // Reset a matrix with new value val
00116 void ResetMatrix( int ** matrix, int val, int row, int col ) 
00117 {
00118         for ( int i = 0; i < row; i++ )
00119                 for ( int j = 0; j < col; j++ )
00120                         matrix[i][j] = val;
00121 }
00122 
00123 // Dispose allocated matrix
00124 void DisposeMatrix( int** matrix, int row )
00125 {
00126         for ( int i = 0; i < row; i++ ) delete[] matrix[i];
00127         delete[] matrix;
00128 }
00129 
00130 
00131 // Create a matrix and fill it with constant given in parameter
00132 float **CreateMatrix( float val, int row, int col ) 
00133 {
00134         float **matrix = new float*[row];
00135         
00136         for ( int i = 0; i < row; i++ ) 
00137         {
00138                 matrix[i] = new float[col];             
00139                 for ( int j = 0; j < col; j++ ) matrix[i][j] = val;
00140         }
00141         return matrix;
00142 }
00143 
00144 // Reset a matrix with new value val
00145 void ResetMatrix( float ** matrix, float val, int row, int col ) 
00146 {
00147         for ( int i = 0; i < row; i++ )
00148                 for ( int j = 0; j < col; j++ )
00149                         matrix[i][j] = val;
00150 }
00151 
00152 // Dispose allocated matrix
00153 void DisposeMatrix( float** matrix, int row )
00154 {
00155         for ( int i = 0; i < row; i++ ) delete[] matrix[i];
00156         delete[] matrix;
00157 }
00158 
00159 
00160 // Returns Euclidean distance between two vectors
00161 float ReturnDistance( float *pat1, float *pat2, int size ) 
00162 {
00163         float dist = 0.0;
00164         
00165         for ( int i = 0; i < size; i++ )
00166                 dist += ( pat1[i] - pat2[i] ) * ( pat1[i] - pat2[i] );
00167         
00168         return (float)( sqrt( dist ) / sqrt( (float)size ) );
00169 }
00170 
00171 // For file reading purposes. Skips blanks and lines starting with #
00172 void SkipComments( ifstream* infile )
00173 {
00174         bool garbage = true;
00175         char c;
00176         
00177         while ( garbage )
00178         {
00179                 // ignore any line feeds left in the stream
00180                 while ( infile->peek() == '\n' || infile->peek() == ' ' || infile->peek() == '\t' ) 
00181                         infile->get();  
00182                 while ( infile->peek() == '#' )infile->ignore( 1000, '\n' );
00183                 infile->get(c);
00184                 if ( c == '\n' || c == '\t' || c == ' ' || c == '#' )
00185                         garbage = true;
00186                 else
00187                         garbage = false;
00188                 infile->putback(c);
00189         }
00190 }
00191 
00192 
00193 void FileCreateError( char* filename )
00194 {
00195         char folder[FILENAME_MAX];
00196         
00197         getcwd( folder, FILENAME_MAX );         
00198         cerr << "Error: Could not create file " << filename << " in directory ";
00199         cerr << folder << endl;
00200 }
00201 
00202 void FileOpenError( char* filename )
00203 {
00204         char folder[FILENAME_MAX];
00205         
00206         getcwd( folder, FILENAME_MAX );         
00207         cerr << "Error: Could not open file " << filename << " in directory ";
00208         cerr << folder << endl;
00209 }
00210 
00211 
00212 // cout, cerr and ostream formatting utilities
00213 
00214 void GetStreamDefaults( void )
00215 {
00216         gWidth = cout.width();
00217         gPrecision = cout.precision();
00218 }
00219 
00220 void AdjustStream( ostream &os, int precision, int width, int pos, bool trailers )
00221 {
00222         os.precision( precision );
00223         os.width( width );
00224         os.fill( ' ' );
00225         if ( trailers )
00226                 os.setf( ios::showpoint,  ios::showpoint );
00227         else
00228                 os.unsetf( ios::showpoint );
00229         if ( pos == kLeft )
00230                 os.setf( ios::left, ios::adjustfield );
00231         else
00232                 os.setf( ios::right, ios::adjustfield );
00233 }
00234 
00235 void SetStreamDefaults( ostream &os )
00236 {
00237         os.precision( gPrecision );
00238         os.width( gWidth );
00239         os.unsetf( ios::showpoint );
00240         os.setf( ios::left, ios::adjustfield );
00241 }
00242 
00243 
00244 // return absolute value
00245 
00246 double SafeAbs( double val1, double val2 )
00247 {
00248         double diff = val1 - val2;
00249         
00250         if ( diff < 0.0 ) return ( 0.0 - diff );
00251         else return diff;
00252 }
00253 
00254 float SafeAbs( float val1, float val2 )
00255 {
00256         float diff = val1 - val2;
00257         
00258         if ( diff < 0.0 ) return (float)( 0.0 - diff );
00259         else return diff;
00260 }
00261 
00262 int SafeAbs( int val1, int val2 )
00263 {
00264         int diff = val1 - val2;
00265         
00266         if ( diff < 0 ) return ( 0 - diff );
00267         else return diff;
00268 }
00269 
00270 double SafeAbs( double val )
00271 {
00272         if ( val < 0.0 ) return ( 0.0 - val );
00273         else return val;
00274 }
00275 
00276 float SafeAbs( float val )
00277 {
00278         if ( val < 0.0 ) return (float)( 0.0 - val );
00279         else return val;
00280 }
00281 
00282 int SafeAbs( int val )
00283 {
00284         if ( val < 0 ) return ( 0 - val );
00285         else return val;
00286 }
00287 
00288 }; // namespace

Generated on Tue Sep 30 01:25:29 2014 for Hugintrunk by  doxygen 1.3.9.1