]> www.ginac.de Git - ginac.git/blobdiff - ginac/numeric.cpp
- epsilon*epsilon contractions work
[ginac.git] / ginac / numeric.cpp
index 3bd24a6a8571f8588ed868e531040c6e9999bdfa..ba9b8210ed250a706ca90786158e0905bf82e382 100644 (file)
@@ -40,6 +40,7 @@
 
 #include "numeric.h"
 #include "ex.h"
+#include "print.h"
 #include "archive.h"
 #include "debugmsg.h"
 #include "utils.h"
@@ -72,8 +73,6 @@ GINAC_IMPLEMENT_REGISTERED_CLASS(numeric, basic)
 // operator and helpers
 //////////
 
-// public
-
 /** default ctor. Numerically it initializes to an integer zero. */
 numeric::numeric() : basic(TINFO_numeric)
 {
@@ -82,19 +81,13 @@ numeric::numeric() : basic(TINFO_numeric)
        setflag(status_flags::evaluated | status_flags::expanded);
 }
 
-// protected
-
-/** For use by copy ctor and assignment operator. */
 void numeric::copy(const numeric &other)
 {
        inherited::copy(other);
        value = other.value;
 }
 
-void numeric::destroy(bool call_parent)
-{
-       if (call_parent) inherited::destroy(call_parent);
-}
+DEFAULT_DESTROY(numeric)
 
 //////////
 // other ctors
@@ -233,7 +226,7 @@ numeric::numeric(const char *s) : basic(TINFO_numeric)
 #else
                        char buf[14];
                        std::ostrstream(buf,sizeof(buf)) << unsigned(Digits) << std::ends;
-                       term += "_" + string(buf);
+                       term += "_" + std::string(buf);
 #endif
                        // construct float using cln::cl_F(const char *) ctor.
                        if (imaginary)
@@ -266,7 +259,6 @@ numeric::numeric(const cln::cl_N &z) : basic(TINFO_numeric)
 // archiving
 //////////
 
-/** Construct object from archive_node. */
 numeric::numeric(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
 {
        debugmsg("numeric ctor from archive_node", LOGLEVEL_CONSTRUCT);
@@ -304,13 +296,6 @@ numeric::numeric(const archive_node &n, const lst &sym_lst) : inherited(n, sym_l
        setflag(status_flags::evaluated | status_flags::expanded);
 }
 
-/** Unarchive the object. */
-ex numeric::unarchive(const archive_node &n, const lst &sym_lst)
-{
-       return (new numeric(n, sym_lst))->setflag(status_flags::dynallocated);
-}
-
-/** Archive the object. */
 void numeric::archive(archive_node &n) const
 {
        inherited::archive(n);
@@ -348,8 +333,10 @@ void numeric::archive(archive_node &n) const
 #endif
 }
 
+DEFAULT_UNARCHIVE(numeric)
+
 //////////
-// functions overriding virtual functions from bases classes
+// functions overriding virtual functions from base classes
 //////////
 
 /** Helper function to print a real number in a nicer way than is CLN's
@@ -359,146 +346,144 @@ void numeric::archive(archive_node &n) const
  *  want to visibly distinguish from cl_LF.
  *
  *  @see numeric::print() */
-static void print_real_number(std::ostream &os, const cln::cl_R &num)
+static void print_real_number(const print_context & c, const cln::cl_R &x)
 {
        cln::cl_print_flags ourflags;
-       if (cln::instanceof(num, cln::cl_RA_ring)) {
-               // case 1: integer or rational, nothing special to do:
-               cln::print_real(os, ourflags, num);
+       if (cln::instanceof(x, cln::cl_RA_ring)) {
+               // case 1: integer or rational
+               if (cln::instanceof(x, cln::cl_I_ring) ||
+                   !is_a<print_latex>(c)) {
+                       cln::print_real(c.s, ourflags, x);
+               } else {  // rational output in LaTeX context
+                       c.s << "\\frac{";
+                       cln::print_real(c.s, ourflags, cln::numerator(cln::the<cln::cl_RA>(x)));
+                       c.s << "}{";
+                       cln::print_real(c.s, ourflags, cln::denominator(cln::the<cln::cl_RA>(x)));
+                       c.s << '}';
+               }
        } else {
                // case 2: float
                // make CLN believe this number has default_float_format, so it prints
                // 'E' as exponent marker instead of 'L':
-               ourflags.default_float_format = cln::float_format(cln::the<cln::cl_F>(num));
-               cln::print_real(os, ourflags, num);
+               ourflags.default_float_format = cln::float_format(cln::the<cln::cl_F>(x));
+               cln::print_real(c.s, ourflags, x);
        }
-       return;
 }
 
 /** This method adds to the output so it blends more consistently together
  *  with the other routines and produces something compatible to ginsh input.
  *  
  *  @see print_real_number() */
-void numeric::print(std::ostream &os, unsigned upper_precedence) const
+void numeric::print(const print_context & c, unsigned level) const
 {
        debugmsg("numeric print", LOGLEVEL_PRINT);
-       cln::cl_R r = cln::realpart(cln::the<cln::cl_N>(value));
-       cln::cl_R i = cln::imagpart(cln::the<cln::cl_N>(value));
-       if (cln::zerop(i)) {
-               // case 1, real:  x  or  -x
-               if ((precedence<=upper_precedence) && (!this->is_nonneg_integer())) {
-                       os << "(";
-                       print_real_number(os, r);
-                       os << ")";
+
+       if (is_a<print_tree>(c)) {
+
+               c.s << std::string(level, ' ') << cln::the<cln::cl_N>(value)
+                   << " (" << class_name() << ")"
+                   << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
+                   << std::endl;
+
+       } else if (is_a<print_csrc>(c)) {
+
+               std::ios::fmtflags oldflags = c.s.flags();
+               c.s.setf(std::ios::scientific);
+               if (this->is_rational() && !this->is_integer()) {
+                       if (compare(_num0()) > 0) {
+                               c.s << "(";
+                               if (is_a<print_csrc_cl_N>(c))
+                                       c.s << "cln::cl_F(\"" << numer().evalf() << "\")";
+                               else
+                                       c.s << numer().to_double();
+                       } else {
+                               c.s << "-(";
+                               if (is_a<print_csrc_cl_N>(c))
+                                       c.s << "cln::cl_F(\"" << -numer().evalf() << "\")";
+                               else
+                                       c.s << -numer().to_double();
+                       }
+                       c.s << "/";
+                       if (is_a<print_csrc_cl_N>(c))
+                               c.s << "cln::cl_F(\"" << denom().evalf() << "\")";
+                       else
+                               c.s << denom().to_double();
+                       c.s << ")";
                } else {
-                       print_real_number(os, r);
+                       if (is_a<print_csrc_cl_N>(c))
+                               c.s << "cln::cl_F(\"" << evalf() << "\")";
+                       else
+                               c.s << to_double();
                }
+               c.s.flags(oldflags);
+
        } else {
-               if (cln::zerop(r)) {
-                       // case 2, imaginary:  y*I  or  -y*I
-                       if ((precedence<=upper_precedence) && (i < 0)) {
-                               if (i == -1) {
-                                       os << "(-I)";
-                               } else {
-                                       os << "(";
-                                       print_real_number(os, i);
-                                       os << "*I)";
-                               }
+               const std::string par_open  = is_a<print_latex>(c) ? "{(" : "(";
+               const std::string par_close = is_a<print_latex>(c) ? ")}" : ")";
+               const std::string imag_sym  = is_a<print_latex>(c) ? "i" : "I";
+               const std::string mul_sym   = is_a<print_latex>(c) ? " " : "*";
+               const cln::cl_R r = cln::realpart(cln::the<cln::cl_N>(value));
+               const cln::cl_R i = cln::imagpart(cln::the<cln::cl_N>(value));
+               if (cln::zerop(i)) {
+                       // case 1, real:  x  or  -x
+                       if ((precedence() <= level) && (!this->is_nonneg_integer())) {
+                               c.s << par_open;
+                               print_real_number(c, r);
+                               c.s << par_close;
                        } else {
-                               if (i == 1) {
-                                       os << "I";
-                               } else {
+                               print_real_number(c, r);
+                       }
+               } else {
+                       if (cln::zerop(r)) {
+                               // case 2, imaginary:  y*I  or  -y*I
+                               if ((precedence() <= level) && (i < 0)) {
                                        if (i == -1) {
-                                               os << "-I";
+                                               c.s << par_open+imag_sym+par_close;
                                        } else {
-                                               print_real_number(os, i);
-                                               os << "*I";
+                                               c.s << par_open;
+                                               print_real_number(c, i);
+                                               c.s << mul_sym+imag_sym+par_close;
                                        }
-                               }
-                       }
-               } else {
-                       // case 3, complex:  x+y*I  or  x-y*I  or  -x+y*I  or  -x-y*I
-                       if (precedence <= upper_precedence)
-                               os << "(";
-                       print_real_number(os, r);
-                       if (i < 0) {
-                               if (i == -1) {
-                                       os << "-I";
                                } else {
-                                       print_real_number(os, i);
-                                       os << "*I";
+                                       if (i == 1) {
+                                               c.s << imag_sym;
+                                       } else {
+                                               if (i == -1) {
+                                                       c.s << "-" << imag_sym;
+                                               } else {
+                                                       print_real_number(c, i);
+                                                       c.s << mul_sym+imag_sym;
+                                               }
+                                       }
                                }
                        } else {
-                               if (i == 1) {
-                                       os << "+I";
+                               // case 3, complex:  x+y*I  or  x-y*I  or  -x+y*I  or  -x-y*I
+                               if (precedence() <= level)
+                                       c.s << par_open;
+                               print_real_number(c, r);
+                               if (i < 0) {
+                                       if (i == -1) {
+                                               c.s << "-"+imag_sym;
+                                       } else {
+                                               print_real_number(c, i);
+                                               c.s << mul_sym+imag_sym;
+                                       }
                                } else {
-                                       os << "+";
-                                       print_real_number(os, i);
-                                       os << "*I";
+                                       if (i == 1) {
+                                               c.s << "+"+imag_sym;
+                                       } else {
+                                               c.s << "+";
+                                               print_real_number(c, i);
+                                               c.s << mul_sym+imag_sym;
+                                       }
                                }
+                               if (precedence() <= level)
+                                       c.s << par_close;
                        }
-                       if (precedence <= upper_precedence)
-                               os << ")";
                }
        }
 }
 
-
-void numeric::printraw(std::ostream &os) const
-{
-       // The method printraw doesn't do much, it simply uses CLN's operator<<()
-       // for output, which is ugly but reliable. e.g: 2+2i
-       debugmsg("numeric printraw", LOGLEVEL_PRINT);
-       os << class_name() << "(" << cln::the<cln::cl_N>(value) << ")";
-}
-
-
-void numeric::printtree(std::ostream &os, unsigned indent) const
-{
-       debugmsg("numeric printtree", LOGLEVEL_PRINT);
-       os << std::string(indent,' ') << cln::the<cln::cl_N>(value)
-          << " (numeric): "
-          << "hash=" << hashvalue
-          << " (0x" << std::hex << hashvalue << std::dec << ")"
-          << ", flags=" << flags << std::endl;
-}
-
-
-void numeric::printcsrc(std::ostream &os, unsigned type, unsigned upper_precedence) const
-{
-       debugmsg("numeric print csrc", LOGLEVEL_PRINT);
-       std::ios::fmtflags oldflags = os.flags();
-       os.setf(std::ios::scientific);
-       if (this->is_rational() && !this->is_integer()) {
-               if (compare(_num0()) > 0) {
-                       os << "(";
-                       if (type == csrc_types::ctype_cl_N)
-                               os << "cln::cl_F(\"" << numer().evalf() << "\")";
-                       else
-                               os << numer().to_double();
-               } else {
-                       os << "-(";
-                       if (type == csrc_types::ctype_cl_N)
-                               os << "cln::cl_F(\"" << -numer().evalf() << "\")";
-                       else
-                               os << -numer().to_double();
-               }
-               os << "/";
-               if (type == csrc_types::ctype_cl_N)
-                       os << "cln::cl_F(\"" << denom().evalf() << "\")";
-               else
-                       os << denom().to_double();
-               os << ")";
-       } else {
-               if (type == csrc_types::ctype_cl_N)
-                       os << "cln::cl_F(\"" << evalf() << "\")";
-               else
-                       os << to_double();
-       }
-       os.flags(oldflags);
-}
-
-
 bool numeric::info(unsigned inf) const
 {
        switch (inf) {
@@ -554,7 +539,7 @@ bool numeric::has(const ex &other) const
 {
        if (!is_exactly_of_type(*other.bp, numeric))
                return false;
-       const numeric &o = static_cast<numeric &>(const_cast<basic &>(*other.bp));
+       const numeric &o = static_cast<const numeric &>(*other.bp);
        if (this->is_equal(o) || this->is_equal(-o))
                return true;
        if (o.imag().is_zero())  // e.g. scan for 3 in -3*I
@@ -599,7 +584,7 @@ ex numeric::evalf(int level) const
 int numeric::compare_same_type(const basic &other) const
 {
        GINAC_ASSERT(is_exactly_of_type(other, numeric));
-       const numeric &o = static_cast<numeric &>(const_cast<basic &>(other));
+       const numeric &o = static_cast<const numeric &>(other);
        
        return this->compare(o);
 }
@@ -608,9 +593,9 @@ int numeric::compare_same_type(const basic &other) const
 bool numeric::is_equal_same_type(const basic &other) const
 {
        GINAC_ASSERT(is_exactly_of_type(other,numeric));
-       const numeric *o = static_cast<const numeric *>(&other);
+       const numeric &o = static_cast<const numeric &>(other);
        
-       return this->is_equal(*o);
+       return this->is_equal(o);
 }
 
 
@@ -961,13 +946,13 @@ bool numeric::is_real(void) const
 
 bool numeric::operator==(const numeric &other) const
 {
-       return equal(cln::the<cln::cl_N>(value), cln::the<cln::cl_N>(other.value));
+       return cln::equal(cln::the<cln::cl_N>(value), cln::the<cln::cl_N>(other.value));
 }
 
 
 bool numeric::operator!=(const numeric &other) const
 {
-       return !equal(cln::the<cln::cl_N>(value), cln::the<cln::cl_N>(other.value));
+       return !cln::equal(cln::the<cln::cl_N>(value), cln::the<cln::cl_N>(other.value));
 }
 
 
@@ -1137,7 +1122,7 @@ const numeric numeric::denom(void) const
        if (this->is_integer())
                return _num1();
        
-       if (instanceof(value, cln::cl_RA_ring))
+       if (cln::instanceof(value, cln::cl_RA_ring))
                return numeric(cln::denominator(cln::the<cln::cl_RA>(value)));
        
        if (!this->is_real()) {  // complex case, handle Q(i):
@@ -1171,15 +1156,6 @@ int numeric::int_length(void) const
                return 0;
 }
 
-
-//////////
-// static member variables
-//////////
-
-// protected
-
-unsigned numeric::precedence = 30;
-
 //////////
 // global constants
 //////////
@@ -1606,7 +1582,7 @@ const numeric bernoulli(const numeric &nn)
        static std::vector< cln::cl_RA > results;
        static int highest_result = 0;
        // algorithm not applicable to B(0), so just store it
-       if (results.size()==0)
+       if (results.empty())
                results.push_back(cln::cl_RA(1));
        
        int n = nn.to_long();
@@ -1773,8 +1749,8 @@ const numeric irem(const numeric &a, const numeric &b, numeric &q)
 const numeric iquo(const numeric &a, const numeric &b)
 {
        if (a.is_integer() && b.is_integer())
-               return truncate1(cln::the<cln::cl_I>(a.to_cl_N()),
-                            cln::the<cln::cl_I>(b.to_cl_N()));
+               return cln::truncate1(cln::the<cln::cl_I>(a.to_cl_N()),
+                                 cln::the<cln::cl_I>(b.to_cl_N()));
        else
                return _num0();
 }