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