]> www.ginac.de Git - ginac.git/blob - ginac/function.hppy
Transform abs(x)^n => x^n if x is real and n is even.
[ginac.git] / ginac / function.hppy
1 /** @file function.h
2  *
3  *  Interface to class of symbolic functions. */
4
5 /*
6  *  This file was generated automatically from function.hppy.
7  *  Please do not modify it directly, edit function.hppy instead!
8  *
9  *  GiNaC Copyright (C) 1999-2010 Johannes Gutenberg University Mainz, Germany
10  *
11  *  This program is free software; you can redistribute it and/or modify
12  *  it under the terms of the GNU General Public License as published by
13  *  the Free Software Foundation; either version 2 of the License, or
14  *  (at your option) any later version.
15  *
16  *  This program is distributed in the hope that it will be useful,
17  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  *  GNU General Public License for more details.
20  *
21  *  You should have received a copy of the GNU General Public License
22  *  along with this program; if not, write to the Free Software
23  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
24  */
25
26 #ifndef GINAC_FUNCTION_H
27 #define GINAC_FUNCTION_H
28
29 #include "exprseq.h"
30
31 // CINT needs <algorithm> to work properly with <vector>
32 #include <algorithm>
33 #include <string>
34 #include <vector>
35
36 +++ for N in range(1, maxargs + 1):
37 #define DECLARE_FUNCTION_@N@P(NAME) \
38 class NAME##_SERIAL { public: static unsigned serial; }; \
39 const unsigned NAME##_NPARAMS = @N@; \
40 template< @seq('typename T%(n)d', N)@ > const GiNaC::function NAME( @seq('const T%(n)d & p%(n)d', N)@ ) { \
41         return GiNaC::function(NAME##_SERIAL::serial, @seq('GiNaC::ex(p%(n)d)', N)@ ); \
42 }
43 ---
44 // end of generated lines
45
46 #define REGISTER_FUNCTION(NAME,OPT) \
47 unsigned NAME##_SERIAL::serial = \
48         GiNaC::function::register_new(GiNaC::function_options(#NAME, NAME##_NPARAMS).OPT);
49
50 namespace GiNaC {
51
52 class function;
53 class symmetry;
54
55 typedef ex (* eval_funcp)();
56 typedef ex (* evalf_funcp)();
57 typedef ex (* conjugate_funcp)();
58 typedef ex (* real_part_funcp)();
59 typedef ex (* imag_part_funcp)();
60 typedef ex (* derivative_funcp)();
61 typedef ex (* power_funcp)();
62 typedef ex (* series_funcp)();
63 typedef void (* print_funcp)();
64
65 // the following lines have been generated for max. @maxargs@ parameters
66 +++ for N, args in [ ( N, seq('const ex &', N) ) for N in range(1, maxargs + 1) ]:
67 typedef ex (* eval_funcp_@N@)( @args@ );
68 typedef ex (* evalf_funcp_@N@)( @args@ );
69 typedef ex (* conjugate_funcp_@N@)( @args@ );
70 typedef ex (* real_part_funcp_@N@)( @args@ );
71 typedef ex (* imag_part_funcp_@N@)( @args@ );
72 typedef ex (* derivative_funcp_@N@)( @args@, unsigned );
73 typedef ex (* power_funcp_@N@)( @args@, const ex & );
74 typedef ex (* series_funcp_@N@)( @args@, const relational &, int, unsigned );
75 typedef void (* print_funcp_@N@)( @args@, const print_context & );
76 ---
77 // end of generated lines
78
79 // Alternatively, an exvector may be passed into the static function, instead
80 // of individual ex objects.  Then, the number of arguments is not limited.
81 +++ for fp in "eval evalf conjugate real_part imag_part".split():
82 typedef ex (* @fp@_funcp_exvector)(const exvector &);
83 ---
84 typedef ex (* derivative_funcp_exvector)(const exvector &, unsigned);
85 typedef ex (* power_funcp_exvector)(const exvector &, const ex &);
86 typedef ex (* series_funcp_exvector)(const exvector &, const relational &, int, unsigned);
87 typedef void (* print_funcp_exvector)(const exvector &, const print_context &);
88
89
90 class function_options
91 {
92         friend class function;
93         friend class fderivative;
94 public:
95         function_options();
96         function_options(std::string const & n, std::string const & tn=std::string());
97         function_options(std::string const & n, unsigned np);
98         ~function_options();
99         void initialize();
100
101         function_options & dummy() { return *this; }
102         function_options & set_name(std::string const & n, std::string const & tn=std::string());
103         function_options & latex_name(std::string const & tn);
104         // following lines have been generated for max. @maxargs@ parameters
105 +++ for f, N in [ (f, N) for f in methods[0:-1] for N in range(1, maxargs + 1) ]:
106         function_options & @f@_func(@f@_funcp_@N@ e);
107 ---
108 +++ for f in methods[0:-1]:
109         function_options & @f@_func(@f@_funcp_exvector e);
110 ---
111 +++ for N in range(1, N + 1):
112         template <class Ctx> function_options & print_func(print_funcp_@N@ p)
113         {
114                 test_and_set_nparams(@N@);
115                 set_print_func(Ctx::get_class_info_static().options.get_id(), print_funcp(p));
116                 return *this;
117         }
118 ---
119         // end of generated lines
120
121         template <class Ctx> function_options & print_func(print_funcp_exvector p)
122         {
123                 print_use_exvector_args = true;
124                 set_print_func(Ctx::get_class_info_static().options.get_id(), print_funcp(p));
125                 return *this;
126         }
127
128         function_options & set_return_type(unsigned rt, const return_type_t* rtt = 0);
129         function_options & do_not_evalf_params();
130         function_options & remember(unsigned size, unsigned assoc_size=0,
131                                     unsigned strategy=remember_strategies::delete_never);
132         function_options & overloaded(unsigned o);
133         function_options & set_symmetry(const symmetry & s);
134
135         std::string get_name() const { return name; }
136         unsigned get_nparams() const { return nparams; }
137
138 protected:
139         bool has_derivative() const { return derivative_f != NULL; }
140         bool has_power() const { return power_f != NULL; }
141         void test_and_set_nparams(unsigned n);
142         void set_print_func(unsigned id, print_funcp f);
143
144         std::string name;
145         std::string TeX_name;
146
147         unsigned nparams;
148
149         eval_funcp eval_f;
150         evalf_funcp evalf_f;
151         conjugate_funcp conjugate_f;
152         real_part_funcp real_part_f;
153         imag_part_funcp imag_part_f;
154         derivative_funcp derivative_f;
155         power_funcp power_f;
156         series_funcp series_f;
157         std::vector<print_funcp> print_dispatch_table;
158
159         bool evalf_params_first;
160
161         bool use_return_type;
162         unsigned return_type;
163         return_type_t return_type_tinfo;
164
165         bool use_remember;
166         unsigned remember_size;
167         unsigned remember_assoc_size;
168         unsigned remember_strategy;
169
170         bool eval_use_exvector_args;
171         bool evalf_use_exvector_args;
172         bool conjugate_use_exvector_args;
173         bool real_part_use_exvector_args;
174         bool imag_part_use_exvector_args;
175         bool derivative_use_exvector_args;
176         bool power_use_exvector_args;
177         bool series_use_exvector_args;
178         bool print_use_exvector_args;
179
180         unsigned functions_with_same_name;
181
182         ex symtree;
183 };
184
185
186 /** Exception class thrown by classes which provide their own series expansion
187  *  to signal that ordinary Taylor expansion is safe. */
188 class do_taylor {};
189
190
191 /** The class function is used to implement builtin functions like sin, cos...
192         and user defined functions */
193 class function : public exprseq
194 {
195         GINAC_DECLARE_REGISTERED_CLASS(function, exprseq)
196
197         friend class remember_table_entry;
198
199 // member functions
200
201         // other constructors
202 public:
203         function(unsigned ser);
204         // the following lines have been generated for max. @maxargs@ parameters
205 +++ for N in range(1, maxargs + 1):
206         function(unsigned ser, @seq('const ex & param%(n)d', N)@);
207 ---
208         // end of generated lines
209         function(unsigned ser, const exprseq & es);
210         function(unsigned ser, const exvector & v, bool discardable = false);
211         function(unsigned ser, std::auto_ptr<exvector> vp);
212         
213         // functions overriding virtual functions from base classes
214 public:
215         void print(const print_context & c, unsigned level = 0) const;
216         unsigned precedence() const {return 70;}
217         ex expand(unsigned options=0) const;
218         ex eval(int level=0) const;
219         ex evalf(int level=0) const;
220         ex eval_ncmul(const exvector & v) const;
221         unsigned calchash() const;
222         ex series(const relational & r, int order, unsigned options = 0) const;
223         ex thiscontainer(const exvector & v) const;
224         ex thiscontainer(std::auto_ptr<exvector> vp) const;
225         ex conjugate() const;
226         ex real_part() const;
227         ex imag_part() const;
228         void archive(archive_node& n) const;
229         void read_archive(const archive_node& n, lst& syms);
230 protected:
231         ex derivative(const symbol & s) const;
232         bool is_equal_same_type(const basic & other) const;
233         bool match_same_type(const basic & other) const;
234         unsigned return_type() const;
235         return_type_t return_type_tinfo() const;
236         
237         // new virtual functions which can be overridden by derived classes
238         // none
239         
240         // non-virtual functions in this class
241 protected:
242         ex pderivative(unsigned diff_param) const; // partial differentiation
243         static std::vector<function_options> & registered_functions();
244         bool lookup_remember_table(ex & result) const;
245         void store_remember_table(ex const & result) const;
246 public:
247         ex power(const ex & exp) const;
248         static unsigned register_new(function_options const & opt);
249         static unsigned current_serial;
250         static unsigned find_function(const std::string &name, unsigned nparams);
251         static std::vector<function_options> get_registered_functions() { return registered_functions(); };
252         unsigned get_serial() const {return serial;}
253         std::string get_name() const;
254
255 // member variables
256
257 protected:
258         unsigned serial;
259 };
260 GINAC_DECLARE_UNARCHIVER(function);
261
262 // utility functions/macros
263
264 template <typename T>
265 inline bool is_the_function(const ex & x)
266 {
267         return is_exactly_a<function>(x)
268             && ex_to<function>(x).get_serial() == T::serial;
269 }
270
271 // Check whether OBJ is the specified symbolic function.
272 #define is_ex_the_function(OBJ, FUNCNAME) (GiNaC::is_the_function<FUNCNAME##_SERIAL>(OBJ))
273
274 } // namespace GiNaC
275
276 #endif // ndef GINAC_FUNCTION_H
277