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