00001
00002
00003
00004
00005 #ifndef FITYK_VM_H_
00006 #define FITYK_VM_H_
00007
00008 #include "fityk.h"
00009 #include "common.h"
00010 using fityk::Point;
00011
00012 class Ftk;
00013 class Variable;
00014
00015
00016 enum Op
00017 {
00018
00019 OP_NUMBER,
00020
00021
00022
00023 OP_SYMBOL,
00024
00025
00026 OP_X,
00027 OP_PUT_DERIV,
00028
00029
00030 OP_ONE_ARG,
00031 OP_NEG = OP_ONE_ARG,
00032 OP_EXP,
00033 OP_ERFC,
00034 OP_ERF,
00035 OP_SIN,
00036 OP_COS,
00037 OP_TAN,
00038 OP_SINH,
00039 OP_COSH,
00040 OP_TANH,
00041 OP_ASIN,
00042 OP_ACOS,
00043 OP_ATAN,
00044 OP_LOG10,
00045 OP_LN,
00046 OP_SQRT,
00047 OP_GAMMA,
00048 OP_LGAMMA,
00049 OP_DIGAMMA,
00050 OP_ABS,
00051 OP_ROUND,
00052
00053
00054 OP_TWO_ARG,
00055 OP_POW = OP_TWO_ARG,
00056 OP_MUL,
00057 OP_DIV,
00058 OP_ADD,
00059 OP_SUB,
00060 OP_VOIGT,
00061 OP_DVOIGT_DX,
00062 OP_DVOIGT_DY,
00063 OP_MOD,
00064 OP_MIN2,
00065 OP_MAX2,
00066 OP_RANDNORM,
00067 OP_RANDU,
00068
00069
00070 OP_GT, OP_GE, OP_LT, OP_LE, OP_EQ, OP_NEQ,
00071
00072
00073
00074 OP_NOT,
00075
00076 OP_OR, OP_AFTER_OR, OP_AND, OP_AFTER_AND,
00077
00078 OP_TERNARY, OP_TERNARY_MID, OP_AFTER_TERNARY,
00079
00080
00081 OP_XINDEX,
00082
00083
00084 OP_PX, OP_PY, OP_PS, OP_PA,
00085 OP_Px, OP_Py, OP_Ps, OP_Pa,
00086 OP_Pn, OP_PM,
00087
00088
00089 OP_ASSIGN_X, OP_ASSIGN_Y, OP_ASSIGN_S, OP_ASSIGN_A,
00090
00091
00092 OP_FUNC, OP_SUM_F, OP_SUM_Z,
00093
00094
00095 OP_NUMAREA, OP_FINDX, OP_FIND_EXTR,
00096
00097 OP_TILDE,
00098
00099
00100 OP_DATASET,
00101 OP_DT_SUM_SAME_X,
00102 OP_DT_AVG_SAME_X,
00103 OP_DT_SHIRLEY_BG,
00104
00105
00106
00107 OP_OPEN_ROUND, OP_OPEN_SQUARE
00108 };
00109
00110
00111 class VMData
00112 {
00113 public:
00114 static bool has_idx(int op)
00115 { return op == OP_NUMBER || op == OP_SYMBOL || op == OP_PUT_DERIV ||
00116 op == OP_DATASET; }
00117
00118 const std::vector<int>& code() const { return code_; }
00119 const std::vector<realt>& numbers() const { return numbers_; }
00120
00121 void append_code(int op) { code_.push_back(op); }
00122 void append_number(realt d);
00123 void clear_data() { code_.clear(); numbers_.clear(); }
00124 void replace_symbols(const std::vector<realt>& vv);
00125 void flip_indices();
00126 bool single_symbol() const {return code_.size()==2 && code_[0]==OP_SYMBOL;}
00127 bool has_op(int op) const;
00128 std::vector<int>& get_mutable_code() { return code_; }
00129
00130 private:
00131 std::vector<int> code_;
00132 std::vector<realt> numbers_;
00133 };
00134
00135 std::string op2str(int op);
00136 std::string vm2str(const std::vector<int>& code,
00137 const std::vector<realt>& data);
00138 inline
00139 std::string vm2str(const VMData& vm) { return vm2str(vm.code(), vm.numbers()); }
00140
00141
00142 class ExprCalculator
00143 {
00144 public:
00145 ExprCalculator(const Ftk* F) : F_(F) {}
00146
00147
00148 realt calculate(int n, const std::vector<Point>& points) const;
00149
00150
00151 realt calculate() const { return calculate(0, std::vector<Point>()); }
00152
00153
00154
00155 realt calculate_custom(const std::vector<realt>& custom_val) const;
00156
00157
00158 void transform_data(std::vector<Point>& points);
00159
00160 const VMData& vm() const { return vm_; }
00161
00162 protected:
00163 const Ftk* F_;
00164 VMData vm_;
00165 };
00166
00167 realt run_code_for_variable(const VMData& vm,
00168 const std::vector<Variable*> &variables,
00169 std::vector<realt> &derivatives);
00170 realt run_code_for_custom_func(const VMData& vm, realt x,
00171 std::vector<realt> &derivatives);
00172 realt run_code_for_custom_func_value(const VMData& vm, realt x,
00173 int code_offset);
00174
00175 #endif // FITYK_VM_H_
00176