1 /** @file expairseq.cpp
3 * Implementation of sequences of expression pairs. */
6 * GiNaC Copyright (C) 1999-2011 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
23 #include "expairseq.h"
28 #include "relational.h"
31 #include "operators.h"
33 #include "hash_seed.h"
37 #if EXPAIRSEQ_USE_HASHTAB
39 #endif // EXPAIRSEQ_USE_HASHTAB
48 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(expairseq, basic,
49 print_func<print_context>(&expairseq::do_print).
50 print_func<print_tree>(&expairseq::do_print_tree))
60 bool operator()(const epp &lh, const epp &rh) const
62 return (*lh).is_less(*rh);
67 // default constructor
72 expairseq::expairseq()
73 #if EXPAIRSEQ_USE_HASHTAB
75 #endif // EXPAIRSEQ_USE_HASHTAB
81 /** For use by copy ctor and assignment operator. */
82 void expairseq::copy(const expairseq &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
108 // other constructors
111 expairseq::expairseq(const ex &lh, const ex &rh)
113 construct_from_2_ex(lh,rh);
114 GINAC_ASSERT(is_canonical());
117 expairseq::expairseq(const exvector &v)
119 construct_from_exvector(v);
120 GINAC_ASSERT(is_canonical());
123 expairseq::expairseq(const epvector &v, const ex &oc, bool do_index_renaming)
126 GINAC_ASSERT(is_a<numeric>(oc));
127 construct_from_epvector(v, do_index_renaming);
128 GINAC_ASSERT(is_canonical());
131 expairseq::expairseq(std::auto_ptr<epvector> vp, const ex &oc, bool do_index_renaming)
134 GINAC_ASSERT(vp.get()!=0);
135 GINAC_ASSERT(is_a<numeric>(oc));
136 construct_from_epvector(*vp, do_index_renaming);
137 GINAC_ASSERT(is_canonical());
144 void expairseq::read_archive(const archive_node &n, lst &sym_lst)
146 inherited::read_archive(n, sym_lst);
147 archive_node::archive_node_cit first = n.find_first("rest");
148 archive_node::archive_node_cit last = n.find_last("coeff");
150 seq.reserve((last-first)/2);
152 for (archive_node::archive_node_cit loc = first; loc < last;) {
155 n.find_ex_by_loc(loc++, rest, sym_lst);
156 n.find_ex_by_loc(loc++, coeff, sym_lst);
157 seq.push_back(expair(rest, coeff));
160 n.find_ex("overall_coeff", overall_coeff, sym_lst);
163 GINAC_ASSERT(is_canonical());
166 void expairseq::archive(archive_node &n) const
168 inherited::archive(n);
169 epvector::const_iterator i = seq.begin(), iend = seq.end();
171 n.add_ex("rest", i->rest);
172 n.add_ex("coeff", i->coeff);
175 n.add_ex("overall_coeff", overall_coeff);
180 // functions overriding virtual functions from base classes
185 void expairseq::do_print(const print_context & c, unsigned level) const
188 printseq(c, ',', precedence(), level);
192 void expairseq::do_print_tree(const print_tree & c, unsigned level) const
194 c.s << std::string(level, ' ') << class_name() << " @" << this
195 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
196 << ", nops=" << nops()
198 size_t num = seq.size();
199 for (size_t i=0; i<num; ++i) {
200 seq[i].rest.print(c, level + c.delta_indent);
201 seq[i].coeff.print(c, level + c.delta_indent);
203 c.s << std::string(level + c.delta_indent, ' ') << "-----" << std::endl;
205 if (!overall_coeff.is_equal(default_overall_coeff())) {
206 c.s << std::string(level + c.delta_indent, ' ') << "-----" << std::endl
207 << std::string(level + c.delta_indent, ' ') << "overall_coeff" << std::endl;
208 overall_coeff.print(c, level + c.delta_indent);
210 c.s << std::string(level + c.delta_indent,' ') << "=====" << std::endl;
211 #if EXPAIRSEQ_USE_HASHTAB
212 c.s << std::string(level + c.delta_indent,' ')
213 << "hashtab size " << hashtabsize << std::endl;
214 if (hashtabsize == 0) return;
216 unsigned count[MAXCOUNT+1];
217 for (int i=0; i<MAXCOUNT+1; ++i)
219 unsigned this_bin_fill;
220 unsigned cum_fill_sq = 0;
221 unsigned cum_fill = 0;
222 for (unsigned i=0; i<hashtabsize; ++i) {
224 if (hashtab[i].size() > 0) {
225 c.s << std::string(level + c.delta_indent, ' ')
226 << "bin " << i << " with entries ";
227 for (epplist::const_iterator it=hashtab[i].begin();
228 it!=hashtab[i].end(); ++it) {
229 c.s << *it-seq.begin() << " ";
233 cum_fill += this_bin_fill;
234 cum_fill_sq += this_bin_fill*this_bin_fill;
236 if (this_bin_fill<MAXCOUNT)
237 ++count[this_bin_fill];
243 double lambda = (1.0*seq.size()) / hashtabsize;
244 for (int k=0; k<MAXCOUNT; ++k) {
247 double prob = std::pow(lambda,k)/fact * std::exp(-lambda);
249 c.s << std::string(level + c.delta_indent, ' ') << "bins with " << k << " entries: "
250 << int(1000.0*count[k]/hashtabsize)/10.0 << "% (expected: "
251 << int(prob*1000)/10.0 << ")" << std::endl;
253 c.s << std::string(level + c.delta_indent, ' ') << "bins with more entries: "
254 << int(1000.0*count[MAXCOUNT]/hashtabsize)/10.0 << "% (expected: "
255 << int((1-cum_prob)*1000)/10.0 << ")" << std::endl;
257 c.s << std::string(level + c.delta_indent, ' ') << "variance: "
258 << 1.0/hashtabsize*cum_fill_sq-(1.0/hashtabsize*cum_fill)*(1.0/hashtabsize*cum_fill)
260 c.s << std::string(level + c.delta_indent, ' ') << "average fill: "
261 << (1.0*cum_fill)/hashtabsize
262 << " (should be equal to " << (1.0*seq.size())/hashtabsize << ")" << std::endl;
263 #endif // EXPAIRSEQ_USE_HASHTAB
266 bool expairseq::info(unsigned inf) const
269 case info_flags::expanded:
270 return (flags & status_flags::expanded);
271 case info_flags::has_indices: {
272 if (flags & status_flags::has_indices)
274 else if (flags & status_flags::has_no_indices)
276 for (epvector::const_iterator i = seq.begin(); i != seq.end(); ++i) {
277 if (i->rest.info(info_flags::has_indices)) {
278 this->setflag(status_flags::has_indices);
279 this->clearflag(status_flags::has_no_indices);
283 this->clearflag(status_flags::has_indices);
284 this->setflag(status_flags::has_no_indices);
288 return inherited::info(inf);
291 size_t expairseq::nops() const
293 if (overall_coeff.is_equal(default_overall_coeff()))
299 ex expairseq::op(size_t i) const
302 return recombine_pair_to_ex(seq[i]);
303 GINAC_ASSERT(!overall_coeff.is_equal(default_overall_coeff()));
304 return overall_coeff;
307 ex expairseq::map(map_function &f) const
309 std::auto_ptr<epvector> v(new epvector);
310 v->reserve(seq.size()+1);
312 epvector::const_iterator cit = seq.begin(), last = seq.end();
313 while (cit != last) {
314 v->push_back(split_ex_to_pair(f(recombine_pair_to_ex(*cit))));
318 if (overall_coeff.is_equal(default_overall_coeff()))
319 return thisexpairseq(v, default_overall_coeff(), true);
321 ex newcoeff = f(overall_coeff);
322 if(is_a<numeric>(newcoeff))
323 return thisexpairseq(v, newcoeff, true);
325 v->push_back(split_ex_to_pair(newcoeff));
326 return thisexpairseq(v, default_overall_coeff(), true);
331 /** Perform coefficient-wise automatic term rewriting rules in this class. */
332 ex expairseq::eval(int level) const
334 if ((level==1) && (flags &status_flags::evaluated))
337 std::auto_ptr<epvector> vp = evalchildren(level);
341 return (new expairseq(vp, overall_coeff))->setflag(status_flags::dynallocated | status_flags::evaluated);
344 epvector* conjugateepvector(const epvector&epv)
346 epvector *newepv = 0;
347 for (epvector::const_iterator i=epv.begin(); i!=epv.end(); ++i) {
349 newepv->push_back(i->conjugate());
352 expair x = i->conjugate();
353 if (x.is_equal(*i)) {
356 newepv = new epvector;
357 newepv->reserve(epv.size());
358 for (epvector::const_iterator j=epv.begin(); j!=i; ++j) {
359 newepv->push_back(*j);
361 newepv->push_back(x);
366 ex expairseq::conjugate() const
368 epvector* newepv = conjugateepvector(seq);
369 ex x = overall_coeff.conjugate();
370 if (!newepv && are_ex_trivially_equal(x, overall_coeff)) {
373 ex result = thisexpairseq(newepv ? *newepv : seq, x);
378 bool expairseq::is_polynomial(const ex & var) const
380 if (!is_exactly_a<add>(*this) && !is_exactly_a<mul>(*this))
381 return basic::is_polynomial(var);
382 for (epvector::const_iterator i=seq.begin(); i!=seq.end(); ++i) {
383 if (!(i->rest).is_polynomial(var))
389 bool expairseq::match(const ex & pattern, exmap & repl_lst) const
391 // This differs from basic::match() because we want "a+b+c+d" to
392 // match "d+*+b" with "*" being "a+c", and we want to honor commutativity
394 if (typeid(*this) == typeid(ex_to<basic>(pattern))) {
396 // Check whether global wildcard (one that matches the "rest of the
397 // expression", like "*" above) is present
398 bool has_global_wildcard = false;
400 for (size_t i=0; i<pattern.nops(); i++) {
401 if (is_exactly_a<wildcard>(pattern.op(i))) {
402 has_global_wildcard = true;
403 global_wildcard = pattern.op(i);
408 // Unfortunately, this is an O(N^2) operation because we can't
409 // sort the pattern in a useful way...
414 for (size_t i=0; i<nops(); i++)
415 ops.push_back(op(i));
417 // Now, for every term of the pattern, look for a matching term in
418 // the expression and remove the match
419 for (size_t i=0; i<pattern.nops(); i++) {
420 ex p = pattern.op(i);
421 if (has_global_wildcard && p.is_equal(global_wildcard))
423 exvector::iterator it = ops.begin(), itend = ops.end();
424 while (it != itend) {
425 if (it->match(p, repl_lst)) {
431 return false; // no match found
435 if (has_global_wildcard) {
437 // Assign all the remaining terms to the global wildcard (unless
438 // it has already been matched before, in which case the matches
440 size_t num = ops.size();
441 std::auto_ptr<epvector> vp(new epvector);
443 for (size_t i=0; i<num; i++)
444 vp->push_back(split_ex_to_pair(ops[i]));
445 ex rest = thisexpairseq(vp, default_overall_coeff());
446 for (exmap::const_iterator it = repl_lst.begin(); it != repl_lst.end(); ++it) {
447 if (it->first.is_equal(global_wildcard))
448 return rest.is_equal(it->second);
450 repl_lst[global_wildcard] = rest;
455 // No global wildcard, then the match fails if there are any
456 // unmatched terms left
460 return inherited::match(pattern, repl_lst);
463 ex expairseq::subs(const exmap & m, unsigned options) const
465 std::auto_ptr<epvector> vp = subschildren(m, options);
467 return ex_to<basic>(thisexpairseq(vp, overall_coeff, true));
468 else if ((options & subs_options::algebraic) && is_exactly_a<mul>(*this))
469 return static_cast<const mul *>(this)->algebraic_subs_mul(m, options);
471 return subs_one_level(m, options);
476 int expairseq::compare_same_type(const basic &other) const
478 GINAC_ASSERT(is_a<expairseq>(other));
479 const expairseq &o = static_cast<const expairseq &>(other);
483 // compare number of elements
484 if (seq.size() != o.seq.size())
485 return (seq.size()<o.seq.size()) ? -1 : 1;
487 // compare overall_coeff
488 cmpval = overall_coeff.compare(o.overall_coeff);
492 #if EXPAIRSEQ_USE_HASHTAB
493 GINAC_ASSERT(hashtabsize==o.hashtabsize);
494 if (hashtabsize==0) {
495 #endif // EXPAIRSEQ_USE_HASHTAB
496 epvector::const_iterator cit1 = seq.begin();
497 epvector::const_iterator cit2 = o.seq.begin();
498 epvector::const_iterator last1 = seq.end();
499 epvector::const_iterator last2 = o.seq.end();
501 for (; (cit1!=last1)&&(cit2!=last2); ++cit1, ++cit2) {
502 cmpval = (*cit1).compare(*cit2);
503 if (cmpval!=0) return cmpval;
506 GINAC_ASSERT(cit1==last1);
507 GINAC_ASSERT(cit2==last2);
510 #if EXPAIRSEQ_USE_HASHTAB
513 // compare number of elements in each hashtab entry
514 for (unsigned i=0; i<hashtabsize; ++i) {
515 unsigned cursize=hashtab[i].size();
516 if (cursize != o.hashtab[i].size())
517 return (cursize < o.hashtab[i].size()) ? -1 : 1;
520 // compare individual (sorted) hashtab entries
521 for (unsigned i=0; i<hashtabsize; ++i) {
522 unsigned sz = hashtab[i].size();
524 const epplist &eppl1 = hashtab[i];
525 const epplist &eppl2 = o.hashtab[i];
526 epplist::const_iterator it1 = eppl1.begin();
527 epplist::const_iterator it2 = eppl2.begin();
528 while (it1!=eppl1.end()) {
529 cmpval = (*(*it1)).compare(*(*it2));
539 #endif // EXPAIRSEQ_USE_HASHTAB
542 bool expairseq::is_equal_same_type(const basic &other) const
544 const expairseq &o = static_cast<const expairseq &>(other);
546 // compare number of elements
547 if (seq.size()!=o.seq.size())
550 // compare overall_coeff
551 if (!overall_coeff.is_equal(o.overall_coeff))
554 #if EXPAIRSEQ_USE_HASHTAB
555 // compare number of elements in each hashtab entry
556 if (hashtabsize!=o.hashtabsize) {
557 std::cout << "this:" << std::endl;
558 print(print_tree(std::cout));
559 std::cout << "other:" << std::endl;
560 other.print(print_tree(std::cout));
563 GINAC_ASSERT(hashtabsize==o.hashtabsize);
565 if (hashtabsize==0) {
566 #endif // EXPAIRSEQ_USE_HASHTAB
567 epvector::const_iterator cit1 = seq.begin();
568 epvector::const_iterator cit2 = o.seq.begin();
569 epvector::const_iterator last1 = seq.end();
571 while (cit1!=last1) {
572 if (!(*cit1).is_equal(*cit2)) return false;
578 #if EXPAIRSEQ_USE_HASHTAB
581 for (unsigned i=0; i<hashtabsize; ++i) {
582 if (hashtab[i].size() != o.hashtab[i].size())
586 // compare individual sorted hashtab entries
587 for (unsigned i=0; i<hashtabsize; ++i) {
588 unsigned sz = hashtab[i].size();
590 const epplist &eppl1 = hashtab[i];
591 const epplist &eppl2 = o.hashtab[i];
592 epplist::const_iterator it1 = eppl1.begin();
593 epplist::const_iterator it2 = eppl2.begin();
594 while (it1!=eppl1.end()) {
595 if (!(*(*it1)).is_equal(*(*it2))) return false;
603 #endif // EXPAIRSEQ_USE_HASHTAB
606 unsigned expairseq::return_type() const
608 return return_types::noncommutative_composite;
611 unsigned expairseq::calchash() const
613 unsigned v = make_hash_seed(typeid(*this));
614 epvector::const_iterator i = seq.begin();
615 const epvector::const_iterator end = seq.end();
617 v ^= i->rest.gethash();
618 #if !EXPAIRSEQ_USE_HASHTAB
619 // rotation spoils commutativity!
621 v ^= i->coeff.gethash();
622 #endif // !EXPAIRSEQ_USE_HASHTAB
626 v ^= overall_coeff.gethash();
628 // store calculated hash value only if object is already evaluated
629 if (flags &status_flags::evaluated) {
630 setflag(status_flags::hash_calculated);
637 ex expairseq::expand(unsigned options) const
639 std::auto_ptr<epvector> vp = expandchildren(options);
641 return thisexpairseq(vp, overall_coeff);
643 // The terms have not changed, so it is safe to declare this expanded
644 return (options == 0) ? setflag(status_flags::expanded) : *this;
649 // new virtual functions which can be overridden by derived classes
654 /** Create an object of this type.
655 * This method works similar to a constructor. It is useful because expairseq
656 * has (at least) two possible different semantics but we want to inherit
657 * methods thus avoiding code duplication. Sometimes a method in expairseq
658 * has to create a new one of the same semantics, which cannot be done by a
659 * ctor because the name (add, mul,...) is unknown on the expaiseq level. In
660 * order for this trick to work a derived class must of course override this
662 ex expairseq::thisexpairseq(const epvector &v, const ex &oc, bool do_index_renaming) const
664 return expairseq(v, oc, do_index_renaming);
667 ex expairseq::thisexpairseq(std::auto_ptr<epvector> vp, const ex &oc, bool do_index_renaming) const
669 return expairseq(vp, oc, do_index_renaming);
672 void expairseq::printpair(const print_context & c, const expair & p, unsigned upper_precedence) const
675 p.rest.print(c, precedence());
677 p.coeff.print(c, precedence());
681 void expairseq::printseq(const print_context & c, char delim,
682 unsigned this_precedence,
683 unsigned upper_precedence) const
685 if (this_precedence <= upper_precedence)
687 epvector::const_iterator it, it_last = seq.end() - 1;
688 for (it=seq.begin(); it!=it_last; ++it) {
689 printpair(c, *it, this_precedence);
692 printpair(c, *it, this_precedence);
693 if (!overall_coeff.is_equal(default_overall_coeff())) {
695 overall_coeff.print(c, this_precedence);
698 if (this_precedence <= upper_precedence)
703 /** Form an expair from an ex, using the corresponding semantics.
704 * @see expairseq::recombine_pair_to_ex() */
705 expair expairseq::split_ex_to_pair(const ex &e) const
707 return expair(e,_ex1);
711 expair expairseq::combine_ex_with_coeff_to_pair(const ex &e,
714 GINAC_ASSERT(is_exactly_a<numeric>(c));
720 expair expairseq::combine_pair_with_coeff_to_pair(const expair &p,
723 GINAC_ASSERT(is_exactly_a<numeric>(p.coeff));
724 GINAC_ASSERT(is_exactly_a<numeric>(c));
726 return expair(p.rest,ex_to<numeric>(p.coeff).mul_dyn(ex_to<numeric>(c)));
730 /** Form an ex out of an expair, using the corresponding semantics.
731 * @see expairseq::split_ex_to_pair() */
732 ex expairseq::recombine_pair_to_ex(const expair &p) const
734 return lst(p.rest,p.coeff);
737 bool expairseq::expair_needs_further_processing(epp it)
739 #if EXPAIRSEQ_USE_HASHTAB
740 //# error "FIXME: expair_needs_further_processing not yet implemented for hashtabs, sorry. A.F."
741 #endif // EXPAIRSEQ_USE_HASHTAB
745 ex expairseq::default_overall_coeff() const
750 void expairseq::combine_overall_coeff(const ex &c)
752 GINAC_ASSERT(is_exactly_a<numeric>(overall_coeff));
753 GINAC_ASSERT(is_exactly_a<numeric>(c));
754 overall_coeff = ex_to<numeric>(overall_coeff).add_dyn(ex_to<numeric>(c));
757 void expairseq::combine_overall_coeff(const ex &c1, const ex &c2)
759 GINAC_ASSERT(is_exactly_a<numeric>(overall_coeff));
760 GINAC_ASSERT(is_exactly_a<numeric>(c1));
761 GINAC_ASSERT(is_exactly_a<numeric>(c2));
762 overall_coeff = ex_to<numeric>(overall_coeff).
763 add_dyn(ex_to<numeric>(c1).mul(ex_to<numeric>(c2)));
766 bool expairseq::can_make_flat(const expair &p) const
773 // non-virtual functions in this class
776 void expairseq::construct_from_2_ex_via_exvector(const ex &lh, const ex &rh)
782 construct_from_exvector(v);
783 #if EXPAIRSEQ_USE_HASHTAB
784 GINAC_ASSERT((hashtabsize==0)||(hashtabsize>=minhashtabsize));
785 GINAC_ASSERT(hashtabsize==calc_hashtabsize(seq.size()));
786 #endif // EXPAIRSEQ_USE_HASHTAB
789 void expairseq::construct_from_2_ex(const ex &lh, const ex &rh)
791 if (typeid(ex_to<basic>(lh)) == typeid(*this)) {
792 if (typeid(ex_to<basic>(rh)) == typeid(*this)) {
793 #if EXPAIRSEQ_USE_HASHTAB
794 unsigned totalsize = ex_to<expairseq>(lh).seq.size() +
795 ex_to<expairseq>(rh).seq.size();
796 if (calc_hashtabsize(totalsize)!=0) {
797 construct_from_2_ex_via_exvector(lh,rh);
799 #endif // EXPAIRSEQ_USE_HASHTAB
800 if (is_a<mul>(lh) && lh.info(info_flags::has_indices) &&
801 rh.info(info_flags::has_indices)) {
802 ex newrh=rename_dummy_indices_uniquely(lh, rh);
803 construct_from_2_expairseq(ex_to<expairseq>(lh),
804 ex_to<expairseq>(newrh));
807 construct_from_2_expairseq(ex_to<expairseq>(lh),
808 ex_to<expairseq>(rh));
809 #if EXPAIRSEQ_USE_HASHTAB
811 #endif // EXPAIRSEQ_USE_HASHTAB
814 #if EXPAIRSEQ_USE_HASHTAB
815 unsigned totalsize = ex_to<expairseq>(lh).seq.size()+1;
816 if (calc_hashtabsize(totalsize)!=0) {
817 construct_from_2_ex_via_exvector(lh, rh);
819 #endif // EXPAIRSEQ_USE_HASHTAB
820 construct_from_expairseq_ex(ex_to<expairseq>(lh), rh);
821 #if EXPAIRSEQ_USE_HASHTAB
823 #endif // EXPAIRSEQ_USE_HASHTAB
826 } else if (typeid(ex_to<basic>(rh)) == typeid(*this)) {
827 #if EXPAIRSEQ_USE_HASHTAB
828 unsigned totalsize=ex_to<expairseq>(rh).seq.size()+1;
829 if (calc_hashtabsize(totalsize)!=0) {
830 construct_from_2_ex_via_exvector(lh,rh);
832 #endif // EXPAIRSEQ_USE_HASHTAB
833 construct_from_expairseq_ex(ex_to<expairseq>(rh),lh);
834 #if EXPAIRSEQ_USE_HASHTAB
836 #endif // EXPAIRSEQ_USE_HASHTAB
840 #if EXPAIRSEQ_USE_HASHTAB
841 if (calc_hashtabsize(2)!=0) {
842 construct_from_2_ex_via_exvector(lh,rh);
846 #endif // EXPAIRSEQ_USE_HASHTAB
848 if (is_exactly_a<numeric>(lh)) {
849 if (is_exactly_a<numeric>(rh)) {
850 combine_overall_coeff(lh);
851 combine_overall_coeff(rh);
853 combine_overall_coeff(lh);
854 seq.push_back(split_ex_to_pair(rh));
857 if (is_exactly_a<numeric>(rh)) {
858 combine_overall_coeff(rh);
859 seq.push_back(split_ex_to_pair(lh));
861 expair p1 = split_ex_to_pair(lh);
862 expair p2 = split_ex_to_pair(rh);
864 int cmpval = p1.rest.compare(p2.rest);
866 p1.coeff = ex_to<numeric>(p1.coeff).add_dyn(ex_to<numeric>(p2.coeff));
867 if (!ex_to<numeric>(p1.coeff).is_zero()) {
868 // no further processing is necessary, since this
869 // one element will usually be recombined in eval()
886 void expairseq::construct_from_2_expairseq(const expairseq &s1,
889 combine_overall_coeff(s1.overall_coeff);
890 combine_overall_coeff(s2.overall_coeff);
892 epvector::const_iterator first1 = s1.seq.begin();
893 epvector::const_iterator last1 = s1.seq.end();
894 epvector::const_iterator first2 = s2.seq.begin();
895 epvector::const_iterator last2 = s2.seq.end();
897 seq.reserve(s1.seq.size()+s2.seq.size());
899 bool needs_further_processing=false;
901 while (first1!=last1 && first2!=last2) {
902 int cmpval = (*first1).rest.compare((*first2).rest);
906 const numeric &newcoeff = ex_to<numeric>(first1->coeff).
907 add(ex_to<numeric>(first2->coeff));
908 if (!newcoeff.is_zero()) {
909 seq.push_back(expair(first1->rest,newcoeff));
910 if (expair_needs_further_processing(seq.end()-1)) {
911 needs_further_processing = true;
916 } else if (cmpval<0) {
917 seq.push_back(*first1);
920 seq.push_back(*first2);
925 while (first1!=last1) {
926 seq.push_back(*first1);
929 while (first2!=last2) {
930 seq.push_back(*first2);
934 if (needs_further_processing) {
937 construct_from_epvector(v);
941 void expairseq::construct_from_expairseq_ex(const expairseq &s,
944 combine_overall_coeff(s.overall_coeff);
945 if (is_exactly_a<numeric>(e)) {
946 combine_overall_coeff(e);
951 epvector::const_iterator first = s.seq.begin();
952 epvector::const_iterator last = s.seq.end();
953 expair p = split_ex_to_pair(e);
955 seq.reserve(s.seq.size()+1);
956 bool p_pushed = false;
958 bool needs_further_processing=false;
960 // merge p into s.seq
961 while (first!=last) {
962 int cmpval = (*first).rest.compare(p.rest);
965 const numeric &newcoeff = ex_to<numeric>(first->coeff).
966 add(ex_to<numeric>(p.coeff));
967 if (!newcoeff.is_zero()) {
968 seq.push_back(expair(first->rest,newcoeff));
969 if (expair_needs_further_processing(seq.end()-1))
970 needs_further_processing = true;
975 } else if (cmpval<0) {
976 seq.push_back(*first);
986 // while loop exited because p was pushed, now push rest of s.seq
987 while (first!=last) {
988 seq.push_back(*first);
992 // while loop exited because s.seq was pushed, now push p
996 if (needs_further_processing) {
999 construct_from_epvector(v);
1003 void expairseq::construct_from_exvector(const exvector &v)
1005 // simplifications: +(a,+(b,c),d) -> +(a,b,c,d) (associativity)
1006 // +(d,b,c,a) -> +(a,b,c,d) (canonicalization)
1007 // +(...,x,*(x,c1),*(x,c2)) -> +(...,*(x,1+c1+c2)) (c1, c2 numeric())
1008 // (same for (+,*) -> (*,^)
1011 #if EXPAIRSEQ_USE_HASHTAB
1012 combine_same_terms();
1015 combine_same_terms_sorted_seq();
1016 #endif // EXPAIRSEQ_USE_HASHTAB
1019 void expairseq::construct_from_epvector(const epvector &v, bool do_index_renaming)
1021 // simplifications: +(a,+(b,c),d) -> +(a,b,c,d) (associativity)
1022 // +(d,b,c,a) -> +(a,b,c,d) (canonicalization)
1023 // +(...,x,*(x,c1),*(x,c2)) -> +(...,*(x,1+c1+c2)) (c1, c2 numeric())
1024 // (same for (+,*) -> (*,^)
1026 make_flat(v, do_index_renaming);
1027 #if EXPAIRSEQ_USE_HASHTAB
1028 combine_same_terms();
1031 combine_same_terms_sorted_seq();
1032 #endif // EXPAIRSEQ_USE_HASHTAB
1035 /** Combine this expairseq with argument exvector.
1036 * It cares for associativity as well as for special handling of numerics. */
1037 void expairseq::make_flat(const exvector &v)
1039 exvector::const_iterator cit;
1041 // count number of operands which are of same expairseq derived type
1042 // and their cumulative number of operands
1043 int nexpairseqs = 0;
1045 bool do_idx_rename = false;
1048 while (cit!=v.end()) {
1049 if (typeid(ex_to<basic>(*cit)) == typeid(*this)) {
1051 noperands += ex_to<expairseq>(*cit).seq.size();
1053 if (is_a<mul>(*this) && (!do_idx_rename) &&
1054 cit->info(info_flags::has_indices))
1055 do_idx_rename = true;
1059 // reserve seq and coeffseq which will hold all operands
1060 seq.reserve(v.size()+noperands-nexpairseqs);
1062 // copy elements and split off numerical part
1063 make_flat_inserter mf(v, do_idx_rename);
1065 while (cit!=v.end()) {
1066 if (typeid(ex_to<basic>(*cit)) == typeid(*this)) {
1067 ex newfactor = mf.handle_factor(*cit, _ex1);
1068 const expairseq &subseqref = ex_to<expairseq>(newfactor);
1069 combine_overall_coeff(subseqref.overall_coeff);
1070 epvector::const_iterator cit_s = subseqref.seq.begin();
1071 while (cit_s!=subseqref.seq.end()) {
1072 seq.push_back(*cit_s);
1076 if (is_exactly_a<numeric>(*cit))
1077 combine_overall_coeff(*cit);
1079 ex newfactor = mf.handle_factor(*cit, _ex1);
1080 seq.push_back(split_ex_to_pair(newfactor));
1087 /** Combine this expairseq with argument epvector.
1088 * It cares for associativity as well as for special handling of numerics. */
1089 void expairseq::make_flat(const epvector &v, bool do_index_renaming)
1091 epvector::const_iterator cit;
1093 // count number of operands which are of same expairseq derived type
1094 // and their cumulative number of operands
1095 int nexpairseqs = 0;
1097 bool really_need_rename_inds = false;
1100 while (cit!=v.end()) {
1101 if (typeid(ex_to<basic>(cit->rest)) == typeid(*this)) {
1103 noperands += ex_to<expairseq>(cit->rest).seq.size();
1105 if ((!really_need_rename_inds) && is_a<mul>(*this) &&
1106 cit->rest.info(info_flags::has_indices))
1107 really_need_rename_inds = true;
1110 do_index_renaming = do_index_renaming && really_need_rename_inds;
1112 // reserve seq and coeffseq which will hold all operands
1113 seq.reserve(v.size()+noperands-nexpairseqs);
1114 make_flat_inserter mf(v, do_index_renaming);
1116 // copy elements and split off numerical part
1118 while (cit!=v.end()) {
1119 if ((typeid(ex_to<basic>(cit->rest)) == typeid(*this)) &&
1120 this->can_make_flat(*cit)) {
1121 ex newrest = mf.handle_factor(cit->rest, cit->coeff);
1122 const expairseq &subseqref = ex_to<expairseq>(newrest);
1123 combine_overall_coeff(ex_to<numeric>(subseqref.overall_coeff),
1124 ex_to<numeric>(cit->coeff));
1125 epvector::const_iterator cit_s = subseqref.seq.begin();
1126 while (cit_s!=subseqref.seq.end()) {
1127 seq.push_back(expair(cit_s->rest,
1128 ex_to<numeric>(cit_s->coeff).mul_dyn(ex_to<numeric>(cit->coeff))));
1129 //seq.push_back(combine_pair_with_coeff_to_pair(*cit_s,
1134 if (cit->is_canonical_numeric())
1135 combine_overall_coeff(mf.handle_factor(cit->rest, _ex1));
1137 ex rest = cit->rest;
1138 ex newrest = mf.handle_factor(rest, cit->coeff);
1139 if (are_ex_trivially_equal(newrest, rest))
1140 seq.push_back(*cit);
1142 seq.push_back(expair(newrest, cit->coeff));
1149 /** Brings this expairseq into a sorted (canonical) form. */
1150 void expairseq::canonicalize()
1152 std::sort(seq.begin(), seq.end(), expair_rest_is_less());
1156 /** Compact a presorted expairseq by combining all matching expairs to one
1157 * each. On an add object, this is responsible for 2*x+3*x+y -> 5*x+y, for
1159 void expairseq::combine_same_terms_sorted_seq()
1164 bool needs_further_processing = false;
1166 epvector::iterator itin1 = seq.begin();
1167 epvector::iterator itin2 = itin1+1;
1168 epvector::iterator itout = itin1;
1169 epvector::iterator last = seq.end();
1170 // must_copy will be set to true the first time some combination is
1171 // possible from then on the sequence has changed and must be compacted
1172 bool must_copy = false;
1173 while (itin2!=last) {
1174 if (itin1->rest.compare(itin2->rest)==0) {
1175 itin1->coeff = ex_to<numeric>(itin1->coeff).
1176 add_dyn(ex_to<numeric>(itin2->coeff));
1177 if (expair_needs_further_processing(itin1))
1178 needs_further_processing = true;
1181 if (!ex_to<numeric>(itin1->coeff).is_zero()) {
1190 if (!ex_to<numeric>(itin1->coeff).is_zero()) {
1196 seq.erase(itout,last);
1198 if (needs_further_processing) {
1201 construct_from_epvector(v);
1205 #if EXPAIRSEQ_USE_HASHTAB
1207 unsigned expairseq::calc_hashtabsize(unsigned sz) const
1210 unsigned nearest_power_of_2 = 1 << log2(sz);
1211 // if (nearest_power_of_2 < maxhashtabsize/hashtabfactor) {
1212 // size = nearest_power_of_2*hashtabfactor;
1213 size = nearest_power_of_2/hashtabfactor;
1214 if (size<minhashtabsize)
1217 // hashtabsize must be a power of 2
1218 GINAC_ASSERT((1U << log2(size))==size);
1222 unsigned expairseq::calc_hashindex(const ex &e) const
1224 // calculate hashindex
1226 if (is_a<numeric>(e)) {
1227 hashindex = hashmask;
1229 hashindex = e.gethash() & hashmask;
1230 // last hashtab entry is reserved for numerics
1231 if (hashindex==hashmask) hashindex = 0;
1233 GINAC_ASSERT((hashindex<hashtabsize)||(hashtabsize==0));
1237 void expairseq::shrink_hashtab()
1239 unsigned new_hashtabsize;
1240 while (hashtabsize!=(new_hashtabsize=calc_hashtabsize(seq.size()))) {
1241 GINAC_ASSERT(new_hashtabsize<hashtabsize);
1242 if (new_hashtabsize==0) {
1249 // shrink by a factor of 2
1250 unsigned half_hashtabsize = hashtabsize/2;
1251 for (unsigned i=0; i<half_hashtabsize-1; ++i)
1252 hashtab[i].merge(hashtab[i+half_hashtabsize],epp_is_less());
1253 // special treatment for numeric hashes
1254 hashtab[0].merge(hashtab[half_hashtabsize-1],epp_is_less());
1255 hashtab[half_hashtabsize-1] = hashtab[hashtabsize-1];
1256 hashtab.resize(half_hashtabsize);
1257 hashtabsize = half_hashtabsize;
1258 hashmask = hashtabsize-1;
1262 void expairseq::remove_hashtab_entry(epvector::const_iterator element)
1265 return; // nothing to do
1267 // calculate hashindex of element to be deleted
1268 unsigned hashindex = calc_hashindex((*element).rest);
1270 // find it in hashtab and remove it
1271 epplist &eppl = hashtab[hashindex];
1272 epplist::iterator epplit = eppl.begin();
1273 bool erased = false;
1274 while (epplit!=eppl.end()) {
1275 if (*epplit == element) {
1283 std::cout << "tried to erase " << element-seq.begin() << std::endl;
1284 std::cout << "size " << seq.end()-seq.begin() << std::endl;
1286 unsigned hashindex = calc_hashindex(element->rest);
1287 epplist &eppl = hashtab[hashindex];
1288 epplist::iterator epplit = eppl.begin();
1289 bool erased = false;
1290 while (epplit!=eppl.end()) {
1291 if (*epplit == element) {
1298 GINAC_ASSERT(erased);
1300 GINAC_ASSERT(erased);
1303 void expairseq::move_hashtab_entry(epvector::const_iterator oldpos,
1304 epvector::iterator newpos)
1306 GINAC_ASSERT(hashtabsize!=0);
1308 // calculate hashindex of element which was moved
1309 unsigned hashindex=calc_hashindex((*newpos).rest);
1311 // find it in hashtab and modify it
1312 epplist &eppl = hashtab[hashindex];
1313 epplist::iterator epplit = eppl.begin();
1314 while (epplit!=eppl.end()) {
1315 if (*epplit == oldpos) {
1321 GINAC_ASSERT(epplit!=eppl.end());
1324 void expairseq::sorted_insert(epplist &eppl, epvector::const_iterator elem)
1326 epplist::const_iterator current = eppl.begin();
1327 while ((current!=eppl.end()) && ((*current)->is_less(*elem))) {
1330 eppl.insert(current,elem);
1333 void expairseq::build_hashtab_and_combine(epvector::iterator &first_numeric,
1334 epvector::iterator &last_non_zero,
1335 std::vector<bool> &touched,
1336 unsigned &number_of_zeroes)
1338 epp current = seq.begin();
1340 while (current!=first_numeric) {
1341 if (is_exactly_a<numeric>(current->rest)) {
1343 iter_swap(current,first_numeric);
1345 // calculate hashindex
1346 unsigned currenthashindex = calc_hashindex(current->rest);
1348 // test if there is already a matching expair in the hashtab-list
1349 epplist &eppl=hashtab[currenthashindex];
1350 epplist::iterator epplit = eppl.begin();
1351 while (epplit!=eppl.end()) {
1352 if (current->rest.is_equal((*epplit)->rest))
1356 if (epplit==eppl.end()) {
1357 // no matching expair found, append this to end of list
1358 sorted_insert(eppl,current);
1361 // epplit points to a matching expair, combine it with current
1362 (*epplit)->coeff = ex_to<numeric>((*epplit)->coeff).
1363 add_dyn(ex_to<numeric>(current->coeff));
1365 // move obsolete current expair to end by swapping with last_non_zero element
1366 // if this was a numeric, it is swapped with the expair before first_numeric
1367 iter_swap(current,last_non_zero);
1369 if (first_numeric!=last_non_zero) iter_swap(first_numeric,current);
1372 // test if combined term has coeff 0 and can be removed is done later
1373 touched[(*epplit)-seq.begin()] = true;
1379 void expairseq::drop_coeff_0_terms(epvector::iterator &first_numeric,
1380 epvector::iterator &last_non_zero,
1381 std::vector<bool> &touched,
1382 unsigned &number_of_zeroes)
1384 // move terms with coeff 0 to end and remove them from hashtab
1385 // check only those elements which have been touched
1386 epp current = seq.begin();
1388 while (current!=first_numeric) {
1392 } else if (!ex_to<numeric>((*current).coeff).is_zero()) {
1396 remove_hashtab_entry(current);
1398 // move element to the end, unless it is already at the end
1399 if (current!=last_non_zero) {
1400 iter_swap(current,last_non_zero);
1402 bool numeric_swapped = first_numeric!=last_non_zero;
1403 if (numeric_swapped)
1404 iter_swap(first_numeric,current);
1405 epvector::iterator changed_entry;
1407 if (numeric_swapped)
1408 changed_entry = first_numeric;
1410 changed_entry = last_non_zero;
1415 if (first_numeric!=current) {
1417 // change entry in hashtab which referred to first_numeric or last_non_zero to current
1418 move_hashtab_entry(changed_entry,current);
1419 touched[current-seq.begin()] = touched[changed_entry-seq.begin()];
1428 GINAC_ASSERT(i==current-seq.begin());
1431 /** True if one of the coeffs vanishes, otherwise false.
1432 * This would be an invariant violation, so this should only be used for
1433 * debugging purposes. */
1434 bool expairseq::has_coeff_0() const
1436 epvector::const_iterator i = seq.begin(), end = seq.end();
1438 if (i->coeff.is_zero())
1445 void expairseq::add_numerics_to_hashtab(epvector::iterator first_numeric,
1446 epvector::const_iterator last_non_zero)
1448 if (first_numeric == seq.end()) return; // no numerics
1450 epvector::const_iterator current = first_numeric, last = last_non_zero + 1;
1451 while (current != last) {
1452 sorted_insert(hashtab[hashmask], current);
1457 void expairseq::combine_same_terms()
1459 // combine same terms, drop term with coeff 0, move numerics to end
1461 // calculate size of hashtab
1462 hashtabsize = calc_hashtabsize(seq.size());
1464 // hashtabsize is a power of 2
1465 hashmask = hashtabsize-1;
1469 hashtab.resize(hashtabsize);
1471 if (hashtabsize==0) {
1473 combine_same_terms_sorted_seq();
1474 GINAC_ASSERT(!has_coeff_0());
1478 // iterate through seq, move numerics to end,
1479 // fill hashtab and combine same terms
1480 epvector::iterator first_numeric = seq.end();
1481 epvector::iterator last_non_zero = seq.end()-1;
1483 size_t num = seq.size();
1484 std::vector<bool> touched(num);
1486 unsigned number_of_zeroes = 0;
1488 GINAC_ASSERT(!has_coeff_0());
1489 build_hashtab_and_combine(first_numeric,last_non_zero,touched,number_of_zeroes);
1491 // there should not be any terms with coeff 0 from the beginning,
1492 // so it should be safe to skip this step
1493 if (number_of_zeroes!=0) {
1494 drop_coeff_0_terms(first_numeric,last_non_zero,touched,number_of_zeroes);
1497 add_numerics_to_hashtab(first_numeric,last_non_zero);
1499 // pop zero elements
1500 for (unsigned i=0; i<number_of_zeroes; ++i) {
1504 // shrink hashtabsize to calculated value
1505 GINAC_ASSERT(!has_coeff_0());
1509 GINAC_ASSERT(!has_coeff_0());
1512 #endif // EXPAIRSEQ_USE_HASHTAB
1514 /** Check if this expairseq is in sorted (canonical) form. Useful mainly for
1515 * debugging or in assertions since being sorted is an invariance. */
1516 bool expairseq::is_canonical() const
1518 if (seq.size() <= 1)
1521 #if EXPAIRSEQ_USE_HASHTAB
1522 if (hashtabsize > 0) return 1; // not canoncalized
1523 #endif // EXPAIRSEQ_USE_HASHTAB
1525 epvector::const_iterator it = seq.begin(), itend = seq.end();
1526 epvector::const_iterator it_last = it;
1527 for (++it; it!=itend; it_last=it, ++it) {
1528 if (!(it_last->is_less(*it) || it_last->is_equal(*it))) {
1529 if (!is_exactly_a<numeric>(it_last->rest) ||
1530 !is_exactly_a<numeric>(it->rest)) {
1531 // double test makes it easier to set a breakpoint...
1532 if (!is_exactly_a<numeric>(it_last->rest) ||
1533 !is_exactly_a<numeric>(it->rest)) {
1534 printpair(std::clog, *it_last, 0);
1536 printpair(std::clog, *it, 0);
1538 std::clog << "pair1:" << std::endl;
1539 it_last->rest.print(print_tree(std::clog));
1540 it_last->coeff.print(print_tree(std::clog));
1541 std::clog << "pair2:" << std::endl;
1542 it->rest.print(print_tree(std::clog));
1543 it->coeff.print(print_tree(std::clog));
1553 /** Member-wise expand the expairs in this sequence.
1555 * @see expairseq::expand()
1556 * @return pointer to epvector containing expanded pairs or zero pointer,
1557 * if no members were changed. */
1558 std::auto_ptr<epvector> expairseq::expandchildren(unsigned options) const
1560 const epvector::const_iterator last = seq.end();
1561 epvector::const_iterator cit = seq.begin();
1563 const ex &expanded_ex = cit->rest.expand(options);
1564 if (!are_ex_trivially_equal(cit->rest,expanded_ex)) {
1566 // something changed, copy seq, eval and return it
1567 std::auto_ptr<epvector> s(new epvector);
1568 s->reserve(seq.size());
1570 // copy parts of seq which are known not to have changed
1571 epvector::const_iterator cit2 = seq.begin();
1573 s->push_back(*cit2);
1577 // copy first changed element
1578 s->push_back(combine_ex_with_coeff_to_pair(expanded_ex,
1583 while (cit2!=last) {
1584 s->push_back(combine_ex_with_coeff_to_pair(cit2->rest.expand(options),
1593 return std::auto_ptr<epvector>(0); // signalling nothing has changed
1597 /** Member-wise evaluate the expairs in this sequence.
1599 * @see expairseq::eval()
1600 * @return pointer to epvector containing evaluated pairs or zero pointer,
1601 * if no members were changed. */
1602 std::auto_ptr<epvector> expairseq::evalchildren(int level) const
1604 // returns a NULL pointer if nothing had to be evaluated
1605 // returns a pointer to a newly created epvector otherwise
1606 // (which has to be deleted somewhere else)
1609 return std::auto_ptr<epvector>(0);
1611 if (level == -max_recursion_level)
1612 throw(std::runtime_error("max recursion level reached"));
1615 epvector::const_iterator last = seq.end();
1616 epvector::const_iterator cit = seq.begin();
1618 const ex &evaled_ex = cit->rest.eval(level);
1619 if (!are_ex_trivially_equal(cit->rest,evaled_ex)) {
1621 // something changed, copy seq, eval and return it
1622 std::auto_ptr<epvector> s(new epvector);
1623 s->reserve(seq.size());
1625 // copy parts of seq which are known not to have changed
1626 epvector::const_iterator cit2=seq.begin();
1628 s->push_back(*cit2);
1632 // copy first changed element
1633 s->push_back(combine_ex_with_coeff_to_pair(evaled_ex,
1638 while (cit2!=last) {
1639 s->push_back(combine_ex_with_coeff_to_pair(cit2->rest.eval(level),
1648 return std::auto_ptr<epvector>(0); // signalling nothing has changed
1651 /** Member-wise substitute in this sequence.
1653 * @see expairseq::subs()
1654 * @return pointer to epvector containing pairs after application of subs,
1655 * or NULL pointer if no members were changed. */
1656 std::auto_ptr<epvector> expairseq::subschildren(const exmap & m, unsigned options) const
1658 // When any of the objects to be substituted is a product or power
1659 // we have to recombine the pairs because the numeric coefficients may
1660 // be part of the search pattern.
1661 if (!(options & (subs_options::pattern_is_product | subs_options::pattern_is_not_product))) {
1663 // Search the list of substitutions and cache our findings
1664 for (exmap::const_iterator it = m.begin(); it != m.end(); ++it) {
1665 if (is_exactly_a<mul>(it->first) || is_exactly_a<power>(it->first)) {
1666 options |= subs_options::pattern_is_product;
1670 if (!(options & subs_options::pattern_is_product))
1671 options |= subs_options::pattern_is_not_product;
1674 if (options & subs_options::pattern_is_product) {
1676 // Substitute in the recombined pairs
1677 epvector::const_iterator cit = seq.begin(), last = seq.end();
1678 while (cit != last) {
1680 const ex &orig_ex = recombine_pair_to_ex(*cit);
1681 const ex &subsed_ex = orig_ex.subs(m, options);
1682 if (!are_ex_trivially_equal(orig_ex, subsed_ex)) {
1684 // Something changed, copy seq, subs and return it
1685 std::auto_ptr<epvector> s(new epvector);
1686 s->reserve(seq.size());
1688 // Copy parts of seq which are known not to have changed
1689 s->insert(s->begin(), seq.begin(), cit);
1691 // Copy first changed element
1692 s->push_back(split_ex_to_pair(subsed_ex));
1696 while (cit != last) {
1697 s->push_back(split_ex_to_pair(recombine_pair_to_ex(*cit).subs(m, options)));
1708 // Substitute only in the "rest" part of the pairs
1709 epvector::const_iterator cit = seq.begin(), last = seq.end();
1710 while (cit != last) {
1712 const ex &subsed_ex = cit->rest.subs(m, options);
1713 if (!are_ex_trivially_equal(cit->rest, subsed_ex)) {
1715 // Something changed, copy seq, subs and return it
1716 std::auto_ptr<epvector> s(new epvector);
1717 s->reserve(seq.size());
1719 // Copy parts of seq which are known not to have changed
1720 s->insert(s->begin(), seq.begin(), cit);
1722 // Copy first changed element
1723 s->push_back(combine_ex_with_coeff_to_pair(subsed_ex, cit->coeff));
1727 while (cit != last) {
1728 s->push_back(combine_ex_with_coeff_to_pair(cit->rest.subs(m, options), cit->coeff));
1738 // Nothing has changed
1739 return std::auto_ptr<epvector>(0);
1743 // static member variables
1746 #if EXPAIRSEQ_USE_HASHTAB
1747 unsigned expairseq::maxhashtabsize = 0x4000000U;
1748 unsigned expairseq::minhashtabsize = 0x1000U;
1749 unsigned expairseq::hashtabfactor = 1;
1750 #endif // EXPAIRSEQ_USE_HASHTAB
1752 } // namespace GiNaC