Hugintrunk  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Utilities.cpp
Go to the documentation of this file.
1 /* Import from Gabor API
2 
3 Copyright (c) 2002-3 Adriaan Tijsseling
4 
5 
6  All Rights Reserved
7 
8  This program is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 2 of the License, or
11  (at your option) any later version.
12 
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21 
22 /*
23  Author: Adriaan Tijsseling (AGT)
24  Copyright: (c) Copyright 2002 Adriaan Tijsseling. All rights reserved.
25  Description: Generic utilities
26 */
27 
28 #include <math.h>
29 #include <stdlib.h>
30 #include "Utilities.h"
31 
32 namespace celeste
33 {
34 std::streamsize gPrecision;
35 std::streamsize gWidth;
36 
37 // for Permute
38 struct tmp
39 {
40  int p; /* permutation */
41  int r; /* random number */
42 };
43 
44 
45 // Permutes an array
46 void Permute( int* array, size_t size )
47 {
48  struct tmp *t;
49  size_t i;
50 
51  t = new tmp[size];
52  for( i = 0; i < size; i++ ) // load up struct with data
53  {
54  t[i].r = rand();
55  t[i].p = array[i];
56  }
57  qsort( t, size, sizeof(struct tmp), cmp ); // shuffle
58 
59  // data back to original array
60  for( i = 0; i < size; i++ ) array[i] = t[i].p;
61 
62  delete[] t;
63 }
64 
65 // use for permuted qsort
66 int cmp( const void *s1, const void *s2 )
67 {
68  struct tmp *a1 = (struct tmp *)s1;
69  struct tmp *a2 = (struct tmp *)s2;
70 
71  return((a1->r) - (a2->r));
72 }
73 
74 
75 float Heavyside( float a )
76 {
77  // if a is larger than 0.5, return 1.0, else return 0.0
78  return (float)( ( a > 0.5 ) ? 1.0 : 0.0 );
79 }
80 
81 
82 float Sigmoid( float act )
83 {
84  return (float)( 1.0 / ( 1.0 + exp( -1.0 * act ) ) );
85 }
86 
87 
88 // beta must be negative
89 // untested
90 float Sigmoid( float beta, float a_pot )
91 {
92  return (float)( 1.0 / ( 1.0 + exp( beta * a_pot ) ) );
93 }
94 
95 float Sigmoid( float beta, float a_pot, float thresh )
96 {
97  return (float)( 1.0 / ( 1.0 + exp( beta * a_pot + thresh ) ) );
98 }
99 
100 // Create a matrix and fill it with constant given in parameter
101 int **CreateMatrix( int val, int row, int col )
102 {
103  int **matrix = new int*[row];
104 
105  for ( int i = 0; i < row; i++ )
106  {
107  matrix[i] = new int[col];
108  for ( int j = 0; j < col; j++ ) matrix[i][j] = val;
109  }
110  return matrix;
111 }
112 
113 // Reset a matrix with new value val
114 void ResetMatrix( int ** matrix, int val, int row, int col )
115 {
116  for ( int i = 0; i < row; i++ )
117  for ( int j = 0; j < col; j++ )
118  matrix[i][j] = val;
119 }
120 
121 // Dispose allocated matrix
122 void DisposeMatrix( int** matrix, int row )
123 {
124  for ( int i = 0; i < row; i++ ) delete[] matrix[i];
125  delete[] matrix;
126 }
127 
128 
129 // Create a matrix and fill it with constant given in parameter
130 float **CreateMatrix( float val, int row, int col )
131 {
132  float **matrix = new float*[row];
133 
134  for ( int i = 0; i < row; i++ )
135  {
136  matrix[i] = new float[col];
137  for ( int j = 0; j < col; j++ ) matrix[i][j] = val;
138  }
139  return matrix;
140 }
141 
142 // Reset a matrix with new value val
143 void ResetMatrix( float ** matrix, float val, int row, int col )
144 {
145  for ( int i = 0; i < row; i++ )
146  for ( int j = 0; j < col; j++ )
147  matrix[i][j] = val;
148 }
149 
150 // Dispose allocated matrix
151 void DisposeMatrix( float** matrix, int row )
152 {
153  for ( int i = 0; i < row; i++ ) delete[] matrix[i];
154  delete[] matrix;
155 }
156 
157 
158 // Returns Euclidean distance between two vectors
159 float ReturnDistance( float *pat1, float *pat2, int size )
160 {
161  float dist = 0.0;
162 
163  for ( int i = 0; i < size; i++ )
164  dist += ( pat1[i] - pat2[i] ) * ( pat1[i] - pat2[i] );
165 
166  return (float)( sqrt( dist ) / sqrt( (float)size ) );
167 }
168 
169 // For file reading purposes. Skips blanks and lines starting with #
170 void SkipComments( std::ifstream* infile )
171 {
172  bool garbage = true;
173  char c;
174 
175  while ( garbage )
176  {
177  // ignore any line feeds left in the stream
178  while ( infile->peek() == '\n' || infile->peek() == ' ' || infile->peek() == '\t' )
179  infile->get();
180  while ( infile->peek() == '#' )infile->ignore( 1000, '\n' );
181  infile->get(c);
182  if ( c == '\n' || c == '\t' || c == ' ' || c == '#' )
183  garbage = true;
184  else
185  garbage = false;
186  infile->putback(c);
187  }
188 }
189 
190 
191 void FileCreateError( char* filename )
192 {
193  char folder[FILENAME_MAX];
194 
195  getcwd( folder, FILENAME_MAX );
196  std::cerr << "Error: Could not create file " << filename << " in directory ";
197  std::cerr << folder << std::endl;
198 }
199 
200 void FileOpenError( char* filename )
201 {
202  char folder[FILENAME_MAX];
203 
204  getcwd( folder, FILENAME_MAX );
205  std::cerr << "Error: Could not open file " << filename << " in directory ";
206  std::cerr << folder << std::endl;
207 }
208 
209 
210 // std::cout, std::cerr and std::ostream formatting utilities
211 
212 void GetStreamDefaults( void )
213 {
214  gWidth = std::cout.width();
215  gPrecision = std::cout.precision();
216 }
217 
218 void AdjustStream( std::ostream &os, int precision, int width, int pos, bool trailers )
219 {
220  os.precision( precision );
221  os.width( width );
222  os.fill( ' ' );
223  if ( trailers )
224  os.setf(std::ios::showpoint, std::ios::showpoint);
225  else
226  os.unsetf(std::ios::showpoint);
227  if ( pos == kLeft )
228  os.setf(std::ios::left, std::ios::adjustfield);
229  else
230  os.setf(std::ios::right, std::ios::adjustfield);
231 }
232 
233 void SetStreamDefaults( std::ostream &os )
234 {
235  os.precision( gPrecision );
236  os.width( gWidth );
237  os.unsetf(std::ios::showpoint);
238  os.setf(std::ios::left, std::ios::adjustfield);
239 }
240 
241 
242 // return absolute value
243 
244 double SafeAbs( double val1, double val2 )
245 {
246  double diff = val1 - val2;
247 
248  if ( diff < 0.0 ) return ( 0.0 - diff );
249  else return diff;
250 }
251 
252 float SafeAbs( float val1, float val2 )
253 {
254  float diff = val1 - val2;
255 
256  if ( diff < 0.0 ) return (float)( 0.0 - diff );
257  else return diff;
258 }
259 
260 int SafeAbs( int val1, int val2 )
261 {
262  int diff = val1 - val2;
263 
264  if ( diff < 0 ) return ( 0 - diff );
265  else return diff;
266 }
267 
268 double SafeAbs( double val )
269 {
270  if ( val < 0.0 ) return ( 0.0 - val );
271  else return val;
272 }
273 
274 float SafeAbs( float val )
275 {
276  if ( val < 0.0 ) return (float)( 0.0 - val );
277  else return val;
278 }
279 
280 int SafeAbs( int val )
281 {
282  if ( val < 0 ) return ( 0 - val );
283  else return val;
284 }
285 
286 }; // namespace
int cmp(const void *s1, const void *s2)
Definition: Utilities.cpp:66
void SkipComments(std::ifstream *infile)
Definition: Utilities.cpp:170
void GetStreamDefaults(void)
Definition: Utilities.cpp:212
void AdjustStream(std::ostream &os, int precision, int width, int pos, bool trailers)
Definition: Utilities.cpp:218
void Permute(int *array, size_t size)
Definition: Utilities.cpp:46
void SetStreamDefaults(std::ostream &os)
Definition: Utilities.cpp:233
void DisposeMatrix(int **matrix, int row)
Definition: Utilities.cpp:122
float ReturnDistance(float *pat1, float *pat2, int size)
Definition: Utilities.cpp:159
void FileOpenError(char *filename)
Definition: Utilities.cpp:200
int ** CreateMatrix(int val, int row, int col)
Definition: Utilities.cpp:101
std::streamsize gWidth
Definition: Utilities.cpp:35
std::streamsize gPrecision
Definition: Utilities.cpp:34
float Heavyside(float a)
Definition: Utilities.cpp:75
double SafeAbs(double val1, double val2)
Definition: Utilities.cpp:244
float Sigmoid(float act)
Definition: Utilities.cpp:82
void FileCreateError(char *filename)
Definition: Utilities.cpp:191
void ResetMatrix(int **matrix, int val, int row, int col)
Definition: Utilities.cpp:114