]> www.ginac.de Git - ginac.git/blob - ginac/expairseq.cpp
d791abf2d53a58fdc01e78109d8c51b54ebea41a
[ginac.git] / ginac / expairseq.cpp
1 /** @file expairseq.cpp
2  *
3  *  Implementation of sequences of expression pairs. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2000 Johannes Gutenberg University Mainz, Germany
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include <algorithm>
24 #include <string>
25 #include <stdexcept>
26 #include <cmath>
27
28 #include "expairseq.h"
29 #include "lst.h"
30 #include "archive.h"
31 #include "debugmsg.h"
32 #include "utils.h"
33
34 #ifndef NO_NAMESPACE_GINAC
35 namespace GiNaC {
36 #endif // ndef NO_NAMESPACE_GINAC
37
38 #ifdef EXPAIRSEQ_USE_HASHTAB
39 #error "FIXME: expair_needs_further_processing not yet implemented for hashtabs, sorry. A.F."
40 #endif // def EXPAIRSEQ_USE_HASHTAB
41
42 GINAC_IMPLEMENT_REGISTERED_CLASS(expairseq, basic)
43
44 //////////
45 // helper classes
46 //////////
47
48 class epp_is_less
49 {
50 public:
51     bool operator()(const epp & lh, const epp & rh) const
52     {
53         return (*lh).is_less(*rh);
54     }
55 };
56
57 //////////
58 // default constructor, destructor, copy constructor assignment operator and helpers
59 //////////
60
61 // public
62
63 expairseq::expairseq(const expairseq & other)
64 {
65     debugmsg("expairseq copy constructor",LOGLEVEL_CONSTRUCT);
66     copy(other);
67 }
68
69 const expairseq & expairseq::operator=(const expairseq & other)
70 {
71     debugmsg("expairseq operator=",LOGLEVEL_ASSIGNMENT);
72     if (this != &other) {
73         destroy(1);
74         copy(other);
75     }
76     return *this;
77 }
78
79 // protected
80
81 void expairseq::copy(const expairseq & other)
82 {
83     inherited::copy(other);
84     seq=other.seq;
85     overall_coeff=other.overall_coeff;
86 #ifdef EXPAIRSEQ_USE_HASHTAB
87     // copy hashtab
88     hashtabsize=other.hashtabsize;
89     if (hashtabsize!=0) {
90     hashmask=other.hashmask;
91         hashtab.resize(hashtabsize);
92         epvector::const_iterator osb=other.seq.begin();
93         for (unsigned i=0; i<hashtabsize; ++i) {
94             hashtab[i].clear();
95             for (epplist::const_iterator cit=other.hashtab[i].begin();
96                  cit!=other.hashtab[i].end(); ++cit) {
97                 hashtab[i].push_back(seq.begin()+((*cit)-osb));
98             }
99         }
100     } else {
101         hashtab.clear();
102     }
103 #endif // def EXPAIRSEQ_USE_HASHTAB
104 }
105
106 //////////
107 // other constructors
108 //////////
109
110 expairseq::expairseq(const ex & lh, const ex & rh) : inherited(TINFO_expairseq)
111 {
112     debugmsg("expairseq constructor from ex,ex",LOGLEVEL_CONSTRUCT);
113     construct_from_2_ex(lh,rh);
114     GINAC_ASSERT(is_canonical());
115 }
116
117 expairseq::expairseq(const exvector & v) : inherited(TINFO_expairseq)
118 {
119     debugmsg("expairseq constructor from exvector",LOGLEVEL_CONSTRUCT);
120     construct_from_exvector(v);
121     GINAC_ASSERT(is_canonical());
122 }
123
124 /*
125 expairseq::expairseq(const epvector & v, bool do_not_canonicalize) :
126     inherited(TINFO_expairseq)
127 {
128     debugmsg("expairseq constructor from epvector",LOGLEVEL_CONSTRUCT);
129     if (do_not_canonicalize) {
130         seq=v;
131 #ifdef EXPAIRSEQ_USE_HASHTAB
132         combine_same_terms(); // to build hashtab
133 #endif // def EXPAIRSEQ_USE_HASHTAB
134     } else {
135         construct_from_epvector(v);
136     }
137     GINAC_ASSERT(is_canonical());
138 }
139 */
140
141 expairseq::expairseq(const epvector & v, const ex & oc) :
142     inherited(TINFO_expairseq), overall_coeff(oc)
143 {
144     debugmsg("expairseq constructor from epvector,ex",LOGLEVEL_CONSTRUCT);
145     construct_from_epvector(v);
146     GINAC_ASSERT(is_canonical());
147 }
148
149 expairseq::expairseq(epvector * vp, const ex & oc) :
150     inherited(TINFO_expairseq), overall_coeff(oc)
151 {
152     debugmsg("expairseq constructor from epvector *,ex",LOGLEVEL_CONSTRUCT);
153     GINAC_ASSERT(vp!=0);
154     construct_from_epvector(*vp);
155     delete vp;
156     GINAC_ASSERT(is_canonical());
157 }
158
159 //////////
160 // archiving
161 //////////
162
163 /** Construct object from archive_node. */
164 expairseq::expairseq(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
165 #ifdef EXPAIRSEQ_USE_HASHTAB
166     , hashtabsize(0)
167 #endif
168 {
169     debugmsg("expairseq constructor from archive_node", LOGLEVEL_CONSTRUCT);
170     for (unsigned int i=0; true; i++) {
171         ex rest;
172         ex coeff;
173         if (n.find_ex("rest", rest, sym_lst, i) && n.find_ex("coeff", coeff, sym_lst, i))
174             seq.push_back(expair(rest, coeff));
175         else
176             break;
177     }
178     n.find_ex("overall_coeff", overall_coeff, sym_lst);
179 }
180
181 /** Unarchive the object. */
182 ex expairseq::unarchive(const archive_node &n, const lst &sym_lst)
183 {
184     return (new expairseq(n, sym_lst))->setflag(status_flags::dynallocated);
185 }
186
187 /** Archive the object. */
188 void expairseq::archive(archive_node &n) const
189 {
190     inherited::archive(n);
191     epvector::const_iterator i = seq.begin(), iend = seq.end();
192     while (i != iend) {
193         n.add_ex("rest", i->rest);
194         n.add_ex("coeff", i->coeff);
195         i++;
196     }
197     n.add_ex("overall_coeff", overall_coeff);
198 }
199
200 //////////
201 // functions overriding virtual functions from bases classes
202 //////////
203
204 // public
205
206 basic * expairseq::duplicate() const
207 {
208     debugmsg("expairseq duplicate",LOGLEVEL_DUPLICATE);
209     return new expairseq(*this);
210 }
211
212 void expairseq::print(ostream & os, unsigned upper_precedence) const
213 {
214     debugmsg("expairseq print",LOGLEVEL_PRINT);
215     os << "[[";
216     printseq(os,',',precedence,upper_precedence);
217     os << "]]";
218 }
219
220 void expairseq::printraw(ostream & os) const
221 {
222     debugmsg("expairseq printraw",LOGLEVEL_PRINT);
223
224     os << "expairseq(";
225     for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
226         os << "(";
227         (*cit).rest.printraw(os);
228         os << ",";
229         (*cit).coeff.printraw(os);
230         os << "),";
231     }
232     os << ")";
233 }
234
235 void expairseq::printtree(ostream & os, unsigned indent) const
236 {
237     debugmsg("expairseq printtree",LOGLEVEL_PRINT);
238
239     os << string(indent,' ') << "type=" << typeid(*this).name()
240        << ", hash=" << hashvalue << " (0x" << hex << hashvalue << dec << ")"
241        << ", flags=" << flags
242        << ", nops=" << nops() << endl;
243     for (unsigned i=0; i<seq.size(); ++i) {
244         seq[i].rest.printtree(os,indent+delta_indent);
245         seq[i].coeff.printtree(os,indent+delta_indent);
246         if (i!=seq.size()-1) {
247             os << string(indent+delta_indent,' ') << "-----" << endl;
248         }
249     }
250     if (!overall_coeff.is_equal(default_overall_coeff())) {
251         os << string(indent+delta_indent,' ') << "-----" << endl;
252         os << string(indent+delta_indent,' ') << "overall_coeff" << endl;
253         overall_coeff.printtree(os,indent+delta_indent);
254     }
255     os << string(indent+delta_indent,' ') << "=====" << endl;
256 #ifdef EXPAIRSEQ_USE_HASHTAB
257     os << string(indent+delta_indent,' ')
258        << "hashtab size " << hashtabsize << endl;
259     if (hashtabsize==0) return;
260 #define MAXCOUNT 5
261     unsigned count[MAXCOUNT+1];
262     for (int i=0; i<MAXCOUNT+1; ++i) count[i]=0;
263     unsigned this_bin_fill;
264     unsigned cum_fill_sq = 0;
265     unsigned cum_fill = 0;
266     for (unsigned i=0; i<hashtabsize; ++i) {
267         this_bin_fill=0;
268         if (hashtab[i].size()>0) {
269             os << string(indent+delta_indent,' ') 
270                << "bin " << i << " with entries ";
271             for (epplist::const_iterator it=hashtab[i].begin();
272                  it!=hashtab[i].end(); ++it) {
273                 os << *it-seq.begin() << " ";
274                 this_bin_fill++;
275             }
276             os << endl;
277             cum_fill += this_bin_fill;
278             cum_fill_sq += this_bin_fill*this_bin_fill;
279         }
280         if (this_bin_fill<MAXCOUNT) {
281             ++count[this_bin_fill];
282         } else {
283             ++count[MAXCOUNT];
284         }
285     }
286     unsigned fact = 1;
287     double cum_prob = 0;
288     double lambda = (1.0*seq.size())/hashtabsize;
289     for (int k=0; k<MAXCOUNT; ++k) {
290         if (k>0) fact *= k;
291         double prob = pow(lambda,k)/fact*exp(-lambda);
292         cum_prob += prob;
293         os << string(indent+delta_indent,' ') << "bins with " << k << " entries: "
294            << int(1000.0*count[k]/hashtabsize)/10.0 << "% (expected: "
295            << int(prob*1000)/10.0 << ")" << endl;
296     }
297     os << string(indent+delta_indent,' ') << "bins with more entries: "
298        << int(1000.0*count[MAXCOUNT]/hashtabsize)/10.0 << "% (expected: "
299        << int((1-cum_prob)*1000)/10.0 << ")" << endl;
300     
301     os << string(indent+delta_indent,' ') << "variance: "
302        << 1.0/hashtabsize*cum_fill_sq-(1.0/hashtabsize*cum_fill)*(1.0/hashtabsize*cum_fill)
303        << endl;
304     os << string(indent+delta_indent,' ') << "average fill: "
305        << (1.0*cum_fill)/hashtabsize
306        << " (should be equal to " << (1.0*seq.size())/hashtabsize << ")" << endl;
307 #endif // def EXPAIRSEQ_USE_HASHTAB
308 }
309
310 bool expairseq::info(unsigned inf) const
311 {
312     return inherited::info(inf);
313 }
314
315 unsigned expairseq::nops() const
316 {
317     if (overall_coeff.is_equal(default_overall_coeff())) {
318         return seq.size();
319     }
320     return seq.size()+1;
321 }
322
323 ex expairseq::op(int i) const
324 {
325     if (unsigned(i)<seq.size()) {
326         return recombine_pair_to_ex(seq[i]);
327     }
328     GINAC_ASSERT(!overall_coeff.is_equal(default_overall_coeff()));
329     return overall_coeff;
330 }
331
332 ex & expairseq::let_op(int i)
333 {
334     throw(std::logic_error("let_op not defined for expairseq and derived classes (add,mul,...)"));
335 }
336
337 ex expairseq::eval(int level) const
338 {
339     if ((level==1)&&(flags & status_flags::evaluated)) {
340         return *this;
341     }
342
343     epvector * vp=evalchildren(level);
344     if (vp==0) {
345         return this->hold();
346     }
347
348     return (new expairseq(vp,overall_coeff))
349                ->setflag(status_flags::dynallocated |
350                          status_flags::evaluated );
351 }
352
353 ex expairseq::evalf(int level) const
354 {
355     return thisexpairseq(evalfchildren(level),overall_coeff.evalf(level-1));
356 }
357
358 ex expairseq::normal(lst &sym_lst, lst &repl_lst, int level) const
359 {
360     ex n = thisexpairseq(normalchildren(level),overall_coeff);
361     return n.bp->basic::normal(sym_lst,repl_lst,level);
362 }
363
364 ex expairseq::subs(const lst & ls, const lst & lr) const
365 {
366     epvector * vp=subschildren(ls,lr);
367     if (vp==0) {
368         return *this;
369     }
370     return thisexpairseq(vp,overall_coeff);
371 }
372
373 // protected
374
375 /** Implementation of ex::diff() for an expairseq. It differentiates all elements of the
376  *  sequence.
377  *  @see ex::diff */
378 ex expairseq::derivative(const symbol & s) const
379 {
380     return thisexpairseq(diffchildren(s),overall_coeff);
381 }
382
383 int expairseq::compare_same_type(const basic & other) const
384 {
385     GINAC_ASSERT(is_of_type(other, expairseq));
386     const expairseq & o = static_cast<const expairseq &>(const_cast<basic &>(other));
387
388     int cmpval;
389     
390     // compare number of elements
391     if (seq.size() != o.seq.size()) {
392         return (seq.size()<o.seq.size()) ? -1 : 1;
393     }
394
395     // compare overall_coeff
396     cmpval = overall_coeff.compare(o.overall_coeff);
397     if (cmpval!=0) return cmpval;
398
399     //if (seq.size()==0) return 0; // empty expairseq's are equal
400
401 #ifdef EXPAIRSEQ_USE_HASHTAB
402     GINAC_ASSERT(hashtabsize==o.hashtabsize);
403     if (hashtabsize==0) {
404 #endif // def EXPAIRSEQ_USE_HASHTAB
405         epvector::const_iterator cit1 = seq.begin();
406         epvector::const_iterator cit2 = o.seq.begin();
407         epvector::const_iterator last1 = seq.end();
408         epvector::const_iterator last2 = o.seq.end();
409         
410         for (; (cit1!=last1)&&(cit2!=last2); ++cit1, ++cit2) {
411             cmpval = (*cit1).compare(*cit2);
412             if (cmpval!=0) return cmpval;
413         }
414
415         GINAC_ASSERT(cit1==last1);
416         GINAC_ASSERT(cit2==last2);
417         
418         return 0;
419 #ifdef EXPAIRSEQ_USE_HASHTAB
420     }
421
422     // compare number of elements in each hashtab entry
423     for (unsigned i=0; i<hashtabsize; ++i) {
424         unsigned cursize=hashtab[i].size();
425         if (cursize != o.hashtab[i].size()) {
426             return (cursize < o.hashtab[i].size()) ? -1 : 1;
427         }
428     }
429     
430     // compare individual (sorted) hashtab entries
431     for (unsigned i=0; i<hashtabsize; ++i) {
432         unsigned sz=hashtab[i].size();
433         if (sz>0) {
434             const epplist & eppl1=hashtab[i];
435             const epplist & eppl2=o.hashtab[i];
436             epplist::const_iterator it1=eppl1.begin();
437             epplist::const_iterator it2=eppl2.begin();
438             while (it1!=eppl1.end()) {
439                 cmpval=(*(*it1)).compare(*(*it2));
440                 if (cmpval!=0) return cmpval;
441                 ++it1;
442                 ++it2;
443             }
444         }
445     }
446     
447     return 0; // equal
448 #endif // def EXPAIRSEQ_USE_HASHTAB
449 }
450
451 bool expairseq::is_equal_same_type(const basic & other) const
452 {
453     const expairseq & o=dynamic_cast<const expairseq &>(const_cast<basic &>(other));
454
455     // compare number of elements
456     if (seq.size() != o.seq.size()) return false;
457
458     // compare overall_coeff
459     if (!overall_coeff.is_equal(o.overall_coeff)) return false;
460
461 #ifdef EXPAIRSEQ_USE_HASHTAB
462     // compare number of elements in each hashtab entry
463     if (hashtabsize!=o.hashtabsize) {
464         cout << "this:" << endl;
465         printtree(cout,0);
466         cout << "other:" << endl;
467         other.printtree(cout,0);
468     }
469         
470     GINAC_ASSERT(hashtabsize==o.hashtabsize);
471     
472     if (hashtabsize==0) {
473 #endif // def EXPAIRSEQ_USE_HASHTAB
474         epvector::const_iterator cit1=seq.begin();
475         epvector::const_iterator cit2=o.seq.begin();
476         epvector::const_iterator last1=seq.end();
477         
478         while (cit1!=last1) {
479             if (!(*cit1).is_equal(*cit2)) return false;
480             ++cit1;
481             ++cit2;
482         }
483         
484         return true;
485 #ifdef EXPAIRSEQ_USE_HASHTAB
486     }
487
488     for (unsigned i=0; i<hashtabsize; ++i) {
489         if (hashtab[i].size() != o.hashtab[i].size()) return false;
490     }
491
492     // compare individual sorted hashtab entries
493     for (unsigned i=0; i<hashtabsize; ++i) {
494         unsigned sz=hashtab[i].size();
495         if (sz>0) {
496             const epplist & eppl1=hashtab[i];
497             const epplist & eppl2=o.hashtab[i];
498             epplist::const_iterator it1=eppl1.begin();
499             epplist::const_iterator it2=eppl2.begin();
500             while (it1!=eppl1.end()) {
501                 if (!(*(*it1)).is_equal(*(*it2))) return false;
502                 ++it1;
503                 ++it2;
504             }
505         }
506     }
507
508     return true;
509 #endif // def EXPAIRSEQ_USE_HASHTAB
510 }
511
512 unsigned expairseq::return_type(void) const
513 {
514     return return_types::noncommutative_composite;
515 }
516
517 unsigned expairseq::calchash(void) const
518 {
519     unsigned v=golden_ratio_hash(tinfo());
520     epvector::const_iterator last=seq.end();
521     for (epvector::const_iterator cit=seq.begin(); cit!=last; ++cit) {
522 #ifndef EXPAIRSEQ_USE_HASHTAB
523         v=rotate_left_31(v); // rotation would spoil commutativity
524 #endif // ndef EXPAIRSEQ_USE_HASHTAB
525         v ^= (*cit).rest.gethash();
526     }
527
528     v ^= overall_coeff.gethash();
529     v=v & 0x7FFFFFFFU;
530     
531     // store calculated hash value only if object is already evaluated
532     if (flags & status_flags::evaluated) {
533         setflag(status_flags::hash_calculated);
534         hashvalue=v;
535     }
536
537     return v;
538 }
539
540 ex expairseq::expand(unsigned options) const
541 {
542     epvector * vp=expandchildren(options);
543     if (vp==0) {
544         return *this;
545     }
546     return thisexpairseq(vp,overall_coeff);
547 }
548
549 //////////
550 // new virtual functions which can be overridden by derived classes
551 //////////
552
553 // protected
554
555 ex expairseq::thisexpairseq(const epvector & v,const ex & oc) const
556 {
557     return expairseq(v,oc);
558 }
559
560 ex expairseq::thisexpairseq(epvector * vp, const ex & oc) const
561 {
562     return expairseq(vp,oc);
563 }
564
565 void expairseq::printpair(ostream & os, const expair & p, unsigned upper_precedence) const
566 {
567     os << "[[";
568     p.rest.bp->print(os,precedence);
569     os << ",";
570     p.coeff.bp->print(os,precedence);
571     os << "]]";
572 }
573
574 void expairseq::printseq(ostream & os, char delim, unsigned this_precedence,
575                          unsigned upper_precedence) const
576 {
577     if (this_precedence<=upper_precedence) os << "(";
578     epvector::const_iterator it,it_last;
579     it_last=seq.end();
580     --it_last;
581     for (it=seq.begin(); it!=it_last; ++it) {
582         printpair(os,*it,this_precedence);
583         os << delim;
584     }
585     printpair(os,*it,this_precedence);
586     if (!overall_coeff.is_equal(default_overall_coeff())) {
587         os << delim << overall_coeff;
588     }
589     if (this_precedence<=upper_precedence) os << ")";
590 }
591     
592 expair expairseq::split_ex_to_pair(const ex & e) const
593 {
594     return expair(e,_ex1());
595 }
596
597 expair expairseq::combine_ex_with_coeff_to_pair(const ex & e,
598                                                 const ex & c) const
599 {
600     GINAC_ASSERT(is_ex_exactly_of_type(c,numeric));
601
602     return expair(e,c);
603 }
604
605 expair expairseq::combine_pair_with_coeff_to_pair(const expair & p,
606                                                   const ex & c) const
607 {
608     GINAC_ASSERT(is_ex_exactly_of_type(p.coeff,numeric));
609     GINAC_ASSERT(is_ex_exactly_of_type(c,numeric));
610     
611     return expair(p.rest,ex_to_numeric(p.coeff).mul_dyn(ex_to_numeric(c)));
612 }
613
614 ex expairseq::recombine_pair_to_ex(const expair & p) const
615 {
616     return lst(p.rest,p.coeff);
617 }
618
619 bool expairseq::expair_needs_further_processing(epp it)
620 {
621     return false;
622 }
623
624 ex expairseq::default_overall_coeff(void) const
625 {
626     return _ex0();
627 }
628
629 void expairseq::combine_overall_coeff(const ex & c)
630 {
631     GINAC_ASSERT(is_ex_exactly_of_type(overall_coeff,numeric));
632     GINAC_ASSERT(is_ex_exactly_of_type(c,numeric));
633     overall_coeff = ex_to_numeric(overall_coeff).add_dyn(ex_to_numeric(c));
634 }
635
636 void expairseq::combine_overall_coeff(const ex & c1, const ex & c2)
637 {
638     GINAC_ASSERT(is_ex_exactly_of_type(overall_coeff,numeric));
639     GINAC_ASSERT(is_ex_exactly_of_type(c1,numeric));
640     GINAC_ASSERT(is_ex_exactly_of_type(c2,numeric));
641     overall_coeff = ex_to_numeric(overall_coeff).
642                         add_dyn(ex_to_numeric(c1).mul(ex_to_numeric(c2)));
643 }
644
645 bool expairseq::can_make_flat(const expair & p) const
646 {
647     return true;
648 }
649
650     
651 //////////
652 // non-virtual functions in this class
653 //////////
654
655 void expairseq::construct_from_2_ex_via_exvector(const ex & lh, const ex & rh)
656 {
657     exvector v;
658     v.reserve(2);
659     v.push_back(lh);
660     v.push_back(rh);
661     construct_from_exvector(v);
662 #ifdef EXPAIRSEQ_USE_HASHTAB
663     GINAC_ASSERT((hashtabsize==0)||(hashtabsize>=minhashtabsize));
664     GINAC_ASSERT(hashtabsize==calc_hashtabsize(seq.size()));
665 #endif // def EXPAIRSEQ_USE_HASHTAB
666 }
667
668 void expairseq::construct_from_2_ex(const ex & lh, const ex & rh)
669 {
670     if (lh.bp->tinfo()==tinfo()) {
671        if (rh.bp->tinfo()==tinfo()) {
672 #ifdef EXPAIRSEQ_USE_HASHTAB
673            unsigned totalsize=ex_to_expairseq(lh).seq.size()+
674                               ex_to_expairseq(rh).seq.size();
675            if (calc_hashtabsize(totalsize)!=0) {
676                construct_from_2_ex_via_exvector(lh,rh);
677            } else {
678 #endif // def EXPAIRSEQ_USE_HASHTAB
679                construct_from_2_expairseq(ex_to_expairseq(lh),
680                                           ex_to_expairseq(rh));
681 #ifdef EXPAIRSEQ_USE_HASHTAB
682            }
683 #endif // def EXPAIRSEQ_USE_HASHTAB
684            return;
685        } else {
686 #ifdef EXPAIRSEQ_USE_HASHTAB
687            unsigned totalsize=ex_to_expairseq(lh).seq.size()+1;
688            if (calc_hashtabsize(totalsize)!=0) {
689                construct_from_2_ex_via_exvector(lh,rh);
690            } else {
691 #endif // def EXPAIRSEQ_USE_HASHTAB
692                construct_from_expairseq_ex(ex_to_expairseq(lh),rh);
693 #ifdef EXPAIRSEQ_USE_HASHTAB
694            }
695 #endif // def EXPAIRSEQ_USE_HASHTAB
696            return;
697        }
698     } else if (rh.bp->tinfo()==tinfo()) {
699 #ifdef EXPAIRSEQ_USE_HASHTAB
700         unsigned totalsize=ex_to_expairseq(rh).seq.size()+1;
701         if (calc_hashtabsize(totalsize)!=0) {
702             construct_from_2_ex_via_exvector(lh,rh);
703         } else {
704 #endif // def EXPAIRSEQ_USE_HASHTAB
705             construct_from_expairseq_ex(ex_to_expairseq(rh),lh);
706 #ifdef EXPAIRSEQ_USE_HASHTAB
707         }
708 #endif // def EXPAIRSEQ_USE_HASHTAB
709         return;
710     }
711
712 #ifdef EXPAIRSEQ_USE_HASHTAB
713     if (calc_hashtabsize(2)!=0) {
714         construct_from_2_ex_via_exvector(lh,rh);
715         return;
716     }
717     hashtabsize=0;
718 #endif // def EXPAIRSEQ_USE_HASHTAB
719     
720     if (is_ex_exactly_of_type(lh,numeric)) {
721         if (is_ex_exactly_of_type(rh,numeric)) {
722             combine_overall_coeff(lh);
723             combine_overall_coeff(rh);
724         } else {
725             combine_overall_coeff(lh);
726             seq.push_back(split_ex_to_pair(rh));
727         }
728     } else {
729         if (is_ex_exactly_of_type(rh,numeric)) {
730             combine_overall_coeff(rh);
731             seq.push_back(split_ex_to_pair(lh));
732         } else {
733             expair p1=split_ex_to_pair(lh);
734             expair p2=split_ex_to_pair(rh);
735
736             int cmpval=p1.rest.compare(p2.rest);
737             if (cmpval==0) {
738                 p1.coeff=ex_to_numeric(p1.coeff).add_dyn(ex_to_numeric(p2.coeff));
739                 if (!ex_to_numeric(p1.coeff).is_zero()) {
740                     // no further processing is necessary, since this
741                     // one element will usually be recombined in eval()
742                     seq.push_back(p1);
743                 }
744             } else {
745                 seq.reserve(2);
746                 if (cmpval<0) {
747                     seq.push_back(p1);
748                     seq.push_back(p2);
749                 } else {
750                     seq.push_back(p2);
751                     seq.push_back(p1);
752                 }
753             }
754         }
755     }
756 }
757
758 void expairseq::construct_from_2_expairseq(const expairseq & s1,
759                                            const expairseq & s2)
760 {
761     combine_overall_coeff(s1.overall_coeff);
762     combine_overall_coeff(s2.overall_coeff);
763
764     epvector::const_iterator first1=s1.seq.begin();
765     epvector::const_iterator last1=s1.seq.end();
766     epvector::const_iterator first2=s2.seq.begin();
767     epvector::const_iterator last2=s2.seq.end();
768
769     seq.reserve(s1.seq.size()+s2.seq.size());
770
771     bool needs_further_processing=false;
772     
773     while (first1!=last1 && first2!=last2) {
774         int cmpval=(*first1).rest.compare((*first2).rest);
775         if (cmpval==0) {
776             // combine terms
777             const numeric & newcoeff=ex_to_numeric((*first1).coeff).
778                                      add(ex_to_numeric((*first2).coeff));
779             if (!newcoeff.is_zero()) {
780                 seq.push_back(expair((*first1).rest,newcoeff));
781                 if (expair_needs_further_processing(seq.end()-1)) {
782                     needs_further_processing = true;
783                 }
784             }
785             ++first1;
786             ++first2;
787         } else if (cmpval<0) {
788             seq.push_back(*first1);
789             ++first1;
790         } else {
791             seq.push_back(*first2);
792             ++first2;
793         }
794     }
795     
796     while (first1!=last1) {
797         seq.push_back(*first1);
798         ++first1;
799     }
800     while (first2!=last2) {
801         seq.push_back(*first2);
802         ++first2;
803     }
804
805     if (needs_further_processing) {
806         epvector v=seq;
807         seq.clear();
808         construct_from_epvector(v);
809     }
810 }
811
812 void expairseq::construct_from_expairseq_ex(const expairseq & s,
813                                             const ex & e)
814 {
815     combine_overall_coeff(s.overall_coeff);
816     if (is_ex_exactly_of_type(e,numeric)) {
817         combine_overall_coeff(e);
818         seq=s.seq;
819         return;
820     }
821
822     epvector::const_iterator first=s.seq.begin();
823     epvector::const_iterator last=s.seq.end();
824     expair p=split_ex_to_pair(e);
825
826     seq.reserve(s.seq.size()+1);
827     bool p_pushed=0;
828
829     bool needs_further_processing=false;
830
831     // merge p into s.seq
832     while (first!=last) {
833         int cmpval=(*first).rest.compare(p.rest);
834         if (cmpval==0) {
835             // combine terms
836             const numeric & newcoeff=ex_to_numeric((*first).coeff).
837                                      add(ex_to_numeric(p.coeff));
838             if (!newcoeff.is_zero()) {
839                 seq.push_back(expair((*first).rest,newcoeff));
840                 if (expair_needs_further_processing(seq.end()-1)) {
841                     needs_further_processing = true;
842                 }
843             }
844             ++first;
845             p_pushed=1;
846             break;
847         } else if (cmpval<0) {
848             seq.push_back(*first);
849             ++first;
850         } else {
851             seq.push_back(p);
852             p_pushed=1;
853             break;
854         }
855     }
856
857     if (p_pushed) {
858         // while loop exited because p was pushed, now push rest of s.seq
859         while (first!=last) {
860             seq.push_back(*first);
861             ++first;
862         }
863     } else {
864         // while loop exited because s.seq was pushed, now push p
865         seq.push_back(p);
866     }
867
868     if (needs_further_processing) {
869         epvector v=seq;
870         seq.clear();
871         construct_from_epvector(v);
872     }
873 }
874
875 void expairseq::construct_from_exvector(const exvector & v)
876 {
877     // simplifications: +(a,+(b,c),d) -> +(a,b,c,d) (associativity)
878     //                  +(d,b,c,a) -> +(a,b,c,d) (canonicalization)
879     //                  +(...,x,*(x,c1),*(x,c2)) -> +(...,*(x,1+c1+c2)) (c1, c2 numeric())
880     //                  (same for (+,*) -> (*,^)
881
882     make_flat(v);
883 #ifdef EXPAIRSEQ_USE_HASHTAB
884     combine_same_terms();
885 #else
886     canonicalize();
887     combine_same_terms_sorted_seq();
888 #endif // def EXPAIRSEQ_USE_HASHTAB
889 }
890
891 void expairseq::construct_from_epvector(const epvector & v)
892 {
893     // simplifications: +(a,+(b,c),d) -> +(a,b,c,d) (associativity)
894     //                  +(d,b,c,a) -> +(a,b,c,d) (canonicalization)
895     //                  +(...,x,*(x,c1),*(x,c2)) -> +(...,*(x,1+c1+c2)) (c1, c2 numeric())
896     //                  (same for (+,*) -> (*,^)
897
898     make_flat(v);
899 #ifdef EXPAIRSEQ_USE_HASHTAB
900     combine_same_terms();
901 #else
902     canonicalize();
903     combine_same_terms_sorted_seq();
904 #endif // def EXPAIRSEQ_USE_HASHTAB
905 }
906
907 #include <iostream>
908
909 void expairseq::make_flat(const exvector & v)
910 {
911     exvector::const_iterator cit, citend = v.end();
912
913     // count number of operands which are of same expairseq derived type
914     // and their cumulative number of operands
915     int nexpairseqs=0;
916     int noperands=0;
917     cit=v.begin();
918     while (cit!=citend) {
919         if (cit->bp->tinfo()==tinfo()) {
920             nexpairseqs++;
921             noperands+=ex_to_expairseq(*cit).seq.size();
922         }
923         ++cit;
924     }
925
926     // reserve seq and coeffseq which will hold all operands
927     seq.reserve(v.size()+noperands-nexpairseqs);
928
929     // copy elements and split off numerical part
930     cit=v.begin();
931     while (cit!=citend) {
932         if (cit->bp->tinfo()==tinfo()) {
933             const expairseq & subseqref=ex_to_expairseq(*cit);
934             combine_overall_coeff(subseqref.overall_coeff);
935             epvector::const_iterator cit_s=subseqref.seq.begin();
936             while (cit_s!=subseqref.seq.end()) {
937                 seq.push_back(*cit_s);
938                 ++cit_s;
939             }
940         } else {
941             if (is_ex_exactly_of_type(*cit,numeric)) {
942                 combine_overall_coeff(*cit);
943             } else {
944                 seq.push_back(split_ex_to_pair(*cit));
945             }
946         }
947         ++cit;
948     }
949
950     /*
951     cout << "after make flat" << endl;
952     for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
953         (*cit).printraw(cout);
954     }
955     cout << endl;
956     */
957 }
958
959 void expairseq::make_flat(const epvector & v)
960 {
961     epvector::const_iterator cit, citend = v.end();
962
963     // count number of operands which are of same expairseq derived type
964     // and their cumulative number of operands
965     int nexpairseqs=0;
966     int noperands=0;
967
968     cit = v.begin();
969     while (cit!=citend) {
970         if (cit->rest.bp->tinfo()==tinfo()) {
971             nexpairseqs++;
972             noperands += ex_to_expairseq((*cit).rest).seq.size();
973         }
974         ++cit;
975     }
976
977     // reserve seq and coeffseq which will hold all operands
978     seq.reserve(v.size()+noperands-nexpairseqs);
979
980     // copy elements and split off numerical part
981     cit = v.begin();
982     while (cit!=citend) {
983         if ((cit->rest.bp->tinfo()==tinfo())&&can_make_flat(*cit)) {
984             const expairseq & subseqref=ex_to_expairseq((*cit).rest);
985             combine_overall_coeff(ex_to_numeric(subseqref.overall_coeff),
986                                   ex_to_numeric((*cit).coeff));
987             epvector::const_iterator cit_s=subseqref.seq.begin();
988             while (cit_s!=subseqref.seq.end()) {
989                 seq.push_back(expair((*cit_s).rest,
990                               ex_to_numeric((*cit_s).coeff).mul_dyn(ex_to_numeric((*cit).coeff))));
991                 //seq.push_back(combine_pair_with_coeff_to_pair(*cit_s,
992                 //                                              (*cit).coeff));
993                 ++cit_s;
994             }
995         } else {
996             if ((*cit).is_numeric_with_coeff_1()) {
997                 combine_overall_coeff((*cit).rest);
998             //if (is_ex_exactly_of_type((*cit).rest,numeric)) {
999             //    combine_overall_coeff(recombine_pair_to_ex(*cit));
1000             } else {
1001                 seq.push_back(*cit);
1002             }
1003         }
1004         ++cit;
1005     }
1006 }
1007
1008 epvector * expairseq::bubblesort(epvector::iterator itbegin, epvector::iterator itend)
1009 {
1010     unsigned n=itend-itbegin;
1011
1012     epvector * sp=new epvector;
1013     sp->reserve(n);
1014
1015     epvector::iterator last=itend-1;
1016     for (epvector::iterator it1=itbegin; it1!=last; ++it1) {
1017         for (epvector::iterator it2=it1+1; it2!=itend; ++it2) {
1018             if ((*it2).rest.compare((*it1).rest)<0) {
1019                 iter_swap(it1,it2);
1020             }
1021         }
1022         sp->push_back(*it1);
1023     }
1024     sp->push_back(*last);
1025     return sp;
1026 }
1027
1028 epvector * expairseq::mergesort(epvector::iterator itbegin, epvector::iterator itend)
1029 {
1030     unsigned n=itend-itbegin;
1031     /*
1032     if (n==1) {
1033         epvector * sp=new epvector;
1034         sp->push_back(*itbegin);
1035         return sp;
1036     }
1037     */
1038     if (n<16) return bubblesort(itbegin, itend);
1039     unsigned m=n/2;
1040     
1041     epvector * s1p=mergesort(itbegin, itbegin+m);
1042     epvector * s2p=mergesort(itbegin+m, itend);
1043
1044     epvector * sp=new epvector;
1045     sp->reserve(s1p->size()+s2p->size());
1046
1047     epvector::iterator first1=s1p->begin();
1048     epvector::iterator last1=s1p->end();
1049
1050     epvector::iterator first2=s2p->begin();
1051     epvector::iterator last2=s2p->end();
1052     
1053     while (first1 != last1 && first2 != last2) {
1054         if ((*first1).rest.compare((*first2).rest)<0) {
1055             sp->push_back(*first1);
1056             ++first1;
1057         } else {
1058             sp->push_back(*first2);
1059             ++first2;
1060         }
1061     }
1062
1063     if (first1 != last1) {
1064         while (first1 != last1) {
1065             sp->push_back(*first1);
1066             ++first1;
1067         }
1068     } else {
1069         while (first2 != last2) {
1070             sp->push_back(*first2);
1071             ++first2;
1072         }
1073     }
1074
1075     delete s1p;
1076     delete s2p;
1077     
1078     return sp;
1079 }
1080             
1081
1082 void expairseq::canonicalize(void)
1083 {
1084     // canonicalize
1085     sort(seq.begin(),seq.end(),expair_is_less());
1086     /*
1087     sort(seq.begin(),seq.end(),expair_is_less_old());
1088     if (seq.size()>1) {
1089         if (is_ex_exactly_of_type((*(seq.begin())).rest,numeric)) {
1090             sort(seq.begin(),seq.end(),expair_is_less());
1091         } else {
1092             epvector::iterator last_numeric=seq.end();
1093             do {
1094                 last_numeric--;
1095             } while (is_ex_exactly_of_type((*last_numeric).rest,numeric));
1096             last_numeric++;
1097             sort(last_numeric,seq.end(),expair_is_less());
1098         }
1099     }
1100     */
1101     
1102     /*
1103     epvector * sorted_seqp=mergesort(seq.begin(),seq.end());
1104     epvector::iterator last=sorted_seqp->end();
1105     epvector::iterator it2=seq.begin();
1106     for (epvector::iterator it1=sorted_seqp->begin(); it1!=last; ++it1, ++it2) {
1107         iter_swap(it1,it2);
1108     }
1109     delete sorted_seqp;
1110     */
1111
1112     /*
1113     cout << "after canonicalize" << endl;
1114     for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
1115         (*cit).printraw(cout);
1116     }
1117     cout << endl;
1118     cout.flush();
1119     */
1120 }
1121
1122 void expairseq::combine_same_terms_sorted_seq(void)
1123 {
1124     bool needs_further_processing=false;
1125     
1126     // combine same terms, drop term with coeff 0
1127     if (seq.size()>1) {
1128         epvector::iterator itin1=seq.begin();
1129         epvector::iterator itin2=itin1+1;
1130         epvector::iterator itout=itin1;
1131         epvector::iterator last=seq.end();
1132         // must_copy will be set to true the first time some combination is possible
1133         // from then on the sequence has changed and must be compacted
1134         bool must_copy=false;
1135         while (itin2!=last) {
1136             if ((*itin1).rest.compare((*itin2).rest)==0) {
1137                 (*itin1).coeff=ex_to_numeric((*itin1).coeff).
1138                                add_dyn(ex_to_numeric((*itin2).coeff));
1139                 if (expair_needs_further_processing(itin1)) {
1140                     needs_further_processing = true;
1141                 }
1142                 must_copy=true;
1143             } else {
1144                 if (!ex_to_numeric((*itin1).coeff).is_zero()) {
1145                     if (must_copy) {
1146                         *itout=*itin1;
1147                     }
1148                     ++itout;
1149                 }
1150                 itin1=itin2;
1151             }
1152             ++itin2;
1153         }
1154         if (!ex_to_numeric((*itin1).coeff).is_zero()) {
1155             if (must_copy) {
1156                 *itout=*itin1;
1157             }
1158             ++itout;
1159         }
1160         if (itout!=last) {
1161             seq.erase(itout,last);
1162         }
1163     }
1164
1165     /*
1166     cout << "after combine" << endl;
1167     for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
1168         (*cit).printraw(cout);
1169     }
1170     cout << endl;
1171     cout.flush();
1172     */
1173     
1174     if (needs_further_processing) {
1175         epvector v=seq;
1176         seq.clear();
1177         construct_from_epvector(v);
1178     }
1179 }
1180
1181 #ifdef EXPAIRSEQ_USE_HASHTAB
1182
1183 unsigned expairseq::calc_hashtabsize(unsigned sz) const
1184 {
1185     unsigned size;
1186     unsigned nearest_power_of_2 = 1 << log2(sz);
1187     //    if (nearest_power_of_2 < maxhashtabsize/hashtabfactor) {
1188     //  size=nearest_power_of_2*hashtabfactor;
1189     size=nearest_power_of_2/hashtabfactor;
1190     if (size<minhashtabsize) return 0;
1191     GINAC_ASSERT(hashtabsize<=0x8000000U); // really max size due to 31 bit hashing
1192     // hashtabsize must be a power of 2
1193     GINAC_ASSERT((1U << log2(size))==size);
1194     return size;
1195 }
1196
1197 unsigned expairseq::calc_hashindex(const ex & e) const
1198 {
1199     // calculate hashindex
1200     unsigned hash=e.gethash();
1201     unsigned hashindex;
1202     if (is_a_numeric_hash(hash)) {
1203         hashindex=hashmask;
1204     } else {
1205         hashindex=hash & hashmask;
1206         // last hashtab entry is reserved for numerics
1207         if (hashindex==hashmask) hashindex=0;
1208     }
1209     GINAC_ASSERT(hashindex>=0);
1210     GINAC_ASSERT((hashindex<hashtabsize)||(hashtabsize==0));
1211     return hashindex;
1212 }
1213
1214 void expairseq::shrink_hashtab(void)
1215 {
1216     unsigned new_hashtabsize;
1217     while (hashtabsize!=(new_hashtabsize=calc_hashtabsize(seq.size()))) {
1218         GINAC_ASSERT(new_hashtabsize<hashtabsize);
1219         if (new_hashtabsize==0) {
1220             hashtab.clear();
1221             hashtabsize=0;
1222             canonicalize();
1223             return;
1224         }
1225         
1226         // shrink by a factor of 2
1227         unsigned half_hashtabsize=hashtabsize/2;
1228         for (unsigned i=0; i<half_hashtabsize-1; ++i) {
1229             hashtab[i].merge(hashtab[i+half_hashtabsize],epp_is_less());
1230         }
1231         // special treatment for numeric hashes
1232         hashtab[0].merge(hashtab[half_hashtabsize-1],epp_is_less());
1233         hashtab[half_hashtabsize-1]=hashtab[hashtabsize-1];
1234         hashtab.resize(half_hashtabsize);
1235         hashtabsize=half_hashtabsize;
1236         hashmask=hashtabsize-1;
1237     }
1238 }
1239
1240 void expairseq::remove_hashtab_entry(epvector::const_iterator element)
1241 {
1242     if (hashtabsize==0) return; // nothing to do
1243     
1244     // calculate hashindex of element to be deleted
1245     unsigned hashindex=calc_hashindex((*element).rest);
1246
1247     // find it in hashtab and remove it
1248     epplist & eppl=hashtab[hashindex];
1249     epplist::iterator epplit=eppl.begin();
1250     bool erased=false;
1251     while (epplit!=eppl.end()) {
1252         if (*epplit == element) {
1253             eppl.erase(epplit);
1254             erased=true;
1255             break;
1256         }
1257         ++epplit;
1258     }
1259     if (!erased) {
1260         printtree(cout,0);
1261         cout << "tried to erase " << element-seq.begin() << endl;
1262         cout << "size " << seq.end()-seq.begin() << endl;
1263
1264         unsigned hashindex=calc_hashindex((*element).rest);
1265         epplist & eppl=hashtab[hashindex];
1266         epplist::iterator epplit=eppl.begin();
1267         bool erased=false;
1268         while (epplit!=eppl.end()) {
1269             if (*epplit == element) {
1270                 eppl.erase(epplit);
1271                 erased=true;
1272                 break;
1273             }
1274             ++epplit;
1275         }
1276         GINAC_ASSERT(erased);
1277     }
1278     GINAC_ASSERT(erased);
1279 }
1280
1281 void expairseq::move_hashtab_entry(epvector::const_iterator oldpos,
1282                                    epvector::iterator newpos)
1283 {
1284     GINAC_ASSERT(hashtabsize!=0);
1285     
1286     // calculate hashindex of element which was moved
1287     unsigned hashindex=calc_hashindex((*newpos).rest);
1288
1289     // find it in hashtab and modify it
1290     epplist & eppl=hashtab[hashindex];
1291     epplist::iterator epplit=eppl.begin();
1292     while (epplit!=eppl.end()) {
1293         if (*epplit == oldpos) {
1294             *epplit=newpos;
1295             break;
1296         }
1297         ++epplit;
1298     }
1299     GINAC_ASSERT(epplit!=eppl.end());
1300 }
1301
1302 void expairseq::sorted_insert(epplist & eppl, epp elem)
1303 {
1304     epplist::iterator current=eppl.begin();
1305     while ((current!=eppl.end())&&((*(*current)).is_less(*elem))) {
1306         ++current;
1307     }
1308     eppl.insert(current,elem);
1309 }    
1310
1311 void expairseq::build_hashtab_and_combine(epvector::iterator & first_numeric,
1312                                           epvector::iterator & last_non_zero,
1313                                           vector<bool> & touched,
1314                                           unsigned & number_of_zeroes)
1315 {
1316     epp current=seq.begin();
1317
1318     while (current!=first_numeric) {
1319         if (is_ex_exactly_of_type((*current).rest,numeric)) {
1320             --first_numeric;
1321             iter_swap(current,first_numeric);
1322         } else {
1323             // calculate hashindex
1324             unsigned currenthashindex=calc_hashindex((*current).rest);
1325
1326             // test if there is already a matching expair in the hashtab-list
1327             epplist & eppl=hashtab[currenthashindex];
1328             epplist::iterator epplit=eppl.begin();
1329             while (epplit!=eppl.end()) {
1330                 if ((*current).rest.is_equal((*(*epplit)).rest)) break;
1331                 ++epplit;
1332             }
1333             if (epplit==eppl.end()) {
1334                 // no matching expair found, append this to end of list
1335                 sorted_insert(eppl,current);
1336                 ++current;
1337             } else {
1338                 // epplit points to a matching expair, combine it with current
1339                 (*(*epplit)).coeff=ex_to_numeric((*(*epplit)).coeff).
1340                                    add_dyn(ex_to_numeric((*current).coeff));
1341                 
1342                 // move obsolete current expair to end by swapping with last_non_zero element
1343                 // if this was a numeric, it is swapped with the expair before first_numeric 
1344                 iter_swap(current,last_non_zero);
1345                 --first_numeric;
1346                 if (first_numeric!=last_non_zero) iter_swap(first_numeric,current);
1347                 --last_non_zero;
1348                 ++number_of_zeroes;
1349                 // test if combined term has coeff 0 and can be removed is done later
1350                 touched[(*epplit)-seq.begin()]=true;
1351             }
1352         }
1353     }
1354 }    
1355
1356 void expairseq::drop_coeff_0_terms(epvector::iterator & first_numeric,
1357                                    epvector::iterator & last_non_zero,
1358                                    vector<bool> & touched,
1359                                    unsigned & number_of_zeroes)
1360 {
1361     // move terms with coeff 0 to end and remove them from hashtab
1362     // check only those elements which have been touched
1363     epp current=seq.begin();
1364     unsigned i=0;
1365     while (current!=first_numeric) {
1366         if (!touched[i]) {
1367             ++current;
1368             ++i;
1369         } else if (!ex_to_numeric((*current).coeff).is_equal(_num0())) {
1370             ++current;
1371             ++i;
1372         } else {
1373             remove_hashtab_entry(current);
1374
1375             // move element to the end, unless it is already at the end
1376             if (current!=last_non_zero) {
1377                 iter_swap(current,last_non_zero);
1378                 --first_numeric;
1379                 bool numeric_swapped=first_numeric!=last_non_zero;
1380                 if (numeric_swapped) iter_swap(first_numeric,current);
1381                 epvector::iterator changed_entry;
1382
1383                 if (numeric_swapped) {
1384                     changed_entry=first_numeric;
1385                 } else {
1386                     changed_entry=last_non_zero;
1387                 }
1388
1389                 --last_non_zero;
1390                 ++number_of_zeroes;
1391
1392                 if (first_numeric!=current) {
1393                 
1394                     // change entry in hashtab which referred to first_numeric or last_non_zero to current
1395                     move_hashtab_entry(changed_entry,current);
1396                     touched[current-seq.begin()]=touched[changed_entry-seq.begin()];
1397                 }
1398             } else {
1399                 --first_numeric;
1400                 --last_non_zero;
1401                 ++number_of_zeroes;
1402             }
1403         }
1404     }
1405     GINAC_ASSERT(i==current-seq.begin());
1406 }
1407
1408 bool expairseq::has_coeff_0(void) const
1409 {
1410     for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
1411         if ((*cit).coeff.is_equal(_ex0())) {
1412             return true;
1413         }
1414     }
1415     return false;
1416 }
1417
1418 void expairseq::add_numerics_to_hashtab(epvector::iterator first_numeric,
1419                                         epvector::const_iterator last_non_zero)
1420 {
1421     if (first_numeric==seq.end()) return; // no numerics    
1422
1423     epvector::iterator current=first_numeric;
1424     epvector::const_iterator last=last_non_zero+1;
1425     while (current!=last) {
1426         sorted_insert(hashtab[hashmask],current);
1427         ++current;
1428     }
1429 }
1430
1431 void expairseq::combine_same_terms(void)
1432 {
1433     // combine same terms, drop term with coeff 0, move numerics to end
1434     
1435     // calculate size of hashtab
1436     hashtabsize=calc_hashtabsize(seq.size());
1437
1438     // hashtabsize is a power of 2
1439     hashmask=hashtabsize-1;
1440
1441     // allocate hashtab
1442     hashtab.clear();
1443     hashtab.resize(hashtabsize);
1444
1445     if (hashtabsize==0) {
1446         canonicalize();
1447         combine_same_terms_sorted_seq();
1448         GINAC_ASSERT(!has_coeff_0());
1449         return;
1450     }
1451
1452     // iterate through seq, move numerics to end,
1453     // fill hashtab and combine same terms
1454     epvector::iterator first_numeric=seq.end();
1455     epvector::iterator last_non_zero=seq.end()-1;
1456
1457     vector<bool> touched;
1458     touched.reserve(seq.size());
1459     for (unsigned i=0; i<seq.size(); ++i) touched[i]=false;
1460
1461     unsigned number_of_zeroes=0;
1462
1463     GINAC_ASSERT(!has_coeff_0());
1464     build_hashtab_and_combine(first_numeric,last_non_zero,touched,number_of_zeroes);
1465     /*
1466     cout << "in combine:" << endl;
1467     printtree(cout,0);
1468     cout << "size=" << seq.end() - seq.begin() << endl;
1469     cout << "first_numeric=" << first_numeric - seq.begin() << endl;
1470     cout << "last_non_zero=" << last_non_zero - seq.begin() << endl;
1471     for (unsigned i=0; i<seq.size(); ++i) {
1472         if (touched[i]) cout << i << " is touched" << endl;
1473     }
1474     cout << "end in combine" << endl;
1475     */
1476     
1477     // there should not be any terms with coeff 0 from the beginning,
1478     // so it should be safe to skip this step
1479     if (number_of_zeroes!=0) {
1480         drop_coeff_0_terms(first_numeric,last_non_zero,touched,number_of_zeroes);
1481         /*
1482         cout << "in combine after drop:" << endl;
1483         printtree(cout,0);
1484         cout << "size=" << seq.end() - seq.begin() << endl;
1485         cout << "first_numeric=" << first_numeric - seq.begin() << endl;
1486         cout << "last_non_zero=" << last_non_zero - seq.begin() << endl;
1487         for (unsigned i=0; i<seq.size(); ++i) {
1488             if (touched[i]) cout << i << " is touched" << endl;
1489         }
1490         cout << "end in combine after drop" << endl;
1491         */
1492     }
1493
1494     add_numerics_to_hashtab(first_numeric,last_non_zero);
1495
1496     // pop zero elements
1497     for (unsigned i=0; i<number_of_zeroes; ++i) {
1498         seq.pop_back();
1499     }
1500
1501     // shrink hashtabsize to calculated value
1502     GINAC_ASSERT(!has_coeff_0());
1503
1504     shrink_hashtab();
1505
1506     GINAC_ASSERT(!has_coeff_0());
1507 }
1508
1509 #endif // def EXPAIRSEQ_USE_HASHTAB
1510
1511 bool expairseq::is_canonical() const
1512 {
1513     if (seq.size()<=1) return 1;
1514
1515 #ifdef EXPAIRSEQ_USE_HASHTAB
1516     if (hashtabsize>0) return 1; // not canoncalized
1517 #endif // def EXPAIRSEQ_USE_HASHTAB
1518     
1519     epvector::const_iterator it = seq.begin();
1520     epvector::const_iterator it_last = it;
1521     for (++it; it!=seq.end(); it_last=it, ++it) {
1522         if (!((*it_last).is_less(*it)||(*it_last).is_equal(*it))) {
1523             if (!is_ex_exactly_of_type((*it_last).rest,numeric)||
1524                 !is_ex_exactly_of_type((*it).rest,numeric)) {
1525                 // double test makes it easier to set a breakpoint...
1526                 if (!is_ex_exactly_of_type((*it_last).rest,numeric)||
1527                     !is_ex_exactly_of_type((*it).rest,numeric)) {
1528                     printpair(cout,*it_last,0);
1529                     cout << ">";
1530                     printpair(cout,*it,0);
1531                     cout << "\n";
1532                     cout << "pair1:" << endl;
1533                     (*it_last).rest.printtree(cout);
1534                     (*it_last).coeff.printtree(cout);
1535                     cout << "pair2:" << endl;
1536                     (*it).rest.printtree(cout);
1537                     (*it).coeff.printtree(cout);
1538                     return 0;
1539                 }
1540             }
1541         }
1542     }
1543     return 1;
1544 }
1545
1546 epvector * expairseq::expandchildren(unsigned options) const
1547 {
1548     epvector::const_iterator last=seq.end();
1549     epvector::const_iterator cit=seq.begin();
1550     while (cit!=last) {
1551         const ex & expanded_ex=(*cit).rest.expand(options);
1552         if (!are_ex_trivially_equal((*cit).rest,expanded_ex)) {
1553
1554             // something changed, copy seq, eval and return it
1555             epvector *s=new epvector;
1556             s->reserve(seq.size());
1557
1558             // copy parts of seq which are known not to have changed
1559             epvector::const_iterator cit2 = seq.begin();
1560             while (cit2!=cit) {
1561                 s->push_back(*cit2);
1562                 ++cit2;
1563             }
1564             // copy first changed element
1565             s->push_back(combine_ex_with_coeff_to_pair(expanded_ex,
1566                                                        (*cit2).coeff));
1567             ++cit2;
1568             // copy rest
1569             while (cit2!=last) {
1570                 s->push_back(combine_ex_with_coeff_to_pair((*cit2).rest.expand(options),
1571                                                            (*cit2).coeff));
1572                 ++cit2;
1573             }
1574             return s;
1575         }
1576         ++cit;
1577     }
1578     
1579     return 0; // nothing has changed
1580 }
1581    
1582 epvector * expairseq::evalchildren(int level) const
1583 {
1584     // returns a NULL pointer if nothing had to be evaluated
1585     // returns a pointer to a newly created epvector otherwise
1586     // (which has to be deleted somewhere else)
1587
1588     if (level==1) {
1589         return 0;
1590     }
1591     if (level == -max_recursion_level) {
1592         throw(std::runtime_error("max recursion level reached"));
1593     }
1594
1595     --level;
1596     epvector::const_iterator last=seq.end();
1597     epvector::const_iterator cit=seq.begin();
1598     while (cit!=last) {
1599         const ex & evaled_ex=(*cit).rest.eval(level);
1600         if (!are_ex_trivially_equal((*cit).rest,evaled_ex)) {
1601
1602             // something changed, copy seq, eval and return it
1603             epvector *s = new epvector;
1604             s->reserve(seq.size());
1605
1606             // copy parts of seq which are known not to have changed
1607             epvector::const_iterator cit2=seq.begin();
1608             while (cit2!=cit) {
1609                 s->push_back(*cit2);
1610                 ++cit2;
1611             }
1612             // copy first changed element
1613             s->push_back(combine_ex_with_coeff_to_pair(evaled_ex,
1614                                                        (*cit2).coeff));
1615             ++cit2;
1616             // copy rest
1617             while (cit2!=last) {
1618                 s->push_back(combine_ex_with_coeff_to_pair((*cit2).rest.eval(level),
1619                                                            (*cit2).coeff));
1620                 ++cit2;
1621             }
1622             return s;
1623         }
1624         ++cit;
1625     }
1626     
1627     return 0; // nothing has changed
1628 }
1629
1630 epvector expairseq::evalfchildren(int level) const
1631 {
1632     if (level==1)
1633         return seq;
1634
1635     if (level==-max_recursion_level)
1636         throw(std::runtime_error("max recursion level reached"));
1637     
1638     epvector s;
1639     s.reserve(seq.size());
1640     
1641     --level;
1642     for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
1643         s.push_back(combine_ex_with_coeff_to_pair((*it).rest.evalf(level),
1644                                                   (*it).coeff.evalf(level)));
1645     }
1646     return s;
1647 }
1648
1649 epvector expairseq::normalchildren(int level) const
1650 {
1651     if (level==1)
1652         return seq;
1653     
1654     if (level == -max_recursion_level)
1655         throw(std::runtime_error("max recursion level reached"));
1656
1657     epvector s;
1658     s.reserve(seq.size());
1659
1660     --level;
1661     for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
1662         s.push_back(combine_ex_with_coeff_to_pair((*it).rest.normal(level),
1663                                                   (*it).coeff));
1664     }
1665     return s;
1666 }
1667
1668 epvector expairseq::diffchildren(const symbol & y) const
1669 {
1670     epvector s;
1671     s.reserve(seq.size());
1672
1673     for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
1674         s.push_back(combine_ex_with_coeff_to_pair((*it).rest.diff(y),
1675                                                   (*it).coeff));
1676     }
1677     return s;
1678 }
1679
1680 epvector * expairseq::subschildren(const lst & ls, const lst & lr) const
1681 {
1682     // returns a NULL pointer if nothing had to be substituted
1683     // returns a pointer to a newly created epvector otherwise
1684     // (which has to be deleted somewhere else)
1685     GINAC_ASSERT(ls.nops()==lr.nops());
1686     
1687     epvector::const_iterator last=seq.end();
1688     epvector::const_iterator cit=seq.begin();
1689     while (cit!=last) {
1690         const ex & subsed_ex=(*cit).rest.subs(ls,lr);
1691         if (!are_ex_trivially_equal((*cit).rest,subsed_ex)) {
1692             
1693             // something changed, copy seq, subs and return it
1694             epvector *s=new epvector;
1695             s->reserve(seq.size());
1696             
1697             // copy parts of seq which are known not to have changed
1698             epvector::const_iterator cit2=seq.begin();
1699             while (cit2!=cit) {
1700                 s->push_back(*cit2);
1701                 ++cit2;
1702             }
1703             // copy first changed element
1704             s->push_back(combine_ex_with_coeff_to_pair(subsed_ex,
1705                                                        (*cit2).coeff));
1706             ++cit2;
1707             // copy rest
1708             while (cit2!=last) {
1709                 s->push_back(combine_ex_with_coeff_to_pair((*cit2).rest.subs(ls,lr),
1710                                                            (*cit2).coeff));
1711                 ++cit2;
1712             }
1713             return s;
1714         }
1715         ++cit;
1716     }
1717     
1718     return 0; // nothing has changed
1719 }
1720
1721 //////////
1722 // static member variables
1723 //////////
1724
1725 // protected
1726
1727 unsigned expairseq::precedence=10;
1728
1729 #ifdef EXPAIRSEQ_USE_HASHTAB
1730 unsigned expairseq::maxhashtabsize=0x4000000U;
1731 unsigned expairseq::minhashtabsize=0x1000U;
1732 unsigned expairseq::hashtabfactor=1;
1733 #endif // def EXPAIRSEQ_USE_HASHTAB
1734
1735 //////////
1736 // global constants
1737 //////////
1738
1739 const expairseq some_expairseq;
1740 const type_info & typeid_expairseq=typeid(some_expairseq);
1741
1742 #ifndef NO_NAMESPACE_GINAC
1743 } // namespace GiNaC
1744 #endif // ndef NO_NAMESPACE_GINAC