1 /** @file expairseq.cpp
3 * Implementation of sequences of expression pairs. */
6 * GiNaC Copyright (C) 1999-2006 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 #include "expairseq.h"
32 #include "relational.h"
35 #include "operators.h"
39 #if EXPAIRSEQ_USE_HASHTAB
41 #endif // EXPAIRSEQ_USE_HASHTAB
46 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(expairseq, basic,
47 print_func<print_context>(&expairseq::do_print).
48 print_func<print_tree>(&expairseq::do_print_tree))
58 bool operator()(const epp &lh, const epp &rh) const
60 return (*lh).is_less(*rh);
65 // default constructor
70 expairseq::expairseq() : inherited(&expairseq::tinfo_static)
71 #if EXPAIRSEQ_USE_HASHTAB
73 #endif // EXPAIRSEQ_USE_HASHTAB
79 /** For use by copy ctor and assignment operator. */
80 void expairseq::copy(const expairseq &other)
83 overall_coeff = other.overall_coeff;
84 #if EXPAIRSEQ_USE_HASHTAB
86 hashtabsize = other.hashtabsize;
88 hashmask = other.hashmask;
89 hashtab.resize(hashtabsize);
90 epvector::const_iterator osb = other.seq.begin();
91 for (unsigned i=0; i<hashtabsize; ++i) {
93 for (epplist::const_iterator cit=other.hashtab[i].begin();
94 cit!=other.hashtab[i].end(); ++cit) {
95 hashtab[i].push_back(seq.begin()+((*cit)-osb));
101 #endif // EXPAIRSEQ_USE_HASHTAB
106 // other constructors
109 expairseq::expairseq(const ex &lh, const ex &rh) : inherited(&expairseq::tinfo_static)
111 construct_from_2_ex(lh,rh);
112 GINAC_ASSERT(is_canonical());
115 expairseq::expairseq(const exvector &v) : inherited(&expairseq::tinfo_static)
117 construct_from_exvector(v);
118 GINAC_ASSERT(is_canonical());
121 expairseq::expairseq(const epvector &v, const ex &oc)
122 : inherited(&expairseq::tinfo_static), overall_coeff(oc)
124 GINAC_ASSERT(is_a<numeric>(oc));
125 construct_from_epvector(v);
126 GINAC_ASSERT(is_canonical());
129 expairseq::expairseq(std::auto_ptr<epvector> vp, const ex &oc)
130 : inherited(&expairseq::tinfo_static), overall_coeff(oc)
132 GINAC_ASSERT(vp.get()!=0);
133 GINAC_ASSERT(is_a<numeric>(oc));
134 construct_from_epvector(*vp);
135 GINAC_ASSERT(is_canonical());
142 expairseq::expairseq(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst)
143 #if EXPAIRSEQ_USE_HASHTAB
147 for (unsigned int i=0; true; i++) {
150 if (n.find_ex("rest", rest, sym_lst, i) && n.find_ex("coeff", coeff, sym_lst, i))
151 seq.push_back(expair(rest, coeff));
156 n.find_ex("overall_coeff", overall_coeff, sym_lst);
159 GINAC_ASSERT(is_canonical());
162 void expairseq::archive(archive_node &n) const
164 inherited::archive(n);
165 epvector::const_iterator i = seq.begin(), iend = seq.end();
167 n.add_ex("rest", i->rest);
168 n.add_ex("coeff", i->coeff);
171 n.add_ex("overall_coeff", overall_coeff);
174 DEFAULT_UNARCHIVE(expairseq)
177 // functions overriding virtual functions from base classes
182 void expairseq::do_print(const print_context & c, unsigned level) const
185 printseq(c, ',', precedence(), level);
189 void expairseq::do_print_tree(const print_tree & c, unsigned level) const
191 c.s << std::string(level, ' ') << class_name() << " @" << this
192 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
193 << ", nops=" << nops()
195 size_t num = seq.size();
196 for (size_t i=0; i<num; ++i) {
197 seq[i].rest.print(c, level + c.delta_indent);
198 seq[i].coeff.print(c, level + c.delta_indent);
200 c.s << std::string(level + c.delta_indent, ' ') << "-----" << std::endl;
202 if (!overall_coeff.is_equal(default_overall_coeff())) {
203 c.s << std::string(level + c.delta_indent, ' ') << "-----" << std::endl
204 << std::string(level + c.delta_indent, ' ') << "overall_coeff" << std::endl;
205 overall_coeff.print(c, level + c.delta_indent);
207 c.s << std::string(level + c.delta_indent,' ') << "=====" << std::endl;
208 #if EXPAIRSEQ_USE_HASHTAB
209 c.s << std::string(level + c.delta_indent,' ')
210 << "hashtab size " << hashtabsize << std::endl;
211 if (hashtabsize == 0) return;
213 unsigned count[MAXCOUNT+1];
214 for (int i=0; i<MAXCOUNT+1; ++i)
216 unsigned this_bin_fill;
217 unsigned cum_fill_sq = 0;
218 unsigned cum_fill = 0;
219 for (unsigned i=0; i<hashtabsize; ++i) {
221 if (hashtab[i].size() > 0) {
222 c.s << std::string(level + c.delta_indent, ' ')
223 << "bin " << i << " with entries ";
224 for (epplist::const_iterator it=hashtab[i].begin();
225 it!=hashtab[i].end(); ++it) {
226 c.s << *it-seq.begin() << " ";
230 cum_fill += this_bin_fill;
231 cum_fill_sq += this_bin_fill*this_bin_fill;
233 if (this_bin_fill<MAXCOUNT)
234 ++count[this_bin_fill];
240 double lambda = (1.0*seq.size()) / hashtabsize;
241 for (int k=0; k<MAXCOUNT; ++k) {
244 double prob = std::pow(lambda,k)/fact * std::exp(-lambda);
246 c.s << std::string(level + c.delta_indent, ' ') << "bins with " << k << " entries: "
247 << int(1000.0*count[k]/hashtabsize)/10.0 << "% (expected: "
248 << int(prob*1000)/10.0 << ")" << std::endl;
250 c.s << std::string(level + c.delta_indent, ' ') << "bins with more entries: "
251 << int(1000.0*count[MAXCOUNT]/hashtabsize)/10.0 << "% (expected: "
252 << int((1-cum_prob)*1000)/10.0 << ")" << std::endl;
254 c.s << std::string(level + c.delta_indent, ' ') << "variance: "
255 << 1.0/hashtabsize*cum_fill_sq-(1.0/hashtabsize*cum_fill)*(1.0/hashtabsize*cum_fill)
257 c.s << std::string(level + c.delta_indent, ' ') << "average fill: "
258 << (1.0*cum_fill)/hashtabsize
259 << " (should be equal to " << (1.0*seq.size())/hashtabsize << ")" << std::endl;
260 #endif // EXPAIRSEQ_USE_HASHTAB
263 bool expairseq::info(unsigned inf) const
265 return inherited::info(inf);
268 size_t expairseq::nops() const
270 if (overall_coeff.is_equal(default_overall_coeff()))
276 ex expairseq::op(size_t i) const
279 return recombine_pair_to_ex(seq[i]);
280 GINAC_ASSERT(!overall_coeff.is_equal(default_overall_coeff()));
281 return overall_coeff;
284 ex expairseq::map(map_function &f) const
286 std::auto_ptr<epvector> v(new epvector);
287 v->reserve(seq.size());
289 epvector::const_iterator cit = seq.begin(), last = seq.end();
290 while (cit != last) {
291 v->push_back(split_ex_to_pair(f(recombine_pair_to_ex(*cit))));
295 if (overall_coeff.is_equal(default_overall_coeff()))
296 return thisexpairseq(v, default_overall_coeff());
298 return thisexpairseq(v, f(overall_coeff));
301 /** Perform coefficient-wise automatic term rewriting rules in this class. */
302 ex expairseq::eval(int level) const
304 if ((level==1) && (flags &status_flags::evaluated))
307 std::auto_ptr<epvector> vp = evalchildren(level);
311 return (new expairseq(vp, overall_coeff))->setflag(status_flags::dynallocated | status_flags::evaluated);
314 epvector* conjugateepvector(const epvector&epv)
316 epvector *newepv = 0;
317 for (epvector::const_iterator i=epv.begin(); i!=epv.end(); ++i) {
319 newepv->push_back(i->conjugate());
322 expair x = i->conjugate();
323 if (x.is_equal(*i)) {
326 newepv = new epvector;
327 newepv->reserve(epv.size());
328 for (epvector::const_iterator j=epv.begin(); j!=i; ++j) {
329 newepv->push_back(*j);
331 newepv->push_back(x);
336 ex expairseq::conjugate() const
338 epvector* newepv = conjugateepvector(seq);
339 ex x = overall_coeff.conjugate();
340 if (!newepv && are_ex_trivially_equal(x, overall_coeff)) {
343 ex result = thisexpairseq(newepv ? *newepv : seq, x);
350 bool expairseq::match(const ex & pattern, lst & repl_lst) const
352 // This differs from basic::match() because we want "a+b+c+d" to
353 // match "d+*+b" with "*" being "a+c", and we want to honor commutativity
355 if (this->tinfo() == ex_to<basic>(pattern).tinfo()) {
357 // Check whether global wildcard (one that matches the "rest of the
358 // expression", like "*" above) is present
359 bool has_global_wildcard = false;
361 for (size_t i=0; i<pattern.nops(); i++) {
362 if (is_exactly_a<wildcard>(pattern.op(i))) {
363 has_global_wildcard = true;
364 global_wildcard = pattern.op(i);
369 // Unfortunately, this is an O(N^2) operation because we can't
370 // sort the pattern in a useful way...
375 for (size_t i=0; i<nops(); i++)
376 ops.push_back(op(i));
378 // Now, for every term of the pattern, look for a matching term in
379 // the expression and remove the match
380 for (size_t i=0; i<pattern.nops(); i++) {
381 ex p = pattern.op(i);
382 if (has_global_wildcard && p.is_equal(global_wildcard))
384 exvector::iterator it = ops.begin(), itend = ops.end();
385 while (it != itend) {
386 if (it->match(p, repl_lst)) {
392 return false; // no match found
396 if (has_global_wildcard) {
398 // Assign all the remaining terms to the global wildcard (unless
399 // it has already been matched before, in which case the matches
401 size_t num = ops.size();
402 std::auto_ptr<epvector> vp(new epvector);
404 for (size_t i=0; i<num; i++)
405 vp->push_back(split_ex_to_pair(ops[i]));
406 ex rest = thisexpairseq(vp, default_overall_coeff());
407 for (lst::const_iterator it = repl_lst.begin(); it != repl_lst.end(); ++it) {
408 if (it->op(0).is_equal(global_wildcard))
409 return rest.is_equal(it->op(1));
411 repl_lst.append(global_wildcard == rest);
416 // No global wildcard, then the match fails if there are any
417 // unmatched terms left
421 return inherited::match(pattern, repl_lst);
424 ex expairseq::subs(const exmap & m, unsigned options) const
426 std::auto_ptr<epvector> vp = subschildren(m, options);
428 return ex_to<basic>(thisexpairseq(vp, overall_coeff));
429 else if ((options & subs_options::algebraic) && is_exactly_a<mul>(*this))
430 return static_cast<const mul *>(this)->algebraic_subs_mul(m, options);
432 return subs_one_level(m, options);
437 int expairseq::compare_same_type(const basic &other) const
439 GINAC_ASSERT(is_a<expairseq>(other));
440 const expairseq &o = static_cast<const expairseq &>(other);
444 // compare number of elements
445 if (seq.size() != o.seq.size())
446 return (seq.size()<o.seq.size()) ? -1 : 1;
448 // compare overall_coeff
449 cmpval = overall_coeff.compare(o.overall_coeff);
453 #if EXPAIRSEQ_USE_HASHTAB
454 GINAC_ASSERT(hashtabsize==o.hashtabsize);
455 if (hashtabsize==0) {
456 #endif // EXPAIRSEQ_USE_HASHTAB
457 epvector::const_iterator cit1 = seq.begin();
458 epvector::const_iterator cit2 = o.seq.begin();
459 epvector::const_iterator last1 = seq.end();
460 epvector::const_iterator last2 = o.seq.end();
462 for (; (cit1!=last1)&&(cit2!=last2); ++cit1, ++cit2) {
463 cmpval = (*cit1).compare(*cit2);
464 if (cmpval!=0) return cmpval;
467 GINAC_ASSERT(cit1==last1);
468 GINAC_ASSERT(cit2==last2);
471 #if EXPAIRSEQ_USE_HASHTAB
474 // compare number of elements in each hashtab entry
475 for (unsigned i=0; i<hashtabsize; ++i) {
476 unsigned cursize=hashtab[i].size();
477 if (cursize != o.hashtab[i].size())
478 return (cursize < o.hashtab[i].size()) ? -1 : 1;
481 // compare individual (sorted) hashtab entries
482 for (unsigned i=0; i<hashtabsize; ++i) {
483 unsigned sz = hashtab[i].size();
485 const epplist &eppl1 = hashtab[i];
486 const epplist &eppl2 = o.hashtab[i];
487 epplist::const_iterator it1 = eppl1.begin();
488 epplist::const_iterator it2 = eppl2.begin();
489 while (it1!=eppl1.end()) {
490 cmpval = (*(*it1)).compare(*(*it2));
500 #endif // EXPAIRSEQ_USE_HASHTAB
503 bool expairseq::is_equal_same_type(const basic &other) const
505 const expairseq &o = static_cast<const expairseq &>(other);
507 // compare number of elements
508 if (seq.size()!=o.seq.size())
511 // compare overall_coeff
512 if (!overall_coeff.is_equal(o.overall_coeff))
515 #if EXPAIRSEQ_USE_HASHTAB
516 // compare number of elements in each hashtab entry
517 if (hashtabsize!=o.hashtabsize) {
518 std::cout << "this:" << std::endl;
519 print(print_tree(std::cout));
520 std::cout << "other:" << std::endl;
521 other.print(print_tree(std::cout));
524 GINAC_ASSERT(hashtabsize==o.hashtabsize);
526 if (hashtabsize==0) {
527 #endif // EXPAIRSEQ_USE_HASHTAB
528 epvector::const_iterator cit1 = seq.begin();
529 epvector::const_iterator cit2 = o.seq.begin();
530 epvector::const_iterator last1 = seq.end();
532 while (cit1!=last1) {
533 if (!(*cit1).is_equal(*cit2)) return false;
539 #if EXPAIRSEQ_USE_HASHTAB
542 for (unsigned i=0; i<hashtabsize; ++i) {
543 if (hashtab[i].size() != o.hashtab[i].size())
547 // compare individual sorted hashtab entries
548 for (unsigned i=0; i<hashtabsize; ++i) {
549 unsigned sz = hashtab[i].size();
551 const epplist &eppl1 = hashtab[i];
552 const epplist &eppl2 = o.hashtab[i];
553 epplist::const_iterator it1 = eppl1.begin();
554 epplist::const_iterator it2 = eppl2.begin();
555 while (it1!=eppl1.end()) {
556 if (!(*(*it1)).is_equal(*(*it2))) return false;
564 #endif // EXPAIRSEQ_USE_HASHTAB
567 unsigned expairseq::return_type() const
569 return return_types::noncommutative_composite;
572 unsigned expairseq::calchash() const
574 unsigned v = golden_ratio_hash((unsigned)this->tinfo());
575 epvector::const_iterator i = seq.begin();
576 const epvector::const_iterator end = seq.end();
578 v ^= i->rest.gethash();
579 #if !EXPAIRSEQ_USE_HASHTAB
580 // rotation spoils commutativity!
582 v ^= i->coeff.gethash();
583 #endif // !EXPAIRSEQ_USE_HASHTAB
587 v ^= overall_coeff.gethash();
589 // store calculated hash value only if object is already evaluated
590 if (flags &status_flags::evaluated) {
591 setflag(status_flags::hash_calculated);
598 ex expairseq::expand(unsigned options) const
600 std::auto_ptr<epvector> vp = expandchildren(options);
602 return thisexpairseq(vp, overall_coeff);
604 // The terms have not changed, so it is safe to declare this expanded
605 return (options == 0) ? setflag(status_flags::expanded) : *this;
610 // new virtual functions which can be overridden by derived classes
615 /** Create an object of this type.
616 * This method works similar to a constructor. It is useful because expairseq
617 * has (at least) two possible different semantics but we want to inherit
618 * methods thus avoiding code duplication. Sometimes a method in expairseq
619 * has to create a new one of the same semantics, which cannot be done by a
620 * ctor because the name (add, mul,...) is unknown on the expaiseq level. In
621 * order for this trick to work a derived class must of course override this
623 ex expairseq::thisexpairseq(const epvector &v, const ex &oc) const
625 return expairseq(v, oc);
628 ex expairseq::thisexpairseq(std::auto_ptr<epvector> vp, const ex &oc) const
630 return expairseq(vp, oc);
633 void expairseq::printpair(const print_context & c, const expair & p, unsigned upper_precedence) const
636 p.rest.print(c, precedence());
638 p.coeff.print(c, precedence());
642 void expairseq::printseq(const print_context & c, char delim,
643 unsigned this_precedence,
644 unsigned upper_precedence) const
646 if (this_precedence <= upper_precedence)
648 epvector::const_iterator it, it_last = seq.end() - 1;
649 for (it=seq.begin(); it!=it_last; ++it) {
650 printpair(c, *it, this_precedence);
653 printpair(c, *it, this_precedence);
654 if (!overall_coeff.is_equal(default_overall_coeff())) {
656 overall_coeff.print(c, this_precedence);
659 if (this_precedence <= upper_precedence)
664 /** Form an expair from an ex, using the corresponding semantics.
665 * @see expairseq::recombine_pair_to_ex() */
666 expair expairseq::split_ex_to_pair(const ex &e) const
668 return expair(e,_ex1);
672 expair expairseq::combine_ex_with_coeff_to_pair(const ex &e,
675 GINAC_ASSERT(is_exactly_a<numeric>(c));
681 expair expairseq::combine_pair_with_coeff_to_pair(const expair &p,
684 GINAC_ASSERT(is_exactly_a<numeric>(p.coeff));
685 GINAC_ASSERT(is_exactly_a<numeric>(c));
687 return expair(p.rest,ex_to<numeric>(p.coeff).mul_dyn(ex_to<numeric>(c)));
691 /** Form an ex out of an expair, using the corresponding semantics.
692 * @see expairseq::split_ex_to_pair() */
693 ex expairseq::recombine_pair_to_ex(const expair &p) const
695 return lst(p.rest,p.coeff);
698 bool expairseq::expair_needs_further_processing(epp it)
700 #if EXPAIRSEQ_USE_HASHTAB
701 //# error "FIXME: expair_needs_further_processing not yet implemented for hashtabs, sorry. A.F."
702 #endif // EXPAIRSEQ_USE_HASHTAB
706 ex expairseq::default_overall_coeff() const
711 void expairseq::combine_overall_coeff(const ex &c)
713 GINAC_ASSERT(is_exactly_a<numeric>(overall_coeff));
714 GINAC_ASSERT(is_exactly_a<numeric>(c));
715 overall_coeff = ex_to<numeric>(overall_coeff).add_dyn(ex_to<numeric>(c));
718 void expairseq::combine_overall_coeff(const ex &c1, const ex &c2)
720 GINAC_ASSERT(is_exactly_a<numeric>(overall_coeff));
721 GINAC_ASSERT(is_exactly_a<numeric>(c1));
722 GINAC_ASSERT(is_exactly_a<numeric>(c2));
723 overall_coeff = ex_to<numeric>(overall_coeff).
724 add_dyn(ex_to<numeric>(c1).mul(ex_to<numeric>(c2)));
727 bool expairseq::can_make_flat(const expair &p) const
734 // non-virtual functions in this class
737 void expairseq::construct_from_2_ex_via_exvector(const ex &lh, const ex &rh)
743 construct_from_exvector(v);
744 #if EXPAIRSEQ_USE_HASHTAB
745 GINAC_ASSERT((hashtabsize==0)||(hashtabsize>=minhashtabsize));
746 GINAC_ASSERT(hashtabsize==calc_hashtabsize(seq.size()));
747 #endif // EXPAIRSEQ_USE_HASHTAB
750 void expairseq::construct_from_2_ex(const ex &lh, const ex &rh)
752 if (ex_to<basic>(lh).tinfo()==this->tinfo()) {
753 if (ex_to<basic>(rh).tinfo()==this->tinfo()) {
754 #if EXPAIRSEQ_USE_HASHTAB
755 unsigned totalsize = ex_to<expairseq>(lh).seq.size() +
756 ex_to<expairseq>(rh).seq.size();
757 if (calc_hashtabsize(totalsize)!=0) {
758 construct_from_2_ex_via_exvector(lh,rh);
760 #endif // EXPAIRSEQ_USE_HASHTAB
763 ex newrh=rename_dummy_indices_uniquely(lh, rh);
764 construct_from_2_expairseq(ex_to<expairseq>(lh),
765 ex_to<expairseq>(newrh));
768 construct_from_2_expairseq(ex_to<expairseq>(lh),
769 ex_to<expairseq>(rh));
770 #if EXPAIRSEQ_USE_HASHTAB
772 #endif // EXPAIRSEQ_USE_HASHTAB
775 #if EXPAIRSEQ_USE_HASHTAB
776 unsigned totalsize = ex_to<expairseq>(lh).seq.size()+1;
777 if (calc_hashtabsize(totalsize)!=0) {
778 construct_from_2_ex_via_exvector(lh, rh);
780 #endif // EXPAIRSEQ_USE_HASHTAB
781 construct_from_expairseq_ex(ex_to<expairseq>(lh), rh);
782 #if EXPAIRSEQ_USE_HASHTAB
784 #endif // EXPAIRSEQ_USE_HASHTAB
787 } else if (ex_to<basic>(rh).tinfo()==this->tinfo()) {
788 #if EXPAIRSEQ_USE_HASHTAB
789 unsigned totalsize=ex_to<expairseq>(rh).seq.size()+1;
790 if (calc_hashtabsize(totalsize)!=0) {
791 construct_from_2_ex_via_exvector(lh,rh);
793 #endif // EXPAIRSEQ_USE_HASHTAB
794 construct_from_expairseq_ex(ex_to<expairseq>(rh),lh);
795 #if EXPAIRSEQ_USE_HASHTAB
797 #endif // EXPAIRSEQ_USE_HASHTAB
801 #if EXPAIRSEQ_USE_HASHTAB
802 if (calc_hashtabsize(2)!=0) {
803 construct_from_2_ex_via_exvector(lh,rh);
807 #endif // EXPAIRSEQ_USE_HASHTAB
809 if (is_exactly_a<numeric>(lh)) {
810 if (is_exactly_a<numeric>(rh)) {
811 combine_overall_coeff(lh);
812 combine_overall_coeff(rh);
814 combine_overall_coeff(lh);
815 seq.push_back(split_ex_to_pair(rh));
818 if (is_exactly_a<numeric>(rh)) {
819 combine_overall_coeff(rh);
820 seq.push_back(split_ex_to_pair(lh));
822 expair p1 = split_ex_to_pair(lh);
823 expair p2 = split_ex_to_pair(rh);
825 int cmpval = p1.rest.compare(p2.rest);
827 p1.coeff = ex_to<numeric>(p1.coeff).add_dyn(ex_to<numeric>(p2.coeff));
828 if (!ex_to<numeric>(p1.coeff).is_zero()) {
829 // no further processing is necessary, since this
830 // one element will usually be recombined in eval()
847 void expairseq::construct_from_2_expairseq(const expairseq &s1,
850 combine_overall_coeff(s1.overall_coeff);
851 combine_overall_coeff(s2.overall_coeff);
853 epvector::const_iterator first1 = s1.seq.begin();
854 epvector::const_iterator last1 = s1.seq.end();
855 epvector::const_iterator first2 = s2.seq.begin();
856 epvector::const_iterator last2 = s2.seq.end();
858 seq.reserve(s1.seq.size()+s2.seq.size());
860 bool needs_further_processing=false;
862 while (first1!=last1 && first2!=last2) {
863 int cmpval = (*first1).rest.compare((*first2).rest);
867 const numeric &newcoeff = ex_to<numeric>(first1->coeff).
868 add(ex_to<numeric>(first2->coeff));
869 if (!newcoeff.is_zero()) {
870 seq.push_back(expair(first1->rest,newcoeff));
871 if (expair_needs_further_processing(seq.end()-1)) {
872 needs_further_processing = true;
877 } else if (cmpval<0) {
878 seq.push_back(*first1);
881 seq.push_back(*first2);
886 while (first1!=last1) {
887 seq.push_back(*first1);
890 while (first2!=last2) {
891 seq.push_back(*first2);
895 if (needs_further_processing) {
898 construct_from_epvector(v);
902 void expairseq::construct_from_expairseq_ex(const expairseq &s,
905 combine_overall_coeff(s.overall_coeff);
906 if (is_exactly_a<numeric>(e)) {
907 combine_overall_coeff(e);
912 epvector::const_iterator first = s.seq.begin();
913 epvector::const_iterator last = s.seq.end();
914 expair p = split_ex_to_pair(e);
916 seq.reserve(s.seq.size()+1);
917 bool p_pushed = false;
919 bool needs_further_processing=false;
921 // merge p into s.seq
922 while (first!=last) {
923 int cmpval = (*first).rest.compare(p.rest);
926 const numeric &newcoeff = ex_to<numeric>(first->coeff).
927 add(ex_to<numeric>(p.coeff));
928 if (!newcoeff.is_zero()) {
929 seq.push_back(expair(first->rest,newcoeff));
930 if (expair_needs_further_processing(seq.end()-1))
931 needs_further_processing = true;
936 } else if (cmpval<0) {
937 seq.push_back(*first);
947 // while loop exited because p was pushed, now push rest of s.seq
948 while (first!=last) {
949 seq.push_back(*first);
953 // while loop exited because s.seq was pushed, now push p
957 if (needs_further_processing) {
960 construct_from_epvector(v);
964 void expairseq::construct_from_exvector(const exvector &v)
966 // simplifications: +(a,+(b,c),d) -> +(a,b,c,d) (associativity)
967 // +(d,b,c,a) -> +(a,b,c,d) (canonicalization)
968 // +(...,x,*(x,c1),*(x,c2)) -> +(...,*(x,1+c1+c2)) (c1, c2 numeric())
969 // (same for (+,*) -> (*,^)
972 #if EXPAIRSEQ_USE_HASHTAB
973 combine_same_terms();
976 combine_same_terms_sorted_seq();
977 #endif // EXPAIRSEQ_USE_HASHTAB
980 void expairseq::construct_from_epvector(const epvector &v)
982 // simplifications: +(a,+(b,c),d) -> +(a,b,c,d) (associativity)
983 // +(d,b,c,a) -> +(a,b,c,d) (canonicalization)
984 // +(...,x,*(x,c1),*(x,c2)) -> +(...,*(x,1+c1+c2)) (c1, c2 numeric())
985 // (same for (+,*) -> (*,^)
988 #if EXPAIRSEQ_USE_HASHTAB
989 combine_same_terms();
992 combine_same_terms_sorted_seq();
993 #endif // EXPAIRSEQ_USE_HASHTAB
996 /** Combine this expairseq with argument exvector.
997 * It cares for associativity as well as for special handling of numerics. */
998 void expairseq::make_flat(const exvector &v)
1000 exvector::const_iterator cit;
1002 // count number of operands which are of same expairseq derived type
1003 // and their cumulative number of operands
1004 int nexpairseqs = 0;
1008 while (cit!=v.end()) {
1009 if (ex_to<basic>(*cit).tinfo()==this->tinfo()) {
1011 noperands += ex_to<expairseq>(*cit).seq.size();
1016 // reserve seq and coeffseq which will hold all operands
1017 seq.reserve(v.size()+noperands-nexpairseqs);
1019 // copy elements and split off numerical part
1020 exvector dummy_indices;
1022 while (cit!=v.end()) {
1023 if (ex_to<basic>(*cit).tinfo()==this->tinfo()) {
1024 const expairseq *subseqref;
1028 exvector dummies_of_factor = get_all_dummy_indices(*cit);
1029 sort(dummies_of_factor.begin(), dummies_of_factor.end(), ex_is_less());
1030 newfactor = rename_dummy_indices_uniquely(dummy_indices, dummies_of_factor, *cit);
1031 subseqref = &(ex_to<expairseq>(newfactor));
1032 exvector new_dummy_indices;
1033 set_union(dummy_indices.begin(), dummy_indices.end(), dummies_of_factor.begin(), dummies_of_factor.end(), std::back_insert_iterator<exvector>(new_dummy_indices), ex_is_less());
1034 dummy_indices.swap(new_dummy_indices);
1037 subseqref = &ex_to<expairseq>(*cit);
1038 combine_overall_coeff(subseqref->overall_coeff);
1039 epvector::const_iterator cit_s = subseqref->seq.begin();
1040 while (cit_s!=subseqref->seq.end()) {
1041 seq.push_back(*cit_s);
1045 if (is_exactly_a<numeric>(*cit))
1046 combine_overall_coeff(*cit);
1048 seq.push_back(split_ex_to_pair(*cit));
1054 /** Combine this expairseq with argument epvector.
1055 * It cares for associativity as well as for special handling of numerics. */
1056 void expairseq::make_flat(const epvector &v)
1058 epvector::const_iterator cit;
1060 // count number of operands which are of same expairseq derived type
1061 // and their cumulative number of operands
1062 int nexpairseqs = 0;
1066 while (cit!=v.end()) {
1067 if (ex_to<basic>(cit->rest).tinfo()==this->tinfo()) {
1069 noperands += ex_to<expairseq>(cit->rest).seq.size();
1074 // reserve seq and coeffseq which will hold all operands
1075 seq.reserve(v.size()+noperands-nexpairseqs);
1077 // copy elements and split off numerical part
1079 while (cit!=v.end()) {
1080 if (ex_to<basic>(cit->rest).tinfo()==this->tinfo() &&
1081 this->can_make_flat(*cit)) {
1082 const expairseq &subseqref = ex_to<expairseq>(cit->rest);
1083 combine_overall_coeff(ex_to<numeric>(subseqref.overall_coeff),
1084 ex_to<numeric>(cit->coeff));
1085 epvector::const_iterator cit_s = subseqref.seq.begin();
1086 while (cit_s!=subseqref.seq.end()) {
1087 seq.push_back(expair(cit_s->rest,
1088 ex_to<numeric>(cit_s->coeff).mul_dyn(ex_to<numeric>(cit->coeff))));
1089 //seq.push_back(combine_pair_with_coeff_to_pair(*cit_s,
1094 if (cit->is_canonical_numeric())
1095 combine_overall_coeff(cit->rest);
1097 seq.push_back(*cit);
1103 /** Brings this expairseq into a sorted (canonical) form. */
1104 void expairseq::canonicalize()
1106 std::sort(seq.begin(), seq.end(), expair_rest_is_less());
1110 /** Compact a presorted expairseq by combining all matching expairs to one
1111 * each. On an add object, this is responsible for 2*x+3*x+y -> 5*x+y, for
1113 void expairseq::combine_same_terms_sorted_seq()
1118 bool needs_further_processing = false;
1120 epvector::iterator itin1 = seq.begin();
1121 epvector::iterator itin2 = itin1+1;
1122 epvector::iterator itout = itin1;
1123 epvector::iterator last = seq.end();
1124 // must_copy will be set to true the first time some combination is
1125 // possible from then on the sequence has changed and must be compacted
1126 bool must_copy = false;
1127 while (itin2!=last) {
1128 if (itin1->rest.compare(itin2->rest)==0) {
1129 itin1->coeff = ex_to<numeric>(itin1->coeff).
1130 add_dyn(ex_to<numeric>(itin2->coeff));
1131 if (expair_needs_further_processing(itin1))
1132 needs_further_processing = true;
1135 if (!ex_to<numeric>(itin1->coeff).is_zero()) {
1144 if (!ex_to<numeric>(itin1->coeff).is_zero()) {
1150 seq.erase(itout,last);
1152 if (needs_further_processing) {
1155 construct_from_epvector(v);
1159 #if EXPAIRSEQ_USE_HASHTAB
1161 unsigned expairseq::calc_hashtabsize(unsigned sz) const
1164 unsigned nearest_power_of_2 = 1 << log2(sz);
1165 // if (nearest_power_of_2 < maxhashtabsize/hashtabfactor) {
1166 // size = nearest_power_of_2*hashtabfactor;
1167 size = nearest_power_of_2/hashtabfactor;
1168 if (size<minhashtabsize)
1171 // hashtabsize must be a power of 2
1172 GINAC_ASSERT((1U << log2(size))==size);
1176 unsigned expairseq::calc_hashindex(const ex &e) const
1178 // calculate hashindex
1180 if (is_a<numeric>(e)) {
1181 hashindex = hashmask;
1183 hashindex = e.gethash() & hashmask;
1184 // last hashtab entry is reserved for numerics
1185 if (hashindex==hashmask) hashindex = 0;
1187 GINAC_ASSERT((hashindex<hashtabsize)||(hashtabsize==0));
1191 void expairseq::shrink_hashtab()
1193 unsigned new_hashtabsize;
1194 while (hashtabsize!=(new_hashtabsize=calc_hashtabsize(seq.size()))) {
1195 GINAC_ASSERT(new_hashtabsize<hashtabsize);
1196 if (new_hashtabsize==0) {
1203 // shrink by a factor of 2
1204 unsigned half_hashtabsize = hashtabsize/2;
1205 for (unsigned i=0; i<half_hashtabsize-1; ++i)
1206 hashtab[i].merge(hashtab[i+half_hashtabsize],epp_is_less());
1207 // special treatment for numeric hashes
1208 hashtab[0].merge(hashtab[half_hashtabsize-1],epp_is_less());
1209 hashtab[half_hashtabsize-1] = hashtab[hashtabsize-1];
1210 hashtab.resize(half_hashtabsize);
1211 hashtabsize = half_hashtabsize;
1212 hashmask = hashtabsize-1;
1216 void expairseq::remove_hashtab_entry(epvector::const_iterator element)
1219 return; // nothing to do
1221 // calculate hashindex of element to be deleted
1222 unsigned hashindex = calc_hashindex((*element).rest);
1224 // find it in hashtab and remove it
1225 epplist &eppl = hashtab[hashindex];
1226 epplist::iterator epplit = eppl.begin();
1227 bool erased = false;
1228 while (epplit!=eppl.end()) {
1229 if (*epplit == element) {
1237 std::cout << "tried to erase " << element-seq.begin() << std::endl;
1238 std::cout << "size " << seq.end()-seq.begin() << std::endl;
1240 unsigned hashindex = calc_hashindex(element->rest);
1241 epplist &eppl = hashtab[hashindex];
1242 epplist::iterator epplit = eppl.begin();
1243 bool erased = false;
1244 while (epplit!=eppl.end()) {
1245 if (*epplit == element) {
1252 GINAC_ASSERT(erased);
1254 GINAC_ASSERT(erased);
1257 void expairseq::move_hashtab_entry(epvector::const_iterator oldpos,
1258 epvector::iterator newpos)
1260 GINAC_ASSERT(hashtabsize!=0);
1262 // calculate hashindex of element which was moved
1263 unsigned hashindex=calc_hashindex((*newpos).rest);
1265 // find it in hashtab and modify it
1266 epplist &eppl = hashtab[hashindex];
1267 epplist::iterator epplit = eppl.begin();
1268 while (epplit!=eppl.end()) {
1269 if (*epplit == oldpos) {
1275 GINAC_ASSERT(epplit!=eppl.end());
1278 void expairseq::sorted_insert(epplist &eppl, epvector::const_iterator elem)
1280 epplist::const_iterator current = eppl.begin();
1281 while ((current!=eppl.end()) && ((*current)->is_less(*elem))) {
1284 eppl.insert(current,elem);
1287 void expairseq::build_hashtab_and_combine(epvector::iterator &first_numeric,
1288 epvector::iterator &last_non_zero,
1289 std::vector<bool> &touched,
1290 unsigned &number_of_zeroes)
1292 epp current = seq.begin();
1294 while (current!=first_numeric) {
1295 if (is_exactly_a<numeric>(current->rest)) {
1297 iter_swap(current,first_numeric);
1299 // calculate hashindex
1300 unsigned currenthashindex = calc_hashindex(current->rest);
1302 // test if there is already a matching expair in the hashtab-list
1303 epplist &eppl=hashtab[currenthashindex];
1304 epplist::iterator epplit = eppl.begin();
1305 while (epplit!=eppl.end()) {
1306 if (current->rest.is_equal((*epplit)->rest))
1310 if (epplit==eppl.end()) {
1311 // no matching expair found, append this to end of list
1312 sorted_insert(eppl,current);
1315 // epplit points to a matching expair, combine it with current
1316 (*epplit)->coeff = ex_to<numeric>((*epplit)->coeff).
1317 add_dyn(ex_to<numeric>(current->coeff));
1319 // move obsolete current expair to end by swapping with last_non_zero element
1320 // if this was a numeric, it is swapped with the expair before first_numeric
1321 iter_swap(current,last_non_zero);
1323 if (first_numeric!=last_non_zero) iter_swap(first_numeric,current);
1326 // test if combined term has coeff 0 and can be removed is done later
1327 touched[(*epplit)-seq.begin()] = true;
1333 void expairseq::drop_coeff_0_terms(epvector::iterator &first_numeric,
1334 epvector::iterator &last_non_zero,
1335 std::vector<bool> &touched,
1336 unsigned &number_of_zeroes)
1338 // move terms with coeff 0 to end and remove them from hashtab
1339 // check only those elements which have been touched
1340 epp current = seq.begin();
1342 while (current!=first_numeric) {
1346 } else if (!ex_to<numeric>((*current).coeff).is_zero()) {
1350 remove_hashtab_entry(current);
1352 // move element to the end, unless it is already at the end
1353 if (current!=last_non_zero) {
1354 iter_swap(current,last_non_zero);
1356 bool numeric_swapped = first_numeric!=last_non_zero;
1357 if (numeric_swapped)
1358 iter_swap(first_numeric,current);
1359 epvector::iterator changed_entry;
1361 if (numeric_swapped)
1362 changed_entry = first_numeric;
1364 changed_entry = last_non_zero;
1369 if (first_numeric!=current) {
1371 // change entry in hashtab which referred to first_numeric or last_non_zero to current
1372 move_hashtab_entry(changed_entry,current);
1373 touched[current-seq.begin()] = touched[changed_entry-seq.begin()];
1382 GINAC_ASSERT(i==current-seq.begin());
1385 /** True if one of the coeffs vanishes, otherwise false.
1386 * This would be an invariant violation, so this should only be used for
1387 * debugging purposes. */
1388 bool expairseq::has_coeff_0() const
1390 epvector::const_iterator i = seq.begin(), end = seq.end();
1392 if (i->coeff.is_zero())
1399 void expairseq::add_numerics_to_hashtab(epvector::iterator first_numeric,
1400 epvector::const_iterator last_non_zero)
1402 if (first_numeric == seq.end()) return; // no numerics
1404 epvector::const_iterator current = first_numeric, last = last_non_zero + 1;
1405 while (current != last) {
1406 sorted_insert(hashtab[hashmask], current);
1411 void expairseq::combine_same_terms()
1413 // combine same terms, drop term with coeff 0, move numerics to end
1415 // calculate size of hashtab
1416 hashtabsize = calc_hashtabsize(seq.size());
1418 // hashtabsize is a power of 2
1419 hashmask = hashtabsize-1;
1423 hashtab.resize(hashtabsize);
1425 if (hashtabsize==0) {
1427 combine_same_terms_sorted_seq();
1428 GINAC_ASSERT(!has_coeff_0());
1432 // iterate through seq, move numerics to end,
1433 // fill hashtab and combine same terms
1434 epvector::iterator first_numeric = seq.end();
1435 epvector::iterator last_non_zero = seq.end()-1;
1437 size_t num = seq.size();
1438 std::vector<bool> touched(num);
1440 unsigned number_of_zeroes = 0;
1442 GINAC_ASSERT(!has_coeff_0());
1443 build_hashtab_and_combine(first_numeric,last_non_zero,touched,number_of_zeroes);
1445 // there should not be any terms with coeff 0 from the beginning,
1446 // so it should be safe to skip this step
1447 if (number_of_zeroes!=0) {
1448 drop_coeff_0_terms(first_numeric,last_non_zero,touched,number_of_zeroes);
1451 add_numerics_to_hashtab(first_numeric,last_non_zero);
1453 // pop zero elements
1454 for (unsigned i=0; i<number_of_zeroes; ++i) {
1458 // shrink hashtabsize to calculated value
1459 GINAC_ASSERT(!has_coeff_0());
1463 GINAC_ASSERT(!has_coeff_0());
1466 #endif // EXPAIRSEQ_USE_HASHTAB
1468 /** Check if this expairseq is in sorted (canonical) form. Useful mainly for
1469 * debugging or in assertions since being sorted is an invariance. */
1470 bool expairseq::is_canonical() const
1472 if (seq.size() <= 1)
1475 #if EXPAIRSEQ_USE_HASHTAB
1476 if (hashtabsize > 0) return 1; // not canoncalized
1477 #endif // EXPAIRSEQ_USE_HASHTAB
1479 epvector::const_iterator it = seq.begin(), itend = seq.end();
1480 epvector::const_iterator it_last = it;
1481 for (++it; it!=itend; it_last=it, ++it) {
1482 if (!(it_last->is_less(*it) || it_last->is_equal(*it))) {
1483 if (!is_exactly_a<numeric>(it_last->rest) ||
1484 !is_exactly_a<numeric>(it->rest)) {
1485 // double test makes it easier to set a breakpoint...
1486 if (!is_exactly_a<numeric>(it_last->rest) ||
1487 !is_exactly_a<numeric>(it->rest)) {
1488 printpair(std::clog, *it_last, 0);
1490 printpair(std::clog, *it, 0);
1492 std::clog << "pair1:" << std::endl;
1493 it_last->rest.print(print_tree(std::clog));
1494 it_last->coeff.print(print_tree(std::clog));
1495 std::clog << "pair2:" << std::endl;
1496 it->rest.print(print_tree(std::clog));
1497 it->coeff.print(print_tree(std::clog));
1507 /** Member-wise expand the expairs in this sequence.
1509 * @see expairseq::expand()
1510 * @return pointer to epvector containing expanded pairs or zero pointer,
1511 * if no members were changed. */
1512 std::auto_ptr<epvector> expairseq::expandchildren(unsigned options) const
1514 const epvector::const_iterator last = seq.end();
1515 epvector::const_iterator cit = seq.begin();
1517 const ex &expanded_ex = cit->rest.expand(options);
1518 if (!are_ex_trivially_equal(cit->rest,expanded_ex)) {
1520 // something changed, copy seq, eval and return it
1521 std::auto_ptr<epvector> s(new epvector);
1522 s->reserve(seq.size());
1524 // copy parts of seq which are known not to have changed
1525 epvector::const_iterator cit2 = seq.begin();
1527 s->push_back(*cit2);
1531 // copy first changed element
1532 s->push_back(combine_ex_with_coeff_to_pair(expanded_ex,
1537 while (cit2!=last) {
1538 s->push_back(combine_ex_with_coeff_to_pair(cit2->rest.expand(options),
1547 return std::auto_ptr<epvector>(0); // signalling nothing has changed
1551 /** Member-wise evaluate the expairs in this sequence.
1553 * @see expairseq::eval()
1554 * @return pointer to epvector containing evaluated pairs or zero pointer,
1555 * if no members were changed. */
1556 std::auto_ptr<epvector> expairseq::evalchildren(int level) const
1558 // returns a NULL pointer if nothing had to be evaluated
1559 // returns a pointer to a newly created epvector otherwise
1560 // (which has to be deleted somewhere else)
1563 return std::auto_ptr<epvector>(0);
1565 if (level == -max_recursion_level)
1566 throw(std::runtime_error("max recursion level reached"));
1569 epvector::const_iterator last = seq.end();
1570 epvector::const_iterator cit = seq.begin();
1572 const ex &evaled_ex = cit->rest.eval(level);
1573 if (!are_ex_trivially_equal(cit->rest,evaled_ex)) {
1575 // something changed, copy seq, eval and return it
1576 std::auto_ptr<epvector> s(new epvector);
1577 s->reserve(seq.size());
1579 // copy parts of seq which are known not to have changed
1580 epvector::const_iterator cit2=seq.begin();
1582 s->push_back(*cit2);
1586 // copy first changed element
1587 s->push_back(combine_ex_with_coeff_to_pair(evaled_ex,
1592 while (cit2!=last) {
1593 s->push_back(combine_ex_with_coeff_to_pair(cit2->rest.eval(level),
1602 return std::auto_ptr<epvector>(0); // signalling nothing has changed
1608 safe_inserter(const ex&, const bool disable_renaming=false);
1609 std::auto_ptr<epvector> getseq(){return epv;}
1610 void insert_old_pair(const expair &p)
1614 void insert_new_pair(const expair &p, const ex &orig_ex);
1616 std::auto_ptr<epvector> epv;
1618 exvector dummy_indices;
1619 void update_dummy_indices(const exvector&);
1622 safe_inserter::safe_inserter(const ex&e, const bool disable_renaming)
1625 epv->reserve(e.nops());
1626 dodummies=is_a<mul>(e);
1627 if(disable_renaming)
1630 dummy_indices = get_all_dummy_indices_safely(e);
1631 sort(dummy_indices.begin(), dummy_indices.end(), ex_is_less());
1635 void safe_inserter::update_dummy_indices(const exvector &v)
1637 exvector new_dummy_indices;
1638 set_union(dummy_indices.begin(), dummy_indices.end(), v.begin(), v.end(),
1639 std::back_insert_iterator<exvector>(new_dummy_indices), ex_is_less());
1640 dummy_indices.swap(new_dummy_indices);
1643 void safe_inserter::insert_new_pair(const expair &p, const ex &orig_ex)
1649 exvector dummies_of_factor = get_all_dummy_indices_safely(p.rest);
1650 if(dummies_of_factor.size() == 0) {
1654 sort(dummies_of_factor.begin(), dummies_of_factor.end(), ex_is_less());
1655 exvector dummies_of_orig_ex = get_all_dummy_indices_safely(orig_ex);
1656 sort(dummies_of_orig_ex.begin(), dummies_of_orig_ex.end(), ex_is_less());
1657 exvector new_dummy_indices;
1658 new_dummy_indices.reserve(dummy_indices.size());
1659 set_difference(dummy_indices.begin(), dummy_indices.end(), dummies_of_orig_ex.begin(), dummies_of_orig_ex.end(),
1660 std::back_insert_iterator<exvector>(new_dummy_indices), ex_is_less());
1661 dummy_indices.swap(new_dummy_indices);
1662 ex newfactor = rename_dummy_indices_uniquely(dummy_indices, dummies_of_factor, p.rest);
1663 update_dummy_indices(dummies_of_factor);
1664 epv -> push_back(expair(newfactor, p.coeff));
1667 /** Member-wise substitute in this sequence.
1669 * @see expairseq::subs()
1670 * @return pointer to epvector containing pairs after application of subs,
1671 * or NULL pointer if no members were changed. */
1672 std::auto_ptr<epvector> expairseq::subschildren(const exmap & m, unsigned options) const
1674 // When any of the objects to be substituted is a product or power
1675 // we have to recombine the pairs because the numeric coefficients may
1676 // be part of the search pattern.
1677 if (!(options & (subs_options::pattern_is_product | subs_options::pattern_is_not_product))) {
1679 // Search the list of substitutions and cache our findings
1680 for (exmap::const_iterator it = m.begin(); it != m.end(); ++it) {
1681 if (is_exactly_a<mul>(it->first) || is_exactly_a<power>(it->first)) {
1682 options |= subs_options::pattern_is_product;
1686 if (!(options & subs_options::pattern_is_product))
1687 options |= subs_options::pattern_is_not_product;
1690 if (options & subs_options::pattern_is_product) {
1692 // Substitute in the recombined pairs
1693 epvector::const_iterator cit = seq.begin(), last = seq.end();
1694 while (cit != last) {
1696 const ex &orig_ex = recombine_pair_to_ex(*cit);
1697 const ex &subsed_ex = orig_ex.subs(m, options);
1698 if (!are_ex_trivially_equal(orig_ex, subsed_ex)) {
1700 // Something changed, copy seq, subs and return it
1701 safe_inserter s(*this, options & subs_options::no_index_renaming);
1703 // Copy parts of seq which are known not to have changed
1704 for(epvector::const_iterator i=seq.begin(); i!=cit; ++i)
1705 s.insert_old_pair(*i);
1707 // Copy first changed element
1708 s.insert_new_pair(split_ex_to_pair(subsed_ex), orig_ex);
1712 while (cit != last) {
1713 ex orig_ex = recombine_pair_to_ex(*cit);
1714 ex subsed_ex = orig_ex.subs(m, options);
1715 if(are_ex_trivially_equal(orig_ex, subsed_ex))
1716 s.insert_old_pair(*cit);
1718 s.insert_new_pair(split_ex_to_pair(subsed_ex), orig_ex);
1729 // Substitute only in the "rest" part of the pairs
1730 epvector::const_iterator cit = seq.begin(), last = seq.end();
1731 while (cit != last) {
1733 const ex &subsed_ex = cit->rest.subs(m, options);
1734 if (!are_ex_trivially_equal(cit->rest, subsed_ex)) {
1736 // Something changed, copy seq, subs and return it
1737 safe_inserter s(*this, options & subs_options::no_index_renaming);
1739 // Copy parts of seq which are known not to have changed
1740 for(epvector::const_iterator i=seq.begin(); i!=cit; ++i)
1741 s.insert_old_pair(*i);
1743 // Copy first changed element
1744 s.insert_new_pair(combine_ex_with_coeff_to_pair(subsed_ex, cit->coeff), cit->rest);
1748 while (cit != last) {
1749 const ex &orig_ex = cit->rest;
1750 const ex &subsed_ex = cit->rest.subs(m, options);
1751 if(are_ex_trivially_equal(orig_ex, subsed_ex))
1752 s.insert_old_pair(*cit);
1754 s.insert_new_pair(combine_ex_with_coeff_to_pair(subsed_ex, cit->coeff), orig_ex);
1764 // Nothing has changed
1765 return std::auto_ptr<epvector>(0);
1769 // static member variables
1772 #if EXPAIRSEQ_USE_HASHTAB
1773 unsigned expairseq::maxhashtabsize = 0x4000000U;
1774 unsigned expairseq::minhashtabsize = 0x1000U;
1775 unsigned expairseq::hashtabfactor = 1;
1776 #endif // EXPAIRSEQ_USE_HASHTAB
1778 } // namespace GiNaC