- changed behaviour of numeric::is_rational() and added numeric::is_cinteger()
[ginac.git] / ginac / expairseq.cpp
index 05b7233d108e259b951009e018fda8b6ee707fa1..ec88239471a393ee20f6cb239b72d1939812e308 100644 (file)
@@ -29,7 +29,9 @@
 #include "debugmsg.h"
 #include "utils.h"
 
+#ifndef NO_GINAC_NAMESPACE
 namespace GiNaC {
+#endif // ndef NO_GINAC_NAMESPACE
 
 #ifdef EXPAIRSEQ_USE_HASHTAB
 #error "FIXME: expair_needs_further_processing not yet implemented for hashtabs, sorry. A.F."
@@ -105,14 +107,14 @@ expairseq::expairseq(ex const & lh, ex const & rh) : basic(TINFO_expairseq)
 {
     debugmsg("expairseq constructor from ex,ex",LOGLEVEL_CONSTRUCT);
     construct_from_2_ex(lh,rh);
-    ASSERT(is_canonical());
+    GINAC_ASSERT(is_canonical());
 }
 
 expairseq::expairseq(exvector const & v) : basic(TINFO_expairseq)
 {
     debugmsg("expairseq constructor from exvector",LOGLEVEL_CONSTRUCT);
     construct_from_exvector(v);
-    ASSERT(is_canonical());
+    GINAC_ASSERT(is_canonical());
 }
 
 /*
@@ -128,7 +130,7 @@ expairseq::expairseq(epvector const & v, bool do_not_canonicalize) :
     } else {
         construct_from_epvector(v);
     }
-    ASSERT(is_canonical());
+    GINAC_ASSERT(is_canonical());
 }
 */
 
@@ -137,17 +139,17 @@ expairseq::expairseq(epvector const & v, ex const & oc) :
 {
     debugmsg("expairseq constructor from epvector,ex",LOGLEVEL_CONSTRUCT);
     construct_from_epvector(v);
-    ASSERT(is_canonical());
+    GINAC_ASSERT(is_canonical());
 }
 
 expairseq::expairseq(epvector * vp, ex const & oc) :
     basic(TINFO_expairseq), overall_coeff(oc)
 {
     debugmsg("expairseq constructor from epvector *,ex",LOGLEVEL_CONSTRUCT);
-    ASSERT(vp!=0);
+    GINAC_ASSERT(vp!=0);
     construct_from_epvector(*vp);
     delete vp;
-    ASSERT(is_canonical());
+    GINAC_ASSERT(is_canonical());
 }
 
 //////////
@@ -180,7 +182,7 @@ ex expairseq::op(int const i) const
     if (unsigned(i)<seq.size()) {
         return recombine_pair_to_ex(seq[i]);
     }
-    ASSERT(!overall_coeff.is_equal(default_overall_coeff()));
+    GINAC_ASSERT(!overall_coeff.is_equal(default_overall_coeff()));
     return overall_coeff;
 }
 
@@ -229,7 +231,7 @@ ex expairseq::subs(lst const & ls, lst const & lr) const
 
 int expairseq::compare_same_type(basic const & other) const
 {
-    ASSERT(is_of_type(other, expairseq));
+    GINAC_ASSERT(is_of_type(other, expairseq));
     expairseq const & o=static_cast<expairseq const &>(const_cast<basic &>(other));
 
     int cmpval;
@@ -246,7 +248,7 @@ int expairseq::compare_same_type(basic const & other) const
     //if (seq.size()==0) return 0; // empty expairseq's are equal
 
 #ifdef EXPAIRSEQ_USE_HASHTAB
-    ASSERT(hashtabsize==o.hashtabsize);
+    GINAC_ASSERT(hashtabsize==o.hashtabsize);
     if (hashtabsize==0) {
 #endif // def EXPAIRSEQ_USE_HASHTAB
         epvector::const_iterator cit1=seq.begin();
@@ -259,8 +261,8 @@ int expairseq::compare_same_type(basic const & other) const
             if (cmpval!=0) return cmpval;
         }
 
-        ASSERT(cit1==last1);
-        ASSERT(cit2==last2);
+        GINAC_ASSERT(cit1==last1);
+        GINAC_ASSERT(cit2==last2);
         
         return 0;
 #ifdef EXPAIRSEQ_USE_HASHTAB
@@ -314,7 +316,7 @@ bool expairseq::is_equal_same_type(basic const & other) const
         other.printtree(cout,0);
     }
         
-    ASSERT(hashtabsize==o.hashtabsize);
+    GINAC_ASSERT(hashtabsize==o.hashtabsize);
     
     if (hashtabsize==0) {
 #endif // def EXPAIRSEQ_USE_HASHTAB
@@ -417,7 +419,7 @@ expair expairseq::split_ex_to_pair(ex const & e) const
 expair expairseq::combine_ex_with_coeff_to_pair(ex const & e,
                                                 ex const & c) const
 {
-    ASSERT(is_ex_exactly_of_type(c,numeric));
+    GINAC_ASSERT(is_ex_exactly_of_type(c,numeric));
 
     return expair(e,c);
 }
@@ -425,8 +427,8 @@ expair expairseq::combine_ex_with_coeff_to_pair(ex const & e,
 expair expairseq::combine_pair_with_coeff_to_pair(expair const & p,
                                                   ex const & c) const
 {
-    ASSERT(is_ex_exactly_of_type(p.coeff,numeric));
-    ASSERT(is_ex_exactly_of_type(c,numeric));
+    GINAC_ASSERT(is_ex_exactly_of_type(p.coeff,numeric));
+    GINAC_ASSERT(is_ex_exactly_of_type(c,numeric));
     
     return expair(p.rest,ex_to_numeric(p.coeff).mul_dyn(ex_to_numeric(c)));
 }
@@ -448,16 +450,16 @@ ex expairseq::default_overall_coeff(void) const
 
 void expairseq::combine_overall_coeff(ex const & c)
 {
-    ASSERT(is_ex_exactly_of_type(overall_coeff,numeric));
-    ASSERT(is_ex_exactly_of_type(c,numeric));
+    GINAC_ASSERT(is_ex_exactly_of_type(overall_coeff,numeric));
+    GINAC_ASSERT(is_ex_exactly_of_type(c,numeric));
     overall_coeff = ex_to_numeric(overall_coeff).add_dyn(ex_to_numeric(c));
 }
 
 void expairseq::combine_overall_coeff(ex const & c1, ex const & c2)
 {
-    ASSERT(is_ex_exactly_of_type(overall_coeff,numeric));
-    ASSERT(is_ex_exactly_of_type(c1,numeric));
-    ASSERT(is_ex_exactly_of_type(c2,numeric));
+    GINAC_ASSERT(is_ex_exactly_of_type(overall_coeff,numeric));
+    GINAC_ASSERT(is_ex_exactly_of_type(c1,numeric));
+    GINAC_ASSERT(is_ex_exactly_of_type(c2,numeric));
     overall_coeff = ex_to_numeric(overall_coeff).
                         add_dyn(ex_to_numeric(c1).mul(ex_to_numeric(c2)));
 }
@@ -480,8 +482,8 @@ void expairseq::construct_from_2_ex_via_exvector(ex const & lh, ex const & rh)
     v.push_back(rh);
     construct_from_exvector(v);
 #ifdef EXPAIRSEQ_USE_HASHTAB
-    ASSERT((hashtabsize==0)||(hashtabsize>=minhashtabsize));
-    ASSERT(hashtabsize==calc_hashtabsize(seq.size()));
+    GINAC_ASSERT((hashtabsize==0)||(hashtabsize>=minhashtabsize));
+    GINAC_ASSERT(hashtabsize==calc_hashtabsize(seq.size()));
 #endif // def EXPAIRSEQ_USE_HASHTAB
 }
 
@@ -1008,9 +1010,9 @@ unsigned expairseq::calc_hashtabsize(unsigned sz) const
     //  size=nearest_power_of_2*hashtabfactor;
     size=nearest_power_of_2/hashtabfactor;
     if (size<minhashtabsize) return 0;
-    ASSERT(hashtabsize<=0x8000000U); // really max size due to 31 bit hashing
+    GINAC_ASSERT(hashtabsize<=0x8000000U); // really max size due to 31 bit hashing
     // hashtabsize must be a power of 2
-    ASSERT((1U << log2(size))==size);
+    GINAC_ASSERT((1U << log2(size))==size);
     return size;
 }
 
@@ -1026,8 +1028,8 @@ unsigned expairseq::calc_hashindex(ex const & e) const
         // last hashtab entry is reserved for numerics
         if (hashindex==hashmask) hashindex=0;
     }
-    ASSERT(hashindex>=0);
-    ASSERT((hashindex<hashtabsize)||(hashtabsize==0));
+    GINAC_ASSERT(hashindex>=0);
+    GINAC_ASSERT((hashindex<hashtabsize)||(hashtabsize==0));
     return hashindex;
 }
 
@@ -1035,7 +1037,7 @@ void expairseq::shrink_hashtab(void)
 {
     unsigned new_hashtabsize;
     while (hashtabsize!=(new_hashtabsize=calc_hashtabsize(seq.size()))) {
-        ASSERT(new_hashtabsize<hashtabsize);
+        GINAC_ASSERT(new_hashtabsize<hashtabsize);
         if (new_hashtabsize==0) {
             hashtab.clear();
             hashtabsize=0;
@@ -1093,15 +1095,15 @@ void expairseq::remove_hashtab_entry(epvector::const_iterator element)
             }
             ++epplit;
         }
-        ASSERT(erased);
+        GINAC_ASSERT(erased);
     }
-    ASSERT(erased);
+    GINAC_ASSERT(erased);
 }
 
 void expairseq::move_hashtab_entry(epvector::const_iterator oldpos,
                                    epvector::iterator newpos)
 {
-    ASSERT(hashtabsize!=0);
+    GINAC_ASSERT(hashtabsize!=0);
     
     // calculate hashindex of element which was moved
     unsigned hashindex=calc_hashindex((*newpos).rest);
@@ -1116,7 +1118,7 @@ void expairseq::move_hashtab_entry(epvector::const_iterator oldpos,
         }
         ++epplit;
     }
-    ASSERT(epplit!=eppl.end());
+    GINAC_ASSERT(epplit!=eppl.end());
 }
 
 void expairseq::sorted_insert(epplist & eppl, epp elem)
@@ -1222,7 +1224,7 @@ void expairseq::drop_coeff_0_terms(epvector::iterator & first_numeric,
             }
         }
     }
-    ASSERT(i==current-seq.begin());
+    GINAC_ASSERT(i==current-seq.begin());
 }
 
 bool expairseq::has_coeff_0(void) const
@@ -1265,7 +1267,7 @@ void expairseq::combine_same_terms(void)
     if (hashtabsize==0) {
         canonicalize();
         combine_same_terms_sorted_seq();
-        ASSERT(!has_coeff_0());
+        GINAC_ASSERT(!has_coeff_0());
         return;
     }
 
@@ -1280,7 +1282,7 @@ void expairseq::combine_same_terms(void)
 
     unsigned number_of_zeroes=0;
 
-    ASSERT(!has_coeff_0());
+    GINAC_ASSERT(!has_coeff_0());
     build_hashtab_and_combine(first_numeric,last_non_zero,touched,number_of_zeroes);
     /*
     cout << "in combine:" << endl;
@@ -1319,11 +1321,11 @@ void expairseq::combine_same_terms(void)
     }
 
     // shrink hashtabsize to calculated value
-    ASSERT(!has_coeff_0());
+    GINAC_ASSERT(!has_coeff_0());
 
     shrink_hashtab();
 
-    ASSERT(!has_coeff_0());
+    GINAC_ASSERT(!has_coeff_0());
 }
 
 #endif // def EXPAIRSEQ_USE_HASHTAB
@@ -1502,17 +1504,18 @@ epvector * expairseq::subschildren(lst const & ls, lst const & lr) const
     // returns a NULL pointer if nothing had to be substituted
     // returns a pointer to a newly created epvector otherwise
     // (which has to be deleted somewhere else)
-
+    GINAC_ASSERT(ls.nops()==lr.nops());
+    
     epvector::const_iterator last=seq.end();
     epvector::const_iterator cit=seq.begin();
     while (cit!=last) {
         ex const & subsed_ex=(*cit).rest.subs(ls,lr);
         if (!are_ex_trivially_equal((*cit).rest,subsed_ex)) {
-
+            
             // something changed, copy seq, subs and return it
             epvector *s=new epvector;
             s->reserve(seq.size());
-
+            
             // copy parts of seq which are known not to have changed
             epvector::const_iterator cit2=seq.begin();
             while (cit2!=cit) {
@@ -1528,7 +1531,7 @@ epvector * expairseq::subschildren(lst const & ls, lst const & lr) const
                 s->push_back(combine_ex_with_coeff_to_pair((*cit2).rest.subs(ls,lr),
                                                            (*cit2).coeff));
                 ++cit2;
-           }
+            }
             return s;
         }
         ++cit;
@@ -1537,75 +1540,6 @@ epvector * expairseq::subschildren(lst const & ls, lst const & lr) const
     return 0; // nothing has changed
 }
 
-/*
-epvector expairseq::subschildren(lst const & ls, lst const & lr) const
-{
-    epvector s;
-    s.reserve(seq.size());
-
-    for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
-        s.push_back(split_ex_to_pair((*it).rest.subs(ls,lr),(*it).coeff));
-    }
-    return s;
-}
-*/
-
-/*
-void expairseq::sort(epviter first, epviter last, expair_is_less comp)
-{
-    if (first != last) {
-        introsort_loop(first, last, lg(last - first) * 2, comp);
-        __final_insertion_sort(first, last, comp);
-    }
-}
-
-ptrdiff_t expairseq::lg(ptrdiff_t n)
-{
-    ptrdiff_t k;
-    for (k = 0; n > 1; n >>= 1) ++k;
-    return k;
-}
-
-void expairseq::introsort_loop(epviter first, epviter last,
-                               ptrdiff_t depth_limit, expair_is_less comp)
-{
-    while (last - first > stl_threshold) {
-        if (depth_limit == 0) {
-            partial_sort(first, last, last, comp);
-            return;
-        }
-        --depth_limit;
-        epviter cut = unguarded_partition(first, last,
-                      expair(__median(*first, *(first + (last - first)/2),
-                      *(last - 1), comp)), comp);
-        introsort_loop(cut, last, depth_limit, comp);
-        last = cut;
-    }
-}
-
-epviter expairseq::unguarded_partition(epviter first, epviter last, 
-                                       expair pivot, expair_is_less comp)
-{
-    while (1) {
-        while (comp(*first, pivot)) ++first;
-        --last;
-        while (comp(pivot, *last)) --last;
-        if (!(first < last)) return first;
-        iter_swap(first, last);
-        ++first;
-    }
-}
-
-void expairseq::partial_sort(epviter first, epviter middle, epviter last,
-                             expair_is_less comp) {
-  make_heap(first, middle, comp);
-  for (RandomAccessIterator i = middle; i < last; ++i)
-    if (comp(*i, *first))
-      __pop_heap(first, middle, i, T(*i), comp, distance_type(first));
-  sort_heap(first, middle, comp);
-}
-*/
-
 //////////
 // static member variables
 //////////
@@ -1627,4 +1561,6 @@ unsigned expairseq::hashtabfactor=1;
 const expairseq some_expairseq;
 type_info const & typeid_expairseq=typeid(some_expairseq);
 
+#ifndef NO_GINAC_NAMESPACE
 } // namespace GiNaC
+#endif // ndef NO_GINAC_NAMESPACE