]> www.ginac.de Git - ginac.git/blob - ginac/utils.h
- Clifford traces of many gammas are a lot faster now (especially with gamma5)
[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 vector of things.
131 template <typename T>
132 int permutation_sign(std::vector<T> s)
133 {
134         if (s.size() < 2)
135                 return 0;
136         int sigma = 1;
137         for (typename std::vector<T>::iterator i=s.begin(); i!=s.end()-1; ++i) {
138                 for (typename std::vector<T>::iterator j=i+1; j!=s.end(); ++j) {
139                         if (*i == *j)
140                                 return 0;
141                         if (*i > *j) {
142                                 iter_swap(i,j);
143                                 sigma = -sigma;
144                         }
145                 }
146         }
147         return sigma;
148 }
149
150 /* Function objects for STL sort() etc. */
151 struct ex_is_less : public binary_function<ex, ex, bool> {
152         bool operator() (const ex &lh, const ex &rh) const { return lh.compare(rh) < 0; }
153 };
154
155 struct ex_is_equal : public binary_function<ex, ex, bool> {
156         bool operator() (const ex &lh, const ex &rh) const { return lh.is_equal(rh); }
157 };
158
159 // Collection of `construct on first use' wrappers for safely avoiding
160 // internal object replication without running into the `static
161 // initialization order fiasco'.  This chest of numbers helps speed up
162 // the library but should not be used outside it since it is
163 // potentially confusing.
164
165 class numeric;
166 class ex;
167
168 const numeric & _num_120(void);   // -120
169 const ex & _ex_120(void);
170 const numeric & _num_60(void);    // -60
171 const ex & _ex_60(void);
172 const numeric & _num_48(void);    // -48
173 const ex & _ex_48(void);
174 const numeric & _num_30(void);    // -30
175 const ex & _ex_30(void);
176 const numeric & _num_25(void);    // -25
177 const ex & _ex_25(void);
178 const numeric & _num_24(void);    // -24
179 const ex & _ex_24(void);
180 const numeric & _num_20(void);    // -20
181 const ex & _ex_20(void);
182 const numeric & _num_18(void);    // -18
183 const ex & _ex_18(void);
184 const numeric & _num_15(void);    // -15
185 const ex & _ex_15(void);
186 const numeric & _num_12(void);    // -12
187 const ex & _ex_12(void);
188 const numeric & _num_11(void);    // -11
189 const ex & _ex_11(void);
190 const numeric & _num_10(void);    // -10
191 const ex & _ex_10(void);
192 const numeric & _num_9(void);     // -9
193 const ex & _ex_9(void);
194 const numeric & _num_8(void);     // -8
195 const ex & _ex_8(void);
196 const numeric & _num_7(void);     // -7
197 const ex & _ex_7(void);
198 const numeric & _num_6(void);     // -6
199 const ex & _ex_6(void);
200 const numeric & _num_5(void);     // -5
201 const ex & _ex_5(void);
202 const numeric & _num_4(void);     // -4
203 const ex & _ex_4(void);
204 const numeric & _num_3(void);     // -3
205 const ex & _ex_3(void);
206 const numeric & _num_2(void);     // -2
207 const ex & _ex_2(void);
208 const numeric & _num_1(void);     // -1
209 const ex & _ex_1(void);
210 const numeric & _num_1_2(void);   // -1/2
211 const ex & _ex_1_2(void);
212 const numeric & _num_1_3(void);   // -1/3
213 const ex & _ex_1_3(void);
214 const numeric & _num_1_4(void);   // -1/4
215 const ex & _ex_1_4(void);
216 const numeric & _num0(void);      //  0
217 const ex & _ex0(void);
218 const numeric & _num1_4(void);    //  1/4
219 const ex & _ex1_4(void);
220 const numeric & _num1_3(void);    //  1/3
221 const ex & _ex1_3(void);
222 const numeric & _num1_2(void);    //  1/2
223 const ex & _ex1_2(void);
224 const numeric & _num1(void);      //  1
225 const ex & _ex1(void);
226 const numeric & _num2(void);      //  2
227 const ex & _ex2(void);
228 const numeric & _num3(void);      //  3
229 const ex & _ex3(void);
230 const numeric & _num4(void);      //  4
231 const ex & _ex4(void);
232 const numeric & _num5(void);      //  5
233 const ex & _ex5(void);
234 const numeric & _num6(void);      //  6
235 const ex & _ex6(void);
236 const numeric & _num7(void);      //  7
237 const ex & _ex7(void);
238 const numeric & _num8(void);      //  8
239 const ex & _ex8(void);
240 const numeric & _num9(void);      //  9
241 const ex & _ex9(void);
242 const numeric & _num10(void);     //  10
243 const ex & _ex10(void);
244 const numeric & _num11(void);     //  11
245 const ex & _ex11(void);
246 const numeric & _num12(void);     //  12
247 const ex & _ex12(void);
248 const numeric & _num15(void);     //  15
249 const ex & _ex15(void);
250 const numeric & _num18(void);     //  18
251 const ex & _ex18(void);
252 const numeric & _num20(void);     //  20
253 const ex & _ex20(void);
254 const numeric & _num24(void);     //  24
255 const ex & _ex24(void);
256 const numeric & _num25(void);     //  25
257 const ex & _ex25(void);
258 const numeric & _num30(void);     //  30
259 const ex & _ex30(void);
260 const numeric & _num48(void);     //  48
261 const ex & _ex48(void);
262 const numeric & _num60(void);     //  60
263 const ex & _ex60(void);
264 const numeric & _num120(void);    //  120
265 const ex & _ex120(void);
266
267
268 // Helper macros for class implementations (mostly useful for trivial classes)
269
270 #define DEFAULT_COPY(classname) \
271 void classname::copy(const classname & other) \
272 { \
273         inherited::copy(other); \
274 }
275
276 #define DEFAULT_DESTROY(classname) \
277 void classname::destroy(bool call_parent) \
278 { \
279         if (call_parent) \
280                 inherited::destroy(call_parent); \
281 }
282
283 #define DEFAULT_CTORS(classname) \
284 classname::classname() : inherited(TINFO_##classname) \
285 { \
286         debugmsg(#classname " default constructor", LOGLEVEL_CONSTRUCT); \
287 } \
288 DEFAULT_COPY(classname) \
289 DEFAULT_DESTROY(classname)
290
291 #define DEFAULT_UNARCHIVE(classname) \
292 ex classname::unarchive(const archive_node &n, const lst &sym_lst) \
293 { \
294         return (new classname(n, sym_lst))->setflag(status_flags::dynallocated); \
295 }
296
297 #define DEFAULT_ARCHIVING(classname) \
298 classname::classname(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst) \
299 { \
300         debugmsg(#classname " constructor from archive_node", LOGLEVEL_CONSTRUCT); \
301 } \
302 DEFAULT_UNARCHIVE(classname) \
303 void classname::archive(archive_node &n) const \
304 { \
305         inherited::archive(n); \
306 }
307
308 #define DEFAULT_COMPARE(classname) \
309 int classname::compare_same_type(const basic & other) const \
310 { \
311         /* by default, the objects are always identical */ \
312         return 0; \
313 }
314
315 #define DEFAULT_PRINT(classname, text) \
316 void classname::print(const print_context & c, unsigned level) const \
317 { \
318         debugmsg(#classname " print", LOGLEVEL_PRINT); \
319         if (is_of_type(c, print_tree)) \
320                 inherited::print(c, level); \
321         else \
322                 c.s << text; \
323 }
324
325 #define DEFAULT_PRINT_LATEX(classname, text, latex) \
326 void classname::print(const print_context & c, unsigned level) const \
327 { \
328         debugmsg(#classname " print", LOGLEVEL_PRINT); \
329         if (is_of_type(c, print_tree)) \
330                 inherited::print(c, level); \
331         else if (is_of_type(c, print_latex)) \
332                 c.s << latex; \
333         else \
334                 c.s << text; \
335 }
336
337 } // namespace GiNaC
338
339 #endif // ndef __GINAC_UTILS_H__