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 namespace celeste
00033 {
00034 std::streamsize gPrecision;
00035 std::streamsize gWidth;
00036 
00037 // for Permute
00038 struct  tmp
00039 {
00040         int p;          /* permutation          */
00041         int r;          /* random number        */
00042 };
00043 
00044 
00045 // Permutes an array
00046 void Permute( int* array, size_t size )
00047 {
00048         struct tmp      *t;
00049         size_t          i;
00050         
00051         t = new tmp[size];
00052         for( i = 0; i < size; i++ )     // load up struct with data
00053         {
00054                 t[i].r = rand();
00055                 t[i].p = array[i];
00056         }       
00057         qsort( t, size, sizeof(struct tmp), cmp );      // shuffle
00058         
00059         // data back to original array
00060         for( i = 0; i < size; i++ ) array[i] = t[i].p;
00061         
00062         delete[] t;
00063 }
00064 
00065 // use for permuted qsort
00066 int cmp( const void *s1, const void *s2 )
00067 {
00068         struct tmp *a1 = (struct tmp *)s1;
00069         struct tmp *a2 = (struct tmp *)s2;
00070         
00071         return((a1->r) - (a2->r));
00072 }
00073 
00074 
00075 float Heavyside( float a ) 
00076 {
00077         // if a is larger than 0.5, return 1.0, else return 0.0
00078         return (float)( ( a > 0.5 ) ? 1.0 : 0.0 );
00079 }
00080 
00081 
00082 float Sigmoid( float act )
00083 {
00084         return (float)( 1.0 / ( 1.0 + exp( -1.0 * act ) ) );
00085 }
00086 
00087 
00088 // beta must be negative
00089 // untested
00090 float Sigmoid( float beta, float a_pot ) 
00091 {
00092         return (float)( 1.0 / ( 1.0 + exp( beta * a_pot ) ) );
00093 }
00094 
00095 float Sigmoid( float beta, float a_pot, float thresh ) 
00096 {
00097         return (float)( 1.0 / ( 1.0 + exp( beta * a_pot + thresh ) ) );
00098 }
00099 
00100 // Create a matrix and fill it with constant given in parameter
00101 int **CreateMatrix( int val, int row, int col ) 
00102 {
00103         int **matrix = new int*[row];
00104         
00105         for ( int i = 0; i < row; i++ ) 
00106         {
00107                 matrix[i] = new int[col];               
00108                 for ( int j = 0; j < col; j++ ) matrix[i][j] = val;
00109         }
00110         return matrix;
00111 }
00112 
00113 // Reset a matrix with new value val
00114 void ResetMatrix( int ** matrix, int val, int row, int col ) 
00115 {
00116         for ( int i = 0; i < row; i++ )
00117                 for ( int j = 0; j < col; j++ )
00118                         matrix[i][j] = val;
00119 }
00120 
00121 // Dispose allocated matrix
00122 void DisposeMatrix( int** matrix, int row )
00123 {
00124         for ( int i = 0; i < row; i++ ) delete[] matrix[i];
00125         delete[] matrix;
00126 }
00127 
00128 
00129 // Create a matrix and fill it with constant given in parameter
00130 float **CreateMatrix( float val, int row, int col ) 
00131 {
00132         float **matrix = new float*[row];
00133         
00134         for ( int i = 0; i < row; i++ ) 
00135         {
00136                 matrix[i] = new float[col];             
00137                 for ( int j = 0; j < col; j++ ) matrix[i][j] = val;
00138         }
00139         return matrix;
00140 }
00141 
00142 // Reset a matrix with new value val
00143 void ResetMatrix( float ** matrix, float val, int row, int col ) 
00144 {
00145         for ( int i = 0; i < row; i++ )
00146                 for ( int j = 0; j < col; j++ )
00147                         matrix[i][j] = val;
00148 }
00149 
00150 // Dispose allocated matrix
00151 void DisposeMatrix( float** matrix, int row )
00152 {
00153         for ( int i = 0; i < row; i++ ) delete[] matrix[i];
00154         delete[] matrix;
00155 }
00156 
00157 
00158 // Returns Euclidean distance between two vectors
00159 float ReturnDistance( float *pat1, float *pat2, int size ) 
00160 {
00161         float dist = 0.0;
00162         
00163         for ( int i = 0; i < size; i++ )
00164                 dist += ( pat1[i] - pat2[i] ) * ( pat1[i] - pat2[i] );
00165         
00166         return (float)( sqrt( dist ) / sqrt( (float)size ) );
00167 }
00168 
00169 // For file reading purposes. Skips blanks and lines starting with #
00170 void SkipComments( std::ifstream* infile )
00171 {
00172         bool garbage = true;
00173         char c;
00174         
00175         while ( garbage )
00176         {
00177                 // ignore any line feeds left in the stream
00178                 while ( infile->peek() == '\n' || infile->peek() == ' ' || infile->peek() == '\t' ) 
00179                         infile->get();  
00180                 while ( infile->peek() == '#' )infile->ignore( 1000, '\n' );
00181                 infile->get(c);
00182                 if ( c == '\n' || c == '\t' || c == ' ' || c == '#' )
00183                         garbage = true;
00184                 else
00185                         garbage = false;
00186                 infile->putback(c);
00187         }
00188 }
00189 
00190 
00191 void FileCreateError( char* filename )
00192 {
00193         char folder[FILENAME_MAX];
00194         
00195         getcwd( folder, FILENAME_MAX );         
00196         std::cerr << "Error: Could not create file " << filename << " in directory ";
00197         std::cerr << folder << std::endl;
00198 }
00199 
00200 void FileOpenError( char* filename )
00201 {
00202         char folder[FILENAME_MAX];
00203         
00204         getcwd( folder, FILENAME_MAX );         
00205         std::cerr << "Error: Could not open file " << filename << " in directory ";
00206         std::cerr << folder << std::endl;
00207 }
00208 
00209 
00210 // std::cout, std::cerr and std::ostream formatting utilities
00211 
00212 void GetStreamDefaults( void )
00213 {
00214         gWidth = std::cout.width();
00215         gPrecision = std::cout.precision();
00216 }
00217 
00218 void AdjustStream( std::ostream &os, int precision, int width, int pos, bool trailers )
00219 {
00220         os.precision( precision );
00221         os.width( width );
00222         os.fill( ' ' );
00223         if ( trailers )
00224         os.setf(std::ios::showpoint, std::ios::showpoint);
00225         else
00226         os.unsetf(std::ios::showpoint);
00227         if ( pos == kLeft )
00228         os.setf(std::ios::left, std::ios::adjustfield);
00229         else
00230         os.setf(std::ios::right, std::ios::adjustfield);
00231 }
00232 
00233 void SetStreamDefaults( std::ostream &os )
00234 {
00235         os.precision( gPrecision );
00236         os.width( gWidth );
00237     os.unsetf(std::ios::showpoint);
00238     os.setf(std::ios::left, std::ios::adjustfield);
00239 }
00240 
00241 
00242 // return absolute value
00243 
00244 double SafeAbs( double val1, double val2 )
00245 {
00246         double diff = val1 - val2;
00247         
00248         if ( diff < 0.0 ) return ( 0.0 - diff );
00249         else return diff;
00250 }
00251 
00252 float SafeAbs( float val1, float val2 )
00253 {
00254         float diff = val1 - val2;
00255         
00256         if ( diff < 0.0 ) return (float)( 0.0 - diff );
00257         else return diff;
00258 }
00259 
00260 int SafeAbs( int val1, int val2 )
00261 {
00262         int diff = val1 - val2;
00263         
00264         if ( diff < 0 ) return ( 0 - diff );
00265         else return diff;
00266 }
00267 
00268 double SafeAbs( double val )
00269 {
00270         if ( val < 0.0 ) return ( 0.0 - val );
00271         else return val;
00272 }
00273 
00274 float SafeAbs( float val )
00275 {
00276         if ( val < 0.0 ) return (float)( 0.0 - val );
00277         else return val;
00278 }
00279 
00280 int SafeAbs( int val )
00281 {
00282         if ( val < 0 ) return ( 0 - val );
00283         else return val;
00284 }
00285 
00286 }; // namespace

Generated on 25 Aug 2016 for Hugintrunk by  doxygen 1.4.7