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