]> www.ginac.de Git - ginac.git/blob - ginac/utils.h
- added symmetrize() and antisymmetrize() functions
[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 <functional>
32 #if defined(HAVE_SSTREAM)
33 #include <sstream>
34 #elif defined(HAVE_STRSTREAM)
35 #include <strstream>
36 #else
37 #error Need either sstream or strstream
38 #endif
39 #include "assertion.h"
40
41 namespace GiNaC {
42
43 // This should be obsoleted once <sstream> is widely deployed.
44 template<class T>
45 std::string ToString(const T & t)
46 {
47 #if defined(HAVE_SSTREAM)
48         std::ostringstream buf;
49         buf << t << std::ends;
50         return buf.str();
51 #else
52         char buf[256];
53         std::ostrstream(buf,sizeof(buf)) << t << std::ends;
54         return buf;
55 #endif
56 }
57
58 /** Exception class thrown by classes which provide their own series expansion
59  *  to signal that ordinary Taylor expansion is safe. */
60 class do_taylor {};
61
62 /** Exception class thrown when a singularity is encountered. */
63 class pole_error : public std::domain_error {
64 public:
65         explicit pole_error(const std::string& what_arg, int degree);
66         int degree(void) const;
67 private:
68         int deg;
69 };
70
71 // some compilers (e.g. cygwin) define a macro log2, causing confusion
72 #ifndef log2
73 unsigned log2(unsigned n);
74 #endif
75
76 /** Compare two pointers (just to establish some sort of canonical order).
77  *  @return -1, 0, or 1 */
78 inline int compare_pointers(const void * a, const void * b)
79 {
80         if (a<b)
81                 return -1;
82         else if (a>b)
83                 return 1;
84         return 0;
85 }
86
87 /** Rotate lower 31 bits of unsigned value by one bit to the left
88  *  (upper bit gets cleared). */
89 inline unsigned rotate_left_31(unsigned n)
90 {
91         // clear highest bit and shift 1 bit to the left
92         n = (n & 0x7FFFFFFFU) << 1;
93         
94         // overflow? clear highest bit and set lowest bit
95         if (n & 0x80000000U)
96                 n = (n & 0x7FFFFFFFU) | 0x00000001U;
97         
98         GINAC_ASSERT(n<0x80000000U);
99         
100         return n;
101 }
102
103 /** Golden ratio hash function for the 31 least significant bits. */
104 inline unsigned golden_ratio_hash(unsigned n)
105 {
106         // This function requires arithmetic with at least 64 significant bits
107 #if SIZEOF_LONG >= 8
108         // So 'long' has 64 bits.  Excellent!  We prefer it because it might be
109         // more efficient than 'long long'.
110         unsigned long l = n * 0x4f1bbcddL;
111         return (l & 0x7fffffffU) ^ (l >> 32);
112 #elif SIZEOF_LONG_LONG >= 8
113         // This requires 'long long' (or an equivalent 64 bit type)---which is,
114         // unfortunately, not ANSI-C++-compliant.
115         // (Yet C99 demands it, which is reason for hope.)
116         unsigned long long l = n * 0x4f1bbcddL;
117         return (l & 0x7fffffffU) ^ (l >> 32);
118 #elif SIZEOF_LONG_DOUBLE > 8
119         // If 'long double' is bigger than 64 bits, we assume that the mantissa
120         // has at least 64 bits. This is not guaranteed but it's a good guess.
121         // Unfortunately, it may lead to horribly slow code.
122         const static long double golden_ratio = .618033988749894848204586834370;
123         long double m = golden_ratio * n;
124         return unsigned((m - int(m)) * 0x80000000);
125 #else
126 #error "No 64 bit data type. You lose."
127 #endif
128 }
129
130 /* Compute the sign of a permutation of a container, with and without an
131    explicitly supplied comparison function. The containers gets modified
132    during the operation. */
133 template <class It>
134 int permutation_sign(It first, It last)
135 {
136         if (first == last)
137                 return 0;
138         It i = first;
139         ++i;
140         if (i == last)
141                 return 0;
142         i = first;
143         It next_to_last = last;
144         --next_to_last;
145
146         int sign = 1;
147         while (i != next_to_last) {
148                 It j = i;
149                 ++j;
150                 while (j != last) {
151                         if (!(*i < *j)) {
152                                 if (!(*j < *i))
153                                         return 0;
154                                 iter_swap(i, j);
155                                 sign = -sign;
156                         }
157                         ++j;
158                 }
159                 ++i;
160         }
161         return sign;
162 }
163
164 /** Compute the sign of a permutation of a container */
165 template <class It, class Cmp>
166 int permutation_sign(It first, It last, Cmp comp)
167 {
168         if (first == last)
169                 return 0;
170         It i = first;
171         ++i;
172         if (i == last)
173                 return 0;
174         i = first;
175         It next_to_last = last;
176         --next_to_last;
177
178         int sign = 1;
179         while (i != next_to_last) {
180                 It j = i;
181                 ++j;
182                 while (j != last) {
183                         if (!comp(*i, *j)) {
184                                 if (!comp(*j, *i))
185                                         return 0;
186                                 iter_swap(i, j);
187                                 sign = -sign;
188                         }
189                         ++j;
190                 }
191                 ++i;
192         }
193         return sign;
194 }
195
196 /* Implementation of shaker sort, only compares adjacent elements. */
197 template <class It, class Cmp>
198 inline void shaker_sort(It first, It last, Cmp comp)
199 {
200         if (first == last)
201                 return;
202         --last;
203         if (first == last)
204                 return;
205         It flag = first;
206         do {
207                 It i = last, other = last;
208                 --other;
209                 while (i > first) {
210                         if (comp(*i, *other)) {
211                                 iter_swap(other, i);
212                                 flag = other;
213                         }
214                         --i; --other;
215                 }
216                 ++flag;
217                 first = flag;
218                 i = first; other = first;
219                 ++other;
220                 while (i < last) {
221                         if (comp(*other, *i)) {
222                                 iter_swap(i, other);
223                                 flag = other;
224                         }
225                         ++i; ++other;
226                 }
227                 last = flag;
228                 --last;
229         } while (first <= last);
230 }
231
232 /* Function objects for STL sort() etc. */
233 struct ex_is_less : public binary_function<ex, ex, bool> {
234         bool operator() (const ex &lh, const ex &rh) const { return lh.compare(rh) < 0; }
235 };
236
237 struct ex_is_equal : public binary_function<ex, ex, bool> {
238         bool operator() (const ex &lh, const ex &rh) const { return lh.is_equal(rh); }
239 };
240
241 // Collection of `construct on first use' wrappers for safely avoiding
242 // internal object replication without running into the `static
243 // initialization order fiasco'.  This chest of numbers helps speed up
244 // the library but should not be used outside it since it is
245 // potentially confusing.
246
247 class numeric;
248 class ex;
249
250 const numeric & _num_120(void);   // -120
251 const ex & _ex_120(void);
252 const numeric & _num_60(void);    // -60
253 const ex & _ex_60(void);
254 const numeric & _num_48(void);    // -48
255 const ex & _ex_48(void);
256 const numeric & _num_30(void);    // -30
257 const ex & _ex_30(void);
258 const numeric & _num_25(void);    // -25
259 const ex & _ex_25(void);
260 const numeric & _num_24(void);    // -24
261 const ex & _ex_24(void);
262 const numeric & _num_20(void);    // -20
263 const ex & _ex_20(void);
264 const numeric & _num_18(void);    // -18
265 const ex & _ex_18(void);
266 const numeric & _num_15(void);    // -15
267 const ex & _ex_15(void);
268 const numeric & _num_12(void);    // -12
269 const ex & _ex_12(void);
270 const numeric & _num_11(void);    // -11
271 const ex & _ex_11(void);
272 const numeric & _num_10(void);    // -10
273 const ex & _ex_10(void);
274 const numeric & _num_9(void);     // -9
275 const ex & _ex_9(void);
276 const numeric & _num_8(void);     // -8
277 const ex & _ex_8(void);
278 const numeric & _num_7(void);     // -7
279 const ex & _ex_7(void);
280 const numeric & _num_6(void);     // -6
281 const ex & _ex_6(void);
282 const numeric & _num_5(void);     // -5
283 const ex & _ex_5(void);
284 const numeric & _num_4(void);     // -4
285 const ex & _ex_4(void);
286 const numeric & _num_3(void);     // -3
287 const ex & _ex_3(void);
288 const numeric & _num_2(void);     // -2
289 const ex & _ex_2(void);
290 const numeric & _num_1(void);     // -1
291 const ex & _ex_1(void);
292 const numeric & _num_1_2(void);   // -1/2
293 const ex & _ex_1_2(void);
294 const numeric & _num_1_3(void);   // -1/3
295 const ex & _ex_1_3(void);
296 const numeric & _num_1_4(void);   // -1/4
297 const ex & _ex_1_4(void);
298 const numeric & _num0(void);      //  0
299 const ex & _ex0(void);
300 const numeric & _num1_4(void);    //  1/4
301 const ex & _ex1_4(void);
302 const numeric & _num1_3(void);    //  1/3
303 const ex & _ex1_3(void);
304 const numeric & _num1_2(void);    //  1/2
305 const ex & _ex1_2(void);
306 const numeric & _num1(void);      //  1
307 const ex & _ex1(void);
308 const numeric & _num2(void);      //  2
309 const ex & _ex2(void);
310 const numeric & _num3(void);      //  3
311 const ex & _ex3(void);
312 const numeric & _num4(void);      //  4
313 const ex & _ex4(void);
314 const numeric & _num5(void);      //  5
315 const ex & _ex5(void);
316 const numeric & _num6(void);      //  6
317 const ex & _ex6(void);
318 const numeric & _num7(void);      //  7
319 const ex & _ex7(void);
320 const numeric & _num8(void);      //  8
321 const ex & _ex8(void);
322 const numeric & _num9(void);      //  9
323 const ex & _ex9(void);
324 const numeric & _num10(void);     //  10
325 const ex & _ex10(void);
326 const numeric & _num11(void);     //  11
327 const ex & _ex11(void);
328 const numeric & _num12(void);     //  12
329 const ex & _ex12(void);
330 const numeric & _num15(void);     //  15
331 const ex & _ex15(void);
332 const numeric & _num18(void);     //  18
333 const ex & _ex18(void);
334 const numeric & _num20(void);     //  20
335 const ex & _ex20(void);
336 const numeric & _num24(void);     //  24
337 const ex & _ex24(void);
338 const numeric & _num25(void);     //  25
339 const ex & _ex25(void);
340 const numeric & _num30(void);     //  30
341 const ex & _ex30(void);
342 const numeric & _num48(void);     //  48
343 const ex & _ex48(void);
344 const numeric & _num60(void);     //  60
345 const ex & _ex60(void);
346 const numeric & _num120(void);    //  120
347 const ex & _ex120(void);
348
349
350 // Helper macros for class implementations (mostly useful for trivial classes)
351
352 #define DEFAULT_COPY(classname) \
353 void classname::copy(const classname & other) \
354 { \
355         inherited::copy(other); \
356 }
357
358 #define DEFAULT_DESTROY(classname) \
359 void classname::destroy(bool call_parent) \
360 { \
361         if (call_parent) \
362                 inherited::destroy(call_parent); \
363 }
364
365 #define DEFAULT_CTORS(classname) \
366 classname::classname() : inherited(TINFO_##classname) \
367 { \
368         debugmsg(#classname " default constructor", LOGLEVEL_CONSTRUCT); \
369 } \
370 DEFAULT_COPY(classname) \
371 DEFAULT_DESTROY(classname)
372
373 #define DEFAULT_UNARCHIVE(classname) \
374 ex classname::unarchive(const archive_node &n, const lst &sym_lst) \
375 { \
376         return (new classname(n, sym_lst))->setflag(status_flags::dynallocated); \
377 }
378
379 #define DEFAULT_ARCHIVING(classname) \
380 classname::classname(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst) \
381 { \
382         debugmsg(#classname " constructor from archive_node", LOGLEVEL_CONSTRUCT); \
383 } \
384 DEFAULT_UNARCHIVE(classname) \
385 void classname::archive(archive_node &n) const \
386 { \
387         inherited::archive(n); \
388 }
389
390 #define DEFAULT_COMPARE(classname) \
391 int classname::compare_same_type(const basic & other) const \
392 { \
393         /* by default, the objects are always identical */ \
394         return 0; \
395 }
396
397 #define DEFAULT_PRINT(classname, text) \
398 void classname::print(const print_context & c, unsigned level) const \
399 { \
400         debugmsg(#classname " print", LOGLEVEL_PRINT); \
401         if (is_of_type(c, print_tree)) \
402                 inherited::print(c, level); \
403         else \
404                 c.s << text; \
405 }
406
407 #define DEFAULT_PRINT_LATEX(classname, text, latex) \
408 void classname::print(const print_context & c, unsigned level) const \
409 { \
410         debugmsg(#classname " print", LOGLEVEL_PRINT); \
411         if (is_of_type(c, print_tree)) \
412                 inherited::print(c, level); \
413         else if (is_of_type(c, print_latex)) \
414                 c.s << latex; \
415         else \
416                 c.s << text; \
417 }
418
419 } // namespace GiNaC
420
421 #endif // ndef __GINAC_UTILS_H__