ltiFunctionGenerator.h
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
00033 #ifndef _LTI_FUNCTION_GENERATOR_H_
00034 #define _LTI_FUNCTION_GENERATOR_H_
00035
00036 #include "ltiMathFunction.h"
00037 #include "ltiMath.h"
00038 #include <string>
00039
00040 namespace lti {
00041
00042
00043
00044
00045
00046
00047
00048
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 class functionGenerator : public mathFunction {
00102 public:
00103
00104
00105
00106 class parameters : public mathFunction::parameters {
00107 public:
00108
00109
00110
00111 parameters();
00112
00113
00114
00115
00116
00117 parameters(const parameters& other);
00118
00119
00120
00121
00122 ~parameters();
00123
00124
00125
00126
00127 const char* getTypeName() const;
00128
00129
00130
00131
00132
00133
00134 parameters& copy(const parameters& other);
00135
00136
00137
00138
00139
00140
00141 parameters& operator=(const parameters& other);
00142
00143
00144
00145
00146
00147 virtual functor::parameters* clone() const;
00148
00149
00150
00151
00152
00153
00154
00155
00156 virtual bool write(ioHandler& handler,const bool complete=true) const;
00157
00158
00159
00160
00161
00162
00163
00164
00165 virtual bool read(ioHandler& handler,const bool complete=true);
00166
00167 # ifdef _LTI_MSC_6
00168
00169
00170
00171
00172
00173
00174
00175 bool readMS(ioHandler& handler,const bool complete=true);
00176
00177
00178
00179
00180
00181
00182
00183
00184 bool writeMS(ioHandler& handler,const bool complete=true) const;
00185 # endif
00186
00187
00188
00189
00190
00191
00192
00193 std::string function;
00194
00195
00196
00197
00198
00199
00200 std::string nameVarX;
00201
00202
00203
00204
00205
00206
00207 std::string nameVarY;
00208 };
00209
00210
00211
00212
00213 functionGenerator();
00214
00215
00216
00217
00218 functionGenerator(const parameters& par);
00219
00220
00221
00222
00223
00224 functionGenerator(const functionGenerator& other);
00225
00226
00227
00228
00229 virtual ~functionGenerator();
00230
00231
00232
00233
00234 virtual const char* getTypeName() const;
00235
00236
00237
00238
00239
00240
00241 virtual bool updateParameters();
00242
00243
00244
00245
00246
00247
00248
00249
00250 bool apply(const double& x,const double& y,double& result) const;
00251
00252
00253
00254
00255
00256
00257
00258 bool apply(const double& x,double& result) const;
00259
00260
00261
00262
00263
00264
00265 functionGenerator& copy(const functionGenerator& other);
00266
00267
00268
00269
00270
00271
00272 functionGenerator& operator=(const functionGenerator& other);
00273
00274
00275
00276
00277 virtual functor* clone() const;
00278
00279
00280
00281
00282 const parameters& getParameters() const;
00283
00284 # ifdef _LTI_MSC_6
00285 public:
00286 # else
00287 private:
00288 # endif
00289 class node {
00290 protected:
00291 node *pRight, *pLeft;
00292 static bool bMathError;
00293
00294 public:
00295 node(node *pl = NULL, node *pr = NULL);
00296
00297 virtual ~node();
00298
00299 node *getRightChild() {
00300 return pRight;
00301 }
00302
00303 node *getLeftChild() {
00304 return pLeft;
00305 }
00306
00307 void setLeftChild(node *p = NULL) {
00308 pLeft = p;
00309 }
00310
00311 void setRightChild(node *p = NULL) {
00312 pRight = p;
00313 }
00314
00315 int isLeaf() {
00316 return (pLeft == NULL && pRight == NULL);
00317 }
00318
00319 virtual double calculate(const double& x, const double& y) = 0;
00320
00321 static bool error();
00322
00323 };
00324
00325 class nodeAdd : public node {
00326 public:
00327 nodeAdd(node *l,node *r) : node(l,r) {
00328 }
00329
00330 virtual double calculate(const double& x, const double& y) {
00331 return getLeftChild()->calculate(x,y) + getRightChild()->calculate(x,y);
00332 }
00333 };
00334
00335 class nodeSub : public node {
00336 public:
00337 nodeSub(node *l,node *r) : node(l,r) {
00338 }
00339
00340 virtual double calculate(const double& x, const double& y) {
00341 return getLeftChild()->calculate(x,y) - getRightChild()->calculate(x,y);
00342 }
00343 };
00344
00345 class nodeMul : public node {
00346 public:
00347 nodeMul(node *l,node *r) : node(l,r) {
00348 }
00349
00350 virtual double calculate(const double& x, const double& y) {
00351 return getLeftChild()->calculate(x,y) * getRightChild()->calculate(x,y);
00352 }
00353 };
00354
00355 class nodeDiv : public node {
00356 public:
00357 nodeDiv(node *l,node *r);
00358 virtual double calculate(const double& x, const double& y);
00359 };
00360
00361 class nodePow : public node {
00362 public:
00363 nodePow(node *l, node *r);
00364 virtual double calculate(const double& x, const double& y);
00365 };
00366
00367 class nodeConst : public node {
00368 private:
00369 double dValue;
00370
00371 public:
00372 nodeConst(double dConst) : node(NULL,NULL),dValue(dConst) {
00373 }
00374
00375 virtual double calculate(const double& x, const double& y) {
00376 return dValue;
00377 }
00378 };
00379
00380 class nodeVarX : public node {
00381 public:
00382 nodeVarX() : node(NULL,NULL) {
00383 }
00384
00385 virtual double calculate(const double& x, const double& y) {
00386 return x;
00387 }
00388 };
00389
00390 class nodeVarY : public node {
00391 public:
00392 nodeVarY() : node(NULL,NULL) {
00393 }
00394
00395 virtual double calculate(const double& x, const double& y) {
00396 return y;
00397 }
00398 };
00399
00400 class nodeFunc : public node {
00401 # ifdef _LTI_MSC_6
00402 public:
00403 # else
00404 private:
00405 # endif
00406 typedef double (*tMathFunc) (double);
00407
00408 static tMathFunc pFuncs[];
00409 int iFunc;
00410
00411 public:
00412 nodeFunc(node *pNode, int iFuncNr);
00413 virtual double calculate(const double& x, const double& y);
00414 static double Pow(double x,double y);
00415
00416 private:
00417 static double Sin(double x);
00418 static double Cos(double x);
00419 static double Tan(double x);
00420 static double Cot(double x);
00421 static double Sec(double x);
00422 static double Cosec(double x);
00423 static double Arcsin(double x);
00424 static double Arccos(double x);
00425 static double Arctan(double x);
00426 static double Arccot(double x);
00427 static double Sinh(double x);
00428 static double Cosh(double x);
00429 static double Tanh(double x);
00430 static double Coth(double x);
00431 static double Arsinh(double x);
00432 static double Arcosh(double x);
00433 static double Artanh(double x);
00434 static double Arcoth(double x);
00435 static double Ln(double x);
00436 static double Lg(double x);
00437 static double Ld(double x);
00438 static double Sqrt(double x);
00439 static double Abs(double x);
00440 static double Sgn(double x);
00441 static double Intfunc(double x);
00442 };
00443
00444 private:
00445
00446
00447
00448 char* strLower(char* s);
00449 int skipBrackets(char* s,int& i);
00450 char* deleteBrackets(char* s);
00451 char* skipWhiteSpace(char* s);
00452 node* compile1(char* s);
00453 node* compile2(char* s);
00454 node* compile3(char* s);
00455 node* compile4(char* s);
00456
00457 const static char* szFuncs[];
00458 const static double pi;
00459 const static double e;
00460
00461 int iError;
00462 char* szErrorPos;
00463 bool bHasTwoVars;
00464 node* pRootNode;
00465 };
00466 }
00467
00468 #endif