]> www.ginac.de Git - ginac.git/commitdiff
Improved CLN output [Sheplyakov].
authorJens Vollinga <vollinga@thep.physik.uni-mainz.de>
Fri, 7 Sep 2007 18:48:22 +0000 (18:48 +0000)
committerJens Vollinga <vollinga@thep.physik.uni-mainz.de>
Fri, 7 Sep 2007 18:48:22 +0000 (18:48 +0000)
ginac/add.cpp
ginac/numeric.cpp
ginac/power.cpp
ginac/power.h

index b37ab79def6926ca36c3dc804f068580c10b7b09..98e8c798e206763a5cb8965d75b12db543bd8493 100644 (file)
@@ -23,6 +23,7 @@
 #include <iostream>
 #include <stdexcept>
 #include <limits>
+#include <string>
 
 #include "add.h"
 #include "mul.h"
@@ -172,37 +173,32 @@ void add::do_print_csrc(const print_csrc & c, unsigned level) const
        if (precedence() <= level)
                c.s << "(";
        
-       // Print arguments, separated by "+"
+       // Print arguments, separated by "+" or "-"
        epvector::const_iterator it = seq.begin(), itend = seq.end();
+       char separator = ' ';
        while (it != itend) {
                
-               // If the coefficient is -1, it is replaced by a single minus sign
-               if (it->coeff.is_equal(_ex1)) {
+               // If the coefficient is negative, separator is "-"
+               if (it->coeff.is_equal(_ex_1) || 
+                       ex_to<numeric>(it->coeff).numer().is_equal(*_num_1_p))
+                       separator = '-';
+               c.s << separator;
+               if (it->coeff.is_equal(_ex1) || it->coeff.is_equal(_ex_1)) {
                        it->rest.print(c, precedence());
-               } else if (it->coeff.is_equal(_ex_1)) {
-                       c.s << "-";
+               } else if (ex_to<numeric>(it->coeff).numer().is_equal(*_num1_p) ||
+                                ex_to<numeric>(it->coeff).numer().is_equal(*_num_1_p))
+               {
                        it->rest.print(c, precedence());
-               } else if (ex_to<numeric>(it->coeff).numer().is_equal(*_num1_p)) {
-                       it->rest.print(c, precedence());
-                       c.s << "/";
-                       ex_to<numeric>(it->coeff).denom().print(c, precedence());
-               } else if (ex_to<numeric>(it->coeff).numer().is_equal(*_num_1_p)) {
-                       c.s << "-";
-                       it->rest.print(c, precedence());
-                       c.s << "/";
+                       c.s << '/';
                        ex_to<numeric>(it->coeff).denom().print(c, precedence());
                } else {
                        it->coeff.print(c, precedence());
-                       c.s << "*";
+                       c.s << '*';
                        it->rest.print(c, precedence());
                }
                
-               // Separator is "+", except if the following expression would have a leading minus sign or the sign is sitting in parenthesis (as in a ctor)
                ++it;
-               if (it != itend
-                && (is_a<print_csrc_cl_N>(c) || !it->coeff.info(info_flags::real)  // sign inside ctor arguments
-                 || !(it->coeff.info(info_flags::negative) || (it->coeff.is_equal(*_num1_p) && is_exactly_a<numeric>(it->rest) && it->rest.info(info_flags::negative)))))
-                       c.s << "+";
+               separator = '+';
        }
        
        if (!overall_coeff.is_zero()) {
index ef61ce75b55e3d86be334982e80d0b9c93fef157..3ca4b58c5ef31487c1e7a654a47885e67df7c4aa 100644 (file)
@@ -399,6 +399,40 @@ static void print_real_csrc(const print_context & c, const cln::cl_R & x)
        }
 }
 
+template<typename T1, typename T2> 
+static inline bool coerce(T1& dst, const T2& arg);
+
+/** 
+ * @brief Check if CLN integer can be converted into int
+ *
+ * @sa http://www.ginac.de/pipermail/cln-list/2006-October/000248.html
+ */
+template<>
+static inline bool coerce<int, cln::cl_I>(int& dst, const cln::cl_I& arg)
+{
+       static const cln::cl_I cl_max_int = 
+               (cln::cl_I)(long)(std::numeric_limits<int>::max());
+       static const cln::cl_I cl_min_int =
+               (cln::cl_I)(long)(std::numeric_limits<int>::min());
+       if ((arg >= cl_min_int) && (arg <= cl_max_int)) {
+               dst = cl_I_to_int(arg);
+               return true;
+       }
+       return false;
+}
+
+template<>
+static inline bool coerce<unsigned int, cln::cl_I>(unsigned int& dst, const cln::cl_I& arg)
+{
+       static const cln::cl_I cl_max_uint = 
+               (cln::cl_I)(unsigned long)(std::numeric_limits<unsigned int>::max());
+       if ((! minusp(arg)) && (arg <= cl_max_uint)) {
+               dst = cl_I_to_uint(arg);
+               return true;
+       }
+       return false;
+}
+
 /** Helper function to print real number in C++ source format using cl_N types.
  *
  *  @see numeric::print() */
@@ -406,11 +440,20 @@ static void print_real_cl_N(const print_context & c, const cln::cl_R & x)
 {
        if (cln::instanceof(x, cln::cl_I_ring)) {
 
-               // Integer number
-               c.s << "cln::cl_I(\"";
-               print_real_number(c, x);
-               c.s << "\")";
-
+    int dst;
+    // fixnum 
+    if (coerce(dst, cln::the<cln::cl_I>(x))) {
+      // can be converted to native int
+      if (dst < 0)
+        c.s << "(-" << dst << ")";
+      else
+        c.s << dst;
+    } else {
+      // bignum
+      c.s << "cln::cl_I(\"";
+      print_real_number(c, x);
+      c.s << "\")";
+    }
        } else if (cln::instanceof(x, cln::cl_RA_ring)) {
 
                // Rational number
index faa0d05cc61eb156e0554a9b787b6db4ff8a7e19..68bec71c949c4866fd2c949c2c9df0db78ec2112 100644 (file)
@@ -50,7 +50,8 @@ GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(power, basic,
   print_func<print_latex>(&power::do_print_latex).
   print_func<print_csrc>(&power::do_print_csrc).
   print_func<print_python>(&power::do_print_python).
-  print_func<print_python_repr>(&power::do_print_python_repr))
+  print_func<print_python_repr>(&power::do_print_python_repr).
+  print_func<print_csrc_cl_N>(&power::do_print_csrc_cl_N))
 
 typedef std::vector<int> intvector;
 
@@ -162,6 +163,21 @@ static void print_sym_pow(const print_context & c, const symbol &x, int exp)
        }
 }
 
+void power::do_print_csrc_cl_N(const print_csrc_cl_N& c, unsigned level) const
+{
+       if (exponent.is_equal(_ex_1)) {
+               c.s << "recip(";
+               basis.print(c);
+               c.s << ')';
+               return;
+       }
+       c.s << "expt(";
+       basis.print(c);
+       c.s << ", ";
+       exponent.print(c);
+       c.s << ')';
+}
+
 void power::do_print_csrc(const print_csrc & c, unsigned level) const
 {
        // Integer powers of symbols are printed in a special, optimized way
@@ -172,29 +188,20 @@ void power::do_print_csrc(const print_csrc & c, unsigned level) const
                        c.s << '(';
                else {
                        exp = -exp;
-                       if (is_a<print_csrc_cl_N>(c))
-                               c.s << "recip(";
-                       else
-                               c.s << "1.0/(";
+                       c.s << "1.0/(";
                }
                print_sym_pow(c, ex_to<symbol>(basis), exp);
                c.s << ')';
 
        // <expr>^-1 is printed as "1.0/<expr>" or with the recip() function of CLN
        } else if (exponent.is_equal(_ex_1)) {
-               if (is_a<print_csrc_cl_N>(c))
-                       c.s << "recip(";
-               else
-                       c.s << "1.0/(";
+               c.s << "1.0/(";
                basis.print(c);
                c.s << ')';
 
-       // Otherwise, use the pow() or expt() (CLN) functions
+       // Otherwise, use the pow() function
        } else {
-               if (is_a<print_csrc_cl_N>(c))
-                       c.s << "expt(";
-               else
-                       c.s << "pow(";
+               c.s << "pow(";
                basis.print(c);
                c.s << ',';
                exponent.print(c);
index f5e65d6a04acdda2cfbcce40de12c6beacc4c171..3375040f56c20a5bc2d77186691cf442e35ec5e1 100644 (file)
@@ -88,6 +88,7 @@ protected:
        void do_print_csrc(const print_csrc & c, unsigned level) const;
        void do_print_python(const print_python & c, unsigned level) const;
        void do_print_python_repr(const print_python_repr & c, unsigned level) const;
+       void do_print_csrc_cl_N(const print_csrc_cl_N & c, unsigned level) const;
 
        ex expand_add(const add & a, int n, unsigned options) const;
        ex expand_add_2(const add & a, unsigned options) const;