]> www.ginac.de Git - ginac.git/commitdiff
Synced to HEAD:
authorJens Vollinga <vollinga@thep.physik.uni-mainz.de>
Fri, 7 Sep 2007 18:57:09 +0000 (18:57 +0000)
committerJens Vollinga <vollinga@thep.physik.uni-mainz.de>
Fri, 7 Sep 2007 18:57:09 +0000 (18:57 +0000)
Improved CLN output [Sheplyakov].

ginac/add.cpp
ginac/numeric.cpp
ginac/power.cpp

index 067f9bf73b578144295f3566a535c6f01fe10f5e..4fd10d9b4ee56d8430afe75f1f3d4369383102c8 100644 (file)
@@ -23,6 +23,7 @@
 #include <iostream>
 #include <stdexcept>
 #include <limits>
+#include <string>
 
 #include "add.h"
 #include "mul.h"
@@ -170,37 +171,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 208a89c0d935462d0dce3a66fef0422cfd9a7b87..9e258857bc4d54d69a6a2c7f01285400e5e0b49c 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 4ebf0aca665e4592a4f1301f1ea6cec97fff5118..f243147aa46932836e2077334642068b7171088c 100644 (file)
@@ -164,6 +164,21 @@ static void print_sym_pow(const print_context & c, const symbol &x, int exp)
 
 void power::do_print_csrc(const print_csrc & c, unsigned level) const
 {
+       if (is_a<print_csrc_cl_N>(c)) {
+               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 << ')';
+               return;
+       }
+
        // Integer powers of symbols are printed in a special, optimized way
        if (exponent.info(info_flags::integer)
         && (is_a<symbol>(basis) || is_a<constant>(basis))) {
@@ -172,29 +187,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);