00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef FUNCTION_DEF_H_
00019 #define FUNCTION_DEF_H_
00020
00021 #include <string>
00022 #include <vector>
00023 #include <memory>
00024 #include <iostream>
00025
00026 #include "sym/Sym.h"
00027 #include "eval/Interval.h"
00028
00029 class FunDef {
00030 public:
00031
00032 virtual ~FunDef() {}
00033
00034
00035 virtual double eval(const SymVec& args, const std::vector<double>& inputs) const;
00036
00037
00038 virtual double eval(const std::vector<double>& args, const std::vector<double>& inputs) const = 0;
00039
00040
00041 virtual Interval eval(const std::vector<Interval>& args, const std::vector<Interval>& inputs) const = 0;
00042
00043
00044 virtual std::string c_print(const std::vector<std::string>& names, const std::vector<std::string>& names) const = 0;
00045
00046 virtual unsigned min_arity() const = 0;
00047 virtual bool has_varargs() const { return false; }
00048
00049 virtual std::string name() const = 0;
00050
00051 protected:
00052
00053 };
00054
00056 extern std::vector<const FunDef*> get_defined_functions();
00057
00059 extern const FunDef& get_element(token_t token);
00060
00062 extern double eval(const Sym& sym, const std::vector<double>& inputs);
00063
00065 extern Interval eval(const Sym& sym, const std::vector<Interval>& inputs);
00066
00068 extern std::string c_print(const Sym& sym);
00069
00071 extern std::string c_print(const Sym& sym, const std::vector<std::string>& var_names);
00072
00074 inline std::ostream& operator<<(std::ostream& os, const Sym& sym) { return os << c_print(sym); }
00075
00076
00077
00079 extern Sym SymConst(double value);
00081 extern Sym SymVar(unsigned idx);
00082
00093 extern Sym SymLambda(Sym expression);
00094
00095 extern Sym SymUnlambda(Sym sym);
00096
00098 extern Sym expand_all(Sym sym);
00099 extern Sym compress(Sym sym);
00100
00102 std::vector<double> get_constants(Sym sym);
00103
00106 Sym set_constants(Sym sym, const std::vector<double>& constants);
00107
00109 extern bool is_constant(token_t token);
00110 extern double get_constant_value(token_t token);
00112 extern bool is_variable(token_t token);
00113 extern unsigned get_variable_index(token_t token);
00114
00116 extern bool is_lambda(token_t token);
00117
00118
00120 extern Sym simplify(Sym sym);
00121
00125 extern Sym differentiate(Sym sym, token_t dx);
00126 struct differentiation_error{};
00127
00128
00129 class LanguageTable;
00130 extern void add_function_to_table(LanguageTable& table, token_t token);
00131
00132 enum {
00133 sum_token,
00134 prod_token,
00135 inv_token,
00136 min_token,
00137 pow_token,
00138 ifltz_token,
00139 sin_token, cos_token, tan_token,
00140 asin_token, acos_token, atan_token,
00141 sinh_token, cosh_token, tanh_token,
00142 acosh_token, asinh_token, atanh_token,
00143 exp_token, log_token,
00144 sqr_token, sqrt_token
00145 };
00146
00147
00148
00149 #define HEADERFUNC(name) inline Sym name(Sym arg) { return Sym(name##_token, arg); }
00150
00151
00152 HEADERFUNC(inv);
00153 HEADERFUNC(sin);
00154 HEADERFUNC(cos);
00155 HEADERFUNC(tan);
00156 HEADERFUNC(asin);
00157 HEADERFUNC(acos);
00158 HEADERFUNC(atan);
00159
00160 HEADERFUNC(sinh);
00161 HEADERFUNC(cosh);
00162 HEADERFUNC(tanh);
00163 HEADERFUNC(asinh);
00164 HEADERFUNC(acosh);
00165 HEADERFUNC(atanh);
00166
00167 HEADERFUNC(exp);
00168 HEADERFUNC(log);
00169
00170 HEADERFUNC(sqr);
00171 HEADERFUNC(sqrt);
00172
00173
00174 extern std::string get_prototypes();
00175
00176
00177 extern std::string write_raw(const Sym& sym);
00178 extern void write_raw(std::ostream& os, const Sym& sym);
00179 extern Sym read_raw(std::string str);
00180 extern Sym read_raw(std::istream& is);
00181 extern void read_raw(std::istream& is, Sym& sym);
00182
00183 #include "SymOps.h"
00184
00185 #endif
00186