]> www.ginac.de Git - ginac.git/blob - ginac/numeric.h
Fix unarchiving empty containers.
[ginac.git] / ginac / numeric.h
1 /** @file numeric.h
2  *
3  *  Makes the interface to the underlying bignum package available. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2019 Johannes Gutenberg University Mainz, Germany
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21  */
22
23 #ifndef GINAC_NUMERIC_H
24 #define GINAC_NUMERIC_H
25
26 #include "basic.h"
27 #include "ex.h"
28 #include "archive.h"
29
30 #include <cln/complex.h>
31 #include <stdexcept>
32 #include <vector>
33
34 namespace GiNaC {
35
36 /** Function pointer to implement callbacks in the case 'Digits' gets changed.
37  *  Main purpose of such callbacks is to adjust look-up tables of certain
38  *  functions to the new precision. Parameter contains the signed difference
39  *  between new Digits and old Digits. */
40 typedef void (* digits_changed_callback)(long);
41
42 /** This class is used to instantiate a global singleton object Digits
43  *  which behaves just like Maple's Digits.  We need an object rather 
44  *  than a dumber basic type since as a side-effect we let it change
45  *  cl_default_float_format when it gets changed.  The only other
46  *  meaningful thing to do with it is converting it to an unsigned,
47  *  for temporarily storing its value e.g.  The user must not create an
48  *  own working object of this class!  Since C++ forces us to make the
49  *  class definition visible in order to use an object we put in a
50  *  flag which prevents other objects of that class to be created. */
51 class _numeric_digits
52 {
53 // member functions
54 public:
55         _numeric_digits();
56         _numeric_digits& operator=(long prec);
57         operator long();
58         void print(std::ostream& os) const;
59         void add_callback(digits_changed_callback callback);
60 // member variables
61 private:
62         long digits;                        ///< Number of decimal digits
63         static bool too_late;               ///< Already one object present
64         // Holds a list of functions that get called when digits is changed.
65         std::vector<digits_changed_callback> callbacklist;
66 };
67
68
69 /** Exception class thrown when a singularity is encountered. */
70 class pole_error : public std::domain_error {
71 public:
72         explicit pole_error(const std::string& what_arg, int degree);
73         int degree() const;
74 private:
75         int deg;
76 };
77
78
79 /** This class is a wrapper around CLN-numbers within the GiNaC class
80  *  hierarchy. Objects of this type may directly be created by the user.*/
81 class numeric : public basic
82 {
83         GINAC_DECLARE_REGISTERED_CLASS(numeric, basic)
84         
85 // member functions
86         
87         // other constructors
88 public:
89         numeric(int i);
90         numeric(unsigned int i);
91         numeric(long i);
92         numeric(unsigned long i);
93         numeric(long numer, long denom);
94         numeric(double d);
95         numeric(const char *);
96         
97         // functions overriding virtual functions from base classes
98 public:
99         unsigned precedence() const override {return 30;}
100         bool info(unsigned inf) const override;
101         bool is_polynomial(const ex & var) const override;
102         int degree(const ex & s) const override;
103         int ldegree(const ex & s) const override;
104         ex coeff(const ex & s, int n = 1) const override;
105         bool has(const ex &other, unsigned options = 0) const override;
106         ex eval() const override;
107         ex evalf() const override;
108         ex subs(const exmap & m, unsigned options = 0) const override { return subs_one_level(m, options); } // overwrites basic::subs() for performance reasons
109         ex normal(exmap & repl, exmap & rev_lookup) const override;
110         ex to_rational(exmap & repl) const override;
111         ex to_polynomial(exmap & repl) const override;
112         numeric integer_content() const override;
113         ex smod(const numeric &xi) const override;
114         numeric max_coefficient() const override;
115         ex conjugate() const override;
116         ex real_part() const override;
117         ex imag_part() const override;
118         /** Save (a.k.a. serialize) object into archive. */
119         void archive(archive_node& n) const override;
120         /** Read (a.k.a. deserialize) object from archive. */
121         void read_archive(const archive_node& n, lst& syms) override;
122 protected:
123         /** Implementation of ex::diff for a numeric always returns 0.
124          *  @see ex::diff */
125         ex derivative(const symbol &s) const override { return 0; }
126         bool is_equal_same_type(const basic &other) const override;
127         unsigned calchash() const override;
128         
129         // new virtual functions which can be overridden by derived classes
130         // (none)
131         
132         // non-virtual functions in this class
133 public:
134         const numeric add(const numeric &other) const;
135         const numeric sub(const numeric &other) const;
136         const numeric mul(const numeric &other) const;
137         const numeric div(const numeric &other) const;
138         const numeric power(const numeric &other) const;
139         const numeric & add_dyn(const numeric &other) const;
140         const numeric & sub_dyn(const numeric &other) const;
141         const numeric & mul_dyn(const numeric &other) const;
142         const numeric & div_dyn(const numeric &other) const;
143         const numeric & power_dyn(const numeric &other) const;
144         const numeric & operator=(int i);
145         const numeric & operator=(unsigned int i);
146         const numeric & operator=(long i);
147         const numeric & operator=(unsigned long i);
148         const numeric & operator=(double d);
149         const numeric & operator=(const char *s);
150         const numeric inverse() const;
151         numeric step() const;
152         int csgn() const;
153         int compare(const numeric &other) const;
154         bool is_equal(const numeric &other) const;
155         bool is_zero() const;
156         bool is_positive() const;
157         bool is_negative() const;
158         bool is_integer() const;
159         bool is_pos_integer() const;
160         bool is_nonneg_integer() const;
161         bool is_even() const;
162         bool is_odd() const;
163         bool is_prime() const;
164         bool is_rational() const;
165         bool is_real() const;
166         bool is_cinteger() const;
167         bool is_crational() const;
168         bool operator==(const numeric &other) const;
169         bool operator!=(const numeric &other) const;
170         bool operator<(const numeric &other) const;
171         bool operator<=(const numeric &other) const;
172         bool operator>(const numeric &other) const;
173         bool operator>=(const numeric &other) const;
174         int to_int() const;
175         long to_long() const;
176         double to_double() const;
177         cln::cl_N to_cl_N() const;
178         const numeric real() const;
179         const numeric imag() const;
180         const numeric numer() const;
181         const numeric denom() const;
182         int int_length() const;
183         // converting routines for interfacing with CLN:
184         explicit numeric(const cln::cl_N &z);
185
186 protected:
187         void print_numeric(const print_context & c, const char *par_open, const char *par_close, const char *imag_sym, const char *mul_sym, unsigned level) const;
188         void do_print(const print_context & c, unsigned level) const;
189         void do_print_latex(const print_latex & c, unsigned level) const;
190         void do_print_csrc(const print_csrc & c, unsigned level) const;
191         void do_print_csrc_cl_N(const print_csrc_cl_N & c, unsigned level) const;
192         void do_print_tree(const print_tree & c, unsigned level) const;
193         void do_print_python_repr(const print_python_repr & c, unsigned level) const;
194
195 // member variables
196
197 protected:
198         cln::cl_N value;
199 };
200 GINAC_DECLARE_UNARCHIVER(numeric); 
201
202
203 // global constants
204
205 extern const numeric I;
206 extern _numeric_digits Digits;
207
208 // global functions
209
210 const numeric exp(const numeric &x);
211 const numeric log(const numeric &x);
212 const numeric sin(const numeric &x);
213 const numeric cos(const numeric &x);
214 const numeric tan(const numeric &x);
215 const numeric asin(const numeric &x);
216 const numeric acos(const numeric &x);
217 const numeric atan(const numeric &x);
218 const numeric atan(const numeric &y, const numeric &x);
219 const numeric sinh(const numeric &x);
220 const numeric cosh(const numeric &x);
221 const numeric tanh(const numeric &x);
222 const numeric asinh(const numeric &x);
223 const numeric acosh(const numeric &x);
224 const numeric atanh(const numeric &x);
225 const numeric Li2(const numeric &x);
226 const numeric zeta(const numeric &x);
227 const numeric lgamma(const numeric &x);
228 const numeric tgamma(const numeric &x);
229 const numeric psi(const numeric &x);
230 const numeric psi(const numeric &n, const numeric &x);
231 const numeric factorial(const numeric &n);
232 const numeric doublefactorial(const numeric &n);
233 const numeric binomial(const numeric &n, const numeric &k);
234 const numeric bernoulli(const numeric &n);
235 const numeric fibonacci(const numeric &n);
236 const numeric isqrt(const numeric &x);
237 const numeric sqrt(const numeric &x);
238 const numeric abs(const numeric &x);
239 const numeric mod(const numeric &a, const numeric &b);
240 const numeric smod(const numeric &a, const numeric &b);
241 const numeric irem(const numeric &a, const numeric &b);
242 const numeric irem(const numeric &a, const numeric &b, numeric &q);
243 const numeric iquo(const numeric &a, const numeric &b);
244 const numeric iquo(const numeric &a, const numeric &b, numeric &r);
245 const numeric gcd(const numeric &a, const numeric &b);
246 const numeric lcm(const numeric &a, const numeric &b);
247
248 // wrapper functions around member functions
249 inline const numeric pow(const numeric &x, const numeric &y)
250 { return x.power(y); }
251
252 inline const numeric inverse(const numeric &x)
253 { return x.inverse(); }
254
255 inline numeric step(const numeric &x)
256 { return x.step(); }
257
258 inline int csgn(const numeric &x)
259 { return x.csgn(); }
260
261 inline bool is_zero(const numeric &x)
262 { return x.is_zero(); }
263
264 inline bool is_positive(const numeric &x)
265 { return x.is_positive(); }
266
267 inline bool is_negative(const numeric &x)
268 { return x.is_negative(); }
269
270 inline bool is_integer(const numeric &x)
271 { return x.is_integer(); }
272
273 inline bool is_pos_integer(const numeric &x)
274 { return x.is_pos_integer(); }
275
276 inline bool is_nonneg_integer(const numeric &x)
277 { return x.is_nonneg_integer(); }
278
279 inline bool is_even(const numeric &x)
280 { return x.is_even(); }
281
282 inline bool is_odd(const numeric &x)
283 { return x.is_odd(); }
284
285 inline bool is_prime(const numeric &x)
286 { return x.is_prime(); }
287
288 inline bool is_rational(const numeric &x)
289 { return x.is_rational(); }
290
291 inline bool is_real(const numeric &x)
292 { return x.is_real(); }
293
294 inline bool is_cinteger(const numeric &x)
295 { return x.is_cinteger(); }
296
297 inline bool is_crational(const numeric &x)
298 { return x.is_crational(); }
299
300 inline int to_int(const numeric &x)
301 { return x.to_int(); }
302
303 inline long to_long(const numeric &x)
304 { return x.to_long(); }
305
306 inline double to_double(const numeric &x)
307 { return x.to_double(); }
308
309 inline const numeric real(const numeric &x)
310 { return x.real(); }
311
312 inline const numeric imag(const numeric &x)
313 { return x.imag(); }
314
315 inline const numeric numer(const numeric &x)
316 { return x.numer(); }
317
318 inline const numeric denom(const numeric &x)
319 { return x.denom(); }
320
321 // numeric evaluation functions for class constant objects:
322
323 ex PiEvalf();
324 ex EulerEvalf();
325 ex CatalanEvalf();
326
327
328 } // namespace GiNaC
329
330 #endif // ndef GINAC_NUMERIC_H