1 /** @file expairseq.cpp
3 * Implementation of sequences of expression pairs. */
6 * GiNaC Copyright (C) 1999-2000 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"
34 #ifndef NO_NAMESPACE_GINAC
36 #endif // ndef NO_NAMESPACE_GINAC
38 #ifdef EXPAIRSEQ_USE_HASHTAB
39 #error "FIXME: expair_needs_further_processing not yet implemented for hashtabs, sorry. A.F."
40 #endif // def EXPAIRSEQ_USE_HASHTAB
42 GINAC_IMPLEMENT_REGISTERED_CLASS(expairseq, basic)
51 bool operator()(const epp & lh, const epp & rh) const
53 return (*lh).is_less(*rh);
58 // default constructor, destructor, copy constructor assignment operator and helpers
63 expairseq::expairseq(const expairseq & other)
65 debugmsg("expairseq copy constructor",LOGLEVEL_CONSTRUCT);
69 const expairseq & expairseq::operator=(const expairseq & other)
71 debugmsg("expairseq operator=",LOGLEVEL_ASSIGNMENT);
81 void expairseq::copy(const expairseq & other)
83 inherited::copy(other);
85 overall_coeff=other.overall_coeff;
86 #ifdef 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 // def EXPAIRSEQ_USE_HASHTAB
107 // other constructors
110 expairseq::expairseq(const ex & lh, const ex & rh) : inherited(TINFO_expairseq)
112 debugmsg("expairseq constructor from ex,ex",LOGLEVEL_CONSTRUCT);
113 construct_from_2_ex(lh,rh);
114 GINAC_ASSERT(is_canonical());
117 expairseq::expairseq(const exvector & v) : inherited(TINFO_expairseq)
119 debugmsg("expairseq constructor from exvector",LOGLEVEL_CONSTRUCT);
120 construct_from_exvector(v);
121 GINAC_ASSERT(is_canonical());
125 expairseq::expairseq(const epvector & v, bool do_not_canonicalize) :
126 inherited(TINFO_expairseq)
128 debugmsg("expairseq constructor from epvector",LOGLEVEL_CONSTRUCT);
129 if (do_not_canonicalize) {
131 #ifdef EXPAIRSEQ_USE_HASHTAB
132 combine_same_terms(); // to build hashtab
133 #endif // def EXPAIRSEQ_USE_HASHTAB
135 construct_from_epvector(v);
137 GINAC_ASSERT(is_canonical());
141 expairseq::expairseq(const epvector & v, const ex & oc) :
142 inherited(TINFO_expairseq), overall_coeff(oc)
144 debugmsg("expairseq constructor from epvector,ex",LOGLEVEL_CONSTRUCT);
145 construct_from_epvector(v);
146 GINAC_ASSERT(is_canonical());
149 expairseq::expairseq(epvector * vp, const ex & oc) :
150 inherited(TINFO_expairseq), overall_coeff(oc)
152 debugmsg("expairseq constructor from epvector *,ex",LOGLEVEL_CONSTRUCT);
154 construct_from_epvector(*vp);
156 GINAC_ASSERT(is_canonical());
163 /** Construct object from archive_node. */
164 expairseq::expairseq(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
165 #ifdef EXPAIRSEQ_USE_HASHTAB
169 debugmsg("expairseq constructor from archive_node", LOGLEVEL_CONSTRUCT);
170 for (unsigned int i=0; true; i++) {
173 if (n.find_ex("rest", rest, sym_lst, i) && n.find_ex("coeff", coeff, sym_lst, i))
174 seq.push_back(expair(rest, coeff));
178 n.find_ex("overall_coeff", overall_coeff, sym_lst);
181 /** Unarchive the object. */
182 ex expairseq::unarchive(const archive_node &n, const lst &sym_lst)
184 return (new expairseq(n, sym_lst))->setflag(status_flags::dynallocated);
187 /** Archive the object. */
188 void expairseq::archive(archive_node &n) const
190 inherited::archive(n);
191 epvector::const_iterator i = seq.begin(), iend = seq.end();
193 n.add_ex("rest", i->rest);
194 n.add_ex("coeff", i->coeff);
197 n.add_ex("overall_coeff", overall_coeff);
201 // functions overriding virtual functions from bases classes
206 basic * expairseq::duplicate() const
208 debugmsg("expairseq duplicate",LOGLEVEL_DUPLICATE);
209 return new expairseq(*this);
212 void expairseq::print(ostream & os, unsigned upper_precedence) const
214 debugmsg("expairseq print",LOGLEVEL_PRINT);
216 printseq(os,',',precedence,upper_precedence);
220 void expairseq::printraw(ostream & os) const
222 debugmsg("expairseq printraw",LOGLEVEL_PRINT);
225 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
227 (*cit).rest.printraw(os);
229 (*cit).coeff.printraw(os);
235 void expairseq::printtree(ostream & os, unsigned indent) const
237 debugmsg("expairseq printtree",LOGLEVEL_PRINT);
239 os << string(indent,' ') << "type=" << typeid(*this).name()
240 << ", hash=" << hashvalue << " (0x" << hex << hashvalue << dec << ")"
241 << ", flags=" << flags
242 << ", nops=" << nops() << endl;
243 for (unsigned i=0; i<seq.size(); ++i) {
244 seq[i].rest.printtree(os,indent+delta_indent);
245 seq[i].coeff.printtree(os,indent+delta_indent);
246 if (i!=seq.size()-1) {
247 os << string(indent+delta_indent,' ') << "-----" << endl;
250 if (!overall_coeff.is_equal(default_overall_coeff())) {
251 os << string(indent+delta_indent,' ') << "-----" << endl;
252 os << string(indent+delta_indent,' ') << "overall_coeff" << endl;
253 overall_coeff.printtree(os,indent+delta_indent);
255 os << string(indent+delta_indent,' ') << "=====" << endl;
256 #ifdef EXPAIRSEQ_USE_HASHTAB
257 os << string(indent+delta_indent,' ')
258 << "hashtab size " << hashtabsize << endl;
259 if (hashtabsize==0) return;
261 unsigned count[MAXCOUNT+1];
262 for (int i=0; i<MAXCOUNT+1; ++i) count[i]=0;
263 unsigned this_bin_fill;
264 unsigned cum_fill_sq=0;
266 for (unsigned i=0; i<hashtabsize; ++i) {
268 if (hashtab[i].size()>0) {
269 os << string(indent+delta_indent,' ')
270 << "bin " << i << " with entries ";
271 for (epplist::const_iterator it=hashtab[i].begin();
272 it!=hashtab[i].end(); ++it) {
273 os << *it-seq.begin() << " ";
277 cum_fill += this_bin_fill;
278 cum_fill_sq += this_bin_fill*this_bin_fill;
280 if (this_bin_fill<MAXCOUNT) {
281 ++count[this_bin_fill];
288 double lambda=(1.0*seq.size())/hashtabsize;
289 for (int k=0; k<MAXCOUNT; ++k) {
291 double prob=pow(lambda,k)/fact*exp(-lambda);
293 os << string(indent+delta_indent,' ') << "bins with " << k << " entries: "
294 << int(1000.0*count[k]/hashtabsize)/10.0 << "% (expected: "
295 << int(prob*1000)/10.0 << ")" << endl;
297 os << string(indent+delta_indent,' ') << "bins with more entries: "
298 << int(1000.0*count[MAXCOUNT]/hashtabsize)/10.0 << "% (expected: "
299 << int((1-cum_prob)*1000)/10.0 << ")" << endl;
301 os << string(indent+delta_indent,' ') << "variance: "
302 << 1.0/hashtabsize*cum_fill_sq-(1.0/hashtabsize*cum_fill)*(1.0/hashtabsize*cum_fill)
304 os << string(indent+delta_indent,' ') << "average fill: "
305 << (1.0*cum_fill)/hashtabsize
306 << " (should be equal to " << (1.0*seq.size())/hashtabsize << ")" << endl;
307 #endif // def EXPAIRSEQ_USE_HASHTAB
310 bool expairseq::info(unsigned inf) const
312 return inherited::info(inf);
315 unsigned expairseq::nops() const
317 if (overall_coeff.is_equal(default_overall_coeff())) {
323 ex expairseq::op(int i) const
325 if (unsigned(i)<seq.size()) {
326 return recombine_pair_to_ex(seq[i]);
328 GINAC_ASSERT(!overall_coeff.is_equal(default_overall_coeff()));
329 return overall_coeff;
332 ex & expairseq::let_op(int i)
334 throw(std::logic_error("let_op not defined for expairseq and derived classes (add,mul,...)"));
337 ex expairseq::eval(int level) const
339 if ((level==1)&&(flags & status_flags::evaluated)) {
343 epvector * vp=evalchildren(level);
348 return (new expairseq(vp,overall_coeff))
349 ->setflag(status_flags::dynallocated |
350 status_flags::evaluated );
353 ex expairseq::evalf(int level) const
355 return thisexpairseq(evalfchildren(level),overall_coeff);
358 ex expairseq::normal(lst &sym_lst, lst &repl_lst, int level) const
360 ex n=thisexpairseq(normalchildren(level),overall_coeff);
361 return n.bp->basic::normal(sym_lst,repl_lst,level);
364 ex expairseq::subs(const lst & ls, const lst & lr) const
366 epvector * vp=subschildren(ls,lr);
370 return thisexpairseq(vp,overall_coeff);
375 int expairseq::compare_same_type(const basic & other) const
377 GINAC_ASSERT(is_of_type(other, expairseq));
378 const expairseq & o=static_cast<const expairseq &>(const_cast<basic &>(other));
382 // compare number of elements
383 if (seq.size() != o.seq.size()) {
384 return (seq.size()<o.seq.size()) ? -1 : 1;
387 // compare overall_coeff
388 cmpval=overall_coeff.compare(o.overall_coeff);
389 if (cmpval!=0) return cmpval;
391 //if (seq.size()==0) return 0; // empty expairseq's are equal
393 #ifdef EXPAIRSEQ_USE_HASHTAB
394 GINAC_ASSERT(hashtabsize==o.hashtabsize);
395 if (hashtabsize==0) {
396 #endif // def EXPAIRSEQ_USE_HASHTAB
397 epvector::const_iterator cit1=seq.begin();
398 epvector::const_iterator cit2=o.seq.begin();
399 epvector::const_iterator last1=seq.end();
400 epvector::const_iterator last2=o.seq.end();
402 for (; (cit1!=last1)&&(cit2!=last2); ++cit1, ++cit2) {
403 cmpval=(*cit1).compare(*cit2);
404 if (cmpval!=0) return cmpval;
407 GINAC_ASSERT(cit1==last1);
408 GINAC_ASSERT(cit2==last2);
411 #ifdef EXPAIRSEQ_USE_HASHTAB
414 // compare number of elements in each hashtab entry
415 for (unsigned i=0; i<hashtabsize; ++i) {
416 unsigned cursize=hashtab[i].size();
417 if (cursize != o.hashtab[i].size()) {
418 return (cursize < o.hashtab[i].size()) ? -1 : 1;
422 // compare individual (sorted) hashtab entries
423 for (unsigned i=0; i<hashtabsize; ++i) {
424 unsigned sz=hashtab[i].size();
426 const epplist & eppl1=hashtab[i];
427 const epplist & eppl2=o.hashtab[i];
428 epplist::const_iterator it1=eppl1.begin();
429 epplist::const_iterator it2=eppl2.begin();
430 while (it1!=eppl1.end()) {
431 cmpval=(*(*it1)).compare(*(*it2));
432 if (cmpval!=0) return cmpval;
440 #endif // def EXPAIRSEQ_USE_HASHTAB
443 bool expairseq::is_equal_same_type(const basic & other) const
445 const expairseq & o=dynamic_cast<const expairseq &>(const_cast<basic &>(other));
447 // compare number of elements
448 if (seq.size() != o.seq.size()) return false;
450 // compare overall_coeff
451 if (!overall_coeff.is_equal(o.overall_coeff)) return false;
453 #ifdef EXPAIRSEQ_USE_HASHTAB
454 // compare number of elements in each hashtab entry
455 if (hashtabsize!=o.hashtabsize) {
456 cout << "this:" << endl;
458 cout << "other:" << endl;
459 other.printtree(cout,0);
462 GINAC_ASSERT(hashtabsize==o.hashtabsize);
464 if (hashtabsize==0) {
465 #endif // def EXPAIRSEQ_USE_HASHTAB
466 epvector::const_iterator cit1=seq.begin();
467 epvector::const_iterator cit2=o.seq.begin();
468 epvector::const_iterator last1=seq.end();
470 while (cit1!=last1) {
471 if (!(*cit1).is_equal(*cit2)) return false;
477 #ifdef EXPAIRSEQ_USE_HASHTAB
480 for (unsigned i=0; i<hashtabsize; ++i) {
481 if (hashtab[i].size() != o.hashtab[i].size()) return false;
484 // compare individual sorted hashtab entries
485 for (unsigned i=0; i<hashtabsize; ++i) {
486 unsigned sz=hashtab[i].size();
488 const epplist & eppl1=hashtab[i];
489 const epplist & eppl2=o.hashtab[i];
490 epplist::const_iterator it1=eppl1.begin();
491 epplist::const_iterator it2=eppl2.begin();
492 while (it1!=eppl1.end()) {
493 if (!(*(*it1)).is_equal(*(*it2))) return false;
501 #endif // def EXPAIRSEQ_USE_HASHTAB
504 unsigned expairseq::return_type(void) const
506 return return_types::noncommutative_composite;
509 unsigned expairseq::calchash(void) const
511 unsigned v=golden_ratio_hash(tinfo());
512 epvector::const_iterator last=seq.end();
513 for (epvector::const_iterator cit=seq.begin(); cit!=last; ++cit) {
514 #ifndef EXPAIRSEQ_USE_HASHTAB
515 v=rotate_left_31(v); // rotation would spoil commutativity
516 #endif // ndef EXPAIRSEQ_USE_HASHTAB
517 v ^= (*cit).rest.gethash();
520 v ^= overall_coeff.gethash();
523 // store calculated hash value only if object is already evaluated
524 if (flags & status_flags::evaluated) {
525 setflag(status_flags::hash_calculated);
532 ex expairseq::expand(unsigned options) const
534 epvector * vp=expandchildren(options);
538 return thisexpairseq(vp,overall_coeff);
542 // new virtual functions which can be overridden by derived classes
547 ex expairseq::thisexpairseq(const epvector & v,const ex & oc) const
549 return expairseq(v,oc);
552 ex expairseq::thisexpairseq(epvector * vp, const ex & oc) const
554 return expairseq(vp,oc);
557 void expairseq::printpair(ostream & os, const expair & p, unsigned upper_precedence) const
560 p.rest.bp->print(os,precedence);
562 p.coeff.bp->print(os,precedence);
566 void expairseq::printseq(ostream & os, char delim, unsigned this_precedence,
567 unsigned upper_precedence) const
569 if (this_precedence<=upper_precedence) os << "(";
570 epvector::const_iterator it,it_last;
573 for (it=seq.begin(); it!=it_last; ++it) {
574 printpair(os,*it,this_precedence);
577 printpair(os,*it,this_precedence);
578 if (!overall_coeff.is_equal(default_overall_coeff())) {
579 os << delim << overall_coeff;
581 if (this_precedence<=upper_precedence) os << ")";
584 expair expairseq::split_ex_to_pair(const ex & e) const
586 return expair(e,_ex1());
589 expair expairseq::combine_ex_with_coeff_to_pair(const ex & e,
592 GINAC_ASSERT(is_ex_exactly_of_type(c,numeric));
597 expair expairseq::combine_pair_with_coeff_to_pair(const expair & p,
600 GINAC_ASSERT(is_ex_exactly_of_type(p.coeff,numeric));
601 GINAC_ASSERT(is_ex_exactly_of_type(c,numeric));
603 return expair(p.rest,ex_to_numeric(p.coeff).mul_dyn(ex_to_numeric(c)));
606 ex expairseq::recombine_pair_to_ex(const expair & p) const
608 return lst(p.rest,p.coeff);
611 bool expairseq::expair_needs_further_processing(epp it)
616 ex expairseq::default_overall_coeff(void) const
621 void expairseq::combine_overall_coeff(const ex & c)
623 GINAC_ASSERT(is_ex_exactly_of_type(overall_coeff,numeric));
624 GINAC_ASSERT(is_ex_exactly_of_type(c,numeric));
625 overall_coeff = ex_to_numeric(overall_coeff).add_dyn(ex_to_numeric(c));
628 void expairseq::combine_overall_coeff(const ex & c1, const ex & c2)
630 GINAC_ASSERT(is_ex_exactly_of_type(overall_coeff,numeric));
631 GINAC_ASSERT(is_ex_exactly_of_type(c1,numeric));
632 GINAC_ASSERT(is_ex_exactly_of_type(c2,numeric));
633 overall_coeff = ex_to_numeric(overall_coeff).
634 add_dyn(ex_to_numeric(c1).mul(ex_to_numeric(c2)));
637 bool expairseq::can_make_flat(const expair & p) const
644 // non-virtual functions in this class
647 void expairseq::construct_from_2_ex_via_exvector(const ex & lh, const ex & rh)
653 construct_from_exvector(v);
654 #ifdef EXPAIRSEQ_USE_HASHTAB
655 GINAC_ASSERT((hashtabsize==0)||(hashtabsize>=minhashtabsize));
656 GINAC_ASSERT(hashtabsize==calc_hashtabsize(seq.size()));
657 #endif // def EXPAIRSEQ_USE_HASHTAB
660 void expairseq::construct_from_2_ex(const ex & lh, const ex & rh)
662 if (lh.bp->tinfo()==tinfo()) {
663 if (rh.bp->tinfo()==tinfo()) {
664 #ifdef EXPAIRSEQ_USE_HASHTAB
665 unsigned totalsize=ex_to_expairseq(lh).seq.size()+
666 ex_to_expairseq(rh).seq.size();
667 if (calc_hashtabsize(totalsize)!=0) {
668 construct_from_2_ex_via_exvector(lh,rh);
670 #endif // def EXPAIRSEQ_USE_HASHTAB
671 construct_from_2_expairseq(ex_to_expairseq(lh),
672 ex_to_expairseq(rh));
673 #ifdef EXPAIRSEQ_USE_HASHTAB
675 #endif // def EXPAIRSEQ_USE_HASHTAB
678 #ifdef EXPAIRSEQ_USE_HASHTAB
679 unsigned totalsize=ex_to_expairseq(lh).seq.size()+1;
680 if (calc_hashtabsize(totalsize)!=0) {
681 construct_from_2_ex_via_exvector(lh,rh);
683 #endif // def EXPAIRSEQ_USE_HASHTAB
684 construct_from_expairseq_ex(ex_to_expairseq(lh),rh);
685 #ifdef EXPAIRSEQ_USE_HASHTAB
687 #endif // def EXPAIRSEQ_USE_HASHTAB
690 } else if (rh.bp->tinfo()==tinfo()) {
691 #ifdef EXPAIRSEQ_USE_HASHTAB
692 unsigned totalsize=ex_to_expairseq(rh).seq.size()+1;
693 if (calc_hashtabsize(totalsize)!=0) {
694 construct_from_2_ex_via_exvector(lh,rh);
696 #endif // def EXPAIRSEQ_USE_HASHTAB
697 construct_from_expairseq_ex(ex_to_expairseq(rh),lh);
698 #ifdef EXPAIRSEQ_USE_HASHTAB
700 #endif // def EXPAIRSEQ_USE_HASHTAB
704 #ifdef EXPAIRSEQ_USE_HASHTAB
705 if (calc_hashtabsize(2)!=0) {
706 construct_from_2_ex_via_exvector(lh,rh);
710 #endif // def EXPAIRSEQ_USE_HASHTAB
712 if (is_ex_exactly_of_type(lh,numeric)) {
713 if (is_ex_exactly_of_type(rh,numeric)) {
714 combine_overall_coeff(lh);
715 combine_overall_coeff(rh);
717 combine_overall_coeff(lh);
718 seq.push_back(split_ex_to_pair(rh));
721 if (is_ex_exactly_of_type(rh,numeric)) {
722 combine_overall_coeff(rh);
723 seq.push_back(split_ex_to_pair(lh));
725 expair p1=split_ex_to_pair(lh);
726 expair p2=split_ex_to_pair(rh);
728 int cmpval=p1.rest.compare(p2.rest);
730 p1.coeff=ex_to_numeric(p1.coeff).add_dyn(ex_to_numeric(p2.coeff));
731 if (!ex_to_numeric(p1.coeff).is_zero()) {
732 // no further processing is necessary, since this
733 // one element will usually be recombined in eval()
750 void expairseq::construct_from_2_expairseq(const expairseq & s1,
751 const expairseq & s2)
753 combine_overall_coeff(s1.overall_coeff);
754 combine_overall_coeff(s2.overall_coeff);
756 epvector::const_iterator first1=s1.seq.begin();
757 epvector::const_iterator last1=s1.seq.end();
758 epvector::const_iterator first2=s2.seq.begin();
759 epvector::const_iterator last2=s2.seq.end();
761 seq.reserve(s1.seq.size()+s2.seq.size());
763 bool needs_further_processing=false;
765 while (first1!=last1 && first2!=last2) {
766 int cmpval=(*first1).rest.compare((*first2).rest);
769 const numeric & newcoeff=ex_to_numeric((*first1).coeff).
770 add(ex_to_numeric((*first2).coeff));
771 if (!newcoeff.is_zero()) {
772 seq.push_back(expair((*first1).rest,newcoeff));
773 if (expair_needs_further_processing(seq.end()-1)) {
774 needs_further_processing = true;
779 } else if (cmpval<0) {
780 seq.push_back(*first1);
783 seq.push_back(*first2);
788 while (first1!=last1) {
789 seq.push_back(*first1);
792 while (first2!=last2) {
793 seq.push_back(*first2);
797 if (needs_further_processing) {
800 construct_from_epvector(v);
804 void expairseq::construct_from_expairseq_ex(const expairseq & s,
807 combine_overall_coeff(s.overall_coeff);
808 if (is_ex_exactly_of_type(e,numeric)) {
809 combine_overall_coeff(e);
814 epvector::const_iterator first=s.seq.begin();
815 epvector::const_iterator last=s.seq.end();
816 expair p=split_ex_to_pair(e);
818 seq.reserve(s.seq.size()+1);
821 bool needs_further_processing=false;
823 // merge p into s.seq
824 while (first!=last) {
825 int cmpval=(*first).rest.compare(p.rest);
828 const numeric & newcoeff=ex_to_numeric((*first).coeff).
829 add(ex_to_numeric(p.coeff));
830 if (!newcoeff.is_zero()) {
831 seq.push_back(expair((*first).rest,newcoeff));
832 if (expair_needs_further_processing(seq.end()-1)) {
833 needs_further_processing = true;
839 } else if (cmpval<0) {
840 seq.push_back(*first);
850 // while loop exited because p was pushed, now push rest of s.seq
851 while (first!=last) {
852 seq.push_back(*first);
856 // while loop exited because s.seq was pushed, now push p
860 if (needs_further_processing) {
863 construct_from_epvector(v);
867 void expairseq::construct_from_exvector(const exvector & v)
869 // simplifications: +(a,+(b,c),d) -> +(a,b,c,d) (associativity)
870 // +(d,b,c,a) -> +(a,b,c,d) (canonicalization)
871 // +(...,x,*(x,c1),*(x,c2)) -> +(...,*(x,1+c1+c2)) (c1, c2 numeric())
872 // (same for (+,*) -> (*,^)
875 #ifdef EXPAIRSEQ_USE_HASHTAB
876 combine_same_terms();
879 combine_same_terms_sorted_seq();
880 #endif // def EXPAIRSEQ_USE_HASHTAB
883 void expairseq::construct_from_epvector(const epvector & v)
885 // simplifications: +(a,+(b,c),d) -> +(a,b,c,d) (associativity)
886 // +(d,b,c,a) -> +(a,b,c,d) (canonicalization)
887 // +(...,x,*(x,c1),*(x,c2)) -> +(...,*(x,1+c1+c2)) (c1, c2 numeric())
888 // (same for (+,*) -> (*,^)
891 #ifdef EXPAIRSEQ_USE_HASHTAB
892 combine_same_terms();
895 combine_same_terms_sorted_seq();
896 #endif // def EXPAIRSEQ_USE_HASHTAB
901 void expairseq::make_flat(const exvector & v)
903 exvector::const_iterator cit, citend = v.end();
905 // count number of operands which are of same expairseq derived type
906 // and their cumulative number of operands
910 while (cit!=citend) {
911 if (cit->bp->tinfo()==tinfo()) {
913 noperands+=ex_to_expairseq(*cit).seq.size();
918 // reserve seq and coeffseq which will hold all operands
919 seq.reserve(v.size()+noperands-nexpairseqs);
921 // copy elements and split off numerical part
923 while (cit!=citend) {
924 if (cit->bp->tinfo()==tinfo()) {
925 const expairseq & subseqref=ex_to_expairseq(*cit);
926 combine_overall_coeff(subseqref.overall_coeff);
927 epvector::const_iterator cit_s=subseqref.seq.begin();
928 while (cit_s!=subseqref.seq.end()) {
929 seq.push_back(*cit_s);
933 if (is_ex_exactly_of_type(*cit,numeric)) {
934 combine_overall_coeff(*cit);
936 seq.push_back(split_ex_to_pair(*cit));
943 cout << "after make flat" << endl;
944 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
945 (*cit).printraw(cout);
952 void expairseq::make_flat(const epvector & v)
954 epvector::const_iterator cit, citend = v.end();
956 // count number of operands which are of same expairseq derived type
957 // and their cumulative number of operands
961 while (cit!=citend) {
962 if (cit->rest.bp->tinfo()==tinfo()) {
964 noperands+=ex_to_expairseq((*cit).rest).seq.size();
969 // reserve seq and coeffseq which will hold all operands
970 seq.reserve(v.size()+noperands-nexpairseqs);
972 // copy elements and split off numerical part
974 while (cit!=citend) {
975 if ((cit->rest.bp->tinfo()==tinfo())&&can_make_flat(*cit)) {
976 const expairseq & subseqref=ex_to_expairseq((*cit).rest);
977 combine_overall_coeff(ex_to_numeric(subseqref.overall_coeff),
978 ex_to_numeric((*cit).coeff));
979 epvector::const_iterator cit_s=subseqref.seq.begin();
980 while (cit_s!=subseqref.seq.end()) {
981 seq.push_back(expair((*cit_s).rest,
982 ex_to_numeric((*cit_s).coeff).mul_dyn(ex_to_numeric((*cit).coeff))));
983 //seq.push_back(combine_pair_with_coeff_to_pair(*cit_s,
988 if ((*cit).is_numeric_with_coeff_1()) {
989 combine_overall_coeff((*cit).rest);
990 //if (is_ex_exactly_of_type((*cit).rest,numeric)) {
991 // combine_overall_coeff(recombine_pair_to_ex(*cit));
1000 epvector * expairseq::bubblesort(epvector::iterator itbegin, epvector::iterator itend)
1002 unsigned n=itend-itbegin;
1004 epvector * sp=new epvector;
1007 epvector::iterator last=itend-1;
1008 for (epvector::iterator it1=itbegin; it1!=last; ++it1) {
1009 for (epvector::iterator it2=it1+1; it2!=itend; ++it2) {
1010 if ((*it2).rest.compare((*it1).rest)<0) {
1014 sp->push_back(*it1);
1016 sp->push_back(*last);
1020 epvector * expairseq::mergesort(epvector::iterator itbegin, epvector::iterator itend)
1022 unsigned n=itend-itbegin;
1025 epvector * sp=new epvector;
1026 sp->push_back(*itbegin);
1030 if (n<16) return bubblesort(itbegin, itend);
1033 epvector * s1p=mergesort(itbegin, itbegin+m);
1034 epvector * s2p=mergesort(itbegin+m, itend);
1036 epvector * sp=new epvector;
1037 sp->reserve(s1p->size()+s2p->size());
1039 epvector::iterator first1=s1p->begin();
1040 epvector::iterator last1=s1p->end();
1042 epvector::iterator first2=s2p->begin();
1043 epvector::iterator last2=s2p->end();
1045 while (first1 != last1 && first2 != last2) {
1046 if ((*first1).rest.compare((*first2).rest)<0) {
1047 sp->push_back(*first1);
1050 sp->push_back(*first2);
1055 if (first1 != last1) {
1056 while (first1 != last1) {
1057 sp->push_back(*first1);
1061 while (first2 != last2) {
1062 sp->push_back(*first2);
1074 void expairseq::canonicalize(void)
1077 sort(seq.begin(),seq.end(),expair_is_less());
1079 sort(seq.begin(),seq.end(),expair_is_less_old());
1081 if (is_ex_exactly_of_type((*(seq.begin())).rest,numeric)) {
1082 sort(seq.begin(),seq.end(),expair_is_less());
1084 epvector::iterator last_numeric=seq.end();
1087 } while (is_ex_exactly_of_type((*last_numeric).rest,numeric));
1089 sort(last_numeric,seq.end(),expair_is_less());
1095 epvector * sorted_seqp=mergesort(seq.begin(),seq.end());
1096 epvector::iterator last=sorted_seqp->end();
1097 epvector::iterator it2=seq.begin();
1098 for (epvector::iterator it1=sorted_seqp->begin(); it1!=last; ++it1, ++it2) {
1105 cout << "after canonicalize" << endl;
1106 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
1107 (*cit).printraw(cout);
1114 void expairseq::combine_same_terms_sorted_seq(void)
1116 bool needs_further_processing=false;
1118 // combine same terms, drop term with coeff 0
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 possible
1125 // 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;
1136 if (!ex_to_numeric((*itin1).coeff).is_zero()) {
1146 if (!ex_to_numeric((*itin1).coeff).is_zero()) {
1153 seq.erase(itout,last);
1158 cout << "after combine" << endl;
1159 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
1160 (*cit).printraw(cout);
1166 if (needs_further_processing) {
1169 construct_from_epvector(v);
1173 #ifdef EXPAIRSEQ_USE_HASHTAB
1175 unsigned expairseq::calc_hashtabsize(unsigned sz) const
1178 unsigned nearest_power_of_2 = 1 << log2(sz);
1179 // if (nearest_power_of_2 < maxhashtabsize/hashtabfactor) {
1180 // size=nearest_power_of_2*hashtabfactor;
1181 size=nearest_power_of_2/hashtabfactor;
1182 if (size<minhashtabsize) return 0;
1183 GINAC_ASSERT(hashtabsize<=0x8000000U); // really max size due to 31 bit hashing
1184 // hashtabsize must be a power of 2
1185 GINAC_ASSERT((1U << log2(size))==size);
1189 unsigned expairseq::calc_hashindex(const ex & e) const
1191 // calculate hashindex
1192 unsigned hash=e.gethash();
1194 if (is_a_numeric_hash(hash)) {
1197 hashindex=hash & hashmask;
1198 // last hashtab entry is reserved for numerics
1199 if (hashindex==hashmask) hashindex=0;
1201 GINAC_ASSERT(hashindex>=0);
1202 GINAC_ASSERT((hashindex<hashtabsize)||(hashtabsize==0));
1206 void expairseq::shrink_hashtab(void)
1208 unsigned new_hashtabsize;
1209 while (hashtabsize!=(new_hashtabsize=calc_hashtabsize(seq.size()))) {
1210 GINAC_ASSERT(new_hashtabsize<hashtabsize);
1211 if (new_hashtabsize==0) {
1218 // shrink by a factor of 2
1219 unsigned half_hashtabsize=hashtabsize/2;
1220 for (unsigned i=0; i<half_hashtabsize-1; ++i) {
1221 hashtab[i].merge(hashtab[i+half_hashtabsize],epp_is_less());
1223 // special treatment for numeric hashes
1224 hashtab[0].merge(hashtab[half_hashtabsize-1],epp_is_less());
1225 hashtab[half_hashtabsize-1]=hashtab[hashtabsize-1];
1226 hashtab.resize(half_hashtabsize);
1227 hashtabsize=half_hashtabsize;
1228 hashmask=hashtabsize-1;
1232 void expairseq::remove_hashtab_entry(epvector::const_iterator element)
1234 if (hashtabsize==0) return; // nothing to do
1236 // calculate hashindex of element to be deleted
1237 unsigned hashindex=calc_hashindex((*element).rest);
1239 // find it in hashtab and remove it
1240 epplist & eppl=hashtab[hashindex];
1241 epplist::iterator epplit=eppl.begin();
1243 while (epplit!=eppl.end()) {
1244 if (*epplit == element) {
1253 cout << "tried to erase " << element-seq.begin() << endl;
1254 cout << "size " << seq.end()-seq.begin() << endl;
1256 unsigned hashindex=calc_hashindex((*element).rest);
1257 epplist & eppl=hashtab[hashindex];
1258 epplist::iterator epplit=eppl.begin();
1260 while (epplit!=eppl.end()) {
1261 if (*epplit == element) {
1268 GINAC_ASSERT(erased);
1270 GINAC_ASSERT(erased);
1273 void expairseq::move_hashtab_entry(epvector::const_iterator oldpos,
1274 epvector::iterator newpos)
1276 GINAC_ASSERT(hashtabsize!=0);
1278 // calculate hashindex of element which was moved
1279 unsigned hashindex=calc_hashindex((*newpos).rest);
1281 // find it in hashtab and modify it
1282 epplist & eppl=hashtab[hashindex];
1283 epplist::iterator epplit=eppl.begin();
1284 while (epplit!=eppl.end()) {
1285 if (*epplit == oldpos) {
1291 GINAC_ASSERT(epplit!=eppl.end());
1294 void expairseq::sorted_insert(epplist & eppl, epp elem)
1296 epplist::iterator current=eppl.begin();
1297 while ((current!=eppl.end())&&((*(*current)).is_less(*elem))) {
1300 eppl.insert(current,elem);
1303 void expairseq::build_hashtab_and_combine(epvector::iterator & first_numeric,
1304 epvector::iterator & last_non_zero,
1305 vector<bool> & touched,
1306 unsigned & number_of_zeroes)
1308 epp current=seq.begin();
1310 while (current!=first_numeric) {
1311 if (is_ex_exactly_of_type((*current).rest,numeric)) {
1313 iter_swap(current,first_numeric);
1315 // calculate hashindex
1316 unsigned currenthashindex=calc_hashindex((*current).rest);
1318 // test if there is already a matching expair in the hashtab-list
1319 epplist & eppl=hashtab[currenthashindex];
1320 epplist::iterator epplit=eppl.begin();
1321 while (epplit!=eppl.end()) {
1322 if ((*current).rest.is_equal((*(*epplit)).rest)) break;
1325 if (epplit==eppl.end()) {
1326 // no matching expair found, append this to end of list
1327 sorted_insert(eppl,current);
1330 // epplit points to a matching expair, combine it with current
1331 (*(*epplit)).coeff=ex_to_numeric((*(*epplit)).coeff).
1332 add_dyn(ex_to_numeric((*current).coeff));
1334 // move obsolete current expair to end by swapping with last_non_zero element
1335 // if this was a numeric, it is swapped with the expair before first_numeric
1336 iter_swap(current,last_non_zero);
1338 if (first_numeric!=last_non_zero) iter_swap(first_numeric,current);
1341 // test if combined term has coeff 0 and can be removed is done later
1342 touched[(*epplit)-seq.begin()]=true;
1348 void expairseq::drop_coeff_0_terms(epvector::iterator & first_numeric,
1349 epvector::iterator & last_non_zero,
1350 vector<bool> & touched,
1351 unsigned & number_of_zeroes)
1353 // move terms with coeff 0 to end and remove them from hashtab
1354 // check only those elements which have been touched
1355 epp current=seq.begin();
1357 while (current!=first_numeric) {
1361 } else if (!ex_to_numeric((*current).coeff).is_equal(_num0())) {
1365 remove_hashtab_entry(current);
1367 // move element to the end, unless it is already at the end
1368 if (current!=last_non_zero) {
1369 iter_swap(current,last_non_zero);
1371 bool numeric_swapped=first_numeric!=last_non_zero;
1372 if (numeric_swapped) iter_swap(first_numeric,current);
1373 epvector::iterator changed_entry;
1375 if (numeric_swapped) {
1376 changed_entry=first_numeric;
1378 changed_entry=last_non_zero;
1384 if (first_numeric!=current) {
1386 // change entry in hashtab which referred to first_numeric or last_non_zero to current
1387 move_hashtab_entry(changed_entry,current);
1388 touched[current-seq.begin()]=touched[changed_entry-seq.begin()];
1397 GINAC_ASSERT(i==current-seq.begin());
1400 bool expairseq::has_coeff_0(void) const
1402 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
1403 if ((*cit).coeff.is_equal(_ex0())) {
1410 void expairseq::add_numerics_to_hashtab(epvector::iterator first_numeric,
1411 epvector::const_iterator last_non_zero)
1413 if (first_numeric==seq.end()) return; // no numerics
1415 epvector::iterator current=first_numeric;
1416 epvector::const_iterator last=last_non_zero+1;
1417 while (current!=last) {
1418 sorted_insert(hashtab[hashmask],current);
1423 void expairseq::combine_same_terms(void)
1425 // combine same terms, drop term with coeff 0, move numerics to end
1427 // calculate size of hashtab
1428 hashtabsize=calc_hashtabsize(seq.size());
1430 // hashtabsize is a power of 2
1431 hashmask=hashtabsize-1;
1435 hashtab.resize(hashtabsize);
1437 if (hashtabsize==0) {
1439 combine_same_terms_sorted_seq();
1440 GINAC_ASSERT(!has_coeff_0());
1444 // iterate through seq, move numerics to end,
1445 // fill hashtab and combine same terms
1446 epvector::iterator first_numeric=seq.end();
1447 epvector::iterator last_non_zero=seq.end()-1;
1449 vector<bool> touched;
1450 touched.reserve(seq.size());
1451 for (unsigned i=0; i<seq.size(); ++i) touched[i]=false;
1453 unsigned number_of_zeroes=0;
1455 GINAC_ASSERT(!has_coeff_0());
1456 build_hashtab_and_combine(first_numeric,last_non_zero,touched,number_of_zeroes);
1458 cout << "in combine:" << endl;
1460 cout << "size=" << seq.end() - seq.begin() << endl;
1461 cout << "first_numeric=" << first_numeric - seq.begin() << endl;
1462 cout << "last_non_zero=" << last_non_zero - seq.begin() << endl;
1463 for (unsigned i=0; i<seq.size(); ++i) {
1464 if (touched[i]) cout << i << " is touched" << endl;
1466 cout << "end in combine" << endl;
1469 // there should not be any terms with coeff 0 from the beginning,
1470 // so it should be safe to skip this step
1471 if (number_of_zeroes!=0) {
1472 drop_coeff_0_terms(first_numeric,last_non_zero,touched,number_of_zeroes);
1474 cout << "in combine after drop:" << endl;
1476 cout << "size=" << seq.end() - seq.begin() << endl;
1477 cout << "first_numeric=" << first_numeric - seq.begin() << endl;
1478 cout << "last_non_zero=" << last_non_zero - seq.begin() << endl;
1479 for (unsigned i=0; i<seq.size(); ++i) {
1480 if (touched[i]) cout << i << " is touched" << endl;
1482 cout << "end in combine after drop" << endl;
1486 add_numerics_to_hashtab(first_numeric,last_non_zero);
1488 // pop zero elements
1489 for (unsigned i=0; i<number_of_zeroes; ++i) {
1493 // shrink hashtabsize to calculated value
1494 GINAC_ASSERT(!has_coeff_0());
1498 GINAC_ASSERT(!has_coeff_0());
1501 #endif // def EXPAIRSEQ_USE_HASHTAB
1503 bool expairseq::is_canonical() const
1505 if (seq.size()<=1) return 1;
1507 #ifdef EXPAIRSEQ_USE_HASHTAB
1508 if (hashtabsize>0) return 1; // not canoncalized
1509 #endif // def EXPAIRSEQ_USE_HASHTAB
1511 epvector::const_iterator it=seq.begin();
1512 epvector::const_iterator it_last=it;
1513 for (++it; it!=seq.end(); it_last=it, ++it) {
1514 if (!((*it_last).is_less(*it)||(*it_last).is_equal(*it))) {
1515 if (!is_ex_exactly_of_type((*it_last).rest,numeric)||
1516 !is_ex_exactly_of_type((*it).rest,numeric)) {
1517 // double test makes it easier to set a breakpoint...
1518 if (!is_ex_exactly_of_type((*it_last).rest,numeric)||
1519 !is_ex_exactly_of_type((*it).rest,numeric)) {
1520 printpair(cout,*it_last,0);
1522 printpair(cout,*it,0);
1524 cout << "pair1:" << endl;
1525 (*it_last).rest.printtree(cout);
1526 (*it_last).coeff.printtree(cout);
1527 cout << "pair2:" << endl;
1528 (*it).rest.printtree(cout);
1529 (*it).coeff.printtree(cout);
1538 epvector * expairseq::expandchildren(unsigned options) const
1540 epvector::const_iterator last=seq.end();
1541 epvector::const_iterator cit=seq.begin();
1543 const ex & expanded_ex=(*cit).rest.expand(options);
1544 if (!are_ex_trivially_equal((*cit).rest,expanded_ex)) {
1546 // something changed, copy seq, eval and return it
1547 epvector *s=new epvector;
1548 s->reserve(seq.size());
1550 // copy parts of seq which are known not to have changed
1551 epvector::const_iterator cit2=seq.begin();
1553 s->push_back(*cit2);
1556 // copy first changed element
1557 s->push_back(combine_ex_with_coeff_to_pair(expanded_ex,
1561 while (cit2!=last) {
1562 s->push_back(combine_ex_with_coeff_to_pair((*cit2).rest.expand(options),
1571 return 0; // nothing has changed
1574 epvector * expairseq::evalchildren(int level) const
1576 // returns a NULL pointer if nothing had to be evaluated
1577 // returns a pointer to a newly created epvector otherwise
1578 // (which has to be deleted somewhere else)
1583 if (level == -max_recursion_level) {
1584 throw(std::runtime_error("max recursion level reached"));
1588 epvector::const_iterator last=seq.end();
1589 epvector::const_iterator cit=seq.begin();
1591 const ex & evaled_ex=(*cit).rest.eval(level);
1592 if (!are_ex_trivially_equal((*cit).rest,evaled_ex)) {
1594 // something changed, copy seq, eval and return it
1595 epvector *s=new epvector;
1596 s->reserve(seq.size());
1598 // copy parts of seq which are known not to have changed
1599 epvector::const_iterator cit2=seq.begin();
1601 s->push_back(*cit2);
1604 // copy first changed element
1605 s->push_back(combine_ex_with_coeff_to_pair(evaled_ex,
1609 while (cit2!=last) {
1610 s->push_back(combine_ex_with_coeff_to_pair((*cit2).rest.eval(level),
1619 return 0; // nothing has changed
1622 epvector expairseq::evalfchildren(int level) const
1625 s.reserve(seq.size());
1630 if (level == -max_recursion_level) {
1631 throw(std::runtime_error("max recursion level reached"));
1634 for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
1635 s.push_back(combine_ex_with_coeff_to_pair((*it).rest.evalf(level),
1641 epvector expairseq::normalchildren(int level) const
1644 s.reserve(seq.size());
1649 if (level == -max_recursion_level) {
1650 throw(std::runtime_error("max recursion level reached"));
1653 for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
1654 s.push_back(combine_ex_with_coeff_to_pair((*it).rest.normal(level),
1660 epvector expairseq::diffchildren(const symbol & y) const
1663 s.reserve(seq.size());
1665 for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
1666 s.push_back(combine_ex_with_coeff_to_pair((*it).rest.diff(y),
1672 epvector * expairseq::subschildren(const lst & ls, const lst & lr) const
1674 // returns a NULL pointer if nothing had to be substituted
1675 // returns a pointer to a newly created epvector otherwise
1676 // (which has to be deleted somewhere else)
1677 GINAC_ASSERT(ls.nops()==lr.nops());
1679 epvector::const_iterator last=seq.end();
1680 epvector::const_iterator cit=seq.begin();
1682 const ex & subsed_ex=(*cit).rest.subs(ls,lr);
1683 if (!are_ex_trivially_equal((*cit).rest,subsed_ex)) {
1685 // something changed, copy seq, subs and return it
1686 epvector *s=new epvector;
1687 s->reserve(seq.size());
1689 // copy parts of seq which are known not to have changed
1690 epvector::const_iterator cit2=seq.begin();
1692 s->push_back(*cit2);
1695 // copy first changed element
1696 s->push_back(combine_ex_with_coeff_to_pair(subsed_ex,
1700 while (cit2!=last) {
1701 s->push_back(combine_ex_with_coeff_to_pair((*cit2).rest.subs(ls,lr),
1710 return 0; // nothing has changed
1714 // static member variables
1719 unsigned expairseq::precedence=10;
1721 #ifdef EXPAIRSEQ_USE_HASHTAB
1722 unsigned expairseq::maxhashtabsize=0x4000000U;
1723 unsigned expairseq::minhashtabsize=0x1000U;
1724 unsigned expairseq::hashtabfactor=1;
1725 #endif // def EXPAIRSEQ_USE_HASHTAB
1731 const expairseq some_expairseq;
1732 const type_info & typeid_expairseq=typeid(some_expairseq);
1734 #ifndef NO_NAMESPACE_GINAC
1735 } // namespace GiNaC
1736 #endif // ndef NO_NAMESPACE_GINAC