00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #ifndef C_UTILITY_H
00033 #define C_UTILITY_H
00034
00035 #include <map>
00036 #include <assert.h>
00037
00038 #include <newmat/newmat.h>
00039
00040
00041
00042
00043
00044 int my_round(double value);
00045
00046 double my_exp(double value);
00047
00048 void getPseudoInverse(Matrix *J, Matrix *pinv, double lambda);
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00131
00137 template <typename T1> class CMyArray
00138 {
00139 protected:
00140 T1 *data;
00141
00142 int *dim;
00143 int numDim;
00144
00145 int size;
00146
00147 CMyArray() {};
00148
00149 void initialize(int numDim, int dim[])
00150 {
00151 this->numDim = numDim;
00152 this->dim = new int[numDim];
00153
00154 memcpy(this->dim, dim, numDim * sizeof(int));
00155
00156 size = 1;
00157
00158 for (int i = 0; i < numDim; i++)
00159 {
00160 size = size * dim[i];
00161 }
00162
00163 data = new T1 [size];
00164 }
00165
00166 public:
00167 CMyArray(int numDim, int dim[])
00168 {
00169 CMyArray<T1>::initialize(numDim, dim);
00170 }
00171
00172 ~CMyArray()
00173 {
00174 delete dim;
00175 delete data;
00176 }
00177
00178 T1 get(int indices[])
00179 {
00180 int index = 0, size = 1;
00181
00182 for (int i = 0; i < numDim; i++)
00183 {
00184 assert(indices[i] < dim[i] && indices[i] >=0);
00185
00186 index += indices[i] * size;
00187 size = size * dim[i];
00188 }
00189 return data[index];
00190 }
00191
00192 void set(int indices[], T1 d)
00193 {
00194 int index = 0, size = 1;
00195
00196 for (int i = 0; i < numDim; i++)
00197 {
00198 assert(indices[i] < dim[i] && indices[i] >=0);
00199
00200 index += indices[i] * size;
00201 size = size * dim[i];
00202 }
00203 data[index] = d;
00204
00205 }
00206
00207 void init(T1 initVal)
00208 {
00209 for (int i = 0; i < size; i++)
00210 {
00211 data[i] = initVal;
00212 }
00213 }
00214
00215 int getSize()
00216 {
00217 return size;
00218 }
00219
00220 void set1D(int index1d, T1 d)
00221 {
00222 data[index1d] = d;
00223 }
00224
00225 T1 get1D(int index1d)
00226 {
00227 return data[index1d];
00228 }
00229 };
00230
00232 class CDistributions
00233 {
00234 public:
00236
00239 static void getGibbsDistribution(double beta, double *values, unsigned int numValues);
00240
00241 static void getS1L0Distribution(double *values, unsigned int numValues);
00242
00243 static double getNormalDistributionSample(double mu, double sigma);
00244
00245 static int getSampledIndex(double *distribution, int numValues);
00246 };
00247
00248
00249
00251 template<typename T1> class CMyArray2D : public CMyArray<T1>
00252 {
00253 public:
00254 CMyArray2D(int xDim, int yDim)
00255 {
00256 int *l_dim = new int[2];
00257 l_dim[0] = xDim;
00258 l_dim[1] = yDim;
00259
00260 CMyArray<T1>::initialize(2, l_dim);
00261
00262 delete l_dim;
00263 }
00264
00265 ~CMyArray2D()
00266 {
00267 }
00268
00269 T1 get(int xIndex, int yIndex)
00270 {
00271 int indices[2];
00272 indices[0] = xIndex;
00273 indices[1] = yIndex;
00274
00275 return CMyArray<T1>::get(indices);
00276 }
00277
00278 void set(int xIndex, int yIndex, T1 d)
00279 {
00280 int indices[2];
00281 indices[0] = xIndex;
00282 indices[1] = yIndex;
00283
00284 CMyArray<T1>::set(indices, d);
00285 }
00286 };
00287
00289 template<typename T1> class CMyArray3D : public CMyArray<T1>
00290 {
00291 public:
00292 CMyArray3D(int xDim, int yDim, int zDim) : CMyArray<T1>()
00293 {
00294 int *ldim = new int[3];
00295 ldim[0] = xDim;
00296 ldim[1] = yDim;
00297 ldim[2] = zDim;
00298
00299 CMyArray<T1>::initialize(3, ldim);
00300 }
00301
00302 ~CMyArray3D()
00303 {
00304 }
00305
00306 T1 get(int xIndex, int yIndex, int zIndex)
00307 {
00308 int indices[3];
00309 indices[0] = xIndex;
00310 indices[1] = yIndex;
00311 indices[2] = zIndex;
00312
00313 return CMyArray<T1>::get(indices);
00314 }
00315
00316 void set(int xIndex, int yIndex, int zIndex, T1 d)
00317 {
00318 int indices[3];
00319 indices[0] = xIndex;
00320 indices[1] = yIndex;
00321 indices[2] = zIndex;
00322
00323 CMyArray<T1>::set(indices, d);
00324 }
00325 };
00326
00327
00328 class CFeatureMap : public std::map<int, double>
00329 {
00330 protected:
00331 double stdValue;
00332
00333 public:
00334 CFeatureMap(double stdValue = 0.0);
00335
00336 double getValue(unsigned int featureIndex);
00337 };
00338
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392 #endif