1 /** @file expairseq.cpp
3 * Implementation of sequences of expression pairs. */
6 * GiNaC Copyright (C) 1999 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
27 #include "expairseq.h"
30 #ifdef EXPAIRSEQ_USE_HASHTAB
31 #error "!!!!!!!!TODO: expair_needs_further_processing not yet implemented for hashtabs, sorry. A.F."
32 #endif // def EXPAIRSEQ_USE_HASHTAB
41 bool operator()(epp const & lh, epp const & rh) const
43 return (*lh).is_less(*rh);
48 // default constructor, destructor, copy constructor assignment operator and helpers
53 expairseq::expairseq(expairseq const & other)
55 debugmsg("expairseq copy constructor",LOGLEVEL_CONSTRUCT);
59 expairseq const & expairseq::operator=(expairseq const & other)
61 debugmsg("expairseq operator=",LOGLEVEL_ASSIGNMENT);
71 void expairseq::copy(expairseq const & other)
75 overall_coeff=other.overall_coeff;
76 #ifdef EXPAIRSEQ_USE_HASHTAB
78 hashtabsize=other.hashtabsize;
80 hashmask=other.hashmask;
81 hashtab.resize(hashtabsize);
82 epvector::const_iterator osb=other.seq.begin();
83 for (unsigned i=0; i<hashtabsize; ++i) {
85 for (epplist::const_iterator cit=other.hashtab[i].begin();
86 cit!=other.hashtab[i].end(); ++cit) {
87 hashtab[i].push_back(seq.begin()+((*cit)-osb));
93 #endif // def EXPAIRSEQ_USE_HASHTAB
100 expairseq::expairseq(ex const & lh, ex const & rh) : basic(TINFO_expairseq)
102 debugmsg("expairseq constructor from ex,ex",LOGLEVEL_CONSTRUCT);
103 construct_from_2_ex(lh,rh);
104 ASSERT(is_canonical());
107 expairseq::expairseq(exvector const & v) : basic(TINFO_expairseq)
109 debugmsg("expairseq constructor from exvector",LOGLEVEL_CONSTRUCT);
110 construct_from_exvector(v);
111 ASSERT(is_canonical());
115 expairseq::expairseq(epvector const & v, bool do_not_canonicalize) :
116 basic(TINFO_expairseq)
118 debugmsg("expairseq constructor from epvector",LOGLEVEL_CONSTRUCT);
119 if (do_not_canonicalize) {
121 #ifdef EXPAIRSEQ_USE_HASHTAB
122 combine_same_terms(); // to build hashtab
123 #endif // def EXPAIRSEQ_USE_HASHTAB
125 construct_from_epvector(v);
127 ASSERT(is_canonical());
131 expairseq::expairseq(epvector const & v, ex const & oc) :
132 basic(TINFO_expairseq), overall_coeff(oc)
134 debugmsg("expairseq constructor from epvector,ex",LOGLEVEL_CONSTRUCT);
135 construct_from_epvector(v);
136 ASSERT(is_canonical());
139 expairseq::expairseq(epvector * vp, ex const & oc) :
140 basic(TINFO_expairseq), overall_coeff(oc)
142 debugmsg("expairseq constructor from epvector *,ex",LOGLEVEL_CONSTRUCT);
144 construct_from_epvector(*vp);
146 ASSERT(is_canonical());
150 // functions overriding virtual functions from bases classes
155 basic * expairseq::duplicate() const
157 debugmsg("expairseq duplicate",LOGLEVEL_DUPLICATE);
158 return new expairseq(*this);
161 bool expairseq::info(unsigned inf) const
163 return basic::info(inf);
166 int expairseq::nops() const
168 if (overall_coeff.is_equal(default_overall_coeff())) {
174 ex expairseq::op(int const i) const
176 if (unsigned(i)<seq.size()) {
177 return recombine_pair_to_ex(seq[i]);
179 ASSERT(!overall_coeff.is_equal(default_overall_coeff()));
180 return overall_coeff;
183 ex & expairseq::let_op(int const i)
185 throw(std::logic_error("let_op not defined for expairseq and derived classes (add,mul,...)"));
188 ex expairseq::eval(int level) const
190 if ((level==1)&&(flags & status_flags::evaluated)) {
194 epvector * vp=evalchildren(level);
199 return (new expairseq(vp,overall_coeff))
200 ->setflag(status_flags::dynallocated |
201 status_flags::evaluated );
204 ex expairseq::evalf(int level) const
206 return thisexpairseq(evalfchildren(level),overall_coeff);
209 ex expairseq::normal(lst &sym_lst, lst &repl_lst, int level) const
211 ex n=thisexpairseq(normalchildren(level),overall_coeff);
212 return n.bp->basic::normal(sym_lst,repl_lst,level);
215 ex expairseq::subs(lst const & ls, lst const & lr) const
217 epvector * vp=subschildren(ls,lr);
221 return thisexpairseq(vp,overall_coeff);
226 int expairseq::compare_same_type(basic const & other) const
228 ASSERT(is_of_type(other, expairseq));
229 expairseq const & o=static_cast<expairseq const &>(const_cast<basic &>(other));
233 // compare number of elements
234 if (seq.size() != o.seq.size()) {
235 return (seq.size()<o.seq.size()) ? -1 : 1;
238 // compare overall_coeff
239 cmpval=overall_coeff.compare(o.overall_coeff);
240 if (cmpval!=0) return cmpval;
242 //if (seq.size()==0) return 0; // empty expairseq's are equal
244 #ifdef EXPAIRSEQ_USE_HASHTAB
245 ASSERT(hashtabsize==o.hashtabsize);
246 if (hashtabsize==0) {
247 #endif // def EXPAIRSEQ_USE_HASHTAB
248 epvector::const_iterator cit1=seq.begin();
249 epvector::const_iterator cit2=o.seq.begin();
250 epvector::const_iterator last1=seq.end();
251 epvector::const_iterator last2=o.seq.end();
253 for (; (cit1!=last1)&&(cit2!=last2); ++cit1, ++cit2) {
254 cmpval=(*cit1).compare(*cit2);
255 if (cmpval!=0) return cmpval;
262 #ifdef EXPAIRSEQ_USE_HASHTAB
265 // compare number of elements in each hashtab entry
266 for (unsigned i=0; i<hashtabsize; ++i) {
267 unsigned cursize=hashtab[i].size();
268 if (cursize != o.hashtab[i].size()) {
269 return (cursize < o.hashtab[i].size()) ? -1 : 1;
273 // compare individual (sorted) hashtab entries
274 for (unsigned i=0; i<hashtabsize; ++i) {
275 unsigned sz=hashtab[i].size();
277 epplist const & eppl1=hashtab[i];
278 epplist const & eppl2=o.hashtab[i];
279 epplist::const_iterator it1=eppl1.begin();
280 epplist::const_iterator it2=eppl2.begin();
281 while (it1!=eppl1.end()) {
282 cmpval=(*(*it1)).compare(*(*it2));
283 if (cmpval!=0) return cmpval;
291 #endif // def EXPAIRSEQ_USE_HASHTAB
294 bool expairseq::is_equal_same_type(basic const & other) const
296 expairseq const & o=dynamic_cast<expairseq const &>(const_cast<basic &>(other));
298 // compare number of elements
299 if (seq.size() != o.seq.size()) return false;
301 // compare overall_coeff
302 if (!overall_coeff.is_equal(o.overall_coeff)) return false;
304 #ifdef EXPAIRSEQ_USE_HASHTAB
305 // compare number of elements in each hashtab entry
306 if (hashtabsize!=o.hashtabsize) {
307 cout << "this:" << endl;
309 cout << "other:" << endl;
310 other.printtree(cout,0);
313 ASSERT(hashtabsize==o.hashtabsize);
315 if (hashtabsize==0) {
316 #endif // def EXPAIRSEQ_USE_HASHTAB
317 epvector::const_iterator cit1=seq.begin();
318 epvector::const_iterator cit2=o.seq.begin();
319 epvector::const_iterator last1=seq.end();
321 while (cit1!=last1) {
322 if (!(*cit1).is_equal(*cit2)) return false;
328 #ifdef EXPAIRSEQ_USE_HASHTAB
331 for (unsigned i=0; i<hashtabsize; ++i) {
332 if (hashtab[i].size() != o.hashtab[i].size()) return false;
335 // compare individual sorted hashtab entries
336 for (unsigned i=0; i<hashtabsize; ++i) {
337 unsigned sz=hashtab[i].size();
339 epplist const & eppl1=hashtab[i];
340 epplist const & eppl2=o.hashtab[i];
341 epplist::const_iterator it1=eppl1.begin();
342 epplist::const_iterator it2=eppl2.begin();
343 while (it1!=eppl1.end()) {
344 if (!(*(*it1)).is_equal(*(*it2))) return false;
352 #endif // def EXPAIRSEQ_USE_HASHTAB
355 unsigned expairseq::return_type(void) const
357 return return_types::noncommutative_composite;
360 unsigned expairseq::calchash(void) const
362 unsigned v=golden_ratio_hash(tinfo());
363 epvector::const_iterator last=seq.end();
364 for (epvector::const_iterator cit=seq.begin(); cit!=last; ++cit) {
365 #ifndef EXPAIRSEQ_USE_HASHTAB
366 v=rotate_left_31(v); // rotation would spoil commutativity
367 #endif // ndef EXPAIRSEQ_USE_HASHTAB
368 v ^= (*cit).rest.gethash();
371 v ^= overall_coeff.gethash();
374 // store calculated hash value only if object is already evaluated
375 if (flags & status_flags::evaluated) {
376 setflag(status_flags::hash_calculated);
383 ex expairseq::expand(unsigned options) const
385 epvector * vp=expandchildren(options);
389 return thisexpairseq(vp,overall_coeff);
393 // new virtual functions which can be overridden by derived classes
398 ex expairseq::thisexpairseq(epvector const & v,ex const & oc) const
400 return expairseq(v,oc);
403 ex expairseq::thisexpairseq(epvector * vp, ex const & oc) const
405 return expairseq(vp,oc);
408 expair expairseq::split_ex_to_pair(ex const & e) const
410 return expair(e,exONE());
413 expair expairseq::combine_ex_with_coeff_to_pair(ex const & e,
416 ASSERT(is_ex_exactly_of_type(c,numeric));
421 expair expairseq::combine_pair_with_coeff_to_pair(expair const & p,
424 ASSERT(is_ex_exactly_of_type(p.coeff,numeric));
425 ASSERT(is_ex_exactly_of_type(c,numeric));
427 return expair(p.rest,ex_to_numeric(p.coeff).mul_dyn(ex_to_numeric(c)));
430 ex expairseq::recombine_pair_to_ex(expair const & p) const
432 return lst(p.rest,p.coeff);
435 bool expairseq::expair_needs_further_processing(epp it)
440 ex expairseq::default_overall_coeff(void) const
445 void expairseq::combine_overall_coeff(ex const & c)
447 ASSERT(is_ex_exactly_of_type(overall_coeff,numeric));
448 ASSERT(is_ex_exactly_of_type(c,numeric));
449 overall_coeff = ex_to_numeric(overall_coeff).add_dyn(ex_to_numeric(c));
452 void expairseq::combine_overall_coeff(ex const & c1, ex const & c2)
454 ASSERT(is_ex_exactly_of_type(overall_coeff,numeric));
455 ASSERT(is_ex_exactly_of_type(c1,numeric));
456 ASSERT(is_ex_exactly_of_type(c2,numeric));
457 overall_coeff = ex_to_numeric(overall_coeff).
458 add_dyn(ex_to_numeric(c1).mul(ex_to_numeric(c2)));
461 bool expairseq::can_make_flat(expair const & p) const
468 // non-virtual functions in this class
471 void expairseq::construct_from_2_ex_via_exvector(ex const & lh, ex const & rh)
477 construct_from_exvector(v);
478 #ifdef EXPAIRSEQ_USE_HASHTAB
479 ASSERT((hashtabsize==0)||(hashtabsize>=minhashtabsize));
480 ASSERT(hashtabsize==calc_hashtabsize(seq.size()));
481 #endif // def EXPAIRSEQ_USE_HASHTAB
484 void expairseq::construct_from_2_ex(ex const & lh, ex const & rh)
486 if (lh.bp->tinfo()==tinfo()) {
487 if (rh.bp->tinfo()==tinfo()) {
488 #ifdef EXPAIRSEQ_USE_HASHTAB
489 unsigned totalsize=ex_to_expairseq(lh).seq.size()+
490 ex_to_expairseq(rh).seq.size();
491 if (calc_hashtabsize(totalsize)!=0) {
492 construct_from_2_ex_via_exvector(lh,rh);
494 #endif // def EXPAIRSEQ_USE_HASHTAB
495 construct_from_2_expairseq(ex_to_expairseq(lh),
496 ex_to_expairseq(rh));
497 #ifdef EXPAIRSEQ_USE_HASHTAB
499 #endif // def EXPAIRSEQ_USE_HASHTAB
502 #ifdef EXPAIRSEQ_USE_HASHTAB
503 unsigned totalsize=ex_to_expairseq(lh).seq.size()+1;
504 if (calc_hashtabsize(totalsize)!=0) {
505 construct_from_2_ex_via_exvector(lh,rh);
507 #endif // def EXPAIRSEQ_USE_HASHTAB
508 construct_from_expairseq_ex(ex_to_expairseq(lh),rh);
509 #ifdef EXPAIRSEQ_USE_HASHTAB
511 #endif // def EXPAIRSEQ_USE_HASHTAB
514 } else if (rh.bp->tinfo()==tinfo()) {
515 #ifdef EXPAIRSEQ_USE_HASHTAB
516 unsigned totalsize=ex_to_expairseq(rh).seq.size()+1;
517 if (calc_hashtabsize(totalsize)!=0) {
518 construct_from_2_ex_via_exvector(lh,rh);
520 #endif // def EXPAIRSEQ_USE_HASHTAB
521 construct_from_expairseq_ex(ex_to_expairseq(rh),lh);
522 #ifdef EXPAIRSEQ_USE_HASHTAB
524 #endif // def EXPAIRSEQ_USE_HASHTAB
528 #ifdef EXPAIRSEQ_USE_HASHTAB
529 if (calc_hashtabsize(2)!=0) {
530 construct_from_2_ex_via_exvector(lh,rh);
534 #endif // def EXPAIRSEQ_USE_HASHTAB
536 if (is_ex_exactly_of_type(lh,numeric)) {
537 if (is_ex_exactly_of_type(rh,numeric)) {
538 combine_overall_coeff(lh);
539 combine_overall_coeff(rh);
541 combine_overall_coeff(lh);
542 seq.push_back(split_ex_to_pair(rh));
545 if (is_ex_exactly_of_type(rh,numeric)) {
546 combine_overall_coeff(rh);
547 seq.push_back(split_ex_to_pair(lh));
549 expair p1=split_ex_to_pair(lh);
550 expair p2=split_ex_to_pair(rh);
552 int cmpval=p1.rest.compare(p2.rest);
554 p1.coeff=ex_to_numeric(p1.coeff).add_dyn(ex_to_numeric(p2.coeff));
555 if (!ex_to_numeric(p1.coeff).is_zero()) {
556 // no further processing is necessary, since this
557 // one element will usually be recombined in eval()
574 void expairseq::construct_from_2_expairseq(expairseq const & s1,
575 expairseq const & s2)
577 combine_overall_coeff(s1.overall_coeff);
578 combine_overall_coeff(s2.overall_coeff);
580 epvector::const_iterator first1=s1.seq.begin();
581 epvector::const_iterator last1=s1.seq.end();
582 epvector::const_iterator first2=s2.seq.begin();
583 epvector::const_iterator last2=s2.seq.end();
585 seq.reserve(s1.seq.size()+s2.seq.size());
587 bool needs_further_processing=false;
589 while (first1!=last1 && first2!=last2) {
590 int cmpval=(*first1).rest.compare((*first2).rest);
593 numeric const & newcoeff=ex_to_numeric((*first1).coeff).
594 add(ex_to_numeric((*first2).coeff));
595 if (!newcoeff.is_zero()) {
596 seq.push_back(expair((*first1).rest,newcoeff));
597 if (expair_needs_further_processing(seq.end()-1)) {
598 needs_further_processing = true;
603 } else if (cmpval<0) {
604 seq.push_back(*first1);
607 seq.push_back(*first2);
612 while (first1!=last1) {
613 seq.push_back(*first1);
616 while (first2!=last2) {
617 seq.push_back(*first2);
621 if (needs_further_processing) {
624 construct_from_epvector(v);
628 void expairseq::construct_from_expairseq_ex(expairseq const & s,
631 combine_overall_coeff(s.overall_coeff);
632 if (is_ex_exactly_of_type(e,numeric)) {
633 combine_overall_coeff(e);
638 epvector::const_iterator first=s.seq.begin();
639 epvector::const_iterator last=s.seq.end();
640 expair p=split_ex_to_pair(e);
642 seq.reserve(s.seq.size()+1);
645 bool needs_further_processing=false;
647 // merge p into s.seq
648 while (first!=last) {
649 int cmpval=(*first).rest.compare(p.rest);
652 numeric const & newcoeff=ex_to_numeric((*first).coeff).
653 add(ex_to_numeric(p.coeff));
654 if (!newcoeff.is_zero()) {
655 seq.push_back(expair((*first).rest,newcoeff));
656 if (expair_needs_further_processing(seq.end()-1)) {
657 needs_further_processing = true;
663 } else if (cmpval<0) {
664 seq.push_back(*first);
674 // while loop exited because p was pushed, now push rest of s.seq
675 while (first!=last) {
676 seq.push_back(*first);
680 // while loop exited because s.seq was pushed, now push p
684 if (needs_further_processing) {
687 construct_from_epvector(v);
691 void expairseq::construct_from_exvector(exvector const & v)
693 // simplifications: +(a,+(b,c),d) -> +(a,b,c,d) (associativity)
694 // +(d,b,c,a) -> +(a,b,c,d) (canonicalization)
695 // +(...,x,*(x,c1),*(x,c2)) -> +(...,*(x,1+c1+c2)) (c1, c2 numeric())
696 // (same for (+,*) -> (*,^)
699 #ifdef EXPAIRSEQ_USE_HASHTAB
700 combine_same_terms();
703 combine_same_terms_sorted_seq();
704 #endif // def EXPAIRSEQ_USE_HASHTAB
707 void expairseq::construct_from_epvector(epvector const & v)
709 // simplifications: +(a,+(b,c),d) -> +(a,b,c,d) (associativity)
710 // +(d,b,c,a) -> +(a,b,c,d) (canonicalization)
711 // +(...,x,*(x,c1),*(x,c2)) -> +(...,*(x,1+c1+c2)) (c1, c2 numeric())
712 // (same for (+,*) -> (*,^)
715 #ifdef EXPAIRSEQ_USE_HASHTAB
716 combine_same_terms();
719 combine_same_terms_sorted_seq();
720 #endif // def EXPAIRSEQ_USE_HASHTAB
725 void expairseq::make_flat(exvector const & v)
727 exvector::const_iterator cit, citend = v.end();
729 // count number of operands which are of same expairseq derived type
730 // and their cumulative number of operands
734 while (cit!=citend) {
735 if (cit->bp->tinfo()==tinfo()) {
737 noperands+=ex_to_expairseq(*cit).seq.size();
742 // reserve seq and coeffseq which will hold all operands
743 seq.reserve(v.size()+noperands-nexpairseqs);
745 // copy elements and split off numerical part
747 while (cit!=citend) {
748 if (cit->bp->tinfo()==tinfo()) {
749 expairseq const & subseqref=ex_to_expairseq(*cit);
750 combine_overall_coeff(subseqref.overall_coeff);
751 epvector::const_iterator cit_s=subseqref.seq.begin();
752 while (cit_s!=subseqref.seq.end()) {
753 seq.push_back(*cit_s);
757 if (is_ex_exactly_of_type(*cit,numeric)) {
758 combine_overall_coeff(*cit);
760 seq.push_back(split_ex_to_pair(*cit));
767 cout << "after make flat" << endl;
768 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
769 (*cit).printraw(cout);
776 void expairseq::make_flat(epvector const & v)
778 epvector::const_iterator cit, citend = v.end();
780 // count number of operands which are of same expairseq derived type
781 // and their cumulative number of operands
785 while (cit!=citend) {
786 if (cit->rest.bp->tinfo()==tinfo()) {
788 noperands+=ex_to_expairseq((*cit).rest).seq.size();
793 // reserve seq and coeffseq which will hold all operands
794 seq.reserve(v.size()+noperands-nexpairseqs);
796 // copy elements and split off numerical part
798 while (cit!=citend) {
799 if ((cit->rest.bp->tinfo()==tinfo())&&can_make_flat(*cit)) {
800 expairseq const & subseqref=ex_to_expairseq((*cit).rest);
801 combine_overall_coeff(ex_to_numeric(subseqref.overall_coeff),
802 ex_to_numeric((*cit).coeff));
803 epvector::const_iterator cit_s=subseqref.seq.begin();
804 while (cit_s!=subseqref.seq.end()) {
805 seq.push_back(expair((*cit_s).rest,
806 ex_to_numeric((*cit_s).coeff).mul_dyn(ex_to_numeric((*cit).coeff))));
807 //seq.push_back(combine_pair_with_coeff_to_pair(*cit_s,
812 if ((*cit).is_numeric_with_coeff_1()) {
813 combine_overall_coeff((*cit).rest);
814 //if (is_ex_exactly_of_type((*cit).rest,numeric)) {
815 // combine_overall_coeff(recombine_pair_to_ex(*cit));
824 epvector * expairseq::bubblesort(epvector::iterator itbegin, epvector::iterator itend)
826 unsigned n=itend-itbegin;
828 epvector * sp=new epvector;
831 epvector::iterator last=itend-1;
832 for (epvector::iterator it1=itbegin; it1!=last; ++it1) {
833 for (epvector::iterator it2=it1+1; it2!=itend; ++it2) {
834 if ((*it2).rest.compare((*it1).rest)<0) {
840 sp->push_back(*last);
844 epvector * expairseq::mergesort(epvector::iterator itbegin, epvector::iterator itend)
846 unsigned n=itend-itbegin;
849 epvector * sp=new epvector;
850 sp->push_back(*itbegin);
854 if (n<16) return bubblesort(itbegin, itend);
857 epvector * s1p=mergesort(itbegin, itbegin+m);
858 epvector * s2p=mergesort(itbegin+m, itend);
860 epvector * sp=new epvector;
861 sp->reserve(s1p->size()+s2p->size());
863 epvector::iterator first1=s1p->begin();
864 epvector::iterator last1=s1p->end();
866 epvector::iterator first2=s2p->begin();
867 epvector::iterator last2=s2p->end();
869 while (first1 != last1 && first2 != last2) {
870 if ((*first1).rest.compare((*first2).rest)<0) {
871 sp->push_back(*first1);
874 sp->push_back(*first2);
879 if (first1 != last1) {
880 while (first1 != last1) {
881 sp->push_back(*first1);
885 while (first2 != last2) {
886 sp->push_back(*first2);
898 void expairseq::canonicalize(void)
901 sort(seq.begin(),seq.end(),expair_is_less());
903 sort(seq.begin(),seq.end(),expair_is_less_old());
905 if (is_ex_exactly_of_type((*(seq.begin())).rest,numeric)) {
906 sort(seq.begin(),seq.end(),expair_is_less());
908 epvector::iterator last_numeric=seq.end();
911 } while (is_ex_exactly_of_type((*last_numeric).rest,numeric));
913 sort(last_numeric,seq.end(),expair_is_less());
919 epvector * sorted_seqp=mergesort(seq.begin(),seq.end());
920 epvector::iterator last=sorted_seqp->end();
921 epvector::iterator it2=seq.begin();
922 for (epvector::iterator it1=sorted_seqp->begin(); it1!=last; ++it1, ++it2) {
929 cout << "after canonicalize" << endl;
930 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
931 (*cit).printraw(cout);
938 void expairseq::combine_same_terms_sorted_seq(void)
940 bool needs_further_processing=false;
942 // combine same terms, drop term with coeff 0
944 epvector::iterator itin1=seq.begin();
945 epvector::iterator itin2=itin1+1;
946 epvector::iterator itout=itin1;
947 epvector::iterator last=seq.end();
948 // must_copy will be set to true the first time some combination is possible
949 // from then on the sequence has changed and must be compacted
950 bool must_copy=false;
951 while (itin2!=last) {
952 if ((*itin1).rest.compare((*itin2).rest)==0) {
953 (*itin1).coeff=ex_to_numeric((*itin1).coeff).
954 add_dyn(ex_to_numeric((*itin2).coeff));
955 if (expair_needs_further_processing(itin1)) {
956 needs_further_processing = true;
960 if (!ex_to_numeric((*itin1).coeff).is_zero()) {
970 if (!ex_to_numeric((*itin1).coeff).is_zero()) {
977 seq.erase(itout,last);
982 cout << "after combine" << endl;
983 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
984 (*cit).printraw(cout);
990 if (needs_further_processing) {
993 construct_from_epvector(v);
997 #ifdef EXPAIRSEQ_USE_HASHTAB
999 unsigned expairseq::calc_hashtabsize(unsigned sz) const
1002 unsigned nearest_power_of_2 = 1 << log2(sz);
1003 // if (nearest_power_of_2 < maxhashtabsize/hashtabfactor) {
1004 // size=nearest_power_of_2*hashtabfactor;
1005 size=nearest_power_of_2/hashtabfactor;
1006 if (size<minhashtabsize) return 0;
1007 ASSERT(hashtabsize<=0x8000000U); // really max size due to 31 bit hashing
1008 // hashtabsize must be a power of 2
1009 ASSERT((1U << log2(size))==size);
1013 unsigned expairseq::calc_hashindex(ex const & e) const
1015 // calculate hashindex
1016 unsigned hash=e.gethash();
1018 if (is_a_numeric_hash(hash)) {
1021 hashindex=hash & hashmask;
1022 // last hashtab entry is reserved for numerics
1023 if (hashindex==hashmask) hashindex=0;
1025 ASSERT(hashindex>=0);
1026 ASSERT((hashindex<hashtabsize)||(hashtabsize==0));
1030 void expairseq::shrink_hashtab(void)
1032 unsigned new_hashtabsize;
1033 while (hashtabsize!=(new_hashtabsize=calc_hashtabsize(seq.size()))) {
1034 ASSERT(new_hashtabsize<hashtabsize);
1035 if (new_hashtabsize==0) {
1042 // shrink by a factor of 2
1043 unsigned half_hashtabsize=hashtabsize/2;
1044 for (unsigned i=0; i<half_hashtabsize-1; ++i) {
1045 hashtab[i].merge(hashtab[i+half_hashtabsize],epp_is_less());
1047 // special treatment for numeric hashes
1048 hashtab[0].merge(hashtab[half_hashtabsize-1],epp_is_less());
1049 hashtab[half_hashtabsize-1]=hashtab[hashtabsize-1];
1050 hashtab.resize(half_hashtabsize);
1051 hashtabsize=half_hashtabsize;
1052 hashmask=hashtabsize-1;
1056 void expairseq::remove_hashtab_entry(epvector::const_iterator element)
1058 if (hashtabsize==0) return; // nothing to do
1060 // calculate hashindex of element to be deleted
1061 unsigned hashindex=calc_hashindex((*element).rest);
1063 // find it in hashtab and remove it
1064 epplist & eppl=hashtab[hashindex];
1065 epplist::iterator epplit=eppl.begin();
1067 while (epplit!=eppl.end()) {
1068 if (*epplit == element) {
1077 cout << "tried to erase " << element-seq.begin() << endl;
1078 cout << "size " << seq.end()-seq.begin() << endl;
1080 unsigned hashindex=calc_hashindex((*element).rest);
1081 epplist & eppl=hashtab[hashindex];
1082 epplist::iterator epplit=eppl.begin();
1084 while (epplit!=eppl.end()) {
1085 if (*epplit == element) {
1097 void expairseq::move_hashtab_entry(epvector::const_iterator oldpos,
1098 epvector::iterator newpos)
1100 ASSERT(hashtabsize!=0);
1102 // calculate hashindex of element which was moved
1103 unsigned hashindex=calc_hashindex((*newpos).rest);
1105 // find it in hashtab and modify it
1106 epplist & eppl=hashtab[hashindex];
1107 epplist::iterator epplit=eppl.begin();
1108 while (epplit!=eppl.end()) {
1109 if (*epplit == oldpos) {
1115 ASSERT(epplit!=eppl.end());
1118 void expairseq::sorted_insert(epplist & eppl, epp elem)
1120 epplist::iterator current=eppl.begin();
1121 while ((current!=eppl.end())&&((*(*current)).is_less(*elem))) {
1124 eppl.insert(current,elem);
1127 void expairseq::build_hashtab_and_combine(epvector::iterator & first_numeric,
1128 epvector::iterator & last_non_zero,
1129 vector<bool> & touched,
1130 unsigned & number_of_zeroes)
1132 epp current=seq.begin();
1134 while (current!=first_numeric) {
1135 if (is_ex_exactly_of_type((*current).rest,numeric)) {
1137 iter_swap(current,first_numeric);
1139 // calculate hashindex
1140 unsigned currenthashindex=calc_hashindex((*current).rest);
1142 // test if there is already a matching expair in the hashtab-list
1143 epplist & eppl=hashtab[currenthashindex];
1144 epplist::iterator epplit=eppl.begin();
1145 while (epplit!=eppl.end()) {
1146 if ((*current).rest.is_equal((*(*epplit)).rest)) break;
1149 if (epplit==eppl.end()) {
1150 // no matching expair found, append this to end of list
1151 sorted_insert(eppl,current);
1154 // epplit points to a matching expair, combine it with current
1155 (*(*epplit)).coeff=ex_to_numeric((*(*epplit)).coeff).
1156 add_dyn(ex_to_numeric((*current).coeff));
1158 // move obsolete current expair to end by swapping with last_non_zero element
1159 // if this was a numeric, it is swapped with the expair before first_numeric
1160 iter_swap(current,last_non_zero);
1162 if (first_numeric!=last_non_zero) iter_swap(first_numeric,current);
1165 // test if combined term has coeff 0 and can be removed is done later
1166 touched[(*epplit)-seq.begin()]=true;
1172 void expairseq::drop_coeff_0_terms(epvector::iterator & first_numeric,
1173 epvector::iterator & last_non_zero,
1174 vector<bool> & touched,
1175 unsigned & number_of_zeroes)
1177 // move terms with coeff 0 to end and remove them from hashtab
1178 // check only those elements which have been touched
1179 epp current=seq.begin();
1181 while (current!=first_numeric) {
1185 } else if (!ex_to_numeric((*current).coeff).is_equal(numZERO())) {
1189 remove_hashtab_entry(current);
1191 // move element to the end, unless it is already at the end
1192 if (current!=last_non_zero) {
1193 iter_swap(current,last_non_zero);
1195 bool numeric_swapped=first_numeric!=last_non_zero;
1196 if (numeric_swapped) iter_swap(first_numeric,current);
1197 epvector::iterator changed_entry;
1199 if (numeric_swapped) {
1200 changed_entry=first_numeric;
1202 changed_entry=last_non_zero;
1208 if (first_numeric!=current) {
1210 // change entry in hashtab which referred to first_numeric or last_non_zero to current
1211 move_hashtab_entry(changed_entry,current);
1212 touched[current-seq.begin()]=touched[changed_entry-seq.begin()];
1221 ASSERT(i==current-seq.begin());
1224 bool expairseq::has_coeff_0(void) const
1226 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
1227 if ((*cit).coeff.is_equal(exZERO())) {
1234 void expairseq::add_numerics_to_hashtab(epvector::iterator first_numeric,
1235 epvector::const_iterator last_non_zero)
1237 if (first_numeric==seq.end()) return; // no numerics
1239 epvector::iterator current=first_numeric;
1240 epvector::const_iterator last=last_non_zero+1;
1241 while (current!=last) {
1242 sorted_insert(hashtab[hashmask],current);
1247 void expairseq::combine_same_terms(void)
1249 // combine same terms, drop term with coeff 0, move numerics to end
1251 // calculate size of hashtab
1252 hashtabsize=calc_hashtabsize(seq.size());
1254 // hashtabsize is a power of 2
1255 hashmask=hashtabsize-1;
1259 hashtab.resize(hashtabsize);
1261 if (hashtabsize==0) {
1263 combine_same_terms_sorted_seq();
1264 ASSERT(!has_coeff_0());
1268 // iterate through seq, move numerics to end,
1269 // fill hashtab and combine same terms
1270 epvector::iterator first_numeric=seq.end();
1271 epvector::iterator last_non_zero=seq.end()-1;
1273 vector<bool> touched;
1274 touched.reserve(seq.size());
1275 for (unsigned i=0; i<seq.size(); ++i) touched[i]=false;
1277 unsigned number_of_zeroes=0;
1279 ASSERT(!has_coeff_0());
1280 build_hashtab_and_combine(first_numeric,last_non_zero,touched,number_of_zeroes);
1282 cout << "in combine:" << endl;
1284 cout << "size=" << seq.end() - seq.begin() << endl;
1285 cout << "first_numeric=" << first_numeric - seq.begin() << endl;
1286 cout << "last_non_zero=" << last_non_zero - seq.begin() << endl;
1287 for (unsigned i=0; i<seq.size(); ++i) {
1288 if (touched[i]) cout << i << " is touched" << endl;
1290 cout << "end in combine" << endl;
1293 // there should not be any terms with coeff 0 from the beginning,
1294 // so it should be safe to skip this step
1295 if (number_of_zeroes!=0) {
1296 drop_coeff_0_terms(first_numeric,last_non_zero,touched,number_of_zeroes);
1298 cout << "in combine after drop:" << endl;
1300 cout << "size=" << seq.end() - seq.begin() << endl;
1301 cout << "first_numeric=" << first_numeric - seq.begin() << endl;
1302 cout << "last_non_zero=" << last_non_zero - seq.begin() << endl;
1303 for (unsigned i=0; i<seq.size(); ++i) {
1304 if (touched[i]) cout << i << " is touched" << endl;
1306 cout << "end in combine after drop" << endl;
1310 add_numerics_to_hashtab(first_numeric,last_non_zero);
1312 // pop zero elements
1313 for (unsigned i=0; i<number_of_zeroes; ++i) {
1317 // shrink hashtabsize to calculated value
1318 ASSERT(!has_coeff_0());
1322 ASSERT(!has_coeff_0());
1325 #endif // def EXPAIRSEQ_USE_HASHTAB
1327 bool expairseq::is_canonical() const
1329 if (seq.size()<=1) return 1;
1331 #ifdef EXPAIRSEQ_USE_HASHTAB
1332 if (hashtabsize>0) return 1; // not canoncalized
1333 #endif // def EXPAIRSEQ_USE_HASHTAB
1335 epvector::const_iterator it=seq.begin();
1336 epvector::const_iterator it_last=it;
1337 for (++it; it!=seq.end(); it_last=it, ++it) {
1338 if (!((*it_last).is_less(*it)||(*it_last).is_equal(*it))) {
1339 if (!is_ex_exactly_of_type((*it_last).rest,numeric)||
1340 !is_ex_exactly_of_type((*it).rest,numeric)) {
1341 // double test makes it easier to set a breakpoint...
1342 if (!is_ex_exactly_of_type((*it_last).rest,numeric)||
1343 !is_ex_exactly_of_type((*it).rest,numeric)) {
1344 printpair(cout,*it_last,0);
1346 printpair(cout,*it,0);
1348 cout << "pair1:" << endl;
1349 (*it_last).rest.printtree(cout);
1350 (*it_last).coeff.printtree(cout);
1351 cout << "pair2:" << endl;
1352 (*it).rest.printtree(cout);
1353 (*it).coeff.printtree(cout);
1362 epvector * expairseq::expandchildren(unsigned options) const
1364 epvector::const_iterator last=seq.end();
1365 epvector::const_iterator cit=seq.begin();
1367 ex const & expanded_ex=(*cit).rest.expand(options);
1368 if (!are_ex_trivially_equal((*cit).rest,expanded_ex)) {
1370 // something changed, copy seq, eval and return it
1371 epvector *s=new epvector;
1372 s->reserve(seq.size());
1374 // copy parts of seq which are known not to have changed
1375 epvector::const_iterator cit2=seq.begin();
1377 s->push_back(*cit2);
1380 // copy first changed element
1381 s->push_back(combine_ex_with_coeff_to_pair(expanded_ex,
1385 while (cit2!=last) {
1386 s->push_back(combine_ex_with_coeff_to_pair((*cit2).rest.expand(options),
1395 return 0; // nothing has changed
1398 epvector * expairseq::evalchildren(int level) const
1400 // returns a NULL pointer if nothing had to be evaluated
1401 // returns a pointer to a newly created epvector otherwise
1402 // (which has to be deleted somewhere else)
1407 if (level == -max_recursion_level) {
1408 throw(std::runtime_error("max recursion level reached"));
1412 epvector::const_iterator last=seq.end();
1413 epvector::const_iterator cit=seq.begin();
1415 ex const & evaled_ex=(*cit).rest.eval(level);
1416 if (!are_ex_trivially_equal((*cit).rest,evaled_ex)) {
1418 // something changed, copy seq, eval and return it
1419 epvector *s=new epvector;
1420 s->reserve(seq.size());
1422 // copy parts of seq which are known not to have changed
1423 epvector::const_iterator cit2=seq.begin();
1425 s->push_back(*cit2);
1428 // copy first changed element
1429 s->push_back(combine_ex_with_coeff_to_pair(evaled_ex,
1433 while (cit2!=last) {
1434 s->push_back(combine_ex_with_coeff_to_pair((*cit2).rest.eval(level),
1443 return 0; // nothing has changed
1446 epvector expairseq::evalfchildren(int level) const
1449 s.reserve(seq.size());
1454 if (level == -max_recursion_level) {
1455 throw(std::runtime_error("max recursion level reached"));
1458 for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
1459 s.push_back(combine_ex_with_coeff_to_pair((*it).rest.evalf(level),
1465 epvector expairseq::normalchildren(int level) const
1468 s.reserve(seq.size());
1473 if (level == -max_recursion_level) {
1474 throw(std::runtime_error("max recursion level reached"));
1477 for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
1478 s.push_back(combine_ex_with_coeff_to_pair((*it).rest.normal(level),
1484 epvector expairseq::diffchildren(symbol const & y) const
1487 s.reserve(seq.size());
1489 for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
1490 s.push_back(combine_ex_with_coeff_to_pair((*it).rest.diff(y),
1496 epvector * expairseq::subschildren(lst const & ls, lst const & lr) const
1498 // returns a NULL pointer if nothing had to be substituted
1499 // returns a pointer to a newly created epvector otherwise
1500 // (which has to be deleted somewhere else)
1502 epvector::const_iterator last=seq.end();
1503 epvector::const_iterator cit=seq.begin();
1505 ex const & subsed_ex=(*cit).rest.subs(ls,lr);
1506 if (!are_ex_trivially_equal((*cit).rest,subsed_ex)) {
1508 // something changed, copy seq, subs and return it
1509 epvector *s=new epvector;
1510 s->reserve(seq.size());
1512 // copy parts of seq which are known not to have changed
1513 epvector::const_iterator cit2=seq.begin();
1515 s->push_back(*cit2);
1518 // copy first changed element
1519 s->push_back(combine_ex_with_coeff_to_pair(subsed_ex,
1523 while (cit2!=last) {
1524 s->push_back(combine_ex_with_coeff_to_pair((*cit2).rest.subs(ls,lr),
1533 return 0; // nothing has changed
1537 epvector expairseq::subschildren(lst const & ls, lst const & lr) const
1540 s.reserve(seq.size());
1542 for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
1543 s.push_back(split_ex_to_pair((*it).rest.subs(ls,lr),(*it).coeff));
1550 void expairseq::sort(epviter first, epviter last, expair_is_less comp)
1552 if (first != last) {
1553 introsort_loop(first, last, lg(last - first) * 2, comp);
1554 __final_insertion_sort(first, last, comp);
1558 ptrdiff_t expairseq::lg(ptrdiff_t n)
1561 for (k = 0; n > 1; n >>= 1) ++k;
1565 void expairseq::introsort_loop(epviter first, epviter last,
1566 ptrdiff_t depth_limit, expair_is_less comp)
1568 while (last - first > stl_threshold) {
1569 if (depth_limit == 0) {
1570 partial_sort(first, last, last, comp);
1574 epviter cut = unguarded_partition(first, last,
1575 expair(__median(*first, *(first + (last - first)/2),
1576 *(last - 1), comp)), comp);
1577 introsort_loop(cut, last, depth_limit, comp);
1582 epviter expairseq::unguarded_partition(epviter first, epviter last,
1583 expair pivot, expair_is_less comp)
1586 while (comp(*first, pivot)) ++first;
1588 while (comp(pivot, *last)) --last;
1589 if (!(first < last)) return first;
1590 iter_swap(first, last);
1595 void expairseq::partial_sort(epviter first, epviter middle, epviter last,
1596 expair_is_less comp) {
1597 make_heap(first, middle, comp);
1598 for (RandomAccessIterator i = middle; i < last; ++i)
1599 if (comp(*i, *first))
1600 __pop_heap(first, middle, i, T(*i), comp, distance_type(first));
1601 sort_heap(first, middle, comp);
1606 // static member variables
1611 unsigned expairseq::precedence=10;
1613 #ifdef EXPAIRSEQ_USE_HASHTAB
1614 unsigned expairseq::maxhashtabsize=0x4000000U;
1615 unsigned expairseq::minhashtabsize=0x1000U;
1616 unsigned expairseq::hashtabfactor=1;
1617 #endif // def EXPAIRSEQ_USE_HASHTAB
1623 const expairseq some_expairseq;
1624 type_info const & typeid_expairseq=typeid(some_expairseq);