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