]> www.ginac.de Git - ginac.git/blob - ginac/utils.h
* Added some long-pending docu.
[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 by functions to signal unimplemented functionality
40  *  so the expression may just be .hold() */
41 class dunno {};
42
43 /** Exception class thrown when a singularity is encountered. */
44 class pole_error : public std::domain_error {
45 public:
46         explicit pole_error(const std::string& what_arg, int degree);
47         int degree(void) const;
48 private:
49         int deg;
50 };
51
52 // some compilers (e.g. cygwin) define a macro log2, causing confusion
53 #ifndef log2
54 unsigned log2(unsigned n);
55 #endif
56
57 /** Compare two pointers (just to establish some sort of canonical order).
58  *  @return -1, 0, or 1 */
59 inline int compare_pointers(const void * a, const void * b)
60 {
61         if (a<b)
62                 return -1;
63         else if (a>b)
64                 return 1;
65         return 0;
66 }
67
68 /** Rotate lower 31 bits of unsigned value by one bit to the left
69  *  (upper bit gets cleared). */
70 inline unsigned rotate_left_31(unsigned n)
71 {
72         // clear highest bit and shift 1 bit to the left
73         n = (n & 0x7FFFFFFFU) << 1;
74         
75         // overflow? clear highest bit and set lowest bit
76         if (n & 0x80000000U)
77                 n = (n & 0x7FFFFFFFU) | 0x00000001U;
78         
79         GINAC_ASSERT(n<0x80000000U);
80         
81         return n;
82 }
83
84 /** Golden ratio hash function for the 31 least significant bits. */
85 inline unsigned golden_ratio_hash(unsigned n)
86 {
87         // This function requires arithmetic with at least 64 significant bits
88 #if SIZEOF_LONG >= 8
89         // So 'long' has 64 bits.  Excellent!  We prefer it because it might be
90         // more efficient than 'long long'.
91         unsigned long l = n * 0x4f1bbcddL;
92         return (l & 0x7fffffffU) ^ (l >> 32);
93 #elif SIZEOF_LONG_LONG >= 8
94         // This requires 'long long' (or an equivalent 64 bit type)---which is,
95         // unfortunately, not ANSI-C++-compliant.
96         // (Yet C99 demands it, which is reason for hope.)
97         unsigned long long l = n * 0x4f1bbcddL;
98         return (l & 0x7fffffffU) ^ (l >> 32);
99 #elif SIZEOF_LONG_DOUBLE > 8
100         // If 'long double' is bigger than 64 bits, we assume that the mantissa
101         // has at least 64 bits. This is not guaranteed but it's a good guess.
102         // Unfortunately, it may lead to horribly slow code.
103         const static long double golden_ratio = .618033988749894848204586834370;
104         long double m = golden_ratio * n;
105         return unsigned((m - int(m)) * 0x80000000);
106 #else
107 #error "No 64 bit data type. You lose."
108 #endif
109 }
110
111 /* Compute the sign of a permutation of a container, with and without an
112    explicitly supplied comparison function. If the sign returned is 1 or -1,
113    the container is sorted after the operation. */
114 template <class It>
115 int permutation_sign(It first, It last)
116 {
117         if (first == last)
118                 return 0;
119         --last;
120         if (first == last)
121                 return 0;
122         It flag = first;
123         int sign = 1;
124
125         do {
126                 It i = last, other = last;
127                 --other;
128                 bool swapped = false;
129                 while (i != first) {
130                         if (*i < *other) {
131                                 std::iter_swap(other, i);
132                                 flag = other;
133                                 swapped = true;
134                                 sign = -sign;
135                         } else if (!(*other < *i))
136                                 return 0;
137                         --i; --other;
138                 }
139                 if (!swapped)
140                         return sign;
141                 ++flag;
142                 if (flag == last)
143                         return sign;
144                 first = flag;
145                 i = first; other = first;
146                 ++other;
147                 swapped = false;
148                 while (i != last) {
149                         if (*other < *i) {
150                                 std::iter_swap(i, other);
151                                 flag = other;
152                                 swapped = true;
153                                 sign = -sign;
154                         } else if (!(*i < *other))
155                                 return 0;
156                         ++i; ++other;
157                 }
158                 if (!swapped)
159                         return sign;
160                 last = flag;
161                 --last;
162         } while (first != last);
163
164         return sign;
165 }
166
167 template <class It, class Cmp, class Swap>
168 int permutation_sign(It first, It last, Cmp comp, Swap swapit)
169 {
170         if (first == last)
171                 return 0;
172         --last;
173         if (first == last)
174                 return 0;
175         It flag = first;
176         int sign = 1;
177
178         do {
179                 It i = last, other = last;
180                 --other;
181                 bool swapped = false;
182                 while (i != first) {
183                         if (comp(*i, *other)) {
184                                 swapit(*other, *i);
185                                 flag = other;
186                                 swapped = true;
187                                 sign = -sign;
188                         } else if (!comp(*other, *i))
189                                 return 0;
190                         --i; --other;
191                 }
192                 if (!swapped)
193                         return sign;
194                 ++flag;
195                 if (flag == last)
196                         return sign;
197                 first = flag;
198                 i = first; other = first;
199                 ++other;
200                 swapped = false;
201                 while (i != last) {
202                         if (comp(*other, *i)) {
203                                 swapit(*i, *other);
204                                 flag = other;
205                                 swapped = true;
206                                 sign = -sign;
207                         } else if (!comp(*i, *other))
208                                 return 0;
209                         ++i; ++other;
210                 }
211                 if (!swapped)
212                         return sign;
213                 last = flag;
214                 --last;
215         } while (first != last);
216
217         return sign;
218 }
219
220 /* Implementation of shaker sort, only compares adjacent elements. */
221 template <class It, class Cmp, class Swap>
222 void shaker_sort(It first, It last, Cmp comp, Swap swapit)
223 {
224         if (first == last)
225                 return;
226         --last;
227         if (first == last)
228                 return;
229         It flag = first;
230
231         do {
232                 It i = last, other = last;
233                 --other;
234                 bool swapped = false;
235                 while (i != first) {
236                         if (comp(*i, *other)) {
237                                 swapit(*other, *i);
238                                 flag = other;
239                                 swapped = true;
240                         }
241                         --i; --other;
242                 }
243                 if (!swapped)
244                         return;
245                 ++flag;
246                 if (flag == last)
247                         return;
248                 first = flag;
249                 i = first; other = first;
250                 ++other;
251                 swapped = false;
252                 while (i != last) {
253                         if (comp(*other, *i)) {
254                                 swapit(*i, *other);
255                                 flag = other;
256                                 swapped = true;
257                         }
258                         ++i; ++other;
259                 }
260                 if (!swapped)
261                         return;
262                 last = flag;
263                 --last;
264         } while (first != last);
265 }
266
267 /* In-place cyclic permutation of a container (no copying, only swapping). */
268 template <class It, class Swap>
269 void cyclic_permutation(It first, It last, It new_first, Swap swapit)
270 {
271         unsigned num = last - first;
272 again:
273         if (first == new_first || num < 2)
274                 return;
275
276         unsigned num1 = new_first - first, num2 = last - new_first;
277         if (num1 >= num2) {
278                 It a = first, b = new_first;
279                 while (b != last) {
280                         swapit(*a, *b);
281                         ++a; ++b;
282                 }
283                 if (num1 > num2) {
284                         first += num2;
285                         num = num1;
286                         goto again;
287                 }
288         } else {
289                 It a = new_first, b = last;
290                 do {
291                         --a; --b;
292                         swapit(*a, *b);
293                 } while (a != first);
294                 last -= num1;
295                 num = num2;
296                 goto again;
297         }
298 }
299
300
301 // Collection of `construct on first use' wrappers for safely avoiding
302 // internal object replication without running into the `static
303 // initialization order fiasco'.  This chest of numbers helps speed up
304 // the library but should not be used outside it since it is
305 // potentially confusing.
306
307 class numeric;
308 class ex;
309
310 extern const numeric *_num_120_p;
311 extern const numeric &_num_120;
312 extern const ex _ex_120;
313 extern const numeric *_num_60_p;
314 extern const numeric &_num_60;
315 extern const ex _ex_60;
316 extern const numeric *_num_48_p;
317 extern const numeric &_num_48;
318 extern const ex _ex_48;
319 extern const numeric *_num_30_p;
320 extern const numeric &_num_30;
321 extern const ex _ex_30;
322 extern const numeric *_num_25_p;
323 extern const numeric &_num_25;
324 extern const ex _ex_25;
325 extern const numeric *_num_24_p;
326 extern const numeric &_num_24;
327 extern const ex _ex_24;
328 extern const numeric *_num_20_p;
329 extern const numeric &_num_20;
330 extern const ex _ex_20;
331 extern const numeric *_num_18_p;
332 extern const numeric &_num_18;
333 extern const ex _ex_18;
334 extern const numeric *_num_15_p;
335 extern const numeric &_num_15;
336 extern const ex _ex_15;
337 extern const numeric *_num_12_p;
338 extern const numeric &_num_12;
339 extern const ex _ex_12;
340 extern const numeric *_num_11_p;
341 extern const numeric &_num_11;
342 extern const ex _ex_11;
343 extern const numeric *_num_10_p;
344 extern const numeric &_num_10;
345 extern const ex _ex_10;
346 extern const numeric *_num_9_p;
347 extern const numeric &_num_9;
348 extern const ex _ex_9;
349 extern const numeric *_num_8_p;
350 extern const numeric &_num_8;
351 extern const ex _ex_8;
352 extern const numeric *_num_7_p;
353 extern const numeric &_num_7;
354 extern const ex _ex_7;
355 extern const numeric *_num_6_p;
356 extern const numeric &_num_6;
357 extern const ex _ex_6;
358 extern const numeric *_num_5_p;
359 extern const numeric &_num_5;
360 extern const ex _ex_5;
361 extern const numeric *_num_4_p;
362 extern const numeric &_num_4;
363 extern const ex _ex_4;
364 extern const numeric *_num_3_p;
365 extern const numeric &_num_3;
366 extern const ex _ex_3;
367 extern const numeric *_num_2_p;
368 extern const numeric &_num_2;
369 extern const ex _ex_2;
370 extern const numeric *_num_1_p;
371 extern const numeric &_num_1;
372 extern const ex _ex_1;
373 extern const numeric *_num_1_2_p;
374 extern const numeric &_num_1_2;
375 extern const ex _ex_1_2;
376 extern const numeric *_num_1_3_p;
377 extern const numeric &_num_1_3;
378 extern const ex _ex_1_3;
379 extern const numeric *_num_1_4_p;
380 extern const numeric &_num_1_4;
381 extern const ex _ex_1_4;
382 extern const numeric *_num0_p;
383 extern const numeric &_num0;
384 extern const ex _ex0;
385 extern const numeric *_num1_4_p;
386 extern const numeric &_num1_4;
387 extern const ex _ex1_4;
388 extern const numeric *_num1_3_p;
389 extern const numeric &_num1_3;
390 extern const ex _ex1_3;
391 extern const numeric *_num1_2_p;
392 extern const numeric &_num1_2;
393 extern const ex _ex1_2;
394 extern const numeric *_num1_p;
395 extern const numeric &_num1;
396 extern const ex _ex1;
397 extern const numeric *_num2_p;
398 extern const numeric &_num2;
399 extern const ex _ex2;
400 extern const numeric *_num3_p;
401 extern const numeric &_num3;
402 extern const ex _ex3;
403 extern const numeric *_num4_p;
404 extern const numeric &_num4;
405 extern const ex _ex4;
406 extern const numeric *_num5_p;
407 extern const numeric &_num5;
408 extern const ex _ex5;
409 extern const numeric *_num6_p;
410 extern const numeric &_num6;
411 extern const ex _ex6;
412 extern const numeric *_num7_p;
413 extern const numeric &_num7;
414 extern const ex _ex7;
415 extern const numeric *_num8_p;
416 extern const numeric &_num8;
417 extern const ex _ex8;
418 extern const numeric *_num9_p;
419 extern const numeric &_num9;
420 extern const ex _ex9;
421 extern const numeric *_num10_p;
422 extern const numeric &_num10;
423 extern const ex _ex10;
424 extern const numeric *_num11_p;
425 extern const numeric &_num11;
426 extern const ex _ex11;
427 extern const numeric *_num12_p;
428 extern const numeric &_num12;
429 extern const ex _ex12;
430 extern const numeric *_num15_p;
431 extern const numeric &_num15;
432 extern const ex _ex15;
433 extern const numeric *_num18_p;
434 extern const numeric &_num18;
435 extern const ex _ex18;
436 extern const numeric *_num20_p;
437 extern const numeric &_num20;
438 extern const ex _ex20;
439 extern const numeric *_num24_p;
440 extern const numeric &_num24;
441 extern const ex _ex24;
442 extern const numeric *_num25_p;
443 extern const numeric &_num25;
444 extern const ex _ex25;
445 extern const numeric *_num30_p;
446 extern const numeric &_num30;
447 extern const ex _ex30;
448 extern const numeric *_num48_p;
449 extern const numeric &_num48;
450 extern const ex _ex48;
451 extern const numeric *_num60_p;
452 extern const numeric &_num60;
453 extern const ex _ex60;
454 extern const numeric *_num120_p;
455 extern const numeric &_num120;
456 extern const ex _ex120;
457
458
459 // Helper macros for class implementations (mostly useful for trivial classes)
460
461 #define DEFAULT_COPY(classname) \
462 void classname::copy(const classname & other) \
463 { \
464         inherited::copy(other); \
465 }
466
467 #define DEFAULT_DESTROY(classname) \
468 void classname::destroy(bool call_parent) \
469 { \
470         if (call_parent) \
471                 inherited::destroy(call_parent); \
472 }
473
474 #define DEFAULT_CTORS(classname) \
475 classname::classname() : inherited(TINFO_##classname) {} \
476 DEFAULT_COPY(classname) \
477 DEFAULT_DESTROY(classname)
478
479 #define DEFAULT_UNARCHIVE(classname) \
480 ex classname::unarchive(const archive_node &n, const lst &sym_lst) \
481 { \
482         return (new classname(n, sym_lst))->setflag(status_flags::dynallocated); \
483 }
484
485 #define DEFAULT_ARCHIVING(classname) \
486 classname::classname(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst) {} \
487 DEFAULT_UNARCHIVE(classname) \
488 void classname::archive(archive_node &n) const \
489 { \
490         inherited::archive(n); \
491 }
492
493 #define DEFAULT_COMPARE(classname) \
494 int classname::compare_same_type(const basic & other) const \
495 { \
496         /* by default, the objects are always identical */ \
497         return 0; \
498 }
499
500 #define DEFAULT_PRINT(classname, text) \
501 void classname::print(const print_context & c, unsigned level) const \
502 { \
503         if (is_a<print_tree>(c)) \
504                 inherited::print(c, level); \
505         else \
506                 c.s << text; \
507 }
508
509 #define DEFAULT_PRINT_LATEX(classname, text, latex) \
510 void classname::print(const print_context & c, unsigned level) const \
511 { \
512         if (is_a<print_tree>(c)) \
513                 inherited::print(c, level); \
514         else if (is_a<print_latex>(c)) \
515                 c.s << latex; \
516         else \
517                 c.s << text; \
518 }
519
520 // Obsolete convenience macros.  TO BE PHASED OUT SOON!
521 // Use the inlined template functions in basic.h instead.  (FIXME: remove them)
522
523 #define is_of_type(OBJ,TYPE) \
524         (dynamic_cast<const TYPE *>(&OBJ)!=0)
525
526 #define is_exactly_of_type(OBJ,TYPE) \
527         ((OBJ).tinfo()==GiNaC::TINFO_##TYPE)
528
529 #define is_ex_of_type(OBJ,TYPE) \
530         (dynamic_cast<const TYPE *>((OBJ).bp)!=0)
531
532 #define is_ex_exactly_of_type(OBJ,TYPE) \
533         ((*(OBJ).bp).tinfo()==GiNaC::TINFO_##TYPE)
534
535 } // namespace GiNaC
536
537
538 #endif // ndef __GINAC_UTILS_H__