]> www.ginac.de Git - ginac.git/blob - ginac/utils.h
- removed non-sources from CVS
[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-2000 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 <strstream>
28 #include <string>
29 #include <stdexcept>
30 #include "config.h"
31 #include "assertion.h"
32
33 #ifndef NO_NAMESPACE_GINAC
34 namespace GiNaC {
35 #endif // ndef NO_NAMESPACE_GINAC
36
37 // This should be obsoleted once <sstream> is widely available.
38 template<class T>
39 std::string ToString(const T & t)
40 {
41         char buf[256];
42         std::ostrstream(buf,sizeof(buf)) << t << std::ends;
43         return buf;
44 }
45
46 /** Exception class thrown by classes which provide their own series expansion
47  *  to signal that ordinary Taylor expansion is safe. */
48 class do_taylor {};
49
50 /** Exception class thrown when a singularity is encountered. */
51 class pole_error : public std::domain_error {
52 public:
53         explicit pole_error(const std::string& what_arg, int degree);
54         int degree(void) const;
55 private:
56         int deg;
57 };
58
59 // some compilers (e.g. cygwin) define a macro log2, causing confusion
60 #ifndef log2
61 unsigned log2(unsigned n);
62 #endif
63
64 int compare_pointers(const void * a, const void * b);
65
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)
69 {
70         // clear highest bit and shift 1 bit to the left
71         n=(n & 0x7FFFFFFFU) << 1;
72
73         // overflow? clear highest bit and set lowest bit
74         if (n & 0x80000000U) {
75                 n=(n & 0x7FFFFFFFU) | 0x00000001U;
76         }
77         GINAC_ASSERT(n<0x80000000U);
78
79         return n;
80 }
81
82 /** Golden ratio hash function. */
83 inline unsigned golden_ratio_hash(unsigned n)
84 {
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
94         // than "long long"
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);
102 #else
103 #error "No 64 bit data type. You lose."
104 #endif
105 }
106
107 // modified from stl_algo.h: always do com(*first1,*first2) instead of comp(*first2,*first1)
108 template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
109 OutputIterator mymerge(InputIterator1 first1, InputIterator1 last1,
110                        InputIterator2 first2, InputIterator2 last2,
111                        OutputIterator result, Compare comp) {
112         while (first1 != last1 && first2 != last2) {
113                 if (comp(*first1, *first2)) {
114                         *result = *first1;
115                         ++first1;
116                 }
117                 else {
118                         *result = *first2;
119                         ++first2;
120                 }
121                 ++result;
122         }
123         return copy(first2, last2, copy(first1, last1, result));
124 }
125
126 // like merge(), but three lists with *last2<*first3
127 template <class InputIterator1, class InputIterator2, class InputIterator3,
128           class OutputIterator, class Compare>
129 OutputIterator mymerge3(InputIterator1 first1, InputIterator1 last1,
130                         InputIterator2 first2, InputIterator2 last2,
131                         InputIterator3 first3, InputIterator3 last3,
132                         OutputIterator result, Compare comp) {
133         while (first1 != last1 && first2 != last2) {
134                 if (comp(*first1, *first2)) {
135                         *result = *first1;
136                         ++first1;
137                 }
138                 else {
139                         *result = *first2;
140                         ++first2;
141                 }
142                 ++result;
143         }
144         
145         if (first1==last1) {
146                 // list1 empty, copy rest of list2, then list3
147                 return copy(first3, last3, copy(first2, last2, result));
148         } else {
149                 // list2 empty, merge rest of list1 with list3
150                 return mymerge(first1,last1,first3,last3,result,comp);
151         }
152 }
153
154 // Compute the sign of a permutation of a vector of things.
155 template <typename T>
156 int permutation_sign(std::vector<T> s)
157 {
158         if (s.size() < 2)
159                 return 0;
160         int sigma = 1;
161         for (typename std::vector<T>::iterator i=s.begin(); i!=s.end()-1; ++i) {
162                 for (typename std::vector<T>::iterator j=i+1; j!=s.end(); ++j) {
163                         if (*i == *j)
164                                 return 0;
165                         if (*i > *j) {
166                                 iter_swap(i,j);
167                                 sigma = -sigma;
168                         }
169                 }
170         }
171         return sigma;
172 }
173
174 // Collection of `construct on first use' wrappers for safely avoiding
175 // internal object replication without running into the `static
176 // initialization order fiasco'.  This chest of numbers helps speed up
177 // the library but should not be used outside it since it is
178 // potentially confusing.
179
180 class numeric;
181 class ex;
182
183 const numeric & _num_120(void);   // -120
184 const ex & _ex_120(void);
185 const numeric & _num_60(void);    // -60
186 const ex & _ex_60(void);
187 const numeric & _num_48(void);    // -48
188 const ex & _ex_48(void);
189 const numeric & _num_30(void);    // -30
190 const ex & _ex_30(void);
191 const numeric & _num_25(void);    // -25
192 const ex & _ex_25(void);
193 const numeric & _num_24(void);    // -24
194 const ex & _ex_24(void);
195 const numeric & _num_20(void);    // -20
196 const ex & _ex_20(void);
197 const numeric & _num_18(void);    // -18
198 const ex & _ex_18(void);
199 const numeric & _num_15(void);    // -15
200 const ex & _ex_15(void);
201 const numeric & _num_12(void);    // -12
202 const ex & _ex_12(void);
203 const numeric & _num_11(void);    // -11
204 const ex & _ex_11(void);
205 const numeric & _num_10(void);    // -10
206 const ex & _ex_10(void);
207 const numeric & _num_9(void);     // -9
208 const ex & _ex_9(void);
209 const numeric & _num_8(void);     // -8
210 const ex & _ex_8(void);
211 const numeric & _num_7(void);     // -7
212 const ex & _ex_7(void);
213 const numeric & _num_6(void);     // -6
214 const ex & _ex_6(void);
215 const numeric & _num_5(void);     // -5
216 const ex & _ex_5(void);
217 const numeric & _num_4(void);     // -4
218 const ex & _ex_4(void);
219 const numeric & _num_3(void);     // -3
220 const ex & _ex_3(void);
221 const numeric & _num_2(void);     // -2
222 const ex & _ex_2(void);
223 const numeric & _num_1(void);     // -1
224 const ex & _ex_1(void);
225 const numeric & _num_1_2(void);   // -1/2
226 const ex & _ex_1_2(void);
227 const numeric & _num_1_3(void);   // -1/3
228 const ex & _ex_1_3(void);
229 const numeric & _num_1_4(void);   // -1/4
230 const ex & _ex_1_4(void);
231 const numeric & _num0(void);      //  0
232 const ex & _ex0(void);
233 const numeric & _num1_4(void);    //  1/4
234 const ex & _ex1_4(void);
235 const numeric & _num1_3(void);    //  1/3
236 const ex & _ex1_3(void);
237 const numeric & _num1_2(void);    //  1/2
238 const ex & _ex1_2(void);
239 const numeric & _num1(void);      //  1
240 const ex & _ex1(void);
241 const numeric & _num2(void);      //  2
242 const ex & _ex2(void);
243 const numeric & _num3(void);      //  3
244 const ex & _ex3(void);
245 const numeric & _num4(void);      //  4
246 const ex & _ex4(void);
247 const numeric & _num5(void);      //  5
248 const ex & _ex5(void);
249 const numeric & _num6(void);      //  6
250 const ex & _ex6(void);
251 const numeric & _num7(void);      //  7
252 const ex & _ex7(void);
253 const numeric & _num8(void);      //  8
254 const ex & _ex8(void);
255 const numeric & _num9(void);      //  9
256 const ex & _ex9(void);
257 const numeric & _num10(void);     //  10
258 const ex & _ex10(void);
259 const numeric & _num11(void);     //  11
260 const ex & _ex11(void);
261 const numeric & _num12(void);     //  12
262 const ex & _ex12(void);
263 const numeric & _num15(void);     //  15
264 const ex & _ex15(void);
265 const numeric & _num18(void);     //  18
266 const ex & _ex18(void);
267 const numeric & _num20(void);     //  20
268 const ex & _ex20(void);
269 const numeric & _num24(void);     //  24
270 const ex & _ex24(void);
271 const numeric & _num25(void);     //  25
272 const ex & _ex25(void);
273 const numeric & _num30(void);     //  30
274 const ex & _ex30(void);
275 const numeric & _num48(void);     //  48
276 const ex & _ex48(void);
277 const numeric & _num60(void);     //  60
278 const ex & _ex60(void);
279 const numeric & _num120(void);    //  120
280 const ex & _ex120(void);
281
282 #ifndef NO_NAMESPACE_GINAC
283 } // namespace GiNaC
284 #endif // ndef NO_NAMESPACE_GINAC
285
286 #endif // ndef __GINAC_UTILS_H__