3 * Interface to several small and furry utilities needed within GiNaC but not
4 * of any interest to the user of the library. */
7 * GiNaC Copyright (C) 1999-2001 Johannes Gutenberg University Mainz, Germany
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.
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.
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
24 #ifndef __GINAC_UTILS_H__
25 #define __GINAC_UTILS_H__
31 #include "assertion.h"
35 // This should be obsoleted once <sstream> is widely deployed.
37 std::string ToString(const T & t)
40 std::ostrstream(buf,sizeof(buf)) << t << std::ends;
44 /** Exception class thrown by classes which provide their own series expansion
45 * to signal that ordinary Taylor expansion is safe. */
48 /** Exception class thrown when a singularity is encountered. */
49 class pole_error : public std::domain_error {
51 explicit pole_error(const std::string& what_arg, int degree);
52 int degree(void) const;
57 // some compilers (e.g. cygwin) define a macro log2, causing confusion
59 unsigned log2(unsigned n);
62 /** Compare two pointers (just to establish some sort of canonical order).
63 * @return -1, 0, or 1 */
64 inline int compare_pointers(const void * a, const void * b)
73 /** Rotate lower 31 bits of unsigned value by one bit to the left
74 * (upper bit gets cleared). */
75 inline unsigned rotate_left_31(unsigned n)
77 // clear highest bit and shift 1 bit to the left
78 n = (n & 0x7FFFFFFFU) << 1;
80 // overflow? clear highest bit and set lowest bit
82 n = (n & 0x7FFFFFFFU) | 0x00000001U;
84 GINAC_ASSERT(n<0x80000000U);
89 /** Golden ratio hash function for the 31 least significant bits. */
90 inline unsigned golden_ratio_hash(unsigned n)
92 // This function requires arithmetic with at least 64 significant bits
94 // So 'long' has 64 bits. Excellent! We prefer it because it might be
95 // more efficient than 'long long'.
96 unsigned long l = n * 0x4f1bbcddL;
97 return (l & 0x7fffffffU) ^ (l >> 32);
98 #elif SIZEOF_LONG_LONG >= 8
99 // This requires 'long long' (or an equivalent 64 bit type)---which is,
100 // unfortunately, not ANSI-C++-compliant.
101 // (Yet C99 demands it, which is reason for hope.)
102 unsigned long long l = n * 0x4f1bbcddL;
103 return (l & 0x7fffffffU) ^ (l >> 32);
104 #elif SIZEOF_LONG_DOUBLE > 8
105 // If 'long double' is bigger than 64 bits, we assume that the mantissa
106 // has at least 64 bits. This is not guaranteed but it's a good guess.
107 // Unfortunately, it may lead to horribly slow code.
108 const static long double golden_ratio = .618033988749894848204586834370;
109 long double m = golden_ratio * n;
110 return unsigned((m - int(m)) * 0x80000000);
112 #error "No 64 bit data type. You lose."
116 // Compute the sign of a permutation of a vector of things.
117 template <typename T>
118 int permutation_sign(std::vector<T> s)
123 for (typename std::vector<T>::iterator i=s.begin(); i!=s.end()-1; ++i) {
124 for (typename std::vector<T>::iterator j=i+1; j!=s.end(); ++j) {
136 void append_exvector_to_exvector(exvector & dest, const exvector & source);
138 // Collection of `construct on first use' wrappers for safely avoiding
139 // internal object replication without running into the `static
140 // initialization order fiasco'. This chest of numbers helps speed up
141 // the library but should not be used outside it since it is
142 // potentially confusing.
147 const numeric & _num_120(void); // -120
148 const ex & _ex_120(void);
149 const numeric & _num_60(void); // -60
150 const ex & _ex_60(void);
151 const numeric & _num_48(void); // -48
152 const ex & _ex_48(void);
153 const numeric & _num_30(void); // -30
154 const ex & _ex_30(void);
155 const numeric & _num_25(void); // -25
156 const ex & _ex_25(void);
157 const numeric & _num_24(void); // -24
158 const ex & _ex_24(void);
159 const numeric & _num_20(void); // -20
160 const ex & _ex_20(void);
161 const numeric & _num_18(void); // -18
162 const ex & _ex_18(void);
163 const numeric & _num_15(void); // -15
164 const ex & _ex_15(void);
165 const numeric & _num_12(void); // -12
166 const ex & _ex_12(void);
167 const numeric & _num_11(void); // -11
168 const ex & _ex_11(void);
169 const numeric & _num_10(void); // -10
170 const ex & _ex_10(void);
171 const numeric & _num_9(void); // -9
172 const ex & _ex_9(void);
173 const numeric & _num_8(void); // -8
174 const ex & _ex_8(void);
175 const numeric & _num_7(void); // -7
176 const ex & _ex_7(void);
177 const numeric & _num_6(void); // -6
178 const ex & _ex_6(void);
179 const numeric & _num_5(void); // -5
180 const ex & _ex_5(void);
181 const numeric & _num_4(void); // -4
182 const ex & _ex_4(void);
183 const numeric & _num_3(void); // -3
184 const ex & _ex_3(void);
185 const numeric & _num_2(void); // -2
186 const ex & _ex_2(void);
187 const numeric & _num_1(void); // -1
188 const ex & _ex_1(void);
189 const numeric & _num_1_2(void); // -1/2
190 const ex & _ex_1_2(void);
191 const numeric & _num_1_3(void); // -1/3
192 const ex & _ex_1_3(void);
193 const numeric & _num_1_4(void); // -1/4
194 const ex & _ex_1_4(void);
195 const numeric & _num0(void); // 0
196 const ex & _ex0(void);
197 const numeric & _num1_4(void); // 1/4
198 const ex & _ex1_4(void);
199 const numeric & _num1_3(void); // 1/3
200 const ex & _ex1_3(void);
201 const numeric & _num1_2(void); // 1/2
202 const ex & _ex1_2(void);
203 const numeric & _num1(void); // 1
204 const ex & _ex1(void);
205 const numeric & _num2(void); // 2
206 const ex & _ex2(void);
207 const numeric & _num3(void); // 3
208 const ex & _ex3(void);
209 const numeric & _num4(void); // 4
210 const ex & _ex4(void);
211 const numeric & _num5(void); // 5
212 const ex & _ex5(void);
213 const numeric & _num6(void); // 6
214 const ex & _ex6(void);
215 const numeric & _num7(void); // 7
216 const ex & _ex7(void);
217 const numeric & _num8(void); // 8
218 const ex & _ex8(void);
219 const numeric & _num9(void); // 9
220 const ex & _ex9(void);
221 const numeric & _num10(void); // 10
222 const ex & _ex10(void);
223 const numeric & _num11(void); // 11
224 const ex & _ex11(void);
225 const numeric & _num12(void); // 12
226 const ex & _ex12(void);
227 const numeric & _num15(void); // 15
228 const ex & _ex15(void);
229 const numeric & _num18(void); // 18
230 const ex & _ex18(void);
231 const numeric & _num20(void); // 20
232 const ex & _ex20(void);
233 const numeric & _num24(void); // 24
234 const ex & _ex24(void);
235 const numeric & _num25(void); // 25
236 const ex & _ex25(void);
237 const numeric & _num30(void); // 30
238 const ex & _ex30(void);
239 const numeric & _num48(void); // 48
240 const ex & _ex48(void);
241 const numeric & _num60(void); // 60
242 const ex & _ex60(void);
243 const numeric & _num120(void); // 120
244 const ex & _ex120(void);
247 // Helper macros for class implementations (mostly useful for trivial classes)
249 #define DEFAULT_COPY(classname) \
250 void classname::copy(const classname & other) \
252 inherited::copy(other); \
255 #define DEFAULT_DESTROY(classname) \
256 void classname::destroy(bool call_parent) \
259 inherited::destroy(call_parent); \
262 #define DEFAULT_CTORS(classname) \
263 classname::classname() : inherited(TINFO_##classname) \
265 debugmsg(#classname " default constructor", LOGLEVEL_CONSTRUCT); \
267 DEFAULT_COPY(classname) \
268 DEFAULT_DESTROY(classname)
270 #define DEFAULT_UNARCHIVE(classname) \
271 ex classname::unarchive(const archive_node &n, const lst &sym_lst) \
273 return (new classname(n, sym_lst))->setflag(status_flags::dynallocated); \
276 #define DEFAULT_ARCHIVING(classname) \
277 classname::classname(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst) \
279 debugmsg(#classname " constructor from archive_node", LOGLEVEL_CONSTRUCT); \
281 DEFAULT_UNARCHIVE(classname) \
282 void classname::archive(archive_node &n) const \
284 inherited::archive(n); \
287 #define DEFAULT_COMPARE(classname) \
288 int classname::compare_same_type(const basic & other) const \
290 /* by default, the objects are always identical */ \
294 #define DEFAULT_PRINT(classname, text) \
295 void classname::print(const print_context & c, unsigned level) const \
297 debugmsg(#classname " print", LOGLEVEL_PRINT); \
298 if (is_of_type(c, print_tree)) \
299 inherited::print(c, level); \
306 #endif // ndef __GINAC_UTILS_H__