]> www.ginac.de Git - ginac.git/blobdiff - ginac/numeric.cpp
- version bump.
[ginac.git] / ginac / numeric.cpp
index 880e0a7ebbde60bcbf0ec559527cc69769f7dd02..9d5f7f012aef95b8375fa7350f2356010a6174e7 100644 (file)
@@ -346,18 +346,27 @@ DEFAULT_UNARCHIVE(numeric)
  *  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);
        }
 }
 
@@ -419,10 +428,10 @@ void numeric::print(const print_context & c, unsigned level) const
                        // case 1, real:  x  or  -x
                        if ((precedence() <= level) && (!this->is_nonneg_integer())) {
                                c.s << par_open;
-                               print_real_number(c.s, r);
+                               print_real_number(c, r);
                                c.s << par_close;
                        } else {
-                               print_real_number(c.s, r);
+                               print_real_number(c, r);
                        }
                } else {
                        if (cln::zerop(r)) {
@@ -432,7 +441,7 @@ void numeric::print(const print_context & c, unsigned level) const
                                                c.s << par_open+imag_sym+par_close;
                                        } else {
                                                c.s << par_open;
-                                               print_real_number(c.s, i);
+                                               print_real_number(c, i);
                                                c.s << mul_sym+imag_sym+par_close;
                                        }
                                } else {
@@ -442,7 +451,7 @@ void numeric::print(const print_context & c, unsigned level) const
                                                if (i == -1) {
                                                        c.s << "-" << imag_sym;
                                                } else {
-                                                       print_real_number(c.s, i);
+                                                       print_real_number(c, i);
                                                        c.s << mul_sym+imag_sym;
                                                }
                                        }
@@ -451,12 +460,12 @@ void numeric::print(const print_context & c, unsigned level) const
                                // 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);
+                               print_real_number(c, r);
                                if (i < 0) {
                                        if (i == -1) {
                                                c.s << "-"+imag_sym;
                                        } else {
-                                               print_real_number(c.s, i);
+                                               print_real_number(c, i);
                                                c.s << mul_sym+imag_sym;
                                        }
                                } else {
@@ -464,7 +473,7 @@ void numeric::print(const print_context & c, unsigned level) const
                                                c.s << "+"+imag_sym;
                                        } else {
                                                c.s << "+";
-                                               print_real_number(c.s, i);
+                                               print_real_number(c, i);
                                                c.s << mul_sym+imag_sym;
                                        }
                                }
@@ -528,9 +537,9 @@ bool numeric::info(unsigned inf) const
  *  sign as a multiplicative factor. */
 bool numeric::has(const ex &other) const
 {
-       if (!is_exactly_of_type(*other.bp, numeric))
+       if (!is_ex_exactly_of_type(other, numeric))
                return false;
-       const numeric &o = static_cast<const numeric &>(*other.bp);
+       const numeric &o = ex_to<numeric>(other);
        if (this->is_equal(o) || this->is_equal(-o))
                return true;
        if (o.imag().is_zero())  // e.g. scan for 3 in -3*I
@@ -574,7 +583,7 @@ ex numeric::evalf(int level) const
 
 int numeric::compare_same_type(const basic &other) const
 {
-       GINAC_ASSERT(is_exactly_of_type(other, numeric));
+       GINAC_ASSERT(is_exactly_a<numeric>(other));
        const numeric &o = static_cast<const numeric &>(other);
        
        return this->compare(o);
@@ -583,7 +592,7 @@ 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));
+       GINAC_ASSERT(is_exactly_a<numeric>(other));
        const numeric &o = static_cast<const numeric &>(other);
        
        return this->is_equal(o);
@@ -1423,10 +1432,7 @@ const numeric zeta(const numeric &x)
                if (cln::zerop(x.to_cl_N()-aux))
                        return cln::zeta(aux);
        }
-       std::clog << "zeta(" << x
-                         << "): Does anybody know a good way to calculate this numerically?"
-                         << std::endl;
-       return numeric(0);
+       throw dunno();
 }
 
 
@@ -1434,17 +1440,11 @@ const numeric zeta(const numeric &x)
  *  This is only a stub! */
 const numeric lgamma(const numeric &x)
 {
-       std::clog << "lgamma(" << x
-                 << "): Does anybody know a good way to calculate this numerically?"
-                 << std::endl;
-       return numeric(0);
+       throw dunno();
 }
 const numeric tgamma(const numeric &x)
 {
-       std::clog << "tgamma(" << x
-                 << "): Does anybody know a good way to calculate this numerically?"
-                 << std::endl;
-       return numeric(0);
+       throw dunno();
 }
 
 
@@ -1452,10 +1452,7 @@ const numeric tgamma(const numeric &x)
  *  This is only a stub! */
 const numeric psi(const numeric &x)
 {
-       std::clog << "psi(" << x
-                 << "): Does anybody know a good way to calculate this numerically?"
-                 << std::endl;
-       return numeric(0);
+       throw dunno();
 }
 
 
@@ -1463,10 +1460,7 @@ const numeric psi(const numeric &x)
  *  This is only a stub! */
 const numeric psi(const numeric &n, const numeric &x)
 {
-       std::clog << "psi(" << n << "," << x
-                 << "): Does anybody know a good way to calculate this numerically?"
-                 << std::endl;
-       return numeric(0);
+       throw dunno();
 }