]> www.ginac.de Git - ginac.git/blob - ginac/function.hppy
cache pseries coeff accesses in pseries::mul_series.
[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-2015 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 #include <string>
32 #include <vector>
33
34 +++ for N in range(1, maxargs + 1):
35 #define DECLARE_FUNCTION_@N@P(NAME) \
36 class NAME##_SERIAL { public: static unsigned serial; }; \
37 const unsigned NAME##_NPARAMS = @N@; \
38 template< @seq('typename T%(n)d', N)@ > const GiNaC::function NAME( @seq('const T%(n)d & p%(n)d', N)@ ) { \
39         return GiNaC::function(NAME##_SERIAL::serial, @seq('GiNaC::ex(p%(n)d)', N)@ ); \
40 }
41 ---
42 // end of generated lines
43
44 #define REGISTER_FUNCTION(NAME,OPT) \
45 unsigned NAME##_SERIAL::serial = \
46         GiNaC::function::register_new(GiNaC::function_options(#NAME, NAME##_NPARAMS).OPT);
47
48 namespace GiNaC {
49
50 class function;
51 class symmetry;
52
53 typedef ex (* eval_funcp)();
54 typedef ex (* evalf_funcp)();
55 typedef ex (* conjugate_funcp)();
56 typedef ex (* real_part_funcp)();
57 typedef ex (* imag_part_funcp)();
58 typedef ex (* expand_funcp)();
59 typedef ex (* derivative_funcp)();
60 typedef ex (* expl_derivative_funcp)();
61 typedef ex (* power_funcp)();
62 typedef ex (* series_funcp)();
63 typedef void (* print_funcp)();
64 typedef bool (* info_funcp)();
65
66 // the following lines have been generated for max. @maxargs@ parameters
67 +++ for N, args in [ ( N, seq('const ex &', N) ) for N in range(1, maxargs + 1) ]:
68 typedef ex (* eval_funcp_@N@)( @args@ );
69 typedef ex (* evalf_funcp_@N@)( @args@ );
70 typedef ex (* conjugate_funcp_@N@)( @args@ );
71 typedef ex (* real_part_funcp_@N@)( @args@ );
72 typedef ex (* imag_part_funcp_@N@)( @args@ );
73 typedef ex (* expand_funcp_@N@)( @args@, unsigned );
74 typedef ex (* derivative_funcp_@N@)( @args@, unsigned );
75 typedef ex (* expl_derivative_funcp_@N@)( @args@, const symbol & );
76 typedef ex (* power_funcp_@N@)( @args@, const ex & );
77 typedef ex (* series_funcp_@N@)( @args@, const relational &, int, unsigned );
78 typedef void (* print_funcp_@N@)( @args@, const print_context & );
79 typedef bool (* info_funcp_@N@)( @args@, unsigned );
80 ---
81 // end of generated lines
82
83 // Alternatively, an exvector may be passed into the static function, instead
84 // of individual ex objects.  Then, the number of arguments is not limited.
85 +++ for fp in "eval evalf conjugate real_part imag_part".split():
86 typedef ex (* @fp@_funcp_exvector)(const exvector &);
87 ---
88 typedef ex (* expand_funcp_exvector)(const exvector &, unsigned);
89 typedef ex (* derivative_funcp_exvector)(const exvector &, unsigned);
90 typedef ex (* expl_derivative_funcp_exvector)(const exvector &, const symbol &);
91 typedef ex (* power_funcp_exvector)(const exvector &, const ex &);
92 typedef ex (* series_funcp_exvector)(const exvector &, const relational &, int, unsigned);
93 typedef void (* print_funcp_exvector)(const exvector &, const print_context &);
94 typedef bool (* info_funcp_exvector)(const exvector &, unsigned);
95
96
97 class function_options
98 {
99         friend class function;
100         friend class fderivative;
101 public:
102         function_options();
103         function_options(std::string const & n, std::string const & tn=std::string());
104         function_options(std::string const & n, unsigned np);
105         ~function_options();
106         void initialize();
107
108         function_options & dummy() { return *this; }
109         function_options & set_name(std::string const & n, std::string const & tn=std::string());
110         function_options & latex_name(std::string const & tn);
111         // following lines have been generated for max. @maxargs@ parameters
112 +++ for f, N in [ (f, N) for f in methods[0:-1] for N in range(1, maxargs + 1) ]:
113         function_options & @f@_func(@f@_funcp_@N@ e);
114 ---
115 +++ for f in methods[0:-1]:
116         function_options & @f@_func(@f@_funcp_exvector e);
117 ---
118 +++ for N in range(1, N + 1):
119         template <class Ctx> function_options & print_func(print_funcp_@N@ p)
120         {
121                 test_and_set_nparams(@N@);
122                 set_print_func(Ctx::get_class_info_static().options.get_id(), print_funcp(p));
123                 return *this;
124         }
125 ---
126         // end of generated lines
127
128         template <class Ctx> function_options & print_func(print_funcp_exvector p)
129         {
130                 print_use_exvector_args = true;
131                 set_print_func(Ctx::get_class_info_static().options.get_id(), print_funcp(p));
132                 return *this;
133         }
134
135         function_options & set_return_type(unsigned rt, const return_type_t* rtt = nullptr);
136         function_options & do_not_evalf_params();
137         function_options & remember(unsigned size, unsigned assoc_size=0,
138                                     unsigned strategy=remember_strategies::delete_never);
139         function_options & overloaded(unsigned o);
140         function_options & set_symmetry(const symmetry & s);
141
142         std::string get_name() const { return name; }
143         unsigned get_nparams() const { return nparams; }
144
145 protected:
146         bool has_derivative() const { return derivative_f != nullptr; }
147         bool has_power() const { return power_f != nullptr; }
148         void test_and_set_nparams(unsigned n);
149         void set_print_func(unsigned id, print_funcp f);
150
151         std::string name;
152         std::string TeX_name;
153
154         unsigned nparams;
155
156         eval_funcp eval_f;
157         evalf_funcp evalf_f;
158         conjugate_funcp conjugate_f;
159         real_part_funcp real_part_f;
160         imag_part_funcp imag_part_f;
161         expand_funcp expand_f;
162         derivative_funcp derivative_f;
163         expl_derivative_funcp expl_derivative_f;
164         power_funcp power_f;
165         series_funcp series_f;
166         std::vector<print_funcp> print_dispatch_table;
167         info_funcp info_f;
168
169         bool evalf_params_first;
170
171         bool use_return_type;
172         unsigned return_type;
173         return_type_t return_type_tinfo;
174
175         bool use_remember;
176         unsigned remember_size;
177         unsigned remember_assoc_size;
178         unsigned remember_strategy;
179
180         bool eval_use_exvector_args;
181         bool evalf_use_exvector_args;
182         bool conjugate_use_exvector_args;
183         bool real_part_use_exvector_args;
184         bool imag_part_use_exvector_args;
185         bool expand_use_exvector_args;
186         bool derivative_use_exvector_args;
187         bool expl_derivative_use_exvector_args;
188         bool power_use_exvector_args;
189         bool series_use_exvector_args;
190         bool print_use_exvector_args;
191         bool info_use_exvector_args;
192
193         unsigned functions_with_same_name;
194
195         ex symtree;
196 };
197
198
199 /** Exception class thrown by classes which provide their own series expansion
200  *  to signal that ordinary Taylor expansion is safe. */
201 class do_taylor {};
202
203
204 /** The class function is used to implement builtin functions like sin, cos...
205         and user defined functions */
206 class function : public exprseq
207 {
208         GINAC_DECLARE_REGISTERED_CLASS(function, exprseq)
209
210         friend class remember_table_entry;
211
212 // member functions
213
214         // other constructors
215 public:
216         function(unsigned ser);
217         // the following lines have been generated for max. @maxargs@ parameters
218 +++ for N in range(1, maxargs + 1):
219         function(unsigned ser, @seq('const ex & param%(n)d', N)@);
220 ---
221         // end of generated lines
222         function(unsigned ser, const exprseq & es);
223         function(unsigned ser, const exvector & v);
224         function(unsigned ser, exvector && v);
225         
226         // functions overriding virtual functions from base classes
227 public:
228         void print(const print_context & c, unsigned level = 0) const override;
229         unsigned precedence() const override {return 70;}
230         ex expand(unsigned options=0) const override;
231         ex eval() const override;
232         ex evalf(int level=0) const override;
233         ex eval_ncmul(const exvector & v) const override;
234         unsigned calchash() const override;
235         ex series(const relational & r, int order, unsigned options = 0) const override;
236         ex thiscontainer(const exvector & v) const override;
237         ex thiscontainer(exvector && v) const override;
238         ex conjugate() const override;
239         ex real_part() const override;
240         ex imag_part() const override;
241         void archive(archive_node& n) const override;
242         void read_archive(const archive_node& n, lst& syms) override;
243         bool info(unsigned inf) const override;
244 protected:
245         ex derivative(const symbol & s) const override;
246         bool is_equal_same_type(const basic & other) const override;
247         bool match_same_type(const basic & other) const override;
248         unsigned return_type() const override;
249         return_type_t return_type_tinfo() const override;
250         
251         // new virtual functions which can be overridden by derived classes
252         // none
253         
254         // non-virtual functions in this class
255 protected:
256         ex pderivative(unsigned diff_param) const; // partial differentiation
257         ex expl_derivative(const symbol & s) const; // partial differentiation
258         static std::vector<function_options> & registered_functions();
259         bool lookup_remember_table(ex & result) const;
260         void store_remember_table(ex const & result) const;
261 public:
262         ex power(const ex & exp) const;
263         static unsigned register_new(function_options const & opt);
264         static unsigned current_serial;
265         static unsigned find_function(const std::string &name, unsigned nparams);
266         static std::vector<function_options> get_registered_functions() { return registered_functions(); };
267         unsigned get_serial() const {return serial;}
268         std::string get_name() const;
269
270 // member variables
271
272 protected:
273         unsigned serial;
274 };
275 GINAC_DECLARE_UNARCHIVER(function);
276
277 // utility functions/macros
278
279 template <typename T>
280 inline bool is_the_function(const ex & x)
281 {
282         return is_exactly_a<function>(x)
283             && ex_to<function>(x).get_serial() == T::serial;
284 }
285
286 // Check whether OBJ is the specified symbolic function.
287 #define is_ex_the_function(OBJ, FUNCNAME) (GiNaC::is_the_function<FUNCNAME##_SERIAL>(OBJ))
288
289 } // namespace GiNaC
290
291 #endif // ndef GINAC_FUNCTION_H
292