1 /** @file expairseq.cpp */
9 #ifdef EXPAIRSEQ_USE_HASHTAB
10 #error "!!!!!!!!TODO: expair_needs_further_processing not yet implemented for hashtabs, sorry. A.F."
11 #endif // def EXPAIRSEQ_USE_HASHTAB
20 bool operator()(epp const & lh, epp const & rh) const
22 return (*lh).is_less(*rh);
27 // default constructor, destructor, copy constructor assignment operator and helpers
32 expairseq::expairseq(expairseq const & other)
34 debugmsg("expairseq copy constructor",LOGLEVEL_CONSTRUCT);
38 expairseq const & expairseq::operator=(expairseq const & other)
40 debugmsg("expairseq operator=",LOGLEVEL_ASSIGNMENT);
50 void expairseq::copy(expairseq const & other)
54 overall_coeff=other.overall_coeff;
55 #ifdef EXPAIRSEQ_USE_HASHTAB
57 hashtabsize=other.hashtabsize;
59 hashmask=other.hashmask;
60 hashtab.resize(hashtabsize);
61 epvector::const_iterator osb=other.seq.begin();
62 for (unsigned i=0; i<hashtabsize; ++i) {
64 for (epplist::const_iterator cit=other.hashtab[i].begin();
65 cit!=other.hashtab[i].end(); ++cit) {
66 hashtab[i].push_back(seq.begin()+((*cit)-osb));
72 #endif // def EXPAIRSEQ_USE_HASHTAB
79 expairseq::expairseq(ex const & lh, ex const & rh) : basic(TINFO_EXPAIRSEQ)
81 debugmsg("expairseq constructor from ex,ex",LOGLEVEL_CONSTRUCT);
82 construct_from_2_ex(lh,rh);
83 ASSERT(is_canonical());
86 expairseq::expairseq(exvector const & v) : basic(TINFO_EXPAIRSEQ)
88 debugmsg("expairseq constructor from exvector",LOGLEVEL_CONSTRUCT);
89 construct_from_exvector(v);
90 ASSERT(is_canonical());
94 expairseq::expairseq(epvector const & v, bool do_not_canonicalize) :
95 basic(TINFO_EXPAIRSEQ)
97 debugmsg("expairseq constructor from epvector",LOGLEVEL_CONSTRUCT);
98 if (do_not_canonicalize) {
100 #ifdef EXPAIRSEQ_USE_HASHTAB
101 combine_same_terms(); // to build hashtab
102 #endif // def EXPAIRSEQ_USE_HASHTAB
104 construct_from_epvector(v);
106 ASSERT(is_canonical());
110 expairseq::expairseq(epvector const & v, ex const & oc) :
111 basic(TINFO_EXPAIRSEQ), overall_coeff(oc)
113 debugmsg("expairseq constructor from epvector,ex",LOGLEVEL_CONSTRUCT);
114 construct_from_epvector(v);
115 ASSERT(is_canonical());
118 expairseq::expairseq(epvector * vp, ex const & oc) :
119 basic(TINFO_EXPAIRSEQ), overall_coeff(oc)
121 debugmsg("expairseq constructor from epvector *,ex",LOGLEVEL_CONSTRUCT);
123 construct_from_epvector(*vp);
125 ASSERT(is_canonical());
129 // functions overriding virtual functions from bases classes
134 basic * expairseq::duplicate() const
136 debugmsg("expairseq duplicate",LOGLEVEL_DUPLICATE);
137 return new expairseq(*this);
140 bool expairseq::info(unsigned inf) const
142 return basic::info(inf);
145 int expairseq::nops() const
147 if (overall_coeff.is_equal(default_overall_coeff())) {
153 ex expairseq::op(int const i) const
155 if (unsigned(i)<seq.size()) {
156 return recombine_pair_to_ex(seq[i]);
158 ASSERT(!overall_coeff.is_equal(default_overall_coeff()));
159 return overall_coeff;
162 ex & expairseq::let_op(int const i)
164 throw(std::logic_error("let_op not defined for expairseq and derived classes (add,mul,...)"));
167 ex expairseq::eval(int level) const
169 if ((level==1)&&(flags & status_flags::evaluated)) {
173 epvector * vp=evalchildren(level);
178 return (new expairseq(vp,overall_coeff))
179 ->setflag(status_flags::dynallocated |
180 status_flags::evaluated );
183 ex expairseq::evalf(int level) const
185 return thisexpairseq(evalfchildren(level),overall_coeff);
188 ex expairseq::normal(lst &sym_lst, lst &repl_lst, int level) const
190 ex n=thisexpairseq(normalchildren(level),overall_coeff);
191 return n.bp->basic::normal(sym_lst,repl_lst,level);
194 ex expairseq::subs(lst const & ls, lst const & lr) const
196 epvector * vp=subschildren(ls,lr);
200 return thisexpairseq(vp,overall_coeff);
205 int expairseq::compare_same_type(basic const & other) const
207 ASSERT(is_of_type(other, expairseq));
208 expairseq const & o=static_cast<expairseq const &>(const_cast<basic &>(other));
212 // compare number of elements
213 if (seq.size() != o.seq.size()) {
214 return (seq.size()<o.seq.size()) ? -1 : 1;
217 // compare overall_coeff
218 cmpval=overall_coeff.compare(o.overall_coeff);
219 if (cmpval!=0) return cmpval;
221 //if (seq.size()==0) return 0; // empty expairseq's are equal
223 #ifdef EXPAIRSEQ_USE_HASHTAB
224 ASSERT(hashtabsize==o.hashtabsize);
225 if (hashtabsize==0) {
226 #endif // def EXPAIRSEQ_USE_HASHTAB
227 epvector::const_iterator cit1=seq.begin();
228 epvector::const_iterator cit2=o.seq.begin();
229 epvector::const_iterator last1=seq.end();
230 epvector::const_iterator last2=o.seq.end();
232 for (; (cit1!=last1)&&(cit2!=last2); ++cit1, ++cit2) {
233 cmpval=(*cit1).compare(*cit2);
234 if (cmpval!=0) return cmpval;
241 #ifdef EXPAIRSEQ_USE_HASHTAB
244 // compare number of elements in each hashtab entry
245 for (unsigned i=0; i<hashtabsize; ++i) {
246 unsigned cursize=hashtab[i].size();
247 if (cursize != o.hashtab[i].size()) {
248 return (cursize < o.hashtab[i].size()) ? -1 : 1;
252 // compare individual (sorted) hashtab entries
253 for (unsigned i=0; i<hashtabsize; ++i) {
254 unsigned sz=hashtab[i].size();
256 epplist const & eppl1=hashtab[i];
257 epplist const & eppl2=o.hashtab[i];
258 epplist::const_iterator it1=eppl1.begin();
259 epplist::const_iterator it2=eppl2.begin();
260 while (it1!=eppl1.end()) {
261 cmpval=(*(*it1)).compare(*(*it2));
262 if (cmpval!=0) return cmpval;
270 #endif // def EXPAIRSEQ_USE_HASHTAB
273 bool expairseq::is_equal_same_type(basic const & other) const
275 expairseq const & o=dynamic_cast<expairseq const &>(const_cast<basic &>(other));
277 // compare number of elements
278 if (seq.size() != o.seq.size()) return false;
280 // compare overall_coeff
281 if (!overall_coeff.is_equal(o.overall_coeff)) return false;
283 #ifdef EXPAIRSEQ_USE_HASHTAB
284 // compare number of elements in each hashtab entry
285 if (hashtabsize!=o.hashtabsize) {
286 cout << "this:" << endl;
288 cout << "other:" << endl;
289 other.printtree(cout,0);
292 ASSERT(hashtabsize==o.hashtabsize);
294 if (hashtabsize==0) {
295 #endif // def EXPAIRSEQ_USE_HASHTAB
296 epvector::const_iterator cit1=seq.begin();
297 epvector::const_iterator cit2=o.seq.begin();
298 epvector::const_iterator last1=seq.end();
300 while (cit1!=last1) {
301 if (!(*cit1).is_equal(*cit2)) return false;
307 #ifdef EXPAIRSEQ_USE_HASHTAB
310 for (unsigned i=0; i<hashtabsize; ++i) {
311 if (hashtab[i].size() != o.hashtab[i].size()) return false;
314 // compare individual sorted hashtab entries
315 for (unsigned i=0; i<hashtabsize; ++i) {
316 unsigned sz=hashtab[i].size();
318 epplist const & eppl1=hashtab[i];
319 epplist const & eppl2=o.hashtab[i];
320 epplist::const_iterator it1=eppl1.begin();
321 epplist::const_iterator it2=eppl2.begin();
322 while (it1!=eppl1.end()) {
323 if (!(*(*it1)).is_equal(*(*it2))) return false;
331 #endif // def EXPAIRSEQ_USE_HASHTAB
334 unsigned expairseq::return_type(void) const
336 return return_types::noncommutative_composite;
339 unsigned expairseq::calchash(void) const
341 unsigned v=golden_ratio_hash(tinfo());
342 epvector::const_iterator last=seq.end();
343 for (epvector::const_iterator cit=seq.begin(); cit!=last; ++cit) {
344 #ifndef EXPAIRSEQ_USE_HASHTAB
345 v=rotate_left_31(v); // rotation would spoil commutativity
346 #endif // ndef EXPAIRSEQ_USE_HASHTAB
347 v ^= (*cit).rest.gethash();
350 v ^= overall_coeff.gethash();
353 // store calculated hash value only if object is already evaluated
354 if (flags & status_flags::evaluated) {
355 setflag(status_flags::hash_calculated);
362 ex expairseq::expand(unsigned options) const
364 epvector * vp=expandchildren(options);
368 return thisexpairseq(vp,overall_coeff);
372 // new virtual functions which can be overridden by derived classes
377 ex expairseq::thisexpairseq(epvector const & v,ex const & oc) const
379 return expairseq(v,oc);
382 ex expairseq::thisexpairseq(epvector * vp, ex const & oc) const
384 return expairseq(vp,oc);
387 expair expairseq::split_ex_to_pair(ex const & e) const
389 return expair(e,exONE());
392 expair expairseq::combine_ex_with_coeff_to_pair(ex const & e,
395 ASSERT(is_ex_exactly_of_type(c,numeric));
400 expair expairseq::combine_pair_with_coeff_to_pair(expair const & p,
403 ASSERT(is_ex_exactly_of_type(p.coeff,numeric));
404 ASSERT(is_ex_exactly_of_type(c,numeric));
406 return expair(p.rest,ex_to_numeric(p.coeff).mul_dyn(ex_to_numeric(c)));
409 ex expairseq::recombine_pair_to_ex(expair const & p) const
411 return lst(p.rest,p.coeff);
414 bool expairseq::expair_needs_further_processing(epp it)
419 ex expairseq::default_overall_coeff(void) const
424 void expairseq::combine_overall_coeff(ex const & c)
426 ASSERT(is_ex_exactly_of_type(overall_coeff,numeric));
427 ASSERT(is_ex_exactly_of_type(c,numeric));
428 overall_coeff = ex_to_numeric(overall_coeff).add_dyn(ex_to_numeric(c));
431 void expairseq::combine_overall_coeff(ex const & c1, ex const & c2)
433 ASSERT(is_ex_exactly_of_type(overall_coeff,numeric));
434 ASSERT(is_ex_exactly_of_type(c1,numeric));
435 ASSERT(is_ex_exactly_of_type(c2,numeric));
436 overall_coeff = ex_to_numeric(overall_coeff).
437 add_dyn(ex_to_numeric(c1).mul(ex_to_numeric(c2)));
440 bool expairseq::can_make_flat(expair const & p) const
447 // non-virtual functions in this class
450 void expairseq::construct_from_2_ex_via_exvector(ex const & lh, ex const & rh)
456 construct_from_exvector(v);
457 #ifdef EXPAIRSEQ_USE_HASHTAB
458 ASSERT((hashtabsize==0)||(hashtabsize>=minhashtabsize));
459 ASSERT(hashtabsize==calc_hashtabsize(seq.size()));
460 #endif // def EXPAIRSEQ_USE_HASHTAB
463 void expairseq::construct_from_2_ex(ex const & lh, ex const & rh)
465 if (lh.bp->tinfo()==tinfo()) {
466 if (rh.bp->tinfo()==tinfo()) {
467 #ifdef EXPAIRSEQ_USE_HASHTAB
468 unsigned totalsize=ex_to_expairseq(lh).seq.size()+
469 ex_to_expairseq(rh).seq.size();
470 if (calc_hashtabsize(totalsize)!=0) {
471 construct_from_2_ex_via_exvector(lh,rh);
473 #endif // def EXPAIRSEQ_USE_HASHTAB
474 construct_from_2_expairseq(ex_to_expairseq(lh),
475 ex_to_expairseq(rh));
476 #ifdef EXPAIRSEQ_USE_HASHTAB
478 #endif // def EXPAIRSEQ_USE_HASHTAB
481 #ifdef EXPAIRSEQ_USE_HASHTAB
482 unsigned totalsize=ex_to_expairseq(lh).seq.size()+1;
483 if (calc_hashtabsize(totalsize)!=0) {
484 construct_from_2_ex_via_exvector(lh,rh);
486 #endif // def EXPAIRSEQ_USE_HASHTAB
487 construct_from_expairseq_ex(ex_to_expairseq(lh),rh);
488 #ifdef EXPAIRSEQ_USE_HASHTAB
490 #endif // def EXPAIRSEQ_USE_HASHTAB
493 } else if (rh.bp->tinfo()==tinfo()) {
494 #ifdef EXPAIRSEQ_USE_HASHTAB
495 unsigned totalsize=ex_to_expairseq(rh).seq.size()+1;
496 if (calc_hashtabsize(totalsize)!=0) {
497 construct_from_2_ex_via_exvector(lh,rh);
499 #endif // def EXPAIRSEQ_USE_HASHTAB
500 construct_from_expairseq_ex(ex_to_expairseq(rh),lh);
501 #ifdef EXPAIRSEQ_USE_HASHTAB
503 #endif // def EXPAIRSEQ_USE_HASHTAB
507 #ifdef EXPAIRSEQ_USE_HASHTAB
508 if (calc_hashtabsize(2)!=0) {
509 construct_from_2_ex_via_exvector(lh,rh);
513 #endif // def EXPAIRSEQ_USE_HASHTAB
515 if (is_ex_exactly_of_type(lh,numeric)) {
516 if (is_ex_exactly_of_type(rh,numeric)) {
517 combine_overall_coeff(lh);
518 combine_overall_coeff(rh);
520 combine_overall_coeff(lh);
521 seq.push_back(split_ex_to_pair(rh));
524 if (is_ex_exactly_of_type(rh,numeric)) {
525 combine_overall_coeff(rh);
526 seq.push_back(split_ex_to_pair(lh));
528 expair p1=split_ex_to_pair(lh);
529 expair p2=split_ex_to_pair(rh);
531 int cmpval=p1.rest.compare(p2.rest);
533 p1.coeff=ex_to_numeric(p1.coeff).add_dyn(ex_to_numeric(p2.coeff));
534 if (!ex_to_numeric(p1.coeff).is_zero()) {
535 // no further processing is necessary, since this
536 // one element will usually be recombined in eval()
553 void expairseq::construct_from_2_expairseq(expairseq const & s1,
554 expairseq const & s2)
556 combine_overall_coeff(s1.overall_coeff);
557 combine_overall_coeff(s2.overall_coeff);
559 epvector::const_iterator first1=s1.seq.begin();
560 epvector::const_iterator last1=s1.seq.end();
561 epvector::const_iterator first2=s2.seq.begin();
562 epvector::const_iterator last2=s2.seq.end();
564 seq.reserve(s1.seq.size()+s2.seq.size());
566 bool needs_further_processing=false;
568 while (first1!=last1 && first2!=last2) {
569 int cmpval=(*first1).rest.compare((*first2).rest);
572 numeric const & newcoeff=ex_to_numeric((*first1).coeff).
573 add(ex_to_numeric((*first2).coeff));
574 if (!newcoeff.is_zero()) {
575 seq.push_back(expair((*first1).rest,newcoeff));
576 if (expair_needs_further_processing(seq.end()-1)) {
577 needs_further_processing = true;
582 } else if (cmpval<0) {
583 seq.push_back(*first1);
586 seq.push_back(*first2);
591 while (first1!=last1) {
592 seq.push_back(*first1);
595 while (first2!=last2) {
596 seq.push_back(*first2);
600 if (needs_further_processing) {
603 construct_from_epvector(v);
607 void expairseq::construct_from_expairseq_ex(expairseq const & s,
610 combine_overall_coeff(s.overall_coeff);
611 if (is_ex_exactly_of_type(e,numeric)) {
612 combine_overall_coeff(e);
617 epvector::const_iterator first=s.seq.begin();
618 epvector::const_iterator last=s.seq.end();
619 expair p=split_ex_to_pair(e);
621 seq.reserve(s.seq.size()+1);
624 bool needs_further_processing=false;
626 // merge p into s.seq
627 while (first!=last) {
628 int cmpval=(*first).rest.compare(p.rest);
631 numeric const & newcoeff=ex_to_numeric((*first).coeff).
632 add(ex_to_numeric(p.coeff));
633 if (!newcoeff.is_zero()) {
634 seq.push_back(expair((*first).rest,newcoeff));
635 if (expair_needs_further_processing(seq.end()-1)) {
636 needs_further_processing = true;
642 } else if (cmpval<0) {
643 seq.push_back(*first);
653 // while loop exited because p was pushed, now push rest of s.seq
654 while (first!=last) {
655 seq.push_back(*first);
659 // while loop exited because s.seq was pushed, now push p
663 if (needs_further_processing) {
666 construct_from_epvector(v);
670 void expairseq::construct_from_exvector(exvector const & v)
672 // simplifications: +(a,+(b,c),d) -> +(a,b,c,d) (associativity)
673 // +(d,b,c,a) -> +(a,b,c,d) (canonicalization)
674 // +(...,x,*(x,c1),*(x,c2)) -> +(...,*(x,1+c1+c2)) (c1, c2 numeric())
675 // (same for (+,*) -> (*,^)
678 #ifdef EXPAIRSEQ_USE_HASHTAB
679 combine_same_terms();
682 combine_same_terms_sorted_seq();
683 #endif // def EXPAIRSEQ_USE_HASHTAB
686 void expairseq::construct_from_epvector(epvector const & v)
688 // simplifications: +(a,+(b,c),d) -> +(a,b,c,d) (associativity)
689 // +(d,b,c,a) -> +(a,b,c,d) (canonicalization)
690 // +(...,x,*(x,c1),*(x,c2)) -> +(...,*(x,1+c1+c2)) (c1, c2 numeric())
691 // (same for (+,*) -> (*,^)
694 #ifdef EXPAIRSEQ_USE_HASHTAB
695 combine_same_terms();
698 combine_same_terms_sorted_seq();
699 #endif // def EXPAIRSEQ_USE_HASHTAB
704 void expairseq::make_flat(exvector const & v)
706 exvector::const_iterator cit, citend = v.end();
708 // count number of operands which are of same expairseq derived type
709 // and their cumulative number of operands
713 while (cit!=citend) {
714 if (cit->bp->tinfo()==tinfo()) {
716 noperands+=ex_to_expairseq(*cit).seq.size();
721 // reserve seq and coeffseq which will hold all operands
722 seq.reserve(v.size()+noperands-nexpairseqs);
724 // copy elements and split off numerical part
726 while (cit!=citend) {
727 if (cit->bp->tinfo()==tinfo()) {
728 expairseq const & subseqref=ex_to_expairseq(*cit);
729 combine_overall_coeff(subseqref.overall_coeff);
730 epvector::const_iterator cit_s=subseqref.seq.begin();
731 while (cit_s!=subseqref.seq.end()) {
732 seq.push_back(*cit_s);
736 if (is_ex_exactly_of_type(*cit,numeric)) {
737 combine_overall_coeff(*cit);
739 seq.push_back(split_ex_to_pair(*cit));
746 cout << "after make flat" << endl;
747 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
748 (*cit).printraw(cout);
755 void expairseq::make_flat(epvector const & v)
757 epvector::const_iterator cit, citend = v.end();
759 // count number of operands which are of same expairseq derived type
760 // and their cumulative number of operands
764 while (cit!=citend) {
765 if (cit->rest.bp->tinfo()==tinfo()) {
767 noperands+=ex_to_expairseq((*cit).rest).seq.size();
772 // reserve seq and coeffseq which will hold all operands
773 seq.reserve(v.size()+noperands-nexpairseqs);
775 // copy elements and split off numerical part
777 while (cit!=citend) {
778 if ((cit->rest.bp->tinfo()==tinfo())&&can_make_flat(*cit)) {
779 expairseq const & subseqref=ex_to_expairseq((*cit).rest);
780 combine_overall_coeff(ex_to_numeric(subseqref.overall_coeff),
781 ex_to_numeric((*cit).coeff));
782 epvector::const_iterator cit_s=subseqref.seq.begin();
783 while (cit_s!=subseqref.seq.end()) {
784 seq.push_back(expair((*cit_s).rest,
785 ex_to_numeric((*cit_s).coeff).mul_dyn(ex_to_numeric((*cit).coeff))));
786 //seq.push_back(combine_pair_with_coeff_to_pair(*cit_s,
791 if ((*cit).is_numeric_with_coeff_1()) {
792 combine_overall_coeff((*cit).rest);
793 //if (is_ex_exactly_of_type((*cit).rest,numeric)) {
794 // combine_overall_coeff(recombine_pair_to_ex(*cit));
803 epvector * expairseq::bubblesort(epvector::iterator itbegin, epvector::iterator itend)
805 unsigned n=itend-itbegin;
807 epvector * sp=new epvector;
810 epvector::iterator last=itend-1;
811 for (epvector::iterator it1=itbegin; it1!=last; ++it1) {
812 for (epvector::iterator it2=it1+1; it2!=itend; ++it2) {
813 if ((*it2).rest.compare((*it1).rest)<0) {
819 sp->push_back(*last);
823 epvector * expairseq::mergesort(epvector::iterator itbegin, epvector::iterator itend)
825 unsigned n=itend-itbegin;
828 epvector * sp=new epvector;
829 sp->push_back(*itbegin);
833 if (n<16) return bubblesort(itbegin, itend);
836 epvector * s1p=mergesort(itbegin, itbegin+m);
837 epvector * s2p=mergesort(itbegin+m, itend);
839 epvector * sp=new epvector;
840 sp->reserve(s1p->size()+s2p->size());
842 epvector::iterator first1=s1p->begin();
843 epvector::iterator last1=s1p->end();
845 epvector::iterator first2=s2p->begin();
846 epvector::iterator last2=s2p->end();
848 while (first1 != last1 && first2 != last2) {
849 if ((*first1).rest.compare((*first2).rest)<0) {
850 sp->push_back(*first1);
853 sp->push_back(*first2);
858 if (first1 != last1) {
859 while (first1 != last1) {
860 sp->push_back(*first1);
864 while (first2 != last2) {
865 sp->push_back(*first2);
877 void expairseq::canonicalize(void)
880 sort(seq.begin(),seq.end(),expair_is_less());
882 sort(seq.begin(),seq.end(),expair_is_less_old());
884 if (is_ex_exactly_of_type((*(seq.begin())).rest,numeric)) {
885 sort(seq.begin(),seq.end(),expair_is_less());
887 epvector::iterator last_numeric=seq.end();
890 } while (is_ex_exactly_of_type((*last_numeric).rest,numeric));
892 sort(last_numeric,seq.end(),expair_is_less());
898 epvector * sorted_seqp=mergesort(seq.begin(),seq.end());
899 epvector::iterator last=sorted_seqp->end();
900 epvector::iterator it2=seq.begin();
901 for (epvector::iterator it1=sorted_seqp->begin(); it1!=last; ++it1, ++it2) {
908 cout << "after canonicalize" << endl;
909 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
910 (*cit).printraw(cout);
917 void expairseq::combine_same_terms_sorted_seq(void)
919 bool needs_further_processing=false;
921 // combine same terms, drop term with coeff 0
923 epvector::iterator itin1=seq.begin();
924 epvector::iterator itin2=itin1+1;
925 epvector::iterator itout=itin1;
926 epvector::iterator last=seq.end();
927 // must_copy will be set to true the first time some combination is possible
928 // from then on the sequence has changed and must be compacted
929 bool must_copy=false;
930 while (itin2!=last) {
931 if ((*itin1).rest.compare((*itin2).rest)==0) {
932 (*itin1).coeff=ex_to_numeric((*itin1).coeff).
933 add_dyn(ex_to_numeric((*itin2).coeff));
934 if (expair_needs_further_processing(itin1)) {
935 needs_further_processing = true;
939 if (!ex_to_numeric((*itin1).coeff).is_zero()) {
949 if (!ex_to_numeric((*itin1).coeff).is_zero()) {
956 seq.erase(itout,last);
961 cout << "after combine" << endl;
962 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
963 (*cit).printraw(cout);
969 if (needs_further_processing) {
972 construct_from_epvector(v);
976 #ifdef EXPAIRSEQ_USE_HASHTAB
978 unsigned expairseq::calc_hashtabsize(unsigned sz) const
981 unsigned nearest_power_of_2 = 1 << log2(sz);
982 // if (nearest_power_of_2 < maxhashtabsize/hashtabfactor) {
983 // size=nearest_power_of_2*hashtabfactor;
984 size=nearest_power_of_2/hashtabfactor;
985 if (size<minhashtabsize) return 0;
986 ASSERT(hashtabsize<=0x8000000U); // really max size due to 31 bit hashing
987 // hashtabsize must be a power of 2
988 ASSERT((1U << log2(size))==size);
992 unsigned expairseq::calc_hashindex(ex const & e) const
994 // calculate hashindex
995 unsigned hash=e.gethash();
997 if (is_a_numeric_hash(hash)) {
1000 hashindex=hash & hashmask;
1001 // last hashtab entry is reserved for numerics
1002 if (hashindex==hashmask) hashindex=0;
1004 ASSERT(hashindex>=0);
1005 ASSERT((hashindex<hashtabsize)||(hashtabsize==0));
1009 void expairseq::shrink_hashtab(void)
1011 unsigned new_hashtabsize;
1012 while (hashtabsize!=(new_hashtabsize=calc_hashtabsize(seq.size()))) {
1013 ASSERT(new_hashtabsize<hashtabsize);
1014 if (new_hashtabsize==0) {
1021 // shrink by a factor of 2
1022 unsigned half_hashtabsize=hashtabsize/2;
1023 for (unsigned i=0; i<half_hashtabsize-1; ++i) {
1024 hashtab[i].merge(hashtab[i+half_hashtabsize],epp_is_less());
1026 // special treatment for numeric hashes
1027 hashtab[0].merge(hashtab[half_hashtabsize-1],epp_is_less());
1028 hashtab[half_hashtabsize-1]=hashtab[hashtabsize-1];
1029 hashtab.resize(half_hashtabsize);
1030 hashtabsize=half_hashtabsize;
1031 hashmask=hashtabsize-1;
1035 void expairseq::remove_hashtab_entry(epvector::const_iterator element)
1037 if (hashtabsize==0) return; // nothing to do
1039 // calculate hashindex of element to be deleted
1040 unsigned hashindex=calc_hashindex((*element).rest);
1042 // find it in hashtab and remove it
1043 epplist & eppl=hashtab[hashindex];
1044 epplist::iterator epplit=eppl.begin();
1046 while (epplit!=eppl.end()) {
1047 if (*epplit == element) {
1056 cout << "tried to erase " << element-seq.begin() << endl;
1057 cout << "size " << seq.end()-seq.begin() << endl;
1059 unsigned hashindex=calc_hashindex((*element).rest);
1060 epplist & eppl=hashtab[hashindex];
1061 epplist::iterator epplit=eppl.begin();
1063 while (epplit!=eppl.end()) {
1064 if (*epplit == element) {
1076 void expairseq::move_hashtab_entry(epvector::const_iterator oldpos,
1077 epvector::iterator newpos)
1079 ASSERT(hashtabsize!=0);
1081 // calculate hashindex of element which was moved
1082 unsigned hashindex=calc_hashindex((*newpos).rest);
1084 // find it in hashtab and modify it
1085 epplist & eppl=hashtab[hashindex];
1086 epplist::iterator epplit=eppl.begin();
1087 while (epplit!=eppl.end()) {
1088 if (*epplit == oldpos) {
1094 ASSERT(epplit!=eppl.end());
1097 void expairseq::sorted_insert(epplist & eppl, epp elem)
1099 epplist::iterator current=eppl.begin();
1100 while ((current!=eppl.end())&&((*(*current)).is_less(*elem))) {
1103 eppl.insert(current,elem);
1106 void expairseq::build_hashtab_and_combine(epvector::iterator & first_numeric,
1107 epvector::iterator & last_non_zero,
1108 vector<bool> & touched,
1109 unsigned & number_of_zeroes)
1111 epp current=seq.begin();
1113 while (current!=first_numeric) {
1114 if (is_ex_exactly_of_type((*current).rest,numeric)) {
1116 iter_swap(current,first_numeric);
1118 // calculate hashindex
1119 unsigned currenthashindex=calc_hashindex((*current).rest);
1121 // test if there is already a matching expair in the hashtab-list
1122 epplist & eppl=hashtab[currenthashindex];
1123 epplist::iterator epplit=eppl.begin();
1124 while (epplit!=eppl.end()) {
1125 if ((*current).rest.is_equal((*(*epplit)).rest)) break;
1128 if (epplit==eppl.end()) {
1129 // no matching expair found, append this to end of list
1130 sorted_insert(eppl,current);
1133 // epplit points to a matching expair, combine it with current
1134 (*(*epplit)).coeff=ex_to_numeric((*(*epplit)).coeff).
1135 add_dyn(ex_to_numeric((*current).coeff));
1137 // move obsolete current expair to end by swapping with last_non_zero element
1138 // if this was a numeric, it is swapped with the expair before first_numeric
1139 iter_swap(current,last_non_zero);
1141 if (first_numeric!=last_non_zero) iter_swap(first_numeric,current);
1144 // test if combined term has coeff 0 and can be removed is done later
1145 touched[(*epplit)-seq.begin()]=true;
1151 void expairseq::drop_coeff_0_terms(epvector::iterator & first_numeric,
1152 epvector::iterator & last_non_zero,
1153 vector<bool> & touched,
1154 unsigned & number_of_zeroes)
1156 // move terms with coeff 0 to end and remove them from hashtab
1157 // check only those elements which have been touched
1158 epp current=seq.begin();
1160 while (current!=first_numeric) {
1164 } else if (!ex_to_numeric((*current).coeff).is_equal(numZERO())) {
1168 remove_hashtab_entry(current);
1170 // move element to the end, unless it is already at the end
1171 if (current!=last_non_zero) {
1172 iter_swap(current,last_non_zero);
1174 bool numeric_swapped=first_numeric!=last_non_zero;
1175 if (numeric_swapped) iter_swap(first_numeric,current);
1176 epvector::iterator changed_entry;
1178 if (numeric_swapped) {
1179 changed_entry=first_numeric;
1181 changed_entry=last_non_zero;
1187 if (first_numeric!=current) {
1189 // change entry in hashtab which referred to first_numeric or last_non_zero to current
1190 move_hashtab_entry(changed_entry,current);
1191 touched[current-seq.begin()]=touched[changed_entry-seq.begin()];
1200 ASSERT(i==current-seq.begin());
1203 bool expairseq::has_coeff_0(void) const
1205 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
1206 if ((*cit).coeff.is_equal(exZERO())) {
1213 void expairseq::add_numerics_to_hashtab(epvector::iterator first_numeric,
1214 epvector::const_iterator last_non_zero)
1216 if (first_numeric==seq.end()) return; // no numerics
1218 epvector::iterator current=first_numeric;
1219 epvector::const_iterator last=last_non_zero+1;
1220 while (current!=last) {
1221 sorted_insert(hashtab[hashmask],current);
1226 void expairseq::combine_same_terms(void)
1228 // combine same terms, drop term with coeff 0, move numerics to end
1230 // calculate size of hashtab
1231 hashtabsize=calc_hashtabsize(seq.size());
1233 // hashtabsize is a power of 2
1234 hashmask=hashtabsize-1;
1238 hashtab.resize(hashtabsize);
1240 if (hashtabsize==0) {
1242 combine_same_terms_sorted_seq();
1243 ASSERT(!has_coeff_0());
1247 // iterate through seq, move numerics to end,
1248 // fill hashtab and combine same terms
1249 epvector::iterator first_numeric=seq.end();
1250 epvector::iterator last_non_zero=seq.end()-1;
1252 vector<bool> touched;
1253 touched.reserve(seq.size());
1254 for (unsigned i=0; i<seq.size(); ++i) touched[i]=false;
1256 unsigned number_of_zeroes=0;
1258 ASSERT(!has_coeff_0());
1259 build_hashtab_and_combine(first_numeric,last_non_zero,touched,number_of_zeroes);
1261 cout << "in combine:" << endl;
1263 cout << "size=" << seq.end() - seq.begin() << endl;
1264 cout << "first_numeric=" << first_numeric - seq.begin() << endl;
1265 cout << "last_non_zero=" << last_non_zero - seq.begin() << endl;
1266 for (unsigned i=0; i<seq.size(); ++i) {
1267 if (touched[i]) cout << i << " is touched" << endl;
1269 cout << "end in combine" << endl;
1272 // there should not be any terms with coeff 0 from the beginning,
1273 // so it should be safe to skip this step
1274 if (number_of_zeroes!=0) {
1275 drop_coeff_0_terms(first_numeric,last_non_zero,touched,number_of_zeroes);
1277 cout << "in combine after drop:" << endl;
1279 cout << "size=" << seq.end() - seq.begin() << endl;
1280 cout << "first_numeric=" << first_numeric - seq.begin() << endl;
1281 cout << "last_non_zero=" << last_non_zero - seq.begin() << endl;
1282 for (unsigned i=0; i<seq.size(); ++i) {
1283 if (touched[i]) cout << i << " is touched" << endl;
1285 cout << "end in combine after drop" << endl;
1289 add_numerics_to_hashtab(first_numeric,last_non_zero);
1291 // pop zero elements
1292 for (unsigned i=0; i<number_of_zeroes; ++i) {
1296 // shrink hashtabsize to calculated value
1297 ASSERT(!has_coeff_0());
1301 ASSERT(!has_coeff_0());
1304 #endif // def EXPAIRSEQ_USE_HASHTAB
1306 bool expairseq::is_canonical() const
1308 if (seq.size()<=1) return 1;
1310 #ifdef EXPAIRSEQ_USE_HASHTAB
1311 if (hashtabsize>0) return 1; // not canoncalized
1312 #endif // def EXPAIRSEQ_USE_HASHTAB
1314 epvector::const_iterator it=seq.begin();
1315 epvector::const_iterator it_last=it;
1316 for (++it; it!=seq.end(); it_last=it, ++it) {
1317 if (!((*it_last).is_less(*it)||(*it_last).is_equal(*it))) {
1318 if (!is_ex_exactly_of_type((*it_last).rest,numeric)||
1319 !is_ex_exactly_of_type((*it).rest,numeric)) {
1320 // double test makes it easier to set a breakpoint...
1321 if (!is_ex_exactly_of_type((*it_last).rest,numeric)||
1322 !is_ex_exactly_of_type((*it).rest,numeric)) {
1323 printpair(cout,*it_last,0);
1325 printpair(cout,*it,0);
1327 cout << "pair1:" << endl;
1328 (*it_last).rest.printtree(cout);
1329 (*it_last).coeff.printtree(cout);
1330 cout << "pair2:" << endl;
1331 (*it).rest.printtree(cout);
1332 (*it).coeff.printtree(cout);
1341 epvector * expairseq::expandchildren(unsigned options) const
1343 epvector::const_iterator last=seq.end();
1344 epvector::const_iterator cit=seq.begin();
1346 ex const & expanded_ex=(*cit).rest.expand(options);
1347 if (!are_ex_trivially_equal((*cit).rest,expanded_ex)) {
1349 // something changed, copy seq, eval and return it
1350 epvector *s=new epvector;
1351 s->reserve(seq.size());
1353 // copy parts of seq which are known not to have changed
1354 epvector::const_iterator cit2=seq.begin();
1356 s->push_back(*cit2);
1359 // copy first changed element
1360 s->push_back(combine_ex_with_coeff_to_pair(expanded_ex,
1364 while (cit2!=last) {
1365 s->push_back(combine_ex_with_coeff_to_pair((*cit2).rest.expand(options),
1374 return 0; // nothing has changed
1377 epvector * expairseq::evalchildren(int level) const
1379 // returns a NULL pointer if nothing had to be evaluated
1380 // returns a pointer to a newly created epvector otherwise
1381 // (which has to be deleted somewhere else)
1386 if (level == -max_recursion_level) {
1387 throw(std::runtime_error("max recursion level reached"));
1391 epvector::const_iterator last=seq.end();
1392 epvector::const_iterator cit=seq.begin();
1394 ex const & evaled_ex=(*cit).rest.eval(level);
1395 if (!are_ex_trivially_equal((*cit).rest,evaled_ex)) {
1397 // something changed, copy seq, eval and return it
1398 epvector *s=new epvector;
1399 s->reserve(seq.size());
1401 // copy parts of seq which are known not to have changed
1402 epvector::const_iterator cit2=seq.begin();
1404 s->push_back(*cit2);
1407 // copy first changed element
1408 s->push_back(combine_ex_with_coeff_to_pair(evaled_ex,
1412 while (cit2!=last) {
1413 s->push_back(combine_ex_with_coeff_to_pair((*cit2).rest.eval(level),
1422 return 0; // nothing has changed
1425 epvector expairseq::evalfchildren(int level) const
1428 s.reserve(seq.size());
1433 if (level == -max_recursion_level) {
1434 throw(std::runtime_error("max recursion level reached"));
1437 for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
1438 s.push_back(combine_ex_with_coeff_to_pair((*it).rest.evalf(level),
1444 epvector expairseq::normalchildren(int level) const
1447 s.reserve(seq.size());
1452 if (level == -max_recursion_level) {
1453 throw(std::runtime_error("max recursion level reached"));
1456 for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
1457 s.push_back(combine_ex_with_coeff_to_pair((*it).rest.normal(level),
1463 epvector expairseq::diffchildren(symbol const & y) const
1466 s.reserve(seq.size());
1468 for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
1469 s.push_back(combine_ex_with_coeff_to_pair((*it).rest.diff(y),
1475 epvector * expairseq::subschildren(lst const & ls, lst const & lr) const
1477 // returns a NULL pointer if nothing had to be substituted
1478 // returns a pointer to a newly created epvector otherwise
1479 // (which has to be deleted somewhere else)
1481 epvector::const_iterator last=seq.end();
1482 epvector::const_iterator cit=seq.begin();
1484 ex const & subsed_ex=(*cit).rest.subs(ls,lr);
1485 if (!are_ex_trivially_equal((*cit).rest,subsed_ex)) {
1487 // something changed, copy seq, subs and return it
1488 epvector *s=new epvector;
1489 s->reserve(seq.size());
1491 // copy parts of seq which are known not to have changed
1492 epvector::const_iterator cit2=seq.begin();
1494 s->push_back(*cit2);
1497 // copy first changed element
1498 s->push_back(combine_ex_with_coeff_to_pair(subsed_ex,
1502 while (cit2!=last) {
1503 s->push_back(combine_ex_with_coeff_to_pair((*cit2).rest.subs(ls,lr),
1512 return 0; // nothing has changed
1516 epvector expairseq::subschildren(lst const & ls, lst const & lr) const
1519 s.reserve(seq.size());
1521 for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
1522 s.push_back(split_ex_to_pair((*it).rest.subs(ls,lr),(*it).coeff));
1529 void expairseq::sort(epviter first, epviter last, expair_is_less comp)
1531 if (first != last) {
1532 introsort_loop(first, last, lg(last - first) * 2, comp);
1533 __final_insertion_sort(first, last, comp);
1537 ptrdiff_t expairseq::lg(ptrdiff_t n)
1540 for (k = 0; n > 1; n >>= 1) ++k;
1544 void expairseq::introsort_loop(epviter first, epviter last,
1545 ptrdiff_t depth_limit, expair_is_less comp)
1547 while (last - first > stl_threshold) {
1548 if (depth_limit == 0) {
1549 partial_sort(first, last, last, comp);
1553 epviter cut = unguarded_partition(first, last,
1554 expair(__median(*first, *(first + (last - first)/2),
1555 *(last - 1), comp)), comp);
1556 introsort_loop(cut, last, depth_limit, comp);
1561 epviter expairseq::unguarded_partition(epviter first, epviter last,
1562 expair pivot, expair_is_less comp)
1565 while (comp(*first, pivot)) ++first;
1567 while (comp(pivot, *last)) --last;
1568 if (!(first < last)) return first;
1569 iter_swap(first, last);
1574 void expairseq::partial_sort(epviter first, epviter middle, epviter last,
1575 expair_is_less comp) {
1576 make_heap(first, middle, comp);
1577 for (RandomAccessIterator i = middle; i < last; ++i)
1578 if (comp(*i, *first))
1579 __pop_heap(first, middle, i, T(*i), comp, distance_type(first));
1580 sort_heap(first, middle, comp);
1585 // static member variables
1590 unsigned expairseq::precedence=10;
1592 #ifdef EXPAIRSEQ_USE_HASHTAB
1593 unsigned expairseq::maxhashtabsize=0x4000000U;
1594 unsigned expairseq::minhashtabsize=0x1000U;
1595 unsigned expairseq::hashtabfactor=1;
1596 #endif // def EXPAIRSEQ_USE_HASHTAB
1602 const expairseq some_expairseq;
1603 type_info const & typeid_expairseq=typeid(some_expairseq);