00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #if !defined(__NumericLib_h)
00020 #define __NumericLib_h
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050 #include <cmath>
00051 #include <cstdio>
00052 #include <vector>
00053 #include <limits>
00054 #include "common_header.h"
00055 #include "realvec.h"
00056
00057 namespace Marsyas
00058 {
00069 class marsyas_EXPORT NumericLib
00070 {
00071 private:
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 unsigned char null(mrs_complex *p,mrs_complex *pred,mrs_natural *n,mrs_complex *root,
00112 mrs_real *maxerr,unsigned char flag);
00113
00114 unsigned char poly_check(mrs_complex *pred,mrs_natural *nred,mrs_natural *n,mrs_complex *root);
00115 void quadratic(mrs_complex *pred,mrs_complex *root);
00116 unsigned char lin_or_quad(mrs_complex *pred,mrs_natural nred,mrs_complex *root);
00117 void monic(mrs_complex *p,mrs_natural *n);
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154 mrs_complex muller(mrs_complex *pred,mrs_natural nred);
00155
00156 mrs_complex x0MULLER_,x1MULLER_,x2MULLER_,
00157 f0MULLER_,f1MULLER_,f2MULLER_,
00158 h1MULLER_,h2MULLER_,
00159 q2MULLER_;
00160 mrs_natural iterMULLER_;
00161
00162 void initialize(mrs_complex *pred,mrs_complex *xb,mrs_real *epsilon);
00163 void root_of_parabola(void);
00164 void iteration_equation(mrs_real *h2abs);
00165 void suppress_overflow(mrs_natural nred);
00166 void too_big_functionvalues(mrs_real *f2absq);
00167 void convergence_check(mrs_natural *overflow,mrs_real f1absq,mrs_real f2absq,
00168 mrs_real epsilon);
00169 void compute_function(mrs_complex *pred,mrs_natural nred,mrs_real f1absq,
00170 mrs_real *f2absq,mrs_real epsilon);
00171 void check_x_value(mrs_complex *xb,mrs_real *f2absqb,mrs_natural *rootd,
00172 mrs_real f1absq,mrs_real f2absq,mrs_real epsilon,
00173 mrs_natural *noise);
00174 void root_check(mrs_complex *pred,mrs_natural nred,mrs_real f2absqb,mrs_natural *seconditer,
00175 mrs_natural *rootd,mrs_natural *noise,mrs_complex xb);
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212 mrs_complex newton(mrs_complex *p,mrs_natural n,mrs_complex ns,mrs_real *dxabs,
00213 unsigned char flag);
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247 void hornc(mrs_complex *pred,mrs_natural nred,mrs_complex x0,unsigned char flag);
00248 void horncd(mrs_complex *pred,mrs_natural nred,mrs_real a,mrs_real b);
00249 mrs_natural poldef(mrs_complex *pred,mrs_natural nred,mrs_complex *root,unsigned char flag);
00250 void fdvalue(mrs_complex *p,mrs_natural n,mrs_complex *f,mrs_complex *df,mrs_complex x0,
00251 unsigned char flag) ;
00252
00253 static mrs_real add(mrs_real *a, mrs_real *b);
00254 static mrs_real pow_di(mrs_real *ap, mrs_natural *bp);
00255
00257
00259 static mrs_real mxGetInf();
00260 static bool mxIsInf(mrs_real s);
00261 static void mxFree( void * s );
00262 static void mexErrMsgTxt(const char * s);
00263 static void assignmentoptimal(mrs_natural *assignment, mrs_real *cost, mrs_real *distMatrix, mrs_natural nOfRows, mrs_natural nOfColumns);
00264 static void buildassignmentvector(mrs_natural *assignment, bool *starMatrix, mrs_natural nOfRows, mrs_natural nOfColumns);
00265 static void computeassignmentcost(mrs_natural *assignment, mrs_real *cost, mrs_real *distMatrix, mrs_natural nOfRows, mrs_natural nOfColumns);
00266 static void step2a(mrs_natural *assignment, mrs_real *distMatrix, bool *starMatrix, bool *newStarMatrix, bool *primeMatrix, bool *coveredColumns, bool *coveredRows, mrs_natural nOfRows, mrs_natural nOfColumns, mrs_natural minDim);
00267 static void step2b(mrs_natural *assignment, mrs_real *distMatrix, bool *starMatrix, bool *newStarMatrix, bool *primeMatrix, bool *coveredColumns, bool *coveredRows, mrs_natural nOfRows, mrs_natural nOfColumns, mrs_natural minDim);
00268 static void step3 (mrs_natural *assignment, mrs_real *distMatrix, bool *starMatrix, bool *newStarMatrix, bool *primeMatrix, bool *coveredColumns, bool *coveredRows, mrs_natural nOfRows, mrs_natural nOfColumns, mrs_natural minDim);
00269 static void step4 (mrs_natural *assignment, mrs_real *distMatrix, bool *starMatrix, bool *newStarMatrix, bool *primeMatrix, bool *coveredColumns, bool *coveredRows, mrs_natural nOfRows, mrs_natural nOfColumns, mrs_natural minDim, mrs_natural row, mrs_natural col);
00270 static void step5 (mrs_natural *assignment, mrs_real *distMatrix, bool *starMatrix, bool *newStarMatrix, bool *primeMatrix, bool *coveredColumns, bool *coveredRows, mrs_natural nOfRows, mrs_natural nOfColumns, mrs_natural minDim);
00271
00272 public:
00273
00274 NumericLib();
00275 ~NumericLib();
00276
00277 bool polyRoots(std::vector<mrs_complex> coefs, bool complexCoefs, mrs_natural order, std::vector<mrs_complex> &roots);
00278 mrs_real determinant(realvec matrix);
00279
00280 static mrs_real gaussian(mrs_real x, mrs_real var, mrs_real mean)
00281 {
00282 return exp(-(x-mean)*(x-mean)/(2*var*var))/(var*sqrt(2*PI));
00283 }
00284
00286
00288 static mrs_real euclideanDistance(const realvec& Vi, const realvec& Vj, const realvec& covMatrix);
00289 static mrs_real mahalanobisDistance(const realvec& Vi, const realvec& Vj, const realvec& covMatrix);
00290 static mrs_real cosineDistance(const realvec& Vi, const realvec& Vj, const realvec& dummy = realvec());
00291 static mrs_real cityblockDistance(const realvec& Vi, const realvec& Vj, const realvec& dummy = realvec());
00292 static mrs_real correlationDistance(const realvec& Vi, const realvec& Vj, const realvec& dummy = realvec());
00293 static mrs_real divergenceShape(const realvec& Ci, const realvec& Cj, const realvec& dummy = realvec());
00294 static mrs_real bhattacharyyaShape(const realvec& Ci, const realvec& Cj, const realvec& dummy = realvec());
00296
00297
00298
00299
00300
00301 static void svd(mrs_natural m, mrs_natural n, realvec &A, realvec &U, realvec &V, realvec &s);
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315 static void tred2(realvec &a, mrs_natural n, realvec &d, realvec &e);
00316
00317
00318
00319
00320
00321
00322
00323
00324 static void tqli(realvec &d, realvec &e, mrs_natural n, realvec &z);
00325
00326
00327
00328
00329
00330
00331
00332 static mrs_real machp(const char *cmach);
00333
00335
00337 static mrs_real hungarianAssignment(realvec& matrixdist, realvec& matrixAssign);
00338
00339 };
00340
00341 }
00342
00343 #endif //__NumericLib_h
00344
00345