1 /** @file expairseq.cpp
3 * Implementation of sequences of expression pairs. */
6 * GiNaC Copyright (C) 1999-2000 Johannes Gutenberg University Mainz, Germany
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include "expairseq.h"
34 #ifndef NO_NAMESPACE_GINAC
36 #endif // ndef NO_NAMESPACE_GINAC
38 #ifdef EXPAIRSEQ_USE_HASHTAB
39 #error "FIXME: expair_needs_further_processing not yet implemented for hashtabs, sorry. A.F."
40 #endif // def EXPAIRSEQ_USE_HASHTAB
42 GINAC_IMPLEMENT_REGISTERED_CLASS(expairseq, basic)
51 bool operator()(const epp & lh, const epp & rh) const
53 return (*lh).is_less(*rh);
58 // default constructor, destructor, copy constructor assignment operator and helpers
63 expairseq::expairseq(const expairseq & other)
65 debugmsg("expairseq copy constructor",LOGLEVEL_CONSTRUCT);
69 const expairseq & expairseq::operator=(const expairseq & other)
71 debugmsg("expairseq operator=",LOGLEVEL_ASSIGNMENT);
81 void expairseq::copy(const expairseq & other)
83 inherited::copy(other);
85 overall_coeff=other.overall_coeff;
86 #ifdef EXPAIRSEQ_USE_HASHTAB
88 hashtabsize=other.hashtabsize;
90 hashmask=other.hashmask;
91 hashtab.resize(hashtabsize);
92 epvector::const_iterator osb=other.seq.begin();
93 for (unsigned i=0; i<hashtabsize; ++i) {
95 for (epplist::const_iterator cit=other.hashtab[i].begin();
96 cit!=other.hashtab[i].end(); ++cit) {
97 hashtab[i].push_back(seq.begin()+((*cit)-osb));
103 #endif // def EXPAIRSEQ_USE_HASHTAB
107 // other constructors
110 expairseq::expairseq(const ex & lh, const ex & rh) : inherited(TINFO_expairseq)
112 debugmsg("expairseq constructor from ex,ex",LOGLEVEL_CONSTRUCT);
113 construct_from_2_ex(lh,rh);
114 GINAC_ASSERT(is_canonical());
117 expairseq::expairseq(const exvector & v) : inherited(TINFO_expairseq)
119 debugmsg("expairseq constructor from exvector",LOGLEVEL_CONSTRUCT);
120 construct_from_exvector(v);
121 GINAC_ASSERT(is_canonical());
125 expairseq::expairseq(const epvector & v, bool do_not_canonicalize) :
126 inherited(TINFO_expairseq)
128 debugmsg("expairseq constructor from epvector",LOGLEVEL_CONSTRUCT);
129 if (do_not_canonicalize) {
131 #ifdef EXPAIRSEQ_USE_HASHTAB
132 combine_same_terms(); // to build hashtab
133 #endif // def EXPAIRSEQ_USE_HASHTAB
135 construct_from_epvector(v);
137 GINAC_ASSERT(is_canonical());
141 expairseq::expairseq(const epvector & v, const ex & oc) :
142 inherited(TINFO_expairseq), overall_coeff(oc)
144 debugmsg("expairseq constructor from epvector,ex",LOGLEVEL_CONSTRUCT);
145 construct_from_epvector(v);
146 GINAC_ASSERT(is_canonical());
149 expairseq::expairseq(epvector * vp, const ex & oc) :
150 inherited(TINFO_expairseq), overall_coeff(oc)
152 debugmsg("expairseq constructor from epvector *,ex",LOGLEVEL_CONSTRUCT);
154 construct_from_epvector(*vp);
156 GINAC_ASSERT(is_canonical());
163 /** Construct object from archive_node. */
164 expairseq::expairseq(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
165 #ifdef EXPAIRSEQ_USE_HASHTAB
169 debugmsg("expairseq constructor from archive_node", LOGLEVEL_CONSTRUCT);
170 for (unsigned int i=0; true; i++) {
173 if (n.find_ex("rest", rest, sym_lst, i) && n.find_ex("coeff", coeff, sym_lst, i))
174 seq.push_back(expair(rest, coeff));
178 n.find_ex("overall_coeff", overall_coeff, sym_lst);
181 /** Unarchive the object. */
182 ex expairseq::unarchive(const archive_node &n, const lst &sym_lst)
184 return (new expairseq(n, sym_lst))->setflag(status_flags::dynallocated);
187 /** Archive the object. */
188 void expairseq::archive(archive_node &n) const
190 inherited::archive(n);
191 epvector::const_iterator i = seq.begin(), iend = seq.end();
193 n.add_ex("rest", i->rest);
194 n.add_ex("coeff", i->coeff);
197 n.add_ex("overall_coeff", overall_coeff);
201 // functions overriding virtual functions from bases classes
206 basic * expairseq::duplicate() const
208 debugmsg("expairseq duplicate",LOGLEVEL_DUPLICATE);
209 return new expairseq(*this);
212 void expairseq::print(ostream & os, unsigned upper_precedence) const
214 debugmsg("expairseq print",LOGLEVEL_PRINT);
216 printseq(os,',',precedence,upper_precedence);
220 void expairseq::printraw(ostream & os) const
222 debugmsg("expairseq printraw",LOGLEVEL_PRINT);
225 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
227 (*cit).rest.printraw(os);
229 (*cit).coeff.printraw(os);
235 void expairseq::printtree(ostream & os, unsigned indent) const
237 debugmsg("expairseq printtree",LOGLEVEL_PRINT);
239 os << string(indent,' ') << "type=" << typeid(*this).name()
240 << ", hash=" << hashvalue << " (0x" << hex << hashvalue << dec << ")"
241 << ", flags=" << flags
242 << ", nops=" << nops() << endl;
243 for (unsigned i=0; i<seq.size(); ++i) {
244 seq[i].rest.printtree(os,indent+delta_indent);
245 seq[i].coeff.printtree(os,indent+delta_indent);
246 if (i!=seq.size()-1) {
247 os << string(indent+delta_indent,' ') << "-----" << endl;
250 if (!overall_coeff.is_equal(default_overall_coeff())) {
251 os << string(indent+delta_indent,' ') << "-----" << endl;
252 os << string(indent+delta_indent,' ') << "overall_coeff" << endl;
253 overall_coeff.printtree(os,indent+delta_indent);
255 os << string(indent+delta_indent,' ') << "=====" << endl;
256 #ifdef EXPAIRSEQ_USE_HASHTAB
257 os << string(indent+delta_indent,' ')
258 << "hashtab size " << hashtabsize << endl;
259 if (hashtabsize==0) return;
261 unsigned count[MAXCOUNT+1];
262 for (int i=0; i<MAXCOUNT+1; ++i) count[i]=0;
263 unsigned this_bin_fill;
264 unsigned cum_fill_sq=0;
266 for (unsigned i=0; i<hashtabsize; ++i) {
268 if (hashtab[i].size()>0) {
269 os << string(indent+delta_indent,' ')
270 << "bin " << i << " with entries ";
271 for (epplist::const_iterator it=hashtab[i].begin();
272 it!=hashtab[i].end(); ++it) {
273 os << *it-seq.begin() << " ";
277 cum_fill += this_bin_fill;
278 cum_fill_sq += this_bin_fill*this_bin_fill;
280 if (this_bin_fill<MAXCOUNT) {
281 ++count[this_bin_fill];
288 double lambda=(1.0*seq.size())/hashtabsize;
289 for (int k=0; k<MAXCOUNT; ++k) {
291 double prob=pow(lambda,k)/fact*exp(-lambda);
293 os << string(indent+delta_indent,' ') << "bins with " << k << " entries: "
294 << int(1000.0*count[k]/hashtabsize)/10.0 << "% (expected: "
295 << int(prob*1000)/10.0 << ")" << endl;
297 os << string(indent+delta_indent,' ') << "bins with more entries: "
298 << int(1000.0*count[MAXCOUNT]/hashtabsize)/10.0 << "% (expected: "
299 << int((1-cum_prob)*1000)/10.0 << ")" << endl;
301 os << string(indent+delta_indent,' ') << "variance: "
302 << 1.0/hashtabsize*cum_fill_sq-(1.0/hashtabsize*cum_fill)*(1.0/hashtabsize*cum_fill)
304 os << string(indent+delta_indent,' ') << "average fill: "
305 << (1.0*cum_fill)/hashtabsize
306 << " (should be equal to " << (1.0*seq.size())/hashtabsize << ")" << endl;
307 #endif // def EXPAIRSEQ_USE_HASHTAB
310 bool expairseq::info(unsigned inf) const
312 return inherited::info(inf);
315 unsigned expairseq::nops() const
317 if (overall_coeff.is_equal(default_overall_coeff())) {
323 ex expairseq::op(int i) const
325 if (unsigned(i)<seq.size()) {
326 return recombine_pair_to_ex(seq[i]);
328 GINAC_ASSERT(!overall_coeff.is_equal(default_overall_coeff()));
329 return overall_coeff;
332 ex & expairseq::let_op(int i)
334 throw(std::logic_error("let_op not defined for expairseq and derived classes (add,mul,...)"));
337 ex expairseq::eval(int level) const
339 if ((level==1)&&(flags & status_flags::evaluated)) {
343 epvector * vp=evalchildren(level);
348 return (new expairseq(vp,overall_coeff))
349 ->setflag(status_flags::dynallocated |
350 status_flags::evaluated );
353 ex expairseq::evalf(int level) const
355 return thisexpairseq(evalfchildren(level),overall_coeff);
358 ex expairseq::normal(lst &sym_lst, lst &repl_lst, int level) const
360 ex n=thisexpairseq(normalchildren(level),overall_coeff);
361 return n.bp->basic::normal(sym_lst,repl_lst,level);
364 ex expairseq::subs(const lst & ls, const lst & lr) const
366 epvector * vp=subschildren(ls,lr);
370 return thisexpairseq(vp,overall_coeff);
375 /** Implementation of ex::diff() for an expairseq. It differentiates all elements of the
378 ex expairseq::derivative(const symbol & s) const
380 return thisexpairseq(diffchildren(s),overall_coeff);
383 int expairseq::compare_same_type(const basic & other) const
385 GINAC_ASSERT(is_of_type(other, expairseq));
386 const expairseq & o=static_cast<const expairseq &>(const_cast<basic &>(other));
390 // compare number of elements
391 if (seq.size() != o.seq.size()) {
392 return (seq.size()<o.seq.size()) ? -1 : 1;
395 // compare overall_coeff
396 cmpval=overall_coeff.compare(o.overall_coeff);
397 if (cmpval!=0) return cmpval;
399 //if (seq.size()==0) return 0; // empty expairseq's are equal
401 #ifdef EXPAIRSEQ_USE_HASHTAB
402 GINAC_ASSERT(hashtabsize==o.hashtabsize);
403 if (hashtabsize==0) {
404 #endif // def EXPAIRSEQ_USE_HASHTAB
405 epvector::const_iterator cit1=seq.begin();
406 epvector::const_iterator cit2=o.seq.begin();
407 epvector::const_iterator last1=seq.end();
408 epvector::const_iterator last2=o.seq.end();
410 for (; (cit1!=last1)&&(cit2!=last2); ++cit1, ++cit2) {
411 cmpval=(*cit1).compare(*cit2);
412 if (cmpval!=0) return cmpval;
415 GINAC_ASSERT(cit1==last1);
416 GINAC_ASSERT(cit2==last2);
419 #ifdef EXPAIRSEQ_USE_HASHTAB
422 // compare number of elements in each hashtab entry
423 for (unsigned i=0; i<hashtabsize; ++i) {
424 unsigned cursize=hashtab[i].size();
425 if (cursize != o.hashtab[i].size()) {
426 return (cursize < o.hashtab[i].size()) ? -1 : 1;
430 // compare individual (sorted) hashtab entries
431 for (unsigned i=0; i<hashtabsize; ++i) {
432 unsigned sz=hashtab[i].size();
434 const epplist & eppl1=hashtab[i];
435 const epplist & eppl2=o.hashtab[i];
436 epplist::const_iterator it1=eppl1.begin();
437 epplist::const_iterator it2=eppl2.begin();
438 while (it1!=eppl1.end()) {
439 cmpval=(*(*it1)).compare(*(*it2));
440 if (cmpval!=0) return cmpval;
448 #endif // def EXPAIRSEQ_USE_HASHTAB
451 bool expairseq::is_equal_same_type(const basic & other) const
453 const expairseq & o=dynamic_cast<const expairseq &>(const_cast<basic &>(other));
455 // compare number of elements
456 if (seq.size() != o.seq.size()) return false;
458 // compare overall_coeff
459 if (!overall_coeff.is_equal(o.overall_coeff)) return false;
461 #ifdef EXPAIRSEQ_USE_HASHTAB
462 // compare number of elements in each hashtab entry
463 if (hashtabsize!=o.hashtabsize) {
464 cout << "this:" << endl;
466 cout << "other:" << endl;
467 other.printtree(cout,0);
470 GINAC_ASSERT(hashtabsize==o.hashtabsize);
472 if (hashtabsize==0) {
473 #endif // def EXPAIRSEQ_USE_HASHTAB
474 epvector::const_iterator cit1=seq.begin();
475 epvector::const_iterator cit2=o.seq.begin();
476 epvector::const_iterator last1=seq.end();
478 while (cit1!=last1) {
479 if (!(*cit1).is_equal(*cit2)) return false;
485 #ifdef EXPAIRSEQ_USE_HASHTAB
488 for (unsigned i=0; i<hashtabsize; ++i) {
489 if (hashtab[i].size() != o.hashtab[i].size()) return false;
492 // compare individual sorted hashtab entries
493 for (unsigned i=0; i<hashtabsize; ++i) {
494 unsigned sz=hashtab[i].size();
496 const epplist & eppl1=hashtab[i];
497 const epplist & eppl2=o.hashtab[i];
498 epplist::const_iterator it1=eppl1.begin();
499 epplist::const_iterator it2=eppl2.begin();
500 while (it1!=eppl1.end()) {
501 if (!(*(*it1)).is_equal(*(*it2))) return false;
509 #endif // def EXPAIRSEQ_USE_HASHTAB
512 unsigned expairseq::return_type(void) const
514 return return_types::noncommutative_composite;
517 unsigned expairseq::calchash(void) const
519 unsigned v=golden_ratio_hash(tinfo());
520 epvector::const_iterator last=seq.end();
521 for (epvector::const_iterator cit=seq.begin(); cit!=last; ++cit) {
522 #ifndef EXPAIRSEQ_USE_HASHTAB
523 v=rotate_left_31(v); // rotation would spoil commutativity
524 #endif // ndef EXPAIRSEQ_USE_HASHTAB
525 v ^= (*cit).rest.gethash();
528 v ^= overall_coeff.gethash();
531 // store calculated hash value only if object is already evaluated
532 if (flags & status_flags::evaluated) {
533 setflag(status_flags::hash_calculated);
540 ex expairseq::expand(unsigned options) const
542 epvector * vp=expandchildren(options);
546 return thisexpairseq(vp,overall_coeff);
550 // new virtual functions which can be overridden by derived classes
555 ex expairseq::thisexpairseq(const epvector & v,const ex & oc) const
557 return expairseq(v,oc);
560 ex expairseq::thisexpairseq(epvector * vp, const ex & oc) const
562 return expairseq(vp,oc);
565 void expairseq::printpair(ostream & os, const expair & p, unsigned upper_precedence) const
568 p.rest.bp->print(os,precedence);
570 p.coeff.bp->print(os,precedence);
574 void expairseq::printseq(ostream & os, char delim, unsigned this_precedence,
575 unsigned upper_precedence) const
577 if (this_precedence<=upper_precedence) os << "(";
578 epvector::const_iterator it,it_last;
581 for (it=seq.begin(); it!=it_last; ++it) {
582 printpair(os,*it,this_precedence);
585 printpair(os,*it,this_precedence);
586 if (!overall_coeff.is_equal(default_overall_coeff())) {
587 os << delim << overall_coeff;
589 if (this_precedence<=upper_precedence) os << ")";
592 expair expairseq::split_ex_to_pair(const ex & e) const
594 return expair(e,_ex1());
597 expair expairseq::combine_ex_with_coeff_to_pair(const ex & e,
600 GINAC_ASSERT(is_ex_exactly_of_type(c,numeric));
605 expair expairseq::combine_pair_with_coeff_to_pair(const expair & p,
608 GINAC_ASSERT(is_ex_exactly_of_type(p.coeff,numeric));
609 GINAC_ASSERT(is_ex_exactly_of_type(c,numeric));
611 return expair(p.rest,ex_to_numeric(p.coeff).mul_dyn(ex_to_numeric(c)));
614 ex expairseq::recombine_pair_to_ex(const expair & p) const
616 return lst(p.rest,p.coeff);
619 bool expairseq::expair_needs_further_processing(epp it)
624 ex expairseq::default_overall_coeff(void) const
629 void expairseq::combine_overall_coeff(const ex & c)
631 GINAC_ASSERT(is_ex_exactly_of_type(overall_coeff,numeric));
632 GINAC_ASSERT(is_ex_exactly_of_type(c,numeric));
633 overall_coeff = ex_to_numeric(overall_coeff).add_dyn(ex_to_numeric(c));
636 void expairseq::combine_overall_coeff(const ex & c1, const ex & c2)
638 GINAC_ASSERT(is_ex_exactly_of_type(overall_coeff,numeric));
639 GINAC_ASSERT(is_ex_exactly_of_type(c1,numeric));
640 GINAC_ASSERT(is_ex_exactly_of_type(c2,numeric));
641 overall_coeff = ex_to_numeric(overall_coeff).
642 add_dyn(ex_to_numeric(c1).mul(ex_to_numeric(c2)));
645 bool expairseq::can_make_flat(const expair & p) const
652 // non-virtual functions in this class
655 void expairseq::construct_from_2_ex_via_exvector(const ex & lh, const ex & rh)
661 construct_from_exvector(v);
662 #ifdef EXPAIRSEQ_USE_HASHTAB
663 GINAC_ASSERT((hashtabsize==0)||(hashtabsize>=minhashtabsize));
664 GINAC_ASSERT(hashtabsize==calc_hashtabsize(seq.size()));
665 #endif // def EXPAIRSEQ_USE_HASHTAB
668 void expairseq::construct_from_2_ex(const ex & lh, const ex & rh)
670 if (lh.bp->tinfo()==tinfo()) {
671 if (rh.bp->tinfo()==tinfo()) {
672 #ifdef EXPAIRSEQ_USE_HASHTAB
673 unsigned totalsize=ex_to_expairseq(lh).seq.size()+
674 ex_to_expairseq(rh).seq.size();
675 if (calc_hashtabsize(totalsize)!=0) {
676 construct_from_2_ex_via_exvector(lh,rh);
678 #endif // def EXPAIRSEQ_USE_HASHTAB
679 construct_from_2_expairseq(ex_to_expairseq(lh),
680 ex_to_expairseq(rh));
681 #ifdef EXPAIRSEQ_USE_HASHTAB
683 #endif // def EXPAIRSEQ_USE_HASHTAB
686 #ifdef EXPAIRSEQ_USE_HASHTAB
687 unsigned totalsize=ex_to_expairseq(lh).seq.size()+1;
688 if (calc_hashtabsize(totalsize)!=0) {
689 construct_from_2_ex_via_exvector(lh,rh);
691 #endif // def EXPAIRSEQ_USE_HASHTAB
692 construct_from_expairseq_ex(ex_to_expairseq(lh),rh);
693 #ifdef EXPAIRSEQ_USE_HASHTAB
695 #endif // def EXPAIRSEQ_USE_HASHTAB
698 } else if (rh.bp->tinfo()==tinfo()) {
699 #ifdef EXPAIRSEQ_USE_HASHTAB
700 unsigned totalsize=ex_to_expairseq(rh).seq.size()+1;
701 if (calc_hashtabsize(totalsize)!=0) {
702 construct_from_2_ex_via_exvector(lh,rh);
704 #endif // def EXPAIRSEQ_USE_HASHTAB
705 construct_from_expairseq_ex(ex_to_expairseq(rh),lh);
706 #ifdef EXPAIRSEQ_USE_HASHTAB
708 #endif // def EXPAIRSEQ_USE_HASHTAB
712 #ifdef EXPAIRSEQ_USE_HASHTAB
713 if (calc_hashtabsize(2)!=0) {
714 construct_from_2_ex_via_exvector(lh,rh);
718 #endif // def EXPAIRSEQ_USE_HASHTAB
720 if (is_ex_exactly_of_type(lh,numeric)) {
721 if (is_ex_exactly_of_type(rh,numeric)) {
722 combine_overall_coeff(lh);
723 combine_overall_coeff(rh);
725 combine_overall_coeff(lh);
726 seq.push_back(split_ex_to_pair(rh));
729 if (is_ex_exactly_of_type(rh,numeric)) {
730 combine_overall_coeff(rh);
731 seq.push_back(split_ex_to_pair(lh));
733 expair p1=split_ex_to_pair(lh);
734 expair p2=split_ex_to_pair(rh);
736 int cmpval=p1.rest.compare(p2.rest);
738 p1.coeff=ex_to_numeric(p1.coeff).add_dyn(ex_to_numeric(p2.coeff));
739 if (!ex_to_numeric(p1.coeff).is_zero()) {
740 // no further processing is necessary, since this
741 // one element will usually be recombined in eval()
758 void expairseq::construct_from_2_expairseq(const expairseq & s1,
759 const expairseq & s2)
761 combine_overall_coeff(s1.overall_coeff);
762 combine_overall_coeff(s2.overall_coeff);
764 epvector::const_iterator first1=s1.seq.begin();
765 epvector::const_iterator last1=s1.seq.end();
766 epvector::const_iterator first2=s2.seq.begin();
767 epvector::const_iterator last2=s2.seq.end();
769 seq.reserve(s1.seq.size()+s2.seq.size());
771 bool needs_further_processing=false;
773 while (first1!=last1 && first2!=last2) {
774 int cmpval=(*first1).rest.compare((*first2).rest);
777 const numeric & newcoeff=ex_to_numeric((*first1).coeff).
778 add(ex_to_numeric((*first2).coeff));
779 if (!newcoeff.is_zero()) {
780 seq.push_back(expair((*first1).rest,newcoeff));
781 if (expair_needs_further_processing(seq.end()-1)) {
782 needs_further_processing = true;
787 } else if (cmpval<0) {
788 seq.push_back(*first1);
791 seq.push_back(*first2);
796 while (first1!=last1) {
797 seq.push_back(*first1);
800 while (first2!=last2) {
801 seq.push_back(*first2);
805 if (needs_further_processing) {
808 construct_from_epvector(v);
812 void expairseq::construct_from_expairseq_ex(const expairseq & s,
815 combine_overall_coeff(s.overall_coeff);
816 if (is_ex_exactly_of_type(e,numeric)) {
817 combine_overall_coeff(e);
822 epvector::const_iterator first=s.seq.begin();
823 epvector::const_iterator last=s.seq.end();
824 expair p=split_ex_to_pair(e);
826 seq.reserve(s.seq.size()+1);
829 bool needs_further_processing=false;
831 // merge p into s.seq
832 while (first!=last) {
833 int cmpval=(*first).rest.compare(p.rest);
836 const numeric & newcoeff=ex_to_numeric((*first).coeff).
837 add(ex_to_numeric(p.coeff));
838 if (!newcoeff.is_zero()) {
839 seq.push_back(expair((*first).rest,newcoeff));
840 if (expair_needs_further_processing(seq.end()-1)) {
841 needs_further_processing = true;
847 } else if (cmpval<0) {
848 seq.push_back(*first);
858 // while loop exited because p was pushed, now push rest of s.seq
859 while (first!=last) {
860 seq.push_back(*first);
864 // while loop exited because s.seq was pushed, now push p
868 if (needs_further_processing) {
871 construct_from_epvector(v);
875 void expairseq::construct_from_exvector(const exvector & v)
877 // simplifications: +(a,+(b,c),d) -> +(a,b,c,d) (associativity)
878 // +(d,b,c,a) -> +(a,b,c,d) (canonicalization)
879 // +(...,x,*(x,c1),*(x,c2)) -> +(...,*(x,1+c1+c2)) (c1, c2 numeric())
880 // (same for (+,*) -> (*,^)
883 #ifdef EXPAIRSEQ_USE_HASHTAB
884 combine_same_terms();
887 combine_same_terms_sorted_seq();
888 #endif // def EXPAIRSEQ_USE_HASHTAB
891 void expairseq::construct_from_epvector(const epvector & v)
893 // simplifications: +(a,+(b,c),d) -> +(a,b,c,d) (associativity)
894 // +(d,b,c,a) -> +(a,b,c,d) (canonicalization)
895 // +(...,x,*(x,c1),*(x,c2)) -> +(...,*(x,1+c1+c2)) (c1, c2 numeric())
896 // (same for (+,*) -> (*,^)
899 #ifdef EXPAIRSEQ_USE_HASHTAB
900 combine_same_terms();
903 combine_same_terms_sorted_seq();
904 #endif // def EXPAIRSEQ_USE_HASHTAB
909 void expairseq::make_flat(const exvector & v)
911 exvector::const_iterator cit, citend = v.end();
913 // count number of operands which are of same expairseq derived type
914 // and their cumulative number of operands
918 while (cit!=citend) {
919 if (cit->bp->tinfo()==tinfo()) {
921 noperands+=ex_to_expairseq(*cit).seq.size();
926 // reserve seq and coeffseq which will hold all operands
927 seq.reserve(v.size()+noperands-nexpairseqs);
929 // copy elements and split off numerical part
931 while (cit!=citend) {
932 if (cit->bp->tinfo()==tinfo()) {
933 const expairseq & subseqref=ex_to_expairseq(*cit);
934 combine_overall_coeff(subseqref.overall_coeff);
935 epvector::const_iterator cit_s=subseqref.seq.begin();
936 while (cit_s!=subseqref.seq.end()) {
937 seq.push_back(*cit_s);
941 if (is_ex_exactly_of_type(*cit,numeric)) {
942 combine_overall_coeff(*cit);
944 seq.push_back(split_ex_to_pair(*cit));
951 cout << "after make flat" << endl;
952 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
953 (*cit).printraw(cout);
959 void expairseq::make_flat(const epvector & v)
961 epvector::const_iterator cit, citend = v.end();
963 // count number of operands which are of same expairseq derived type
964 // and their cumulative number of operands
969 while (cit!=citend) {
970 if (cit->rest.bp->tinfo()==tinfo()) {
972 noperands += ex_to_expairseq((*cit).rest).seq.size();
977 // reserve seq and coeffseq which will hold all operands
978 seq.reserve(v.size()+noperands-nexpairseqs);
980 // copy elements and split off numerical part
982 while (cit!=citend) {
983 if ((cit->rest.bp->tinfo()==tinfo())&&can_make_flat(*cit)) {
984 const expairseq & subseqref=ex_to_expairseq((*cit).rest);
985 combine_overall_coeff(ex_to_numeric(subseqref.overall_coeff),
986 ex_to_numeric((*cit).coeff));
987 epvector::const_iterator cit_s=subseqref.seq.begin();
988 while (cit_s!=subseqref.seq.end()) {
989 seq.push_back(expair((*cit_s).rest,
990 ex_to_numeric((*cit_s).coeff).mul_dyn(ex_to_numeric((*cit).coeff))));
991 //seq.push_back(combine_pair_with_coeff_to_pair(*cit_s,
996 if ((*cit).is_numeric_with_coeff_1()) {
997 combine_overall_coeff((*cit).rest);
998 //if (is_ex_exactly_of_type((*cit).rest,numeric)) {
999 // combine_overall_coeff(recombine_pair_to_ex(*cit));
1001 seq.push_back(*cit);
1008 epvector * expairseq::bubblesort(epvector::iterator itbegin, epvector::iterator itend)
1010 unsigned n=itend-itbegin;
1012 epvector * sp=new epvector;
1015 epvector::iterator last=itend-1;
1016 for (epvector::iterator it1=itbegin; it1!=last; ++it1) {
1017 for (epvector::iterator it2=it1+1; it2!=itend; ++it2) {
1018 if ((*it2).rest.compare((*it1).rest)<0) {
1022 sp->push_back(*it1);
1024 sp->push_back(*last);
1028 epvector * expairseq::mergesort(epvector::iterator itbegin, epvector::iterator itend)
1030 unsigned n=itend-itbegin;
1033 epvector * sp=new epvector;
1034 sp->push_back(*itbegin);
1038 if (n<16) return bubblesort(itbegin, itend);
1041 epvector * s1p=mergesort(itbegin, itbegin+m);
1042 epvector * s2p=mergesort(itbegin+m, itend);
1044 epvector * sp=new epvector;
1045 sp->reserve(s1p->size()+s2p->size());
1047 epvector::iterator first1=s1p->begin();
1048 epvector::iterator last1=s1p->end();
1050 epvector::iterator first2=s2p->begin();
1051 epvector::iterator last2=s2p->end();
1053 while (first1 != last1 && first2 != last2) {
1054 if ((*first1).rest.compare((*first2).rest)<0) {
1055 sp->push_back(*first1);
1058 sp->push_back(*first2);
1063 if (first1 != last1) {
1064 while (first1 != last1) {
1065 sp->push_back(*first1);
1069 while (first2 != last2) {
1070 sp->push_back(*first2);
1082 void expairseq::canonicalize(void)
1085 sort(seq.begin(),seq.end(),expair_is_less());
1087 sort(seq.begin(),seq.end(),expair_is_less_old());
1089 if (is_ex_exactly_of_type((*(seq.begin())).rest,numeric)) {
1090 sort(seq.begin(),seq.end(),expair_is_less());
1092 epvector::iterator last_numeric=seq.end();
1095 } while (is_ex_exactly_of_type((*last_numeric).rest,numeric));
1097 sort(last_numeric,seq.end(),expair_is_less());
1103 epvector * sorted_seqp=mergesort(seq.begin(),seq.end());
1104 epvector::iterator last=sorted_seqp->end();
1105 epvector::iterator it2=seq.begin();
1106 for (epvector::iterator it1=sorted_seqp->begin(); it1!=last; ++it1, ++it2) {
1113 cout << "after canonicalize" << endl;
1114 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
1115 (*cit).printraw(cout);
1122 void expairseq::combine_same_terms_sorted_seq(void)
1124 bool needs_further_processing=false;
1126 // combine same terms, drop term with coeff 0
1128 epvector::iterator itin1=seq.begin();
1129 epvector::iterator itin2=itin1+1;
1130 epvector::iterator itout=itin1;
1131 epvector::iterator last=seq.end();
1132 // must_copy will be set to true the first time some combination is possible
1133 // from then on the sequence has changed and must be compacted
1134 bool must_copy=false;
1135 while (itin2!=last) {
1136 if ((*itin1).rest.compare((*itin2).rest)==0) {
1137 (*itin1).coeff=ex_to_numeric((*itin1).coeff).
1138 add_dyn(ex_to_numeric((*itin2).coeff));
1139 if (expair_needs_further_processing(itin1)) {
1140 needs_further_processing = true;
1144 if (!ex_to_numeric((*itin1).coeff).is_zero()) {
1154 if (!ex_to_numeric((*itin1).coeff).is_zero()) {
1161 seq.erase(itout,last);
1166 cout << "after combine" << endl;
1167 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
1168 (*cit).printraw(cout);
1174 if (needs_further_processing) {
1177 construct_from_epvector(v);
1181 #ifdef EXPAIRSEQ_USE_HASHTAB
1183 unsigned expairseq::calc_hashtabsize(unsigned sz) const
1186 unsigned nearest_power_of_2 = 1 << log2(sz);
1187 // if (nearest_power_of_2 < maxhashtabsize/hashtabfactor) {
1188 // size=nearest_power_of_2*hashtabfactor;
1189 size=nearest_power_of_2/hashtabfactor;
1190 if (size<minhashtabsize) return 0;
1191 GINAC_ASSERT(hashtabsize<=0x8000000U); // really max size due to 31 bit hashing
1192 // hashtabsize must be a power of 2
1193 GINAC_ASSERT((1U << log2(size))==size);
1197 unsigned expairseq::calc_hashindex(const ex & e) const
1199 // calculate hashindex
1200 unsigned hash=e.gethash();
1202 if (is_a_numeric_hash(hash)) {
1205 hashindex=hash & hashmask;
1206 // last hashtab entry is reserved for numerics
1207 if (hashindex==hashmask) hashindex=0;
1209 GINAC_ASSERT(hashindex>=0);
1210 GINAC_ASSERT((hashindex<hashtabsize)||(hashtabsize==0));
1214 void expairseq::shrink_hashtab(void)
1216 unsigned new_hashtabsize;
1217 while (hashtabsize!=(new_hashtabsize=calc_hashtabsize(seq.size()))) {
1218 GINAC_ASSERT(new_hashtabsize<hashtabsize);
1219 if (new_hashtabsize==0) {
1226 // shrink by a factor of 2
1227 unsigned half_hashtabsize=hashtabsize/2;
1228 for (unsigned i=0; i<half_hashtabsize-1; ++i) {
1229 hashtab[i].merge(hashtab[i+half_hashtabsize],epp_is_less());
1231 // special treatment for numeric hashes
1232 hashtab[0].merge(hashtab[half_hashtabsize-1],epp_is_less());
1233 hashtab[half_hashtabsize-1]=hashtab[hashtabsize-1];
1234 hashtab.resize(half_hashtabsize);
1235 hashtabsize=half_hashtabsize;
1236 hashmask=hashtabsize-1;
1240 void expairseq::remove_hashtab_entry(epvector::const_iterator element)
1242 if (hashtabsize==0) return; // nothing to do
1244 // calculate hashindex of element to be deleted
1245 unsigned hashindex=calc_hashindex((*element).rest);
1247 // find it in hashtab and remove it
1248 epplist & eppl=hashtab[hashindex];
1249 epplist::iterator epplit=eppl.begin();
1251 while (epplit!=eppl.end()) {
1252 if (*epplit == element) {
1261 cout << "tried to erase " << element-seq.begin() << endl;
1262 cout << "size " << seq.end()-seq.begin() << endl;
1264 unsigned hashindex=calc_hashindex((*element).rest);
1265 epplist & eppl=hashtab[hashindex];
1266 epplist::iterator epplit=eppl.begin();
1268 while (epplit!=eppl.end()) {
1269 if (*epplit == element) {
1276 GINAC_ASSERT(erased);
1278 GINAC_ASSERT(erased);
1281 void expairseq::move_hashtab_entry(epvector::const_iterator oldpos,
1282 epvector::iterator newpos)
1284 GINAC_ASSERT(hashtabsize!=0);
1286 // calculate hashindex of element which was moved
1287 unsigned hashindex=calc_hashindex((*newpos).rest);
1289 // find it in hashtab and modify it
1290 epplist & eppl=hashtab[hashindex];
1291 epplist::iterator epplit=eppl.begin();
1292 while (epplit!=eppl.end()) {
1293 if (*epplit == oldpos) {
1299 GINAC_ASSERT(epplit!=eppl.end());
1302 void expairseq::sorted_insert(epplist & eppl, epp elem)
1304 epplist::iterator current=eppl.begin();
1305 while ((current!=eppl.end())&&((*(*current)).is_less(*elem))) {
1308 eppl.insert(current,elem);
1311 void expairseq::build_hashtab_and_combine(epvector::iterator & first_numeric,
1312 epvector::iterator & last_non_zero,
1313 vector<bool> & touched,
1314 unsigned & number_of_zeroes)
1316 epp current=seq.begin();
1318 while (current!=first_numeric) {
1319 if (is_ex_exactly_of_type((*current).rest,numeric)) {
1321 iter_swap(current,first_numeric);
1323 // calculate hashindex
1324 unsigned currenthashindex=calc_hashindex((*current).rest);
1326 // test if there is already a matching expair in the hashtab-list
1327 epplist & eppl=hashtab[currenthashindex];
1328 epplist::iterator epplit=eppl.begin();
1329 while (epplit!=eppl.end()) {
1330 if ((*current).rest.is_equal((*(*epplit)).rest)) break;
1333 if (epplit==eppl.end()) {
1334 // no matching expair found, append this to end of list
1335 sorted_insert(eppl,current);
1338 // epplit points to a matching expair, combine it with current
1339 (*(*epplit)).coeff=ex_to_numeric((*(*epplit)).coeff).
1340 add_dyn(ex_to_numeric((*current).coeff));
1342 // move obsolete current expair to end by swapping with last_non_zero element
1343 // if this was a numeric, it is swapped with the expair before first_numeric
1344 iter_swap(current,last_non_zero);
1346 if (first_numeric!=last_non_zero) iter_swap(first_numeric,current);
1349 // test if combined term has coeff 0 and can be removed is done later
1350 touched[(*epplit)-seq.begin()]=true;
1356 void expairseq::drop_coeff_0_terms(epvector::iterator & first_numeric,
1357 epvector::iterator & last_non_zero,
1358 vector<bool> & touched,
1359 unsigned & number_of_zeroes)
1361 // move terms with coeff 0 to end and remove them from hashtab
1362 // check only those elements which have been touched
1363 epp current=seq.begin();
1365 while (current!=first_numeric) {
1369 } else if (!ex_to_numeric((*current).coeff).is_equal(_num0())) {
1373 remove_hashtab_entry(current);
1375 // move element to the end, unless it is already at the end
1376 if (current!=last_non_zero) {
1377 iter_swap(current,last_non_zero);
1379 bool numeric_swapped=first_numeric!=last_non_zero;
1380 if (numeric_swapped) iter_swap(first_numeric,current);
1381 epvector::iterator changed_entry;
1383 if (numeric_swapped) {
1384 changed_entry=first_numeric;
1386 changed_entry=last_non_zero;
1392 if (first_numeric!=current) {
1394 // change entry in hashtab which referred to first_numeric or last_non_zero to current
1395 move_hashtab_entry(changed_entry,current);
1396 touched[current-seq.begin()]=touched[changed_entry-seq.begin()];
1405 GINAC_ASSERT(i==current-seq.begin());
1408 bool expairseq::has_coeff_0(void) const
1410 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
1411 if ((*cit).coeff.is_equal(_ex0())) {
1418 void expairseq::add_numerics_to_hashtab(epvector::iterator first_numeric,
1419 epvector::const_iterator last_non_zero)
1421 if (first_numeric==seq.end()) return; // no numerics
1423 epvector::iterator current=first_numeric;
1424 epvector::const_iterator last=last_non_zero+1;
1425 while (current!=last) {
1426 sorted_insert(hashtab[hashmask],current);
1431 void expairseq::combine_same_terms(void)
1433 // combine same terms, drop term with coeff 0, move numerics to end
1435 // calculate size of hashtab
1436 hashtabsize=calc_hashtabsize(seq.size());
1438 // hashtabsize is a power of 2
1439 hashmask=hashtabsize-1;
1443 hashtab.resize(hashtabsize);
1445 if (hashtabsize==0) {
1447 combine_same_terms_sorted_seq();
1448 GINAC_ASSERT(!has_coeff_0());
1452 // iterate through seq, move numerics to end,
1453 // fill hashtab and combine same terms
1454 epvector::iterator first_numeric=seq.end();
1455 epvector::iterator last_non_zero=seq.end()-1;
1457 vector<bool> touched;
1458 touched.reserve(seq.size());
1459 for (unsigned i=0; i<seq.size(); ++i) touched[i]=false;
1461 unsigned number_of_zeroes=0;
1463 GINAC_ASSERT(!has_coeff_0());
1464 build_hashtab_and_combine(first_numeric,last_non_zero,touched,number_of_zeroes);
1466 cout << "in combine:" << endl;
1468 cout << "size=" << seq.end() - seq.begin() << endl;
1469 cout << "first_numeric=" << first_numeric - seq.begin() << endl;
1470 cout << "last_non_zero=" << last_non_zero - seq.begin() << endl;
1471 for (unsigned i=0; i<seq.size(); ++i) {
1472 if (touched[i]) cout << i << " is touched" << endl;
1474 cout << "end in combine" << endl;
1477 // there should not be any terms with coeff 0 from the beginning,
1478 // so it should be safe to skip this step
1479 if (number_of_zeroes!=0) {
1480 drop_coeff_0_terms(first_numeric,last_non_zero,touched,number_of_zeroes);
1482 cout << "in combine after drop:" << endl;
1484 cout << "size=" << seq.end() - seq.begin() << endl;
1485 cout << "first_numeric=" << first_numeric - seq.begin() << endl;
1486 cout << "last_non_zero=" << last_non_zero - seq.begin() << endl;
1487 for (unsigned i=0; i<seq.size(); ++i) {
1488 if (touched[i]) cout << i << " is touched" << endl;
1490 cout << "end in combine after drop" << endl;
1494 add_numerics_to_hashtab(first_numeric,last_non_zero);
1496 // pop zero elements
1497 for (unsigned i=0; i<number_of_zeroes; ++i) {
1501 // shrink hashtabsize to calculated value
1502 GINAC_ASSERT(!has_coeff_0());
1506 GINAC_ASSERT(!has_coeff_0());
1509 #endif // def EXPAIRSEQ_USE_HASHTAB
1511 bool expairseq::is_canonical() const
1513 if (seq.size()<=1) return 1;
1515 #ifdef EXPAIRSEQ_USE_HASHTAB
1516 if (hashtabsize>0) return 1; // not canoncalized
1517 #endif // def EXPAIRSEQ_USE_HASHTAB
1519 epvector::const_iterator it=seq.begin();
1520 epvector::const_iterator it_last=it;
1521 for (++it; it!=seq.end(); it_last=it, ++it) {
1522 if (!((*it_last).is_less(*it)||(*it_last).is_equal(*it))) {
1523 if (!is_ex_exactly_of_type((*it_last).rest,numeric)||
1524 !is_ex_exactly_of_type((*it).rest,numeric)) {
1525 // double test makes it easier to set a breakpoint...
1526 if (!is_ex_exactly_of_type((*it_last).rest,numeric)||
1527 !is_ex_exactly_of_type((*it).rest,numeric)) {
1528 printpair(cout,*it_last,0);
1530 printpair(cout,*it,0);
1532 cout << "pair1:" << endl;
1533 (*it_last).rest.printtree(cout);
1534 (*it_last).coeff.printtree(cout);
1535 cout << "pair2:" << endl;
1536 (*it).rest.printtree(cout);
1537 (*it).coeff.printtree(cout);
1546 epvector * expairseq::expandchildren(unsigned options) const
1548 epvector::const_iterator last=seq.end();
1549 epvector::const_iterator cit=seq.begin();
1551 const ex & expanded_ex=(*cit).rest.expand(options);
1552 if (!are_ex_trivially_equal((*cit).rest,expanded_ex)) {
1554 // something changed, copy seq, eval and return it
1555 epvector *s=new epvector;
1556 s->reserve(seq.size());
1558 // copy parts of seq which are known not to have changed
1559 epvector::const_iterator cit2=seq.begin();
1561 s->push_back(*cit2);
1564 // copy first changed element
1565 s->push_back(combine_ex_with_coeff_to_pair(expanded_ex,
1569 while (cit2!=last) {
1570 s->push_back(combine_ex_with_coeff_to_pair((*cit2).rest.expand(options),
1579 return 0; // nothing has changed
1582 epvector * expairseq::evalchildren(int level) const
1584 // returns a NULL pointer if nothing had to be evaluated
1585 // returns a pointer to a newly created epvector otherwise
1586 // (which has to be deleted somewhere else)
1591 if (level == -max_recursion_level) {
1592 throw(std::runtime_error("max recursion level reached"));
1596 epvector::const_iterator last=seq.end();
1597 epvector::const_iterator cit=seq.begin();
1599 const ex & evaled_ex=(*cit).rest.eval(level);
1600 if (!are_ex_trivially_equal((*cit).rest,evaled_ex)) {
1602 // something changed, copy seq, eval and return it
1603 epvector *s=new epvector;
1604 s->reserve(seq.size());
1606 // copy parts of seq which are known not to have changed
1607 epvector::const_iterator cit2=seq.begin();
1609 s->push_back(*cit2);
1612 // copy first changed element
1613 s->push_back(combine_ex_with_coeff_to_pair(evaled_ex,
1617 while (cit2!=last) {
1618 s->push_back(combine_ex_with_coeff_to_pair((*cit2).rest.eval(level),
1627 return 0; // nothing has changed
1630 epvector expairseq::evalfchildren(int level) const
1633 s.reserve(seq.size());
1638 if (level == -max_recursion_level) {
1639 throw(std::runtime_error("max recursion level reached"));
1642 for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
1643 s.push_back(combine_ex_with_coeff_to_pair((*it).rest.evalf(level),
1649 epvector expairseq::normalchildren(int level) const
1652 s.reserve(seq.size());
1657 if (level == -max_recursion_level) {
1658 throw(std::runtime_error("max recursion level reached"));
1661 for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
1662 s.push_back(combine_ex_with_coeff_to_pair((*it).rest.normal(level),
1668 epvector expairseq::diffchildren(const symbol & y) const
1671 s.reserve(seq.size());
1673 for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
1674 s.push_back(combine_ex_with_coeff_to_pair((*it).rest.diff(y),
1680 epvector * expairseq::subschildren(const lst & ls, const lst & lr) const
1682 // returns a NULL pointer if nothing had to be substituted
1683 // returns a pointer to a newly created epvector otherwise
1684 // (which has to be deleted somewhere else)
1685 GINAC_ASSERT(ls.nops()==lr.nops());
1687 epvector::const_iterator last=seq.end();
1688 epvector::const_iterator cit=seq.begin();
1690 const ex & subsed_ex=(*cit).rest.subs(ls,lr);
1691 if (!are_ex_trivially_equal((*cit).rest,subsed_ex)) {
1693 // something changed, copy seq, subs and return it
1694 epvector *s=new epvector;
1695 s->reserve(seq.size());
1697 // copy parts of seq which are known not to have changed
1698 epvector::const_iterator cit2=seq.begin();
1700 s->push_back(*cit2);
1703 // copy first changed element
1704 s->push_back(combine_ex_with_coeff_to_pair(subsed_ex,
1708 while (cit2!=last) {
1709 s->push_back(combine_ex_with_coeff_to_pair((*cit2).rest.subs(ls,lr),
1718 return 0; // nothing has changed
1722 // static member variables
1727 unsigned expairseq::precedence=10;
1729 #ifdef EXPAIRSEQ_USE_HASHTAB
1730 unsigned expairseq::maxhashtabsize=0x4000000U;
1731 unsigned expairseq::minhashtabsize=0x1000U;
1732 unsigned expairseq::hashtabfactor=1;
1733 #endif // def EXPAIRSEQ_USE_HASHTAB
1739 const expairseq some_expairseq;
1740 const type_info & typeid_expairseq=typeid(some_expairseq);
1742 #ifndef NO_NAMESPACE_GINAC
1743 } // namespace GiNaC
1744 #endif // ndef NO_NAMESPACE_GINAC