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