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-2000 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"
33 #ifndef NO_NAMESPACE_GINAC
35 #endif // ndef NO_NAMESPACE_GINAC
37 // This should be obsoleted once <sstream> is widely available.
39 std::string ToString(const T & t)
42 std::ostrstream(buf,sizeof(buf)) << t << std::ends;
46 /** Exception class thrown by classes which provide their own series expansion
47 * to signal that ordinary Taylor expansion is safe. */
50 /** Exception class thrown when a singularity is encountered. */
51 class pole_error : public std::domain_error {
53 explicit pole_error(const std::string& what_arg, int degree);
54 int degree(void) const;
59 // some compilers (e.g. cygwin) define a macro log2, causing confusion
61 unsigned log2(unsigned n);
64 int compare_pointers(const void * a, const void * b);
66 /** Rotate lower 31 bits of unsigned value by one bit to the left
67 * (upper bits get cleared). */
68 inline unsigned rotate_left_31(unsigned n)
70 // clear highest bit and shift 1 bit to the left
71 n=(n & 0x7FFFFFFFU) << 1;
73 // overflow? clear highest bit and set lowest bit
74 if (n & 0x80000000U) {
75 n=(n & 0x7FFFFFFFU) | 0x00000001U;
77 GINAC_ASSERT(n<0x80000000U);
82 /** Golden ratio hash function. */
83 inline unsigned golden_ratio_hash(unsigned n)
85 // This function requires arithmetic with at least 64 significant bits
86 #if SIZEOF_LONG_DOUBLE > 8
87 // If "long double" is bigger than 64 bits, we assume that the mantissa
88 // has at least 64 bits. This is not guaranteed but it's a good guess.
89 const static long double golden_ratio = .618033988749894848204586834370;
90 long double m = golden_ratio * n;
91 return unsigned((m - int(m)) * 0x80000000);
92 #elif SIZEOF_LONG >= 8
93 // "long" has 64 bits, so we prefer it because it might be more efficient
95 unsigned long l = n * 0x4f1bbcddL;
96 return (l & 0x7fffffffU) ^ (l >> 32);
97 #elif SIZEOF_LONG_LONG >= 8
98 // This requires ´long long´ (or an equivalent 64 bit type)---which is,
99 // unfortunately, not ANSI-compliant:
100 unsigned long long l = n * 0x4f1bbcddLL;
101 return (l & 0x7fffffffU) ^ (l >> 32);
103 #error "No 64 bit data type. You lose."
107 // modified from stl_algo.h: always do com(*first1,*first2) instead of comp(*first2,*first1)
108 template <class InputIterator1, class InputIterator2, class OutputIterator,
110 OutputIterator mymerge(InputIterator1 first1, InputIterator1 last1,
111 InputIterator2 first2, InputIterator2 last2,
112 OutputIterator result, Compare comp) {
113 while (first1 != last1 && first2 != last2) {
114 if (comp(*first1, *first2)) {
124 return copy(first2, last2, copy(first1, last1, result));
127 // like merge(), but three lists with *last2<*first3
128 template <class InputIterator1, class InputIterator2, class InputIterator3,
129 class OutputIterator, class Compare>
130 OutputIterator mymerge3(InputIterator1 first1, InputIterator1 last1,
131 InputIterator2 first2, InputIterator2 last2,
132 InputIterator3 first3, InputIterator3 last3,
133 OutputIterator result, Compare comp) {
134 while (first1 != last1 && first2 != last2) {
135 if (comp(*first1, *first2)) {
147 // list1 empty, copy rest of list2, then list3
148 return copy(first3, last3, copy(first2, last2, result));
150 // list2 empty, merge rest of list1 with list3
151 return mymerge(first1,last1,first3,last3,result,comp);
155 // Compute the sign of a permutation of a vector of things.
156 template <typename T>
157 int permutation_sign(std::vector<T> s)
162 for (typename std::vector<T>::iterator i=s.begin(); i!=s.end()-1; ++i) {
163 for (typename std::vector<T>::iterator j=i+1; j!=s.end(); ++j) {
175 // Collection of `construct on first use' wrappers for safely avoiding
176 // internal object replication without running into the `static
177 // initialization order fiasco'. This chest of numbers helps speed up
178 // the library but should not be used outside it since it is
179 // potentially confusing.
184 const numeric & _num_120(void); // -120
185 const ex & _ex_120(void);
186 const numeric & _num_60(void); // -60
187 const ex & _ex_60(void);
188 const numeric & _num_48(void); // -48
189 const ex & _ex_48(void);
190 const numeric & _num_30(void); // -30
191 const ex & _ex_30(void);
192 const numeric & _num_25(void); // -25
193 const ex & _ex_25(void);
194 const numeric & _num_24(void); // -24
195 const ex & _ex_24(void);
196 const numeric & _num_20(void); // -20
197 const ex & _ex_20(void);
198 const numeric & _num_18(void); // -18
199 const ex & _ex_18(void);
200 const numeric & _num_15(void); // -15
201 const ex & _ex_15(void);
202 const numeric & _num_12(void); // -12
203 const ex & _ex_12(void);
204 const numeric & _num_11(void); // -11
205 const ex & _ex_11(void);
206 const numeric & _num_10(void); // -10
207 const ex & _ex_10(void);
208 const numeric & _num_9(void); // -9
209 const ex & _ex_9(void);
210 const numeric & _num_8(void); // -8
211 const ex & _ex_8(void);
212 const numeric & _num_7(void); // -7
213 const ex & _ex_7(void);
214 const numeric & _num_6(void); // -6
215 const ex & _ex_6(void);
216 const numeric & _num_5(void); // -5
217 const ex & _ex_5(void);
218 const numeric & _num_4(void); // -4
219 const ex & _ex_4(void);
220 const numeric & _num_3(void); // -3
221 const ex & _ex_3(void);
222 const numeric & _num_2(void); // -2
223 const ex & _ex_2(void);
224 const numeric & _num_1(void); // -1
225 const ex & _ex_1(void);
226 const numeric & _num_1_2(void); // -1/2
227 const ex & _ex_1_2(void);
228 const numeric & _num_1_3(void); // -1/3
229 const ex & _ex_1_3(void);
230 const numeric & _num_1_4(void); // -1/4
231 const ex & _ex_1_4(void);
232 const numeric & _num0(void); // 0
233 const ex & _ex0(void);
234 const numeric & _num1_4(void); // 1/4
235 const ex & _ex1_4(void);
236 const numeric & _num1_3(void); // 1/3
237 const ex & _ex1_3(void);
238 const numeric & _num1_2(void); // 1/2
239 const ex & _ex1_2(void);
240 const numeric & _num1(void); // 1
241 const ex & _ex1(void);
242 const numeric & _num2(void); // 2
243 const ex & _ex2(void);
244 const numeric & _num3(void); // 3
245 const ex & _ex3(void);
246 const numeric & _num4(void); // 4
247 const ex & _ex4(void);
248 const numeric & _num5(void); // 5
249 const ex & _ex5(void);
250 const numeric & _num6(void); // 6
251 const ex & _ex6(void);
252 const numeric & _num7(void); // 7
253 const ex & _ex7(void);
254 const numeric & _num8(void); // 8
255 const ex & _ex8(void);
256 const numeric & _num9(void); // 9
257 const ex & _ex9(void);
258 const numeric & _num10(void); // 10
259 const ex & _ex10(void);
260 const numeric & _num11(void); // 11
261 const ex & _ex11(void);
262 const numeric & _num12(void); // 12
263 const ex & _ex12(void);
264 const numeric & _num15(void); // 15
265 const ex & _ex15(void);
266 const numeric & _num18(void); // 18
267 const ex & _ex18(void);
268 const numeric & _num20(void); // 20
269 const ex & _ex20(void);
270 const numeric & _num24(void); // 24
271 const ex & _ex24(void);
272 const numeric & _num25(void); // 25
273 const ex & _ex25(void);
274 const numeric & _num30(void); // 30
275 const ex & _ex30(void);
276 const numeric & _num48(void); // 48
277 const ex & _ex48(void);
278 const numeric & _num60(void); // 60
279 const ex & _ex60(void);
280 const numeric & _num120(void); // 120
281 const ex & _ex120(void);
283 #ifndef NO_NAMESPACE_GINAC
285 #endif // ndef NO_NAMESPACE_GINAC
287 #endif // ndef __GINAC_UTILS_H__