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"
34 #ifdef EXPAIRSEQ_USE_HASHTAB
35 #error "FIXME: expair_needs_further_processing not yet implemented for hashtabs, sorry. A.F."
36 #endif // def EXPAIRSEQ_USE_HASHTAB
45 bool operator()(epp const & lh, epp const & rh) const
47 return (*lh).is_less(*rh);
52 // default constructor, destructor, copy constructor assignment operator and helpers
57 expairseq::expairseq(expairseq const & other)
59 debugmsg("expairseq copy constructor",LOGLEVEL_CONSTRUCT);
63 expairseq const & expairseq::operator=(expairseq const & other)
65 debugmsg("expairseq operator=",LOGLEVEL_ASSIGNMENT);
75 void expairseq::copy(expairseq const & other)
79 overall_coeff=other.overall_coeff;
80 #ifdef EXPAIRSEQ_USE_HASHTAB
82 hashtabsize=other.hashtabsize;
84 hashmask=other.hashmask;
85 hashtab.resize(hashtabsize);
86 epvector::const_iterator osb=other.seq.begin();
87 for (unsigned i=0; i<hashtabsize; ++i) {
89 for (epplist::const_iterator cit=other.hashtab[i].begin();
90 cit!=other.hashtab[i].end(); ++cit) {
91 hashtab[i].push_back(seq.begin()+((*cit)-osb));
97 #endif // def EXPAIRSEQ_USE_HASHTAB
101 // other constructors
104 expairseq::expairseq(ex const & lh, ex const & rh) : basic(TINFO_expairseq)
106 debugmsg("expairseq constructor from ex,ex",LOGLEVEL_CONSTRUCT);
107 construct_from_2_ex(lh,rh);
108 GINAC_ASSERT(is_canonical());
111 expairseq::expairseq(exvector const & v) : basic(TINFO_expairseq)
113 debugmsg("expairseq constructor from exvector",LOGLEVEL_CONSTRUCT);
114 construct_from_exvector(v);
115 GINAC_ASSERT(is_canonical());
119 expairseq::expairseq(epvector const & v, bool do_not_canonicalize) :
120 basic(TINFO_expairseq)
122 debugmsg("expairseq constructor from epvector",LOGLEVEL_CONSTRUCT);
123 if (do_not_canonicalize) {
125 #ifdef EXPAIRSEQ_USE_HASHTAB
126 combine_same_terms(); // to build hashtab
127 #endif // def EXPAIRSEQ_USE_HASHTAB
129 construct_from_epvector(v);
131 GINAC_ASSERT(is_canonical());
135 expairseq::expairseq(epvector const & v, ex const & oc) :
136 basic(TINFO_expairseq), overall_coeff(oc)
138 debugmsg("expairseq constructor from epvector,ex",LOGLEVEL_CONSTRUCT);
139 construct_from_epvector(v);
140 GINAC_ASSERT(is_canonical());
143 expairseq::expairseq(epvector * vp, ex const & oc) :
144 basic(TINFO_expairseq), overall_coeff(oc)
146 debugmsg("expairseq constructor from epvector *,ex",LOGLEVEL_CONSTRUCT);
148 construct_from_epvector(*vp);
150 GINAC_ASSERT(is_canonical());
154 // functions overriding virtual functions from bases classes
159 basic * expairseq::duplicate() const
161 debugmsg("expairseq duplicate",LOGLEVEL_DUPLICATE);
162 return new expairseq(*this);
165 bool expairseq::info(unsigned inf) const
167 return basic::info(inf);
170 int expairseq::nops() const
172 if (overall_coeff.is_equal(default_overall_coeff())) {
178 ex expairseq::op(int const i) const
180 if (unsigned(i)<seq.size()) {
181 return recombine_pair_to_ex(seq[i]);
183 GINAC_ASSERT(!overall_coeff.is_equal(default_overall_coeff()));
184 return overall_coeff;
187 ex & expairseq::let_op(int const i)
189 throw(std::logic_error("let_op not defined for expairseq and derived classes (add,mul,...)"));
192 ex expairseq::eval(int level) const
194 if ((level==1)&&(flags & status_flags::evaluated)) {
198 epvector * vp=evalchildren(level);
203 return (new expairseq(vp,overall_coeff))
204 ->setflag(status_flags::dynallocated |
205 status_flags::evaluated );
208 ex expairseq::evalf(int level) const
210 return thisexpairseq(evalfchildren(level),overall_coeff);
213 ex expairseq::normal(lst &sym_lst, lst &repl_lst, int level) const
215 ex n=thisexpairseq(normalchildren(level),overall_coeff);
216 return n.bp->basic::normal(sym_lst,repl_lst,level);
219 ex expairseq::subs(lst const & ls, lst const & lr) const
221 epvector * vp=subschildren(ls,lr);
225 return thisexpairseq(vp,overall_coeff);
230 int expairseq::compare_same_type(basic const & other) const
232 GINAC_ASSERT(is_of_type(other, expairseq));
233 expairseq const & o=static_cast<expairseq const &>(const_cast<basic &>(other));
237 // compare number of elements
238 if (seq.size() != o.seq.size()) {
239 return (seq.size()<o.seq.size()) ? -1 : 1;
242 // compare overall_coeff
243 cmpval=overall_coeff.compare(o.overall_coeff);
244 if (cmpval!=0) return cmpval;
246 //if (seq.size()==0) return 0; // empty expairseq's are equal
248 #ifdef EXPAIRSEQ_USE_HASHTAB
249 GINAC_ASSERT(hashtabsize==o.hashtabsize);
250 if (hashtabsize==0) {
251 #endif // def EXPAIRSEQ_USE_HASHTAB
252 epvector::const_iterator cit1=seq.begin();
253 epvector::const_iterator cit2=o.seq.begin();
254 epvector::const_iterator last1=seq.end();
255 epvector::const_iterator last2=o.seq.end();
257 for (; (cit1!=last1)&&(cit2!=last2); ++cit1, ++cit2) {
258 cmpval=(*cit1).compare(*cit2);
259 if (cmpval!=0) return cmpval;
262 GINAC_ASSERT(cit1==last1);
263 GINAC_ASSERT(cit2==last2);
266 #ifdef EXPAIRSEQ_USE_HASHTAB
269 // compare number of elements in each hashtab entry
270 for (unsigned i=0; i<hashtabsize; ++i) {
271 unsigned cursize=hashtab[i].size();
272 if (cursize != o.hashtab[i].size()) {
273 return (cursize < o.hashtab[i].size()) ? -1 : 1;
277 // compare individual (sorted) hashtab entries
278 for (unsigned i=0; i<hashtabsize; ++i) {
279 unsigned sz=hashtab[i].size();
281 epplist const & eppl1=hashtab[i];
282 epplist const & eppl2=o.hashtab[i];
283 epplist::const_iterator it1=eppl1.begin();
284 epplist::const_iterator it2=eppl2.begin();
285 while (it1!=eppl1.end()) {
286 cmpval=(*(*it1)).compare(*(*it2));
287 if (cmpval!=0) return cmpval;
295 #endif // def EXPAIRSEQ_USE_HASHTAB
298 bool expairseq::is_equal_same_type(basic const & other) const
300 expairseq const & o=dynamic_cast<expairseq const &>(const_cast<basic &>(other));
302 // compare number of elements
303 if (seq.size() != o.seq.size()) return false;
305 // compare overall_coeff
306 if (!overall_coeff.is_equal(o.overall_coeff)) return false;
308 #ifdef EXPAIRSEQ_USE_HASHTAB
309 // compare number of elements in each hashtab entry
310 if (hashtabsize!=o.hashtabsize) {
311 cout << "this:" << endl;
313 cout << "other:" << endl;
314 other.printtree(cout,0);
317 GINAC_ASSERT(hashtabsize==o.hashtabsize);
319 if (hashtabsize==0) {
320 #endif // def EXPAIRSEQ_USE_HASHTAB
321 epvector::const_iterator cit1=seq.begin();
322 epvector::const_iterator cit2=o.seq.begin();
323 epvector::const_iterator last1=seq.end();
325 while (cit1!=last1) {
326 if (!(*cit1).is_equal(*cit2)) return false;
332 #ifdef EXPAIRSEQ_USE_HASHTAB
335 for (unsigned i=0; i<hashtabsize; ++i) {
336 if (hashtab[i].size() != o.hashtab[i].size()) return false;
339 // compare individual sorted hashtab entries
340 for (unsigned i=0; i<hashtabsize; ++i) {
341 unsigned sz=hashtab[i].size();
343 epplist const & eppl1=hashtab[i];
344 epplist const & eppl2=o.hashtab[i];
345 epplist::const_iterator it1=eppl1.begin();
346 epplist::const_iterator it2=eppl2.begin();
347 while (it1!=eppl1.end()) {
348 if (!(*(*it1)).is_equal(*(*it2))) return false;
356 #endif // def EXPAIRSEQ_USE_HASHTAB
359 unsigned expairseq::return_type(void) const
361 return return_types::noncommutative_composite;
364 unsigned expairseq::calchash(void) const
366 unsigned v=golden_ratio_hash(tinfo());
367 epvector::const_iterator last=seq.end();
368 for (epvector::const_iterator cit=seq.begin(); cit!=last; ++cit) {
369 #ifndef EXPAIRSEQ_USE_HASHTAB
370 v=rotate_left_31(v); // rotation would spoil commutativity
371 #endif // ndef EXPAIRSEQ_USE_HASHTAB
372 v ^= (*cit).rest.gethash();
375 v ^= overall_coeff.gethash();
378 // store calculated hash value only if object is already evaluated
379 if (flags & status_flags::evaluated) {
380 setflag(status_flags::hash_calculated);
387 ex expairseq::expand(unsigned options) const
389 epvector * vp=expandchildren(options);
393 return thisexpairseq(vp,overall_coeff);
397 // new virtual functions which can be overridden by derived classes
402 ex expairseq::thisexpairseq(epvector const & v,ex const & oc) const
404 return expairseq(v,oc);
407 ex expairseq::thisexpairseq(epvector * vp, ex const & oc) const
409 return expairseq(vp,oc);
412 expair expairseq::split_ex_to_pair(ex const & e) const
414 return expair(e,exONE());
417 expair expairseq::combine_ex_with_coeff_to_pair(ex const & e,
420 GINAC_ASSERT(is_ex_exactly_of_type(c,numeric));
425 expair expairseq::combine_pair_with_coeff_to_pair(expair const & p,
428 GINAC_ASSERT(is_ex_exactly_of_type(p.coeff,numeric));
429 GINAC_ASSERT(is_ex_exactly_of_type(c,numeric));
431 return expair(p.rest,ex_to_numeric(p.coeff).mul_dyn(ex_to_numeric(c)));
434 ex expairseq::recombine_pair_to_ex(expair const & p) const
436 return lst(p.rest,p.coeff);
439 bool expairseq::expair_needs_further_processing(epp it)
444 ex expairseq::default_overall_coeff(void) const
449 void expairseq::combine_overall_coeff(ex const & c)
451 GINAC_ASSERT(is_ex_exactly_of_type(overall_coeff,numeric));
452 GINAC_ASSERT(is_ex_exactly_of_type(c,numeric));
453 overall_coeff = ex_to_numeric(overall_coeff).add_dyn(ex_to_numeric(c));
456 void expairseq::combine_overall_coeff(ex const & c1, ex const & c2)
458 GINAC_ASSERT(is_ex_exactly_of_type(overall_coeff,numeric));
459 GINAC_ASSERT(is_ex_exactly_of_type(c1,numeric));
460 GINAC_ASSERT(is_ex_exactly_of_type(c2,numeric));
461 overall_coeff = ex_to_numeric(overall_coeff).
462 add_dyn(ex_to_numeric(c1).mul(ex_to_numeric(c2)));
465 bool expairseq::can_make_flat(expair const & p) const
472 // non-virtual functions in this class
475 void expairseq::construct_from_2_ex_via_exvector(ex const & lh, ex const & rh)
481 construct_from_exvector(v);
482 #ifdef EXPAIRSEQ_USE_HASHTAB
483 GINAC_ASSERT((hashtabsize==0)||(hashtabsize>=minhashtabsize));
484 GINAC_ASSERT(hashtabsize==calc_hashtabsize(seq.size()));
485 #endif // def EXPAIRSEQ_USE_HASHTAB
488 void expairseq::construct_from_2_ex(ex const & lh, ex const & rh)
490 if (lh.bp->tinfo()==tinfo()) {
491 if (rh.bp->tinfo()==tinfo()) {
492 #ifdef EXPAIRSEQ_USE_HASHTAB
493 unsigned totalsize=ex_to_expairseq(lh).seq.size()+
494 ex_to_expairseq(rh).seq.size();
495 if (calc_hashtabsize(totalsize)!=0) {
496 construct_from_2_ex_via_exvector(lh,rh);
498 #endif // def EXPAIRSEQ_USE_HASHTAB
499 construct_from_2_expairseq(ex_to_expairseq(lh),
500 ex_to_expairseq(rh));
501 #ifdef EXPAIRSEQ_USE_HASHTAB
503 #endif // def EXPAIRSEQ_USE_HASHTAB
506 #ifdef EXPAIRSEQ_USE_HASHTAB
507 unsigned totalsize=ex_to_expairseq(lh).seq.size()+1;
508 if (calc_hashtabsize(totalsize)!=0) {
509 construct_from_2_ex_via_exvector(lh,rh);
511 #endif // def EXPAIRSEQ_USE_HASHTAB
512 construct_from_expairseq_ex(ex_to_expairseq(lh),rh);
513 #ifdef EXPAIRSEQ_USE_HASHTAB
515 #endif // def EXPAIRSEQ_USE_HASHTAB
518 } else if (rh.bp->tinfo()==tinfo()) {
519 #ifdef EXPAIRSEQ_USE_HASHTAB
520 unsigned totalsize=ex_to_expairseq(rh).seq.size()+1;
521 if (calc_hashtabsize(totalsize)!=0) {
522 construct_from_2_ex_via_exvector(lh,rh);
524 #endif // def EXPAIRSEQ_USE_HASHTAB
525 construct_from_expairseq_ex(ex_to_expairseq(rh),lh);
526 #ifdef EXPAIRSEQ_USE_HASHTAB
528 #endif // def EXPAIRSEQ_USE_HASHTAB
532 #ifdef EXPAIRSEQ_USE_HASHTAB
533 if (calc_hashtabsize(2)!=0) {
534 construct_from_2_ex_via_exvector(lh,rh);
538 #endif // def EXPAIRSEQ_USE_HASHTAB
540 if (is_ex_exactly_of_type(lh,numeric)) {
541 if (is_ex_exactly_of_type(rh,numeric)) {
542 combine_overall_coeff(lh);
543 combine_overall_coeff(rh);
545 combine_overall_coeff(lh);
546 seq.push_back(split_ex_to_pair(rh));
549 if (is_ex_exactly_of_type(rh,numeric)) {
550 combine_overall_coeff(rh);
551 seq.push_back(split_ex_to_pair(lh));
553 expair p1=split_ex_to_pair(lh);
554 expair p2=split_ex_to_pair(rh);
556 int cmpval=p1.rest.compare(p2.rest);
558 p1.coeff=ex_to_numeric(p1.coeff).add_dyn(ex_to_numeric(p2.coeff));
559 if (!ex_to_numeric(p1.coeff).is_zero()) {
560 // no further processing is necessary, since this
561 // one element will usually be recombined in eval()
578 void expairseq::construct_from_2_expairseq(expairseq const & s1,
579 expairseq const & s2)
581 combine_overall_coeff(s1.overall_coeff);
582 combine_overall_coeff(s2.overall_coeff);
584 epvector::const_iterator first1=s1.seq.begin();
585 epvector::const_iterator last1=s1.seq.end();
586 epvector::const_iterator first2=s2.seq.begin();
587 epvector::const_iterator last2=s2.seq.end();
589 seq.reserve(s1.seq.size()+s2.seq.size());
591 bool needs_further_processing=false;
593 while (first1!=last1 && first2!=last2) {
594 int cmpval=(*first1).rest.compare((*first2).rest);
597 numeric const & newcoeff=ex_to_numeric((*first1).coeff).
598 add(ex_to_numeric((*first2).coeff));
599 if (!newcoeff.is_zero()) {
600 seq.push_back(expair((*first1).rest,newcoeff));
601 if (expair_needs_further_processing(seq.end()-1)) {
602 needs_further_processing = true;
607 } else if (cmpval<0) {
608 seq.push_back(*first1);
611 seq.push_back(*first2);
616 while (first1!=last1) {
617 seq.push_back(*first1);
620 while (first2!=last2) {
621 seq.push_back(*first2);
625 if (needs_further_processing) {
628 construct_from_epvector(v);
632 void expairseq::construct_from_expairseq_ex(expairseq const & s,
635 combine_overall_coeff(s.overall_coeff);
636 if (is_ex_exactly_of_type(e,numeric)) {
637 combine_overall_coeff(e);
642 epvector::const_iterator first=s.seq.begin();
643 epvector::const_iterator last=s.seq.end();
644 expair p=split_ex_to_pair(e);
646 seq.reserve(s.seq.size()+1);
649 bool needs_further_processing=false;
651 // merge p into s.seq
652 while (first!=last) {
653 int cmpval=(*first).rest.compare(p.rest);
656 numeric const & newcoeff=ex_to_numeric((*first).coeff).
657 add(ex_to_numeric(p.coeff));
658 if (!newcoeff.is_zero()) {
659 seq.push_back(expair((*first).rest,newcoeff));
660 if (expair_needs_further_processing(seq.end()-1)) {
661 needs_further_processing = true;
667 } else if (cmpval<0) {
668 seq.push_back(*first);
678 // while loop exited because p was pushed, now push rest of s.seq
679 while (first!=last) {
680 seq.push_back(*first);
684 // while loop exited because s.seq was pushed, now push p
688 if (needs_further_processing) {
691 construct_from_epvector(v);
695 void expairseq::construct_from_exvector(exvector const & v)
697 // simplifications: +(a,+(b,c),d) -> +(a,b,c,d) (associativity)
698 // +(d,b,c,a) -> +(a,b,c,d) (canonicalization)
699 // +(...,x,*(x,c1),*(x,c2)) -> +(...,*(x,1+c1+c2)) (c1, c2 numeric())
700 // (same for (+,*) -> (*,^)
703 #ifdef EXPAIRSEQ_USE_HASHTAB
704 combine_same_terms();
707 combine_same_terms_sorted_seq();
708 #endif // def EXPAIRSEQ_USE_HASHTAB
711 void expairseq::construct_from_epvector(epvector const & v)
713 // simplifications: +(a,+(b,c),d) -> +(a,b,c,d) (associativity)
714 // +(d,b,c,a) -> +(a,b,c,d) (canonicalization)
715 // +(...,x,*(x,c1),*(x,c2)) -> +(...,*(x,1+c1+c2)) (c1, c2 numeric())
716 // (same for (+,*) -> (*,^)
719 #ifdef EXPAIRSEQ_USE_HASHTAB
720 combine_same_terms();
723 combine_same_terms_sorted_seq();
724 #endif // def EXPAIRSEQ_USE_HASHTAB
729 void expairseq::make_flat(exvector const & v)
731 exvector::const_iterator cit, citend = v.end();
733 // count number of operands which are of same expairseq derived type
734 // and their cumulative number of operands
738 while (cit!=citend) {
739 if (cit->bp->tinfo()==tinfo()) {
741 noperands+=ex_to_expairseq(*cit).seq.size();
746 // reserve seq and coeffseq which will hold all operands
747 seq.reserve(v.size()+noperands-nexpairseqs);
749 // copy elements and split off numerical part
751 while (cit!=citend) {
752 if (cit->bp->tinfo()==tinfo()) {
753 expairseq const & subseqref=ex_to_expairseq(*cit);
754 combine_overall_coeff(subseqref.overall_coeff);
755 epvector::const_iterator cit_s=subseqref.seq.begin();
756 while (cit_s!=subseqref.seq.end()) {
757 seq.push_back(*cit_s);
761 if (is_ex_exactly_of_type(*cit,numeric)) {
762 combine_overall_coeff(*cit);
764 seq.push_back(split_ex_to_pair(*cit));
771 cout << "after make flat" << endl;
772 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
773 (*cit).printraw(cout);
780 void expairseq::make_flat(epvector const & v)
782 epvector::const_iterator cit, citend = v.end();
784 // count number of operands which are of same expairseq derived type
785 // and their cumulative number of operands
789 while (cit!=citend) {
790 if (cit->rest.bp->tinfo()==tinfo()) {
792 noperands+=ex_to_expairseq((*cit).rest).seq.size();
797 // reserve seq and coeffseq which will hold all operands
798 seq.reserve(v.size()+noperands-nexpairseqs);
800 // copy elements and split off numerical part
802 while (cit!=citend) {
803 if ((cit->rest.bp->tinfo()==tinfo())&&can_make_flat(*cit)) {
804 expairseq const & subseqref=ex_to_expairseq((*cit).rest);
805 combine_overall_coeff(ex_to_numeric(subseqref.overall_coeff),
806 ex_to_numeric((*cit).coeff));
807 epvector::const_iterator cit_s=subseqref.seq.begin();
808 while (cit_s!=subseqref.seq.end()) {
809 seq.push_back(expair((*cit_s).rest,
810 ex_to_numeric((*cit_s).coeff).mul_dyn(ex_to_numeric((*cit).coeff))));
811 //seq.push_back(combine_pair_with_coeff_to_pair(*cit_s,
816 if ((*cit).is_numeric_with_coeff_1()) {
817 combine_overall_coeff((*cit).rest);
818 //if (is_ex_exactly_of_type((*cit).rest,numeric)) {
819 // combine_overall_coeff(recombine_pair_to_ex(*cit));
828 epvector * expairseq::bubblesort(epvector::iterator itbegin, epvector::iterator itend)
830 unsigned n=itend-itbegin;
832 epvector * sp=new epvector;
835 epvector::iterator last=itend-1;
836 for (epvector::iterator it1=itbegin; it1!=last; ++it1) {
837 for (epvector::iterator it2=it1+1; it2!=itend; ++it2) {
838 if ((*it2).rest.compare((*it1).rest)<0) {
844 sp->push_back(*last);
848 epvector * expairseq::mergesort(epvector::iterator itbegin, epvector::iterator itend)
850 unsigned n=itend-itbegin;
853 epvector * sp=new epvector;
854 sp->push_back(*itbegin);
858 if (n<16) return bubblesort(itbegin, itend);
861 epvector * s1p=mergesort(itbegin, itbegin+m);
862 epvector * s2p=mergesort(itbegin+m, itend);
864 epvector * sp=new epvector;
865 sp->reserve(s1p->size()+s2p->size());
867 epvector::iterator first1=s1p->begin();
868 epvector::iterator last1=s1p->end();
870 epvector::iterator first2=s2p->begin();
871 epvector::iterator last2=s2p->end();
873 while (first1 != last1 && first2 != last2) {
874 if ((*first1).rest.compare((*first2).rest)<0) {
875 sp->push_back(*first1);
878 sp->push_back(*first2);
883 if (first1 != last1) {
884 while (first1 != last1) {
885 sp->push_back(*first1);
889 while (first2 != last2) {
890 sp->push_back(*first2);
902 void expairseq::canonicalize(void)
905 sort(seq.begin(),seq.end(),expair_is_less());
907 sort(seq.begin(),seq.end(),expair_is_less_old());
909 if (is_ex_exactly_of_type((*(seq.begin())).rest,numeric)) {
910 sort(seq.begin(),seq.end(),expair_is_less());
912 epvector::iterator last_numeric=seq.end();
915 } while (is_ex_exactly_of_type((*last_numeric).rest,numeric));
917 sort(last_numeric,seq.end(),expair_is_less());
923 epvector * sorted_seqp=mergesort(seq.begin(),seq.end());
924 epvector::iterator last=sorted_seqp->end();
925 epvector::iterator it2=seq.begin();
926 for (epvector::iterator it1=sorted_seqp->begin(); it1!=last; ++it1, ++it2) {
933 cout << "after canonicalize" << endl;
934 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
935 (*cit).printraw(cout);
942 void expairseq::combine_same_terms_sorted_seq(void)
944 bool needs_further_processing=false;
946 // combine same terms, drop term with coeff 0
948 epvector::iterator itin1=seq.begin();
949 epvector::iterator itin2=itin1+1;
950 epvector::iterator itout=itin1;
951 epvector::iterator last=seq.end();
952 // must_copy will be set to true the first time some combination is possible
953 // from then on the sequence has changed and must be compacted
954 bool must_copy=false;
955 while (itin2!=last) {
956 if ((*itin1).rest.compare((*itin2).rest)==0) {
957 (*itin1).coeff=ex_to_numeric((*itin1).coeff).
958 add_dyn(ex_to_numeric((*itin2).coeff));
959 if (expair_needs_further_processing(itin1)) {
960 needs_further_processing = true;
964 if (!ex_to_numeric((*itin1).coeff).is_zero()) {
974 if (!ex_to_numeric((*itin1).coeff).is_zero()) {
981 seq.erase(itout,last);
986 cout << "after combine" << endl;
987 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
988 (*cit).printraw(cout);
994 if (needs_further_processing) {
997 construct_from_epvector(v);
1001 #ifdef EXPAIRSEQ_USE_HASHTAB
1003 unsigned expairseq::calc_hashtabsize(unsigned sz) const
1006 unsigned nearest_power_of_2 = 1 << log2(sz);
1007 // if (nearest_power_of_2 < maxhashtabsize/hashtabfactor) {
1008 // size=nearest_power_of_2*hashtabfactor;
1009 size=nearest_power_of_2/hashtabfactor;
1010 if (size<minhashtabsize) return 0;
1011 GINAC_ASSERT(hashtabsize<=0x8000000U); // really max size due to 31 bit hashing
1012 // hashtabsize must be a power of 2
1013 GINAC_ASSERT((1U << log2(size))==size);
1017 unsigned expairseq::calc_hashindex(ex const & e) const
1019 // calculate hashindex
1020 unsigned hash=e.gethash();
1022 if (is_a_numeric_hash(hash)) {
1025 hashindex=hash & hashmask;
1026 // last hashtab entry is reserved for numerics
1027 if (hashindex==hashmask) hashindex=0;
1029 GINAC_ASSERT(hashindex>=0);
1030 GINAC_ASSERT((hashindex<hashtabsize)||(hashtabsize==0));
1034 void expairseq::shrink_hashtab(void)
1036 unsigned new_hashtabsize;
1037 while (hashtabsize!=(new_hashtabsize=calc_hashtabsize(seq.size()))) {
1038 GINAC_ASSERT(new_hashtabsize<hashtabsize);
1039 if (new_hashtabsize==0) {
1046 // shrink by a factor of 2
1047 unsigned half_hashtabsize=hashtabsize/2;
1048 for (unsigned i=0; i<half_hashtabsize-1; ++i) {
1049 hashtab[i].merge(hashtab[i+half_hashtabsize],epp_is_less());
1051 // special treatment for numeric hashes
1052 hashtab[0].merge(hashtab[half_hashtabsize-1],epp_is_less());
1053 hashtab[half_hashtabsize-1]=hashtab[hashtabsize-1];
1054 hashtab.resize(half_hashtabsize);
1055 hashtabsize=half_hashtabsize;
1056 hashmask=hashtabsize-1;
1060 void expairseq::remove_hashtab_entry(epvector::const_iterator element)
1062 if (hashtabsize==0) return; // nothing to do
1064 // calculate hashindex of element to be deleted
1065 unsigned hashindex=calc_hashindex((*element).rest);
1067 // find it in hashtab and remove it
1068 epplist & eppl=hashtab[hashindex];
1069 epplist::iterator epplit=eppl.begin();
1071 while (epplit!=eppl.end()) {
1072 if (*epplit == element) {
1081 cout << "tried to erase " << element-seq.begin() << endl;
1082 cout << "size " << seq.end()-seq.begin() << endl;
1084 unsigned hashindex=calc_hashindex((*element).rest);
1085 epplist & eppl=hashtab[hashindex];
1086 epplist::iterator epplit=eppl.begin();
1088 while (epplit!=eppl.end()) {
1089 if (*epplit == element) {
1096 GINAC_ASSERT(erased);
1098 GINAC_ASSERT(erased);
1101 void expairseq::move_hashtab_entry(epvector::const_iterator oldpos,
1102 epvector::iterator newpos)
1104 GINAC_ASSERT(hashtabsize!=0);
1106 // calculate hashindex of element which was moved
1107 unsigned hashindex=calc_hashindex((*newpos).rest);
1109 // find it in hashtab and modify it
1110 epplist & eppl=hashtab[hashindex];
1111 epplist::iterator epplit=eppl.begin();
1112 while (epplit!=eppl.end()) {
1113 if (*epplit == oldpos) {
1119 GINAC_ASSERT(epplit!=eppl.end());
1122 void expairseq::sorted_insert(epplist & eppl, epp elem)
1124 epplist::iterator current=eppl.begin();
1125 while ((current!=eppl.end())&&((*(*current)).is_less(*elem))) {
1128 eppl.insert(current,elem);
1131 void expairseq::build_hashtab_and_combine(epvector::iterator & first_numeric,
1132 epvector::iterator & last_non_zero,
1133 vector<bool> & touched,
1134 unsigned & number_of_zeroes)
1136 epp current=seq.begin();
1138 while (current!=first_numeric) {
1139 if (is_ex_exactly_of_type((*current).rest,numeric)) {
1141 iter_swap(current,first_numeric);
1143 // calculate hashindex
1144 unsigned currenthashindex=calc_hashindex((*current).rest);
1146 // test if there is already a matching expair in the hashtab-list
1147 epplist & eppl=hashtab[currenthashindex];
1148 epplist::iterator epplit=eppl.begin();
1149 while (epplit!=eppl.end()) {
1150 if ((*current).rest.is_equal((*(*epplit)).rest)) break;
1153 if (epplit==eppl.end()) {
1154 // no matching expair found, append this to end of list
1155 sorted_insert(eppl,current);
1158 // epplit points to a matching expair, combine it with current
1159 (*(*epplit)).coeff=ex_to_numeric((*(*epplit)).coeff).
1160 add_dyn(ex_to_numeric((*current).coeff));
1162 // move obsolete current expair to end by swapping with last_non_zero element
1163 // if this was a numeric, it is swapped with the expair before first_numeric
1164 iter_swap(current,last_non_zero);
1166 if (first_numeric!=last_non_zero) iter_swap(first_numeric,current);
1169 // test if combined term has coeff 0 and can be removed is done later
1170 touched[(*epplit)-seq.begin()]=true;
1176 void expairseq::drop_coeff_0_terms(epvector::iterator & first_numeric,
1177 epvector::iterator & last_non_zero,
1178 vector<bool> & touched,
1179 unsigned & number_of_zeroes)
1181 // move terms with coeff 0 to end and remove them from hashtab
1182 // check only those elements which have been touched
1183 epp current=seq.begin();
1185 while (current!=first_numeric) {
1189 } else if (!ex_to_numeric((*current).coeff).is_equal(numZERO())) {
1193 remove_hashtab_entry(current);
1195 // move element to the end, unless it is already at the end
1196 if (current!=last_non_zero) {
1197 iter_swap(current,last_non_zero);
1199 bool numeric_swapped=first_numeric!=last_non_zero;
1200 if (numeric_swapped) iter_swap(first_numeric,current);
1201 epvector::iterator changed_entry;
1203 if (numeric_swapped) {
1204 changed_entry=first_numeric;
1206 changed_entry=last_non_zero;
1212 if (first_numeric!=current) {
1214 // change entry in hashtab which referred to first_numeric or last_non_zero to current
1215 move_hashtab_entry(changed_entry,current);
1216 touched[current-seq.begin()]=touched[changed_entry-seq.begin()];
1225 GINAC_ASSERT(i==current-seq.begin());
1228 bool expairseq::has_coeff_0(void) const
1230 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
1231 if ((*cit).coeff.is_equal(exZERO())) {
1238 void expairseq::add_numerics_to_hashtab(epvector::iterator first_numeric,
1239 epvector::const_iterator last_non_zero)
1241 if (first_numeric==seq.end()) return; // no numerics
1243 epvector::iterator current=first_numeric;
1244 epvector::const_iterator last=last_non_zero+1;
1245 while (current!=last) {
1246 sorted_insert(hashtab[hashmask],current);
1251 void expairseq::combine_same_terms(void)
1253 // combine same terms, drop term with coeff 0, move numerics to end
1255 // calculate size of hashtab
1256 hashtabsize=calc_hashtabsize(seq.size());
1258 // hashtabsize is a power of 2
1259 hashmask=hashtabsize-1;
1263 hashtab.resize(hashtabsize);
1265 if (hashtabsize==0) {
1267 combine_same_terms_sorted_seq();
1268 GINAC_ASSERT(!has_coeff_0());
1272 // iterate through seq, move numerics to end,
1273 // fill hashtab and combine same terms
1274 epvector::iterator first_numeric=seq.end();
1275 epvector::iterator last_non_zero=seq.end()-1;
1277 vector<bool> touched;
1278 touched.reserve(seq.size());
1279 for (unsigned i=0; i<seq.size(); ++i) touched[i]=false;
1281 unsigned number_of_zeroes=0;
1283 GINAC_ASSERT(!has_coeff_0());
1284 build_hashtab_and_combine(first_numeric,last_non_zero,touched,number_of_zeroes);
1286 cout << "in combine:" << endl;
1288 cout << "size=" << seq.end() - seq.begin() << endl;
1289 cout << "first_numeric=" << first_numeric - seq.begin() << endl;
1290 cout << "last_non_zero=" << last_non_zero - seq.begin() << endl;
1291 for (unsigned i=0; i<seq.size(); ++i) {
1292 if (touched[i]) cout << i << " is touched" << endl;
1294 cout << "end in combine" << endl;
1297 // there should not be any terms with coeff 0 from the beginning,
1298 // so it should be safe to skip this step
1299 if (number_of_zeroes!=0) {
1300 drop_coeff_0_terms(first_numeric,last_non_zero,touched,number_of_zeroes);
1302 cout << "in combine after drop:" << endl;
1304 cout << "size=" << seq.end() - seq.begin() << endl;
1305 cout << "first_numeric=" << first_numeric - seq.begin() << endl;
1306 cout << "last_non_zero=" << last_non_zero - seq.begin() << endl;
1307 for (unsigned i=0; i<seq.size(); ++i) {
1308 if (touched[i]) cout << i << " is touched" << endl;
1310 cout << "end in combine after drop" << endl;
1314 add_numerics_to_hashtab(first_numeric,last_non_zero);
1316 // pop zero elements
1317 for (unsigned i=0; i<number_of_zeroes; ++i) {
1321 // shrink hashtabsize to calculated value
1322 GINAC_ASSERT(!has_coeff_0());
1326 GINAC_ASSERT(!has_coeff_0());
1329 #endif // def EXPAIRSEQ_USE_HASHTAB
1331 bool expairseq::is_canonical() const
1333 if (seq.size()<=1) return 1;
1335 #ifdef EXPAIRSEQ_USE_HASHTAB
1336 if (hashtabsize>0) return 1; // not canoncalized
1337 #endif // def EXPAIRSEQ_USE_HASHTAB
1339 epvector::const_iterator it=seq.begin();
1340 epvector::const_iterator it_last=it;
1341 for (++it; it!=seq.end(); it_last=it, ++it) {
1342 if (!((*it_last).is_less(*it)||(*it_last).is_equal(*it))) {
1343 if (!is_ex_exactly_of_type((*it_last).rest,numeric)||
1344 !is_ex_exactly_of_type((*it).rest,numeric)) {
1345 // double test makes it easier to set a breakpoint...
1346 if (!is_ex_exactly_of_type((*it_last).rest,numeric)||
1347 !is_ex_exactly_of_type((*it).rest,numeric)) {
1348 printpair(cout,*it_last,0);
1350 printpair(cout,*it,0);
1352 cout << "pair1:" << endl;
1353 (*it_last).rest.printtree(cout);
1354 (*it_last).coeff.printtree(cout);
1355 cout << "pair2:" << endl;
1356 (*it).rest.printtree(cout);
1357 (*it).coeff.printtree(cout);
1366 epvector * expairseq::expandchildren(unsigned options) const
1368 epvector::const_iterator last=seq.end();
1369 epvector::const_iterator cit=seq.begin();
1371 ex const & expanded_ex=(*cit).rest.expand(options);
1372 if (!are_ex_trivially_equal((*cit).rest,expanded_ex)) {
1374 // something changed, copy seq, eval and return it
1375 epvector *s=new epvector;
1376 s->reserve(seq.size());
1378 // copy parts of seq which are known not to have changed
1379 epvector::const_iterator cit2=seq.begin();
1381 s->push_back(*cit2);
1384 // copy first changed element
1385 s->push_back(combine_ex_with_coeff_to_pair(expanded_ex,
1389 while (cit2!=last) {
1390 s->push_back(combine_ex_with_coeff_to_pair((*cit2).rest.expand(options),
1399 return 0; // nothing has changed
1402 epvector * expairseq::evalchildren(int level) const
1404 // returns a NULL pointer if nothing had to be evaluated
1405 // returns a pointer to a newly created epvector otherwise
1406 // (which has to be deleted somewhere else)
1411 if (level == -max_recursion_level) {
1412 throw(std::runtime_error("max recursion level reached"));
1416 epvector::const_iterator last=seq.end();
1417 epvector::const_iterator cit=seq.begin();
1419 ex const & evaled_ex=(*cit).rest.eval(level);
1420 if (!are_ex_trivially_equal((*cit).rest,evaled_ex)) {
1422 // something changed, copy seq, eval and return it
1423 epvector *s=new epvector;
1424 s->reserve(seq.size());
1426 // copy parts of seq which are known not to have changed
1427 epvector::const_iterator cit2=seq.begin();
1429 s->push_back(*cit2);
1432 // copy first changed element
1433 s->push_back(combine_ex_with_coeff_to_pair(evaled_ex,
1437 while (cit2!=last) {
1438 s->push_back(combine_ex_with_coeff_to_pair((*cit2).rest.eval(level),
1447 return 0; // nothing has changed
1450 epvector expairseq::evalfchildren(int level) const
1453 s.reserve(seq.size());
1458 if (level == -max_recursion_level) {
1459 throw(std::runtime_error("max recursion level reached"));
1462 for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
1463 s.push_back(combine_ex_with_coeff_to_pair((*it).rest.evalf(level),
1469 epvector expairseq::normalchildren(int level) const
1472 s.reserve(seq.size());
1477 if (level == -max_recursion_level) {
1478 throw(std::runtime_error("max recursion level reached"));
1481 for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
1482 s.push_back(combine_ex_with_coeff_to_pair((*it).rest.normal(level),
1488 epvector expairseq::diffchildren(symbol const & y) const
1491 s.reserve(seq.size());
1493 for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
1494 s.push_back(combine_ex_with_coeff_to_pair((*it).rest.diff(y),
1500 epvector * expairseq::subschildren(lst const & ls, lst const & lr) const
1502 // returns a NULL pointer if nothing had to be substituted
1503 // returns a pointer to a newly created epvector otherwise
1504 // (which has to be deleted somewhere else)
1506 epvector::const_iterator last=seq.end();
1507 epvector::const_iterator cit=seq.begin();
1509 ex const & subsed_ex=(*cit).rest.subs(ls,lr);
1510 if (!are_ex_trivially_equal((*cit).rest,subsed_ex)) {
1512 // something changed, copy seq, subs and return it
1513 epvector *s=new epvector;
1514 s->reserve(seq.size());
1516 // copy parts of seq which are known not to have changed
1517 epvector::const_iterator cit2=seq.begin();
1519 s->push_back(*cit2);
1522 // copy first changed element
1523 s->push_back(combine_ex_with_coeff_to_pair(subsed_ex,
1527 while (cit2!=last) {
1528 s->push_back(combine_ex_with_coeff_to_pair((*cit2).rest.subs(ls,lr),
1537 return 0; // nothing has changed
1541 epvector expairseq::subschildren(lst const & ls, lst const & lr) const
1544 s.reserve(seq.size());
1546 for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
1547 s.push_back(split_ex_to_pair((*it).rest.subs(ls,lr),(*it).coeff));
1554 void expairseq::sort(epviter first, epviter last, expair_is_less comp)
1556 if (first != last) {
1557 introsort_loop(first, last, lg(last - first) * 2, comp);
1558 __final_insertion_sort(first, last, comp);
1562 ptrdiff_t expairseq::lg(ptrdiff_t n)
1565 for (k = 0; n > 1; n >>= 1) ++k;
1569 void expairseq::introsort_loop(epviter first, epviter last,
1570 ptrdiff_t depth_limit, expair_is_less comp)
1572 while (last - first > stl_threshold) {
1573 if (depth_limit == 0) {
1574 partial_sort(first, last, last, comp);
1578 epviter cut = unguarded_partition(first, last,
1579 expair(__median(*first, *(first + (last - first)/2),
1580 *(last - 1), comp)), comp);
1581 introsort_loop(cut, last, depth_limit, comp);
1586 epviter expairseq::unguarded_partition(epviter first, epviter last,
1587 expair pivot, expair_is_less comp)
1590 while (comp(*first, pivot)) ++first;
1592 while (comp(pivot, *last)) --last;
1593 if (!(first < last)) return first;
1594 iter_swap(first, last);
1599 void expairseq::partial_sort(epviter first, epviter middle, epviter last,
1600 expair_is_less comp) {
1601 make_heap(first, middle, comp);
1602 for (RandomAccessIterator i = middle; i < last; ++i)
1603 if (comp(*i, *first))
1604 __pop_heap(first, middle, i, T(*i), comp, distance_type(first));
1605 sort_heap(first, middle, comp);
1610 // static member variables
1615 unsigned expairseq::precedence=10;
1617 #ifdef EXPAIRSEQ_USE_HASHTAB
1618 unsigned expairseq::maxhashtabsize=0x4000000U;
1619 unsigned expairseq::minhashtabsize=0x1000U;
1620 unsigned expairseq::hashtabfactor=1;
1621 #endif // def EXPAIRSEQ_USE_HASHTAB
1627 const expairseq some_expairseq;
1628 type_info const & typeid_expairseq=typeid(some_expairseq);
1630 } // namespace GiNaC