1 /** @file expairseq.cpp
3 * Implementation of sequences of expression pairs. */
6 * GiNaC Copyright (C) 1999-2003 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
28 #include "expairseq.h"
32 #include "relational.h"
36 #include "operators.h"
39 #if EXPAIRSEQ_USE_HASHTAB
41 #endif // EXPAIRSEQ_USE_HASHTAB
46 GINAC_IMPLEMENT_REGISTERED_CLASS(expairseq, basic)
55 bool operator()(const epp &lh, const epp &rh) const
57 return (*lh).is_less(*rh);
62 // default constructor
67 expairseq::expairseq() : inherited(TINFO_expairseq)
68 #if EXPAIRSEQ_USE_HASHTAB
70 #endif // EXPAIRSEQ_USE_HASHTAB
76 /** For use by copy ctor and assignment operator. */
77 void expairseq::copy(const expairseq &other)
80 overall_coeff = other.overall_coeff;
81 #if EXPAIRSEQ_USE_HASHTAB
83 hashtabsize = other.hashtabsize;
85 hashmask = other.hashmask;
86 hashtab.resize(hashtabsize);
87 epvector::const_iterator osb = other.seq.begin();
88 for (unsigned i=0; i<hashtabsize; ++i) {
90 for (epplist::const_iterator cit=other.hashtab[i].begin();
91 cit!=other.hashtab[i].end(); ++cit) {
92 hashtab[i].push_back(seq.begin()+((*cit)-osb));
98 #endif // EXPAIRSEQ_USE_HASHTAB
103 // other constructors
106 expairseq::expairseq(const ex &lh, const ex &rh) : inherited(TINFO_expairseq)
108 construct_from_2_ex(lh,rh);
109 GINAC_ASSERT(is_canonical());
112 expairseq::expairseq(const exvector &v) : inherited(TINFO_expairseq)
114 construct_from_exvector(v);
115 GINAC_ASSERT(is_canonical());
118 expairseq::expairseq(const epvector &v, const ex &oc)
119 : inherited(TINFO_expairseq), overall_coeff(oc)
121 GINAC_ASSERT(is_a<numeric>(oc));
122 construct_from_epvector(v);
123 GINAC_ASSERT(is_canonical());
126 expairseq::expairseq(epvector *vp, const ex &oc)
127 : inherited(TINFO_expairseq), overall_coeff(oc)
130 GINAC_ASSERT(is_a<numeric>(oc));
131 construct_from_epvector(*vp);
133 GINAC_ASSERT(is_canonical());
140 expairseq::expairseq(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst)
141 #if EXPAIRSEQ_USE_HASHTAB
145 for (unsigned int i=0; true; i++) {
148 if (n.find_ex("rest", rest, sym_lst, i) && n.find_ex("coeff", coeff, sym_lst, i))
149 seq.push_back(expair(rest, coeff));
154 n.find_ex("overall_coeff", overall_coeff, sym_lst);
157 GINAC_ASSERT(is_canonical());
160 void expairseq::archive(archive_node &n) const
162 inherited::archive(n);
163 epvector::const_iterator i = seq.begin(), iend = seq.end();
165 n.add_ex("rest", i->rest);
166 n.add_ex("coeff", i->coeff);
169 n.add_ex("overall_coeff", overall_coeff);
172 DEFAULT_UNARCHIVE(expairseq)
175 // functions overriding virtual functions from base classes
180 void expairseq::print(const print_context &c, unsigned level) const
182 if (is_a<print_tree>(c)) {
184 unsigned delta_indent = static_cast<const print_tree &>(c).delta_indent;
186 c.s << std::string(level, ' ') << class_name()
187 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
188 << ", nops=" << nops()
190 size_t num = seq.size();
191 for (size_t i=0; i<num; ++i) {
192 seq[i].rest.print(c, level + delta_indent);
193 seq[i].coeff.print(c, level + delta_indent);
195 c.s << std::string(level + delta_indent, ' ') << "-----" << std::endl;
197 if (!overall_coeff.is_equal(default_overall_coeff())) {
198 c.s << std::string(level + delta_indent, ' ') << "-----" << std::endl
199 << std::string(level + delta_indent, ' ') << "overall_coeff" << std::endl;
200 overall_coeff.print(c, level + delta_indent);
202 c.s << std::string(level + delta_indent,' ') << "=====" << std::endl;
203 #if EXPAIRSEQ_USE_HASHTAB
204 c.s << std::string(level + delta_indent,' ')
205 << "hashtab size " << hashtabsize << std::endl;
206 if (hashtabsize == 0) return;
208 unsigned count[MAXCOUNT+1];
209 for (int i=0; i<MAXCOUNT+1; ++i)
211 unsigned this_bin_fill;
212 unsigned cum_fill_sq = 0;
213 unsigned cum_fill = 0;
214 for (unsigned i=0; i<hashtabsize; ++i) {
216 if (hashtab[i].size() > 0) {
217 c.s << std::string(level + delta_indent, ' ')
218 << "bin " << i << " with entries ";
219 for (epplist::const_iterator it=hashtab[i].begin();
220 it!=hashtab[i].end(); ++it) {
221 c.s << *it-seq.begin() << " ";
225 cum_fill += this_bin_fill;
226 cum_fill_sq += this_bin_fill*this_bin_fill;
228 if (this_bin_fill<MAXCOUNT)
229 ++count[this_bin_fill];
235 double lambda = (1.0*seq.size()) / hashtabsize;
236 for (int k=0; k<MAXCOUNT; ++k) {
239 double prob = std::pow(lambda,k)/fact * std::exp(-lambda);
241 c.s << std::string(level + delta_indent, ' ') << "bins with " << k << " entries: "
242 << int(1000.0*count[k]/hashtabsize)/10.0 << "% (expected: "
243 << int(prob*1000)/10.0 << ")" << std::endl;
245 c.s << std::string(level + delta_indent, ' ') << "bins with more entries: "
246 << int(1000.0*count[MAXCOUNT]/hashtabsize)/10.0 << "% (expected: "
247 << int((1-cum_prob)*1000)/10.0 << ")" << std::endl;
249 c.s << std::string(level + delta_indent, ' ') << "variance: "
250 << 1.0/hashtabsize*cum_fill_sq-(1.0/hashtabsize*cum_fill)*(1.0/hashtabsize*cum_fill)
252 c.s << std::string(level + delta_indent, ' ') << "average fill: "
253 << (1.0*cum_fill)/hashtabsize
254 << " (should be equal to " << (1.0*seq.size())/hashtabsize << ")" << std::endl;
255 #endif // EXPAIRSEQ_USE_HASHTAB
259 printseq(c, ',', precedence(), level);
264 bool expairseq::info(unsigned inf) const
266 return inherited::info(inf);
269 size_t expairseq::nops() const
271 if (overall_coeff.is_equal(default_overall_coeff()))
277 ex expairseq::op(size_t i) const
280 return recombine_pair_to_ex(seq[i]);
281 GINAC_ASSERT(!overall_coeff.is_equal(default_overall_coeff()));
282 return overall_coeff;
285 ex expairseq::map(map_function &f) const
287 epvector *v = new epvector;
288 v->reserve(seq.size());
290 epvector::const_iterator cit = seq.begin(), last = seq.end();
291 while (cit != last) {
292 v->push_back(split_ex_to_pair(f(recombine_pair_to_ex(*cit))));
296 if (overall_coeff.is_equal(default_overall_coeff()))
297 return thisexpairseq(v, default_overall_coeff());
299 return thisexpairseq(v, f(overall_coeff));
302 /** Perform coefficient-wise automatic term rewriting rules in this class. */
303 ex expairseq::eval(int level) const
305 if ((level==1) && (flags &status_flags::evaluated))
308 epvector *vp = evalchildren(level);
312 return (new expairseq(vp,overall_coeff))->setflag(status_flags::dynallocated | status_flags::evaluated);
315 bool expairseq::match(const ex & pattern, lst & repl_lst) const
317 // This differs from basic::match() because we want "a+b+c+d" to
318 // match "d+*+b" with "*" being "a+c", and we want to honor commutativity
320 if (this->tinfo() == ex_to<basic>(pattern).tinfo()) {
322 // Check whether global wildcard (one that matches the "rest of the
323 // expression", like "*" above) is present
324 bool has_global_wildcard = false;
326 for (size_t i=0; i<pattern.nops(); i++) {
327 if (is_exactly_a<wildcard>(pattern.op(i))) {
328 has_global_wildcard = true;
329 global_wildcard = pattern.op(i);
334 // Unfortunately, this is an O(N^2) operation because we can't
335 // sort the pattern in a useful way...
340 for (size_t i=0; i<nops(); i++)
341 ops.push_back(op(i));
343 // Now, for every term of the pattern, look for a matching term in
344 // the expression and remove the match
345 for (size_t i=0; i<pattern.nops(); i++) {
346 ex p = pattern.op(i);
347 if (has_global_wildcard && p.is_equal(global_wildcard))
349 exvector::iterator it = ops.begin(), itend = ops.end();
350 while (it != itend) {
351 if (it->match(p, repl_lst)) {
357 return false; // no match found
361 if (has_global_wildcard) {
363 // Assign all the remaining terms to the global wildcard (unless
364 // it has already been matched before, in which case the matches
366 size_t num = ops.size();
367 epvector *vp = new epvector();
369 for (size_t i=0; i<num; i++)
370 vp->push_back(split_ex_to_pair(ops[i]));
371 ex rest = thisexpairseq(vp, default_overall_coeff());
372 for (lst::const_iterator it = repl_lst.begin(); it != repl_lst.end(); ++it) {
373 if (it->op(0).is_equal(global_wildcard))
374 return rest.is_equal(it->op(1));
376 repl_lst.append(global_wildcard == rest);
381 // No global wildcard, then the match fails if there are any
382 // unmatched terms left
386 return inherited::match(pattern, repl_lst);
389 ex expairseq::subs(const exmap & m, unsigned options) const
391 epvector *vp = subschildren(m, options);
393 return ex_to<basic>(thisexpairseq(vp, overall_coeff));
394 else if ((options & subs_options::algebraic) && is_exactly_a<mul>(*this))
395 return static_cast<const mul *>(this)->algebraic_subs_mul(m, options);
397 return subs_one_level(m, options);
402 int expairseq::compare_same_type(const basic &other) const
404 GINAC_ASSERT(is_a<expairseq>(other));
405 const expairseq &o = static_cast<const expairseq &>(other);
409 // compare number of elements
410 if (seq.size() != o.seq.size())
411 return (seq.size()<o.seq.size()) ? -1 : 1;
413 // compare overall_coeff
414 cmpval = overall_coeff.compare(o.overall_coeff);
418 #if EXPAIRSEQ_USE_HASHTAB
419 GINAC_ASSERT(hashtabsize==o.hashtabsize);
420 if (hashtabsize==0) {
421 #endif // EXPAIRSEQ_USE_HASHTAB
422 epvector::const_iterator cit1 = seq.begin();
423 epvector::const_iterator cit2 = o.seq.begin();
424 epvector::const_iterator last1 = seq.end();
425 epvector::const_iterator last2 = o.seq.end();
427 for (; (cit1!=last1)&&(cit2!=last2); ++cit1, ++cit2) {
428 cmpval = (*cit1).compare(*cit2);
429 if (cmpval!=0) return cmpval;
432 GINAC_ASSERT(cit1==last1);
433 GINAC_ASSERT(cit2==last2);
436 #if EXPAIRSEQ_USE_HASHTAB
439 // compare number of elements in each hashtab entry
440 for (unsigned i=0; i<hashtabsize; ++i) {
441 unsigned cursize=hashtab[i].size();
442 if (cursize != o.hashtab[i].size())
443 return (cursize < o.hashtab[i].size()) ? -1 : 1;
446 // compare individual (sorted) hashtab entries
447 for (unsigned i=0; i<hashtabsize; ++i) {
448 unsigned sz = hashtab[i].size();
450 const epplist &eppl1 = hashtab[i];
451 const epplist &eppl2 = o.hashtab[i];
452 epplist::const_iterator it1 = eppl1.begin();
453 epplist::const_iterator it2 = eppl2.begin();
454 while (it1!=eppl1.end()) {
455 cmpval = (*(*it1)).compare(*(*it2));
465 #endif // EXPAIRSEQ_USE_HASHTAB
468 bool expairseq::is_equal_same_type(const basic &other) const
470 const expairseq &o = static_cast<const expairseq &>(other);
472 // compare number of elements
473 if (seq.size()!=o.seq.size())
476 // compare overall_coeff
477 if (!overall_coeff.is_equal(o.overall_coeff))
480 #if EXPAIRSEQ_USE_HASHTAB
481 // compare number of elements in each hashtab entry
482 if (hashtabsize!=o.hashtabsize) {
483 std::cout << "this:" << std::endl;
484 print(print_tree(std::cout));
485 std::cout << "other:" << std::endl;
486 other.print(print_tree(std::cout));
489 GINAC_ASSERT(hashtabsize==o.hashtabsize);
491 if (hashtabsize==0) {
492 #endif // EXPAIRSEQ_USE_HASHTAB
493 epvector::const_iterator cit1 = seq.begin();
494 epvector::const_iterator cit2 = o.seq.begin();
495 epvector::const_iterator last1 = seq.end();
497 while (cit1!=last1) {
498 if (!(*cit1).is_equal(*cit2)) return false;
504 #if EXPAIRSEQ_USE_HASHTAB
507 for (unsigned i=0; i<hashtabsize; ++i) {
508 if (hashtab[i].size() != o.hashtab[i].size())
512 // compare individual sorted hashtab entries
513 for (unsigned i=0; i<hashtabsize; ++i) {
514 unsigned sz = hashtab[i].size();
516 const epplist &eppl1 = hashtab[i];
517 const epplist &eppl2 = o.hashtab[i];
518 epplist::const_iterator it1 = eppl1.begin();
519 epplist::const_iterator it2 = eppl2.begin();
520 while (it1!=eppl1.end()) {
521 if (!(*(*it1)).is_equal(*(*it2))) return false;
529 #endif // EXPAIRSEQ_USE_HASHTAB
532 unsigned expairseq::return_type() const
534 return return_types::noncommutative_composite;
537 unsigned expairseq::calchash() const
539 unsigned v = golden_ratio_hash(this->tinfo());
540 epvector::const_iterator i = seq.begin();
541 const epvector::const_iterator end = seq.end();
543 v ^= i->rest.gethash();
544 #if !EXPAIRSEQ_USE_HASHTAB
545 // rotation spoils commutativity!
547 v ^= i->coeff.gethash();
548 #endif // !EXPAIRSEQ_USE_HASHTAB
552 v ^= overall_coeff.gethash();
554 // store calculated hash value only if object is already evaluated
555 if (flags &status_flags::evaluated) {
556 setflag(status_flags::hash_calculated);
563 ex expairseq::expand(unsigned options) const
565 epvector *vp = expandchildren(options);
567 // The terms have not changed, so it is safe to declare this expanded
568 return (options == 0) ? setflag(status_flags::expanded) : *this;
570 return thisexpairseq(vp, overall_coeff);
574 // new virtual functions which can be overridden by derived classes
579 /** Create an object of this type.
580 * This method works similar to a constructor. It is useful because expairseq
581 * has (at least) two possible different semantics but we want to inherit
582 * methods thus avoiding code duplication. Sometimes a method in expairseq
583 * has to create a new one of the same semantics, which cannot be done by a
584 * ctor because the name (add, mul,...) is unknown on the expaiseq level. In
585 * order for this trick to work a derived class must of course override this
587 ex expairseq::thisexpairseq(const epvector &v, const ex &oc) const
589 return expairseq(v,oc);
592 ex expairseq::thisexpairseq(epvector *vp, const ex &oc) const
594 return expairseq(vp,oc);
597 void expairseq::printpair(const print_context & c, const expair & p, unsigned upper_precedence) const
600 p.rest.print(c, precedence());
602 p.coeff.print(c, precedence());
606 void expairseq::printseq(const print_context & c, char delim,
607 unsigned this_precedence,
608 unsigned upper_precedence) const
610 if (this_precedence <= upper_precedence)
612 epvector::const_iterator it, it_last = seq.end() - 1;
613 for (it=seq.begin(); it!=it_last; ++it) {
614 printpair(c, *it, this_precedence);
617 printpair(c, *it, this_precedence);
618 if (!overall_coeff.is_equal(default_overall_coeff())) {
620 overall_coeff.print(c, this_precedence);
623 if (this_precedence <= upper_precedence)
628 /** Form an expair from an ex, using the corresponding semantics.
629 * @see expairseq::recombine_pair_to_ex() */
630 expair expairseq::split_ex_to_pair(const ex &e) const
632 return expair(e,_ex1);
636 expair expairseq::combine_ex_with_coeff_to_pair(const ex &e,
639 GINAC_ASSERT(is_exactly_a<numeric>(c));
645 expair expairseq::combine_pair_with_coeff_to_pair(const expair &p,
648 GINAC_ASSERT(is_exactly_a<numeric>(p.coeff));
649 GINAC_ASSERT(is_exactly_a<numeric>(c));
651 return expair(p.rest,ex_to<numeric>(p.coeff).mul_dyn(ex_to<numeric>(c)));
655 /** Form an ex out of an expair, using the corresponding semantics.
656 * @see expairseq::split_ex_to_pair() */
657 ex expairseq::recombine_pair_to_ex(const expair &p) const
659 return lst(p.rest,p.coeff);
662 bool expairseq::expair_needs_further_processing(epp it)
664 #if EXPAIRSEQ_USE_HASHTAB
665 //# error "FIXME: expair_needs_further_processing not yet implemented for hashtabs, sorry. A.F."
666 #endif // EXPAIRSEQ_USE_HASHTAB
670 ex expairseq::default_overall_coeff() const
675 void expairseq::combine_overall_coeff(const ex &c)
677 GINAC_ASSERT(is_exactly_a<numeric>(overall_coeff));
678 GINAC_ASSERT(is_exactly_a<numeric>(c));
679 overall_coeff = ex_to<numeric>(overall_coeff).add_dyn(ex_to<numeric>(c));
682 void expairseq::combine_overall_coeff(const ex &c1, const ex &c2)
684 GINAC_ASSERT(is_exactly_a<numeric>(overall_coeff));
685 GINAC_ASSERT(is_exactly_a<numeric>(c1));
686 GINAC_ASSERT(is_exactly_a<numeric>(c2));
687 overall_coeff = ex_to<numeric>(overall_coeff).
688 add_dyn(ex_to<numeric>(c1).mul(ex_to<numeric>(c2)));
691 bool expairseq::can_make_flat(const expair &p) const
698 // non-virtual functions in this class
701 void expairseq::construct_from_2_ex_via_exvector(const ex &lh, const ex &rh)
707 construct_from_exvector(v);
708 #if EXPAIRSEQ_USE_HASHTAB
709 GINAC_ASSERT((hashtabsize==0)||(hashtabsize>=minhashtabsize));
710 GINAC_ASSERT(hashtabsize==calc_hashtabsize(seq.size()));
711 #endif // EXPAIRSEQ_USE_HASHTAB
714 void expairseq::construct_from_2_ex(const ex &lh, const ex &rh)
716 if (ex_to<basic>(lh).tinfo()==this->tinfo()) {
717 if (ex_to<basic>(rh).tinfo()==this->tinfo()) {
718 #if EXPAIRSEQ_USE_HASHTAB
719 unsigned totalsize = ex_to<expairseq>(lh).seq.size() +
720 ex_to<expairseq>(rh).seq.size();
721 if (calc_hashtabsize(totalsize)!=0) {
722 construct_from_2_ex_via_exvector(lh,rh);
724 #endif // EXPAIRSEQ_USE_HASHTAB
725 construct_from_2_expairseq(ex_to<expairseq>(lh),
726 ex_to<expairseq>(rh));
727 #if EXPAIRSEQ_USE_HASHTAB
729 #endif // EXPAIRSEQ_USE_HASHTAB
732 #if EXPAIRSEQ_USE_HASHTAB
733 unsigned totalsize = ex_to<expairseq>(lh).seq.size()+1;
734 if (calc_hashtabsize(totalsize)!=0) {
735 construct_from_2_ex_via_exvector(lh, rh);
737 #endif // EXPAIRSEQ_USE_HASHTAB
738 construct_from_expairseq_ex(ex_to<expairseq>(lh), rh);
739 #if EXPAIRSEQ_USE_HASHTAB
741 #endif // EXPAIRSEQ_USE_HASHTAB
744 } else if (ex_to<basic>(rh).tinfo()==this->tinfo()) {
745 #if EXPAIRSEQ_USE_HASHTAB
746 unsigned totalsize=ex_to<expairseq>(rh).seq.size()+1;
747 if (calc_hashtabsize(totalsize)!=0) {
748 construct_from_2_ex_via_exvector(lh,rh);
750 #endif // EXPAIRSEQ_USE_HASHTAB
751 construct_from_expairseq_ex(ex_to<expairseq>(rh),lh);
752 #if EXPAIRSEQ_USE_HASHTAB
754 #endif // EXPAIRSEQ_USE_HASHTAB
758 #if EXPAIRSEQ_USE_HASHTAB
759 if (calc_hashtabsize(2)!=0) {
760 construct_from_2_ex_via_exvector(lh,rh);
764 #endif // EXPAIRSEQ_USE_HASHTAB
766 if (is_exactly_a<numeric>(lh)) {
767 if (is_exactly_a<numeric>(rh)) {
768 combine_overall_coeff(lh);
769 combine_overall_coeff(rh);
771 combine_overall_coeff(lh);
772 seq.push_back(split_ex_to_pair(rh));
775 if (is_exactly_a<numeric>(rh)) {
776 combine_overall_coeff(rh);
777 seq.push_back(split_ex_to_pair(lh));
779 expair p1 = split_ex_to_pair(lh);
780 expair p2 = split_ex_to_pair(rh);
782 int cmpval = p1.rest.compare(p2.rest);
784 p1.coeff = ex_to<numeric>(p1.coeff).add_dyn(ex_to<numeric>(p2.coeff));
785 if (!ex_to<numeric>(p1.coeff).is_zero()) {
786 // no further processing is necessary, since this
787 // one element will usually be recombined in eval()
804 void expairseq::construct_from_2_expairseq(const expairseq &s1,
807 combine_overall_coeff(s1.overall_coeff);
808 combine_overall_coeff(s2.overall_coeff);
810 epvector::const_iterator first1 = s1.seq.begin();
811 epvector::const_iterator last1 = s1.seq.end();
812 epvector::const_iterator first2 = s2.seq.begin();
813 epvector::const_iterator last2 = s2.seq.end();
815 seq.reserve(s1.seq.size()+s2.seq.size());
817 bool needs_further_processing=false;
819 while (first1!=last1 && first2!=last2) {
820 int cmpval = (*first1).rest.compare((*first2).rest);
823 const numeric &newcoeff = ex_to<numeric>(first1->coeff).
824 add(ex_to<numeric>(first2->coeff));
825 if (!newcoeff.is_zero()) {
826 seq.push_back(expair(first1->rest,newcoeff));
827 if (expair_needs_further_processing(seq.end()-1)) {
828 needs_further_processing = true;
833 } else if (cmpval<0) {
834 seq.push_back(*first1);
837 seq.push_back(*first2);
842 while (first1!=last1) {
843 seq.push_back(*first1);
846 while (first2!=last2) {
847 seq.push_back(*first2);
851 if (needs_further_processing) {
854 construct_from_epvector(v);
858 void expairseq::construct_from_expairseq_ex(const expairseq &s,
861 combine_overall_coeff(s.overall_coeff);
862 if (is_exactly_a<numeric>(e)) {
863 combine_overall_coeff(e);
868 epvector::const_iterator first = s.seq.begin();
869 epvector::const_iterator last = s.seq.end();
870 expair p = split_ex_to_pair(e);
872 seq.reserve(s.seq.size()+1);
873 bool p_pushed = false;
875 bool needs_further_processing=false;
877 // merge p into s.seq
878 while (first!=last) {
879 int cmpval = (*first).rest.compare(p.rest);
882 const numeric &newcoeff = ex_to<numeric>(first->coeff).
883 add(ex_to<numeric>(p.coeff));
884 if (!newcoeff.is_zero()) {
885 seq.push_back(expair(first->rest,newcoeff));
886 if (expair_needs_further_processing(seq.end()-1))
887 needs_further_processing = true;
892 } else if (cmpval<0) {
893 seq.push_back(*first);
903 // while loop exited because p was pushed, now push rest of s.seq
904 while (first!=last) {
905 seq.push_back(*first);
909 // while loop exited because s.seq was pushed, now push p
913 if (needs_further_processing) {
916 construct_from_epvector(v);
920 void expairseq::construct_from_exvector(const exvector &v)
922 // simplifications: +(a,+(b,c),d) -> +(a,b,c,d) (associativity)
923 // +(d,b,c,a) -> +(a,b,c,d) (canonicalization)
924 // +(...,x,*(x,c1),*(x,c2)) -> +(...,*(x,1+c1+c2)) (c1, c2 numeric())
925 // (same for (+,*) -> (*,^)
928 #if EXPAIRSEQ_USE_HASHTAB
929 combine_same_terms();
932 combine_same_terms_sorted_seq();
933 #endif // EXPAIRSEQ_USE_HASHTAB
936 void expairseq::construct_from_epvector(const epvector &v)
938 // simplifications: +(a,+(b,c),d) -> +(a,b,c,d) (associativity)
939 // +(d,b,c,a) -> +(a,b,c,d) (canonicalization)
940 // +(...,x,*(x,c1),*(x,c2)) -> +(...,*(x,1+c1+c2)) (c1, c2 numeric())
941 // (same for (+,*) -> (*,^)
944 #if EXPAIRSEQ_USE_HASHTAB
945 combine_same_terms();
948 combine_same_terms_sorted_seq();
949 #endif // EXPAIRSEQ_USE_HASHTAB
952 /** Combine this expairseq with argument exvector.
953 * It cares for associativity as well as for special handling of numerics. */
954 void expairseq::make_flat(const exvector &v)
956 exvector::const_iterator cit;
958 // count number of operands which are of same expairseq derived type
959 // and their cumulative number of operands
964 while (cit!=v.end()) {
965 if (ex_to<basic>(*cit).tinfo()==this->tinfo()) {
967 noperands += ex_to<expairseq>(*cit).seq.size();
972 // reserve seq and coeffseq which will hold all operands
973 seq.reserve(v.size()+noperands-nexpairseqs);
975 // copy elements and split off numerical part
977 while (cit!=v.end()) {
978 if (ex_to<basic>(*cit).tinfo()==this->tinfo()) {
979 const expairseq &subseqref = ex_to<expairseq>(*cit);
980 combine_overall_coeff(subseqref.overall_coeff);
981 epvector::const_iterator cit_s = subseqref.seq.begin();
982 while (cit_s!=subseqref.seq.end()) {
983 seq.push_back(*cit_s);
987 if (is_exactly_a<numeric>(*cit))
988 combine_overall_coeff(*cit);
990 seq.push_back(split_ex_to_pair(*cit));
996 /** Combine this expairseq with argument epvector.
997 * It cares for associativity as well as for special handling of numerics. */
998 void expairseq::make_flat(const epvector &v)
1000 epvector::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->rest).tinfo()==this->tinfo()) {
1011 noperands += ex_to<expairseq>(cit->rest).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
1021 while (cit!=v.end()) {
1022 if (ex_to<basic>(cit->rest).tinfo()==this->tinfo() &&
1023 this->can_make_flat(*cit)) {
1024 const expairseq &subseqref = ex_to<expairseq>(cit->rest);
1025 combine_overall_coeff(ex_to<numeric>(subseqref.overall_coeff),
1026 ex_to<numeric>(cit->coeff));
1027 epvector::const_iterator cit_s = subseqref.seq.begin();
1028 while (cit_s!=subseqref.seq.end()) {
1029 seq.push_back(expair(cit_s->rest,
1030 ex_to<numeric>(cit_s->coeff).mul_dyn(ex_to<numeric>(cit->coeff))));
1031 //seq.push_back(combine_pair_with_coeff_to_pair(*cit_s,
1036 if (cit->is_canonical_numeric())
1037 combine_overall_coeff(cit->rest);
1039 seq.push_back(*cit);
1045 /** Brings this expairseq into a sorted (canonical) form. */
1046 void expairseq::canonicalize()
1048 std::sort(seq.begin(), seq.end(), expair_rest_is_less());
1052 /** Compact a presorted expairseq by combining all matching expairs to one
1053 * each. On an add object, this is responsible for 2*x+3*x+y -> 5*x+y, for
1055 void expairseq::combine_same_terms_sorted_seq()
1060 bool needs_further_processing = false;
1062 epvector::iterator itin1 = seq.begin();
1063 epvector::iterator itin2 = itin1+1;
1064 epvector::iterator itout = itin1;
1065 epvector::iterator last = seq.end();
1066 // must_copy will be set to true the first time some combination is
1067 // possible from then on the sequence has changed and must be compacted
1068 bool must_copy = false;
1069 while (itin2!=last) {
1070 if (itin1->rest.compare(itin2->rest)==0) {
1071 itin1->coeff = ex_to<numeric>(itin1->coeff).
1072 add_dyn(ex_to<numeric>(itin2->coeff));
1073 if (expair_needs_further_processing(itin1))
1074 needs_further_processing = true;
1077 if (!ex_to<numeric>(itin1->coeff).is_zero()) {
1086 if (!ex_to<numeric>(itin1->coeff).is_zero()) {
1092 seq.erase(itout,last);
1094 if (needs_further_processing) {
1097 construct_from_epvector(v);
1101 #if EXPAIRSEQ_USE_HASHTAB
1103 unsigned expairseq::calc_hashtabsize(unsigned sz) const
1106 unsigned nearest_power_of_2 = 1 << log2(sz);
1107 // if (nearest_power_of_2 < maxhashtabsize/hashtabfactor) {
1108 // size = nearest_power_of_2*hashtabfactor;
1109 size = nearest_power_of_2/hashtabfactor;
1110 if (size<minhashtabsize)
1113 // hashtabsize must be a power of 2
1114 GINAC_ASSERT((1U << log2(size))==size);
1118 unsigned expairseq::calc_hashindex(const ex &e) const
1120 // calculate hashindex
1122 if (is_a<numeric>(e)) {
1123 hashindex = hashmask;
1125 hashindex = e.gethash() & hashmask;
1126 // last hashtab entry is reserved for numerics
1127 if (hashindex==hashmask) hashindex = 0;
1129 GINAC_ASSERT((hashindex<hashtabsize)||(hashtabsize==0));
1133 void expairseq::shrink_hashtab()
1135 unsigned new_hashtabsize;
1136 while (hashtabsize!=(new_hashtabsize=calc_hashtabsize(seq.size()))) {
1137 GINAC_ASSERT(new_hashtabsize<hashtabsize);
1138 if (new_hashtabsize==0) {
1145 // shrink by a factor of 2
1146 unsigned half_hashtabsize = hashtabsize/2;
1147 for (unsigned i=0; i<half_hashtabsize-1; ++i)
1148 hashtab[i].merge(hashtab[i+half_hashtabsize],epp_is_less());
1149 // special treatment for numeric hashes
1150 hashtab[0].merge(hashtab[half_hashtabsize-1],epp_is_less());
1151 hashtab[half_hashtabsize-1] = hashtab[hashtabsize-1];
1152 hashtab.resize(half_hashtabsize);
1153 hashtabsize = half_hashtabsize;
1154 hashmask = hashtabsize-1;
1158 void expairseq::remove_hashtab_entry(epvector::const_iterator element)
1161 return; // nothing to do
1163 // calculate hashindex of element to be deleted
1164 unsigned hashindex = calc_hashindex((*element).rest);
1166 // find it in hashtab and remove it
1167 epplist &eppl = hashtab[hashindex];
1168 epplist::iterator epplit = eppl.begin();
1169 bool erased = false;
1170 while (epplit!=eppl.end()) {
1171 if (*epplit == element) {
1179 std::cout << "tried to erase " << element-seq.begin() << std::endl;
1180 std::cout << "size " << seq.end()-seq.begin() << std::endl;
1182 unsigned hashindex = calc_hashindex(element->rest);
1183 epplist &eppl = hashtab[hashindex];
1184 epplist::iterator epplit = eppl.begin();
1185 bool erased = false;
1186 while (epplit!=eppl.end()) {
1187 if (*epplit == element) {
1194 GINAC_ASSERT(erased);
1196 GINAC_ASSERT(erased);
1199 void expairseq::move_hashtab_entry(epvector::const_iterator oldpos,
1200 epvector::iterator newpos)
1202 GINAC_ASSERT(hashtabsize!=0);
1204 // calculate hashindex of element which was moved
1205 unsigned hashindex=calc_hashindex((*newpos).rest);
1207 // find it in hashtab and modify it
1208 epplist &eppl = hashtab[hashindex];
1209 epplist::iterator epplit = eppl.begin();
1210 while (epplit!=eppl.end()) {
1211 if (*epplit == oldpos) {
1217 GINAC_ASSERT(epplit!=eppl.end());
1220 void expairseq::sorted_insert(epplist &eppl, epvector::const_iterator elem)
1222 epplist::const_iterator current = eppl.begin();
1223 while ((current!=eppl.end()) && ((*current)->is_less(*elem))) {
1226 eppl.insert(current,elem);
1229 void expairseq::build_hashtab_and_combine(epvector::iterator &first_numeric,
1230 epvector::iterator &last_non_zero,
1231 std::vector<bool> &touched,
1232 unsigned &number_of_zeroes)
1234 epp current = seq.begin();
1236 while (current!=first_numeric) {
1237 if (is_exactly_a<numeric>(current->rest)) {
1239 iter_swap(current,first_numeric);
1241 // calculate hashindex
1242 unsigned currenthashindex = calc_hashindex(current->rest);
1244 // test if there is already a matching expair in the hashtab-list
1245 epplist &eppl=hashtab[currenthashindex];
1246 epplist::iterator epplit = eppl.begin();
1247 while (epplit!=eppl.end()) {
1248 if (current->rest.is_equal((*epplit)->rest))
1252 if (epplit==eppl.end()) {
1253 // no matching expair found, append this to end of list
1254 sorted_insert(eppl,current);
1257 // epplit points to a matching expair, combine it with current
1258 (*epplit)->coeff = ex_to<numeric>((*epplit)->coeff).
1259 add_dyn(ex_to<numeric>(current->coeff));
1261 // move obsolete current expair to end by swapping with last_non_zero element
1262 // if this was a numeric, it is swapped with the expair before first_numeric
1263 iter_swap(current,last_non_zero);
1265 if (first_numeric!=last_non_zero) iter_swap(first_numeric,current);
1268 // test if combined term has coeff 0 and can be removed is done later
1269 touched[(*epplit)-seq.begin()] = true;
1275 void expairseq::drop_coeff_0_terms(epvector::iterator &first_numeric,
1276 epvector::iterator &last_non_zero,
1277 std::vector<bool> &touched,
1278 unsigned &number_of_zeroes)
1280 // move terms with coeff 0 to end and remove them from hashtab
1281 // check only those elements which have been touched
1282 epp current = seq.begin();
1284 while (current!=first_numeric) {
1288 } else if (!ex_to<numeric>((*current).coeff).is_zero()) {
1292 remove_hashtab_entry(current);
1294 // move element to the end, unless it is already at the end
1295 if (current!=last_non_zero) {
1296 iter_swap(current,last_non_zero);
1298 bool numeric_swapped = first_numeric!=last_non_zero;
1299 if (numeric_swapped)
1300 iter_swap(first_numeric,current);
1301 epvector::iterator changed_entry;
1303 if (numeric_swapped)
1304 changed_entry = first_numeric;
1306 changed_entry = last_non_zero;
1311 if (first_numeric!=current) {
1313 // change entry in hashtab which referred to first_numeric or last_non_zero to current
1314 move_hashtab_entry(changed_entry,current);
1315 touched[current-seq.begin()] = touched[changed_entry-seq.begin()];
1324 GINAC_ASSERT(i==current-seq.begin());
1327 /** True if one of the coeffs vanishes, otherwise false.
1328 * This would be an invariant violation, so this should only be used for
1329 * debugging purposes. */
1330 bool expairseq::has_coeff_0() const
1332 epvector::const_iterator i = seq.begin(), end = seq.end();
1334 if (i->coeff.is_zero())
1341 void expairseq::add_numerics_to_hashtab(epvector::iterator first_numeric,
1342 epvector::const_iterator last_non_zero)
1344 if (first_numeric == seq.end()) return; // no numerics
1346 epvector::const_iterator current = first_numeric, last = last_non_zero + 1;
1347 while (current != last) {
1348 sorted_insert(hashtab[hashmask], current);
1353 void expairseq::combine_same_terms()
1355 // combine same terms, drop term with coeff 0, move numerics to end
1357 // calculate size of hashtab
1358 hashtabsize = calc_hashtabsize(seq.size());
1360 // hashtabsize is a power of 2
1361 hashmask = hashtabsize-1;
1365 hashtab.resize(hashtabsize);
1367 if (hashtabsize==0) {
1369 combine_same_terms_sorted_seq();
1370 GINAC_ASSERT(!has_coeff_0());
1374 // iterate through seq, move numerics to end,
1375 // fill hashtab and combine same terms
1376 epvector::iterator first_numeric = seq.end();
1377 epvector::iterator last_non_zero = seq.end()-1;
1379 size_t num = seq.size();
1380 std::vector<bool> touched(num);
1382 unsigned number_of_zeroes = 0;
1384 GINAC_ASSERT(!has_coeff_0());
1385 build_hashtab_and_combine(first_numeric,last_non_zero,touched,number_of_zeroes);
1387 // there should not be any terms with coeff 0 from the beginning,
1388 // so it should be safe to skip this step
1389 if (number_of_zeroes!=0) {
1390 drop_coeff_0_terms(first_numeric,last_non_zero,touched,number_of_zeroes);
1393 add_numerics_to_hashtab(first_numeric,last_non_zero);
1395 // pop zero elements
1396 for (unsigned i=0; i<number_of_zeroes; ++i) {
1400 // shrink hashtabsize to calculated value
1401 GINAC_ASSERT(!has_coeff_0());
1405 GINAC_ASSERT(!has_coeff_0());
1408 #endif // EXPAIRSEQ_USE_HASHTAB
1410 /** Check if this expairseq is in sorted (canonical) form. Useful mainly for
1411 * debugging or in assertions since being sorted is an invariance. */
1412 bool expairseq::is_canonical() const
1414 if (seq.size() <= 1)
1417 #if EXPAIRSEQ_USE_HASHTAB
1418 if (hashtabsize > 0) return 1; // not canoncalized
1419 #endif // EXPAIRSEQ_USE_HASHTAB
1421 epvector::const_iterator it = seq.begin(), itend = seq.end();
1422 epvector::const_iterator it_last = it;
1423 for (++it; it!=itend; it_last=it, ++it) {
1424 if (!(it_last->is_less(*it) || it_last->is_equal(*it))) {
1425 if (!is_exactly_a<numeric>(it_last->rest) ||
1426 !is_exactly_a<numeric>(it->rest)) {
1427 // double test makes it easier to set a breakpoint...
1428 if (!is_exactly_a<numeric>(it_last->rest) ||
1429 !is_exactly_a<numeric>(it->rest)) {
1430 printpair(std::clog, *it_last, 0);
1432 printpair(std::clog, *it, 0);
1434 std::clog << "pair1:" << std::endl;
1435 it_last->rest.print(print_tree(std::clog));
1436 it_last->coeff.print(print_tree(std::clog));
1437 std::clog << "pair2:" << std::endl;
1438 it->rest.print(print_tree(std::clog));
1439 it->coeff.print(print_tree(std::clog));
1449 /** Member-wise expand the expairs in this sequence.
1451 * @see expairseq::expand()
1452 * @return pointer to epvector containing expanded pairs or zero pointer,
1453 * if no members were changed. */
1454 epvector * expairseq::expandchildren(unsigned options) const
1456 const epvector::const_iterator last = seq.end();
1457 epvector::const_iterator cit = seq.begin();
1459 const ex &expanded_ex = cit->rest.expand(options);
1460 if (!are_ex_trivially_equal(cit->rest,expanded_ex)) {
1462 // something changed, copy seq, eval and return it
1463 epvector *s = new epvector;
1464 s->reserve(seq.size());
1466 // copy parts of seq which are known not to have changed
1467 epvector::const_iterator cit2 = seq.begin();
1469 s->push_back(*cit2);
1472 // copy first changed element
1473 s->push_back(combine_ex_with_coeff_to_pair(expanded_ex,
1477 while (cit2!=last) {
1478 s->push_back(combine_ex_with_coeff_to_pair(cit2->rest.expand(options),
1487 return 0; // signalling nothing has changed
1491 /** Member-wise evaluate the expairs in this sequence.
1493 * @see expairseq::eval()
1494 * @return pointer to epvector containing evaluated pairs or zero pointer,
1495 * if no members were changed. */
1496 epvector * expairseq::evalchildren(int level) const
1498 // returns a NULL pointer if nothing had to be evaluated
1499 // returns a pointer to a newly created epvector otherwise
1500 // (which has to be deleted somewhere else)
1505 if (level == -max_recursion_level)
1506 throw(std::runtime_error("max recursion level reached"));
1509 epvector::const_iterator last = seq.end();
1510 epvector::const_iterator cit = seq.begin();
1512 const ex &evaled_ex = cit->rest.eval(level);
1513 if (!are_ex_trivially_equal(cit->rest,evaled_ex)) {
1515 // something changed, copy seq, eval and return it
1516 epvector *s = new epvector;
1517 s->reserve(seq.size());
1519 // copy parts of seq which are known not to have changed
1520 epvector::const_iterator cit2=seq.begin();
1522 s->push_back(*cit2);
1525 // copy first changed element
1526 s->push_back(combine_ex_with_coeff_to_pair(evaled_ex,
1530 while (cit2!=last) {
1531 s->push_back(combine_ex_with_coeff_to_pair(cit2->rest.eval(level),
1540 return 0; // signalling nothing has changed
1544 /** Member-wise substitute in this sequence.
1546 * @see expairseq::subs()
1547 * @return pointer to epvector containing pairs after application of subs,
1548 * or NULL pointer if no members were changed. */
1549 epvector * expairseq::subschildren(const exmap & m, unsigned options) const
1551 // When any of the objects to be substituted is a product or power
1552 // we have to recombine the pairs because the numeric coefficients may
1553 // be part of the search pattern.
1554 if (!(options & (subs_options::pattern_is_product | subs_options::pattern_is_not_product))) {
1556 // Search the list of substitutions and cache our findings
1557 for (exmap::const_iterator it = m.begin(); it != m.end(); ++it) {
1558 if (is_exactly_a<mul>(it->first) || is_exactly_a<power>(it->first)) {
1559 options |= subs_options::pattern_is_product;
1563 if (!(options & subs_options::pattern_is_product))
1564 options |= subs_options::pattern_is_not_product;
1567 if (options & subs_options::pattern_is_product) {
1569 // Substitute in the recombined pairs
1570 epvector::const_iterator cit = seq.begin(), last = seq.end();
1571 while (cit != last) {
1573 const ex &orig_ex = recombine_pair_to_ex(*cit);
1574 const ex &subsed_ex = orig_ex.subs(m, options);
1575 if (!are_ex_trivially_equal(orig_ex, subsed_ex)) {
1577 // Something changed, copy seq, subs and return it
1578 epvector *s = new epvector;
1579 s->reserve(seq.size());
1581 // Copy parts of seq which are known not to have changed
1582 s->insert(s->begin(), seq.begin(), cit);
1584 // Copy first changed element
1585 s->push_back(split_ex_to_pair(subsed_ex));
1589 while (cit != last) {
1590 s->push_back(split_ex_to_pair(recombine_pair_to_ex(*cit).subs(m, options)));
1601 // Substitute only in the "rest" part of the pairs
1602 epvector::const_iterator cit = seq.begin(), last = seq.end();
1603 while (cit != last) {
1605 const ex &subsed_ex = cit->rest.subs(m, options);
1606 if (!are_ex_trivially_equal(cit->rest, subsed_ex)) {
1608 // Something changed, copy seq, subs and return it
1609 epvector *s = new epvector;
1610 s->reserve(seq.size());
1612 // Copy parts of seq which are known not to have changed
1613 s->insert(s->begin(), seq.begin(), cit);
1615 // Copy first changed element
1616 s->push_back(combine_ex_with_coeff_to_pair(subsed_ex, cit->coeff));
1620 while (cit != last) {
1621 s->push_back(combine_ex_with_coeff_to_pair(cit->rest.subs(m, options),
1632 // Nothing has changed
1637 // static member variables
1640 #if EXPAIRSEQ_USE_HASHTAB
1641 unsigned expairseq::maxhashtabsize = 0x4000000U;
1642 unsigned expairseq::minhashtabsize = 0x1000U;
1643 unsigned expairseq::hashtabfactor = 1;
1644 #endif // EXPAIRSEQ_USE_HASHTAB
1646 } // namespace GiNaC