]> www.ginac.de Git - ginac.git/blob - ginac/numeric.cpp
the destructor, copy constructor, and assignment operator (which were the
[ginac.git] / ginac / numeric.cpp
1 /** @file numeric.cpp
2  *
3  *  This file contains the interface to the underlying bignum package.
4  *  Its most important design principle is to completely hide the inner
5  *  working of that other package from the user of GiNaC.  It must either 
6  *  provide implementation of arithmetic operators and numerical evaluation
7  *  of special functions or implement the interface to the bignum package. */
8
9 /*
10  *  GiNaC Copyright (C) 1999-2001 Johannes Gutenberg University Mainz, Germany
11  *
12  *  This program is free software; you can redistribute it and/or modify
13  *  it under the terms of the GNU General Public License as published by
14  *  the Free Software Foundation; either version 2 of the License, or
15  *  (at your option) any later version.
16  *
17  *  This program is distributed in the hope that it will be useful,
18  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  *  GNU General Public License for more details.
21  *
22  *  You should have received a copy of the GNU General Public License
23  *  along with this program; if not, write to the Free Software
24  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25  */
26
27 #include "config.h"
28
29 #include <vector>
30 #include <stdexcept>
31 #include <string>
32
33 #if defined(HAVE_SSTREAM)
34 #include <sstream>
35 #elif defined(HAVE_STRSTREAM)
36 #include <strstream>
37 #else
38 #error Need either sstream or strstream
39 #endif
40
41 #include "numeric.h"
42 #include "ex.h"
43 #include "archive.h"
44 #include "debugmsg.h"
45 #include "utils.h"
46
47 // CLN should pollute the global namespace as little as possible.  Hence, we
48 // include most of it here and include only the part needed for properly
49 // declaring cln::cl_number in numeric.h.  This can only be safely done in
50 // namespaced versions of CLN, i.e. version > 1.1.0.  Also, we only need a
51 // subset of CLN, so we don't include the complete <cln/cln.h> but only the
52 // essential stuff:
53 #include <cln/output.h>
54 #include <cln/integer_io.h>
55 #include <cln/integer_ring.h>
56 #include <cln/rational_io.h>
57 #include <cln/rational_ring.h>
58 #include <cln/lfloat_class.h>
59 #include <cln/lfloat_io.h>
60 #include <cln/real_io.h>
61 #include <cln/real_ring.h>
62 #include <cln/complex_io.h>
63 #include <cln/complex_ring.h>
64 #include <cln/numtheory.h>
65
66 #ifndef NO_NAMESPACE_GINAC
67 namespace GiNaC {
68 #endif  // ndef NO_NAMESPACE_GINAC
69
70 GINAC_IMPLEMENT_REGISTERED_CLASS(numeric, basic)
71
72 //////////
73 // default constructor, destructor, copy constructor assignment
74 // operator and helpers
75 //////////
76
77 // public
78
79 /** default ctor. Numerically it initializes to an integer zero. */
80 numeric::numeric() : basic(TINFO_numeric)
81 {
82         debugmsg("numeric default constructor", LOGLEVEL_CONSTRUCT);
83         value = cln::cl_I(0);
84         calchash();
85         setflag(status_flags::evaluated |
86                 status_flags::expanded |
87                 status_flags::hash_calculated);
88 }
89
90 // protected
91
92 void numeric::copy(const numeric & other)
93 {
94         basic::copy(other);
95         value = other.value;
96 }
97
98 void numeric::destroy(bool call_parent)
99 {
100         if (call_parent) basic::destroy(call_parent);
101 }
102
103 //////////
104 // other constructors
105 //////////
106
107 // public
108
109 numeric::numeric(int i) : basic(TINFO_numeric)
110 {
111         debugmsg("numeric constructor from int",LOGLEVEL_CONSTRUCT);
112         // Not the whole int-range is available if we don't cast to long
113         // first.  This is due to the behaviour of the cl_I-ctor, which
114         // emphasizes efficiency.  However, if the integer is small enough, 
115         // i.e. satisfies cl_immediate_p(), we save space and dereferences by
116         // using an immediate type:
117         if (cln::cl_immediate_p(i))
118                 value = cln::cl_I(i);
119         else
120                 value = cln::cl_I((long) i);
121         calchash();
122         setflag(status_flags::evaluated |
123                 status_flags::expanded |
124                 status_flags::hash_calculated);
125 }
126
127
128 numeric::numeric(unsigned int i) : basic(TINFO_numeric)
129 {
130         debugmsg("numeric constructor from uint",LOGLEVEL_CONSTRUCT);
131         // Not the whole uint-range is available if we don't cast to ulong
132         // first.  This is due to the behaviour of the cl_I-ctor, which
133         // emphasizes efficiency.  However, if the integer is small enough, 
134         // i.e. satisfies cl_immediate_p(), we save space and dereferences by
135         // using an immediate type:
136         if (cln::cl_immediate_p(i))
137                 value = cln::cl_I(i);
138         else
139                 value = cln::cl_I((unsigned long) i);
140         calchash();
141         setflag(status_flags::evaluated |
142                 status_flags::expanded |
143                 status_flags::hash_calculated);
144 }
145
146
147 numeric::numeric(long i) : basic(TINFO_numeric)
148 {
149         debugmsg("numeric constructor from long",LOGLEVEL_CONSTRUCT);
150         value = cln::cl_I(i);
151         calchash();
152         setflag(status_flags::evaluated |
153                 status_flags::expanded |
154                 status_flags::hash_calculated);
155 }
156
157
158 numeric::numeric(unsigned long i) : basic(TINFO_numeric)
159 {
160         debugmsg("numeric constructor from ulong",LOGLEVEL_CONSTRUCT);
161         value = cln::cl_I(i);
162         calchash();
163         setflag(status_flags::evaluated |
164                 status_flags::expanded |
165                 status_flags::hash_calculated);
166 }
167
168 /** Ctor for rational numerics a/b.
169  *
170  *  @exception overflow_error (division by zero) */
171 numeric::numeric(long numer, long denom) : basic(TINFO_numeric)
172 {
173         debugmsg("numeric constructor from long/long",LOGLEVEL_CONSTRUCT);
174         if (!denom)
175                 throw std::overflow_error("division by zero");
176         value = cln::cl_I(numer) / cln::cl_I(denom);
177         calchash();
178         setflag(status_flags::evaluated |
179                 status_flags::expanded |
180                 status_flags::hash_calculated);
181 }
182
183
184 numeric::numeric(double d) : basic(TINFO_numeric)
185 {
186         debugmsg("numeric constructor from double",LOGLEVEL_CONSTRUCT);
187         // We really want to explicitly use the type cl_LF instead of the
188         // more general cl_F, since that would give us a cl_DF only which
189         // will not be promoted to cl_LF if overflow occurs:
190         value = cln::cl_float(d, cln::default_float_format);
191         calchash();
192         setflag(status_flags::evaluated |
193                 status_flags::expanded |
194                 status_flags::hash_calculated);
195 }
196
197 /** ctor from C-style string.  It also accepts complex numbers in GiNaC
198  *  notation like "2+5*I". */
199 numeric::numeric(const char *s) : basic(TINFO_numeric)
200 {
201         debugmsg("numeric constructor from string",LOGLEVEL_CONSTRUCT);
202         cln::cl_N ctorval = 0;
203         // parse complex numbers (functional but not completely safe, unfortunately
204         // std::string does not understand regexpese):
205         // ss should represent a simple sum like 2+5*I
206         std::string ss(s);
207         // make it safe by adding explicit sign
208         if (ss.at(0) != '+' && ss.at(0) != '-' && ss.at(0) != '#')
209                 ss = '+' + ss;
210         std::string::size_type delim;
211         do {
212                 // chop ss into terms from left to right
213                 std::string term;
214                 bool imaginary = false;
215                 delim = ss.find_first_of(std::string("+-"),1);
216                 // Do we have an exponent marker like "31.415E-1"?  If so, hop on!
217                 if ((delim != std::string::npos) && (ss.at(delim-1) == 'E'))
218                         delim = ss.find_first_of(std::string("+-"),delim+1);
219                 term = ss.substr(0,delim);
220                 if (delim != std::string::npos)
221                         ss = ss.substr(delim);
222                 // is the term imaginary?
223                 if (term.find("I") != std::string::npos) {
224                         // erase 'I':
225                         term = term.replace(term.find("I"),1,"");
226                         // erase '*':
227                         if (term.find("*") != std::string::npos)
228                                 term = term.replace(term.find("*"),1,"");
229                         // correct for trivial +/-I without explicit factor on I:
230                         if (term.size() == 1)
231                                 term += "1";
232                         imaginary = true;
233                 }
234                 if (term.find(".") != std::string::npos) {
235                         // CLN's short type cl_SF is not very useful within the GiNaC
236                         // framework where we are mainly interested in the arbitrary
237                         // precision type cl_LF.  Hence we go straight to the construction
238                         // of generic floats.  In order to create them we have to convert
239                         // our own floating point notation used for output and construction
240                         // from char * to CLN's generic notation:
241                         // 3.14      -->   3.14e0_<Digits>
242                         // 31.4E-1   -->   31.4e-1_<Digits>
243                         // and s on.
244                         // No exponent marker?  Let's add a trivial one.
245                         if (term.find("E") == std::string::npos)
246                                 term += "E0";
247                         // E to lower case
248                         term = term.replace(term.find("E"),1,"e");
249                         // append _<Digits> to term
250 #if defined(HAVE_SSTREAM)
251                         std::ostringstream buf;
252                         buf << unsigned(Digits) << std::ends;
253                         term += "_" + buf.str();
254 #else
255                         char buf[14];
256                         std::ostrstream(buf,sizeof(buf)) << unsigned(Digits) << std::ends;
257                         term += "_" + string(buf);
258 #endif
259                         // construct float using cln::cl_F(const char *) ctor.
260                         if (imaginary)
261                                 ctorval = ctorval + cln::complex(cln::cl_I(0),cln::cl_F(term.c_str()));
262                         else
263                                 ctorval = ctorval + cln::cl_F(term.c_str());
264                 } else {
265                         // not a floating point number...
266                         if (imaginary)
267                                 ctorval = ctorval + cln::complex(cln::cl_I(0),cln::cl_R(term.c_str()));
268                         else
269                                 ctorval = ctorval + cln::cl_R(term.c_str());
270                 }
271         } while(delim != std::string::npos);
272         value = ctorval;
273         calchash();
274         setflag(status_flags::evaluated |
275                         status_flags::expanded |
276                         status_flags::hash_calculated);
277 }
278
279 /** Ctor from CLN types.  This is for the initiated user or internal use
280  *  only. */
281 numeric::numeric(const cln::cl_N & z) : basic(TINFO_numeric)
282 {
283         debugmsg("numeric constructor from cl_N", LOGLEVEL_CONSTRUCT);
284         value = z;
285         calchash();
286         setflag(status_flags::evaluated |
287                 status_flags::expanded |
288                 status_flags::hash_calculated);
289 }
290
291 //////////
292 // archiving
293 //////////
294
295 /** Construct object from archive_node. */
296 numeric::numeric(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
297 {
298         debugmsg("numeric constructor from archive_node", LOGLEVEL_CONSTRUCT);
299         cln::cl_N ctorval = 0;
300
301         // Read number as string
302         std::string str;
303         if (n.find_string("number", str)) {
304 #ifdef HAVE_SSTREAM
305                 std::istringstream s(str);
306 #else
307                 std::istrstream s(str.c_str(), str.size() + 1);
308 #endif
309                 cln::cl_idecoded_float re, im;
310                 char c;
311                 s.get(c);
312                 switch (c) {
313                         case 'R':    // Integer-decoded real number
314                                 s >> re.sign >> re.mantissa >> re.exponent;
315                                 ctorval = re.sign * re.mantissa * cln::expt(cln::cl_float(2.0, cln::default_float_format), re.exponent);
316                                 break;
317                         case 'C':    // Integer-decoded complex number
318                                 s >> re.sign >> re.mantissa >> re.exponent;
319                                 s >> im.sign >> im.mantissa >> im.exponent;
320                                 ctorval = cln::complex(re.sign * re.mantissa * cln::expt(cln::cl_float(2.0, cln::default_float_format), re.exponent),
321                                                        im.sign * im.mantissa * cln::expt(cln::cl_float(2.0, cln::default_float_format), im.exponent));
322                                 break;
323                         default:    // Ordinary number
324                                 s.putback(c);
325                                 s >> ctorval;
326                                 break;
327                 }
328         }
329         value = ctorval;
330         calchash();
331         setflag(status_flags::evaluated |
332                 status_flags::expanded |
333                 status_flags::hash_calculated);
334 }
335
336 /** Unarchive the object. */
337 ex numeric::unarchive(const archive_node &n, const lst &sym_lst)
338 {
339         return (new numeric(n, sym_lst))->setflag(status_flags::dynallocated);
340 }
341
342 /** Archive the object. */
343 void numeric::archive(archive_node &n) const
344 {
345         inherited::archive(n);
346
347         // Write number as string
348 #ifdef HAVE_SSTREAM
349         std::ostringstream s;
350 #else
351         char buf[1024];
352         std::ostrstream s(buf, 1024);
353 #endif
354         if (this->is_crational())
355                 s << cln::the<cln::cl_N>(value);
356         else {
357                 // Non-rational numbers are written in an integer-decoded format
358                 // to preserve the precision
359                 if (this->is_real()) {
360                         cln::cl_idecoded_float re = cln::integer_decode_float(cln::the<cln::cl_F>(value));
361                         s << "R";
362                         s << re.sign << " " << re.mantissa << " " << re.exponent;
363                 } else {
364                         cln::cl_idecoded_float re = cln::integer_decode_float(cln::the<cln::cl_F>(cln::realpart(cln::the<cln::cl_N>(value))));
365                         cln::cl_idecoded_float im = cln::integer_decode_float(cln::the<cln::cl_F>(cln::imagpart(cln::the<cln::cl_N>(value))));
366                         s << "C";
367                         s << re.sign << " " << re.mantissa << " " << re.exponent << " ";
368                         s << im.sign << " " << im.mantissa << " " << im.exponent;
369                 }
370         }
371 #ifdef HAVE_SSTREAM
372         n.add_string("number", s.str());
373 #else
374         s << ends;
375         std::string str(buf);
376         n.add_string("number", str);
377 #endif
378 }
379
380 //////////
381 // functions overriding virtual functions from bases classes
382 //////////
383
384 // public
385
386 basic * numeric::duplicate() const
387 {
388         debugmsg("numeric duplicate", LOGLEVEL_DUPLICATE);
389         return new numeric(*this);
390 }
391
392
393 /** Helper function to print a real number in a nicer way than is CLN's
394  *  default.  Instead of printing 42.0L0 this just prints 42.0 to ostream os
395  *  and instead of 3.99168L7 it prints 3.99168E7.  This is fine in GiNaC as
396  *  long as it only uses cl_LF and no other floating point types that we might
397  *  want to visibly distinguish from cl_LF.
398  *
399  *  @see numeric::print() */
400 static void print_real_number(std::ostream & os, const cln::cl_R & num)
401 {
402         cln::cl_print_flags ourflags;
403         if (cln::instanceof(num, cln::cl_RA_ring)) {
404                 // case 1: integer or rational, nothing special to do:
405                 cln::print_real(os, ourflags, num);
406         } else {
407                 // case 2: float
408                 // make CLN believe this number has default_float_format, so it prints
409                 // 'E' as exponent marker instead of 'L':
410                 ourflags.default_float_format = cln::float_format(cln::the<cln::cl_F>(num));
411                 cln::print_real(os, ourflags, num);
412         }
413         return;
414 }
415
416 /** This method adds to the output so it blends more consistently together
417  *  with the other routines and produces something compatible to ginsh input.
418  *  
419  *  @see print_real_number() */
420 void numeric::print(std::ostream & os, unsigned upper_precedence) const
421 {
422         debugmsg("numeric print", LOGLEVEL_PRINT);
423         cln::cl_R r = cln::realpart(cln::the<cln::cl_N>(value));
424         cln::cl_R i = cln::imagpart(cln::the<cln::cl_N>(value));
425         if (cln::zerop(i)) {
426                 // case 1, real:  x  or  -x
427                 if ((precedence<=upper_precedence) && (!this->is_nonneg_integer())) {
428                         os << "(";
429                         print_real_number(os, r);
430                         os << ")";
431                 } else {
432                         print_real_number(os, r);
433                 }
434         } else {
435                 if (cln::zerop(r)) {
436                         // case 2, imaginary:  y*I  or  -y*I
437                         if ((precedence<=upper_precedence) && (i < 0)) {
438                                 if (i == -1) {
439                                         os << "(-I)";
440                                 } else {
441                                         os << "(";
442                                         print_real_number(os, i);
443                                         os << "*I)";
444                                 }
445                         } else {
446                                 if (i == 1) {
447                                         os << "I";
448                                 } else {
449                                         if (i == -1) {
450                                                 os << "-I";
451                                         } else {
452                                                 print_real_number(os, i);
453                                                 os << "*I";
454                                         }
455                                 }
456                         }
457                 } else {
458                         // case 3, complex:  x+y*I  or  x-y*I  or  -x+y*I  or  -x-y*I
459                         if (precedence <= upper_precedence)
460                                 os << "(";
461                         print_real_number(os, r);
462                         if (i < 0) {
463                                 if (i == -1) {
464                                         os << "-I";
465                                 } else {
466                                         print_real_number(os, i);
467                                         os << "*I";
468                                 }
469                         } else {
470                                 if (i == 1) {
471                                         os << "+I";
472                                 } else {
473                                         os << "+";
474                                         print_real_number(os, i);
475                                         os << "*I";
476                                 }
477                         }
478                         if (precedence <= upper_precedence)
479                                 os << ")";
480                 }
481         }
482 }
483
484
485 void numeric::printraw(std::ostream & os) const
486 {
487         // The method printraw doesn't do much, it simply uses CLN's operator<<()
488         // for output, which is ugly but reliable. e.g: 2+2i
489         debugmsg("numeric printraw", LOGLEVEL_PRINT);
490         os << "numeric(" << cln::the<cln::cl_N>(value) << ")";
491 }
492
493
494 void numeric::printtree(std::ostream & os, unsigned indent) const
495 {
496         debugmsg("numeric printtree", LOGLEVEL_PRINT);
497         os << std::string(indent,' ') << cln::the<cln::cl_N>(value)
498            << " (numeric): "
499            << "hash=" << hashvalue
500            << " (0x" << std::hex << hashvalue << std::dec << ")"
501            << ", flags=" << flags << std::endl;
502 }
503
504
505 void numeric::printcsrc(std::ostream & os, unsigned type, unsigned upper_precedence) const
506 {
507         debugmsg("numeric print csrc", LOGLEVEL_PRINT);
508         std::ios::fmtflags oldflags = os.flags();
509         os.setf(std::ios::scientific);
510         if (this->is_rational() && !this->is_integer()) {
511                 if (compare(_num0()) > 0) {
512                         os << "(";
513                         if (type == csrc_types::ctype_cl_N)
514                                 os << "cln::cl_F(\"" << numer().evalf() << "\")";
515                         else
516                                 os << numer().to_double();
517                 } else {
518                         os << "-(";
519                         if (type == csrc_types::ctype_cl_N)
520                                 os << "cln::cl_F(\"" << -numer().evalf() << "\")";
521                         else
522                                 os << -numer().to_double();
523                 }
524                 os << "/";
525                 if (type == csrc_types::ctype_cl_N)
526                         os << "cln::cl_F(\"" << denom().evalf() << "\")";
527                 else
528                         os << denom().to_double();
529                 os << ")";
530         } else {
531                 if (type == csrc_types::ctype_cl_N)
532                         os << "cln::cl_F(\"" << evalf() << "\")";
533                 else
534                         os << to_double();
535         }
536         os.flags(oldflags);
537 }
538
539
540 bool numeric::info(unsigned inf) const
541 {
542         switch (inf) {
543                 case info_flags::numeric:
544                 case info_flags::polynomial:
545                 case info_flags::rational_function:
546                         return true;
547                 case info_flags::real:
548                         return is_real();
549                 case info_flags::rational:
550                 case info_flags::rational_polynomial:
551                         return is_rational();
552                 case info_flags::crational:
553                 case info_flags::crational_polynomial:
554                         return is_crational();
555                 case info_flags::integer:
556                 case info_flags::integer_polynomial:
557                         return is_integer();
558                 case info_flags::cinteger:
559                 case info_flags::cinteger_polynomial:
560                         return is_cinteger();
561                 case info_flags::positive:
562                         return is_positive();
563                 case info_flags::negative:
564                         return is_negative();
565                 case info_flags::nonnegative:
566                         return !is_negative();
567                 case info_flags::posint:
568                         return is_pos_integer();
569                 case info_flags::negint:
570                         return is_integer() && is_negative();
571                 case info_flags::nonnegint:
572                         return is_nonneg_integer();
573                 case info_flags::even:
574                         return is_even();
575                 case info_flags::odd:
576                         return is_odd();
577                 case info_flags::prime:
578                         return is_prime();
579                 case info_flags::algebraic:
580                         return !is_real();
581         }
582         return false;
583 }
584
585 /** Disassemble real part and imaginary part to scan for the occurrence of a
586  *  single number.  Also handles the imaginary unit.  It ignores the sign on
587  *  both this and the argument, which may lead to what might appear as funny
588  *  results:  (2+I).has(-2) -> true.  But this is consistent, since we also
589  *  would like to have (-2+I).has(2) -> true and we want to think about the
590  *  sign as a multiplicative factor. */
591 bool numeric::has(const ex & other) const
592 {
593         if (!is_exactly_of_type(*other.bp, numeric))
594                 return false;
595         const numeric & o = static_cast<numeric &>(const_cast<basic &>(*other.bp));
596         if (this->is_equal(o) || this->is_equal(-o))
597                 return true;
598         if (o.imag().is_zero())  // e.g. scan for 3 in -3*I
599                 return (this->real().is_equal(o) || this->imag().is_equal(o) ||
600                         this->real().is_equal(-o) || this->imag().is_equal(-o));
601         else {
602                 if (o.is_equal(I))  // e.g scan for I in 42*I
603                         return !this->is_real();
604                 if (o.real().is_zero())  // e.g. scan for 2*I in 2*I+1
605                         return (this->real().has(o*I) || this->imag().has(o*I) ||
606                                 this->real().has(-o*I) || this->imag().has(-o*I));
607         }
608         return false;
609 }
610
611
612 /** Evaluation of numbers doesn't do anything at all. */
613 ex numeric::eval(int level) const
614 {
615         // Warning: if this is ever gonna do something, the ex ctors from all kinds
616         // of numbers should be checking for status_flags::evaluated.
617         return this->hold();
618 }
619
620
621 /** Cast numeric into a floating-point object.  For example exact numeric(1) is
622  *  returned as a 1.0000000000000000000000 and so on according to how Digits is
623  *  currently set.  In case the object already was a floating point number the
624  *  precision is trimmed to match the currently set default.
625  *
626  *  @param level  ignored, only needed for overriding basic::evalf.
627  *  @return  an ex-handle to a numeric. */
628 ex numeric::evalf(int level) const
629 {
630         // level can safely be discarded for numeric objects.
631         return numeric(cln::cl_float(1.0, cln::default_float_format) *
632                                    (cln::the<cln::cl_N>(value)));
633 }
634
635 // protected
636
637 /** Implementation of ex::diff() for a numeric. It always returns 0.
638  *
639  *  @see ex::diff */
640 ex numeric::derivative(const symbol & s) const
641 {
642         return _ex0();
643 }
644
645
646 int numeric::compare_same_type(const basic & other) const
647 {
648         GINAC_ASSERT(is_exactly_of_type(other, numeric));
649         const numeric & o = static_cast<numeric &>(const_cast<basic &>(other));
650         
651         return this->compare(o);
652 }
653
654
655 bool numeric::is_equal_same_type(const basic & other) const
656 {
657         GINAC_ASSERT(is_exactly_of_type(other,numeric));
658         const numeric *o = static_cast<const numeric *>(&other);
659         
660         return this->is_equal(*o);
661 }
662
663
664 unsigned numeric::calchash(void) const
665 {
666         // Use CLN's hashcode.  Warning: It depends only on the number's value, not
667         // its type or precision (i.e. a true equivalence relation on numbers).  As
668         // a consequence, 3 and 3.0 share the same hashvalue.
669         return (hashvalue = cln::equal_hashcode(cln::the<cln::cl_N>(value)) | 0x80000000U);
670 }
671
672
673 //////////
674 // new virtual functions which can be overridden by derived classes
675 //////////
676
677 // none
678
679 //////////
680 // non-virtual functions in this class
681 //////////
682
683 // public
684
685 /** Numerical addition method.  Adds argument to *this and returns result as
686  *  a new numeric object. */
687 const numeric numeric::add(const numeric & other) const
688 {
689         // Efficiency shortcut: trap the neutral element by pointer.
690         static const numeric * _num0p = &_num0();
691         if (this==_num0p)
692                 return other;
693         else if (&other==_num0p)
694                 return *this;
695         
696         return numeric(cln::the<cln::cl_N>(value)+cln::the<cln::cl_N>(other.value));
697 }
698
699
700 /** Numerical subtraction method.  Subtracts argument from *this and returns
701  *  result as a new numeric object. */
702 const numeric numeric::sub(const numeric & other) const
703 {
704         return numeric(cln::the<cln::cl_N>(value)-cln::the<cln::cl_N>(other.value));
705 }
706
707
708 /** Numerical multiplication method.  Multiplies *this and argument and returns
709  *  result as a new numeric object. */
710 const numeric numeric::mul(const numeric & other) const
711 {
712         // Efficiency shortcut: trap the neutral element by pointer.
713         static const numeric * _num1p = &_num1();
714         if (this==_num1p)
715                 return other;
716         else if (&other==_num1p)
717                 return *this;
718         
719         return numeric(cln::the<cln::cl_N>(value)*cln::the<cln::cl_N>(other.value));
720 }
721
722
723 /** Numerical division method.  Divides *this by argument and returns result as
724  *  a new numeric object.
725  *
726  *  @exception overflow_error (division by zero) */
727 const numeric numeric::div(const numeric & other) const
728 {
729         if (cln::zerop(cln::the<cln::cl_N>(other.value)))
730                 throw std::overflow_error("numeric::div(): division by zero");
731         return numeric(cln::the<cln::cl_N>(value)/cln::the<cln::cl_N>(other.value));
732 }
733
734
735 const numeric numeric::power(const numeric & other) const
736 {
737         // Efficiency shortcut: trap the neutral exponent by pointer.
738         static const numeric * _num1p = &_num1();
739         if (&other==_num1p)
740                 return *this;
741         
742         if (cln::zerop(cln::the<cln::cl_N>(value))) {
743                 if (cln::zerop(cln::the<cln::cl_N>(other.value)))
744                         throw std::domain_error("numeric::eval(): pow(0,0) is undefined");
745                 else if (cln::zerop(cln::realpart(cln::the<cln::cl_N>(other.value))))
746                         throw std::domain_error("numeric::eval(): pow(0,I) is undefined");
747                 else if (cln::minusp(cln::realpart(cln::the<cln::cl_N>(other.value))))
748                         throw std::overflow_error("numeric::eval(): division by zero");
749                 else
750                         return _num0();
751         }
752         return numeric(cln::expt(cln::the<cln::cl_N>(value),cln::the<cln::cl_N>(other.value)));
753 }
754
755
756 const numeric & numeric::add_dyn(const numeric & other) const
757 {
758         // Efficiency shortcut: trap the neutral element by pointer.
759         static const numeric * _num0p = &_num0();
760         if (this==_num0p)
761                 return other;
762         else if (&other==_num0p)
763                 return *this;
764         
765         return static_cast<const numeric &>((new numeric(cln::the<cln::cl_N>(value)+cln::the<cln::cl_N>(other.value)))->
766                                                                                 setflag(status_flags::dynallocated));
767 }
768
769
770 const numeric & numeric::sub_dyn(const numeric & other) const
771 {
772         return static_cast<const numeric &>((new numeric(cln::the<cln::cl_N>(value)-cln::the<cln::cl_N>(other.value)))->
773                                                                                 setflag(status_flags::dynallocated));
774 }
775
776
777 const numeric & numeric::mul_dyn(const numeric & other) const
778 {
779         // Efficiency shortcut: trap the neutral element by pointer.
780         static const numeric * _num1p = &_num1();
781         if (this==_num1p)
782                 return other;
783         else if (&other==_num1p)
784                 return *this;
785         
786         return static_cast<const numeric &>((new numeric(cln::the<cln::cl_N>(value)*cln::the<cln::cl_N>(other.value)))->
787                                                                                 setflag(status_flags::dynallocated));
788 }
789
790
791 const numeric & numeric::div_dyn(const numeric & other) const
792 {
793         if (cln::zerop(cln::the<cln::cl_N>(other.value)))
794                 throw std::overflow_error("division by zero");
795         return static_cast<const numeric &>((new numeric(cln::the<cln::cl_N>(value)/cln::the<cln::cl_N>(other.value)))->
796                                                                                 setflag(status_flags::dynallocated));
797 }
798
799
800 const numeric & numeric::power_dyn(const numeric & other) const
801 {
802         // Efficiency shortcut: trap the neutral exponent by pointer.
803         static const numeric * _num1p=&_num1();
804         if (&other==_num1p)
805                 return *this;
806         
807         if (cln::zerop(cln::the<cln::cl_N>(value))) {
808                 if (cln::zerop(cln::the<cln::cl_N>(other.value)))
809                         throw std::domain_error("numeric::eval(): pow(0,0) is undefined");
810                 else if (cln::zerop(cln::realpart(cln::the<cln::cl_N>(other.value))))
811                         throw std::domain_error("numeric::eval(): pow(0,I) is undefined");
812                 else if (cln::minusp(cln::realpart(cln::the<cln::cl_N>(other.value))))
813                         throw std::overflow_error("numeric::eval(): division by zero");
814                 else
815                         return _num0();
816         }
817         return static_cast<const numeric &>((new numeric(cln::expt(cln::the<cln::cl_N>(value),cln::the<cln::cl_N>(other.value))))->
818                                              setflag(status_flags::dynallocated));
819 }
820
821
822 const numeric & numeric::operator=(int i)
823 {
824         return operator=(numeric(i));
825 }
826
827
828 const numeric & numeric::operator=(unsigned int i)
829 {
830         return operator=(numeric(i));
831 }
832
833
834 const numeric & numeric::operator=(long i)
835 {
836         return operator=(numeric(i));
837 }
838
839
840 const numeric & numeric::operator=(unsigned long i)
841 {
842         return operator=(numeric(i));
843 }
844
845
846 const numeric & numeric::operator=(double d)
847 {
848         return operator=(numeric(d));
849 }
850
851
852 const numeric & numeric::operator=(const char * s)
853 {
854         return operator=(numeric(s));
855 }
856
857
858 /** Inverse of a number. */
859 const numeric numeric::inverse(void) const
860 {
861         if (cln::zerop(cln::the<cln::cl_N>(value)))
862                 throw std::overflow_error("numeric::inverse(): division by zero");
863         return numeric(cln::recip(cln::the<cln::cl_N>(value)));
864 }
865
866
867 /** Return the complex half-plane (left or right) in which the number lies.
868  *  csgn(x)==0 for x==0, csgn(x)==1 for Re(x)>0 or Re(x)=0 and Im(x)>0,
869  *  csgn(x)==-1 for Re(x)<0 or Re(x)=0 and Im(x)<0.
870  *
871  *  @see numeric::compare(const numeric & other) */
872 int numeric::csgn(void) const
873 {
874         if (cln::zerop(cln::the<cln::cl_N>(value)))
875                 return 0;
876         cln::cl_R r = cln::realpart(cln::the<cln::cl_N>(value));
877         if (!cln::zerop(r)) {
878                 if (cln::plusp(r))
879                         return 1;
880                 else
881                         return -1;
882         } else {
883                 if (cln::plusp(cln::imagpart(cln::the<cln::cl_N>(value))))
884                         return 1;
885                 else
886                         return -1;
887         }
888 }
889
890
891 /** This method establishes a canonical order on all numbers.  For complex
892  *  numbers this is not possible in a mathematically consistent way but we need
893  *  to establish some order and it ought to be fast.  So we simply define it
894  *  to be compatible with our method csgn.
895  *
896  *  @return csgn(*this-other)
897  *  @see numeric::csgn(void) */
898 int numeric::compare(const numeric & other) const
899 {
900         // Comparing two real numbers?
901         if (cln::instanceof(value, cln::cl_R_ring) &&
902                 cln::instanceof(other.value, cln::cl_R_ring))
903                 // Yes, so just cln::compare them
904                 return cln::compare(cln::the<cln::cl_R>(value), cln::the<cln::cl_R>(other.value));
905         else {
906                 // No, first cln::compare real parts...
907                 cl_signean real_cmp = cln::compare(cln::realpart(cln::the<cln::cl_N>(value)), cln::realpart(cln::the<cln::cl_N>(other.value)));
908                 if (real_cmp)
909                         return real_cmp;
910                 // ...and then the imaginary parts.
911                 return cln::compare(cln::imagpart(cln::the<cln::cl_N>(value)), cln::imagpart(cln::the<cln::cl_N>(other.value)));
912         }
913 }
914
915
916 bool numeric::is_equal(const numeric & other) const
917 {
918         return cln::equal(cln::the<cln::cl_N>(value),cln::the<cln::cl_N>(other.value));
919 }
920
921
922 /** True if object is zero. */
923 bool numeric::is_zero(void) const
924 {
925         return cln::zerop(cln::the<cln::cl_N>(value));
926 }
927
928
929 /** True if object is not complex and greater than zero. */
930 bool numeric::is_positive(void) const
931 {
932         if (this->is_real())
933                 return cln::plusp(cln::the<cln::cl_R>(value));
934         return false;
935 }
936
937
938 /** True if object is not complex and less than zero. */
939 bool numeric::is_negative(void) const
940 {
941         if (this->is_real())
942                 return cln::minusp(cln::the<cln::cl_R>(value));
943         return false;
944 }
945
946
947 /** True if object is a non-complex integer. */
948 bool numeric::is_integer(void) const
949 {
950         return cln::instanceof(value, cln::cl_I_ring);
951 }
952
953
954 /** True if object is an exact integer greater than zero. */
955 bool numeric::is_pos_integer(void) const
956 {
957         return (this->is_integer() && cln::plusp(cln::the<cln::cl_I>(value)));
958 }
959
960
961 /** True if object is an exact integer greater or equal zero. */
962 bool numeric::is_nonneg_integer(void) const
963 {
964         return (this->is_integer() && !cln::minusp(cln::the<cln::cl_I>(value)));
965 }
966
967
968 /** True if object is an exact even integer. */
969 bool numeric::is_even(void) const
970 {
971         return (this->is_integer() && cln::evenp(cln::the<cln::cl_I>(value)));
972 }
973
974
975 /** True if object is an exact odd integer. */
976 bool numeric::is_odd(void) const
977 {
978         return (this->is_integer() && cln::oddp(cln::the<cln::cl_I>(value)));
979 }
980
981
982 /** Probabilistic primality test.
983  *
984  *  @return  true if object is exact integer and prime. */
985 bool numeric::is_prime(void) const
986 {
987         return (this->is_integer() && cln::isprobprime(cln::the<cln::cl_I>(value)));
988 }
989
990
991 /** True if object is an exact rational number, may even be complex
992  *  (denominator may be unity). */
993 bool numeric::is_rational(void) const
994 {
995         return cln::instanceof(value, cln::cl_RA_ring);
996 }
997
998
999 /** True if object is a real integer, rational or float (but not complex). */
1000 bool numeric::is_real(void) const
1001 {
1002         return cln::instanceof(value, cln::cl_R_ring);
1003 }
1004
1005
1006 bool numeric::operator==(const numeric & other) const
1007 {
1008         return equal(cln::the<cln::cl_N>(value), cln::the<cln::cl_N>(other.value));
1009 }
1010
1011
1012 bool numeric::operator!=(const numeric & other) const
1013 {
1014         return !equal(cln::the<cln::cl_N>(value), cln::the<cln::cl_N>(other.value));
1015 }
1016
1017
1018 /** True if object is element of the domain of integers extended by I, i.e. is
1019  *  of the form a+b*I, where a and b are integers. */
1020 bool numeric::is_cinteger(void) const
1021 {
1022         if (cln::instanceof(value, cln::cl_I_ring))
1023                 return true;
1024         else if (!this->is_real()) {  // complex case, handle n+m*I
1025                 if (cln::instanceof(cln::realpart(cln::the<cln::cl_N>(value)), cln::cl_I_ring) &&
1026                     cln::instanceof(cln::imagpart(cln::the<cln::cl_N>(value)), cln::cl_I_ring))
1027                         return true;
1028         }
1029         return false;
1030 }
1031
1032
1033 /** True if object is an exact rational number, may even be complex
1034  *  (denominator may be unity). */
1035 bool numeric::is_crational(void) const
1036 {
1037         if (cln::instanceof(value, cln::cl_RA_ring))
1038                 return true;
1039         else if (!this->is_real()) {  // complex case, handle Q(i):
1040                 if (cln::instanceof(cln::realpart(cln::the<cln::cl_N>(value)), cln::cl_RA_ring) &&
1041                     cln::instanceof(cln::imagpart(cln::the<cln::cl_N>(value)), cln::cl_RA_ring))
1042                         return true;
1043         }
1044         return false;
1045 }
1046
1047
1048 /** Numerical comparison: less.
1049  *
1050  *  @exception invalid_argument (complex inequality) */ 
1051 bool numeric::operator<(const numeric & other) const
1052 {
1053         if (this->is_real() && other.is_real())
1054                 return (cln::the<cln::cl_R>(value) < cln::the<cln::cl_R>(other.value));
1055         throw std::invalid_argument("numeric::operator<(): complex inequality");
1056 }
1057
1058
1059 /** Numerical comparison: less or equal.
1060  *
1061  *  @exception invalid_argument (complex inequality) */ 
1062 bool numeric::operator<=(const numeric & other) const
1063 {
1064         if (this->is_real() && other.is_real())
1065                 return (cln::the<cln::cl_R>(value) <= cln::the<cln::cl_R>(other.value));
1066         throw std::invalid_argument("numeric::operator<=(): complex inequality");
1067 }
1068
1069
1070 /** Numerical comparison: greater.
1071  *
1072  *  @exception invalid_argument (complex inequality) */ 
1073 bool numeric::operator>(const numeric & other) const
1074 {
1075         if (this->is_real() && other.is_real())
1076                 return (cln::the<cln::cl_R>(value) > cln::the<cln::cl_R>(other.value));
1077         throw std::invalid_argument("numeric::operator>(): complex inequality");
1078 }
1079
1080
1081 /** Numerical comparison: greater or equal.
1082  *
1083  *  @exception invalid_argument (complex inequality) */  
1084 bool numeric::operator>=(const numeric & other) const
1085 {
1086         if (this->is_real() && other.is_real())
1087                 return (cln::the<cln::cl_R>(value) >= cln::the<cln::cl_R>(other.value));
1088         throw std::invalid_argument("numeric::operator>=(): complex inequality");
1089 }
1090
1091
1092 /** Converts numeric types to machine's int.  You should check with
1093  *  is_integer() if the number is really an integer before calling this method.
1094  *  You may also consider checking the range first. */
1095 int numeric::to_int(void) const
1096 {
1097         GINAC_ASSERT(this->is_integer());
1098         return cln::cl_I_to_int(cln::the<cln::cl_I>(value));
1099 }
1100
1101
1102 /** Converts numeric types to machine's long.  You should check with
1103  *  is_integer() if the number is really an integer before calling this method.
1104  *  You may also consider checking the range first. */
1105 long numeric::to_long(void) const
1106 {
1107         GINAC_ASSERT(this->is_integer());
1108         return cln::cl_I_to_long(cln::the<cln::cl_I>(value));
1109 }
1110
1111
1112 /** Converts numeric types to machine's double. You should check with is_real()
1113  *  if the number is really not complex before calling this method. */
1114 double numeric::to_double(void) const
1115 {
1116         GINAC_ASSERT(this->is_real());
1117         return cln::double_approx(cln::realpart(cln::the<cln::cl_N>(value)));
1118 }
1119
1120
1121 /** Returns a new CLN object of type cl_N, representing the value of *this.
1122  *  This method may be used when mixing GiNaC and CLN in one project.
1123  */
1124 cln::cl_N numeric::to_cl_N(void) const
1125 {
1126         return cln::cl_N(cln::the<cln::cl_N>(value));
1127 }
1128
1129
1130 /** Real part of a number. */
1131 const numeric numeric::real(void) const
1132 {
1133         return numeric(cln::realpart(cln::the<cln::cl_N>(value)));
1134 }
1135
1136
1137 /** Imaginary part of a number. */
1138 const numeric numeric::imag(void) const
1139 {
1140         return numeric(cln::imagpart(cln::the<cln::cl_N>(value)));
1141 }
1142
1143
1144 /** Numerator.  Computes the numerator of rational numbers, rationalized
1145  *  numerator of complex if real and imaginary part are both rational numbers
1146  *  (i.e numer(4/3+5/6*I) == 8+5*I), the number carrying the sign in all other
1147  *  cases. */
1148 const numeric numeric::numer(void) const
1149 {
1150         if (this->is_integer())
1151                 return numeric(*this);
1152         
1153         else if (cln::instanceof(value, cln::cl_RA_ring))
1154                 return numeric(cln::numerator(cln::the<cln::cl_RA>(value)));
1155         
1156         else if (!this->is_real()) {  // complex case, handle Q(i):
1157                 const cln::cl_RA r = cln::the<cln::cl_RA>(cln::realpart(cln::the<cln::cl_N>(value)));
1158                 const cln::cl_RA i = cln::the<cln::cl_RA>(cln::imagpart(cln::the<cln::cl_N>(value)));
1159                 if (cln::instanceof(r, cln::cl_I_ring) && cln::instanceof(i, cln::cl_I_ring))
1160                         return numeric(*this);
1161                 if (cln::instanceof(r, cln::cl_I_ring) && cln::instanceof(i, cln::cl_RA_ring))
1162                         return numeric(cln::complex(r*cln::denominator(i), cln::numerator(i)));
1163                 if (cln::instanceof(r, cln::cl_RA_ring) && cln::instanceof(i, cln::cl_I_ring))
1164                         return numeric(cln::complex(cln::numerator(r), i*cln::denominator(r)));
1165                 if (cln::instanceof(r, cln::cl_RA_ring) && cln::instanceof(i, cln::cl_RA_ring)) {
1166                         const cln::cl_I s = cln::lcm(cln::denominator(r), cln::denominator(i));
1167                         return numeric(cln::complex(cln::numerator(r)*(cln::exquo(s,cln::denominator(r))),
1168                                                             cln::numerator(i)*(cln::exquo(s,cln::denominator(i)))));
1169                 }
1170         }
1171         // at least one float encountered
1172         return numeric(*this);
1173 }
1174
1175
1176 /** Denominator.  Computes the denominator of rational numbers, common integer
1177  *  denominator of complex if real and imaginary part are both rational numbers
1178  *  (i.e denom(4/3+5/6*I) == 6), one in all other cases. */
1179 const numeric numeric::denom(void) const
1180 {
1181         if (this->is_integer())
1182                 return _num1();
1183         
1184         if (instanceof(value, cln::cl_RA_ring))
1185                 return numeric(cln::denominator(cln::the<cln::cl_RA>(value)));
1186         
1187         if (!this->is_real()) {  // complex case, handle Q(i):
1188                 const cln::cl_RA r = cln::the<cln::cl_RA>(cln::realpart(cln::the<cln::cl_N>(value)));
1189                 const cln::cl_RA i = cln::the<cln::cl_RA>(cln::imagpart(cln::the<cln::cl_N>(value)));
1190                 if (cln::instanceof(r, cln::cl_I_ring) && cln::instanceof(i, cln::cl_I_ring))
1191                         return _num1();
1192                 if (cln::instanceof(r, cln::cl_I_ring) && cln::instanceof(i, cln::cl_RA_ring))
1193                         return numeric(cln::denominator(i));
1194                 if (cln::instanceof(r, cln::cl_RA_ring) && cln::instanceof(i, cln::cl_I_ring))
1195                         return numeric(cln::denominator(r));
1196                 if (cln::instanceof(r, cln::cl_RA_ring) && cln::instanceof(i, cln::cl_RA_ring))
1197                         return numeric(cln::lcm(cln::denominator(r), cln::denominator(i)));
1198         }
1199         // at least one float encountered
1200         return _num1();
1201 }
1202
1203
1204 /** Size in binary notation.  For integers, this is the smallest n >= 0 such
1205  *  that -2^n <= x < 2^n. If x > 0, this is the unique n > 0 such that
1206  *  2^(n-1) <= x < 2^n.
1207  *
1208  *  @return  number of bits (excluding sign) needed to represent that number
1209  *  in two's complement if it is an integer, 0 otherwise. */    
1210 int numeric::int_length(void) const
1211 {
1212         if (this->is_integer())
1213                 return cln::integer_length(cln::the<cln::cl_I>(value));
1214         else
1215                 return 0;
1216 }
1217
1218
1219 //////////
1220 // static member variables
1221 //////////
1222
1223 // protected
1224
1225 unsigned numeric::precedence = 30;
1226
1227 //////////
1228 // global constants
1229 //////////
1230
1231 /** Imaginary unit.  This is not a constant but a numeric since we are
1232  *  natively handing complex numbers anyways. */
1233 const numeric I = numeric(cln::complex(cln::cl_I(0),cln::cl_I(1)));
1234
1235
1236 /** Exponential function.
1237  *
1238  *  @return  arbitrary precision numerical exp(x). */
1239 const numeric exp(const numeric & x)
1240 {
1241         return cln::exp(x.to_cl_N());
1242 }
1243
1244
1245 /** Natural logarithm.
1246  *
1247  *  @param z complex number
1248  *  @return  arbitrary precision numerical log(x).
1249  *  @exception pole_error("log(): logarithmic pole",0) */
1250 const numeric log(const numeric & z)
1251 {
1252         if (z.is_zero())
1253                 throw pole_error("log(): logarithmic pole",0);
1254         return cln::log(z.to_cl_N());
1255 }
1256
1257
1258 /** Numeric sine (trigonometric function).
1259  *
1260  *  @return  arbitrary precision numerical sin(x). */
1261 const numeric sin(const numeric & x)
1262 {
1263         return cln::sin(x.to_cl_N());
1264 }
1265
1266
1267 /** Numeric cosine (trigonometric function).
1268  *
1269  *  @return  arbitrary precision numerical cos(x). */
1270 const numeric cos(const numeric & x)
1271 {
1272         return cln::cos(x.to_cl_N());
1273 }
1274
1275
1276 /** Numeric tangent (trigonometric function).
1277  *
1278  *  @return  arbitrary precision numerical tan(x). */
1279 const numeric tan(const numeric & x)
1280 {
1281         return cln::tan(x.to_cl_N());
1282 }
1283         
1284
1285 /** Numeric inverse sine (trigonometric function).
1286  *
1287  *  @return  arbitrary precision numerical asin(x). */
1288 const numeric asin(const numeric & x)
1289 {
1290         return cln::asin(x.to_cl_N());
1291 }
1292
1293
1294 /** Numeric inverse cosine (trigonometric function).
1295  *
1296  *  @return  arbitrary precision numerical acos(x). */
1297 const numeric acos(const numeric & x)
1298 {
1299         return cln::acos(x.to_cl_N());
1300 }
1301         
1302
1303 /** Arcustangent.
1304  *
1305  *  @param z complex number
1306  *  @return atan(z)
1307  *  @exception pole_error("atan(): logarithmic pole",0) */
1308 const numeric atan(const numeric & x)
1309 {
1310         if (!x.is_real() &&
1311             x.real().is_zero() &&
1312             abs(x.imag()).is_equal(_num1()))
1313                 throw pole_error("atan(): logarithmic pole",0);
1314         return cln::atan(x.to_cl_N());
1315 }
1316
1317
1318 /** Arcustangent.
1319  *
1320  *  @param x real number
1321  *  @param y real number
1322  *  @return atan(y/x) */
1323 const numeric atan(const numeric & y, const numeric & x)
1324 {
1325         if (x.is_real() && y.is_real())
1326                 return cln::atan(cln::the<cln::cl_R>(x.to_cl_N()),
1327                                  cln::the<cln::cl_R>(y.to_cl_N()));
1328         else
1329                 throw std::invalid_argument("atan(): complex argument");        
1330 }
1331
1332
1333 /** Numeric hyperbolic sine (trigonometric function).
1334  *
1335  *  @return  arbitrary precision numerical sinh(x). */
1336 const numeric sinh(const numeric & x)
1337 {
1338         return cln::sinh(x.to_cl_N());
1339 }
1340
1341
1342 /** Numeric hyperbolic cosine (trigonometric function).
1343  *
1344  *  @return  arbitrary precision numerical cosh(x). */
1345 const numeric cosh(const numeric & x)
1346 {
1347         return cln::cosh(x.to_cl_N());
1348 }
1349
1350
1351 /** Numeric hyperbolic tangent (trigonometric function).
1352  *
1353  *  @return  arbitrary precision numerical tanh(x). */
1354 const numeric tanh(const numeric & x)
1355 {
1356         return cln::tanh(x.to_cl_N());
1357 }
1358         
1359
1360 /** Numeric inverse hyperbolic sine (trigonometric function).
1361  *
1362  *  @return  arbitrary precision numerical asinh(x). */
1363 const numeric asinh(const numeric & x)
1364 {
1365         return cln::asinh(x.to_cl_N());
1366 }
1367
1368
1369 /** Numeric inverse hyperbolic cosine (trigonometric function).
1370  *
1371  *  @return  arbitrary precision numerical acosh(x). */
1372 const numeric acosh(const numeric & x)
1373 {
1374         return cln::acosh(x.to_cl_N());
1375 }
1376
1377
1378 /** Numeric inverse hyperbolic tangent (trigonometric function).
1379  *
1380  *  @return  arbitrary precision numerical atanh(x). */
1381 const numeric atanh(const numeric & x)
1382 {
1383         return cln::atanh(x.to_cl_N());
1384 }
1385
1386
1387 /*static cln::cl_N Li2_series(const ::cl_N & x,
1388                             const ::float_format_t & prec)
1389 {
1390         // Note: argument must be in the unit circle
1391         // This is very inefficient unless we have fast floating point Bernoulli
1392         // numbers implemented!
1393         cln::cl_N c1 = -cln::log(1-x);
1394         cln::cl_N c2 = c1;
1395         // hard-wire the first two Bernoulli numbers
1396         cln::cl_N acc = c1 - cln::square(c1)/4;
1397         cln::cl_N aug;
1398         cln::cl_F pisq = cln::square(cln::cl_pi(prec));  // pi^2
1399         cln::cl_F piac = cln::cl_float(1, prec);  // accumulator: pi^(2*i)
1400         unsigned i = 1;
1401         c1 = cln::square(c1);
1402         do {
1403                 c2 = c1 * c2;
1404                 piac = piac * pisq;
1405                 aug = c2 * (*(bernoulli(numeric(2*i)).clnptr())) / cln::factorial(2*i+1);
1406                 // aug = c2 * cln::cl_I(i%2 ? 1 : -1) / cln::cl_I(2*i+1) * cln::cl_zeta(2*i, prec) / piac / (cln::cl_I(1)<<(2*i-1));
1407                 acc = acc + aug;
1408                 ++i;
1409         } while (acc != acc+aug);
1410         return acc;
1411 }*/
1412
1413 /** Numeric evaluation of Dilogarithm within circle of convergence (unit
1414  *  circle) using a power series. */
1415 static cln::cl_N Li2_series(const cln::cl_N & x,
1416                             const cln::float_format_t & prec)
1417 {
1418         // Note: argument must be in the unit circle
1419         cln::cl_N aug, acc;
1420         cln::cl_N num = cln::complex(cln::cl_float(1, prec), 0);
1421         cln::cl_I den = 0;
1422         unsigned i = 1;
1423         do {
1424                 num = num * x;
1425                 den = den + i;  // 1, 4, 9, 16, ...
1426                 i += 2;
1427                 aug = num / den;
1428                 acc = acc + aug;
1429         } while (acc != acc+aug);
1430         return acc;
1431 }
1432
1433 /** Folds Li2's argument inside a small rectangle to enhance convergence. */
1434 static cln::cl_N Li2_projection(const cln::cl_N & x,
1435                                 const cln::float_format_t & prec)
1436 {
1437         const cln::cl_R re = cln::realpart(x);
1438         const cln::cl_R im = cln::imagpart(x);
1439         if (re > cln::cl_F(".5"))
1440                 // zeta(2) - Li2(1-x) - log(x)*log(1-x)
1441                 return(cln::zeta(2)
1442                        - Li2_series(1-x, prec)
1443                        - cln::log(x)*cln::log(1-x));
1444         if ((re <= 0 && cln::abs(im) > cln::cl_F(".75")) || (re < cln::cl_F("-.5")))
1445                 // -log(1-x)^2 / 2 - Li2(x/(x-1))
1446                 return(- cln::square(cln::log(1-x))/2
1447                        - Li2_series(x/(x-1), prec));
1448         if (re > 0 && cln::abs(im) > cln::cl_LF(".75"))
1449                 // Li2(x^2)/2 - Li2(-x)
1450                 return(Li2_projection(cln::square(x), prec)/2
1451                        - Li2_projection(-x, prec));
1452         return Li2_series(x, prec);
1453 }
1454
1455 /** Numeric evaluation of Dilogarithm.  The domain is the entire complex plane,
1456  *  the branch cut lies along the positive real axis, starting at 1 and
1457  *  continuous with quadrant IV.
1458  *
1459  *  @return  arbitrary precision numerical Li2(x). */
1460 const numeric Li2(const numeric & x)
1461 {
1462         if (x.is_zero())
1463                 return _num0();
1464         
1465         // what is the desired float format?
1466         // first guess: default format
1467         cln::float_format_t prec = cln::default_float_format;
1468         const cln::cl_N value = x.to_cl_N();
1469         // second guess: the argument's format
1470         if (!x.real().is_rational())
1471                 prec = cln::float_format(cln::the<cln::cl_F>(cln::realpart(value)));
1472         else if (!x.imag().is_rational())
1473                 prec = cln::float_format(cln::the<cln::cl_F>(cln::imagpart(value)));
1474         
1475         if (cln::the<cln::cl_N>(value)==1)  // may cause trouble with log(1-x)
1476                 return cln::zeta(2, prec);
1477         
1478         if (cln::abs(value) > 1)
1479                 // -log(-x)^2 / 2 - zeta(2) - Li2(1/x)
1480                 return(- cln::square(cln::log(-value))/2
1481                        - cln::zeta(2, prec)
1482                        - Li2_projection(cln::recip(value), prec));
1483         else
1484                 return Li2_projection(x.to_cl_N(), prec);
1485 }
1486
1487
1488 /** Numeric evaluation of Riemann's Zeta function.  Currently works only for
1489  *  integer arguments. */
1490 const numeric zeta(const numeric & x)
1491 {
1492         // A dirty hack to allow for things like zeta(3.0), since CLN currently
1493         // only knows about integer arguments and zeta(3).evalf() automatically
1494         // cascades down to zeta(3.0).evalf().  The trick is to rely on 3.0-3
1495         // being an exact zero for CLN, which can be tested and then we can just
1496         // pass the number casted to an int:
1497         if (x.is_real()) {
1498                 const int aux = (int)(cln::double_approx(cln::the<cln::cl_R>(x.to_cl_N())));
1499                 if (cln::zerop(x.to_cl_N()-aux))
1500                         return cln::zeta(aux);
1501         }
1502         std::clog << "zeta(" << x
1503                           << "): Does anybody know good way to calculate this numerically?"
1504                           << std::endl;
1505         return numeric(0);
1506 }
1507
1508
1509 /** The Gamma function.
1510  *  This is only a stub! */
1511 const numeric lgamma(const numeric & x)
1512 {
1513         std::clog << "lgamma(" << x
1514                   << "): Does anybody know good way to calculate this numerically?"
1515                   << std::endl;
1516         return numeric(0);
1517 }
1518 const numeric tgamma(const numeric & x)
1519 {
1520         std::clog << "tgamma(" << x
1521                   << "): Does anybody know good way to calculate this numerically?"
1522                   << std::endl;
1523         return numeric(0);
1524 }
1525
1526
1527 /** The psi function (aka polygamma function).
1528  *  This is only a stub! */
1529 const numeric psi(const numeric & x)
1530 {
1531         std::clog << "psi(" << x
1532                   << "): Does anybody know good way to calculate this numerically?"
1533                   << std::endl;
1534         return numeric(0);
1535 }
1536
1537
1538 /** The psi functions (aka polygamma functions).
1539  *  This is only a stub! */
1540 const numeric psi(const numeric & n, const numeric & x)
1541 {
1542         std::clog << "psi(" << n << "," << x
1543                   << "): Does anybody know good way to calculate this numerically?"
1544                   << std::endl;
1545         return numeric(0);
1546 }
1547
1548
1549 /** Factorial combinatorial function.
1550  *
1551  *  @param n  integer argument >= 0
1552  *  @exception range_error (argument must be integer >= 0) */
1553 const numeric factorial(const numeric & n)
1554 {
1555         if (!n.is_nonneg_integer())
1556                 throw std::range_error("numeric::factorial(): argument must be integer >= 0");
1557         return numeric(cln::factorial(n.to_int()));
1558 }
1559
1560
1561 /** The double factorial combinatorial function.  (Scarcely used, but still
1562  *  useful in cases, like for exact results of tgamma(n+1/2) for instance.)
1563  *
1564  *  @param n  integer argument >= -1
1565  *  @return n!! == n * (n-2) * (n-4) * ... * ({1|2}) with 0!! == (-1)!! == 1
1566  *  @exception range_error (argument must be integer >= -1) */
1567 const numeric doublefactorial(const numeric & n)
1568 {
1569         if (n == numeric(-1))
1570                 return _num1();
1571         
1572         if (!n.is_nonneg_integer())
1573                 throw std::range_error("numeric::doublefactorial(): argument must be integer >= -1");
1574         
1575         return numeric(cln::doublefactorial(n.to_int()));
1576 }
1577
1578
1579 /** The Binomial coefficients.  It computes the binomial coefficients.  For
1580  *  integer n and k and positive n this is the number of ways of choosing k
1581  *  objects from n distinct objects.  If n is negative, the formula
1582  *  binomial(n,k) == (-1)^k*binomial(k-n-1,k) is used to compute the result. */
1583 const numeric binomial(const numeric & n, const numeric & k)
1584 {
1585         if (n.is_integer() && k.is_integer()) {
1586                 if (n.is_nonneg_integer()) {
1587                         if (k.compare(n)!=1 && k.compare(_num0())!=-1)
1588                                 return numeric(cln::binomial(n.to_int(),k.to_int()));
1589                         else
1590                                 return _num0();
1591                 } else {
1592                         return _num_1().power(k)*binomial(k-n-_num1(),k);
1593                 }
1594         }
1595         
1596         // should really be gamma(n+1)/gamma(r+1)/gamma(n-r+1) or a suitable limit
1597         throw std::range_error("numeric::binomial(): don´t know how to evaluate that.");
1598 }
1599
1600
1601 /** Bernoulli number.  The nth Bernoulli number is the coefficient of x^n/n!
1602  *  in the expansion of the function x/(e^x-1).
1603  *
1604  *  @return the nth Bernoulli number (a rational number).
1605  *  @exception range_error (argument must be integer >= 0) */
1606 const numeric bernoulli(const numeric & nn)
1607 {
1608         if (!nn.is_integer() || nn.is_negative())
1609                 throw std::range_error("numeric::bernoulli(): argument must be integer >= 0");
1610         
1611         // Method:
1612         //
1613         // The Bernoulli numbers are rational numbers that may be computed using
1614         // the relation
1615         //
1616         //     B_n = - 1/(n+1) * sum_{k=0}^{n-1}(binomial(n+1,k)*B_k)
1617         //
1618         // with B(0) = 1.  Since the n'th Bernoulli number depends on all the
1619         // previous ones, the computation is necessarily very expensive.  There are
1620         // several other ways of computing them, a particularly good one being
1621         // cl_I s = 1;
1622         // cl_I c = n+1;
1623         // cl_RA Bern = 0;
1624         // for (unsigned i=0; i<n; i++) {
1625         //     c = exquo(c*(i-n),(i+2));
1626         //     Bern = Bern + c*s/(i+2);
1627         //     s = s + expt_pos(cl_I(i+2),n);
1628         // }
1629         // return Bern;
1630         // 
1631         // But if somebody works with the n'th Bernoulli number she is likely to
1632         // also need all previous Bernoulli numbers. So we need a complete remember
1633         // table and above divide and conquer algorithm is not suited to build one
1634         // up.  The code below is adapted from Pari's function bernvec().
1635         // 
1636         // (There is an interesting relation with the tangent polynomials described
1637         // in `Concrete Mathematics', which leads to a program twice as fast as our
1638         // implementation below, but it requires storing one such polynomial in
1639         // addition to the remember table.  This doubles the memory footprint so
1640         // we don't use it.)
1641         
1642         // the special cases not covered by the algorithm below
1643         if (nn.is_equal(_num1()))
1644                 return _num_1_2();
1645         if (nn.is_odd())
1646                 return _num0();
1647         
1648         // store nonvanishing Bernoulli numbers here
1649         static std::vector< cln::cl_RA > results;
1650         static int highest_result = 0;
1651         // algorithm not applicable to B(0), so just store it
1652         if (results.size()==0)
1653                 results.push_back(cln::cl_RA(1));
1654         
1655         int n = nn.to_long();
1656         for (int i=highest_result; i<n/2; ++i) {
1657                 cln::cl_RA B = 0;
1658                 long n = 8;
1659                 long m = 5;
1660                 long d1 = i;
1661                 long d2 = 2*i-1;
1662                 for (int j=i; j>0; --j) {
1663                         B = cln::cl_I(n*m) * (B+results[j]) / (d1*d2);
1664                         n += 4;
1665                         m += 2;
1666                         d1 -= 1;
1667                         d2 -= 2;
1668                 }
1669                 B = (1 - ((B+1)/(2*i+3))) / (cln::cl_I(1)<<(2*i+2));
1670                 results.push_back(B);
1671                 ++highest_result;
1672         }
1673         return results[n/2];
1674 }
1675
1676
1677 /** Fibonacci number.  The nth Fibonacci number F(n) is defined by the
1678  *  recurrence formula F(n)==F(n-1)+F(n-2) with F(0)==0 and F(1)==1.
1679  *
1680  *  @param n an integer
1681  *  @return the nth Fibonacci number F(n) (an integer number)
1682  *  @exception range_error (argument must be an integer) */
1683 const numeric fibonacci(const numeric & n)
1684 {
1685         if (!n.is_integer())
1686                 throw std::range_error("numeric::fibonacci(): argument must be integer");
1687         // Method:
1688         //
1689         // The following addition formula holds:
1690         //
1691         //      F(n+m)   = F(m-1)*F(n) + F(m)*F(n+1)  for m >= 1, n >= 0.
1692         //
1693         // (Proof: For fixed m, the LHS and the RHS satisfy the same recurrence
1694         // w.r.t. n, and the initial values (n=0, n=1) agree. Hence all values
1695         // agree.)
1696         // Replace m by m+1:
1697         //      F(n+m+1) = F(m)*F(n) + F(m+1)*F(n+1)      for m >= 0, n >= 0
1698         // Now put in m = n, to get
1699         //      F(2n) = (F(n+1)-F(n))*F(n) + F(n)*F(n+1) = F(n)*(2*F(n+1) - F(n))
1700         //      F(2n+1) = F(n)^2 + F(n+1)^2
1701         // hence
1702         //      F(2n+2) = F(n+1)*(2*F(n) + F(n+1))
1703         if (n.is_zero())
1704                 return _num0();
1705         if (n.is_negative())
1706                 if (n.is_even())
1707                         return -fibonacci(-n);
1708                 else
1709                         return fibonacci(-n);
1710         
1711         cln::cl_I u(0);
1712         cln::cl_I v(1);
1713         cln::cl_I m = cln::the<cln::cl_I>(n.to_cl_N()) >> 1L;  // floor(n/2);
1714         for (uintL bit=cln::integer_length(m); bit>0; --bit) {
1715                 // Since a squaring is cheaper than a multiplication, better use
1716                 // three squarings instead of one multiplication and two squarings.
1717                 cln::cl_I u2 = cln::square(u);
1718                 cln::cl_I v2 = cln::square(v);
1719                 if (cln::logbitp(bit-1, m)) {
1720                         v = cln::square(u + v) - u2;
1721                         u = u2 + v2;
1722                 } else {
1723                         u = v2 - cln::square(v - u);
1724                         v = u2 + v2;
1725                 }
1726         }
1727         if (n.is_even())
1728                 // Here we don't use the squaring formula because one multiplication
1729                 // is cheaper than two squarings.
1730                 return u * ((v << 1) - u);
1731         else
1732                 return cln::square(u) + cln::square(v);    
1733 }
1734
1735
1736 /** Absolute value. */
1737 const numeric abs(const numeric& x)
1738 {
1739         return cln::abs(x.to_cl_N());
1740 }
1741
1742
1743 /** Modulus (in positive representation).
1744  *  In general, mod(a,b) has the sign of b or is zero, and rem(a,b) has the
1745  *  sign of a or is zero. This is different from Maple's modp, where the sign
1746  *  of b is ignored. It is in agreement with Mathematica's Mod.
1747  *
1748  *  @return a mod b in the range [0,abs(b)-1] with sign of b if both are
1749  *  integer, 0 otherwise. */
1750 const numeric mod(const numeric & a, const numeric & b)
1751 {
1752         if (a.is_integer() && b.is_integer())
1753                 return cln::mod(cln::the<cln::cl_I>(a.to_cl_N()),
1754                                 cln::the<cln::cl_I>(b.to_cl_N()));
1755         else
1756                 return _num0();
1757 }
1758
1759
1760 /** Modulus (in symmetric representation).
1761  *  Equivalent to Maple's mods.
1762  *
1763  *  @return a mod b in the range [-iquo(abs(m)-1,2), iquo(abs(m),2)]. */
1764 const numeric smod(const numeric & a, const numeric & b)
1765 {
1766         if (a.is_integer() && b.is_integer()) {
1767                 const cln::cl_I b2 = cln::ceiling1(cln::the<cln::cl_I>(b.to_cl_N()) >> 1) - 1;
1768                 return cln::mod(cln::the<cln::cl_I>(a.to_cl_N()) + b2,
1769                                 cln::the<cln::cl_I>(b.to_cl_N())) - b2;
1770         } else
1771                 return _num0();
1772 }
1773
1774
1775 /** Numeric integer remainder.
1776  *  Equivalent to Maple's irem(a,b) as far as sign conventions are concerned.
1777  *  In general, mod(a,b) has the sign of b or is zero, and irem(a,b) has the
1778  *  sign of a or is zero.
1779  *
1780  *  @return remainder of a/b if both are integer, 0 otherwise. */
1781 const numeric irem(const numeric & a, const numeric & b)
1782 {
1783         if (a.is_integer() && b.is_integer())
1784                 return cln::rem(cln::the<cln::cl_I>(a.to_cl_N()),
1785                                 cln::the<cln::cl_I>(b.to_cl_N()));
1786         else
1787                 return _num0();
1788 }
1789
1790
1791 /** Numeric integer remainder.
1792  *  Equivalent to Maple's irem(a,b,'q') it obeyes the relation
1793  *  irem(a,b,q) == a - q*b.  In general, mod(a,b) has the sign of b or is zero,
1794  *  and irem(a,b) has the sign of a or is zero.  
1795  *
1796  *  @return remainder of a/b and quotient stored in q if both are integer,
1797  *  0 otherwise. */
1798 const numeric irem(const numeric & a, const numeric & b, numeric & q)
1799 {
1800         if (a.is_integer() && b.is_integer()) {
1801                 const cln::cl_I_div_t rem_quo = cln::truncate2(cln::the<cln::cl_I>(a.to_cl_N()),
1802                                                                cln::the<cln::cl_I>(b.to_cl_N()));
1803                 q = rem_quo.quotient;
1804                 return rem_quo.remainder;
1805         } else {
1806                 q = _num0();
1807                 return _num0();
1808         }
1809 }
1810
1811
1812 /** Numeric integer quotient.
1813  *  Equivalent to Maple's iquo as far as sign conventions are concerned.
1814  *  
1815  *  @return truncated quotient of a/b if both are integer, 0 otherwise. */
1816 const numeric iquo(const numeric & a, const numeric & b)
1817 {
1818         if (a.is_integer() && b.is_integer())
1819                 return truncate1(cln::the<cln::cl_I>(a.to_cl_N()),
1820                              cln::the<cln::cl_I>(b.to_cl_N()));
1821         else
1822                 return _num0();
1823 }
1824
1825
1826 /** Numeric integer quotient.
1827  *  Equivalent to Maple's iquo(a,b,'r') it obeyes the relation
1828  *  r == a - iquo(a,b,r)*b.
1829  *
1830  *  @return truncated quotient of a/b and remainder stored in r if both are
1831  *  integer, 0 otherwise. */
1832 const numeric iquo(const numeric & a, const numeric & b, numeric & r)
1833 {
1834         if (a.is_integer() && b.is_integer()) {
1835                 const cln::cl_I_div_t rem_quo = cln::truncate2(cln::the<cln::cl_I>(a.to_cl_N()),
1836                                                                cln::the<cln::cl_I>(b.to_cl_N()));
1837                 r = rem_quo.remainder;
1838                 return rem_quo.quotient;
1839         } else {
1840                 r = _num0();
1841                 return _num0();
1842         }
1843 }
1844
1845
1846 /** Greatest Common Divisor.
1847  *   
1848  *  @return  The GCD of two numbers if both are integer, a numerical 1
1849  *  if they are not. */
1850 const numeric gcd(const numeric & a, const numeric & b)
1851 {
1852         if (a.is_integer() && b.is_integer())
1853                 return cln::gcd(cln::the<cln::cl_I>(a.to_cl_N()),
1854                                 cln::the<cln::cl_I>(b.to_cl_N()));
1855         else
1856                 return _num1();
1857 }
1858
1859
1860 /** Least Common Multiple.
1861  *   
1862  *  @return  The LCM of two numbers if both are integer, the product of those
1863  *  two numbers if they are not. */
1864 const numeric lcm(const numeric & a, const numeric & b)
1865 {
1866         if (a.is_integer() && b.is_integer())
1867                 return cln::lcm(cln::the<cln::cl_I>(a.to_cl_N()),
1868                                 cln::the<cln::cl_I>(b.to_cl_N()));
1869         else
1870                 return a.mul(b);
1871 }
1872
1873
1874 /** Numeric square root.
1875  *  If possible, sqrt(z) should respect squares of exact numbers, i.e. sqrt(4)
1876  *  should return integer 2.
1877  *
1878  *  @param z numeric argument
1879  *  @return square root of z. Branch cut along negative real axis, the negative
1880  *  real axis itself where imag(z)==0 and real(z)<0 belongs to the upper part
1881  *  where imag(z)>0. */
1882 const numeric sqrt(const numeric & z)
1883 {
1884         return cln::sqrt(z.to_cl_N());
1885 }
1886
1887
1888 /** Integer numeric square root. */
1889 const numeric isqrt(const numeric & x)
1890 {
1891         if (x.is_integer()) {
1892                 cln::cl_I root;
1893                 cln::isqrt(cln::the<cln::cl_I>(x.to_cl_N()), &root);
1894                 return root;
1895         } else
1896                 return _num0();
1897 }
1898
1899
1900 /** Floating point evaluation of Archimedes' constant Pi. */
1901 ex PiEvalf(void)
1902
1903         return numeric(cln::pi(cln::default_float_format));
1904 }
1905
1906
1907 /** Floating point evaluation of Euler's constant gamma. */
1908 ex EulerEvalf(void)
1909
1910         return numeric(cln::eulerconst(cln::default_float_format));
1911 }
1912
1913
1914 /** Floating point evaluation of Catalan's constant. */
1915 ex CatalanEvalf(void)
1916 {
1917         return numeric(cln::catalanconst(cln::default_float_format));
1918 }
1919
1920
1921 _numeric_digits::_numeric_digits()
1922   : digits(17)
1923 {
1924         // It initializes to 17 digits, because in CLN float_format(17) turns out
1925         // to be 61 (<64) while float_format(18)=65.  The reason is we want to
1926         // have a cl_LF instead of cl_SF, cl_FF or cl_DF.
1927         assert(!too_late);
1928         too_late = true;
1929         cln::default_float_format = cln::float_format(17);
1930 }
1931
1932
1933 /** Assign a native long to global Digits object. */
1934 _numeric_digits& _numeric_digits::operator=(long prec)
1935 {
1936         digits = prec;
1937         cln::default_float_format = cln::float_format(prec); 
1938         return *this;
1939 }
1940
1941
1942 /** Convert global Digits object to native type long. */
1943 _numeric_digits::operator long()
1944 {
1945         // BTW, this is approx. unsigned(cln::default_float_format*0.301)-1
1946         return (long)digits;
1947 }
1948
1949
1950 /** Append global Digits object to ostream. */
1951 void _numeric_digits::print(std::ostream & os) const
1952 {
1953         debugmsg("_numeric_digits print", LOGLEVEL_PRINT);
1954         os << digits;
1955 }
1956
1957
1958 std::ostream& operator<<(std::ostream& os, const _numeric_digits & e)
1959 {
1960         e.print(os);
1961         return os;
1962 }
1963
1964 //////////
1965 // static member variables
1966 //////////
1967
1968 // private
1969
1970 bool _numeric_digits::too_late = false;
1971
1972
1973 /** Accuracy in decimal digits.  Only object of this type!  Can be set using
1974  *  assignment from C++ unsigned ints and evaluated like any built-in type. */
1975 _numeric_digits Digits;
1976
1977 #ifndef NO_NAMESPACE_GINAC
1978 } // namespace GiNaC
1979 #endif // ndef NO_NAMESPACE_GINAC