Synced to HEAD
[ginac.git] / ginac / utils.cpp
1 /** @file utils.cpp
2  *
3  *  Implementation of several small and furry utilities needed within GiNaC
4  *  but not of any interest to the user of the library. */
5
6 /*
7  *  GiNaC Copyright (C) 1999-2004 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 #include "ex.h"
25 #include "numeric.h"
26 #include "utils.h"
27 #include "version.h"
28
29 namespace GiNaC {
30
31 /* Version information buried into the library */
32 const int version_major = GINACLIB_MAJOR_VERSION;
33 const int version_minor = GINACLIB_MINOR_VERSION;
34 const int version_micro = GINACLIB_MICRO_VERSION;
35
36
37 /** ctor for pole_error exception class. */
38 pole_error::pole_error(const std::string& what_arg, int degree)
39         : domain_error(what_arg), deg(degree) { }
40
41 /** Return the degree of the pole_error exception class. */
42 int pole_error::degree() const
43 {
44         return deg;
45 }
46
47 /** Integer binary logarithm */
48 unsigned log2(unsigned n)
49 {
50         unsigned k;
51         for (k = 0; n > 1; n >>= 1)
52                 ++k;
53         return k;
54 }
55
56
57 //////////
58 // flyweight chest of numbers is initialized here:
59 //////////
60
61 /** How many static objects were created?  Only the first one must create
62  *  the static flyweights on the heap. */
63 int library_init::count = 0;
64
65 // static numeric -120
66 const numeric *_num_120_p;
67 const numeric &_num_120 = *_num_120_p;
68 const ex _ex_120 = _num_120;
69
70 // static numeric -60
71 const numeric *_num_60_p;
72 const numeric &_num_60 = *_num_60_p;
73 const ex _ex_60 = _num_60;
74
75 // static numeric -48
76 const numeric *_num_48_p;
77 const numeric &_num_48 = *_num_48_p;
78 const ex _ex_48 = _num_48;
79
80 // static numeric -30
81 const numeric *_num_30_p;
82 const numeric &_num_30 = *_num_30_p;
83 const ex _ex_30 = _num_30;
84
85 // static numeric -25
86 const numeric *_num_25_p;
87 const numeric &_num_25 = *_num_25_p;
88 const ex _ex_25 = _num_25;
89
90 // static numeric -24
91 const numeric *_num_24_p;
92 const numeric &_num_24 = *_num_24_p;
93 const ex _ex_24 = _num_24;
94
95 // static numeric -20
96 const numeric *_num_20_p;
97 const numeric &_num_20 = *_num_20_p;
98 const ex _ex_20 = _num_20;
99
100 // static numeric -18
101 const numeric *_num_18_p;
102 const numeric &_num_18 = *_num_18_p;
103 const ex _ex_18 = _num_18;
104
105 // static numeric -15
106 const numeric *_num_15_p;
107 const numeric &_num_15 = *_num_15_p;
108 const ex _ex_15 = _num_15;
109
110 // static numeric -12
111 const numeric *_num_12_p;
112 const numeric &_num_12 = *_num_12_p;
113 const ex _ex_12 = _num_12;
114
115 // static numeric -11
116 const numeric *_num_11_p;
117 const numeric &_num_11 = *_num_11_p;
118 const ex _ex_11 = _num_11;
119
120 // static numeric -10
121 const numeric *_num_10_p;
122 const numeric &_num_10 = *_num_10_p;
123 const ex _ex_10 = _num_10;
124
125 // static numeric -9
126 const numeric *_num_9_p;
127 const numeric &_num_9 = *_num_9_p;
128 const ex _ex_9 = _num_9;
129
130 // static numeric -8
131 const numeric *_num_8_p;
132 const numeric &_num_8 = *_num_8_p;
133 const ex _ex_8 = _num_8;
134
135 // static numeric -7
136 const numeric *_num_7_p;
137 const numeric &_num_7 = *_num_7_p;
138 const ex _ex_7 = _num_7;
139
140 // static numeric -6
141 const numeric *_num_6_p;
142 const numeric &_num_6 = *_num_6_p;
143 const ex _ex_6 = _num_6;
144
145 // static numeric -5
146 const numeric *_num_5_p;
147 const numeric &_num_5 = *_num_5_p;
148 const ex _ex_5 = _num_5;
149
150 // static numeric -4
151 const numeric *_num_4_p;
152 const numeric &_num_4 = *_num_4_p;
153 const ex _ex_4 = _num_4;
154
155 // static numeric -3
156 const numeric *_num_3_p;
157 const numeric &_num_3 = *_num_3_p;
158 const ex _ex_3 = _num_3;
159
160 // static numeric -2
161 const numeric *_num_2_p;
162 const numeric &_num_2 = *_num_2_p;
163 const ex _ex_2 = _num_2;
164
165 // static numeric -1
166 const numeric *_num_1_p;
167 const numeric &_num_1 = *_num_1_p;
168 const ex _ex_1 = _num_1;
169
170 // static numeric -1/2
171 const numeric *_num_1_2_p;
172 const numeric &_num_1_2= *_num_1_2_p;
173 const ex _ex_1_2= _num_1_2;
174
175 // static numeric -1/3
176 const numeric *_num_1_3_p;
177 const numeric &_num_1_3= *_num_1_3_p;
178 const ex _ex_1_3= _num_1_3;
179
180 // static numeric -1/4
181 const numeric *_num_1_4_p;
182 const numeric &_num_1_4= *_num_1_4_p;
183 const ex _ex_1_4= _num_1_4;
184
185 // static numeric 0
186 const numeric *_num0_p;
187 const basic *_num0_bp;
188 const numeric &_num0 = *_num0_p;
189 const ex _ex0 = _num0;
190
191 // static numeric 1/4
192 const numeric *_num1_4_p;
193 const numeric &_num1_4 = *_num1_4_p;
194 const ex _ex1_4 = _num1_4;
195
196 // static numeric 1/3
197 const numeric *_num1_3_p;
198 const numeric &_num1_3 = *_num1_3_p;
199 const ex _ex1_3 = _num1_3;
200
201 // static numeric 1/2
202 const numeric *_num1_2_p;
203 const numeric &_num1_2 = *_num1_2_p;
204 const ex _ex1_2 = _num1_2;
205
206 // static numeric 1
207 const numeric *_num1_p;
208 const numeric &_num1 = *_num1_p;
209 const ex _ex1 = _num1;
210
211 // static numeric 2
212 const numeric *_num2_p;
213 const numeric &_num2 = *_num2_p;
214 const ex _ex2 = _num2;
215
216 // static numeric 3
217 const numeric *_num3_p;
218 const numeric &_num3 = *_num3_p;
219 const ex _ex3 = _num3;
220
221 // static numeric 4
222 const numeric *_num4_p;
223 const numeric &_num4 = *_num4_p;
224 const ex _ex4 = _num4;
225
226 // static numeric 5
227 const numeric *_num5_p;
228 const numeric &_num5 = *_num5_p;
229 const ex _ex5 = _num5;
230
231 // static numeric 6
232 const numeric *_num6_p;
233 const numeric &_num6 = *_num6_p;
234 const ex _ex6 = _num6;
235
236 // static numeric 7
237 const numeric *_num7_p;
238 const numeric &_num7 = *_num7_p;
239 const ex _ex7 = _num7;
240
241 // static numeric 8
242 const numeric *_num8_p;
243 const numeric &_num8 = *_num8_p;
244 const ex _ex8 = _num8;
245
246 // static numeric 9
247 const numeric *_num9_p;
248 const numeric &_num9 = *_num9_p;
249 const ex _ex9 = _num9;
250
251 // static numeric 10
252 const numeric *_num10_p;
253 const numeric &_num10 = *_num10_p;
254 const ex _ex10 = _num10;
255
256 // static numeric 11
257 const numeric *_num11_p;
258 const numeric &_num11 = *_num11_p;
259 const ex _ex11 = _num11;
260
261 // static numeric 12
262 const numeric *_num12_p;
263 const numeric &_num12 = *_num12_p;
264 const ex _ex12 = _num12;
265
266 // static numeric 15
267 const numeric *_num15_p;
268 const numeric &_num15 = *_num15_p;
269 const ex _ex15 = _num15;
270
271 // static numeric 18
272 const numeric *_num18_p;
273 const numeric &_num18 = *_num18_p;
274 const ex _ex18 = _num18;
275
276 // static numeric 20
277 const numeric *_num20_p;
278 const numeric &_num20 = *_num20_p;
279 const ex _ex20 = _num20;
280
281 // static numeric 24
282 const numeric *_num24_p;
283 const numeric &_num24 = *_num24_p;
284 const ex _ex24 = _num24;
285
286 // static numeric 25
287 const numeric *_num25_p;
288 const numeric &_num25 = *_num25_p;
289 const ex _ex25 = _num25;
290
291 // static numeric 30
292 const numeric *_num30_p;
293 const numeric &_num30 = *_num30_p;
294 const ex _ex30 = _num30;
295
296 // static numeric 48
297 const numeric *_num48_p;
298 const numeric &_num48 = *_num48_p;
299 const ex _ex48 = _num48;
300
301 // static numeric 60
302 const numeric *_num60_p;
303 const numeric &_num60 = *_num60_p;
304 const ex _ex60 = _num60;
305
306 // static numeric 120
307 const numeric *_num120_p;
308 const numeric &_num120 = *_num120_p;
309 const ex _ex120 = _num120;
310
311 /** Ctor of static initialization helpers.  The fist call to this is going
312  *  to initialize the library, the others do nothing. */
313 library_init::library_init()
314 {
315         if (count++==0) {
316                 _num_120_p= static_cast<const numeric*>(&((new numeric(-120))->setflag(status_flags::dynallocated)));
317                 _num_60_p = static_cast<const numeric*>(&((new numeric(-60))->setflag(status_flags::dynallocated)));
318                 _num_48_p = static_cast<const numeric*>(&((new numeric(-48))->setflag(status_flags::dynallocated)));
319                 _num_30_p = static_cast<const numeric*>(&((new numeric(-30))->setflag(status_flags::dynallocated)));
320                 _num_25_p = static_cast<const numeric*>(&((new numeric(-25))->setflag(status_flags::dynallocated)));
321                 _num_24_p = static_cast<const numeric*>(&((new numeric(-24))->setflag(status_flags::dynallocated)));
322                 _num_20_p = static_cast<const numeric*>(&((new numeric(-20))->setflag(status_flags::dynallocated)));
323                 _num_18_p = static_cast<const numeric*>(&((new numeric(-18))->setflag(status_flags::dynallocated)));
324                 _num_15_p = static_cast<const numeric*>(&((new numeric(-15))->setflag(status_flags::dynallocated)));
325                 _num_12_p = static_cast<const numeric*>(&((new numeric(-12))->setflag(status_flags::dynallocated)));
326                 _num_11_p = static_cast<const numeric*>(&((new numeric(-11))->setflag(status_flags::dynallocated)));
327                 _num_10_p = static_cast<const numeric*>(&((new numeric(-10))->setflag(status_flags::dynallocated)));
328                 _num_9_p  = static_cast<const numeric*>(&((new numeric(-9))->setflag(status_flags::dynallocated)));
329                 _num_8_p  = static_cast<const numeric*>(&((new numeric(-8))->setflag(status_flags::dynallocated)));
330                 _num_7_p  = static_cast<const numeric*>(&((new numeric(-7))->setflag(status_flags::dynallocated)));
331                 _num_6_p  = static_cast<const numeric*>(&((new numeric(-6))->setflag(status_flags::dynallocated)));
332                 _num_5_p  = static_cast<const numeric*>(&((new numeric(-5))->setflag(status_flags::dynallocated)));
333                 _num_4_p  = static_cast<const numeric*>(&((new numeric(-4))->setflag(status_flags::dynallocated)));
334                 _num_3_p  = static_cast<const numeric*>(&((new numeric(-3))->setflag(status_flags::dynallocated)));
335                 _num_2_p  = static_cast<const numeric*>(&((new numeric(-2))->setflag(status_flags::dynallocated)));
336                 _num_1_p  = static_cast<const numeric*>(&((new numeric(-1))->setflag(status_flags::dynallocated)));
337                 _num_1_2_p= static_cast<const numeric*>(&((new numeric(-1,2))->setflag(status_flags::dynallocated)));
338                 _num_1_3_p= static_cast<const numeric*>(&((new numeric(-1,3))->setflag(status_flags::dynallocated)));
339                 _num_1_4_p= static_cast<const numeric*>(&((new numeric(-1,4))->setflag(status_flags::dynallocated)));
340                 _num0_p   = static_cast<const numeric*>(&((new numeric(0))->setflag(status_flags::dynallocated)));
341                 _num0_bp  = _num0_p;  // Cf. class ex default ctor.
342                 _num1_4_p = static_cast<const numeric*>(&((new numeric(1,4))->setflag(status_flags::dynallocated)));
343                 _num1_3_p = static_cast<const numeric*>(&((new numeric(1,3))->setflag(status_flags::dynallocated)));
344                 _num1_2_p = static_cast<const numeric*>(&((new numeric(1,2))->setflag(status_flags::dynallocated)));
345                 _num1_p   = static_cast<const numeric*>(&((new numeric(1))->setflag(status_flags::dynallocated)));
346                 _num2_p   = static_cast<const numeric*>(&((new numeric(2))->setflag(status_flags::dynallocated)));
347                 _num3_p   = static_cast<const numeric*>(&((new numeric(3))->setflag(status_flags::dynallocated)));
348                 _num4_p   = static_cast<const numeric*>(&((new numeric(4))->setflag(status_flags::dynallocated)));
349                 _num5_p   = static_cast<const numeric*>(&((new numeric(5))->setflag(status_flags::dynallocated)));
350                 _num6_p   = static_cast<const numeric*>(&((new numeric(6))->setflag(status_flags::dynallocated)));
351                 _num7_p   = static_cast<const numeric*>(&((new numeric(7))->setflag(status_flags::dynallocated)));
352                 _num8_p   = static_cast<const numeric*>(&((new numeric(8))->setflag(status_flags::dynallocated)));
353                 _num9_p   = static_cast<const numeric*>(&((new numeric(9))->setflag(status_flags::dynallocated)));
354                 _num10_p  = static_cast<const numeric*>(&((new numeric(10))->setflag(status_flags::dynallocated)));
355                 _num11_p  = static_cast<const numeric*>(&((new numeric(11))->setflag(status_flags::dynallocated)));
356                 _num12_p  = static_cast<const numeric*>(&((new numeric(12))->setflag(status_flags::dynallocated)));
357                 _num15_p  = static_cast<const numeric*>(&((new numeric(15))->setflag(status_flags::dynallocated)));
358                 _num18_p  = static_cast<const numeric*>(&((new numeric(18))->setflag(status_flags::dynallocated)));
359                 _num20_p  = static_cast<const numeric*>(&((new numeric(20))->setflag(status_flags::dynallocated)));
360                 _num24_p  = static_cast<const numeric*>(&((new numeric(24))->setflag(status_flags::dynallocated)));
361                 _num25_p  = static_cast<const numeric*>(&((new numeric(25))->setflag(status_flags::dynallocated)));
362                 _num30_p  = static_cast<const numeric*>(&((new numeric(30))->setflag(status_flags::dynallocated)));
363                 _num48_p  = static_cast<const numeric*>(&((new numeric(48))->setflag(status_flags::dynallocated)));
364                 _num60_p  = static_cast<const numeric*>(&((new numeric(60))->setflag(status_flags::dynallocated)));
365                 _num120_p = static_cast<const numeric*>(&((new numeric(120))->setflag(status_flags::dynallocated)));
366
367                 // Initialize print context class info (this is not strictly necessary
368                 // but we do it anyway to make print_context_class_info::dump_hierarchy()
369                 // output the whole hierarchy whether or not the classes are actually
370                 // used)
371                 print_context::get_class_info_static();
372                 print_dflt::get_class_info_static();
373                 print_latex::get_class_info_static();
374                 print_python::get_class_info_static();
375                 print_python_repr::get_class_info_static();
376                 print_tree::get_class_info_static();
377                 print_csrc::get_class_info_static();
378                 print_csrc_float::get_class_info_static();
379                 print_csrc_double::get_class_info_static();
380                 print_csrc_cl_N::get_class_info_static();
381         }
382 }
383
384
385 /** Dtor of static initialization helpers.  The last call to this is going
386  *  to shut down the library, the others do nothing. */
387 library_init::~library_init()
388 {
389         if (--count==0) {
390                 // In theory, we would have to clean up here.  But since we were
391                 // only initializing memory in the ctor and that memory is reclaimed
392                 // anyways by the OS when the program exits, we skip this.
393         }
394 }
395
396 // comment skeleton for header files
397
398
399 // member functions
400
401         // default constructor, destructor, copy constructor and assignment operator
402         // none
403
404         // other constructors
405         // none
406
407         // functions overriding virtual functions from base classes
408         // none
409         
410         // new virtual functions which can be overridden by derived classes
411         // none
412
413         // non-virtual functions in this class
414         // none
415
416 // member variables
417 // none
418         
419
420
421 // comment skeleton for implementation files
422
423
424 //////////
425 // default constructor, destructor, copy constructor and assignment operator
426 //////////
427
428 // public
429 // protected
430
431 //////////
432 // other constructors
433 //////////
434
435 // public
436 // none
437
438 //////////
439 // functions overriding virtual functions from base classes
440 //////////
441
442 // public
443 // protected
444 // none
445
446 //////////
447 // new virtual functions which can be overridden by derived classes
448 //////////
449
450 // public
451 // protected
452 // none
453
454 //////////
455 // non-virtual functions in this class
456 //////////
457
458 // public
459 // protected
460 // none
461
462 //////////
463 // static member variables
464 //////////
465
466 // protected
467 // private
468 // none
469
470
471 } // namespace GiNaC