]> www.ginac.de Git - ginac.git/blob - ginac/utils.cpp
minor optimization in power::expand_add
[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-2016 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 = (const numeric *)&dynallocate<numeric>(-120);
268                 _num_60_p = (const numeric *)&dynallocate<numeric>(-60);
269                 _num_48_p = (const numeric *)&dynallocate<numeric>(-48);
270                 _num_30_p = (const numeric *)&dynallocate<numeric>(-30);
271                 _num_25_p = (const numeric *)&dynallocate<numeric>(-25);
272                 _num_24_p = (const numeric *)&dynallocate<numeric>(-24);
273                 _num_20_p = (const numeric *)&dynallocate<numeric>(-20);
274                 _num_18_p = (const numeric *)&dynallocate<numeric>(-18);
275                 _num_15_p = (const numeric *)&dynallocate<numeric>(-15);
276                 _num_12_p = (const numeric *)&dynallocate<numeric>(-12);
277                 _num_11_p = (const numeric *)&dynallocate<numeric>(-11);
278                 _num_10_p = (const numeric *)&dynallocate<numeric>(-10);
279                 _num_9_p = (const numeric *)&dynallocate<numeric>(-9);
280                 _num_8_p = (const numeric *)&dynallocate<numeric>(-8);
281                 _num_7_p = (const numeric *)&dynallocate<numeric>(-7);
282                 _num_6_p = (const numeric *)&dynallocate<numeric>(-6);
283                 _num_5_p = (const numeric *)&dynallocate<numeric>(-5);
284                 _num_4_p = (const numeric *)&dynallocate<numeric>(-4);
285                 _num_3_p = (const numeric *)&dynallocate<numeric>(-3);
286                 _num_2_p = (const numeric *)&dynallocate<numeric>(-2);
287                 _num_1_p = (const numeric *)&dynallocate<numeric>(-1);
288                 _num_1_2_p = (const numeric *)&dynallocate<numeric>(-1,2);
289                 _num_1_3_p = (const numeric *)&dynallocate<numeric>(-1,3);
290                 _num_1_4_p = (const numeric *)&dynallocate<numeric>(-1,4);
291                 _num0_p = (const numeric *)&dynallocate<numeric>(0);
292                 _num0_bp  = _num0_p;  // Cf. class ex default ctor.
293                 _num1_4_p = (const numeric *)&dynallocate<numeric>(1,4);
294                 _num1_3_p = (const numeric *)&dynallocate<numeric>(1,3);
295                 _num1_2_p = (const numeric *)&dynallocate<numeric>(1,2);
296                 _num1_p = (const numeric *)&dynallocate<numeric>(1);
297                 _num2_p = (const numeric *)&dynallocate<numeric>(2);
298                 _num3_p = (const numeric *)&dynallocate<numeric>(3);
299                 _num4_p = (const numeric *)&dynallocate<numeric>(4);
300                 _num5_p = (const numeric *)&dynallocate<numeric>(5);
301                 _num6_p = (const numeric *)&dynallocate<numeric>(6);
302                 _num7_p = (const numeric *)&dynallocate<numeric>(7);
303                 _num8_p = (const numeric *)&dynallocate<numeric>(8);
304                 _num9_p = (const numeric *)&dynallocate<numeric>(9);
305                 _num10_p = (const numeric *)&dynallocate<numeric>(10);
306                 _num11_p = (const numeric *)&dynallocate<numeric>(11);
307                 _num12_p = (const numeric *)&dynallocate<numeric>(12);
308                 _num15_p = (const numeric *)&dynallocate<numeric>(15);
309                 _num18_p = (const numeric *)&dynallocate<numeric>(18);
310                 _num20_p = (const numeric *)&dynallocate<numeric>(20);
311                 _num24_p = (const numeric *)&dynallocate<numeric>(24);
312                 _num25_p = (const numeric *)&dynallocate<numeric>(25);
313                 _num30_p = (const numeric *)&dynallocate<numeric>(30);
314                 _num48_p = (const numeric *)&dynallocate<numeric>(48);
315                 _num60_p = (const numeric *)&dynallocate<numeric>(60);
316                 _num120_p = (const numeric *)&dynallocate<numeric>(120);
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                 // Let the ex dtors care for deleting the numerics!
395                 _ex120.~ex();
396                 _ex_120.~ex();
397                 _ex60.~ex();
398                 _ex_60.~ex();
399                 _ex48.~ex();
400                 _ex_48.~ex();
401                 _ex30.~ex();
402                 _ex_30.~ex();
403                 _ex25.~ex();
404                 _ex_25.~ex();
405                 _ex24.~ex();
406                 _ex_24.~ex();
407                 _ex20.~ex();
408                 _ex_20.~ex();
409                 _ex18.~ex();
410                 _ex_18.~ex();
411                 _ex15.~ex();
412                 _ex_15.~ex();
413                 _ex12.~ex();
414                 _ex_12.~ex();
415                 _ex11.~ex();
416                 _ex_11.~ex();
417                 _ex10.~ex();
418                 _ex_10.~ex();
419                 _ex9.~ex();
420                 _ex_9.~ex();
421                 _ex8.~ex();
422                 _ex_8.~ex();
423                 _ex7.~ex();
424                 _ex_7.~ex();
425                 _ex6.~ex();
426                 _ex_6.~ex();
427                 _ex5.~ex();
428                 _ex_5.~ex();
429                 _ex4.~ex();
430                 _ex_4.~ex();
431                 _ex3.~ex();
432                 _ex_3.~ex();
433                 _ex2.~ex();
434                 _ex_2.~ex();
435                 _ex1.~ex();
436                 _ex_1.~ex();
437                 _ex1_2.~ex();
438                 _ex_1_2.~ex();
439                 _ex1_3.~ex();
440                 _ex_1_3.~ex();
441                 _ex1_4.~ex();
442                 _ex_1_4.~ex();
443                 _ex0.~ex();
444         }
445 }
446
447 void library_init::init_unarchivers() { }
448
449 // comment skeleton for header files
450
451
452 // member functions
453
454         // default constructor, destructor, copy constructor and assignment operator
455         // none
456
457         // other constructors
458         // none
459
460         // functions overriding virtual functions from base classes
461         // none
462         
463         // new virtual functions which can be overridden by derived classes
464         // none
465
466         // non-virtual functions in this class
467         // none
468
469 // member variables
470 // none
471         
472
473
474 // comment skeleton for implementation files
475
476
477 //////////
478 // default constructor, destructor, copy constructor and assignment operator
479 //////////
480
481 // public
482 // protected
483
484 //////////
485 // other constructors
486 //////////
487
488 // public
489 // none
490
491 //////////
492 // functions overriding virtual functions from base classes
493 //////////
494
495 // public
496 // protected
497 // none
498
499 //////////
500 // new virtual functions which can be overridden by derived classes
501 //////////
502
503 // public
504 // protected
505 // none
506
507 //////////
508 // non-virtual functions in this class
509 //////////
510
511 // public
512 // protected
513 // none
514
515 //////////
516 // static member variables
517 //////////
518
519 // protected
520 // private
521 // none
522
523
524 } // namespace GiNaC