]> www.ginac.de Git - ginac.git/blobdiff - ginac/expairseq.cpp
* numeric::print(): increase precision for C source output.
[ginac.git] / ginac / expairseq.cpp
index c2dfc6a57cebd26363980b574739c89a7003d8f2..3c5a656bd2d52d686eec3abcadbe8ff679d8c83d 100644 (file)
@@ -3,7 +3,7 @@
  *  Implementation of sequences of expression pairs. */
 
 /*
- *  GiNaC Copyright (C) 1999-2001 Johannes Gutenberg University Mainz, Germany
+ *  GiNaC Copyright (C) 1999-2002 Johannes Gutenberg University Mainz, Germany
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
@@ -20,6 +20,7 @@
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
+#include <iostream>
 #include <algorithm>
 #include <string>
 #include <stdexcept>
@@ -32,7 +33,6 @@
 #include "wildcard.h"
 #include "print.h"
 #include "archive.h"
-#include "debugmsg.h"
 #include "utils.h"
 
 #if EXPAIRSEQ_USE_HASHTAB
@@ -58,20 +58,18 @@ public:
 };
 
 //////////
-// default ctor, dtor, copy ctor assignment operator and helpers
+// default ctor, dtor, copy ctor, assignment operator and helpers
 //////////
 
 // public
 
 expairseq::expairseq(const expairseq &other)
 {
-       debugmsg("expairseq copy ctor",LOGLEVEL_CONSTRUCT);
        copy(other);
 }
 
 const expairseq &expairseq::operator=(const expairseq &other)
 {
-       debugmsg("expairseq operator=",LOGLEVEL_ASSIGNMENT);
        if (this != &other) {
                destroy(true);
                copy(other);
@@ -115,14 +113,12 @@ DEFAULT_DESTROY(expairseq)
 
 expairseq::expairseq(const ex &lh, const ex &rh) : inherited(TINFO_expairseq)
 {
-       debugmsg("expairseq ctor from ex,ex",LOGLEVEL_CONSTRUCT);
        construct_from_2_ex(lh,rh);
        GINAC_ASSERT(is_canonical());
 }
 
 expairseq::expairseq(const exvector &v) : inherited(TINFO_expairseq)
 {
-       debugmsg("expairseq ctor from exvector",LOGLEVEL_CONSTRUCT);
        construct_from_exvector(v);
        GINAC_ASSERT(is_canonical());
 }
@@ -130,7 +126,7 @@ expairseq::expairseq(const exvector &v) : inherited(TINFO_expairseq)
 expairseq::expairseq(const epvector &v, const ex &oc)
   : inherited(TINFO_expairseq), overall_coeff(oc)
 {
-       debugmsg("expairseq ctor from epvector,ex",LOGLEVEL_CONSTRUCT);
+       GINAC_ASSERT(is_a<numeric>(oc));
        construct_from_epvector(v);
        GINAC_ASSERT(is_canonical());
 }
@@ -138,8 +134,8 @@ expairseq::expairseq(const epvector &v, const ex &oc)
 expairseq::expairseq(epvector *vp, const ex &oc)
   : inherited(TINFO_expairseq), overall_coeff(oc)
 {
-       debugmsg("expairseq ctor from epvector *,ex",LOGLEVEL_CONSTRUCT);
        GINAC_ASSERT(vp!=0);
+       GINAC_ASSERT(is_a<numeric>(oc));
        construct_from_epvector(*vp);
        delete vp;
        GINAC_ASSERT(is_canonical());
@@ -154,7 +150,6 @@ expairseq::expairseq(const archive_node &n, const lst &sym_lst) : inherited(n, s
        , hashtabsize(0)
 #endif
 {
-       debugmsg("expairseq ctor from archive_node", LOGLEVEL_CONSTRUCT);
        for (unsigned int i=0; true; i++) {
                ex rest;
                ex coeff;
@@ -188,14 +183,11 @@ DEFAULT_UNARCHIVE(expairseq)
 
 basic *expairseq::duplicate() const
 {
-       debugmsg("expairseq duplicate",LOGLEVEL_DUPLICATE);
        return new expairseq(*this);
 }
 
 void expairseq::print(const print_context &c, unsigned level) const
 {
-       debugmsg("expairseq print",LOGLEVEL_PRINT);
-
        if (is_of_type(c, print_tree)) {
 
                unsigned delta_indent = static_cast<const print_tree &>(c).delta_indent;
@@ -412,7 +404,7 @@ ex expairseq::subs(const lst &ls, const lst &lr, bool no_pattern) const
 {
        epvector *vp = subschildren(ls, lr, no_pattern);
        if (vp)
-               return ex_to<basic>(thisexpairseq(vp, overall_coeff)).basic::subs(ls, lr, no_pattern);
+               return ex_to<basic>(thisexpairseq(vp, overall_coeff));
        else
                return basic::subs(ls, lr, no_pattern);
 }
@@ -651,7 +643,7 @@ void expairseq::printseq(const print_context & c, char delim,
  *  @see expairseq::recombine_pair_to_ex() */
 expair expairseq::split_ex_to_pair(const ex &e) const
 {
-       return expair(e,_ex1());
+       return expair(e,_ex1);
 }
 
 
@@ -691,7 +683,7 @@ bool expairseq::expair_needs_further_processing(epp it)
 
 ex expairseq::default_overall_coeff(void) const
 {
-       return _ex0();
+       return _ex0;
 }
 
 void expairseq::combine_overall_coeff(const ex &c)
@@ -1067,7 +1059,7 @@ void expairseq::make_flat(const epvector &v)
 /** Brings this expairseq into a sorted (canonical) form. */
 void expairseq::canonicalize(void)
 {
-       sort(seq.begin(), seq.end(), expair_is_less());
+       std::sort(seq.begin(), seq.end(), expair_rest_is_less());
 }
 
 
@@ -1076,42 +1068,43 @@ void expairseq::canonicalize(void)
  *  instance. */
 void expairseq::combine_same_terms_sorted_seq(void)
 {
+       if (seq.size()<2)
+               return;
+
        bool needs_further_processing = false;
-       
-       if (seq.size()>1) {
-               epvector::iterator itin1 = seq.begin();
-               epvector::iterator itin2 = itin1+1;
-               epvector::iterator itout = itin1;
-               epvector::iterator last = seq.end();
-               // must_copy will be set to true the first time some combination is 
-               // possible from then on the sequence has changed and must be compacted
-               bool must_copy = false;
-               while (itin2!=last) {
-                       if (itin1->rest.compare(itin2->rest)==0) {
-                               itin1->coeff = ex_to<numeric>(itin1->coeff).
-                                              add_dyn(ex_to<numeric>(itin2->coeff));
-                               if (expair_needs_further_processing(itin1))
-                                       needs_further_processing = true;
-                               must_copy = true;
-                       } else {
-                               if (!ex_to<numeric>(itin1->coeff).is_zero()) {
-                                       if (must_copy)
-                                               *itout = *itin1;
-                                       ++itout;
-                               }
-                               itin1 = itin2;
+
+       epvector::iterator itin1 = seq.begin();
+       epvector::iterator itin2 = itin1+1;
+       epvector::iterator itout = itin1;
+       epvector::iterator last = seq.end();
+       // must_copy will be set to true the first time some combination is 
+       // possible from then on the sequence has changed and must be compacted
+       bool must_copy = false;
+       while (itin2!=last) {
+               if (itin1->rest.compare(itin2->rest)==0) {
+                       itin1->coeff = ex_to<numeric>(itin1->coeff).
+                                      add_dyn(ex_to<numeric>(itin2->coeff));
+                       if (expair_needs_further_processing(itin1))
+                               needs_further_processing = true;
+                       must_copy = true;
+               } else {
+                       if (!ex_to<numeric>(itin1->coeff).is_zero()) {
+                               if (must_copy)
+                                       *itout = *itin1;
+                               ++itout;
                        }
-                       ++itin2;
+                       itin1 = itin2;
                }
-               if (!ex_to<numeric>(itin1->coeff).is_zero()) {
-                       if (must_copy)
-                               *itout = *itin1;
-                       ++itout;
-               }
-               if (itout!=last)
-                       seq.erase(itout,last);
+               ++itin2;
        }
-       
+       if (!ex_to<numeric>(itin1->coeff).is_zero()) {
+               if (must_copy)
+                       *itout = *itin1;
+               ++itout;
+       }
+       if (itout!=last)
+               seq.erase(itout,last);
+
        if (needs_further_processing) {
                epvector v = seq;
                seq.clear();
@@ -1476,7 +1469,7 @@ bool expairseq::is_canonical() const
  *  if no members were changed. */
 epvector * expairseq::expandchildren(unsigned options) const
 {
-       epvector::const_iterator last = seq.end();
+       const epvector::const_iterator last = seq.end();
        epvector::const_iterator cit = seq.begin();
        while (cit!=last) {
                const ex &expanded_ex = cit->rest.expand(options);