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"
33 #ifdef EXPAIRSEQ_USE_HASHTAB
34 #error "FIXME: expair_needs_further_processing not yet implemented for hashtabs, sorry. A.F."
35 #endif // def EXPAIRSEQ_USE_HASHTAB
44 bool operator()(epp const & lh, epp const & rh) const
46 return (*lh).is_less(*rh);
51 // default constructor, destructor, copy constructor assignment operator and helpers
56 expairseq::expairseq(expairseq const & other)
58 debugmsg("expairseq copy constructor",LOGLEVEL_CONSTRUCT);
62 expairseq const & expairseq::operator=(expairseq const & other)
64 debugmsg("expairseq operator=",LOGLEVEL_ASSIGNMENT);
74 void expairseq::copy(expairseq const & other)
78 overall_coeff=other.overall_coeff;
79 #ifdef EXPAIRSEQ_USE_HASHTAB
81 hashtabsize=other.hashtabsize;
83 hashmask=other.hashmask;
84 hashtab.resize(hashtabsize);
85 epvector::const_iterator osb=other.seq.begin();
86 for (unsigned i=0; i<hashtabsize; ++i) {
88 for (epplist::const_iterator cit=other.hashtab[i].begin();
89 cit!=other.hashtab[i].end(); ++cit) {
90 hashtab[i].push_back(seq.begin()+((*cit)-osb));
96 #endif // def EXPAIRSEQ_USE_HASHTAB
100 // other constructors
103 expairseq::expairseq(ex const & lh, ex const & rh) : basic(TINFO_expairseq)
105 debugmsg("expairseq constructor from ex,ex",LOGLEVEL_CONSTRUCT);
106 construct_from_2_ex(lh,rh);
107 ASSERT(is_canonical());
110 expairseq::expairseq(exvector const & v) : basic(TINFO_expairseq)
112 debugmsg("expairseq constructor from exvector",LOGLEVEL_CONSTRUCT);
113 construct_from_exvector(v);
114 ASSERT(is_canonical());
118 expairseq::expairseq(epvector const & v, bool do_not_canonicalize) :
119 basic(TINFO_expairseq)
121 debugmsg("expairseq constructor from epvector",LOGLEVEL_CONSTRUCT);
122 if (do_not_canonicalize) {
124 #ifdef EXPAIRSEQ_USE_HASHTAB
125 combine_same_terms(); // to build hashtab
126 #endif // def EXPAIRSEQ_USE_HASHTAB
128 construct_from_epvector(v);
130 ASSERT(is_canonical());
134 expairseq::expairseq(epvector const & v, ex const & oc) :
135 basic(TINFO_expairseq), overall_coeff(oc)
137 debugmsg("expairseq constructor from epvector,ex",LOGLEVEL_CONSTRUCT);
138 construct_from_epvector(v);
139 ASSERT(is_canonical());
142 expairseq::expairseq(epvector * vp, ex const & oc) :
143 basic(TINFO_expairseq), overall_coeff(oc)
145 debugmsg("expairseq constructor from epvector *,ex",LOGLEVEL_CONSTRUCT);
147 construct_from_epvector(*vp);
149 ASSERT(is_canonical());
153 // functions overriding virtual functions from bases classes
158 basic * expairseq::duplicate() const
160 debugmsg("expairseq duplicate",LOGLEVEL_DUPLICATE);
161 return new expairseq(*this);
164 bool expairseq::info(unsigned inf) const
166 return basic::info(inf);
169 int expairseq::nops() const
171 if (overall_coeff.is_equal(default_overall_coeff())) {
177 ex expairseq::op(int const i) const
179 if (unsigned(i)<seq.size()) {
180 return recombine_pair_to_ex(seq[i]);
182 ASSERT(!overall_coeff.is_equal(default_overall_coeff()));
183 return overall_coeff;
186 ex & expairseq::let_op(int const i)
188 throw(std::logic_error("let_op not defined for expairseq and derived classes (add,mul,...)"));
191 ex expairseq::eval(int level) const
193 if ((level==1)&&(flags & status_flags::evaluated)) {
197 epvector * vp=evalchildren(level);
202 return (new expairseq(vp,overall_coeff))
203 ->setflag(status_flags::dynallocated |
204 status_flags::evaluated );
207 ex expairseq::evalf(int level) const
209 return thisexpairseq(evalfchildren(level),overall_coeff);
212 ex expairseq::normal(lst &sym_lst, lst &repl_lst, int level) const
214 ex n=thisexpairseq(normalchildren(level),overall_coeff);
215 return n.bp->basic::normal(sym_lst,repl_lst,level);
218 ex expairseq::subs(lst const & ls, lst const & lr) const
220 epvector * vp=subschildren(ls,lr);
224 return thisexpairseq(vp,overall_coeff);
229 int expairseq::compare_same_type(basic const & other) const
231 ASSERT(is_of_type(other, expairseq));
232 expairseq const & o=static_cast<expairseq const &>(const_cast<basic &>(other));
236 // compare number of elements
237 if (seq.size() != o.seq.size()) {
238 return (seq.size()<o.seq.size()) ? -1 : 1;
241 // compare overall_coeff
242 cmpval=overall_coeff.compare(o.overall_coeff);
243 if (cmpval!=0) return cmpval;
245 //if (seq.size()==0) return 0; // empty expairseq's are equal
247 #ifdef EXPAIRSEQ_USE_HASHTAB
248 ASSERT(hashtabsize==o.hashtabsize);
249 if (hashtabsize==0) {
250 #endif // def EXPAIRSEQ_USE_HASHTAB
251 epvector::const_iterator cit1=seq.begin();
252 epvector::const_iterator cit2=o.seq.begin();
253 epvector::const_iterator last1=seq.end();
254 epvector::const_iterator last2=o.seq.end();
256 for (; (cit1!=last1)&&(cit2!=last2); ++cit1, ++cit2) {
257 cmpval=(*cit1).compare(*cit2);
258 if (cmpval!=0) return cmpval;
265 #ifdef EXPAIRSEQ_USE_HASHTAB
268 // compare number of elements in each hashtab entry
269 for (unsigned i=0; i<hashtabsize; ++i) {
270 unsigned cursize=hashtab[i].size();
271 if (cursize != o.hashtab[i].size()) {
272 return (cursize < o.hashtab[i].size()) ? -1 : 1;
276 // compare individual (sorted) hashtab entries
277 for (unsigned i=0; i<hashtabsize; ++i) {
278 unsigned sz=hashtab[i].size();
280 epplist const & eppl1=hashtab[i];
281 epplist const & eppl2=o.hashtab[i];
282 epplist::const_iterator it1=eppl1.begin();
283 epplist::const_iterator it2=eppl2.begin();
284 while (it1!=eppl1.end()) {
285 cmpval=(*(*it1)).compare(*(*it2));
286 if (cmpval!=0) return cmpval;
294 #endif // def EXPAIRSEQ_USE_HASHTAB
297 bool expairseq::is_equal_same_type(basic const & other) const
299 expairseq const & o=dynamic_cast<expairseq const &>(const_cast<basic &>(other));
301 // compare number of elements
302 if (seq.size() != o.seq.size()) return false;
304 // compare overall_coeff
305 if (!overall_coeff.is_equal(o.overall_coeff)) return false;
307 #ifdef EXPAIRSEQ_USE_HASHTAB
308 // compare number of elements in each hashtab entry
309 if (hashtabsize!=o.hashtabsize) {
310 cout << "this:" << endl;
312 cout << "other:" << endl;
313 other.printtree(cout,0);
316 ASSERT(hashtabsize==o.hashtabsize);
318 if (hashtabsize==0) {
319 #endif // def EXPAIRSEQ_USE_HASHTAB
320 epvector::const_iterator cit1=seq.begin();
321 epvector::const_iterator cit2=o.seq.begin();
322 epvector::const_iterator last1=seq.end();
324 while (cit1!=last1) {
325 if (!(*cit1).is_equal(*cit2)) return false;
331 #ifdef EXPAIRSEQ_USE_HASHTAB
334 for (unsigned i=0; i<hashtabsize; ++i) {
335 if (hashtab[i].size() != o.hashtab[i].size()) return false;
338 // compare individual sorted hashtab entries
339 for (unsigned i=0; i<hashtabsize; ++i) {
340 unsigned sz=hashtab[i].size();
342 epplist const & eppl1=hashtab[i];
343 epplist const & eppl2=o.hashtab[i];
344 epplist::const_iterator it1=eppl1.begin();
345 epplist::const_iterator it2=eppl2.begin();
346 while (it1!=eppl1.end()) {
347 if (!(*(*it1)).is_equal(*(*it2))) return false;
355 #endif // def EXPAIRSEQ_USE_HASHTAB
358 unsigned expairseq::return_type(void) const
360 return return_types::noncommutative_composite;
363 unsigned expairseq::calchash(void) const
365 unsigned v=golden_ratio_hash(tinfo());
366 epvector::const_iterator last=seq.end();
367 for (epvector::const_iterator cit=seq.begin(); cit!=last; ++cit) {
368 #ifndef EXPAIRSEQ_USE_HASHTAB
369 v=rotate_left_31(v); // rotation would spoil commutativity
370 #endif // ndef EXPAIRSEQ_USE_HASHTAB
371 v ^= (*cit).rest.gethash();
374 v ^= overall_coeff.gethash();
377 // store calculated hash value only if object is already evaluated
378 if (flags & status_flags::evaluated) {
379 setflag(status_flags::hash_calculated);
386 ex expairseq::expand(unsigned options) const
388 epvector * vp=expandchildren(options);
392 return thisexpairseq(vp,overall_coeff);
396 // new virtual functions which can be overridden by derived classes
401 ex expairseq::thisexpairseq(epvector const & v,ex const & oc) const
403 return expairseq(v,oc);
406 ex expairseq::thisexpairseq(epvector * vp, ex const & oc) const
408 return expairseq(vp,oc);
411 expair expairseq::split_ex_to_pair(ex const & e) const
413 return expair(e,exONE());
416 expair expairseq::combine_ex_with_coeff_to_pair(ex const & e,
419 ASSERT(is_ex_exactly_of_type(c,numeric));
424 expair expairseq::combine_pair_with_coeff_to_pair(expair const & p,
427 ASSERT(is_ex_exactly_of_type(p.coeff,numeric));
428 ASSERT(is_ex_exactly_of_type(c,numeric));
430 return expair(p.rest,ex_to_numeric(p.coeff).mul_dyn(ex_to_numeric(c)));
433 ex expairseq::recombine_pair_to_ex(expair const & p) const
435 return lst(p.rest,p.coeff);
438 bool expairseq::expair_needs_further_processing(epp it)
443 ex expairseq::default_overall_coeff(void) const
448 void expairseq::combine_overall_coeff(ex const & c)
450 ASSERT(is_ex_exactly_of_type(overall_coeff,numeric));
451 ASSERT(is_ex_exactly_of_type(c,numeric));
452 overall_coeff = ex_to_numeric(overall_coeff).add_dyn(ex_to_numeric(c));
455 void expairseq::combine_overall_coeff(ex const & c1, ex const & c2)
457 ASSERT(is_ex_exactly_of_type(overall_coeff,numeric));
458 ASSERT(is_ex_exactly_of_type(c1,numeric));
459 ASSERT(is_ex_exactly_of_type(c2,numeric));
460 overall_coeff = ex_to_numeric(overall_coeff).
461 add_dyn(ex_to_numeric(c1).mul(ex_to_numeric(c2)));
464 bool expairseq::can_make_flat(expair const & p) const
471 // non-virtual functions in this class
474 void expairseq::construct_from_2_ex_via_exvector(ex const & lh, ex const & rh)
480 construct_from_exvector(v);
481 #ifdef EXPAIRSEQ_USE_HASHTAB
482 ASSERT((hashtabsize==0)||(hashtabsize>=minhashtabsize));
483 ASSERT(hashtabsize==calc_hashtabsize(seq.size()));
484 #endif // def EXPAIRSEQ_USE_HASHTAB
487 void expairseq::construct_from_2_ex(ex const & lh, ex const & rh)
489 if (lh.bp->tinfo()==tinfo()) {
490 if (rh.bp->tinfo()==tinfo()) {
491 #ifdef EXPAIRSEQ_USE_HASHTAB
492 unsigned totalsize=ex_to_expairseq(lh).seq.size()+
493 ex_to_expairseq(rh).seq.size();
494 if (calc_hashtabsize(totalsize)!=0) {
495 construct_from_2_ex_via_exvector(lh,rh);
497 #endif // def EXPAIRSEQ_USE_HASHTAB
498 construct_from_2_expairseq(ex_to_expairseq(lh),
499 ex_to_expairseq(rh));
500 #ifdef EXPAIRSEQ_USE_HASHTAB
502 #endif // def EXPAIRSEQ_USE_HASHTAB
505 #ifdef EXPAIRSEQ_USE_HASHTAB
506 unsigned totalsize=ex_to_expairseq(lh).seq.size()+1;
507 if (calc_hashtabsize(totalsize)!=0) {
508 construct_from_2_ex_via_exvector(lh,rh);
510 #endif // def EXPAIRSEQ_USE_HASHTAB
511 construct_from_expairseq_ex(ex_to_expairseq(lh),rh);
512 #ifdef EXPAIRSEQ_USE_HASHTAB
514 #endif // def EXPAIRSEQ_USE_HASHTAB
517 } else if (rh.bp->tinfo()==tinfo()) {
518 #ifdef EXPAIRSEQ_USE_HASHTAB
519 unsigned totalsize=ex_to_expairseq(rh).seq.size()+1;
520 if (calc_hashtabsize(totalsize)!=0) {
521 construct_from_2_ex_via_exvector(lh,rh);
523 #endif // def EXPAIRSEQ_USE_HASHTAB
524 construct_from_expairseq_ex(ex_to_expairseq(rh),lh);
525 #ifdef EXPAIRSEQ_USE_HASHTAB
527 #endif // def EXPAIRSEQ_USE_HASHTAB
531 #ifdef EXPAIRSEQ_USE_HASHTAB
532 if (calc_hashtabsize(2)!=0) {
533 construct_from_2_ex_via_exvector(lh,rh);
537 #endif // def EXPAIRSEQ_USE_HASHTAB
539 if (is_ex_exactly_of_type(lh,numeric)) {
540 if (is_ex_exactly_of_type(rh,numeric)) {
541 combine_overall_coeff(lh);
542 combine_overall_coeff(rh);
544 combine_overall_coeff(lh);
545 seq.push_back(split_ex_to_pair(rh));
548 if (is_ex_exactly_of_type(rh,numeric)) {
549 combine_overall_coeff(rh);
550 seq.push_back(split_ex_to_pair(lh));
552 expair p1=split_ex_to_pair(lh);
553 expair p2=split_ex_to_pair(rh);
555 int cmpval=p1.rest.compare(p2.rest);
557 p1.coeff=ex_to_numeric(p1.coeff).add_dyn(ex_to_numeric(p2.coeff));
558 if (!ex_to_numeric(p1.coeff).is_zero()) {
559 // no further processing is necessary, since this
560 // one element will usually be recombined in eval()
577 void expairseq::construct_from_2_expairseq(expairseq const & s1,
578 expairseq const & s2)
580 combine_overall_coeff(s1.overall_coeff);
581 combine_overall_coeff(s2.overall_coeff);
583 epvector::const_iterator first1=s1.seq.begin();
584 epvector::const_iterator last1=s1.seq.end();
585 epvector::const_iterator first2=s2.seq.begin();
586 epvector::const_iterator last2=s2.seq.end();
588 seq.reserve(s1.seq.size()+s2.seq.size());
590 bool needs_further_processing=false;
592 while (first1!=last1 && first2!=last2) {
593 int cmpval=(*first1).rest.compare((*first2).rest);
596 numeric const & newcoeff=ex_to_numeric((*first1).coeff).
597 add(ex_to_numeric((*first2).coeff));
598 if (!newcoeff.is_zero()) {
599 seq.push_back(expair((*first1).rest,newcoeff));
600 if (expair_needs_further_processing(seq.end()-1)) {
601 needs_further_processing = true;
606 } else if (cmpval<0) {
607 seq.push_back(*first1);
610 seq.push_back(*first2);
615 while (first1!=last1) {
616 seq.push_back(*first1);
619 while (first2!=last2) {
620 seq.push_back(*first2);
624 if (needs_further_processing) {
627 construct_from_epvector(v);
631 void expairseq::construct_from_expairseq_ex(expairseq const & s,
634 combine_overall_coeff(s.overall_coeff);
635 if (is_ex_exactly_of_type(e,numeric)) {
636 combine_overall_coeff(e);
641 epvector::const_iterator first=s.seq.begin();
642 epvector::const_iterator last=s.seq.end();
643 expair p=split_ex_to_pair(e);
645 seq.reserve(s.seq.size()+1);
648 bool needs_further_processing=false;
650 // merge p into s.seq
651 while (first!=last) {
652 int cmpval=(*first).rest.compare(p.rest);
655 numeric const & newcoeff=ex_to_numeric((*first).coeff).
656 add(ex_to_numeric(p.coeff));
657 if (!newcoeff.is_zero()) {
658 seq.push_back(expair((*first).rest,newcoeff));
659 if (expair_needs_further_processing(seq.end()-1)) {
660 needs_further_processing = true;
666 } else if (cmpval<0) {
667 seq.push_back(*first);
677 // while loop exited because p was pushed, now push rest of s.seq
678 while (first!=last) {
679 seq.push_back(*first);
683 // while loop exited because s.seq was pushed, now push p
687 if (needs_further_processing) {
690 construct_from_epvector(v);
694 void expairseq::construct_from_exvector(exvector const & v)
696 // simplifications: +(a,+(b,c),d) -> +(a,b,c,d) (associativity)
697 // +(d,b,c,a) -> +(a,b,c,d) (canonicalization)
698 // +(...,x,*(x,c1),*(x,c2)) -> +(...,*(x,1+c1+c2)) (c1, c2 numeric())
699 // (same for (+,*) -> (*,^)
702 #ifdef EXPAIRSEQ_USE_HASHTAB
703 combine_same_terms();
706 combine_same_terms_sorted_seq();
707 #endif // def EXPAIRSEQ_USE_HASHTAB
710 void expairseq::construct_from_epvector(epvector const & v)
712 // simplifications: +(a,+(b,c),d) -> +(a,b,c,d) (associativity)
713 // +(d,b,c,a) -> +(a,b,c,d) (canonicalization)
714 // +(...,x,*(x,c1),*(x,c2)) -> +(...,*(x,1+c1+c2)) (c1, c2 numeric())
715 // (same for (+,*) -> (*,^)
718 #ifdef EXPAIRSEQ_USE_HASHTAB
719 combine_same_terms();
722 combine_same_terms_sorted_seq();
723 #endif // def EXPAIRSEQ_USE_HASHTAB
728 void expairseq::make_flat(exvector const & v)
730 exvector::const_iterator cit, citend = v.end();
732 // count number of operands which are of same expairseq derived type
733 // and their cumulative number of operands
737 while (cit!=citend) {
738 if (cit->bp->tinfo()==tinfo()) {
740 noperands+=ex_to_expairseq(*cit).seq.size();
745 // reserve seq and coeffseq which will hold all operands
746 seq.reserve(v.size()+noperands-nexpairseqs);
748 // copy elements and split off numerical part
750 while (cit!=citend) {
751 if (cit->bp->tinfo()==tinfo()) {
752 expairseq const & subseqref=ex_to_expairseq(*cit);
753 combine_overall_coeff(subseqref.overall_coeff);
754 epvector::const_iterator cit_s=subseqref.seq.begin();
755 while (cit_s!=subseqref.seq.end()) {
756 seq.push_back(*cit_s);
760 if (is_ex_exactly_of_type(*cit,numeric)) {
761 combine_overall_coeff(*cit);
763 seq.push_back(split_ex_to_pair(*cit));
770 cout << "after make flat" << endl;
771 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
772 (*cit).printraw(cout);
779 void expairseq::make_flat(epvector const & v)
781 epvector::const_iterator cit, citend = v.end();
783 // count number of operands which are of same expairseq derived type
784 // and their cumulative number of operands
788 while (cit!=citend) {
789 if (cit->rest.bp->tinfo()==tinfo()) {
791 noperands+=ex_to_expairseq((*cit).rest).seq.size();
796 // reserve seq and coeffseq which will hold all operands
797 seq.reserve(v.size()+noperands-nexpairseqs);
799 // copy elements and split off numerical part
801 while (cit!=citend) {
802 if ((cit->rest.bp->tinfo()==tinfo())&&can_make_flat(*cit)) {
803 expairseq const & subseqref=ex_to_expairseq((*cit).rest);
804 combine_overall_coeff(ex_to_numeric(subseqref.overall_coeff),
805 ex_to_numeric((*cit).coeff));
806 epvector::const_iterator cit_s=subseqref.seq.begin();
807 while (cit_s!=subseqref.seq.end()) {
808 seq.push_back(expair((*cit_s).rest,
809 ex_to_numeric((*cit_s).coeff).mul_dyn(ex_to_numeric((*cit).coeff))));
810 //seq.push_back(combine_pair_with_coeff_to_pair(*cit_s,
815 if ((*cit).is_numeric_with_coeff_1()) {
816 combine_overall_coeff((*cit).rest);
817 //if (is_ex_exactly_of_type((*cit).rest,numeric)) {
818 // combine_overall_coeff(recombine_pair_to_ex(*cit));
827 epvector * expairseq::bubblesort(epvector::iterator itbegin, epvector::iterator itend)
829 unsigned n=itend-itbegin;
831 epvector * sp=new epvector;
834 epvector::iterator last=itend-1;
835 for (epvector::iterator it1=itbegin; it1!=last; ++it1) {
836 for (epvector::iterator it2=it1+1; it2!=itend; ++it2) {
837 if ((*it2).rest.compare((*it1).rest)<0) {
843 sp->push_back(*last);
847 epvector * expairseq::mergesort(epvector::iterator itbegin, epvector::iterator itend)
849 unsigned n=itend-itbegin;
852 epvector * sp=new epvector;
853 sp->push_back(*itbegin);
857 if (n<16) return bubblesort(itbegin, itend);
860 epvector * s1p=mergesort(itbegin, itbegin+m);
861 epvector * s2p=mergesort(itbegin+m, itend);
863 epvector * sp=new epvector;
864 sp->reserve(s1p->size()+s2p->size());
866 epvector::iterator first1=s1p->begin();
867 epvector::iterator last1=s1p->end();
869 epvector::iterator first2=s2p->begin();
870 epvector::iterator last2=s2p->end();
872 while (first1 != last1 && first2 != last2) {
873 if ((*first1).rest.compare((*first2).rest)<0) {
874 sp->push_back(*first1);
877 sp->push_back(*first2);
882 if (first1 != last1) {
883 while (first1 != last1) {
884 sp->push_back(*first1);
888 while (first2 != last2) {
889 sp->push_back(*first2);
901 void expairseq::canonicalize(void)
904 sort(seq.begin(),seq.end(),expair_is_less());
906 sort(seq.begin(),seq.end(),expair_is_less_old());
908 if (is_ex_exactly_of_type((*(seq.begin())).rest,numeric)) {
909 sort(seq.begin(),seq.end(),expair_is_less());
911 epvector::iterator last_numeric=seq.end();
914 } while (is_ex_exactly_of_type((*last_numeric).rest,numeric));
916 sort(last_numeric,seq.end(),expair_is_less());
922 epvector * sorted_seqp=mergesort(seq.begin(),seq.end());
923 epvector::iterator last=sorted_seqp->end();
924 epvector::iterator it2=seq.begin();
925 for (epvector::iterator it1=sorted_seqp->begin(); it1!=last; ++it1, ++it2) {
932 cout << "after canonicalize" << endl;
933 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
934 (*cit).printraw(cout);
941 void expairseq::combine_same_terms_sorted_seq(void)
943 bool needs_further_processing=false;
945 // combine same terms, drop term with coeff 0
947 epvector::iterator itin1=seq.begin();
948 epvector::iterator itin2=itin1+1;
949 epvector::iterator itout=itin1;
950 epvector::iterator last=seq.end();
951 // must_copy will be set to true the first time some combination is possible
952 // from then on the sequence has changed and must be compacted
953 bool must_copy=false;
954 while (itin2!=last) {
955 if ((*itin1).rest.compare((*itin2).rest)==0) {
956 (*itin1).coeff=ex_to_numeric((*itin1).coeff).
957 add_dyn(ex_to_numeric((*itin2).coeff));
958 if (expair_needs_further_processing(itin1)) {
959 needs_further_processing = true;
963 if (!ex_to_numeric((*itin1).coeff).is_zero()) {
973 if (!ex_to_numeric((*itin1).coeff).is_zero()) {
980 seq.erase(itout,last);
985 cout << "after combine" << endl;
986 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
987 (*cit).printraw(cout);
993 if (needs_further_processing) {
996 construct_from_epvector(v);
1000 #ifdef EXPAIRSEQ_USE_HASHTAB
1002 unsigned expairseq::calc_hashtabsize(unsigned sz) const
1005 unsigned nearest_power_of_2 = 1 << log2(sz);
1006 // if (nearest_power_of_2 < maxhashtabsize/hashtabfactor) {
1007 // size=nearest_power_of_2*hashtabfactor;
1008 size=nearest_power_of_2/hashtabfactor;
1009 if (size<minhashtabsize) return 0;
1010 ASSERT(hashtabsize<=0x8000000U); // really max size due to 31 bit hashing
1011 // hashtabsize must be a power of 2
1012 ASSERT((1U << log2(size))==size);
1016 unsigned expairseq::calc_hashindex(ex const & e) const
1018 // calculate hashindex
1019 unsigned hash=e.gethash();
1021 if (is_a_numeric_hash(hash)) {
1024 hashindex=hash & hashmask;
1025 // last hashtab entry is reserved for numerics
1026 if (hashindex==hashmask) hashindex=0;
1028 ASSERT(hashindex>=0);
1029 ASSERT((hashindex<hashtabsize)||(hashtabsize==0));
1033 void expairseq::shrink_hashtab(void)
1035 unsigned new_hashtabsize;
1036 while (hashtabsize!=(new_hashtabsize=calc_hashtabsize(seq.size()))) {
1037 ASSERT(new_hashtabsize<hashtabsize);
1038 if (new_hashtabsize==0) {
1045 // shrink by a factor of 2
1046 unsigned half_hashtabsize=hashtabsize/2;
1047 for (unsigned i=0; i<half_hashtabsize-1; ++i) {
1048 hashtab[i].merge(hashtab[i+half_hashtabsize],epp_is_less());
1050 // special treatment for numeric hashes
1051 hashtab[0].merge(hashtab[half_hashtabsize-1],epp_is_less());
1052 hashtab[half_hashtabsize-1]=hashtab[hashtabsize-1];
1053 hashtab.resize(half_hashtabsize);
1054 hashtabsize=half_hashtabsize;
1055 hashmask=hashtabsize-1;
1059 void expairseq::remove_hashtab_entry(epvector::const_iterator element)
1061 if (hashtabsize==0) return; // nothing to do
1063 // calculate hashindex of element to be deleted
1064 unsigned hashindex=calc_hashindex((*element).rest);
1066 // find it in hashtab and remove it
1067 epplist & eppl=hashtab[hashindex];
1068 epplist::iterator epplit=eppl.begin();
1070 while (epplit!=eppl.end()) {
1071 if (*epplit == element) {
1080 cout << "tried to erase " << element-seq.begin() << endl;
1081 cout << "size " << seq.end()-seq.begin() << endl;
1083 unsigned hashindex=calc_hashindex((*element).rest);
1084 epplist & eppl=hashtab[hashindex];
1085 epplist::iterator epplit=eppl.begin();
1087 while (epplit!=eppl.end()) {
1088 if (*epplit == element) {
1100 void expairseq::move_hashtab_entry(epvector::const_iterator oldpos,
1101 epvector::iterator newpos)
1103 ASSERT(hashtabsize!=0);
1105 // calculate hashindex of element which was moved
1106 unsigned hashindex=calc_hashindex((*newpos).rest);
1108 // find it in hashtab and modify it
1109 epplist & eppl=hashtab[hashindex];
1110 epplist::iterator epplit=eppl.begin();
1111 while (epplit!=eppl.end()) {
1112 if (*epplit == oldpos) {
1118 ASSERT(epplit!=eppl.end());
1121 void expairseq::sorted_insert(epplist & eppl, epp elem)
1123 epplist::iterator current=eppl.begin();
1124 while ((current!=eppl.end())&&((*(*current)).is_less(*elem))) {
1127 eppl.insert(current,elem);
1130 void expairseq::build_hashtab_and_combine(epvector::iterator & first_numeric,
1131 epvector::iterator & last_non_zero,
1132 vector<bool> & touched,
1133 unsigned & number_of_zeroes)
1135 epp current=seq.begin();
1137 while (current!=first_numeric) {
1138 if (is_ex_exactly_of_type((*current).rest,numeric)) {
1140 iter_swap(current,first_numeric);
1142 // calculate hashindex
1143 unsigned currenthashindex=calc_hashindex((*current).rest);
1145 // test if there is already a matching expair in the hashtab-list
1146 epplist & eppl=hashtab[currenthashindex];
1147 epplist::iterator epplit=eppl.begin();
1148 while (epplit!=eppl.end()) {
1149 if ((*current).rest.is_equal((*(*epplit)).rest)) break;
1152 if (epplit==eppl.end()) {
1153 // no matching expair found, append this to end of list
1154 sorted_insert(eppl,current);
1157 // epplit points to a matching expair, combine it with current
1158 (*(*epplit)).coeff=ex_to_numeric((*(*epplit)).coeff).
1159 add_dyn(ex_to_numeric((*current).coeff));
1161 // move obsolete current expair to end by swapping with last_non_zero element
1162 // if this was a numeric, it is swapped with the expair before first_numeric
1163 iter_swap(current,last_non_zero);
1165 if (first_numeric!=last_non_zero) iter_swap(first_numeric,current);
1168 // test if combined term has coeff 0 and can be removed is done later
1169 touched[(*epplit)-seq.begin()]=true;
1175 void expairseq::drop_coeff_0_terms(epvector::iterator & first_numeric,
1176 epvector::iterator & last_non_zero,
1177 vector<bool> & touched,
1178 unsigned & number_of_zeroes)
1180 // move terms with coeff 0 to end and remove them from hashtab
1181 // check only those elements which have been touched
1182 epp current=seq.begin();
1184 while (current!=first_numeric) {
1188 } else if (!ex_to_numeric((*current).coeff).is_equal(numZERO())) {
1192 remove_hashtab_entry(current);
1194 // move element to the end, unless it is already at the end
1195 if (current!=last_non_zero) {
1196 iter_swap(current,last_non_zero);
1198 bool numeric_swapped=first_numeric!=last_non_zero;
1199 if (numeric_swapped) iter_swap(first_numeric,current);
1200 epvector::iterator changed_entry;
1202 if (numeric_swapped) {
1203 changed_entry=first_numeric;
1205 changed_entry=last_non_zero;
1211 if (first_numeric!=current) {
1213 // change entry in hashtab which referred to first_numeric or last_non_zero to current
1214 move_hashtab_entry(changed_entry,current);
1215 touched[current-seq.begin()]=touched[changed_entry-seq.begin()];
1224 ASSERT(i==current-seq.begin());
1227 bool expairseq::has_coeff_0(void) const
1229 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
1230 if ((*cit).coeff.is_equal(exZERO())) {
1237 void expairseq::add_numerics_to_hashtab(epvector::iterator first_numeric,
1238 epvector::const_iterator last_non_zero)
1240 if (first_numeric==seq.end()) return; // no numerics
1242 epvector::iterator current=first_numeric;
1243 epvector::const_iterator last=last_non_zero+1;
1244 while (current!=last) {
1245 sorted_insert(hashtab[hashmask],current);
1250 void expairseq::combine_same_terms(void)
1252 // combine same terms, drop term with coeff 0, move numerics to end
1254 // calculate size of hashtab
1255 hashtabsize=calc_hashtabsize(seq.size());
1257 // hashtabsize is a power of 2
1258 hashmask=hashtabsize-1;
1262 hashtab.resize(hashtabsize);
1264 if (hashtabsize==0) {
1266 combine_same_terms_sorted_seq();
1267 ASSERT(!has_coeff_0());
1271 // iterate through seq, move numerics to end,
1272 // fill hashtab and combine same terms
1273 epvector::iterator first_numeric=seq.end();
1274 epvector::iterator last_non_zero=seq.end()-1;
1276 vector<bool> touched;
1277 touched.reserve(seq.size());
1278 for (unsigned i=0; i<seq.size(); ++i) touched[i]=false;
1280 unsigned number_of_zeroes=0;
1282 ASSERT(!has_coeff_0());
1283 build_hashtab_and_combine(first_numeric,last_non_zero,touched,number_of_zeroes);
1285 cout << "in combine:" << endl;
1287 cout << "size=" << seq.end() - seq.begin() << endl;
1288 cout << "first_numeric=" << first_numeric - seq.begin() << endl;
1289 cout << "last_non_zero=" << last_non_zero - seq.begin() << endl;
1290 for (unsigned i=0; i<seq.size(); ++i) {
1291 if (touched[i]) cout << i << " is touched" << endl;
1293 cout << "end in combine" << endl;
1296 // there should not be any terms with coeff 0 from the beginning,
1297 // so it should be safe to skip this step
1298 if (number_of_zeroes!=0) {
1299 drop_coeff_0_terms(first_numeric,last_non_zero,touched,number_of_zeroes);
1301 cout << "in combine after drop:" << endl;
1303 cout << "size=" << seq.end() - seq.begin() << endl;
1304 cout << "first_numeric=" << first_numeric - seq.begin() << endl;
1305 cout << "last_non_zero=" << last_non_zero - seq.begin() << endl;
1306 for (unsigned i=0; i<seq.size(); ++i) {
1307 if (touched[i]) cout << i << " is touched" << endl;
1309 cout << "end in combine after drop" << endl;
1313 add_numerics_to_hashtab(first_numeric,last_non_zero);
1315 // pop zero elements
1316 for (unsigned i=0; i<number_of_zeroes; ++i) {
1320 // shrink hashtabsize to calculated value
1321 ASSERT(!has_coeff_0());
1325 ASSERT(!has_coeff_0());
1328 #endif // def EXPAIRSEQ_USE_HASHTAB
1330 bool expairseq::is_canonical() const
1332 if (seq.size()<=1) return 1;
1334 #ifdef EXPAIRSEQ_USE_HASHTAB
1335 if (hashtabsize>0) return 1; // not canoncalized
1336 #endif // def EXPAIRSEQ_USE_HASHTAB
1338 epvector::const_iterator it=seq.begin();
1339 epvector::const_iterator it_last=it;
1340 for (++it; it!=seq.end(); it_last=it, ++it) {
1341 if (!((*it_last).is_less(*it)||(*it_last).is_equal(*it))) {
1342 if (!is_ex_exactly_of_type((*it_last).rest,numeric)||
1343 !is_ex_exactly_of_type((*it).rest,numeric)) {
1344 // double test makes it easier to set a breakpoint...
1345 if (!is_ex_exactly_of_type((*it_last).rest,numeric)||
1346 !is_ex_exactly_of_type((*it).rest,numeric)) {
1347 printpair(cout,*it_last,0);
1349 printpair(cout,*it,0);
1351 cout << "pair1:" << endl;
1352 (*it_last).rest.printtree(cout);
1353 (*it_last).coeff.printtree(cout);
1354 cout << "pair2:" << endl;
1355 (*it).rest.printtree(cout);
1356 (*it).coeff.printtree(cout);
1365 epvector * expairseq::expandchildren(unsigned options) const
1367 epvector::const_iterator last=seq.end();
1368 epvector::const_iterator cit=seq.begin();
1370 ex const & expanded_ex=(*cit).rest.expand(options);
1371 if (!are_ex_trivially_equal((*cit).rest,expanded_ex)) {
1373 // something changed, copy seq, eval and return it
1374 epvector *s=new epvector;
1375 s->reserve(seq.size());
1377 // copy parts of seq which are known not to have changed
1378 epvector::const_iterator cit2=seq.begin();
1380 s->push_back(*cit2);
1383 // copy first changed element
1384 s->push_back(combine_ex_with_coeff_to_pair(expanded_ex,
1388 while (cit2!=last) {
1389 s->push_back(combine_ex_with_coeff_to_pair((*cit2).rest.expand(options),
1398 return 0; // nothing has changed
1401 epvector * expairseq::evalchildren(int level) const
1403 // returns a NULL pointer if nothing had to be evaluated
1404 // returns a pointer to a newly created epvector otherwise
1405 // (which has to be deleted somewhere else)
1410 if (level == -max_recursion_level) {
1411 throw(std::runtime_error("max recursion level reached"));
1415 epvector::const_iterator last=seq.end();
1416 epvector::const_iterator cit=seq.begin();
1418 ex const & evaled_ex=(*cit).rest.eval(level);
1419 if (!are_ex_trivially_equal((*cit).rest,evaled_ex)) {
1421 // something changed, copy seq, eval and return it
1422 epvector *s=new epvector;
1423 s->reserve(seq.size());
1425 // copy parts of seq which are known not to have changed
1426 epvector::const_iterator cit2=seq.begin();
1428 s->push_back(*cit2);
1431 // copy first changed element
1432 s->push_back(combine_ex_with_coeff_to_pair(evaled_ex,
1436 while (cit2!=last) {
1437 s->push_back(combine_ex_with_coeff_to_pair((*cit2).rest.eval(level),
1446 return 0; // nothing has changed
1449 epvector expairseq::evalfchildren(int level) const
1452 s.reserve(seq.size());
1457 if (level == -max_recursion_level) {
1458 throw(std::runtime_error("max recursion level reached"));
1461 for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
1462 s.push_back(combine_ex_with_coeff_to_pair((*it).rest.evalf(level),
1468 epvector expairseq::normalchildren(int level) const
1471 s.reserve(seq.size());
1476 if (level == -max_recursion_level) {
1477 throw(std::runtime_error("max recursion level reached"));
1480 for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
1481 s.push_back(combine_ex_with_coeff_to_pair((*it).rest.normal(level),
1487 epvector expairseq::diffchildren(symbol const & y) const
1490 s.reserve(seq.size());
1492 for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
1493 s.push_back(combine_ex_with_coeff_to_pair((*it).rest.diff(y),
1499 epvector * expairseq::subschildren(lst const & ls, lst const & lr) const
1501 // returns a NULL pointer if nothing had to be substituted
1502 // returns a pointer to a newly created epvector otherwise
1503 // (which has to be deleted somewhere else)
1505 epvector::const_iterator last=seq.end();
1506 epvector::const_iterator cit=seq.begin();
1508 ex const & subsed_ex=(*cit).rest.subs(ls,lr);
1509 if (!are_ex_trivially_equal((*cit).rest,subsed_ex)) {
1511 // something changed, copy seq, subs and return it
1512 epvector *s=new epvector;
1513 s->reserve(seq.size());
1515 // copy parts of seq which are known not to have changed
1516 epvector::const_iterator cit2=seq.begin();
1518 s->push_back(*cit2);
1521 // copy first changed element
1522 s->push_back(combine_ex_with_coeff_to_pair(subsed_ex,
1526 while (cit2!=last) {
1527 s->push_back(combine_ex_with_coeff_to_pair((*cit2).rest.subs(ls,lr),
1536 return 0; // nothing has changed
1540 epvector expairseq::subschildren(lst const & ls, lst const & lr) const
1543 s.reserve(seq.size());
1545 for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
1546 s.push_back(split_ex_to_pair((*it).rest.subs(ls,lr),(*it).coeff));
1553 void expairseq::sort(epviter first, epviter last, expair_is_less comp)
1555 if (first != last) {
1556 introsort_loop(first, last, lg(last - first) * 2, comp);
1557 __final_insertion_sort(first, last, comp);
1561 ptrdiff_t expairseq::lg(ptrdiff_t n)
1564 for (k = 0; n > 1; n >>= 1) ++k;
1568 void expairseq::introsort_loop(epviter first, epviter last,
1569 ptrdiff_t depth_limit, expair_is_less comp)
1571 while (last - first > stl_threshold) {
1572 if (depth_limit == 0) {
1573 partial_sort(first, last, last, comp);
1577 epviter cut = unguarded_partition(first, last,
1578 expair(__median(*first, *(first + (last - first)/2),
1579 *(last - 1), comp)), comp);
1580 introsort_loop(cut, last, depth_limit, comp);
1585 epviter expairseq::unguarded_partition(epviter first, epviter last,
1586 expair pivot, expair_is_less comp)
1589 while (comp(*first, pivot)) ++first;
1591 while (comp(pivot, *last)) --last;
1592 if (!(first < last)) return first;
1593 iter_swap(first, last);
1598 void expairseq::partial_sort(epviter first, epviter middle, epviter last,
1599 expair_is_less comp) {
1600 make_heap(first, middle, comp);
1601 for (RandomAccessIterator i = middle; i < last; ++i)
1602 if (comp(*i, *first))
1603 __pop_heap(first, middle, i, T(*i), comp, distance_type(first));
1604 sort_heap(first, middle, comp);
1609 // static member variables
1614 unsigned expairseq::precedence=10;
1616 #ifdef EXPAIRSEQ_USE_HASHTAB
1617 unsigned expairseq::maxhashtabsize=0x4000000U;
1618 unsigned expairseq::minhashtabsize=0x1000U;
1619 unsigned expairseq::hashtabfactor=1;
1620 #endif // def EXPAIRSEQ_USE_HASHTAB
1626 const expairseq some_expairseq;
1627 type_info const & typeid_expairseq=typeid(some_expairseq);
1629 } // namespace GiNaC