]> www.ginac.de Git - ginac.git/blobdiff - ginac/numeric.cpp
* color.cpp: remove duplicated default args.
[ginac.git] / ginac / numeric.cpp
index a4a40c8c013dbaf3765d2b9cacb75f435db19548..ef4e3e929ce5732f854bfae80d3f72f9f55e2b34 100644 (file)
@@ -40,6 +40,7 @@
 
 #include "numeric.h"
 #include "ex.h"
+#include "print.h"
 #include "archive.h"
 #include "debugmsg.h"
 #include "utils.h"
@@ -365,126 +366,116 @@ static void print_real_number(std::ostream &os, const cln::cl_R &num)
  *  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_of_type(c, print_tree)) {
+
+               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_of_type(c, print_csrc)) {
+
+               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_of_type(c, print_csrc_cl_N))
+                                       c.s << "cln::cl_F(\"" << numer().evalf() << "\")";
+                               else
+                                       c.s << numer().to_double();
+                       } else {
+                               c.s << "-(";
+                               if (is_of_type(c, print_csrc_cl_N))
+                                       c.s << "cln::cl_F(\"" << -numer().evalf() << "\")";
+                               else
+                                       c.s << -numer().to_double();
+                       }
+                       c.s << "/";
+                       if (is_of_type(c, print_csrc_cl_N))
+                               c.s << "cln::cl_F(\"" << denom().evalf() << "\")";
+                       else
+                               c.s << denom().to_double();
+                       c.s << ")";
                } else {
-                       print_real_number(os, r);
+                       if (is_of_type(c, print_csrc_cl_N))
+                               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_of_type(c, print_latex) ? "{(" : "(";
+               const std::string par_close = is_of_type(c, print_latex) ? ")}" : ")";
+               const std::string imag_sym  = is_of_type(c, print_latex) ? "i" : "I";
+               const std::string mul_sym   = is_of_type(c, print_latex) ? " " : "*";
+               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.s, r);
+                               c.s << par_close;
                        } else {
-                               if (i == 1) {
-                                       os << "I";
-                               } else {
+                               print_real_number(c.s, 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.s, 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.s, 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.s, r);
+                               if (i < 0) {
+                                       if (i == -1) {
+                                               c.s << "-"+imag_sym;
+                                       } else {
+                                               print_real_number(c.s, 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.s, 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) {
@@ -947,13 +938,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));
 }
 
 
@@ -1123,7 +1114,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):
@@ -1759,8 +1750,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();
 }