Prettified source code.
[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-2009 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  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 ex _ex_120 = _ex_120;
68
69 // static numeric -60
70 const numeric *_num_60_p;
71 const ex _ex_60 = _ex_60;
72
73 // static numeric -48
74 const numeric *_num_48_p;
75 const ex _ex_48 = _ex_48;
76
77 // static numeric -30
78 const numeric *_num_30_p;
79 const ex _ex_30 = _ex_30;
80
81 // static numeric -25
82 const numeric *_num_25_p;
83 const ex _ex_25 = _ex_25;
84
85 // static numeric -24
86 const numeric *_num_24_p;
87 const ex _ex_24 = _ex_24;
88
89 // static numeric -20
90 const numeric *_num_20_p;
91 const ex _ex_20 = _ex_20;
92
93 // static numeric -18
94 const numeric *_num_18_p;
95 const ex _ex_18 = _ex_18;
96
97 // static numeric -15
98 const numeric *_num_15_p;
99 const ex _ex_15 = _ex_15;
100
101 // static numeric -12
102 const numeric *_num_12_p;
103 const ex _ex_12 = _ex_12;
104
105 // static numeric -11
106 const numeric *_num_11_p;
107 const ex _ex_11 = _ex_11;
108
109 // static numeric -10
110 const numeric *_num_10_p;
111 const ex _ex_10 = _ex_10;
112
113 // static numeric -9
114 const numeric *_num_9_p;
115 const ex _ex_9 = _ex_9;
116
117 // static numeric -8
118 const numeric *_num_8_p;
119 const ex _ex_8 = _ex_8;
120
121 // static numeric -7
122 const numeric *_num_7_p;
123 const ex _ex_7 = _ex_7;
124
125 // static numeric -6
126 const numeric *_num_6_p;
127 const ex _ex_6 = _ex_6;
128
129 // static numeric -5
130 const numeric *_num_5_p;
131 const ex _ex_5 = _ex_5;
132
133 // static numeric -4
134 const numeric *_num_4_p;
135 const ex _ex_4 = _ex_4;
136
137 // static numeric -3
138 const numeric *_num_3_p;
139 const ex _ex_3 = _ex_3;
140
141 // static numeric -2
142 const numeric *_num_2_p;
143 const ex _ex_2 = _ex_2;
144
145 // static numeric -1
146 const numeric *_num_1_p;
147 const ex _ex_1 = _ex_1;
148
149 // static numeric -1/2
150 const numeric *_num_1_2_p;
151 const ex _ex_1_2= _ex_1_2;
152
153 // static numeric -1/3
154 const numeric *_num_1_3_p;
155 const ex _ex_1_3= _ex_1_3;
156
157 // static numeric -1/4
158 const numeric *_num_1_4_p;
159 const ex _ex_1_4= _ex_1_4;
160
161 // static numeric 0
162 const numeric *_num0_p;
163 const basic *_num0_bp;
164 const ex _ex0 = _ex0;
165
166 // static numeric 1/4
167 const numeric *_num1_4_p;
168 const ex _ex1_4 = _ex1_4;
169
170 // static numeric 1/3
171 const numeric *_num1_3_p;
172 const ex _ex1_3 = _ex1_3;
173
174 // static numeric 1/2
175 const numeric *_num1_2_p;
176 const ex _ex1_2 = _ex1_2;
177
178 // static numeric 1
179 const numeric *_num1_p;
180 const ex _ex1 = _ex1;
181
182 // static numeric 2
183 const numeric *_num2_p;
184 const ex _ex2 = _ex2;
185
186 // static numeric 3
187 const numeric *_num3_p;
188 const ex _ex3 = _ex3;
189
190 // static numeric 4
191 const numeric *_num4_p;
192 const ex _ex4 = _ex4;
193
194 // static numeric 5
195 const numeric *_num5_p;
196 const ex _ex5 = _ex5;
197
198 // static numeric 6
199 const numeric *_num6_p;
200 const ex _ex6 = _ex6;
201
202 // static numeric 7
203 const numeric *_num7_p;
204 const ex _ex7 = _ex7;
205
206 // static numeric 8
207 const numeric *_num8_p;
208 const ex _ex8 = _ex8;
209
210 // static numeric 9
211 const numeric *_num9_p;
212 const ex _ex9 = _ex9;
213
214 // static numeric 10
215 const numeric *_num10_p;
216 const ex _ex10 = _ex10;
217
218 // static numeric 11
219 const numeric *_num11_p;
220 const ex _ex11 = _ex11;
221
222 // static numeric 12
223 const numeric *_num12_p;
224 const ex _ex12 = _ex12;
225
226 // static numeric 15
227 const numeric *_num15_p;
228 const ex _ex15 = _ex15;
229
230 // static numeric 18
231 const numeric *_num18_p;
232 const ex _ex18 = _ex18;
233
234 // static numeric 20
235 const numeric *_num20_p;
236 const ex _ex20 = _ex20;
237
238 // static numeric 24
239 const numeric *_num24_p;
240 const ex _ex24 = _ex24;
241
242 // static numeric 25
243 const numeric *_num25_p;
244 const ex _ex25 = _ex25;
245
246 // static numeric 30
247 const numeric *_num30_p;
248 const ex _ex30 = _ex30;
249
250 // static numeric 48
251 const numeric *_num48_p;
252 const ex _ex48 = _ex48;
253
254 // static numeric 60
255 const numeric *_num60_p;
256 const ex _ex60 = _ex60;
257
258 // static numeric 120
259 const numeric *_num120_p;
260 const ex _ex120 = _ex120;
261
262 /** Ctor of static initialization helpers.  The fist call to this is going
263  *  to initialize the library, the others do nothing. */
264 library_init::library_init()
265 {
266         if (count++==0) {
267                 (_num_120_p = new numeric(-120))->setflag(status_flags::dynallocated);
268                 (_num_60_p = new numeric(-60))->setflag(status_flags::dynallocated);
269                 (_num_48_p = new numeric(-48))->setflag(status_flags::dynallocated);
270                 (_num_30_p = new numeric(-30))->setflag(status_flags::dynallocated);
271                 (_num_25_p = new numeric(-25))->setflag(status_flags::dynallocated);
272                 (_num_24_p = new numeric(-24))->setflag(status_flags::dynallocated);
273                 (_num_20_p = new numeric(-20))->setflag(status_flags::dynallocated);
274                 (_num_18_p = new numeric(-18))->setflag(status_flags::dynallocated);
275                 (_num_15_p = new numeric(-15))->setflag(status_flags::dynallocated);
276                 (_num_12_p = new numeric(-12))->setflag(status_flags::dynallocated);
277                 (_num_11_p = new numeric(-11))->setflag(status_flags::dynallocated);
278                 (_num_10_p = new numeric(-10))->setflag(status_flags::dynallocated);
279                 (_num_9_p = new numeric(-9))->setflag(status_flags::dynallocated);
280                 (_num_8_p = new numeric(-8))->setflag(status_flags::dynallocated);
281                 (_num_7_p = new numeric(-7))->setflag(status_flags::dynallocated);
282                 (_num_6_p = new numeric(-6))->setflag(status_flags::dynallocated);
283                 (_num_5_p = new numeric(-5))->setflag(status_flags::dynallocated);
284                 (_num_4_p = new numeric(-4))->setflag(status_flags::dynallocated);
285                 (_num_3_p = new numeric(-3))->setflag(status_flags::dynallocated);
286                 (_num_2_p = new numeric(-2))->setflag(status_flags::dynallocated);
287                 (_num_1_p = new numeric(-1))->setflag(status_flags::dynallocated);
288                 (_num_1_2_p = new numeric(-1,2))->setflag(status_flags::dynallocated);
289                 (_num_1_3_p = new numeric(-1,3))->setflag(status_flags::dynallocated);
290                 (_num_1_4_p = new numeric(-1,4))->setflag(status_flags::dynallocated);
291                 (_num0_p = new numeric(0))->setflag(status_flags::dynallocated);
292                 _num0_bp  = _num0_p;  // Cf. class ex default ctor.
293                 (_num1_4_p = new numeric(1,4))->setflag(status_flags::dynallocated);
294                 (_num1_3_p = new numeric(1,3))->setflag(status_flags::dynallocated);
295                 (_num1_2_p = new numeric(1,2))->setflag(status_flags::dynallocated);
296                 (_num1_p = new numeric(1))->setflag(status_flags::dynallocated);
297                 (_num2_p = new numeric(2))->setflag(status_flags::dynallocated);
298                 (_num3_p = new numeric(3))->setflag(status_flags::dynallocated);
299                 (_num4_p = new numeric(4))->setflag(status_flags::dynallocated);
300                 (_num5_p = new numeric(5))->setflag(status_flags::dynallocated);
301                 (_num6_p = new numeric(6))->setflag(status_flags::dynallocated);
302                 (_num7_p = new numeric(7))->setflag(status_flags::dynallocated);
303                 (_num8_p = new numeric(8))->setflag(status_flags::dynallocated);
304                 (_num9_p = new numeric(9))->setflag(status_flags::dynallocated);
305                 (_num10_p = new numeric(10))->setflag(status_flags::dynallocated);
306                 (_num11_p = new numeric(11))->setflag(status_flags::dynallocated);
307                 (_num12_p = new numeric(12))->setflag(status_flags::dynallocated);
308                 (_num15_p = new numeric(15))->setflag(status_flags::dynallocated);
309                 (_num18_p = new numeric(18))->setflag(status_flags::dynallocated);
310                 (_num20_p = new numeric(20))->setflag(status_flags::dynallocated);
311                 (_num24_p = new numeric(24))->setflag(status_flags::dynallocated);
312                 (_num25_p = new numeric(25))->setflag(status_flags::dynallocated);
313                 (_num30_p = new numeric(30))->setflag(status_flags::dynallocated);
314                 (_num48_p = new numeric(48))->setflag(status_flags::dynallocated);
315                 (_num60_p = new numeric(60))->setflag(status_flags::dynallocated);
316                 (_num120_p = new numeric(120))->setflag(status_flags::dynallocated);
317
318                 new((void*)&_ex_120) ex(*_num_120_p);
319                 new((void*)&_ex_60) ex(*_num_60_p);
320                 new((void*)&_ex_48) ex(*_num_48_p);
321                 new((void*)&_ex_30) ex(*_num_30_p);
322                 new((void*)&_ex_25) ex(*_num_25_p);
323                 new((void*)&_ex_24) ex(*_num_24_p);
324                 new((void*)&_ex_20) ex(*_num_20_p);
325                 new((void*)&_ex_18) ex(*_num_18_p);
326                 new((void*)&_ex_15) ex(*_num_15_p);
327                 new((void*)&_ex_12) ex(*_num_12_p);
328                 new((void*)&_ex_11) ex(*_num_11_p);
329                 new((void*)&_ex_10) ex(*_num_10_p);
330                 new((void*)&_ex_9) ex(*_num_9_p);
331                 new((void*)&_ex_8) ex(*_num_8_p);
332                 new((void*)&_ex_7) ex(*_num_7_p);
333                 new((void*)&_ex_6) ex(*_num_6_p);
334                 new((void*)&_ex_5) ex(*_num_5_p);
335                 new((void*)&_ex_4) ex(*_num_4_p);
336                 new((void*)&_ex_3) ex(*_num_3_p);
337                 new((void*)&_ex_2) ex(*_num_2_p);
338                 new((void*)&_ex_1) ex(*_num_1_p);
339                 new((void*)&_ex_1_2) ex(*_num_1_2_p);
340                 new((void*)&_ex_1_3) ex(*_num_1_3_p);
341                 new((void*)&_ex_1_4) ex(*_num_1_4_p);
342                 new((void*)&_ex0) ex(*_num0_p);
343                 new((void*)&_ex1_4) ex(*_num1_4_p);
344                 new((void*)&_ex1_3) ex(*_num1_3_p);
345                 new((void*)&_ex1_2) ex(*_num1_2_p);
346                 new((void*)&_ex1) ex(*_num1_p);
347                 new((void*)&_ex2) ex(*_num2_p);
348                 new((void*)&_ex3) ex(*_num3_p);
349                 new((void*)&_ex4) ex(*_num4_p);
350                 new((void*)&_ex5) ex(*_num5_p);
351                 new((void*)&_ex6) ex(*_num6_p);
352                 new((void*)&_ex7) ex(*_num7_p);
353                 new((void*)&_ex8) ex(*_num8_p);
354                 new((void*)&_ex9) ex(*_num9_p);
355                 new((void*)&_ex10) ex(*_num10_p);
356                 new((void*)&_ex11) ex(*_num11_p);
357                 new((void*)&_ex12) ex(*_num12_p);
358                 new((void*)&_ex15) ex(*_num15_p);
359                 new((void*)&_ex18) ex(*_num18_p);
360                 new((void*)&_ex20) ex(*_num20_p);
361                 new((void*)&_ex24) ex(*_num24_p);
362                 new((void*)&_ex25) ex(*_num25_p);
363                 new((void*)&_ex30) ex(*_num30_p);
364                 new((void*)&_ex48) ex(*_num48_p);
365                 new((void*)&_ex60) ex(*_num60_p);
366                 new((void*)&_ex120) ex(*_num120_p);
367
368                 // Initialize print context class info (this is not strictly necessary
369                 // but we do it anyway to make print_context_class_info::dump_hierarchy()
370                 // output the whole hierarchy whether or not the classes are actually
371                 // used)
372                 print_context::get_class_info_static();
373                 print_dflt::get_class_info_static();
374                 print_latex::get_class_info_static();
375                 print_python::get_class_info_static();
376                 print_python_repr::get_class_info_static();
377                 print_tree::get_class_info_static();
378                 print_csrc::get_class_info_static();
379                 print_csrc_float::get_class_info_static();
380                 print_csrc_double::get_class_info_static();
381                 print_csrc_cl_N::get_class_info_static();
382         }
383 }
384
385
386 /** Dtor of static initialization helpers.  The last call to this is going
387  *  to shut down the library, the others do nothing. */
388 library_init::~library_init()
389 {
390         if (--count==0) {
391                 // It's really necessary to clean up, since the program
392                 // lifetime might not be the same as libginac.{so,dll} one
393                 // (e.g. consider // dlopen/dlsym/dlclose sequence).
394                 delete _num120_p;
395                 delete _num_120_p;
396                 delete _num60_p;
397                 delete _num_60_p;
398                 delete _num48_p;
399                 delete _num_48_p;
400                 delete _num30_p;
401                 delete _num_30_p;
402                 delete _num25_p;
403                 delete _num_25_p;
404                 delete _num24_p;
405                 delete _num_24_p;
406                 delete _num20_p;
407                 delete _num_20_p;
408                 delete _num18_p;
409                 delete _num_18_p;
410                 delete _num15_p;
411                 delete _num_15_p;
412                 delete _num12_p;
413                 delete _num_12_p;
414                 delete _num11_p;
415                 delete _num_11_p;
416                 delete _num10_p;
417                 delete _num_10_p;
418                 delete _num9_p;
419                 delete _num_9_p;
420                 delete _num8_p;
421                 delete _num_8_p;
422                 delete _num7_p;
423                 delete _num_7_p;
424                 delete _num6_p;
425                 delete _num_6_p;
426                 delete _num5_p;
427                 delete _num_5_p;
428                 delete _num4_p;
429                 delete _num_4_p;
430                 delete _num3_p;
431                 delete _num_3_p;
432                 delete _num2_p;
433                 delete _num_2_p;
434                 delete _num1_p;
435                 delete _num_1_p;
436                 delete _num1_2_p;
437                 delete _num_1_2_p;
438                 delete _num1_3_p;
439                 delete _num_1_3_p;
440                 delete _num1_4_p;
441                 delete _num_1_4_p;
442                 delete _num0_p;
443         }
444 }
445
446 void library_init::init_unarchivers() { }
447
448 // comment skeleton for header files
449
450
451 // member functions
452
453         // default constructor, destructor, copy constructor and assignment operator
454         // none
455
456         // other constructors
457         // none
458
459         // functions overriding virtual functions from base classes
460         // none
461         
462         // new virtual functions which can be overridden by derived classes
463         // none
464
465         // non-virtual functions in this class
466         // none
467
468 // member variables
469 // none
470         
471
472
473 // comment skeleton for implementation files
474
475
476 //////////
477 // default constructor, destructor, copy constructor and assignment operator
478 //////////
479
480 // public
481 // protected
482
483 //////////
484 // other constructors
485 //////////
486
487 // public
488 // none
489
490 //////////
491 // functions overriding virtual functions from base classes
492 //////////
493
494 // public
495 // protected
496 // none
497
498 //////////
499 // new virtual functions which can be overridden by derived classes
500 //////////
501
502 // public
503 // protected
504 // none
505
506 //////////
507 // non-virtual functions in this class
508 //////////
509
510 // public
511 // protected
512 // none
513
514 //////////
515 // static member variables
516 //////////
517
518 // protected
519 // private
520 // none
521
522
523 } // namespace GiNaC