1 /** @file expairseq.cpp
3 * Implementation of sequences of expression pairs. */
6 * GiNaC Copyright (C) 1999-2001 Johannes Gutenberg University Mainz, Germany
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.
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.
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
27 #include "expairseq.h"
29 #include "relational.h"
35 #if EXPAIRSEQ_USE_HASHTAB
37 #endif // EXPAIRSEQ_USE_HASHTAB
41 GINAC_IMPLEMENT_REGISTERED_CLASS_NO_CTORS(expairseq, basic)
50 bool operator()(const epp &lh, const epp &rh) const
52 return (*lh).is_less(*rh);
57 // default ctor, dtor, copy ctor assignment operator and helpers
62 expairseq::expairseq(const expairseq &other)
64 debugmsg("expairseq copy ctor",LOGLEVEL_CONSTRUCT);
68 const expairseq &expairseq::operator=(const expairseq &other)
70 debugmsg("expairseq operator=",LOGLEVEL_ASSIGNMENT);
80 /** For use by copy ctor and assignment operator. */
81 void expairseq::copy(const expairseq &other)
83 inherited::copy(other);
85 overall_coeff = other.overall_coeff;
86 #if EXPAIRSEQ_USE_HASHTAB
88 hashtabsize = other.hashtabsize;
90 hashmask = other.hashmask;
91 hashtab.resize(hashtabsize);
92 epvector::const_iterator osb = other.seq.begin();
93 for (unsigned i=0; i<hashtabsize; ++i) {
95 for (epplist::const_iterator cit=other.hashtab[i].begin();
96 cit!=other.hashtab[i].end(); ++cit) {
97 hashtab[i].push_back(seq.begin()+((*cit)-osb));
103 #endif // EXPAIRSEQ_USE_HASHTAB
106 DEFAULT_DESTROY(expairseq)
112 expairseq::expairseq(const ex &lh, const ex &rh) : inherited(TINFO_expairseq)
114 debugmsg("expairseq ctor from ex,ex",LOGLEVEL_CONSTRUCT);
115 construct_from_2_ex(lh,rh);
116 GINAC_ASSERT(is_canonical());
119 expairseq::expairseq(const exvector &v) : inherited(TINFO_expairseq)
121 debugmsg("expairseq ctor from exvector",LOGLEVEL_CONSTRUCT);
122 construct_from_exvector(v);
123 GINAC_ASSERT(is_canonical());
126 expairseq::expairseq(const epvector &v, const ex &oc)
127 : inherited(TINFO_expairseq), overall_coeff(oc)
129 debugmsg("expairseq ctor from epvector,ex",LOGLEVEL_CONSTRUCT);
130 construct_from_epvector(v);
131 GINAC_ASSERT(is_canonical());
134 expairseq::expairseq(epvector *vp, const ex &oc)
135 : inherited(TINFO_expairseq), overall_coeff(oc)
137 debugmsg("expairseq ctor from epvector *,ex",LOGLEVEL_CONSTRUCT);
139 construct_from_epvector(*vp);
141 GINAC_ASSERT(is_canonical());
148 expairseq::expairseq(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
149 #if EXPAIRSEQ_USE_HASHTAB
153 debugmsg("expairseq ctor from archive_node", LOGLEVEL_CONSTRUCT);
154 for (unsigned int i=0; true; i++) {
157 if (n.find_ex("rest", rest, sym_lst, i) && n.find_ex("coeff", coeff, sym_lst, i))
158 seq.push_back(expair(rest, coeff));
162 n.find_ex("overall_coeff", overall_coeff, sym_lst);
165 void expairseq::archive(archive_node &n) const
167 inherited::archive(n);
168 epvector::const_iterator i = seq.begin(), iend = seq.end();
170 n.add_ex("rest", i->rest);
171 n.add_ex("coeff", i->coeff);
174 n.add_ex("overall_coeff", overall_coeff);
177 DEFAULT_UNARCHIVE(expairseq)
180 // functions overriding virtual functions from bases classes
185 basic *expairseq::duplicate() const
187 debugmsg("expairseq duplicate",LOGLEVEL_DUPLICATE);
188 return new expairseq(*this);
191 void expairseq::print(const print_context & c, unsigned level) const
193 debugmsg("expairseq print",LOGLEVEL_PRINT);
195 if (is_of_type(c, print_tree)) {
197 unsigned delta_indent = static_cast<const print_tree &>(c).delta_indent;
199 c.s << std::string(level, ' ') << class_name()
200 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
201 << ", nops=" << nops()
203 for (unsigned i=0; i<seq.size(); ++i) {
204 seq[i].rest.print(c, level + delta_indent);
205 seq[i].coeff.print(c, level + delta_indent);
206 if (i != seq.size()-1)
207 c.s << std::string(level + delta_indent, ' ') << "-----" << std::endl;
209 if (!overall_coeff.is_equal(default_overall_coeff())) {
210 c.s << std::string(level + delta_indent, ' ') << "-----" << std::endl
211 << std::string(level + delta_indent, ' ') << "overall_coeff" << std::endl;
212 overall_coeff.print(c, level + delta_indent);
214 c.s << std::string(level + delta_indent,' ') << "=====" << std::endl;
215 #if EXPAIRSEQ_USE_HASHTAB
216 c.s << std::string(level + delta_indent,' ')
217 << "hashtab size " << hashtabsize << std::endl;
218 if (hashtabsize == 0) return;
220 unsigned count[MAXCOUNT+1];
221 for (int i=0; i<MAXCOUNT+1; ++i)
223 unsigned this_bin_fill;
224 unsigned cum_fill_sq = 0;
225 unsigned cum_fill = 0;
226 for (unsigned i=0; i<hashtabsize; ++i) {
228 if (hashtab[i].size() > 0) {
229 c.s << std::string(level + delta_indent, ' ')
230 << "bin " << i << " with entries ";
231 for (epplist::const_iterator it=hashtab[i].begin();
232 it!=hashtab[i].end(); ++it) {
233 c.s << *it-seq.begin() << " ";
237 cum_fill += this_bin_fill;
238 cum_fill_sq += this_bin_fill*this_bin_fill;
240 if (this_bin_fill<MAXCOUNT)
241 ++count[this_bin_fill];
247 double lambda = (1.0*seq.size()) / hashtabsize;
248 for (int k=0; k<MAXCOUNT; ++k) {
251 double prob = std::pow(lambda,k)/fact * std::exp(-lambda);
253 c.s << std::string(level + delta_indent, ' ') << "bins with " << k << " entries: "
254 << int(1000.0*count[k]/hashtabsize)/10.0 << "% (expected: "
255 << int(prob*1000)/10.0 << ")" << std::endl;
257 c.s << std::string(level + delta_indent, ' ') << "bins with more entries: "
258 << int(1000.0*count[MAXCOUNT]/hashtabsize)/10.0 << "% (expected: "
259 << int((1-cum_prob)*1000)/10.0 << ")" << std::endl;
261 c.s << std::string(level + delta_indent, ' ') << "variance: "
262 << 1.0/hashtabsize*cum_fill_sq-(1.0/hashtabsize*cum_fill)*(1.0/hashtabsize*cum_fill)
264 c.s << std::string(level + delta_indent, ' ') << "average fill: "
265 << (1.0*cum_fill)/hashtabsize
266 << " (should be equal to " << (1.0*seq.size())/hashtabsize << ")" << std::endl;
267 #endif // EXPAIRSEQ_USE_HASHTAB
271 printseq(c, ',', precedence(), level);
276 bool expairseq::info(unsigned inf) const
278 return inherited::info(inf);
281 unsigned expairseq::nops() const
283 if (overall_coeff.is_equal(default_overall_coeff()))
289 ex expairseq::op(int i) const
291 if (unsigned(i)<seq.size())
292 return recombine_pair_to_ex(seq[i]);
293 GINAC_ASSERT(!overall_coeff.is_equal(default_overall_coeff()));
294 return overall_coeff;
297 ex &expairseq::let_op(int i)
299 throw(std::logic_error("let_op not defined for expairseq and derived classes (add,mul,...)"));
302 ex expairseq::map(map_function & f) const
304 epvector *v = new epvector;
305 v->reserve(seq.size());
307 epvector::const_iterator cit = seq.begin(), last = seq.end();
308 while (cit != last) {
309 v->push_back(split_ex_to_pair(f(recombine_pair_to_ex(*cit))));
313 return thisexpairseq(v, f(overall_coeff));
316 ex expairseq::eval(int level) const
318 if ((level==1) && (flags &status_flags::evaluated))
321 epvector *vp = evalchildren(level);
325 return (new expairseq(vp,overall_coeff))->setflag(status_flags::dynallocated | status_flags::evaluated);
328 ex expairseq::evalf(int level) const
330 return thisexpairseq(evalfchildren(level),overall_coeff.evalf(level-1));
333 ex expairseq::normal(lst &sym_lst, lst &repl_lst, int level) const
335 ex n = thisexpairseq(normalchildren(level),overall_coeff);
336 return n.bp->basic::normal(sym_lst,repl_lst,level);
339 bool expairseq::match(const ex & pattern, lst & repl_lst) const
341 // This differs from basic::match() because we want "a+b+c+d" to
342 // match "d+*+b" with "*" being "a+c", and we want to honor commutativity
344 if (tinfo() == pattern.bp->tinfo()) {
346 // Check whether global wildcard (one that matches the "rest of the
347 // expression", like "*" above) is present
348 bool has_global_wildcard = false;
350 for (unsigned int i=0; i<pattern.nops(); i++) {
351 if (is_ex_exactly_of_type(pattern.op(i), wildcard)) {
352 has_global_wildcard = true;
353 global_wildcard = pattern.op(i);
358 // Unfortunately, this is an O(N^2) operation because we can't
359 // sort the pattern in a useful way...
364 for (unsigned i=0; i<nops(); i++)
365 ops.push_back(op(i));
367 // Now, for every term of the pattern, look for a matching term in
368 // the expression and remove the match
369 for (unsigned i=0; i<pattern.nops(); i++) {
370 ex p = pattern.op(i);
371 if (has_global_wildcard && p.is_equal(global_wildcard))
373 exvector::iterator it = ops.begin(), itend = ops.end();
374 while (it != itend) {
375 if (it->match(p, repl_lst)) {
381 return false; // no match found
385 if (has_global_wildcard) {
387 // Assign all the remaining terms to the global wildcard (unless
388 // it has already been matched before, in which case the matches
390 epvector *vp = new epvector();
391 vp->reserve(ops.size());
392 for (unsigned i=0; i<ops.size(); i++)
393 vp->push_back(split_ex_to_pair(ops[i]));
394 ex rest = thisexpairseq(vp, default_overall_coeff());
395 for (unsigned i=0; i<repl_lst.nops(); i++) {
396 if (repl_lst.op(i).op(0).is_equal(global_wildcard))
397 return rest.is_equal(*repl_lst.op(i).op(1).bp);
399 repl_lst.append(global_wildcard == rest);
404 // No global wildcard, then the match fails if there are any
405 // unmatched terms left
409 return inherited::match(pattern, repl_lst);
412 ex expairseq::subs(const lst &ls, const lst &lr, bool no_pattern) const
414 epvector *vp = subschildren(ls, lr, no_pattern);
416 return thisexpairseq(vp, overall_coeff).bp->basic::subs(ls, lr, no_pattern);
418 return basic::subs(ls, lr, no_pattern);
423 /** Implementation of ex::diff() for an expairseq.
424 * It differentiates all elements of the sequence.
426 ex expairseq::derivative(const symbol &s) const
428 return thisexpairseq(diffchildren(s),overall_coeff);
431 int expairseq::compare_same_type(const basic &other) const
433 GINAC_ASSERT(is_of_type(other, expairseq));
434 const expairseq &o = static_cast<const expairseq &>(const_cast<basic &>(other));
438 // compare number of elements
439 if (seq.size() != o.seq.size())
440 return (seq.size()<o.seq.size()) ? -1 : 1;
442 // compare overall_coeff
443 cmpval = overall_coeff.compare(o.overall_coeff);
447 #if EXPAIRSEQ_USE_HASHTAB
448 GINAC_ASSERT(hashtabsize==o.hashtabsize);
449 if (hashtabsize==0) {
450 #endif // EXPAIRSEQ_USE_HASHTAB
451 epvector::const_iterator cit1 = seq.begin();
452 epvector::const_iterator cit2 = o.seq.begin();
453 epvector::const_iterator last1 = seq.end();
454 epvector::const_iterator last2 = o.seq.end();
456 for (; (cit1!=last1)&&(cit2!=last2); ++cit1, ++cit2) {
457 cmpval = (*cit1).compare(*cit2);
458 if (cmpval!=0) return cmpval;
461 GINAC_ASSERT(cit1==last1);
462 GINAC_ASSERT(cit2==last2);
465 #if EXPAIRSEQ_USE_HASHTAB
468 // compare number of elements in each hashtab entry
469 for (unsigned i=0; i<hashtabsize; ++i) {
470 unsigned cursize=hashtab[i].size();
471 if (cursize != o.hashtab[i].size())
472 return (cursize < o.hashtab[i].size()) ? -1 : 1;
475 // compare individual (sorted) hashtab entries
476 for (unsigned i=0; i<hashtabsize; ++i) {
477 unsigned sz = hashtab[i].size();
479 const epplist &eppl1 = hashtab[i];
480 const epplist &eppl2 = o.hashtab[i];
481 epplist::const_iterator it1 = eppl1.begin();
482 epplist::const_iterator it2 = eppl2.begin();
483 while (it1!=eppl1.end()) {
484 cmpval = (*(*it1)).compare(*(*it2));
494 #endif // EXPAIRSEQ_USE_HASHTAB
497 bool expairseq::is_equal_same_type(const basic &other) const
499 const expairseq &o = dynamic_cast<const expairseq &>(const_cast<basic &>(other));
501 // compare number of elements
502 if (seq.size()!=o.seq.size())
505 // compare overall_coeff
506 if (!overall_coeff.is_equal(o.overall_coeff))
509 #if EXPAIRSEQ_USE_HASHTAB
510 // compare number of elements in each hashtab entry
511 if (hashtabsize!=o.hashtabsize) {
512 std::cout << "this:" << std::endl;
513 print(print_tree(std::cout));
514 std::cout << "other:" << std::endl;
515 other.print(print_tree(std::cout));
518 GINAC_ASSERT(hashtabsize==o.hashtabsize);
520 if (hashtabsize==0) {
521 #endif // EXPAIRSEQ_USE_HASHTAB
522 epvector::const_iterator cit1 = seq.begin();
523 epvector::const_iterator cit2 = o.seq.begin();
524 epvector::const_iterator last1 = seq.end();
526 while (cit1!=last1) {
527 if (!(*cit1).is_equal(*cit2)) return false;
533 #if EXPAIRSEQ_USE_HASHTAB
536 for (unsigned i=0; i<hashtabsize; ++i) {
537 if (hashtab[i].size() != o.hashtab[i].size())
541 // compare individual sorted hashtab entries
542 for (unsigned i=0; i<hashtabsize; ++i) {
543 unsigned sz = hashtab[i].size();
545 const epplist &eppl1 = hashtab[i];
546 const epplist &eppl2 = o.hashtab[i];
547 epplist::const_iterator it1 = eppl1.begin();
548 epplist::const_iterator it2 = eppl2.begin();
549 while (it1!=eppl1.end()) {
550 if (!(*(*it1)).is_equal(*(*it2))) return false;
558 #endif // EXPAIRSEQ_USE_HASHTAB
561 unsigned expairseq::return_type(void) const
563 return return_types::noncommutative_composite;
566 unsigned expairseq::calchash(void) const
568 unsigned v = golden_ratio_hash(tinfo());
569 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
570 #if !EXPAIRSEQ_USE_HASHTAB
571 v = rotate_left_31(v); // rotation would spoil commutativity
572 #endif // EXPAIRSEQ_USE_HASHTAB
573 v ^= cit->rest.gethash();
574 #if !EXPAIRSEQ_USE_HASHTAB
575 v = rotate_left_31(v);
576 v ^= cit->coeff.gethash();
577 #endif // EXPAIRSEQ_USE_HASHTAB
580 v ^= overall_coeff.gethash();
583 // store calculated hash value only if object is already evaluated
584 if (flags &status_flags::evaluated) {
585 setflag(status_flags::hash_calculated);
592 ex expairseq::expand(unsigned options) const
594 epvector *vp = expandchildren(options);
596 // the terms have not changed, so it is safe to declare this expanded
597 setflag(status_flags::expanded);
601 return thisexpairseq(vp,overall_coeff);
605 // new virtual functions which can be overridden by derived classes
610 /** Create an object of this type.
611 * This method works similar to a constructor. It is useful because expairseq
612 * has (at least) two possible different semantics but we want to inherit
613 * methods thus avoiding code duplication. Sometimes a method in expairseq
614 * has to create a new one of the same semantics, which cannot be done by a
615 * ctor because the name (add, mul,...) is unknown on the expaiseq level. In
616 * order for this trick to work a derived class must of course override this
618 ex expairseq::thisexpairseq(const epvector &v, const ex &oc) const
620 return expairseq(v,oc);
623 ex expairseq::thisexpairseq(epvector *vp, const ex &oc) const
625 return expairseq(vp,oc);
628 void expairseq::printpair(const print_context & c, const expair & p, unsigned upper_precedence) const
631 p.rest.bp->print(c, precedence());
633 p.coeff.bp->print(c, precedence());
637 void expairseq::printseq(const print_context & c, char delim,
638 unsigned this_precedence,
639 unsigned upper_precedence) const
641 if (this_precedence <= upper_precedence)
643 epvector::const_iterator it, it_last = seq.end() - 1;
644 for (it=seq.begin(); it!=it_last; ++it) {
645 printpair(c, *it, this_precedence);
648 printpair(c, *it, this_precedence);
649 if (!overall_coeff.is_equal(default_overall_coeff())) {
651 overall_coeff.print(c, this_precedence);
654 if (this_precedence <= upper_precedence)
659 /** Form an expair from an ex, using the corresponding semantics.
660 * @see expairseq::recombine_pair_to_ex() */
661 expair expairseq::split_ex_to_pair(const ex &e) const
663 return expair(e,_ex1());
667 expair expairseq::combine_ex_with_coeff_to_pair(const ex &e,
670 GINAC_ASSERT(is_ex_exactly_of_type(c,numeric));
676 expair expairseq::combine_pair_with_coeff_to_pair(const expair &p,
679 GINAC_ASSERT(is_ex_exactly_of_type(p.coeff,numeric));
680 GINAC_ASSERT(is_ex_exactly_of_type(c,numeric));
682 return expair(p.rest,ex_to_numeric(p.coeff).mul_dyn(ex_to_numeric(c)));
686 /** Form an ex out of an expair, using the corresponding semantics.
687 * @see expairseq::split_ex_to_pair() */
688 ex expairseq::recombine_pair_to_ex(const expair &p) const
690 return lst(p.rest,p.coeff);
693 bool expairseq::expair_needs_further_processing(epp it)
695 #if EXPAIRSEQ_USE_HASHTAB
696 //# error "FIXME: expair_needs_further_processing not yet implemented for hashtabs, sorry. A.F."
697 #endif // EXPAIRSEQ_USE_HASHTAB
701 ex expairseq::default_overall_coeff(void) const
706 void expairseq::combine_overall_coeff(const ex &c)
708 GINAC_ASSERT(is_ex_exactly_of_type(overall_coeff,numeric));
709 GINAC_ASSERT(is_ex_exactly_of_type(c,numeric));
710 overall_coeff = ex_to_numeric(overall_coeff).add_dyn(ex_to_numeric(c));
713 void expairseq::combine_overall_coeff(const ex &c1, const ex &c2)
715 GINAC_ASSERT(is_ex_exactly_of_type(overall_coeff,numeric));
716 GINAC_ASSERT(is_ex_exactly_of_type(c1,numeric));
717 GINAC_ASSERT(is_ex_exactly_of_type(c2,numeric));
718 overall_coeff = ex_to_numeric(overall_coeff).
719 add_dyn(ex_to_numeric(c1).mul(ex_to_numeric(c2)));
722 bool expairseq::can_make_flat(const expair &p) const
729 // non-virtual functions in this class
732 void expairseq::construct_from_2_ex_via_exvector(const ex &lh, const ex &rh)
738 construct_from_exvector(v);
739 #if EXPAIRSEQ_USE_HASHTAB
740 GINAC_ASSERT((hashtabsize==0)||(hashtabsize>=minhashtabsize));
741 GINAC_ASSERT(hashtabsize==calc_hashtabsize(seq.size()));
742 #endif // EXPAIRSEQ_USE_HASHTAB
745 void expairseq::construct_from_2_ex(const ex &lh, const ex &rh)
747 if (lh.bp->tinfo()==tinfo()) {
748 if (rh.bp->tinfo()==tinfo()) {
749 #if EXPAIRSEQ_USE_HASHTAB
750 unsigned totalsize = ex_to_expairseq(lh).seq.size() +
751 ex_to_expairseq(rh).seq.size();
752 if (calc_hashtabsize(totalsize)!=0) {
753 construct_from_2_ex_via_exvector(lh,rh);
755 #endif // EXPAIRSEQ_USE_HASHTAB
756 construct_from_2_expairseq(ex_to_expairseq(lh),
757 ex_to_expairseq(rh));
758 #if EXPAIRSEQ_USE_HASHTAB
760 #endif // EXPAIRSEQ_USE_HASHTAB
763 #if EXPAIRSEQ_USE_HASHTAB
764 unsigned totalsize = ex_to_expairseq(lh).seq.size()+1;
765 if (calc_hashtabsize(totalsize)!=0) {
766 construct_from_2_ex_via_exvector(lh, rh);
768 #endif // EXPAIRSEQ_USE_HASHTAB
769 construct_from_expairseq_ex(ex_to_expairseq(lh), rh);
770 #if EXPAIRSEQ_USE_HASHTAB
772 #endif // EXPAIRSEQ_USE_HASHTAB
775 } else if (rh.bp->tinfo()==tinfo()) {
776 #if EXPAIRSEQ_USE_HASHTAB
777 unsigned totalsize=ex_to_expairseq(rh).seq.size()+1;
778 if (calc_hashtabsize(totalsize)!=0) {
779 construct_from_2_ex_via_exvector(lh,rh);
781 #endif // EXPAIRSEQ_USE_HASHTAB
782 construct_from_expairseq_ex(ex_to_expairseq(rh),lh);
783 #if EXPAIRSEQ_USE_HASHTAB
785 #endif // EXPAIRSEQ_USE_HASHTAB
789 #if EXPAIRSEQ_USE_HASHTAB
790 if (calc_hashtabsize(2)!=0) {
791 construct_from_2_ex_via_exvector(lh,rh);
795 #endif // EXPAIRSEQ_USE_HASHTAB
797 if (is_ex_exactly_of_type(lh,numeric)) {
798 if (is_ex_exactly_of_type(rh,numeric)) {
799 combine_overall_coeff(lh);
800 combine_overall_coeff(rh);
802 combine_overall_coeff(lh);
803 seq.push_back(split_ex_to_pair(rh));
806 if (is_ex_exactly_of_type(rh,numeric)) {
807 combine_overall_coeff(rh);
808 seq.push_back(split_ex_to_pair(lh));
810 expair p1 = split_ex_to_pair(lh);
811 expair p2 = split_ex_to_pair(rh);
813 int cmpval = p1.rest.compare(p2.rest);
815 p1.coeff=ex_to_numeric(p1.coeff).add_dyn(ex_to_numeric(p2.coeff));
816 if (!ex_to_numeric(p1.coeff).is_zero()) {
817 // no further processing is necessary, since this
818 // one element will usually be recombined in eval()
835 void expairseq::construct_from_2_expairseq(const expairseq &s1,
838 combine_overall_coeff(s1.overall_coeff);
839 combine_overall_coeff(s2.overall_coeff);
841 epvector::const_iterator first1 = s1.seq.begin();
842 epvector::const_iterator last1 = s1.seq.end();
843 epvector::const_iterator first2 = s2.seq.begin();
844 epvector::const_iterator last2 = s2.seq.end();
846 seq.reserve(s1.seq.size()+s2.seq.size());
848 bool needs_further_processing=false;
850 while (first1!=last1 && first2!=last2) {
851 int cmpval = (*first1).rest.compare((*first2).rest);
854 const numeric &newcoeff = ex_to_numeric((*first1).coeff).
855 add(ex_to_numeric((*first2).coeff));
856 if (!newcoeff.is_zero()) {
857 seq.push_back(expair((*first1).rest,newcoeff));
858 if (expair_needs_further_processing(seq.end()-1)) {
859 needs_further_processing = true;
864 } else if (cmpval<0) {
865 seq.push_back(*first1);
868 seq.push_back(*first2);
873 while (first1!=last1) {
874 seq.push_back(*first1);
877 while (first2!=last2) {
878 seq.push_back(*first2);
882 if (needs_further_processing) {
885 construct_from_epvector(v);
889 void expairseq::construct_from_expairseq_ex(const expairseq &s,
892 combine_overall_coeff(s.overall_coeff);
893 if (is_ex_exactly_of_type(e,numeric)) {
894 combine_overall_coeff(e);
899 epvector::const_iterator first = s.seq.begin();
900 epvector::const_iterator last = s.seq.end();
901 expair p = split_ex_to_pair(e);
903 seq.reserve(s.seq.size()+1);
904 bool p_pushed = false;
906 bool needs_further_processing=false;
908 // merge p into s.seq
909 while (first!=last) {
910 int cmpval=(*first).rest.compare(p.rest);
913 const numeric &newcoeff = ex_to_numeric((*first).coeff).
914 add(ex_to_numeric(p.coeff));
915 if (!newcoeff.is_zero()) {
916 seq.push_back(expair((*first).rest,newcoeff));
917 if (expair_needs_further_processing(seq.end()-1)) {
918 needs_further_processing = true;
924 } else if (cmpval<0) {
925 seq.push_back(*first);
935 // while loop exited because p was pushed, now push rest of s.seq
936 while (first!=last) {
937 seq.push_back(*first);
941 // while loop exited because s.seq was pushed, now push p
945 if (needs_further_processing) {
948 construct_from_epvector(v);
952 void expairseq::construct_from_exvector(const exvector &v)
954 // simplifications: +(a,+(b,c),d) -> +(a,b,c,d) (associativity)
955 // +(d,b,c,a) -> +(a,b,c,d) (canonicalization)
956 // +(...,x,*(x,c1),*(x,c2)) -> +(...,*(x,1+c1+c2)) (c1, c2 numeric())
957 // (same for (+,*) -> (*,^)
960 #if EXPAIRSEQ_USE_HASHTAB
961 combine_same_terms();
964 combine_same_terms_sorted_seq();
965 #endif // EXPAIRSEQ_USE_HASHTAB
969 void expairseq::construct_from_epvector(const epvector &v)
971 // simplifications: +(a,+(b,c),d) -> +(a,b,c,d) (associativity)
972 // +(d,b,c,a) -> +(a,b,c,d) (canonicalization)
973 // +(...,x,*(x,c1),*(x,c2)) -> +(...,*(x,1+c1+c2)) (c1, c2 numeric())
974 // (same for (+,*) -> (*,^)
977 #if EXPAIRSEQ_USE_HASHTAB
978 combine_same_terms();
981 combine_same_terms_sorted_seq();
982 #endif // EXPAIRSEQ_USE_HASHTAB
986 /** Combine this expairseq with argument exvector.
987 * It cares for associativity as well as for special handling of numerics. */
988 void expairseq::make_flat(const exvector &v)
990 exvector::const_iterator cit;
992 // count number of operands which are of same expairseq derived type
993 // and their cumulative number of operands
998 while (cit!=v.end()) {
999 if (cit->bp->tinfo()==this->tinfo()) {
1001 noperands += ex_to_expairseq(*cit).seq.size();
1006 // reserve seq and coeffseq which will hold all operands
1007 seq.reserve(v.size()+noperands-nexpairseqs);
1009 // copy elements and split off numerical part
1011 while (cit!=v.end()) {
1012 if (cit->bp->tinfo()==this->tinfo()) {
1013 const expairseq &subseqref = ex_to_expairseq(*cit);
1014 combine_overall_coeff(subseqref.overall_coeff);
1015 epvector::const_iterator cit_s = subseqref.seq.begin();
1016 while (cit_s!=subseqref.seq.end()) {
1017 seq.push_back(*cit_s);
1021 if (is_ex_exactly_of_type(*cit,numeric))
1022 combine_overall_coeff(*cit);
1024 seq.push_back(split_ex_to_pair(*cit));
1032 /** Combine this expairseq with argument epvector.
1033 * It cares for associativity as well as for special handling of numerics. */
1034 void expairseq::make_flat(const epvector &v)
1036 epvector::const_iterator cit;
1038 // count number of operands which are of same expairseq derived type
1039 // and their cumulative number of operands
1040 int nexpairseqs = 0;
1044 while (cit!=v.end()) {
1045 if (cit->rest.bp->tinfo()==this->tinfo()) {
1047 noperands += ex_to_expairseq((*cit).rest).seq.size();
1052 // reserve seq and coeffseq which will hold all operands
1053 seq.reserve(v.size()+noperands-nexpairseqs);
1055 // copy elements and split off numerical part
1057 while (cit!=v.end()) {
1058 if (cit->rest.bp->tinfo()==this->tinfo() &&
1059 this->can_make_flat(*cit)) {
1060 const expairseq &subseqref = ex_to_expairseq((*cit).rest);
1061 combine_overall_coeff(ex_to_numeric(subseqref.overall_coeff),
1062 ex_to_numeric((*cit).coeff));
1063 epvector::const_iterator cit_s = subseqref.seq.begin();
1064 while (cit_s!=subseqref.seq.end()) {
1065 seq.push_back(expair((*cit_s).rest,
1066 ex_to_numeric((*cit_s).coeff).mul_dyn(ex_to_numeric((*cit).coeff))));
1067 //seq.push_back(combine_pair_with_coeff_to_pair(*cit_s,
1072 if (cit->is_canonical_numeric())
1073 combine_overall_coeff(cit->rest);
1075 seq.push_back(*cit);
1082 /** Brings this expairseq into a sorted (canonical) form. */
1083 void expairseq::canonicalize(void)
1086 sort(seq.begin(),seq.end(),expair_is_less());
1091 /** Compact a presorted expairseq by combining all matching expairs to one
1092 * each. On an add object, this is responsible for 2*x+3*x+y -> 5*x+y, for
1094 void expairseq::combine_same_terms_sorted_seq(void)
1096 bool needs_further_processing = false;
1099 epvector::iterator itin1 = seq.begin();
1100 epvector::iterator itin2 = itin1+1;
1101 epvector::iterator itout = itin1;
1102 epvector::iterator last = seq.end();
1103 // must_copy will be set to true the first time some combination is
1104 // possible from then on the sequence has changed and must be compacted
1105 bool must_copy = false;
1106 while (itin2!=last) {
1107 if ((*itin1).rest.compare((*itin2).rest)==0) {
1108 (*itin1).coeff = ex_to_numeric((*itin1).coeff).
1109 add_dyn(ex_to_numeric((*itin2).coeff));
1110 if (expair_needs_further_processing(itin1))
1111 needs_further_processing = true;
1114 if (!ex_to_numeric((*itin1).coeff).is_zero()) {
1123 if (!ex_to_numeric((*itin1).coeff).is_zero()) {
1129 seq.erase(itout,last);
1132 if (needs_further_processing) {
1135 construct_from_epvector(v);
1140 #if EXPAIRSEQ_USE_HASHTAB
1142 unsigned expairseq::calc_hashtabsize(unsigned sz) const
1145 unsigned nearest_power_of_2 = 1 << log2(sz);
1146 // if (nearest_power_of_2 < maxhashtabsize/hashtabfactor) {
1147 // size = nearest_power_of_2*hashtabfactor;
1148 size = nearest_power_of_2/hashtabfactor;
1149 if (size<minhashtabsize)
1151 GINAC_ASSERT(hashtabsize<=0x8000000U); // really max size due to 31 bit hashing
1152 // hashtabsize must be a power of 2
1153 GINAC_ASSERT((1U << log2(size))==size);
1157 unsigned expairseq::calc_hashindex(const ex &e) const
1159 // calculate hashindex
1160 unsigned hash = e.gethash();
1162 if (is_a_numeric_hash(hash)) {
1163 hashindex = hashmask;
1165 hashindex = hash &hashmask;
1166 // last hashtab entry is reserved for numerics
1167 if (hashindex==hashmask) hashindex = 0;
1169 GINAC_ASSERT(hashindex>=0);
1170 GINAC_ASSERT((hashindex<hashtabsize)||(hashtabsize==0));
1174 void expairseq::shrink_hashtab(void)
1176 unsigned new_hashtabsize;
1177 while (hashtabsize!=(new_hashtabsize=calc_hashtabsize(seq.size()))) {
1178 GINAC_ASSERT(new_hashtabsize<hashtabsize);
1179 if (new_hashtabsize==0) {
1186 // shrink by a factor of 2
1187 unsigned half_hashtabsize = hashtabsize/2;
1188 for (unsigned i=0; i<half_hashtabsize-1; ++i)
1189 hashtab[i].merge(hashtab[i+half_hashtabsize],epp_is_less());
1190 // special treatment for numeric hashes
1191 hashtab[0].merge(hashtab[half_hashtabsize-1],epp_is_less());
1192 hashtab[half_hashtabsize-1] = hashtab[hashtabsize-1];
1193 hashtab.resize(half_hashtabsize);
1194 hashtabsize = half_hashtabsize;
1195 hashmask = hashtabsize-1;
1199 void expairseq::remove_hashtab_entry(epvector::const_iterator element)
1202 return; // nothing to do
1204 // calculate hashindex of element to be deleted
1205 unsigned hashindex = calc_hashindex((*element).rest);
1207 // find it in hashtab and remove it
1208 epplist &eppl = hashtab[hashindex];
1209 epplist::iterator epplit = eppl.begin();
1210 bool erased = false;
1211 while (epplit!=eppl.end()) {
1212 if (*epplit == element) {
1221 cout << "tried to erase " << element-seq.begin() << std::endl;
1222 cout << "size " << seq.end()-seq.begin() << std::endl;
1224 unsigned hashindex = calc_hashindex((*element).rest);
1225 epplist &eppl = hashtab[hashindex];
1226 epplist::iterator epplit=eppl.begin();
1228 while (epplit!=eppl.end()) {
1229 if (*epplit == element) {
1236 GINAC_ASSERT(erased);
1238 GINAC_ASSERT(erased);
1241 void expairseq::move_hashtab_entry(epvector::const_iterator oldpos,
1242 epvector::iterator newpos)
1244 GINAC_ASSERT(hashtabsize!=0);
1246 // calculate hashindex of element which was moved
1247 unsigned hashindex=calc_hashindex((*newpos).rest);
1249 // find it in hashtab and modify it
1250 epplist &eppl = hashtab[hashindex];
1251 epplist::iterator epplit = eppl.begin();
1252 while (epplit!=eppl.end()) {
1253 if (*epplit == oldpos) {
1259 GINAC_ASSERT(epplit!=eppl.end());
1262 void expairseq::sorted_insert(epplist &eppl, epp elem)
1264 epplist::iterator current = eppl.begin();
1265 while ((current!=eppl.end())&&((*(*current)).is_less(*elem))) {
1268 eppl.insert(current,elem);
1271 void expairseq::build_hashtab_and_combine(epvector::iterator &first_numeric,
1272 epvector::iterator &last_non_zero,
1273 std::vector<bool> &touched,
1274 unsigned &number_of_zeroes)
1276 epp current=seq.begin();
1278 while (current!=first_numeric) {
1279 if (is_ex_exactly_of_type((*current).rest,numeric)) {
1281 iter_swap(current,first_numeric);
1283 // calculate hashindex
1284 unsigned currenthashindex = calc_hashindex((*current).rest);
1286 // test if there is already a matching expair in the hashtab-list
1287 epplist &eppl=hashtab[currenthashindex];
1288 epplist::iterator epplit = eppl.begin();
1289 while (epplit!=eppl.end()) {
1290 if ((*current).rest.is_equal((*(*epplit)).rest))
1294 if (epplit==eppl.end()) {
1295 // no matching expair found, append this to end of list
1296 sorted_insert(eppl,current);
1299 // epplit points to a matching expair, combine it with current
1300 (*(*epplit)).coeff = ex_to_numeric((*(*epplit)).coeff).
1301 add_dyn(ex_to_numeric((*current).coeff));
1303 // move obsolete current expair to end by swapping with last_non_zero element
1304 // if this was a numeric, it is swapped with the expair before first_numeric
1305 iter_swap(current,last_non_zero);
1307 if (first_numeric!=last_non_zero) iter_swap(first_numeric,current);
1310 // test if combined term has coeff 0 and can be removed is done later
1311 touched[(*epplit)-seq.begin()]=true;
1317 void expairseq::drop_coeff_0_terms(epvector::iterator &first_numeric,
1318 epvector::iterator &last_non_zero,
1319 std::vector<bool> &touched,
1320 unsigned &number_of_zeroes)
1322 // move terms with coeff 0 to end and remove them from hashtab
1323 // check only those elements which have been touched
1324 epp current = seq.begin();
1326 while (current!=first_numeric) {
1330 } else if (!ex_to_numeric((*current).coeff).is_zero()) {
1334 remove_hashtab_entry(current);
1336 // move element to the end, unless it is already at the end
1337 if (current!=last_non_zero) {
1338 iter_swap(current,last_non_zero);
1340 bool numeric_swapped=first_numeric!=last_non_zero;
1341 if (numeric_swapped) iter_swap(first_numeric,current);
1342 epvector::iterator changed_entry;
1344 if (numeric_swapped)
1345 changed_entry = first_numeric;
1347 changed_entry = last_non_zero;
1352 if (first_numeric!=current) {
1354 // change entry in hashtab which referred to first_numeric or last_non_zero to current
1355 move_hashtab_entry(changed_entry,current);
1356 touched[current-seq.begin()] = touched[changed_entry-seq.begin()];
1365 GINAC_ASSERT(i==current-seq.begin());
1368 /** True if one of the coeffs vanishes, otherwise false.
1369 * This would be an invariant violation, so this should only be used for
1370 * debugging purposes. */
1371 bool expairseq::has_coeff_0(void) const
1373 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
1374 if ((*cit).coeff.is_zero())
1380 void expairseq::add_numerics_to_hashtab(epvector::iterator first_numeric,
1381 epvector::const_iterator last_non_zero)
1383 if (first_numeric==seq.end()) return; // no numerics
1385 epvector::iterator current = first_numeric;
1386 epvector::const_iterator last = last_non_zero+1;
1387 while (current!=last) {
1388 sorted_insert(hashtab[hashmask],current);
1393 void expairseq::combine_same_terms(void)
1395 // combine same terms, drop term with coeff 0, move numerics to end
1397 // calculate size of hashtab
1398 hashtabsize = calc_hashtabsize(seq.size());
1400 // hashtabsize is a power of 2
1401 hashmask = hashtabsize-1;
1405 hashtab.resize(hashtabsize);
1407 if (hashtabsize==0) {
1409 combine_same_terms_sorted_seq();
1410 GINAC_ASSERT(!has_coeff_0());
1414 // iterate through seq, move numerics to end,
1415 // fill hashtab and combine same terms
1416 epvector::iterator first_numeric = seq.end();
1417 epvector::iterator last_non_zero = seq.end()-1;
1419 std::vector<bool> touched;
1420 touched.reserve(seq.size());
1421 for (unsigned i=0; i<seq.size(); ++i) touched[i]=false;
1423 unsigned number_of_zeroes = 0;
1425 GINAC_ASSERT(!has_coeff_0());
1426 build_hashtab_and_combine(first_numeric,last_non_zero,touched,number_of_zeroes);
1428 cout << "in combine:" << std::endl;
1430 cout << "size=" << seq.end() - seq.begin() << std::endl;
1431 cout << "first_numeric=" << first_numeric - seq.begin() << std::endl;
1432 cout << "last_non_zero=" << last_non_zero - seq.begin() << std::endl;
1433 for (unsigned i=0; i<seq.size(); ++i) {
1434 if (touched[i]) cout << i << " is touched" << std::endl;
1436 cout << "end in combine" << std::endl;
1439 // there should not be any terms with coeff 0 from the beginning,
1440 // so it should be safe to skip this step
1441 if (number_of_zeroes!=0) {
1442 drop_coeff_0_terms(first_numeric,last_non_zero,touched,number_of_zeroes);
1444 cout << "in combine after drop:" << std::endl;
1446 cout << "size=" << seq.end() - seq.begin() << std::endl;
1447 cout << "first_numeric=" << first_numeric - seq.begin() << std::endl;
1448 cout << "last_non_zero=" << last_non_zero - seq.begin() << std::endl;
1449 for (unsigned i=0; i<seq.size(); ++i) {
1450 if (touched[i]) cout << i << " is touched" << std::endl;
1452 cout << "end in combine after drop" << std::endl;
1456 add_numerics_to_hashtab(first_numeric,last_non_zero);
1458 // pop zero elements
1459 for (unsigned i=0; i<number_of_zeroes; ++i) {
1463 // shrink hashtabsize to calculated value
1464 GINAC_ASSERT(!has_coeff_0());
1468 GINAC_ASSERT(!has_coeff_0());
1471 #endif // EXPAIRSEQ_USE_HASHTAB
1473 /** Check if this expairseq is in sorted (canonical) form. Useful mainly for
1474 * debugging or in assertions since being sorted is an invariance. */
1475 bool expairseq::is_canonical() const
1477 if (seq.size() <= 1)
1480 #if EXPAIRSEQ_USE_HASHTAB
1481 if (hashtabsize > 0) return 1; // not canoncalized
1482 #endif // EXPAIRSEQ_USE_HASHTAB
1484 epvector::const_iterator it = seq.begin();
1485 epvector::const_iterator it_last = it;
1486 for (++it; it!=seq.end(); it_last=it, ++it) {
1487 if (!((*it_last).is_less(*it) || (*it_last).is_equal(*it))) {
1488 if (!is_ex_exactly_of_type((*it_last).rest,numeric) ||
1489 !is_ex_exactly_of_type((*it).rest,numeric)) {
1490 // double test makes it easier to set a breakpoint...
1491 if (!is_ex_exactly_of_type((*it_last).rest,numeric) ||
1492 !is_ex_exactly_of_type((*it).rest,numeric)) {
1493 printpair(std::clog, *it_last, 0);
1495 printpair(std::clog, *it, 0);
1497 std::clog << "pair1:" << std::endl;
1498 (*it_last).rest.print(print_tree(std::clog));
1499 (*it_last).coeff.print(print_tree(std::clog));
1500 std::clog << "pair2:" << std::endl;
1501 (*it).rest.print(print_tree(std::clog));
1502 (*it).coeff.print(print_tree(std::clog));
1512 /** Member-wise expand the expairs in this sequence.
1514 * @see expairseq::expand()
1515 * @return pointer to epvector containing expanded pairs or zero pointer,
1516 * if no members were changed. */
1517 epvector * expairseq::expandchildren(unsigned options) const
1519 epvector::const_iterator last = seq.end();
1520 epvector::const_iterator cit = seq.begin();
1522 const ex &expanded_ex = (*cit).rest.expand(options);
1523 if (!are_ex_trivially_equal((*cit).rest,expanded_ex)) {
1525 // something changed, copy seq, eval and return it
1526 epvector *s = new epvector;
1527 s->reserve(seq.size());
1529 // copy parts of seq which are known not to have changed
1530 epvector::const_iterator cit2 = seq.begin();
1532 s->push_back(*cit2);
1535 // copy first changed element
1536 s->push_back(combine_ex_with_coeff_to_pair(expanded_ex,
1540 while (cit2!=last) {
1541 s->push_back(combine_ex_with_coeff_to_pair((*cit2).rest.expand(options),
1550 return 0; // signalling nothing has changed
1554 /** Member-wise evaluate the expairs in this sequence.
1556 * @see expairseq::eval()
1557 * @return pointer to epvector containing evaluated pairs or zero pointer,
1558 * if no members were changed. */
1559 epvector * expairseq::evalchildren(int level) const
1561 // returns a NULL pointer if nothing had to be evaluated
1562 // returns a pointer to a newly created epvector otherwise
1563 // (which has to be deleted somewhere else)
1568 if (level == -max_recursion_level)
1569 throw(std::runtime_error("max recursion level reached"));
1572 epvector::const_iterator last=seq.end();
1573 epvector::const_iterator cit=seq.begin();
1575 const ex &evaled_ex = (*cit).rest.eval(level);
1576 if (!are_ex_trivially_equal((*cit).rest,evaled_ex)) {
1578 // something changed, copy seq, eval and return it
1579 epvector *s = new epvector;
1580 s->reserve(seq.size());
1582 // copy parts of seq which are known not to have changed
1583 epvector::const_iterator cit2=seq.begin();
1585 s->push_back(*cit2);
1588 // copy first changed element
1589 s->push_back(combine_ex_with_coeff_to_pair(evaled_ex,
1593 while (cit2!=last) {
1594 s->push_back(combine_ex_with_coeff_to_pair((*cit2).rest.eval(level),
1603 return 0; // signalling nothing has changed
1607 /** Member-wise evaluate numerically all expairs in this sequence.
1609 * @see expairseq::evalf()
1610 * @return epvector with all entries evaluated numerically. */
1611 epvector expairseq::evalfchildren(int level) const
1616 if (level==-max_recursion_level)
1617 throw(std::runtime_error("max recursion level reached"));
1620 s.reserve(seq.size());
1623 for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
1624 s.push_back(combine_ex_with_coeff_to_pair((*it).rest.evalf(level),
1625 (*it).coeff.evalf(level)));
1631 /** Member-wise normalize all expairs in this sequence.
1633 * @see expairseq::normal()
1634 * @return epvector with all entries normalized. */
1635 epvector expairseq::normalchildren(int level) const
1640 if (level==-max_recursion_level)
1641 throw(std::runtime_error("max recursion level reached"));
1644 s.reserve(seq.size());
1647 for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
1648 s.push_back(combine_ex_with_coeff_to_pair((*it).rest.normal(level),
1655 /** Member-wise differentiate all expairs in this sequence.
1657 * @see expairseq::diff()
1658 * @return epvector with all entries differentiated. */
1659 epvector expairseq::diffchildren(const symbol &y) const
1662 s.reserve(seq.size());
1664 for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
1665 s.push_back(combine_ex_with_coeff_to_pair((*it).rest.diff(y),
1672 /** Member-wise substitute in this sequence.
1674 * @see expairseq::subs()
1675 * @return pointer to epvector containing pairs after application of subs,
1676 * or NULL pointer if no members were changed. */
1677 epvector * expairseq::subschildren(const lst &ls, const lst &lr, bool no_pattern) const
1679 GINAC_ASSERT(ls.nops()==lr.nops());
1681 // The substitution is "complex" when any of the objects to be substituted
1682 // is a product or power. In this case we have to recombine the pairs
1683 // because the numeric coefficients may be part of the search pattern.
1684 bool complex_subs = false;
1685 for (unsigned i=0; i<ls.nops(); i++)
1686 if (is_ex_exactly_of_type(ls.op(i), mul) || is_ex_exactly_of_type(ls.op(i), power)) {
1687 complex_subs = true;
1693 // Substitute in the recombined pairs
1694 epvector::const_iterator cit = seq.begin(), last = seq.end();
1695 while (cit != last) {
1697 const ex &orig_ex = recombine_pair_to_ex(*cit);
1698 const ex &subsed_ex = orig_ex.subs(ls, lr, no_pattern);
1699 if (!are_ex_trivially_equal(orig_ex, subsed_ex)) {
1701 // Something changed, copy seq, subs and return it
1702 epvector *s = new epvector;
1703 s->reserve(seq.size());
1705 // Copy parts of seq which are known not to have changed
1706 s->insert(s->begin(), seq.begin(), cit);
1708 // Copy first changed element
1709 s->push_back(split_ex_to_pair(subsed_ex));
1713 while (cit != last) {
1714 s->push_back(split_ex_to_pair(recombine_pair_to_ex(*cit).subs(ls, lr, no_pattern)));
1725 // Substitute only in the "rest" part of the pairs
1726 epvector::const_iterator cit = seq.begin(), last = seq.end();
1727 while (cit != last) {
1729 const ex &subsed_ex = cit->rest.subs(ls, lr, no_pattern);
1730 if (!are_ex_trivially_equal(cit->rest, subsed_ex)) {
1732 // Something changed, copy seq, subs and return it
1733 epvector *s = new epvector;
1734 s->reserve(seq.size());
1736 // Copy parts of seq which are known not to have changed
1737 s->insert(s->begin(), seq.begin(), cit);
1739 // Copy first changed element
1740 s->push_back(combine_ex_with_coeff_to_pair(subsed_ex, cit->coeff));
1744 while (cit != last) {
1745 s->push_back(combine_ex_with_coeff_to_pair(cit->rest.subs(ls, lr, no_pattern),
1756 // Nothing has changed
1761 // static member variables
1764 #if EXPAIRSEQ_USE_HASHTAB
1765 unsigned expairseq::maxhashtabsize = 0x4000000U;
1766 unsigned expairseq::minhashtabsize = 0x1000U;
1767 unsigned expairseq::hashtabfactor = 1;
1768 #endif // EXPAIRSEQ_USE_HASHTAB
1770 } // namespace GiNaC