- added ex::to_rational() to convert general expression to rational expression
[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::to_rational(lst &repl_lst) const
365 {
366     epvector s;
367     s.reserve(seq.size());
368     for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
369         s.push_back(combine_ex_with_coeff_to_pair((*it).rest.to_rational(repl_lst),
370                                                   (*it).coeff));
371     }
372     return thisexpairseq(s, overall_coeff);
373 }
374
375 ex expairseq::subs(const lst & ls, const lst & lr) const
376 {
377     epvector * vp=subschildren(ls,lr);
378     if (vp==0) {
379         return *this;
380     }
381     return thisexpairseq(vp,overall_coeff);
382 }
383
384 // protected
385
386 /** Implementation of ex::diff() for an expairseq. It differentiates all elements of the
387  *  sequence.
388  *  @see ex::diff */
389 ex expairseq::derivative(const symbol & s) const
390 {
391     return thisexpairseq(diffchildren(s),overall_coeff);
392 }
393
394 int expairseq::compare_same_type(const basic & other) const
395 {
396     GINAC_ASSERT(is_of_type(other, expairseq));
397     const expairseq & o = static_cast<const expairseq &>(const_cast<basic &>(other));
398
399     int cmpval;
400     
401     // compare number of elements
402     if (seq.size() != o.seq.size()) {
403         return (seq.size()<o.seq.size()) ? -1 : 1;
404     }
405
406     // compare overall_coeff
407     cmpval = overall_coeff.compare(o.overall_coeff);
408     if (cmpval!=0) return cmpval;
409
410     //if (seq.size()==0) return 0; // empty expairseq's are equal
411
412 #ifdef EXPAIRSEQ_USE_HASHTAB
413     GINAC_ASSERT(hashtabsize==o.hashtabsize);
414     if (hashtabsize==0) {
415 #endif // def EXPAIRSEQ_USE_HASHTAB
416         epvector::const_iterator cit1 = seq.begin();
417         epvector::const_iterator cit2 = o.seq.begin();
418         epvector::const_iterator last1 = seq.end();
419         epvector::const_iterator last2 = o.seq.end();
420         
421         for (; (cit1!=last1)&&(cit2!=last2); ++cit1, ++cit2) {
422             cmpval = (*cit1).compare(*cit2);
423             if (cmpval!=0) return cmpval;
424         }
425
426         GINAC_ASSERT(cit1==last1);
427         GINAC_ASSERT(cit2==last2);
428         
429         return 0;
430 #ifdef EXPAIRSEQ_USE_HASHTAB
431     }
432
433     // compare number of elements in each hashtab entry
434     for (unsigned i=0; i<hashtabsize; ++i) {
435         unsigned cursize=hashtab[i].size();
436         if (cursize != o.hashtab[i].size()) {
437             return (cursize < o.hashtab[i].size()) ? -1 : 1;
438         }
439     }
440     
441     // compare individual (sorted) hashtab entries
442     for (unsigned i=0; i<hashtabsize; ++i) {
443         unsigned sz=hashtab[i].size();
444         if (sz>0) {
445             const epplist & eppl1=hashtab[i];
446             const epplist & eppl2=o.hashtab[i];
447             epplist::const_iterator it1=eppl1.begin();
448             epplist::const_iterator it2=eppl2.begin();
449             while (it1!=eppl1.end()) {
450                 cmpval=(*(*it1)).compare(*(*it2));
451                 if (cmpval!=0) return cmpval;
452                 ++it1;
453                 ++it2;
454             }
455         }
456     }
457     
458     return 0; // equal
459 #endif // def EXPAIRSEQ_USE_HASHTAB
460 }
461
462 bool expairseq::is_equal_same_type(const basic & other) const
463 {
464     const expairseq & o=dynamic_cast<const expairseq &>(const_cast<basic &>(other));
465
466     // compare number of elements
467     if (seq.size() != o.seq.size()) return false;
468
469     // compare overall_coeff
470     if (!overall_coeff.is_equal(o.overall_coeff)) return false;
471
472 #ifdef EXPAIRSEQ_USE_HASHTAB
473     // compare number of elements in each hashtab entry
474     if (hashtabsize!=o.hashtabsize) {
475         cout << "this:" << endl;
476         printtree(cout,0);
477         cout << "other:" << endl;
478         other.printtree(cout,0);
479     }
480         
481     GINAC_ASSERT(hashtabsize==o.hashtabsize);
482     
483     if (hashtabsize==0) {
484 #endif // def EXPAIRSEQ_USE_HASHTAB
485         epvector::const_iterator cit1=seq.begin();
486         epvector::const_iterator cit2=o.seq.begin();
487         epvector::const_iterator last1=seq.end();
488         
489         while (cit1!=last1) {
490             if (!(*cit1).is_equal(*cit2)) return false;
491             ++cit1;
492             ++cit2;
493         }
494         
495         return true;
496 #ifdef EXPAIRSEQ_USE_HASHTAB
497     }
498
499     for (unsigned i=0; i<hashtabsize; ++i) {
500         if (hashtab[i].size() != o.hashtab[i].size()) return false;
501     }
502
503     // compare individual sorted hashtab entries
504     for (unsigned i=0; i<hashtabsize; ++i) {
505         unsigned sz=hashtab[i].size();
506         if (sz>0) {
507             const epplist & eppl1=hashtab[i];
508             const epplist & eppl2=o.hashtab[i];
509             epplist::const_iterator it1=eppl1.begin();
510             epplist::const_iterator it2=eppl2.begin();
511             while (it1!=eppl1.end()) {
512                 if (!(*(*it1)).is_equal(*(*it2))) return false;
513                 ++it1;
514                 ++it2;
515             }
516         }
517     }
518
519     return true;
520 #endif // def EXPAIRSEQ_USE_HASHTAB
521 }
522
523 unsigned expairseq::return_type(void) const
524 {
525     return return_types::noncommutative_composite;
526 }
527
528 unsigned expairseq::calchash(void) const
529 {
530     unsigned v=golden_ratio_hash(tinfo());
531     epvector::const_iterator last=seq.end();
532     for (epvector::const_iterator cit=seq.begin(); cit!=last; ++cit) {
533 #ifndef EXPAIRSEQ_USE_HASHTAB
534         v=rotate_left_31(v); // rotation would spoil commutativity
535 #endif // ndef EXPAIRSEQ_USE_HASHTAB
536         v ^= (*cit).rest.gethash();
537     }
538
539     v ^= overall_coeff.gethash();
540     v=v & 0x7FFFFFFFU;
541     
542     // store calculated hash value only if object is already evaluated
543     if (flags & status_flags::evaluated) {
544         setflag(status_flags::hash_calculated);
545         hashvalue=v;
546     }
547
548     return v;
549 }
550
551 ex expairseq::expand(unsigned options) const
552 {
553     epvector * vp = expandchildren(options);
554     if (vp==0) {
555         return *this;
556     }
557     return thisexpairseq(vp,overall_coeff);
558 }
559
560 //////////
561 // new virtual functions which can be overridden by derived classes
562 //////////
563
564 // protected
565
566 ex expairseq::thisexpairseq(const epvector & v,const ex & oc) const
567 {
568     return expairseq(v,oc);
569 }
570
571 ex expairseq::thisexpairseq(epvector * vp, const ex & oc) const
572 {
573     return expairseq(vp,oc);
574 }
575
576 void expairseq::printpair(ostream & os, const expair & p, unsigned upper_precedence) const
577 {
578     os << "[[";
579     p.rest.bp->print(os,precedence);
580     os << ",";
581     p.coeff.bp->print(os,precedence);
582     os << "]]";
583 }
584
585 void expairseq::printseq(ostream & os, char delim, unsigned this_precedence,
586                          unsigned upper_precedence) const
587 {
588     if (this_precedence<=upper_precedence) os << "(";
589     epvector::const_iterator it,it_last;
590     it_last=seq.end();
591     --it_last;
592     for (it=seq.begin(); it!=it_last; ++it) {
593         printpair(os,*it,this_precedence);
594         os << delim;
595     }
596     printpair(os,*it,this_precedence);
597     if (!overall_coeff.is_equal(default_overall_coeff())) {
598         os << delim << overall_coeff;
599     }
600     if (this_precedence<=upper_precedence) os << ")";
601 }
602     
603 expair expairseq::split_ex_to_pair(const ex & e) const
604 {
605     return expair(e,_ex1());
606 }
607
608 expair expairseq::combine_ex_with_coeff_to_pair(const ex & e,
609                                                 const ex & c) const
610 {
611     GINAC_ASSERT(is_ex_exactly_of_type(c,numeric));
612
613     return expair(e,c);
614 }
615
616 expair expairseq::combine_pair_with_coeff_to_pair(const expair & p,
617                                                   const ex & c) const
618 {
619     GINAC_ASSERT(is_ex_exactly_of_type(p.coeff,numeric));
620     GINAC_ASSERT(is_ex_exactly_of_type(c,numeric));
621     
622     return expair(p.rest,ex_to_numeric(p.coeff).mul_dyn(ex_to_numeric(c)));
623 }
624
625 ex expairseq::recombine_pair_to_ex(const expair & p) const
626 {
627     return lst(p.rest,p.coeff);
628 }
629
630 bool expairseq::expair_needs_further_processing(epp it)
631 {
632     return false;
633 }
634
635 ex expairseq::default_overall_coeff(void) const
636 {
637     return _ex0();
638 }
639
640 void expairseq::combine_overall_coeff(const ex & c)
641 {
642     GINAC_ASSERT(is_ex_exactly_of_type(overall_coeff,numeric));
643     GINAC_ASSERT(is_ex_exactly_of_type(c,numeric));
644     overall_coeff = ex_to_numeric(overall_coeff).add_dyn(ex_to_numeric(c));
645 }
646
647 void expairseq::combine_overall_coeff(const ex & c1, const ex & c2)
648 {
649     GINAC_ASSERT(is_ex_exactly_of_type(overall_coeff,numeric));
650     GINAC_ASSERT(is_ex_exactly_of_type(c1,numeric));
651     GINAC_ASSERT(is_ex_exactly_of_type(c2,numeric));
652     overall_coeff = ex_to_numeric(overall_coeff).
653                         add_dyn(ex_to_numeric(c1).mul(ex_to_numeric(c2)));
654 }
655
656 bool expairseq::can_make_flat(const expair & p) const
657 {
658     return true;
659 }
660
661     
662 //////////
663 // non-virtual functions in this class
664 //////////
665
666 void expairseq::construct_from_2_ex_via_exvector(const ex & lh, const ex & rh)
667 {
668     exvector v;
669     v.reserve(2);
670     v.push_back(lh);
671     v.push_back(rh);
672     construct_from_exvector(v);
673 #ifdef EXPAIRSEQ_USE_HASHTAB
674     GINAC_ASSERT((hashtabsize==0)||(hashtabsize>=minhashtabsize));
675     GINAC_ASSERT(hashtabsize==calc_hashtabsize(seq.size()));
676 #endif // def EXPAIRSEQ_USE_HASHTAB
677 }
678
679 void expairseq::construct_from_2_ex(const ex & lh, const ex & rh)
680 {
681     if (lh.bp->tinfo()==tinfo()) {
682        if (rh.bp->tinfo()==tinfo()) {
683 #ifdef EXPAIRSEQ_USE_HASHTAB
684            unsigned totalsize=ex_to_expairseq(lh).seq.size()+
685                               ex_to_expairseq(rh).seq.size();
686            if (calc_hashtabsize(totalsize)!=0) {
687                construct_from_2_ex_via_exvector(lh,rh);
688            } else {
689 #endif // def EXPAIRSEQ_USE_HASHTAB
690                construct_from_2_expairseq(ex_to_expairseq(lh),
691                                           ex_to_expairseq(rh));
692 #ifdef EXPAIRSEQ_USE_HASHTAB
693            }
694 #endif // def EXPAIRSEQ_USE_HASHTAB
695            return;
696        } else {
697 #ifdef EXPAIRSEQ_USE_HASHTAB
698            unsigned totalsize=ex_to_expairseq(lh).seq.size()+1;
699            if (calc_hashtabsize(totalsize)!=0) {
700                construct_from_2_ex_via_exvector(lh,rh);
701            } else {
702 #endif // def EXPAIRSEQ_USE_HASHTAB
703                construct_from_expairseq_ex(ex_to_expairseq(lh),rh);
704 #ifdef EXPAIRSEQ_USE_HASHTAB
705            }
706 #endif // def EXPAIRSEQ_USE_HASHTAB
707            return;
708        }
709     } else if (rh.bp->tinfo()==tinfo()) {
710 #ifdef EXPAIRSEQ_USE_HASHTAB
711         unsigned totalsize=ex_to_expairseq(rh).seq.size()+1;
712         if (calc_hashtabsize(totalsize)!=0) {
713             construct_from_2_ex_via_exvector(lh,rh);
714         } else {
715 #endif // def EXPAIRSEQ_USE_HASHTAB
716             construct_from_expairseq_ex(ex_to_expairseq(rh),lh);
717 #ifdef EXPAIRSEQ_USE_HASHTAB
718         }
719 #endif // def EXPAIRSEQ_USE_HASHTAB
720         return;
721     }
722
723 #ifdef EXPAIRSEQ_USE_HASHTAB
724     if (calc_hashtabsize(2)!=0) {
725         construct_from_2_ex_via_exvector(lh,rh);
726         return;
727     }
728     hashtabsize=0;
729 #endif // def EXPAIRSEQ_USE_HASHTAB
730     
731     if (is_ex_exactly_of_type(lh,numeric)) {
732         if (is_ex_exactly_of_type(rh,numeric)) {
733             combine_overall_coeff(lh);
734             combine_overall_coeff(rh);
735         } else {
736             combine_overall_coeff(lh);
737             seq.push_back(split_ex_to_pair(rh));
738         }
739     } else {
740         if (is_ex_exactly_of_type(rh,numeric)) {
741             combine_overall_coeff(rh);
742             seq.push_back(split_ex_to_pair(lh));
743         } else {
744             expair p1=split_ex_to_pair(lh);
745             expair p2=split_ex_to_pair(rh);
746
747             int cmpval=p1.rest.compare(p2.rest);
748             if (cmpval==0) {
749                 p1.coeff=ex_to_numeric(p1.coeff).add_dyn(ex_to_numeric(p2.coeff));
750                 if (!ex_to_numeric(p1.coeff).is_zero()) {
751                     // no further processing is necessary, since this
752                     // one element will usually be recombined in eval()
753                     seq.push_back(p1);
754                 }
755             } else {
756                 seq.reserve(2);
757                 if (cmpval<0) {
758                     seq.push_back(p1);
759                     seq.push_back(p2);
760                 } else {
761                     seq.push_back(p2);
762                     seq.push_back(p1);
763                 }
764             }
765         }
766     }
767 }
768
769 void expairseq::construct_from_2_expairseq(const expairseq & s1,
770                                            const expairseq & s2)
771 {
772     combine_overall_coeff(s1.overall_coeff);
773     combine_overall_coeff(s2.overall_coeff);
774
775     epvector::const_iterator first1=s1.seq.begin();
776     epvector::const_iterator last1=s1.seq.end();
777     epvector::const_iterator first2=s2.seq.begin();
778     epvector::const_iterator last2=s2.seq.end();
779
780     seq.reserve(s1.seq.size()+s2.seq.size());
781
782     bool needs_further_processing=false;
783     
784     while (first1!=last1 && first2!=last2) {
785         int cmpval=(*first1).rest.compare((*first2).rest);
786         if (cmpval==0) {
787             // combine terms
788             const numeric & newcoeff=ex_to_numeric((*first1).coeff).
789                                      add(ex_to_numeric((*first2).coeff));
790             if (!newcoeff.is_zero()) {
791                 seq.push_back(expair((*first1).rest,newcoeff));
792                 if (expair_needs_further_processing(seq.end()-1)) {
793                     needs_further_processing = true;
794                 }
795             }
796             ++first1;
797             ++first2;
798         } else if (cmpval<0) {
799             seq.push_back(*first1);
800             ++first1;
801         } else {
802             seq.push_back(*first2);
803             ++first2;
804         }
805     }
806     
807     while (first1!=last1) {
808         seq.push_back(*first1);
809         ++first1;
810     }
811     while (first2!=last2) {
812         seq.push_back(*first2);
813         ++first2;
814     }
815
816     if (needs_further_processing) {
817         epvector v=seq;
818         seq.clear();
819         construct_from_epvector(v);
820     }
821 }
822
823 void expairseq::construct_from_expairseq_ex(const expairseq & s,
824                                             const ex & e)
825 {
826     combine_overall_coeff(s.overall_coeff);
827     if (is_ex_exactly_of_type(e,numeric)) {
828         combine_overall_coeff(e);
829         seq=s.seq;
830         return;
831     }
832
833     epvector::const_iterator first=s.seq.begin();
834     epvector::const_iterator last=s.seq.end();
835     expair p=split_ex_to_pair(e);
836
837     seq.reserve(s.seq.size()+1);
838     bool p_pushed=0;
839
840     bool needs_further_processing=false;
841
842     // merge p into s.seq
843     while (first!=last) {
844         int cmpval=(*first).rest.compare(p.rest);
845         if (cmpval==0) {
846             // combine terms
847             const numeric & newcoeff=ex_to_numeric((*first).coeff).
848                                      add(ex_to_numeric(p.coeff));
849             if (!newcoeff.is_zero()) {
850                 seq.push_back(expair((*first).rest,newcoeff));
851                 if (expair_needs_further_processing(seq.end()-1)) {
852                     needs_further_processing = true;
853                 }
854             }
855             ++first;
856             p_pushed=1;
857             break;
858         } else if (cmpval<0) {
859             seq.push_back(*first);
860             ++first;
861         } else {
862             seq.push_back(p);
863             p_pushed=1;
864             break;
865         }
866     }
867
868     if (p_pushed) {
869         // while loop exited because p was pushed, now push rest of s.seq
870         while (first!=last) {
871             seq.push_back(*first);
872             ++first;
873         }
874     } else {
875         // while loop exited because s.seq was pushed, now push p
876         seq.push_back(p);
877     }
878
879     if (needs_further_processing) {
880         epvector v=seq;
881         seq.clear();
882         construct_from_epvector(v);
883     }
884 }
885
886 void expairseq::construct_from_exvector(const exvector & v)
887 {
888     // simplifications: +(a,+(b,c),d) -> +(a,b,c,d) (associativity)
889     //                  +(d,b,c,a) -> +(a,b,c,d) (canonicalization)
890     //                  +(...,x,*(x,c1),*(x,c2)) -> +(...,*(x,1+c1+c2)) (c1, c2 numeric())
891     //                  (same for (+,*) -> (*,^)
892
893     make_flat(v);
894 #ifdef EXPAIRSEQ_USE_HASHTAB
895     combine_same_terms();
896 #else
897     canonicalize();
898     combine_same_terms_sorted_seq();
899 #endif // def EXPAIRSEQ_USE_HASHTAB
900 }
901
902 void expairseq::construct_from_epvector(const epvector & v)
903 {
904     // simplifications: +(a,+(b,c),d) -> +(a,b,c,d) (associativity)
905     //                  +(d,b,c,a) -> +(a,b,c,d) (canonicalization)
906     //                  +(...,x,*(x,c1),*(x,c2)) -> +(...,*(x,1+c1+c2)) (c1, c2 numeric())
907     //                  (same for (+,*) -> (*,^)
908
909     make_flat(v);
910 #ifdef EXPAIRSEQ_USE_HASHTAB
911     combine_same_terms();
912 #else
913     canonicalize();
914     combine_same_terms_sorted_seq();
915 #endif // def EXPAIRSEQ_USE_HASHTAB
916 }
917
918 #include <iostream>
919
920 void expairseq::make_flat(const exvector & v)
921 {
922     exvector::const_iterator cit, citend = v.end();
923
924     // count number of operands which are of same expairseq derived type
925     // and their cumulative number of operands
926     int nexpairseqs=0;
927     int noperands=0;
928     cit=v.begin();
929     while (cit!=citend) {
930         if (cit->bp->tinfo()==tinfo()) {
931             nexpairseqs++;
932             noperands+=ex_to_expairseq(*cit).seq.size();
933         }
934         ++cit;
935     }
936
937     // reserve seq and coeffseq which will hold all operands
938     seq.reserve(v.size()+noperands-nexpairseqs);
939
940     // copy elements and split off numerical part
941     cit=v.begin();
942     while (cit!=citend) {
943         if (cit->bp->tinfo()==tinfo()) {
944             const expairseq & subseqref=ex_to_expairseq(*cit);
945             combine_overall_coeff(subseqref.overall_coeff);
946             epvector::const_iterator cit_s=subseqref.seq.begin();
947             while (cit_s!=subseqref.seq.end()) {
948                 seq.push_back(*cit_s);
949                 ++cit_s;
950             }
951         } else {
952             if (is_ex_exactly_of_type(*cit,numeric)) {
953                 combine_overall_coeff(*cit);
954             } else {
955                 seq.push_back(split_ex_to_pair(*cit));
956             }
957         }
958         ++cit;
959     }
960
961     /*
962     cout << "after make flat" << endl;
963     for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
964         (*cit).printraw(cout);
965     }
966     cout << endl;
967     */
968 }
969
970 void expairseq::make_flat(const epvector & v)
971 {
972     epvector::const_iterator cit, citend = v.end();
973
974     // count number of operands which are of same expairseq derived type
975     // and their cumulative number of operands
976     int nexpairseqs=0;
977     int noperands=0;
978
979     cit = v.begin();
980     while (cit!=citend) {
981         if (cit->rest.bp->tinfo()==tinfo()) {
982             nexpairseqs++;
983             noperands += ex_to_expairseq((*cit).rest).seq.size();
984         }
985         ++cit;
986     }
987
988     // reserve seq and coeffseq which will hold all operands
989     seq.reserve(v.size()+noperands-nexpairseqs);
990
991     // copy elements and split off numerical part
992     cit = v.begin();
993     while (cit!=citend) {
994         if ((cit->rest.bp->tinfo()==tinfo())&&can_make_flat(*cit)) {
995             const expairseq & subseqref=ex_to_expairseq((*cit).rest);
996             combine_overall_coeff(ex_to_numeric(subseqref.overall_coeff),
997                                   ex_to_numeric((*cit).coeff));
998             epvector::const_iterator cit_s=subseqref.seq.begin();
999             while (cit_s!=subseqref.seq.end()) {
1000                 seq.push_back(expair((*cit_s).rest,
1001                               ex_to_numeric((*cit_s).coeff).mul_dyn(ex_to_numeric((*cit).coeff))));
1002                 //seq.push_back(combine_pair_with_coeff_to_pair(*cit_s,
1003                 //                                              (*cit).coeff));
1004                 ++cit_s;
1005             }
1006         } else {
1007             if ((*cit).is_numeric_with_coeff_1()) {
1008                 combine_overall_coeff((*cit).rest);
1009             //if (is_ex_exactly_of_type((*cit).rest,numeric)) {
1010             //    combine_overall_coeff(recombine_pair_to_ex(*cit));
1011             } else {
1012                 seq.push_back(*cit);
1013             }
1014         }
1015         ++cit;
1016     }
1017 }
1018
1019 epvector * expairseq::bubblesort(epvector::iterator itbegin, epvector::iterator itend)
1020 {
1021     unsigned n=itend-itbegin;
1022
1023     epvector * sp=new epvector;
1024     sp->reserve(n);
1025
1026     epvector::iterator last=itend-1;
1027     for (epvector::iterator it1=itbegin; it1!=last; ++it1) {
1028         for (epvector::iterator it2=it1+1; it2!=itend; ++it2) {
1029             if ((*it2).rest.compare((*it1).rest)<0) {
1030                 iter_swap(it1,it2);
1031             }
1032         }
1033         sp->push_back(*it1);
1034     }
1035     sp->push_back(*last);
1036     return sp;
1037 }
1038
1039 epvector * expairseq::mergesort(epvector::iterator itbegin, epvector::iterator itend)
1040 {
1041     unsigned n=itend-itbegin;
1042     /*
1043     if (n==1) {
1044         epvector * sp=new epvector;
1045         sp->push_back(*itbegin);
1046         return sp;
1047     }
1048     */
1049     if (n<16) return bubblesort(itbegin, itend);
1050     unsigned m=n/2;
1051     
1052     epvector * s1p=mergesort(itbegin, itbegin+m);
1053     epvector * s2p=mergesort(itbegin+m, itend);
1054
1055     epvector * sp=new epvector;
1056     sp->reserve(s1p->size()+s2p->size());
1057
1058     epvector::iterator first1=s1p->begin();
1059     epvector::iterator last1=s1p->end();
1060
1061     epvector::iterator first2=s2p->begin();
1062     epvector::iterator last2=s2p->end();
1063     
1064     while (first1 != last1 && first2 != last2) {
1065         if ((*first1).rest.compare((*first2).rest)<0) {
1066             sp->push_back(*first1);
1067             ++first1;
1068         } else {
1069             sp->push_back(*first2);
1070             ++first2;
1071         }
1072     }
1073
1074     if (first1 != last1) {
1075         while (first1 != last1) {
1076             sp->push_back(*first1);
1077             ++first1;
1078         }
1079     } else {
1080         while (first2 != last2) {
1081             sp->push_back(*first2);
1082             ++first2;
1083         }
1084     }
1085
1086     delete s1p;
1087     delete s2p;
1088     
1089     return sp;
1090 }
1091             
1092
1093 void expairseq::canonicalize(void)
1094 {
1095     // canonicalize
1096     sort(seq.begin(),seq.end(),expair_is_less());
1097     /*
1098     sort(seq.begin(),seq.end(),expair_is_less_old());
1099     if (seq.size()>1) {
1100         if (is_ex_exactly_of_type((*(seq.begin())).rest,numeric)) {
1101             sort(seq.begin(),seq.end(),expair_is_less());
1102         } else {
1103             epvector::iterator last_numeric=seq.end();
1104             do {
1105                 last_numeric--;
1106             } while (is_ex_exactly_of_type((*last_numeric).rest,numeric));
1107             last_numeric++;
1108             sort(last_numeric,seq.end(),expair_is_less());
1109         }
1110     }
1111     */
1112     
1113     /*
1114     epvector * sorted_seqp=mergesort(seq.begin(),seq.end());
1115     epvector::iterator last=sorted_seqp->end();
1116     epvector::iterator it2=seq.begin();
1117     for (epvector::iterator it1=sorted_seqp->begin(); it1!=last; ++it1, ++it2) {
1118         iter_swap(it1,it2);
1119     }
1120     delete sorted_seqp;
1121     */
1122
1123     /*
1124     cout << "after canonicalize" << endl;
1125     for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
1126         (*cit).printraw(cout);
1127     }
1128     cout << endl;
1129     cout.flush();
1130     */
1131 }
1132
1133 void expairseq::combine_same_terms_sorted_seq(void)
1134 {
1135     bool needs_further_processing=false;
1136     
1137     // combine same terms, drop term with coeff 0
1138     if (seq.size()>1) {
1139         epvector::iterator itin1=seq.begin();
1140         epvector::iterator itin2=itin1+1;
1141         epvector::iterator itout=itin1;
1142         epvector::iterator last=seq.end();
1143         // must_copy will be set to true the first time some combination is possible
1144         // from then on the sequence has changed and must be compacted
1145         bool must_copy=false;
1146         while (itin2!=last) {
1147             if ((*itin1).rest.compare((*itin2).rest)==0) {
1148                 (*itin1).coeff=ex_to_numeric((*itin1).coeff).
1149                                add_dyn(ex_to_numeric((*itin2).coeff));
1150                 if (expair_needs_further_processing(itin1)) {
1151                     needs_further_processing = true;
1152                 }
1153                 must_copy=true;
1154             } else {
1155                 if (!ex_to_numeric((*itin1).coeff).is_zero()) {
1156                     if (must_copy) {
1157                         *itout=*itin1;
1158                     }
1159                     ++itout;
1160                 }
1161                 itin1=itin2;
1162             }
1163             ++itin2;
1164         }
1165         if (!ex_to_numeric((*itin1).coeff).is_zero()) {
1166             if (must_copy) {
1167                 *itout=*itin1;
1168             }
1169             ++itout;
1170         }
1171         if (itout!=last) {
1172             seq.erase(itout,last);
1173         }
1174     }
1175
1176     /*
1177     cout << "after combine" << endl;
1178     for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
1179         (*cit).printraw(cout);
1180     }
1181     cout << endl;
1182     cout.flush();
1183     */
1184     
1185     if (needs_further_processing) {
1186         epvector v=seq;
1187         seq.clear();
1188         construct_from_epvector(v);
1189     }
1190 }
1191
1192 #ifdef EXPAIRSEQ_USE_HASHTAB
1193
1194 unsigned expairseq::calc_hashtabsize(unsigned sz) const
1195 {
1196     unsigned size;
1197     unsigned nearest_power_of_2 = 1 << log2(sz);
1198     //    if (nearest_power_of_2 < maxhashtabsize/hashtabfactor) {
1199     //  size=nearest_power_of_2*hashtabfactor;
1200     size=nearest_power_of_2/hashtabfactor;
1201     if (size<minhashtabsize) return 0;
1202     GINAC_ASSERT(hashtabsize<=0x8000000U); // really max size due to 31 bit hashing
1203     // hashtabsize must be a power of 2
1204     GINAC_ASSERT((1U << log2(size))==size);
1205     return size;
1206 }
1207
1208 unsigned expairseq::calc_hashindex(const ex & e) const
1209 {
1210     // calculate hashindex
1211     unsigned hash=e.gethash();
1212     unsigned hashindex;
1213     if (is_a_numeric_hash(hash)) {
1214         hashindex=hashmask;
1215     } else {
1216         hashindex=hash & hashmask;
1217         // last hashtab entry is reserved for numerics
1218         if (hashindex==hashmask) hashindex=0;
1219     }
1220     GINAC_ASSERT(hashindex>=0);
1221     GINAC_ASSERT((hashindex<hashtabsize)||(hashtabsize==0));
1222     return hashindex;
1223 }
1224
1225 void expairseq::shrink_hashtab(void)
1226 {
1227     unsigned new_hashtabsize;
1228     while (hashtabsize!=(new_hashtabsize=calc_hashtabsize(seq.size()))) {
1229         GINAC_ASSERT(new_hashtabsize<hashtabsize);
1230         if (new_hashtabsize==0) {
1231             hashtab.clear();
1232             hashtabsize=0;
1233             canonicalize();
1234             return;
1235         }
1236         
1237         // shrink by a factor of 2
1238         unsigned half_hashtabsize=hashtabsize/2;
1239         for (unsigned i=0; i<half_hashtabsize-1; ++i) {
1240             hashtab[i].merge(hashtab[i+half_hashtabsize],epp_is_less());
1241         }
1242         // special treatment for numeric hashes
1243         hashtab[0].merge(hashtab[half_hashtabsize-1],epp_is_less());
1244         hashtab[half_hashtabsize-1]=hashtab[hashtabsize-1];
1245         hashtab.resize(half_hashtabsize);
1246         hashtabsize=half_hashtabsize;
1247         hashmask=hashtabsize-1;
1248     }
1249 }
1250
1251 void expairseq::remove_hashtab_entry(epvector::const_iterator element)
1252 {
1253     if (hashtabsize==0) return; // nothing to do
1254     
1255     // calculate hashindex of element to be deleted
1256     unsigned hashindex=calc_hashindex((*element).rest);
1257
1258     // find it in hashtab and remove it
1259     epplist & eppl=hashtab[hashindex];
1260     epplist::iterator epplit=eppl.begin();
1261     bool erased=false;
1262     while (epplit!=eppl.end()) {
1263         if (*epplit == element) {
1264             eppl.erase(epplit);
1265             erased=true;
1266             break;
1267         }
1268         ++epplit;
1269     }
1270     if (!erased) {
1271         printtree(cout,0);
1272         cout << "tried to erase " << element-seq.begin() << endl;
1273         cout << "size " << seq.end()-seq.begin() << endl;
1274
1275         unsigned hashindex=calc_hashindex((*element).rest);
1276         epplist & eppl=hashtab[hashindex];
1277         epplist::iterator epplit=eppl.begin();
1278         bool erased=false;
1279         while (epplit!=eppl.end()) {
1280             if (*epplit == element) {
1281                 eppl.erase(epplit);
1282                 erased=true;
1283                 break;
1284             }
1285             ++epplit;
1286         }
1287         GINAC_ASSERT(erased);
1288     }
1289     GINAC_ASSERT(erased);
1290 }
1291
1292 void expairseq::move_hashtab_entry(epvector::const_iterator oldpos,
1293                                    epvector::iterator newpos)
1294 {
1295     GINAC_ASSERT(hashtabsize!=0);
1296     
1297     // calculate hashindex of element which was moved
1298     unsigned hashindex=calc_hashindex((*newpos).rest);
1299
1300     // find it in hashtab and modify it
1301     epplist & eppl=hashtab[hashindex];
1302     epplist::iterator epplit=eppl.begin();
1303     while (epplit!=eppl.end()) {
1304         if (*epplit == oldpos) {
1305             *epplit=newpos;
1306             break;
1307         }
1308         ++epplit;
1309     }
1310     GINAC_ASSERT(epplit!=eppl.end());
1311 }
1312
1313 void expairseq::sorted_insert(epplist & eppl, epp elem)
1314 {
1315     epplist::iterator current=eppl.begin();
1316     while ((current!=eppl.end())&&((*(*current)).is_less(*elem))) {
1317         ++current;
1318     }
1319     eppl.insert(current,elem);
1320 }    
1321
1322 void expairseq::build_hashtab_and_combine(epvector::iterator & first_numeric,
1323                                           epvector::iterator & last_non_zero,
1324                                           vector<bool> & touched,
1325                                           unsigned & number_of_zeroes)
1326 {
1327     epp current=seq.begin();
1328
1329     while (current!=first_numeric) {
1330         if (is_ex_exactly_of_type((*current).rest,numeric)) {
1331             --first_numeric;
1332             iter_swap(current,first_numeric);
1333         } else {
1334             // calculate hashindex
1335             unsigned currenthashindex=calc_hashindex((*current).rest);
1336
1337             // test if there is already a matching expair in the hashtab-list
1338             epplist & eppl=hashtab[currenthashindex];
1339             epplist::iterator epplit=eppl.begin();
1340             while (epplit!=eppl.end()) {
1341                 if ((*current).rest.is_equal((*(*epplit)).rest)) break;
1342                 ++epplit;
1343             }
1344             if (epplit==eppl.end()) {
1345                 // no matching expair found, append this to end of list
1346                 sorted_insert(eppl,current);
1347                 ++current;
1348             } else {
1349                 // epplit points to a matching expair, combine it with current
1350                 (*(*epplit)).coeff=ex_to_numeric((*(*epplit)).coeff).
1351                                    add_dyn(ex_to_numeric((*current).coeff));
1352                 
1353                 // move obsolete current expair to end by swapping with last_non_zero element
1354                 // if this was a numeric, it is swapped with the expair before first_numeric 
1355                 iter_swap(current,last_non_zero);
1356                 --first_numeric;
1357                 if (first_numeric!=last_non_zero) iter_swap(first_numeric,current);
1358                 --last_non_zero;
1359                 ++number_of_zeroes;
1360                 // test if combined term has coeff 0 and can be removed is done later
1361                 touched[(*epplit)-seq.begin()]=true;
1362             }
1363         }
1364     }
1365 }    
1366
1367 void expairseq::drop_coeff_0_terms(epvector::iterator & first_numeric,
1368                                    epvector::iterator & last_non_zero,
1369                                    vector<bool> & touched,
1370                                    unsigned & number_of_zeroes)
1371 {
1372     // move terms with coeff 0 to end and remove them from hashtab
1373     // check only those elements which have been touched
1374     epp current=seq.begin();
1375     unsigned i=0;
1376     while (current!=first_numeric) {
1377         if (!touched[i]) {
1378             ++current;
1379             ++i;
1380         } else if (!ex_to_numeric((*current).coeff).is_equal(_num0())) {
1381             ++current;
1382             ++i;
1383         } else {
1384             remove_hashtab_entry(current);
1385
1386             // move element to the end, unless it is already at the end
1387             if (current!=last_non_zero) {
1388                 iter_swap(current,last_non_zero);
1389                 --first_numeric;
1390                 bool numeric_swapped=first_numeric!=last_non_zero;
1391                 if (numeric_swapped) iter_swap(first_numeric,current);
1392                 epvector::iterator changed_entry;
1393
1394                 if (numeric_swapped) {
1395                     changed_entry=first_numeric;
1396                 } else {
1397                     changed_entry=last_non_zero;
1398                 }
1399
1400                 --last_non_zero;
1401                 ++number_of_zeroes;
1402
1403                 if (first_numeric!=current) {
1404                 
1405                     // change entry in hashtab which referred to first_numeric or last_non_zero to current
1406                     move_hashtab_entry(changed_entry,current);
1407                     touched[current-seq.begin()]=touched[changed_entry-seq.begin()];
1408                 }
1409             } else {
1410                 --first_numeric;
1411                 --last_non_zero;
1412                 ++number_of_zeroes;
1413             }
1414         }
1415     }
1416     GINAC_ASSERT(i==current-seq.begin());
1417 }
1418
1419 bool expairseq::has_coeff_0(void) const
1420 {
1421     for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
1422         if ((*cit).coeff.is_equal(_ex0())) {
1423             return true;
1424         }
1425     }
1426     return false;
1427 }
1428
1429 void expairseq::add_numerics_to_hashtab(epvector::iterator first_numeric,
1430                                         epvector::const_iterator last_non_zero)
1431 {
1432     if (first_numeric==seq.end()) return; // no numerics    
1433
1434     epvector::iterator current=first_numeric;
1435     epvector::const_iterator last=last_non_zero+1;
1436     while (current!=last) {
1437         sorted_insert(hashtab[hashmask],current);
1438         ++current;
1439     }
1440 }
1441
1442 void expairseq::combine_same_terms(void)
1443 {
1444     // combine same terms, drop term with coeff 0, move numerics to end
1445     
1446     // calculate size of hashtab
1447     hashtabsize=calc_hashtabsize(seq.size());
1448
1449     // hashtabsize is a power of 2
1450     hashmask=hashtabsize-1;
1451
1452     // allocate hashtab
1453     hashtab.clear();
1454     hashtab.resize(hashtabsize);
1455
1456     if (hashtabsize==0) {
1457         canonicalize();
1458         combine_same_terms_sorted_seq();
1459         GINAC_ASSERT(!has_coeff_0());
1460         return;
1461     }
1462
1463     // iterate through seq, move numerics to end,
1464     // fill hashtab and combine same terms
1465     epvector::iterator first_numeric=seq.end();
1466     epvector::iterator last_non_zero=seq.end()-1;
1467
1468     vector<bool> touched;
1469     touched.reserve(seq.size());
1470     for (unsigned i=0; i<seq.size(); ++i) touched[i]=false;
1471
1472     unsigned number_of_zeroes=0;
1473
1474     GINAC_ASSERT(!has_coeff_0());
1475     build_hashtab_and_combine(first_numeric,last_non_zero,touched,number_of_zeroes);
1476     /*
1477     cout << "in combine:" << endl;
1478     printtree(cout,0);
1479     cout << "size=" << seq.end() - seq.begin() << endl;
1480     cout << "first_numeric=" << first_numeric - seq.begin() << endl;
1481     cout << "last_non_zero=" << last_non_zero - seq.begin() << endl;
1482     for (unsigned i=0; i<seq.size(); ++i) {
1483         if (touched[i]) cout << i << " is touched" << endl;
1484     }
1485     cout << "end in combine" << endl;
1486     */
1487     
1488     // there should not be any terms with coeff 0 from the beginning,
1489     // so it should be safe to skip this step
1490     if (number_of_zeroes!=0) {
1491         drop_coeff_0_terms(first_numeric,last_non_zero,touched,number_of_zeroes);
1492         /*
1493         cout << "in combine after drop:" << endl;
1494         printtree(cout,0);
1495         cout << "size=" << seq.end() - seq.begin() << endl;
1496         cout << "first_numeric=" << first_numeric - seq.begin() << endl;
1497         cout << "last_non_zero=" << last_non_zero - seq.begin() << endl;
1498         for (unsigned i=0; i<seq.size(); ++i) {
1499             if (touched[i]) cout << i << " is touched" << endl;
1500         }
1501         cout << "end in combine after drop" << endl;
1502         */
1503     }
1504
1505     add_numerics_to_hashtab(first_numeric,last_non_zero);
1506
1507     // pop zero elements
1508     for (unsigned i=0; i<number_of_zeroes; ++i) {
1509         seq.pop_back();
1510     }
1511
1512     // shrink hashtabsize to calculated value
1513     GINAC_ASSERT(!has_coeff_0());
1514
1515     shrink_hashtab();
1516
1517     GINAC_ASSERT(!has_coeff_0());
1518 }
1519
1520 #endif // def EXPAIRSEQ_USE_HASHTAB
1521
1522 bool expairseq::is_canonical() const
1523 {
1524     if (seq.size()<=1) return 1;
1525
1526 #ifdef EXPAIRSEQ_USE_HASHTAB
1527     if (hashtabsize>0) return 1; // not canoncalized
1528 #endif // def EXPAIRSEQ_USE_HASHTAB
1529     
1530     epvector::const_iterator it = seq.begin();
1531     epvector::const_iterator it_last = it;
1532     for (++it; it!=seq.end(); it_last=it, ++it) {
1533         if (!((*it_last).is_less(*it)||(*it_last).is_equal(*it))) {
1534             if (!is_ex_exactly_of_type((*it_last).rest,numeric)||
1535                 !is_ex_exactly_of_type((*it).rest,numeric)) {
1536                 // double test makes it easier to set a breakpoint...
1537                 if (!is_ex_exactly_of_type((*it_last).rest,numeric)||
1538                     !is_ex_exactly_of_type((*it).rest,numeric)) {
1539                     printpair(cout,*it_last,0);
1540                     cout << ">";
1541                     printpair(cout,*it,0);
1542                     cout << "\n";
1543                     cout << "pair1:" << endl;
1544                     (*it_last).rest.printtree(cout);
1545                     (*it_last).coeff.printtree(cout);
1546                     cout << "pair2:" << endl;
1547                     (*it).rest.printtree(cout);
1548                     (*it).coeff.printtree(cout);
1549                     return 0;
1550                 }
1551             }
1552         }
1553     }
1554     return 1;
1555 }
1556
1557 epvector * expairseq::expandchildren(unsigned options) const
1558 {
1559     epvector::const_iterator last = seq.end();
1560     epvector::const_iterator cit = seq.begin();
1561     while (cit!=last) {
1562         const ex & expanded_ex=(*cit).rest.expand(options);
1563         if (!are_ex_trivially_equal((*cit).rest,expanded_ex)) {
1564
1565             // something changed, copy seq, eval and return it
1566             epvector *s=new epvector;
1567             s->reserve(seq.size());
1568
1569             // copy parts of seq which are known not to have changed
1570             epvector::const_iterator cit2 = seq.begin();
1571             while (cit2!=cit) {
1572                 s->push_back(*cit2);
1573                 ++cit2;
1574             }
1575             // copy first changed element
1576             s->push_back(combine_ex_with_coeff_to_pair(expanded_ex,
1577                                                        (*cit2).coeff));
1578             ++cit2;
1579             // copy rest
1580             while (cit2!=last) {
1581                 s->push_back(combine_ex_with_coeff_to_pair((*cit2).rest.expand(options),
1582                                                            (*cit2).coeff));
1583                 ++cit2;
1584             }
1585             return s;
1586         }
1587         ++cit;
1588     }
1589     
1590     return 0; // nothing has changed
1591 }
1592    
1593 epvector * expairseq::evalchildren(int level) const
1594 {
1595     // returns a NULL pointer if nothing had to be evaluated
1596     // returns a pointer to a newly created epvector otherwise
1597     // (which has to be deleted somewhere else)
1598
1599     if (level==1) {
1600         return 0;
1601     }
1602     if (level == -max_recursion_level) {
1603         throw(std::runtime_error("max recursion level reached"));
1604     }
1605
1606     --level;
1607     epvector::const_iterator last=seq.end();
1608     epvector::const_iterator cit=seq.begin();
1609     while (cit!=last) {
1610         const ex & evaled_ex=(*cit).rest.eval(level);
1611         if (!are_ex_trivially_equal((*cit).rest,evaled_ex)) {
1612
1613             // something changed, copy seq, eval and return it
1614             epvector *s = new epvector;
1615             s->reserve(seq.size());
1616
1617             // copy parts of seq which are known not to have changed
1618             epvector::const_iterator cit2=seq.begin();
1619             while (cit2!=cit) {
1620                 s->push_back(*cit2);
1621                 ++cit2;
1622             }
1623             // copy first changed element
1624             s->push_back(combine_ex_with_coeff_to_pair(evaled_ex,
1625                                                        (*cit2).coeff));
1626             ++cit2;
1627             // copy rest
1628             while (cit2!=last) {
1629                 s->push_back(combine_ex_with_coeff_to_pair((*cit2).rest.eval(level),
1630                                                            (*cit2).coeff));
1631                 ++cit2;
1632             }
1633             return s;
1634         }
1635         ++cit;
1636     }
1637     
1638     return 0; // nothing has changed
1639 }
1640
1641 epvector expairseq::evalfchildren(int level) const
1642 {
1643     if (level==1)
1644         return seq;
1645
1646     if (level==-max_recursion_level)
1647         throw(std::runtime_error("max recursion level reached"));
1648     
1649     epvector s;
1650     s.reserve(seq.size());
1651     
1652     --level;
1653     for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
1654         s.push_back(combine_ex_with_coeff_to_pair((*it).rest.evalf(level),
1655                                                   (*it).coeff.evalf(level)));
1656     }
1657     return s;
1658 }
1659
1660 epvector expairseq::normalchildren(int level) const
1661 {
1662     if (level==1)
1663         return seq;
1664     
1665     if (level == -max_recursion_level)
1666         throw(std::runtime_error("max recursion level reached"));
1667
1668     epvector s;
1669     s.reserve(seq.size());
1670
1671     --level;
1672     for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
1673         s.push_back(combine_ex_with_coeff_to_pair((*it).rest.normal(level),
1674                                                   (*it).coeff));
1675     }
1676     return s;
1677 }
1678
1679 epvector expairseq::diffchildren(const symbol & y) const
1680 {
1681     epvector s;
1682     s.reserve(seq.size());
1683
1684     for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
1685         s.push_back(combine_ex_with_coeff_to_pair((*it).rest.diff(y),
1686                                                   (*it).coeff));
1687     }
1688     return s;
1689 }
1690
1691 epvector * expairseq::subschildren(const lst & ls, const lst & lr) const
1692 {
1693     // returns a NULL pointer if nothing had to be substituted
1694     // returns a pointer to a newly created epvector otherwise
1695     // (which has to be deleted somewhere else)
1696     GINAC_ASSERT(ls.nops()==lr.nops());
1697     
1698     epvector::const_iterator last=seq.end();
1699     epvector::const_iterator cit=seq.begin();
1700     while (cit!=last) {
1701         const ex & subsed_ex=(*cit).rest.subs(ls,lr);
1702         if (!are_ex_trivially_equal((*cit).rest,subsed_ex)) {
1703             
1704             // something changed, copy seq, subs and return it
1705             epvector *s=new epvector;
1706             s->reserve(seq.size());
1707             
1708             // copy parts of seq which are known not to have changed
1709             epvector::const_iterator cit2=seq.begin();
1710             while (cit2!=cit) {
1711                 s->push_back(*cit2);
1712                 ++cit2;
1713             }
1714             // copy first changed element
1715             s->push_back(combine_ex_with_coeff_to_pair(subsed_ex,
1716                                                        (*cit2).coeff));
1717             ++cit2;
1718             // copy rest
1719             while (cit2!=last) {
1720                 s->push_back(combine_ex_with_coeff_to_pair((*cit2).rest.subs(ls,lr),
1721                                                            (*cit2).coeff));
1722                 ++cit2;
1723             }
1724             return s;
1725         }
1726         ++cit;
1727     }
1728     
1729     return 0; // nothing has changed
1730 }
1731
1732 //////////
1733 // static member variables
1734 //////////
1735
1736 // protected
1737
1738 unsigned expairseq::precedence=10;
1739
1740 #ifdef EXPAIRSEQ_USE_HASHTAB
1741 unsigned expairseq::maxhashtabsize=0x4000000U;
1742 unsigned expairseq::minhashtabsize=0x1000U;
1743 unsigned expairseq::hashtabfactor=1;
1744 #endif // def EXPAIRSEQ_USE_HASHTAB
1745
1746 //////////
1747 // global constants
1748 //////////
1749
1750 const expairseq some_expairseq;
1751 const type_info & typeid_expairseq=typeid(some_expairseq);
1752
1753 #ifndef NO_NAMESPACE_GINAC
1754 } // namespace GiNaC
1755 #endif // ndef NO_NAMESPACE_GINAC