]> www.ginac.de Git - ginac.git/blob - ginac/utils.h
- in LaTeX context, fractions now print as \frac{n}{d}.
[ginac.git] / ginac / utils.h
1 /** @file utils.h
2  *
3  *  Interface to several small and furry utilities needed within GiNaC but not
4  *  of any interest to the user of the library. */
5
6 /*
7  *  GiNaC Copyright (C) 1999-2001 Johannes Gutenberg University Mainz, Germany
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or
12  *  (at your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, write to the Free Software
21  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23
24 #ifndef __GINAC_UTILS_H__
25 #define __GINAC_UTILS_H__
26
27 #include "config.h"
28
29 #include <string>
30 #include <stdexcept>
31 #include "assertion.h"
32
33 namespace GiNaC {
34
35 /** Exception class thrown by classes which provide their own series expansion
36  *  to signal that ordinary Taylor expansion is safe. */
37 class do_taylor {};
38
39 /** Exception class thrown when a singularity is encountered. */
40 class pole_error : public std::domain_error {
41 public:
42         explicit pole_error(const std::string& what_arg, int degree);
43         int degree(void) const;
44 private:
45         int deg;
46 };
47
48 // some compilers (e.g. cygwin) define a macro log2, causing confusion
49 #ifndef log2
50 unsigned log2(unsigned n);
51 #endif
52
53 /** Compare two pointers (just to establish some sort of canonical order).
54  *  @return -1, 0, or 1 */
55 inline int compare_pointers(const void * a, const void * b)
56 {
57         if (a<b)
58                 return -1;
59         else if (a>b)
60                 return 1;
61         return 0;
62 }
63
64 /** Rotate lower 31 bits of unsigned value by one bit to the left
65  *  (upper bit gets cleared). */
66 inline unsigned rotate_left_31(unsigned n)
67 {
68         // clear highest bit and shift 1 bit to the left
69         n = (n & 0x7FFFFFFFU) << 1;
70         
71         // overflow? clear highest bit and set lowest bit
72         if (n & 0x80000000U)
73                 n = (n & 0x7FFFFFFFU) | 0x00000001U;
74         
75         GINAC_ASSERT(n<0x80000000U);
76         
77         return n;
78 }
79
80 /** Golden ratio hash function for the 31 least significant bits. */
81 inline unsigned golden_ratio_hash(unsigned n)
82 {
83         // This function requires arithmetic with at least 64 significant bits
84 #if SIZEOF_LONG >= 8
85         // So 'long' has 64 bits.  Excellent!  We prefer it because it might be
86         // more efficient than 'long long'.
87         unsigned long l = n * 0x4f1bbcddL;
88         return (l & 0x7fffffffU) ^ (l >> 32);
89 #elif SIZEOF_LONG_LONG >= 8
90         // This requires 'long long' (or an equivalent 64 bit type)---which is,
91         // unfortunately, not ANSI-C++-compliant.
92         // (Yet C99 demands it, which is reason for hope.)
93         unsigned long long l = n * 0x4f1bbcddL;
94         return (l & 0x7fffffffU) ^ (l >> 32);
95 #elif SIZEOF_LONG_DOUBLE > 8
96         // If 'long double' is bigger than 64 bits, we assume that the mantissa
97         // has at least 64 bits. This is not guaranteed but it's a good guess.
98         // Unfortunately, it may lead to horribly slow code.
99         const static long double golden_ratio = .618033988749894848204586834370;
100         long double m = golden_ratio * n;
101         return unsigned((m - int(m)) * 0x80000000);
102 #else
103 #error "No 64 bit data type. You lose."
104 #endif
105 }
106
107 /* Compute the sign of a permutation of a container, with and without an
108    explicitly supplied comparison function. If the sign returned is 1 or -1,
109    the container is sorted after the operation. */
110 template <class It>
111 int permutation_sign(It first, It last)
112 {
113         if (first == last)
114                 return 0;
115         --last;
116         if (first == last)
117                 return 0;
118         It flag = first;
119         int sign = 1;
120
121         do {
122                 It i = last, other = last;
123                 --other;
124                 bool swapped = false;
125                 while (i != first) {
126                         if (*i < *other) {
127                                 std::iter_swap(other, i);
128                                 flag = other;
129                                 swapped = true;
130                                 sign = -sign;
131                         } else if (!(*other < *i))
132                                 return 0;
133                         --i; --other;
134                 }
135                 if (!swapped)
136                         return sign;
137                 ++flag;
138                 if (flag == last)
139                         return sign;
140                 first = flag;
141                 i = first; other = first;
142                 ++other;
143                 swapped = false;
144                 while (i != last) {
145                         if (*other < *i) {
146                                 std::iter_swap(i, other);
147                                 flag = other;
148                                 swapped = true;
149                                 sign = -sign;
150                         } else if (!(*i < *other))
151                                 return 0;
152                         ++i; ++other;
153                 }
154                 if (!swapped)
155                         return sign;
156                 last = flag;
157                 --last;
158         } while (first != last);
159
160         return sign;
161 }
162
163 template <class It, class Cmp, class Swap>
164 int permutation_sign(It first, It last, Cmp comp, Swap swapit)
165 {
166         if (first == last)
167                 return 0;
168         --last;
169         if (first == last)
170                 return 0;
171         It flag = first;
172         int sign = 1;
173
174         do {
175                 It i = last, other = last;
176                 --other;
177                 bool swapped = false;
178                 while (i != first) {
179                         if (comp(*i, *other)) {
180                                 swapit(*other, *i);
181                                 flag = other;
182                                 swapped = true;
183                                 sign = -sign;
184                         } else if (!comp(*other, *i))
185                                 return 0;
186                         --i; --other;
187                 }
188                 if (!swapped)
189                         return sign;
190                 ++flag;
191                 if (flag == last)
192                         return sign;
193                 first = flag;
194                 i = first; other = first;
195                 ++other;
196                 swapped = false;
197                 while (i != last) {
198                         if (comp(*other, *i)) {
199                                 swapit(*i, *other);
200                                 flag = other;
201                                 swapped = true;
202                                 sign = -sign;
203                         } else if (!comp(*i, *other))
204                                 return 0;
205                         ++i; ++other;
206                 }
207                 if (!swapped)
208                         return sign;
209                 last = flag;
210                 --last;
211         } while (first != last);
212
213         return sign;
214 }
215
216 /* Implementation of shaker sort, only compares adjacent elements. */
217 template <class It, class Cmp, class Swap>
218 void shaker_sort(It first, It last, Cmp comp, Swap swapit)
219 {
220         if (first == last)
221                 return;
222         --last;
223         if (first == last)
224                 return;
225         It flag = first;
226
227         do {
228                 It i = last, other = last;
229                 --other;
230                 bool swapped = false;
231                 while (i != first) {
232                         if (comp(*i, *other)) {
233                                 swapit(*other, *i);
234                                 flag = other;
235                                 swapped = true;
236                         }
237                         --i; --other;
238                 }
239                 if (!swapped)
240                         return;
241                 ++flag;
242                 if (flag == last)
243                         return;
244                 first = flag;
245                 i = first; other = first;
246                 ++other;
247                 swapped = false;
248                 while (i != last) {
249                         if (comp(*other, *i)) {
250                                 swapit(*i, *other);
251                                 flag = other;
252                                 swapped = true;
253                         }
254                         ++i; ++other;
255                 }
256                 if (!swapped)
257                         return;
258                 last = flag;
259                 --last;
260         } while (first != last);
261 }
262
263 /* In-place cyclic permutation of a container (no copying, only swapping). */
264 template <class It, class Swap>
265 void cyclic_permutation(It first, It last, It new_first, Swap swapit)
266 {
267         unsigned num = last - first;
268 again:
269         if (first == new_first || num < 2)
270                 return;
271
272         unsigned num1 = new_first - first, num2 = last - new_first;
273         if (num1 >= num2) {
274                 It a = first, b = new_first;
275                 while (b != last) {
276                         swapit(*a, *b);
277                         ++a; ++b;
278                 }
279                 if (num1 > num2) {
280                         first += num2;
281                         num = num1;
282                         goto again;
283                 }
284         } else {
285                 It a = new_first, b = last;
286                 do {
287                         --a; --b;
288                         swapit(*a, *b);
289                 } while (a != first);
290                 last -= num1;
291                 num = num2;
292                 goto again;
293         }
294 }
295
296
297 // Collection of `construct on first use' wrappers for safely avoiding
298 // internal object replication without running into the `static
299 // initialization order fiasco'.  This chest of numbers helps speed up
300 // the library but should not be used outside it since it is
301 // potentially confusing.
302
303 class numeric;
304 class ex;
305
306 const numeric & _num_120(void);   // -120
307 const ex & _ex_120(void);
308 const numeric & _num_60(void);    // -60
309 const ex & _ex_60(void);
310 const numeric & _num_48(void);    // -48
311 const ex & _ex_48(void);
312 const numeric & _num_30(void);    // -30
313 const ex & _ex_30(void);
314 const numeric & _num_25(void);    // -25
315 const ex & _ex_25(void);
316 const numeric & _num_24(void);    // -24
317 const ex & _ex_24(void);
318 const numeric & _num_20(void);    // -20
319 const ex & _ex_20(void);
320 const numeric & _num_18(void);    // -18
321 const ex & _ex_18(void);
322 const numeric & _num_15(void);    // -15
323 const ex & _ex_15(void);
324 const numeric & _num_12(void);    // -12
325 const ex & _ex_12(void);
326 const numeric & _num_11(void);    // -11
327 const ex & _ex_11(void);
328 const numeric & _num_10(void);    // -10
329 const ex & _ex_10(void);
330 const numeric & _num_9(void);     // -9
331 const ex & _ex_9(void);
332 const numeric & _num_8(void);     // -8
333 const ex & _ex_8(void);
334 const numeric & _num_7(void);     // -7
335 const ex & _ex_7(void);
336 const numeric & _num_6(void);     // -6
337 const ex & _ex_6(void);
338 const numeric & _num_5(void);     // -5
339 const ex & _ex_5(void);
340 const numeric & _num_4(void);     // -4
341 const ex & _ex_4(void);
342 const numeric & _num_3(void);     // -3
343 const ex & _ex_3(void);
344 const numeric & _num_2(void);     // -2
345 const ex & _ex_2(void);
346 const numeric & _num_1(void);     // -1
347 const ex & _ex_1(void);
348 const numeric & _num_1_2(void);   // -1/2
349 const ex & _ex_1_2(void);
350 const numeric & _num_1_3(void);   // -1/3
351 const ex & _ex_1_3(void);
352 const numeric & _num_1_4(void);   // -1/4
353 const ex & _ex_1_4(void);
354 const numeric & _num0(void);      //  0
355 const ex & _ex0(void);
356 const numeric & _num1_4(void);    //  1/4
357 const ex & _ex1_4(void);
358 const numeric & _num1_3(void);    //  1/3
359 const ex & _ex1_3(void);
360 const numeric & _num1_2(void);    //  1/2
361 const ex & _ex1_2(void);
362 const numeric & _num1(void);      //  1
363 const ex & _ex1(void);
364 const numeric & _num2(void);      //  2
365 const ex & _ex2(void);
366 const numeric & _num3(void);      //  3
367 const ex & _ex3(void);
368 const numeric & _num4(void);      //  4
369 const ex & _ex4(void);
370 const numeric & _num5(void);      //  5
371 const ex & _ex5(void);
372 const numeric & _num6(void);      //  6
373 const ex & _ex6(void);
374 const numeric & _num7(void);      //  7
375 const ex & _ex7(void);
376 const numeric & _num8(void);      //  8
377 const ex & _ex8(void);
378 const numeric & _num9(void);      //  9
379 const ex & _ex9(void);
380 const numeric & _num10(void);     //  10
381 const ex & _ex10(void);
382 const numeric & _num11(void);     //  11
383 const ex & _ex11(void);
384 const numeric & _num12(void);     //  12
385 const ex & _ex12(void);
386 const numeric & _num15(void);     //  15
387 const ex & _ex15(void);
388 const numeric & _num18(void);     //  18
389 const ex & _ex18(void);
390 const numeric & _num20(void);     //  20
391 const ex & _ex20(void);
392 const numeric & _num24(void);     //  24
393 const ex & _ex24(void);
394 const numeric & _num25(void);     //  25
395 const ex & _ex25(void);
396 const numeric & _num30(void);     //  30
397 const ex & _ex30(void);
398 const numeric & _num48(void);     //  48
399 const ex & _ex48(void);
400 const numeric & _num60(void);     //  60
401 const ex & _ex60(void);
402 const numeric & _num120(void);    //  120
403 const ex & _ex120(void);
404
405
406 // Helper macros for class implementations (mostly useful for trivial classes)
407
408 #define DEFAULT_COPY(classname) \
409 void classname::copy(const classname & other) \
410 { \
411         inherited::copy(other); \
412 }
413
414 #define DEFAULT_DESTROY(classname) \
415 void classname::destroy(bool call_parent) \
416 { \
417         if (call_parent) \
418                 inherited::destroy(call_parent); \
419 }
420
421 #define DEFAULT_CTORS(classname) \
422 classname::classname() : inherited(TINFO_##classname) \
423 { \
424         debugmsg(#classname " default constructor", LOGLEVEL_CONSTRUCT); \
425 } \
426 DEFAULT_COPY(classname) \
427 DEFAULT_DESTROY(classname)
428
429 #define DEFAULT_UNARCHIVE(classname) \
430 ex classname::unarchive(const archive_node &n, const lst &sym_lst) \
431 { \
432         return (new classname(n, sym_lst))->setflag(status_flags::dynallocated); \
433 }
434
435 #define DEFAULT_ARCHIVING(classname) \
436 classname::classname(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst) \
437 { \
438         debugmsg(#classname " constructor from archive_node", LOGLEVEL_CONSTRUCT); \
439 } \
440 DEFAULT_UNARCHIVE(classname) \
441 void classname::archive(archive_node &n) const \
442 { \
443         inherited::archive(n); \
444 }
445
446 #define DEFAULT_COMPARE(classname) \
447 int classname::compare_same_type(const basic & other) const \
448 { \
449         /* by default, the objects are always identical */ \
450         return 0; \
451 }
452
453 #define DEFAULT_PRINT(classname, text) \
454 void classname::print(const print_context & c, unsigned level) const \
455 { \
456         debugmsg(#classname " print", LOGLEVEL_PRINT); \
457         if (is_a<print_tree>(c)) \
458                 inherited::print(c, level); \
459         else \
460                 c.s << text; \
461 }
462
463 #define DEFAULT_PRINT_LATEX(classname, text, latex) \
464 void classname::print(const print_context & c, unsigned level) const \
465 { \
466         debugmsg(#classname " print", LOGLEVEL_PRINT); \
467         if (is_a<print_tree>(c)) \
468                 inherited::print(c, level); \
469         else if (is_a<print_latex>(c)) \
470                 c.s << latex; \
471         else \
472                 c.s << text; \
473 }
474
475 } // namespace GiNaC
476
477 #endif // ndef __GINAC_UTILS_H__