FunDef.h

00001 /*          
00002  *             Copyright (C) 2005 Maarten Keijzer
00003  *
00004  *          This program is free software; you can redistribute it and/or modify
00005  *          it under the terms of version 2 of the GNU General Public License as 
00006  *          published by the Free Software Foundation. 
00007  *
00008  *          This program is distributed in the hope that it will be useful,
00009  *          but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011  *          GNU General Public License for more details.
00012  *
00013  *          You should have received a copy of the GNU General Public License
00014  *          along with this program; if not, write to the Free Software
00015  *          Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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.h"
00027 #include "Interval.h"
00028 
00029 class FunDef {
00030     public:
00031     
00032     virtual ~FunDef() {}
00033     
00034     // (possibly) lazy evaluation function, default implementation calls 'eager' eval
00035     virtual double eval(const SymVec&   args, const std::vector<double>& inputs) const;
00036 
00037     // eager evaluation function 
00038     virtual double eval(const std::vector<double>& args, const std::vector<double>& inputs) const = 0;
00039    
00040     // interval evaluation
00041     virtual Interval eval(const std::vector<Interval>& args, const std::vector<Interval>& inputs) const = 0; 
00042     
00043     // prints 'c' like code
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; } // sum, prod, min, max are variable arity
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 /* Support for Ephemeral Random Constants (ERC) */
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{}; // thrown in case of ifltz
00127 
00128 /* Add function to the language table (and take a guess at the arity) */
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 /* Defition of function overloads: for example, this define the function 'Sym sin(Sym)' */
00148 
00149 #define HEADERFUNC(name) inline Sym name(Sym arg) { return Sym(name##_token, arg); }
00150 
00151 /* This defines the tokens: sin_token, cos_token, etc. */
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 /* Get the prototype functions out, this is needed for compilation */
00174 extern std::string get_prototypes();
00175 
00176 // reading and writing in internal format, no parser for symbolic functions implemented yet
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 

Generated on Thu Oct 19 05:06:40 2006 for EO by  doxygen 1.3.9.1