1 /** @file expairseq.cpp
3 * Implementation of sequences of expression pairs. */
6 * GiNaC Copyright (C) 1999-2001 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 #ifndef NO_NAMESPACE_GINAC
35 #endif // ndef NO_NAMESPACE_GINAC
37 #ifdef EXPAIRSEQ_USE_HASHTAB
38 #error "FIXME: expair_needs_further_processing not yet implemented for hashtabs, sorry. A.F."
39 #endif // def EXPAIRSEQ_USE_HASHTAB
41 GINAC_IMPLEMENT_REGISTERED_CLASS_NO_CTORS(expairseq, basic)
50 bool operator()(const epp & lh, const epp & rh) const
52 return (*lh).is_less(*rh);
57 // default constructor, destructor, copy constructor assignment operator and helpers
62 expairseq::expairseq(const expairseq & other)
64 debugmsg("expairseq copy constructor",LOGLEVEL_CONSTRUCT);
68 const expairseq & expairseq::operator=(const expairseq & other)
70 debugmsg("expairseq operator=",LOGLEVEL_ASSIGNMENT);
80 void expairseq::copy(const expairseq & other)
82 inherited::copy(other);
84 overall_coeff=other.overall_coeff;
85 #ifdef EXPAIRSEQ_USE_HASHTAB
87 hashtabsize=other.hashtabsize;
89 hashmask=other.hashmask;
90 hashtab.resize(hashtabsize);
91 epvector::const_iterator osb=other.seq.begin();
92 for (unsigned i=0; i<hashtabsize; ++i) {
94 for (epplist::const_iterator cit=other.hashtab[i].begin();
95 cit!=other.hashtab[i].end(); ++cit) {
96 hashtab[i].push_back(seq.begin()+((*cit)-osb));
102 #endif // def EXPAIRSEQ_USE_HASHTAB
106 // other constructors
109 expairseq::expairseq(const ex & lh, const ex & rh) : inherited(TINFO_expairseq)
111 debugmsg("expairseq constructor from ex,ex",LOGLEVEL_CONSTRUCT);
112 construct_from_2_ex(lh,rh);
113 GINAC_ASSERT(is_canonical());
116 expairseq::expairseq(const exvector & v) : inherited(TINFO_expairseq)
118 debugmsg("expairseq constructor from exvector",LOGLEVEL_CONSTRUCT);
119 construct_from_exvector(v);
120 GINAC_ASSERT(is_canonical());
124 expairseq::expairseq(const epvector & v, bool do_not_canonicalize)
125 : inherited(TINFO_expairseq)
127 debugmsg("expairseq constructor from epvector",LOGLEVEL_CONSTRUCT);
128 if (do_not_canonicalize) {
130 #ifdef EXPAIRSEQ_USE_HASHTAB
131 combine_same_terms(); // to build hashtab
132 #endif // def EXPAIRSEQ_USE_HASHTAB
134 construct_from_epvector(v);
136 GINAC_ASSERT(is_canonical());
140 expairseq::expairseq(const epvector & v, const ex & oc)
141 : inherited(TINFO_expairseq), overall_coeff(oc)
143 debugmsg("expairseq constructor from epvector,ex",LOGLEVEL_CONSTRUCT);
144 construct_from_epvector(v);
145 GINAC_ASSERT(is_canonical());
148 expairseq::expairseq(epvector * vp, const ex & oc)
149 : inherited(TINFO_expairseq), overall_coeff(oc)
151 debugmsg("expairseq constructor from epvector *,ex",LOGLEVEL_CONSTRUCT);
153 construct_from_epvector(*vp);
155 GINAC_ASSERT(is_canonical());
162 /** Construct object from archive_node. */
163 expairseq::expairseq(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
164 #ifdef EXPAIRSEQ_USE_HASHTAB
168 debugmsg("expairseq constructor from archive_node", LOGLEVEL_CONSTRUCT);
169 for (unsigned int i=0; true; i++) {
172 if (n.find_ex("rest", rest, sym_lst, i) && n.find_ex("coeff", coeff, sym_lst, i))
173 seq.push_back(expair(rest, coeff));
177 n.find_ex("overall_coeff", overall_coeff, sym_lst);
180 /** Unarchive the object. */
181 ex expairseq::unarchive(const archive_node &n, const lst &sym_lst)
183 return (new expairseq(n, sym_lst))->setflag(status_flags::dynallocated);
186 /** Archive the object. */
187 void expairseq::archive(archive_node &n) const
189 inherited::archive(n);
190 epvector::const_iterator i = seq.begin(), iend = seq.end();
192 n.add_ex("rest", i->rest);
193 n.add_ex("coeff", i->coeff);
196 n.add_ex("overall_coeff", overall_coeff);
200 // functions overriding virtual functions from bases classes
205 basic * expairseq::duplicate() const
207 debugmsg("expairseq duplicate",LOGLEVEL_DUPLICATE);
208 return new expairseq(*this);
211 void expairseq::print(std::ostream & os, unsigned upper_precedence) const
213 debugmsg("expairseq print",LOGLEVEL_PRINT);
215 printseq(os,',',precedence,upper_precedence);
219 void expairseq::printraw(std::ostream & os) const
221 debugmsg("expairseq printraw",LOGLEVEL_PRINT);
224 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
226 (*cit).rest.printraw(os);
228 (*cit).coeff.printraw(os);
234 void expairseq::printtree(std::ostream & os, unsigned indent) const
236 debugmsg("expairseq printtree",LOGLEVEL_PRINT);
238 os << std::string(indent,' ') << "type=" << class_name()
239 << ", hash=" << hashvalue
240 << " (0x" << std::hex << hashvalue << std::dec << ")"
241 << ", flags=" << flags
242 << ", nops=" << nops() << std::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 << std::string(indent+delta_indent,' ') << "-----" << std::endl;
250 if (!overall_coeff.is_equal(default_overall_coeff())) {
251 os << std::string(indent+delta_indent,' ') << "-----" << std::endl;
252 os << std::string(indent+delta_indent,' ') << "overall_coeff" << std::endl;
253 overall_coeff.printtree(os,indent+delta_indent);
255 os << std::string(indent+delta_indent,' ') << "=====" << std::endl;
256 #ifdef EXPAIRSEQ_USE_HASHTAB
257 os << std::string(indent+delta_indent,' ')
258 << "hashtab size " << hashtabsize << std::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;
265 unsigned cum_fill = 0;
266 for (unsigned i=0; i<hashtabsize; ++i) {
268 if (hashtab[i].size()>0) {
269 os << std::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 << std::string(indent+delta_indent,' ') << "bins with " << k << " entries: "
294 << int(1000.0*count[k]/hashtabsize)/10.0 << "% (expected: "
295 << int(prob*1000)/10.0 << ")" << std::endl;
297 os << std::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 << ")" << std::endl;
301 os << std::string(indent+delta_indent,' ') << "variance: "
302 << 1.0/hashtabsize*cum_fill_sq-(1.0/hashtabsize*cum_fill)*(1.0/hashtabsize*cum_fill)
304 os << std::string(indent+delta_indent,' ') << "average fill: "
305 << (1.0*cum_fill)/hashtabsize
306 << " (should be equal to " << (1.0*seq.size())/hashtabsize << ")" << std::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))->setflag(status_flags::dynallocated | status_flags::evaluated);
351 ex expairseq::evalf(int level) const
353 return thisexpairseq(evalfchildren(level),overall_coeff.evalf(level-1));
356 ex expairseq::normal(lst &sym_lst, lst &repl_lst, int level) const
358 ex n = thisexpairseq(normalchildren(level),overall_coeff);
359 return n.bp->basic::normal(sym_lst,repl_lst,level);
362 ex expairseq::subs(const lst & ls, const lst & lr) const
364 epvector * vp=subschildren(ls,lr);
368 return thisexpairseq(vp,overall_coeff);
373 /** Implementation of ex::diff() for an expairseq. It differentiates all elements of the
376 ex expairseq::derivative(const symbol & s) const
378 return thisexpairseq(diffchildren(s),overall_coeff);
381 int expairseq::compare_same_type(const basic & other) const
383 GINAC_ASSERT(is_of_type(other, expairseq));
384 const expairseq & o = static_cast<const expairseq &>(const_cast<basic &>(other));
388 // compare number of elements
389 if (seq.size() != o.seq.size()) {
390 return (seq.size()<o.seq.size()) ? -1 : 1;
393 // compare overall_coeff
394 cmpval = overall_coeff.compare(o.overall_coeff);
395 if (cmpval!=0) return cmpval;
397 //if (seq.size()==0) return 0; // empty expairseq's are equal
399 #ifdef EXPAIRSEQ_USE_HASHTAB
400 GINAC_ASSERT(hashtabsize==o.hashtabsize);
401 if (hashtabsize==0) {
402 #endif // def EXPAIRSEQ_USE_HASHTAB
403 epvector::const_iterator cit1 = seq.begin();
404 epvector::const_iterator cit2 = o.seq.begin();
405 epvector::const_iterator last1 = seq.end();
406 epvector::const_iterator last2 = o.seq.end();
408 for (; (cit1!=last1)&&(cit2!=last2); ++cit1, ++cit2) {
409 cmpval = (*cit1).compare(*cit2);
410 if (cmpval!=0) return cmpval;
413 GINAC_ASSERT(cit1==last1);
414 GINAC_ASSERT(cit2==last2);
417 #ifdef EXPAIRSEQ_USE_HASHTAB
420 // compare number of elements in each hashtab entry
421 for (unsigned i=0; i<hashtabsize; ++i) {
422 unsigned cursize=hashtab[i].size();
423 if (cursize != o.hashtab[i].size()) {
424 return (cursize < o.hashtab[i].size()) ? -1 : 1;
428 // compare individual (sorted) hashtab entries
429 for (unsigned i=0; i<hashtabsize; ++i) {
430 unsigned sz=hashtab[i].size();
432 const epplist & eppl1=hashtab[i];
433 const epplist & eppl2=o.hashtab[i];
434 epplist::const_iterator it1=eppl1.begin();
435 epplist::const_iterator it2=eppl2.begin();
436 while (it1!=eppl1.end()) {
437 cmpval=(*(*it1)).compare(*(*it2));
438 if (cmpval!=0) return cmpval;
446 #endif // def EXPAIRSEQ_USE_HASHTAB
449 bool expairseq::is_equal_same_type(const basic & other) const
451 const expairseq & o=dynamic_cast<const expairseq &>(const_cast<basic &>(other));
453 // compare number of elements
454 if (seq.size() != o.seq.size()) return false;
456 // compare overall_coeff
457 if (!overall_coeff.is_equal(o.overall_coeff)) return false;
459 #ifdef EXPAIRSEQ_USE_HASHTAB
460 // compare number of elements in each hashtab entry
461 if (hashtabsize!=o.hashtabsize) {
462 cout << "this:" << std::endl;
464 cout << "other:" << std::endl;
465 other.printtree(cout,0);
468 GINAC_ASSERT(hashtabsize==o.hashtabsize);
470 if (hashtabsize==0) {
471 #endif // def EXPAIRSEQ_USE_HASHTAB
472 epvector::const_iterator cit1=seq.begin();
473 epvector::const_iterator cit2=o.seq.begin();
474 epvector::const_iterator last1=seq.end();
476 while (cit1!=last1) {
477 if (!(*cit1).is_equal(*cit2)) return false;
483 #ifdef EXPAIRSEQ_USE_HASHTAB
486 for (unsigned i=0; i<hashtabsize; ++i) {
487 if (hashtab[i].size() != o.hashtab[i].size()) return false;
490 // compare individual sorted hashtab entries
491 for (unsigned i=0; i<hashtabsize; ++i) {
492 unsigned sz=hashtab[i].size();
494 const epplist & eppl1=hashtab[i];
495 const epplist & eppl2=o.hashtab[i];
496 epplist::const_iterator it1=eppl1.begin();
497 epplist::const_iterator it2=eppl2.begin();
498 while (it1!=eppl1.end()) {
499 if (!(*(*it1)).is_equal(*(*it2))) return false;
507 #endif // def EXPAIRSEQ_USE_HASHTAB
510 unsigned expairseq::return_type(void) const
512 return return_types::noncommutative_composite;
515 unsigned expairseq::calchash(void) const
517 unsigned v=golden_ratio_hash(tinfo());
518 epvector::const_iterator last=seq.end();
519 for (epvector::const_iterator cit=seq.begin(); cit!=last; ++cit) {
520 #ifndef EXPAIRSEQ_USE_HASHTAB
521 v=rotate_left_31(v); // rotation would spoil commutativity
522 #endif // ndef EXPAIRSEQ_USE_HASHTAB
523 v ^= (*cit).rest.gethash();
526 v ^= overall_coeff.gethash();
529 // store calculated hash value only if object is already evaluated
530 if (flags & status_flags::evaluated) {
531 setflag(status_flags::hash_calculated);
538 ex expairseq::expand(unsigned options) const
540 epvector * vp = expandchildren(options);
544 return thisexpairseq(vp,overall_coeff);
548 // new virtual functions which can be overridden by derived classes
553 ex expairseq::thisexpairseq(const epvector & v, const ex & oc) const
555 return expairseq(v,oc);
558 ex expairseq::thisexpairseq(epvector * vp, const ex & oc) const
560 return expairseq(vp,oc);
563 void expairseq::printpair(std::ostream & os, const expair & p, unsigned upper_precedence) const
566 p.rest.bp->print(os,precedence);
568 p.coeff.bp->print(os,precedence);
572 void expairseq::printseq(std::ostream & os, char delim,
573 unsigned this_precedence,
574 unsigned upper_precedence) const
576 if (this_precedence<=upper_precedence) os << "(";
577 epvector::const_iterator it,it_last;
580 for (it=seq.begin(); it!=it_last; ++it) {
581 printpair(os,*it,this_precedence);
584 printpair(os,*it,this_precedence);
585 if (!overall_coeff.is_equal(default_overall_coeff())) {
586 os << delim << overall_coeff;
588 if (this_precedence<=upper_precedence) os << ")";
591 expair expairseq::split_ex_to_pair(const ex & e) const
593 return expair(e,_ex1());
596 expair expairseq::combine_ex_with_coeff_to_pair(const ex & e,
599 GINAC_ASSERT(is_ex_exactly_of_type(c,numeric));
604 expair expairseq::combine_pair_with_coeff_to_pair(const expair & p,
607 GINAC_ASSERT(is_ex_exactly_of_type(p.coeff,numeric));
608 GINAC_ASSERT(is_ex_exactly_of_type(c,numeric));
610 return expair(p.rest,ex_to_numeric(p.coeff).mul_dyn(ex_to_numeric(c)));
613 ex expairseq::recombine_pair_to_ex(const expair & p) const
615 return lst(p.rest,p.coeff);
618 bool expairseq::expair_needs_further_processing(epp it)
623 ex expairseq::default_overall_coeff(void) const
628 void expairseq::combine_overall_coeff(const ex & c)
630 GINAC_ASSERT(is_ex_exactly_of_type(overall_coeff,numeric));
631 GINAC_ASSERT(is_ex_exactly_of_type(c,numeric));
632 overall_coeff = ex_to_numeric(overall_coeff).add_dyn(ex_to_numeric(c));
635 void expairseq::combine_overall_coeff(const ex & c1, const ex & c2)
637 GINAC_ASSERT(is_ex_exactly_of_type(overall_coeff,numeric));
638 GINAC_ASSERT(is_ex_exactly_of_type(c1,numeric));
639 GINAC_ASSERT(is_ex_exactly_of_type(c2,numeric));
640 overall_coeff = ex_to_numeric(overall_coeff).
641 add_dyn(ex_to_numeric(c1).mul(ex_to_numeric(c2)));
644 bool expairseq::can_make_flat(const expair & p) const
651 // non-virtual functions in this class
654 void expairseq::construct_from_2_ex_via_exvector(const ex & lh, const ex & rh)
660 construct_from_exvector(v);
661 #ifdef EXPAIRSEQ_USE_HASHTAB
662 GINAC_ASSERT((hashtabsize==0)||(hashtabsize>=minhashtabsize));
663 GINAC_ASSERT(hashtabsize==calc_hashtabsize(seq.size()));
664 #endif // def EXPAIRSEQ_USE_HASHTAB
667 void expairseq::construct_from_2_ex(const ex & lh, const ex & rh)
669 if (lh.bp->tinfo()==tinfo()) {
670 if (rh.bp->tinfo()==tinfo()) {
671 #ifdef EXPAIRSEQ_USE_HASHTAB
672 unsigned totalsize = ex_to_expairseq(lh).seq.size() +
673 ex_to_expairseq(rh).seq.size();
674 if (calc_hashtabsize(totalsize)!=0) {
675 construct_from_2_ex_via_exvector(lh,rh);
677 #endif // def EXPAIRSEQ_USE_HASHTAB
678 construct_from_2_expairseq(ex_to_expairseq(lh),
679 ex_to_expairseq(rh));
680 #ifdef EXPAIRSEQ_USE_HASHTAB
682 #endif // def EXPAIRSEQ_USE_HASHTAB
685 #ifdef EXPAIRSEQ_USE_HASHTAB
686 unsigned totalsize=ex_to_expairseq(lh).seq.size()+1;
687 if (calc_hashtabsize(totalsize) != 0) {
688 construct_from_2_ex_via_exvector(lh, rh);
690 #endif // def EXPAIRSEQ_USE_HASHTAB
691 construct_from_expairseq_ex(ex_to_expairseq(lh), rh);
692 #ifdef EXPAIRSEQ_USE_HASHTAB
694 #endif // def EXPAIRSEQ_USE_HASHTAB
697 } else if (rh.bp->tinfo()==tinfo()) {
698 #ifdef EXPAIRSEQ_USE_HASHTAB
699 unsigned totalsize=ex_to_expairseq(rh).seq.size()+1;
700 if (calc_hashtabsize(totalsize)!=0) {
701 construct_from_2_ex_via_exvector(lh,rh);
703 #endif // def EXPAIRSEQ_USE_HASHTAB
704 construct_from_expairseq_ex(ex_to_expairseq(rh),lh);
705 #ifdef EXPAIRSEQ_USE_HASHTAB
707 #endif // def EXPAIRSEQ_USE_HASHTAB
711 #ifdef EXPAIRSEQ_USE_HASHTAB
712 if (calc_hashtabsize(2)!=0) {
713 construct_from_2_ex_via_exvector(lh,rh);
717 #endif // def EXPAIRSEQ_USE_HASHTAB
719 if (is_ex_exactly_of_type(lh,numeric)) {
720 if (is_ex_exactly_of_type(rh,numeric)) {
721 combine_overall_coeff(lh);
722 combine_overall_coeff(rh);
724 combine_overall_coeff(lh);
725 seq.push_back(split_ex_to_pair(rh));
728 if (is_ex_exactly_of_type(rh,numeric)) {
729 combine_overall_coeff(rh);
730 seq.push_back(split_ex_to_pair(lh));
732 expair p1=split_ex_to_pair(lh);
733 expair p2=split_ex_to_pair(rh);
735 int cmpval=p1.rest.compare(p2.rest);
737 p1.coeff=ex_to_numeric(p1.coeff).add_dyn(ex_to_numeric(p2.coeff));
738 if (!ex_to_numeric(p1.coeff).is_zero()) {
739 // no further processing is necessary, since this
740 // one element will usually be recombined in eval()
757 void expairseq::construct_from_2_expairseq(const expairseq & s1,
758 const expairseq & s2)
760 combine_overall_coeff(s1.overall_coeff);
761 combine_overall_coeff(s2.overall_coeff);
763 epvector::const_iterator first1=s1.seq.begin();
764 epvector::const_iterator last1=s1.seq.end();
765 epvector::const_iterator first2=s2.seq.begin();
766 epvector::const_iterator last2=s2.seq.end();
768 seq.reserve(s1.seq.size()+s2.seq.size());
770 bool needs_further_processing=false;
772 while (first1!=last1 && first2!=last2) {
773 int cmpval=(*first1).rest.compare((*first2).rest);
776 const numeric & newcoeff = ex_to_numeric((*first1).coeff).
777 add(ex_to_numeric((*first2).coeff));
778 if (!newcoeff.is_zero()) {
779 seq.push_back(expair((*first1).rest,newcoeff));
780 if (expair_needs_further_processing(seq.end()-1)) {
781 needs_further_processing = true;
786 } else if (cmpval<0) {
787 seq.push_back(*first1);
790 seq.push_back(*first2);
795 while (first1!=last1) {
796 seq.push_back(*first1);
799 while (first2!=last2) {
800 seq.push_back(*first2);
804 if (needs_further_processing) {
807 construct_from_epvector(v);
811 void expairseq::construct_from_expairseq_ex(const expairseq & s,
814 combine_overall_coeff(s.overall_coeff);
815 if (is_ex_exactly_of_type(e,numeric)) {
816 combine_overall_coeff(e);
821 epvector::const_iterator first=s.seq.begin();
822 epvector::const_iterator last=s.seq.end();
823 expair p=split_ex_to_pair(e);
825 seq.reserve(s.seq.size()+1);
828 bool needs_further_processing=false;
830 // merge p into s.seq
831 while (first!=last) {
832 int cmpval=(*first).rest.compare(p.rest);
835 const numeric & newcoeff = ex_to_numeric((*first).coeff).
836 add(ex_to_numeric(p.coeff));
837 if (!newcoeff.is_zero()) {
838 seq.push_back(expair((*first).rest,newcoeff));
839 if (expair_needs_further_processing(seq.end()-1)) {
840 needs_further_processing = true;
846 } else if (cmpval<0) {
847 seq.push_back(*first);
857 // while loop exited because p was pushed, now push rest of s.seq
858 while (first!=last) {
859 seq.push_back(*first);
863 // while loop exited because s.seq was pushed, now push p
867 if (needs_further_processing) {
870 construct_from_epvector(v);
874 void expairseq::construct_from_exvector(const exvector & v)
876 // simplifications: +(a,+(b,c),d) -> +(a,b,c,d) (associativity)
877 // +(d,b,c,a) -> +(a,b,c,d) (canonicalization)
878 // +(...,x,*(x,c1),*(x,c2)) -> +(...,*(x,1+c1+c2)) (c1, c2 numeric())
879 // (same for (+,*) -> (*,^)
882 #ifdef EXPAIRSEQ_USE_HASHTAB
883 combine_same_terms();
886 combine_same_terms_sorted_seq();
887 #endif // def EXPAIRSEQ_USE_HASHTAB
890 void expairseq::construct_from_epvector(const epvector & v)
892 // simplifications: +(a,+(b,c),d) -> +(a,b,c,d) (associativity)
893 // +(d,b,c,a) -> +(a,b,c,d) (canonicalization)
894 // +(...,x,*(x,c1),*(x,c2)) -> +(...,*(x,1+c1+c2)) (c1, c2 numeric())
895 // (same for (+,*) -> (*,^)
898 #ifdef EXPAIRSEQ_USE_HASHTAB
899 combine_same_terms();
902 combine_same_terms_sorted_seq();
903 #endif // def EXPAIRSEQ_USE_HASHTAB
906 void expairseq::make_flat(const exvector & v)
908 exvector::const_iterator cit, citend = v.end();
910 // count number of operands which are of same expairseq derived type
911 // and their cumulative number of operands
915 while (cit!=citend) {
916 if (cit->bp->tinfo()==tinfo()) {
918 noperands+=ex_to_expairseq(*cit).seq.size();
923 // reserve seq and coeffseq which will hold all operands
924 seq.reserve(v.size()+noperands-nexpairseqs);
926 // copy elements and split off numerical part
928 while (cit!=citend) {
929 if (cit->bp->tinfo()==tinfo()) {
930 const expairseq & subseqref=ex_to_expairseq(*cit);
931 combine_overall_coeff(subseqref.overall_coeff);
932 epvector::const_iterator cit_s=subseqref.seq.begin();
933 while (cit_s!=subseqref.seq.end()) {
934 seq.push_back(*cit_s);
938 if (is_ex_exactly_of_type(*cit,numeric)) {
939 combine_overall_coeff(*cit);
941 seq.push_back(split_ex_to_pair(*cit));
948 cout << "after make flat" << std::endl;
949 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
950 (*cit).printraw(cout);
956 void expairseq::make_flat(const epvector & v)
958 epvector::const_iterator cit, citend = v.end();
960 // count number of operands which are of same expairseq derived type
961 // and their cumulative number of operands
966 while (cit!=citend) {
967 if (cit->rest.bp->tinfo()==tinfo()) {
969 noperands += ex_to_expairseq((*cit).rest).seq.size();
974 // reserve seq and coeffseq which will hold all operands
975 seq.reserve(v.size()+noperands-nexpairseqs);
977 // copy elements and split off numerical part
979 while (cit!=citend) {
980 if ((cit->rest.bp->tinfo()==tinfo())&&can_make_flat(*cit)) {
981 const expairseq & subseqref=ex_to_expairseq((*cit).rest);
982 combine_overall_coeff(ex_to_numeric(subseqref.overall_coeff),
983 ex_to_numeric((*cit).coeff));
984 epvector::const_iterator cit_s=subseqref.seq.begin();
985 while (cit_s!=subseqref.seq.end()) {
986 seq.push_back(expair((*cit_s).rest,
987 ex_to_numeric((*cit_s).coeff).mul_dyn(ex_to_numeric((*cit).coeff))));
988 //seq.push_back(combine_pair_with_coeff_to_pair(*cit_s,
993 if ((*cit).is_numeric_with_coeff_1()) {
994 combine_overall_coeff((*cit).rest);
995 //if (is_ex_exactly_of_type((*cit).rest,numeric)) {
996 // combine_overall_coeff(recombine_pair_to_ex(*cit));
1005 epvector * expairseq::bubblesort(epvector::iterator itbegin, epvector::iterator itend)
1007 unsigned n=itend-itbegin;
1009 epvector * sp=new epvector;
1012 epvector::iterator last=itend-1;
1013 for (epvector::iterator it1=itbegin; it1!=last; ++it1) {
1014 for (epvector::iterator it2=it1+1; it2!=itend; ++it2) {
1015 if ((*it2).rest.compare((*it1).rest)<0) {
1019 sp->push_back(*it1);
1021 sp->push_back(*last);
1025 epvector * expairseq::mergesort(epvector::iterator itbegin, epvector::iterator itend)
1027 unsigned n=itend-itbegin;
1030 epvector * sp=new epvector;
1031 sp->push_back(*itbegin);
1035 if (n<16) return bubblesort(itbegin, itend);
1038 epvector * s1p=mergesort(itbegin, itbegin+m);
1039 epvector * s2p=mergesort(itbegin+m, itend);
1041 epvector * sp=new epvector;
1042 sp->reserve(s1p->size()+s2p->size());
1044 epvector::iterator first1=s1p->begin();
1045 epvector::iterator last1=s1p->end();
1047 epvector::iterator first2=s2p->begin();
1048 epvector::iterator last2=s2p->end();
1050 while (first1 != last1 && first2 != last2) {
1051 if ((*first1).rest.compare((*first2).rest)<0) {
1052 sp->push_back(*first1);
1055 sp->push_back(*first2);
1060 if (first1 != last1) {
1061 while (first1 != last1) {
1062 sp->push_back(*first1);
1066 while (first2 != last2) {
1067 sp->push_back(*first2);
1079 void expairseq::canonicalize(void)
1082 sort(seq.begin(),seq.end(),expair_is_less());
1084 sort(seq.begin(),seq.end(),expair_is_less_old());
1086 if (is_ex_exactly_of_type((*(seq.begin())).rest,numeric)) {
1087 sort(seq.begin(),seq.end(),expair_is_less());
1089 epvector::iterator last_numeric=seq.end();
1092 } while (is_ex_exactly_of_type((*last_numeric).rest,numeric));
1094 sort(last_numeric,seq.end(),expair_is_less());
1100 epvector * sorted_seqp=mergesort(seq.begin(),seq.end());
1101 epvector::iterator last=sorted_seqp->end();
1102 epvector::iterator it2=seq.begin();
1103 for (epvector::iterator it1=sorted_seqp->begin(); it1!=last; ++it1, ++it2) {
1110 cout << "after canonicalize" << std::endl;
1111 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
1112 (*cit).printraw(cout);
1119 void expairseq::combine_same_terms_sorted_seq(void)
1121 bool needs_further_processing=false;
1123 // combine same terms, drop term with coeff 0
1125 epvector::iterator itin1=seq.begin();
1126 epvector::iterator itin2=itin1+1;
1127 epvector::iterator itout=itin1;
1128 epvector::iterator last=seq.end();
1129 // must_copy will be set to true the first time some combination is possible
1130 // from then on the sequence has changed and must be compacted
1131 bool must_copy=false;
1132 while (itin2!=last) {
1133 if ((*itin1).rest.compare((*itin2).rest)==0) {
1134 (*itin1).coeff = ex_to_numeric((*itin1).coeff).
1135 add_dyn(ex_to_numeric((*itin2).coeff));
1136 if (expair_needs_further_processing(itin1)) {
1137 needs_further_processing = true;
1141 if (!ex_to_numeric((*itin1).coeff).is_zero()) {
1151 if (!ex_to_numeric((*itin1).coeff).is_zero()) {
1158 seq.erase(itout,last);
1163 cout << "after combine" << std::endl;
1164 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
1165 (*cit).printraw(cout);
1171 if (needs_further_processing) {
1174 construct_from_epvector(v);
1178 #ifdef EXPAIRSEQ_USE_HASHTAB
1180 unsigned expairseq::calc_hashtabsize(unsigned sz) const
1183 unsigned nearest_power_of_2 = 1 << log2(sz);
1184 // if (nearest_power_of_2 < maxhashtabsize/hashtabfactor) {
1185 // size=nearest_power_of_2*hashtabfactor;
1186 size=nearest_power_of_2/hashtabfactor;
1187 if (size<minhashtabsize) return 0;
1188 GINAC_ASSERT(hashtabsize<=0x8000000U); // really max size due to 31 bit hashing
1189 // hashtabsize must be a power of 2
1190 GINAC_ASSERT((1U << log2(size))==size);
1194 unsigned expairseq::calc_hashindex(const ex & e) const
1196 // calculate hashindex
1197 unsigned hash=e.gethash();
1199 if (is_a_numeric_hash(hash)) {
1200 hashindex = hashmask;
1202 hashindex = hash & hashmask;
1203 // last hashtab entry is reserved for numerics
1204 if (hashindex==hashmask) hashindex = 0;
1206 GINAC_ASSERT(hashindex>=0);
1207 GINAC_ASSERT((hashindex<hashtabsize)||(hashtabsize==0));
1211 void expairseq::shrink_hashtab(void)
1213 unsigned new_hashtabsize;
1214 while (hashtabsize!=(new_hashtabsize=calc_hashtabsize(seq.size()))) {
1215 GINAC_ASSERT(new_hashtabsize<hashtabsize);
1216 if (new_hashtabsize==0) {
1223 // shrink by a factor of 2
1224 unsigned half_hashtabsize=hashtabsize/2;
1225 for (unsigned i=0; i<half_hashtabsize-1; ++i)
1226 hashtab[i].merge(hashtab[i+half_hashtabsize],epp_is_less());
1227 // special treatment for numeric hashes
1228 hashtab[0].merge(hashtab[half_hashtabsize-1],epp_is_less());
1229 hashtab[half_hashtabsize-1] = hashtab[hashtabsize-1];
1230 hashtab.resize(half_hashtabsize);
1231 hashtabsize = half_hashtabsize;
1232 hashmask = hashtabsize-1;
1236 void expairseq::remove_hashtab_entry(epvector::const_iterator element)
1238 if (hashtabsize==0) return; // nothing to do
1240 // calculate hashindex of element to be deleted
1241 unsigned hashindex = calc_hashindex((*element).rest);
1243 // find it in hashtab and remove it
1244 epplist & eppl = hashtab[hashindex];
1245 epplist::iterator epplit = eppl.begin();
1246 bool erased = false;
1247 while (epplit!=eppl.end()) {
1248 if (*epplit == element) {
1257 cout << "tried to erase " << element-seq.begin() << std::endl;
1258 cout << "size " << seq.end()-seq.begin() << std::endl;
1260 unsigned hashindex = calc_hashindex((*element).rest);
1261 epplist & eppl = hashtab[hashindex];
1262 epplist::iterator epplit=eppl.begin();
1264 while (epplit!=eppl.end()) {
1265 if (*epplit == element) {
1272 GINAC_ASSERT(erased);
1274 GINAC_ASSERT(erased);
1277 void expairseq::move_hashtab_entry(epvector::const_iterator oldpos,
1278 epvector::iterator newpos)
1280 GINAC_ASSERT(hashtabsize!=0);
1282 // calculate hashindex of element which was moved
1283 unsigned hashindex=calc_hashindex((*newpos).rest);
1285 // find it in hashtab and modify it
1286 epplist & eppl = hashtab[hashindex];
1287 epplist::iterator epplit=eppl.begin();
1288 while (epplit!=eppl.end()) {
1289 if (*epplit == oldpos) {
1295 GINAC_ASSERT(epplit!=eppl.end());
1298 void expairseq::sorted_insert(epplist & eppl, epp elem)
1300 epplist::iterator current = eppl.begin();
1301 while ((current!=eppl.end())&&((*(*current)).is_less(*elem))) {
1304 eppl.insert(current,elem);
1307 void expairseq::build_hashtab_and_combine(epvector::iterator & first_numeric,
1308 epvector::iterator & last_non_zero,
1309 vector<bool> & touched,
1310 unsigned & number_of_zeroes)
1312 epp current=seq.begin();
1314 while (current!=first_numeric) {
1315 if (is_ex_exactly_of_type((*current).rest,numeric)) {
1317 iter_swap(current,first_numeric);
1319 // calculate hashindex
1320 unsigned currenthashindex=calc_hashindex((*current).rest);
1322 // test if there is already a matching expair in the hashtab-list
1323 epplist & eppl=hashtab[currenthashindex];
1324 epplist::iterator epplit=eppl.begin();
1325 while (epplit!=eppl.end()) {
1326 if ((*current).rest.is_equal((*(*epplit)).rest)) break;
1329 if (epplit==eppl.end()) {
1330 // no matching expair found, append this to end of list
1331 sorted_insert(eppl,current);
1334 // epplit points to a matching expair, combine it with current
1335 (*(*epplit)).coeff = ex_to_numeric((*(*epplit)).coeff).
1336 add_dyn(ex_to_numeric((*current).coeff));
1338 // move obsolete current expair to end by swapping with last_non_zero element
1339 // if this was a numeric, it is swapped with the expair before first_numeric
1340 iter_swap(current,last_non_zero);
1342 if (first_numeric!=last_non_zero) iter_swap(first_numeric,current);
1345 // test if combined term has coeff 0 and can be removed is done later
1346 touched[(*epplit)-seq.begin()]=true;
1352 void expairseq::drop_coeff_0_terms(epvector::iterator & first_numeric,
1353 epvector::iterator & last_non_zero,
1354 vector<bool> & touched,
1355 unsigned & number_of_zeroes)
1357 // move terms with coeff 0 to end and remove them from hashtab
1358 // check only those elements which have been touched
1359 epp current=seq.begin();
1361 while (current!=first_numeric) {
1365 } else if (!ex_to_numeric((*current).coeff).is_equal(_num0())) {
1369 remove_hashtab_entry(current);
1371 // move element to the end, unless it is already at the end
1372 if (current!=last_non_zero) {
1373 iter_swap(current,last_non_zero);
1375 bool numeric_swapped=first_numeric!=last_non_zero;
1376 if (numeric_swapped) iter_swap(first_numeric,current);
1377 epvector::iterator changed_entry;
1379 if (numeric_swapped) {
1380 changed_entry=first_numeric;
1382 changed_entry=last_non_zero;
1388 if (first_numeric!=current) {
1390 // change entry in hashtab which referred to first_numeric or last_non_zero to current
1391 move_hashtab_entry(changed_entry,current);
1392 touched[current-seq.begin()]=touched[changed_entry-seq.begin()];
1401 GINAC_ASSERT(i==current-seq.begin());
1404 bool expairseq::has_coeff_0(void) const
1406 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
1407 if ((*cit).coeff.is_equal(_ex0())) {
1414 void expairseq::add_numerics_to_hashtab(epvector::iterator first_numeric,
1415 epvector::const_iterator last_non_zero)
1417 if (first_numeric==seq.end()) return; // no numerics
1419 epvector::iterator current=first_numeric;
1420 epvector::const_iterator last=last_non_zero+1;
1421 while (current!=last) {
1422 sorted_insert(hashtab[hashmask],current);
1427 void expairseq::combine_same_terms(void)
1429 // combine same terms, drop term with coeff 0, move numerics to end
1431 // calculate size of hashtab
1432 hashtabsize = calc_hashtabsize(seq.size());
1434 // hashtabsize is a power of 2
1435 hashmask = hashtabsize-1;
1439 hashtab.resize(hashtabsize);
1441 if (hashtabsize==0) {
1443 combine_same_terms_sorted_seq();
1444 GINAC_ASSERT(!has_coeff_0());
1448 // iterate through seq, move numerics to end,
1449 // fill hashtab and combine same terms
1450 epvector::iterator first_numeric=seq.end();
1451 epvector::iterator last_non_zero=seq.end()-1;
1453 vector<bool> touched;
1454 touched.reserve(seq.size());
1455 for (unsigned i=0; i<seq.size(); ++i) touched[i]=false;
1457 unsigned number_of_zeroes = 0;
1459 GINAC_ASSERT(!has_coeff_0());
1460 build_hashtab_and_combine(first_numeric,last_non_zero,touched,number_of_zeroes);
1462 cout << "in combine:" << std::endl;
1464 cout << "size=" << seq.end() - seq.begin() << std::endl;
1465 cout << "first_numeric=" << first_numeric - seq.begin() << std::endl;
1466 cout << "last_non_zero=" << last_non_zero - seq.begin() << std::endl;
1467 for (unsigned i=0; i<seq.size(); ++i) {
1468 if (touched[i]) cout << i << " is touched" << std::endl;
1470 cout << "end in combine" << std::endl;
1473 // there should not be any terms with coeff 0 from the beginning,
1474 // so it should be safe to skip this step
1475 if (number_of_zeroes!=0) {
1476 drop_coeff_0_terms(first_numeric,last_non_zero,touched,number_of_zeroes);
1478 cout << "in combine after drop:" << std::endl;
1480 cout << "size=" << seq.end() - seq.begin() << std::endl;
1481 cout << "first_numeric=" << first_numeric - seq.begin() << std::endl;
1482 cout << "last_non_zero=" << last_non_zero - seq.begin() << std::endl;
1483 for (unsigned i=0; i<seq.size(); ++i) {
1484 if (touched[i]) cout << i << " is touched" << std::endl;
1486 cout << "end in combine after drop" << std::endl;
1490 add_numerics_to_hashtab(first_numeric,last_non_zero);
1492 // pop zero elements
1493 for (unsigned i=0; i<number_of_zeroes; ++i) {
1497 // shrink hashtabsize to calculated value
1498 GINAC_ASSERT(!has_coeff_0());
1502 GINAC_ASSERT(!has_coeff_0());
1505 #endif // def EXPAIRSEQ_USE_HASHTAB
1507 bool expairseq::is_canonical() const
1509 if (seq.size()<=1) return 1;
1511 #ifdef EXPAIRSEQ_USE_HASHTAB
1512 if (hashtabsize>0) return 1; // not canoncalized
1513 #endif // def EXPAIRSEQ_USE_HASHTAB
1515 epvector::const_iterator it = seq.begin();
1516 epvector::const_iterator it_last = it;
1517 for (++it; it!=seq.end(); it_last=it, ++it) {
1518 if (!((*it_last).is_less(*it)||(*it_last).is_equal(*it))) {
1519 if (!is_ex_exactly_of_type((*it_last).rest,numeric)||
1520 !is_ex_exactly_of_type((*it).rest,numeric)) {
1521 // double test makes it easier to set a breakpoint...
1522 if (!is_ex_exactly_of_type((*it_last).rest,numeric)||
1523 !is_ex_exactly_of_type((*it).rest,numeric)) {
1524 printpair(std::clog,*it_last,0);
1526 printpair(std::clog,*it,0);
1528 std::clog << "pair1:" << std::endl;
1529 (*it_last).rest.printtree(std::clog);
1530 (*it_last).coeff.printtree(std::clog);
1531 std::clog << "pair2:" << std::endl;
1532 (*it).rest.printtree(std::clog);
1533 (*it).coeff.printtree(std::clog);
1542 epvector * expairseq::expandchildren(unsigned options) const
1544 epvector::const_iterator last = seq.end();
1545 epvector::const_iterator cit = seq.begin();
1547 const ex & expanded_ex=(*cit).rest.expand(options);
1548 if (!are_ex_trivially_equal((*cit).rest,expanded_ex)) {
1550 // something changed, copy seq, eval and return it
1551 epvector *s=new epvector;
1552 s->reserve(seq.size());
1554 // copy parts of seq which are known not to have changed
1555 epvector::const_iterator cit2 = seq.begin();
1557 s->push_back(*cit2);
1560 // copy first changed element
1561 s->push_back(combine_ex_with_coeff_to_pair(expanded_ex,
1565 while (cit2!=last) {
1566 s->push_back(combine_ex_with_coeff_to_pair((*cit2).rest.expand(options),
1575 return 0; // nothing has changed
1578 epvector * expairseq::evalchildren(int level) const
1580 // returns a NULL pointer if nothing had to be evaluated
1581 // returns a pointer to a newly created epvector otherwise
1582 // (which has to be deleted somewhere else)
1587 if (level == -max_recursion_level) {
1588 throw(std::runtime_error("max recursion level reached"));
1592 epvector::const_iterator last=seq.end();
1593 epvector::const_iterator cit=seq.begin();
1595 const ex & evaled_ex=(*cit).rest.eval(level);
1596 if (!are_ex_trivially_equal((*cit).rest,evaled_ex)) {
1598 // something changed, copy seq, eval and return it
1599 epvector *s = new epvector;
1600 s->reserve(seq.size());
1602 // copy parts of seq which are known not to have changed
1603 epvector::const_iterator cit2=seq.begin();
1605 s->push_back(*cit2);
1608 // copy first changed element
1609 s->push_back(combine_ex_with_coeff_to_pair(evaled_ex,
1613 while (cit2!=last) {
1614 s->push_back(combine_ex_with_coeff_to_pair((*cit2).rest.eval(level),
1623 return 0; // nothing has changed
1626 epvector expairseq::evalfchildren(int level) const
1631 if (level==-max_recursion_level)
1632 throw(std::runtime_error("max recursion level reached"));
1635 s.reserve(seq.size());
1638 for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
1639 s.push_back(combine_ex_with_coeff_to_pair((*it).rest.evalf(level),
1640 (*it).coeff.evalf(level)));
1645 epvector expairseq::normalchildren(int level) const
1650 if (level == -max_recursion_level)
1651 throw(std::runtime_error("max recursion level reached"));
1654 s.reserve(seq.size());
1657 for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
1658 s.push_back(combine_ex_with_coeff_to_pair((*it).rest.normal(level),
1664 epvector expairseq::diffchildren(const symbol & y) const
1667 s.reserve(seq.size());
1669 for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
1670 s.push_back(combine_ex_with_coeff_to_pair((*it).rest.diff(y),
1676 epvector * expairseq::subschildren(const lst & ls, const lst & lr) const
1678 // returns a NULL pointer if nothing had to be substituted
1679 // returns a pointer to a newly created epvector otherwise
1680 // (which has to be deleted somewhere else)
1681 GINAC_ASSERT(ls.nops()==lr.nops());
1683 epvector::const_iterator last=seq.end();
1684 epvector::const_iterator cit=seq.begin();
1686 const ex & subsed_ex=(*cit).rest.subs(ls,lr);
1687 if (!are_ex_trivially_equal((*cit).rest,subsed_ex)) {
1689 // something changed, copy seq, subs and return it
1690 epvector *s = new epvector;
1691 s->reserve(seq.size());
1693 // copy parts of seq which are known not to have changed
1694 epvector::const_iterator cit2=seq.begin();
1696 s->push_back(*cit2);
1699 // copy first changed element
1700 s->push_back(combine_ex_with_coeff_to_pair(subsed_ex,
1704 while (cit2!=last) {
1705 s->push_back(combine_ex_with_coeff_to_pair((*cit2).rest.subs(ls,lr),
1714 return 0; // nothing has changed
1718 // static member variables
1723 unsigned expairseq::precedence=10;
1725 #ifdef EXPAIRSEQ_USE_HASHTAB
1726 unsigned expairseq::maxhashtabsize=0x4000000U;
1727 unsigned expairseq::minhashtabsize=0x1000U;
1728 unsigned expairseq::hashtabfactor=1;
1729 #endif // def EXPAIRSEQ_USE_HASHTAB
1731 #ifndef NO_NAMESPACE_GINAC
1732 } // namespace GiNaC
1733 #endif // ndef NO_NAMESPACE_GINAC