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"
34 #if EXPAIRSEQ_USE_HASHTAB
36 #endif // EXPAIRSEQ_USE_HASHTAB
40 GINAC_IMPLEMENT_REGISTERED_CLASS_NO_CTORS(expairseq, basic)
49 bool operator()(const epp &lh, const epp &rh) const
51 return (*lh).is_less(*rh);
56 // default ctor, dtor, copy ctor assignment operator and helpers
61 expairseq::expairseq(const expairseq &other)
63 debugmsg("expairseq copy ctor",LOGLEVEL_CONSTRUCT);
67 const expairseq &expairseq::operator=(const expairseq &other)
69 debugmsg("expairseq operator=",LOGLEVEL_ASSIGNMENT);
79 /** For use by copy ctor and assignment operator. */
80 void expairseq::copy(const expairseq &other)
82 inherited::copy(other);
84 overall_coeff = other.overall_coeff;
85 #if 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 // EXPAIRSEQ_USE_HASHTAB
105 DEFAULT_DESTROY(expairseq)
111 expairseq::expairseq(const ex &lh, const ex &rh) : inherited(TINFO_expairseq)
113 debugmsg("expairseq ctor from ex,ex",LOGLEVEL_CONSTRUCT);
114 construct_from_2_ex(lh,rh);
115 GINAC_ASSERT(is_canonical());
118 expairseq::expairseq(const exvector &v) : inherited(TINFO_expairseq)
120 debugmsg("expairseq ctor from exvector",LOGLEVEL_CONSTRUCT);
121 construct_from_exvector(v);
122 GINAC_ASSERT(is_canonical());
125 expairseq::expairseq(const epvector &v, const ex &oc)
126 : inherited(TINFO_expairseq), overall_coeff(oc)
128 debugmsg("expairseq ctor from epvector,ex",LOGLEVEL_CONSTRUCT);
129 construct_from_epvector(v);
130 GINAC_ASSERT(is_canonical());
133 expairseq::expairseq(epvector *vp, const ex &oc)
134 : inherited(TINFO_expairseq), overall_coeff(oc)
136 debugmsg("expairseq ctor from epvector *,ex",LOGLEVEL_CONSTRUCT);
138 construct_from_epvector(*vp);
140 GINAC_ASSERT(is_canonical());
147 expairseq::expairseq(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
148 #if EXPAIRSEQ_USE_HASHTAB
152 debugmsg("expairseq ctor from archive_node", LOGLEVEL_CONSTRUCT);
153 for (unsigned int i=0; true; i++) {
156 if (n.find_ex("rest", rest, sym_lst, i) && n.find_ex("coeff", coeff, sym_lst, i))
157 seq.push_back(expair(rest, coeff));
161 n.find_ex("overall_coeff", overall_coeff, sym_lst);
164 void expairseq::archive(archive_node &n) const
166 inherited::archive(n);
167 epvector::const_iterator i = seq.begin(), iend = seq.end();
169 n.add_ex("rest", i->rest);
170 n.add_ex("coeff", i->coeff);
173 n.add_ex("overall_coeff", overall_coeff);
176 DEFAULT_UNARCHIVE(expairseq)
179 // functions overriding virtual functions from bases classes
184 basic *expairseq::duplicate() const
186 debugmsg("expairseq duplicate",LOGLEVEL_DUPLICATE);
187 return new expairseq(*this);
190 void expairseq::print(const print_context & c, unsigned level) const
192 debugmsg("expairseq print",LOGLEVEL_PRINT);
194 if (is_of_type(c, print_tree)) {
196 unsigned delta_indent = static_cast<const print_tree &>(c).delta_indent;
198 c.s << std::string(level, ' ') << class_name()
199 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
200 << ", nops=" << nops()
202 for (unsigned i=0; i<seq.size(); ++i) {
203 seq[i].rest.print(c, level + delta_indent);
204 seq[i].coeff.print(c, level + delta_indent);
205 if (i != seq.size()-1)
206 c.s << std::string(level + delta_indent, ' ') << "-----" << std::endl;
208 if (!overall_coeff.is_equal(default_overall_coeff())) {
209 c.s << std::string(level + delta_indent, ' ') << "-----" << std::endl
210 << std::string(level + delta_indent, ' ') << "overall_coeff" << std::endl;
211 overall_coeff.print(c, level + delta_indent);
213 c.s << std::string(level + delta_indent,' ') << "=====" << std::endl;
214 #if EXPAIRSEQ_USE_HASHTAB
215 c.s << std::string(level + delta_indent,' ')
216 << "hashtab size " << hashtabsize << std::endl;
217 if (hashtabsize == 0) return;
219 unsigned count[MAXCOUNT+1];
220 for (int i=0; i<MAXCOUNT+1; ++i)
222 unsigned this_bin_fill;
223 unsigned cum_fill_sq = 0;
224 unsigned cum_fill = 0;
225 for (unsigned i=0; i<hashtabsize; ++i) {
227 if (hashtab[i].size() > 0) {
228 c.s << std::string(level + delta_indent, ' ')
229 << "bin " << i << " with entries ";
230 for (epplist::const_iterator it=hashtab[i].begin();
231 it!=hashtab[i].end(); ++it) {
232 c.s << *it-seq.begin() << " ";
236 cum_fill += this_bin_fill;
237 cum_fill_sq += this_bin_fill*this_bin_fill;
239 if (this_bin_fill<MAXCOUNT)
240 ++count[this_bin_fill];
246 double lambda = (1.0*seq.size()) / hashtabsize;
247 for (int k=0; k<MAXCOUNT; ++k) {
250 double prob = std::pow(lambda,k)/fact * std::exp(-lambda);
252 c.s << std::string(level + delta_indent, ' ') << "bins with " << k << " entries: "
253 << int(1000.0*count[k]/hashtabsize)/10.0 << "% (expected: "
254 << int(prob*1000)/10.0 << ")" << std::endl;
256 c.s << std::string(level + delta_indent, ' ') << "bins with more entries: "
257 << int(1000.0*count[MAXCOUNT]/hashtabsize)/10.0 << "% (expected: "
258 << int((1-cum_prob)*1000)/10.0 << ")" << std::endl;
260 c.s << std::string(level + delta_indent, ' ') << "variance: "
261 << 1.0/hashtabsize*cum_fill_sq-(1.0/hashtabsize*cum_fill)*(1.0/hashtabsize*cum_fill)
263 c.s << std::string(level + delta_indent, ' ') << "average fill: "
264 << (1.0*cum_fill)/hashtabsize
265 << " (should be equal to " << (1.0*seq.size())/hashtabsize << ")" << std::endl;
266 #endif // EXPAIRSEQ_USE_HASHTAB
270 printseq(c, ',', precedence, level);
275 bool expairseq::info(unsigned inf) const
277 return inherited::info(inf);
280 unsigned expairseq::nops() const
282 if (overall_coeff.is_equal(default_overall_coeff()))
288 ex expairseq::op(int i) const
290 if (unsigned(i)<seq.size())
291 return recombine_pair_to_ex(seq[i]);
292 GINAC_ASSERT(!overall_coeff.is_equal(default_overall_coeff()));
293 return overall_coeff;
296 ex &expairseq::let_op(int i)
298 throw(std::logic_error("let_op not defined for expairseq and derived classes (add,mul,...)"));
301 ex expairseq::eval(int level) const
303 if ((level==1) && (flags &status_flags::evaluated))
306 epvector *vp = evalchildren(level);
310 return (new expairseq(vp,overall_coeff))->setflag(status_flags::dynallocated | status_flags::evaluated);
313 ex expairseq::evalf(int level) const
315 return thisexpairseq(evalfchildren(level),overall_coeff.evalf(level-1));
318 ex expairseq::normal(lst &sym_lst, lst &repl_lst, int level) const
320 ex n = thisexpairseq(normalchildren(level),overall_coeff);
321 return n.bp->basic::normal(sym_lst,repl_lst,level);
324 ex expairseq::subs(const lst &ls, const lst &lr) const
326 epvector *vp = subschildren(ls,lr);
328 return inherited::subs(ls, lr);
330 return thisexpairseq(vp,overall_coeff);
335 /** Implementation of ex::diff() for an expairseq.
336 * It differentiates all elements of the sequence.
338 ex expairseq::derivative(const symbol &s) const
340 return thisexpairseq(diffchildren(s),overall_coeff);
343 int expairseq::compare_same_type(const basic &other) const
345 GINAC_ASSERT(is_of_type(other, expairseq));
346 const expairseq &o = static_cast<const expairseq &>(const_cast<basic &>(other));
350 // compare number of elements
351 if (seq.size() != o.seq.size())
352 return (seq.size()<o.seq.size()) ? -1 : 1;
354 // compare overall_coeff
355 cmpval = overall_coeff.compare(o.overall_coeff);
359 #if EXPAIRSEQ_USE_HASHTAB
360 GINAC_ASSERT(hashtabsize==o.hashtabsize);
361 if (hashtabsize==0) {
362 #endif // EXPAIRSEQ_USE_HASHTAB
363 epvector::const_iterator cit1 = seq.begin();
364 epvector::const_iterator cit2 = o.seq.begin();
365 epvector::const_iterator last1 = seq.end();
366 epvector::const_iterator last2 = o.seq.end();
368 for (; (cit1!=last1)&&(cit2!=last2); ++cit1, ++cit2) {
369 cmpval = (*cit1).compare(*cit2);
370 if (cmpval!=0) return cmpval;
373 GINAC_ASSERT(cit1==last1);
374 GINAC_ASSERT(cit2==last2);
377 #if EXPAIRSEQ_USE_HASHTAB
380 // compare number of elements in each hashtab entry
381 for (unsigned i=0; i<hashtabsize; ++i) {
382 unsigned cursize=hashtab[i].size();
383 if (cursize != o.hashtab[i].size())
384 return (cursize < o.hashtab[i].size()) ? -1 : 1;
387 // compare individual (sorted) hashtab entries
388 for (unsigned i=0; i<hashtabsize; ++i) {
389 unsigned sz = hashtab[i].size();
391 const epplist &eppl1 = hashtab[i];
392 const epplist &eppl2 = o.hashtab[i];
393 epplist::const_iterator it1 = eppl1.begin();
394 epplist::const_iterator it2 = eppl2.begin();
395 while (it1!=eppl1.end()) {
396 cmpval = (*(*it1)).compare(*(*it2));
406 #endif // EXPAIRSEQ_USE_HASHTAB
409 bool expairseq::is_equal_same_type(const basic &other) const
411 const expairseq &o = dynamic_cast<const expairseq &>(const_cast<basic &>(other));
413 // compare number of elements
414 if (seq.size()!=o.seq.size())
417 // compare overall_coeff
418 if (!overall_coeff.is_equal(o.overall_coeff))
421 #if EXPAIRSEQ_USE_HASHTAB
422 // compare number of elements in each hashtab entry
423 if (hashtabsize!=o.hashtabsize) {
424 std::cout << "this:" << std::endl;
425 print(print_tree(std::cout));
426 std::cout << "other:" << std::endl;
427 other.print(print_tree(std::cout));
430 GINAC_ASSERT(hashtabsize==o.hashtabsize);
432 if (hashtabsize==0) {
433 #endif // EXPAIRSEQ_USE_HASHTAB
434 epvector::const_iterator cit1 = seq.begin();
435 epvector::const_iterator cit2 = o.seq.begin();
436 epvector::const_iterator last1 = seq.end();
438 while (cit1!=last1) {
439 if (!(*cit1).is_equal(*cit2)) return false;
445 #if EXPAIRSEQ_USE_HASHTAB
448 for (unsigned i=0; i<hashtabsize; ++i) {
449 if (hashtab[i].size() != o.hashtab[i].size())
453 // compare individual sorted hashtab entries
454 for (unsigned i=0; i<hashtabsize; ++i) {
455 unsigned sz = hashtab[i].size();
457 const epplist &eppl1 = hashtab[i];
458 const epplist &eppl2 = o.hashtab[i];
459 epplist::const_iterator it1 = eppl1.begin();
460 epplist::const_iterator it2 = eppl2.begin();
461 while (it1!=eppl1.end()) {
462 if (!(*(*it1)).is_equal(*(*it2))) return false;
470 #endif // EXPAIRSEQ_USE_HASHTAB
473 unsigned expairseq::return_type(void) const
475 return return_types::noncommutative_composite;
478 unsigned expairseq::calchash(void) const
480 unsigned v = golden_ratio_hash(tinfo());
481 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
482 #if !EXPAIRSEQ_USE_HASHTAB
483 v = rotate_left_31(v); // rotation would spoil commutativity
484 #endif // EXPAIRSEQ_USE_HASHTAB
485 v ^= cit->rest.gethash();
486 #if !EXPAIRSEQ_USE_HASHTAB
487 v = rotate_left_31(v);
488 v ^= cit->coeff.gethash();
489 #endif // EXPAIRSEQ_USE_HASHTAB
492 v ^= overall_coeff.gethash();
495 // store calculated hash value only if object is already evaluated
496 if (flags &status_flags::evaluated) {
497 setflag(status_flags::hash_calculated);
504 ex expairseq::expand(unsigned options) const
506 epvector *vp = expandchildren(options);
508 // the terms have not changed, so it is safe to declare this expanded
509 setflag(status_flags::expanded);
513 return thisexpairseq(vp,overall_coeff);
517 // new virtual functions which can be overridden by derived classes
522 /** Create an object of this type.
523 * This method works similar to a constructor. It is useful because expairseq
524 * has (at least) two possible different semantics but we want to inherit
525 * methods thus avoiding code duplication. Sometimes a method in expairseq
526 * has to create a new one of the same semantics, which cannot be done by a
527 * ctor because the name (add, mul,...) is unknown on the expaiseq level. In
528 * order for this trick to work a derived class must of course override this
530 ex expairseq::thisexpairseq(const epvector &v, const ex &oc) const
532 return expairseq(v,oc);
535 ex expairseq::thisexpairseq(epvector *vp, const ex &oc) const
537 return expairseq(vp,oc);
540 void expairseq::printpair(const print_context & c, const expair & p, unsigned upper_precedence) const
543 p.rest.bp->print(c, precedence);
545 p.coeff.bp->print(c, precedence);
549 void expairseq::printseq(const print_context & c, char delim,
550 unsigned this_precedence,
551 unsigned upper_precedence) const
553 if (this_precedence <= upper_precedence)
555 epvector::const_iterator it, it_last = seq.end() - 1;
556 for (it=seq.begin(); it!=it_last; ++it) {
557 printpair(c, *it, this_precedence);
560 printpair(c, *it, this_precedence);
561 if (!overall_coeff.is_equal(default_overall_coeff())) {
563 overall_coeff.print(c, this_precedence);
566 if (this_precedence <= upper_precedence)
571 /** Form an expair from an ex, using the corresponding semantics.
572 * @see expairseq::recombine_pair_to_ex() */
573 expair expairseq::split_ex_to_pair(const ex &e) const
575 return expair(e,_ex1());
579 expair expairseq::combine_ex_with_coeff_to_pair(const ex &e,
582 GINAC_ASSERT(is_ex_exactly_of_type(c,numeric));
588 expair expairseq::combine_pair_with_coeff_to_pair(const expair &p,
591 GINAC_ASSERT(is_ex_exactly_of_type(p.coeff,numeric));
592 GINAC_ASSERT(is_ex_exactly_of_type(c,numeric));
594 return expair(p.rest,ex_to_numeric(p.coeff).mul_dyn(ex_to_numeric(c)));
598 /** Form an ex out of an expair, using the corresponding semantics.
599 * @see expairseq::split_ex_to_pair() */
600 ex expairseq::recombine_pair_to_ex(const expair &p) const
602 return lst(p.rest,p.coeff);
605 bool expairseq::expair_needs_further_processing(epp it)
607 #if EXPAIRSEQ_USE_HASHTAB
608 //# error "FIXME: expair_needs_further_processing not yet implemented for hashtabs, sorry. A.F."
609 #endif // EXPAIRSEQ_USE_HASHTAB
613 ex expairseq::default_overall_coeff(void) const
618 void expairseq::combine_overall_coeff(const ex &c)
620 GINAC_ASSERT(is_ex_exactly_of_type(overall_coeff,numeric));
621 GINAC_ASSERT(is_ex_exactly_of_type(c,numeric));
622 overall_coeff = ex_to_numeric(overall_coeff).add_dyn(ex_to_numeric(c));
625 void expairseq::combine_overall_coeff(const ex &c1, const ex &c2)
627 GINAC_ASSERT(is_ex_exactly_of_type(overall_coeff,numeric));
628 GINAC_ASSERT(is_ex_exactly_of_type(c1,numeric));
629 GINAC_ASSERT(is_ex_exactly_of_type(c2,numeric));
630 overall_coeff = ex_to_numeric(overall_coeff).
631 add_dyn(ex_to_numeric(c1).mul(ex_to_numeric(c2)));
634 bool expairseq::can_make_flat(const expair &p) const
641 // non-virtual functions in this class
644 void expairseq::construct_from_2_ex_via_exvector(const ex &lh, const ex &rh)
650 construct_from_exvector(v);
651 #if EXPAIRSEQ_USE_HASHTAB
652 GINAC_ASSERT((hashtabsize==0)||(hashtabsize>=minhashtabsize));
653 GINAC_ASSERT(hashtabsize==calc_hashtabsize(seq.size()));
654 #endif // EXPAIRSEQ_USE_HASHTAB
657 void expairseq::construct_from_2_ex(const ex &lh, const ex &rh)
659 if (lh.bp->tinfo()==tinfo()) {
660 if (rh.bp->tinfo()==tinfo()) {
661 #if EXPAIRSEQ_USE_HASHTAB
662 unsigned totalsize = ex_to_expairseq(lh).seq.size() +
663 ex_to_expairseq(rh).seq.size();
664 if (calc_hashtabsize(totalsize)!=0) {
665 construct_from_2_ex_via_exvector(lh,rh);
667 #endif // EXPAIRSEQ_USE_HASHTAB
668 construct_from_2_expairseq(ex_to_expairseq(lh),
669 ex_to_expairseq(rh));
670 #if EXPAIRSEQ_USE_HASHTAB
672 #endif // EXPAIRSEQ_USE_HASHTAB
675 #if EXPAIRSEQ_USE_HASHTAB
676 unsigned totalsize = ex_to_expairseq(lh).seq.size()+1;
677 if (calc_hashtabsize(totalsize)!=0) {
678 construct_from_2_ex_via_exvector(lh, rh);
680 #endif // EXPAIRSEQ_USE_HASHTAB
681 construct_from_expairseq_ex(ex_to_expairseq(lh), rh);
682 #if EXPAIRSEQ_USE_HASHTAB
684 #endif // EXPAIRSEQ_USE_HASHTAB
687 } else if (rh.bp->tinfo()==tinfo()) {
688 #if EXPAIRSEQ_USE_HASHTAB
689 unsigned totalsize=ex_to_expairseq(rh).seq.size()+1;
690 if (calc_hashtabsize(totalsize)!=0) {
691 construct_from_2_ex_via_exvector(lh,rh);
693 #endif // EXPAIRSEQ_USE_HASHTAB
694 construct_from_expairseq_ex(ex_to_expairseq(rh),lh);
695 #if EXPAIRSEQ_USE_HASHTAB
697 #endif // EXPAIRSEQ_USE_HASHTAB
701 #if EXPAIRSEQ_USE_HASHTAB
702 if (calc_hashtabsize(2)!=0) {
703 construct_from_2_ex_via_exvector(lh,rh);
707 #endif // EXPAIRSEQ_USE_HASHTAB
709 if (is_ex_exactly_of_type(lh,numeric)) {
710 if (is_ex_exactly_of_type(rh,numeric)) {
711 combine_overall_coeff(lh);
712 combine_overall_coeff(rh);
714 combine_overall_coeff(lh);
715 seq.push_back(split_ex_to_pair(rh));
718 if (is_ex_exactly_of_type(rh,numeric)) {
719 combine_overall_coeff(rh);
720 seq.push_back(split_ex_to_pair(lh));
722 expair p1 = split_ex_to_pair(lh);
723 expair p2 = split_ex_to_pair(rh);
725 int cmpval = p1.rest.compare(p2.rest);
727 p1.coeff=ex_to_numeric(p1.coeff).add_dyn(ex_to_numeric(p2.coeff));
728 if (!ex_to_numeric(p1.coeff).is_zero()) {
729 // no further processing is necessary, since this
730 // one element will usually be recombined in eval()
747 void expairseq::construct_from_2_expairseq(const expairseq &s1,
750 combine_overall_coeff(s1.overall_coeff);
751 combine_overall_coeff(s2.overall_coeff);
753 epvector::const_iterator first1 = s1.seq.begin();
754 epvector::const_iterator last1 = s1.seq.end();
755 epvector::const_iterator first2 = s2.seq.begin();
756 epvector::const_iterator last2 = s2.seq.end();
758 seq.reserve(s1.seq.size()+s2.seq.size());
760 bool needs_further_processing=false;
762 while (first1!=last1 && first2!=last2) {
763 int cmpval = (*first1).rest.compare((*first2).rest);
766 const numeric &newcoeff = ex_to_numeric((*first1).coeff).
767 add(ex_to_numeric((*first2).coeff));
768 if (!newcoeff.is_zero()) {
769 seq.push_back(expair((*first1).rest,newcoeff));
770 if (expair_needs_further_processing(seq.end()-1)) {
771 needs_further_processing = true;
776 } else if (cmpval<0) {
777 seq.push_back(*first1);
780 seq.push_back(*first2);
785 while (first1!=last1) {
786 seq.push_back(*first1);
789 while (first2!=last2) {
790 seq.push_back(*first2);
794 if (needs_further_processing) {
797 construct_from_epvector(v);
801 void expairseq::construct_from_expairseq_ex(const expairseq &s,
804 combine_overall_coeff(s.overall_coeff);
805 if (is_ex_exactly_of_type(e,numeric)) {
806 combine_overall_coeff(e);
811 epvector::const_iterator first = s.seq.begin();
812 epvector::const_iterator last = s.seq.end();
813 expair p = split_ex_to_pair(e);
815 seq.reserve(s.seq.size()+1);
816 bool p_pushed = false;
818 bool needs_further_processing=false;
820 // merge p into s.seq
821 while (first!=last) {
822 int cmpval=(*first).rest.compare(p.rest);
825 const numeric &newcoeff = ex_to_numeric((*first).coeff).
826 add(ex_to_numeric(p.coeff));
827 if (!newcoeff.is_zero()) {
828 seq.push_back(expair((*first).rest,newcoeff));
829 if (expair_needs_further_processing(seq.end()-1)) {
830 needs_further_processing = true;
836 } else if (cmpval<0) {
837 seq.push_back(*first);
847 // while loop exited because p was pushed, now push rest of s.seq
848 while (first!=last) {
849 seq.push_back(*first);
853 // while loop exited because s.seq was pushed, now push p
857 if (needs_further_processing) {
860 construct_from_epvector(v);
864 void expairseq::construct_from_exvector(const exvector &v)
866 // simplifications: +(a,+(b,c),d) -> +(a,b,c,d) (associativity)
867 // +(d,b,c,a) -> +(a,b,c,d) (canonicalization)
868 // +(...,x,*(x,c1),*(x,c2)) -> +(...,*(x,1+c1+c2)) (c1, c2 numeric())
869 // (same for (+,*) -> (*,^)
872 #if EXPAIRSEQ_USE_HASHTAB
873 combine_same_terms();
876 combine_same_terms_sorted_seq();
877 #endif // EXPAIRSEQ_USE_HASHTAB
881 void expairseq::construct_from_epvector(const epvector &v)
883 // simplifications: +(a,+(b,c),d) -> +(a,b,c,d) (associativity)
884 // +(d,b,c,a) -> +(a,b,c,d) (canonicalization)
885 // +(...,x,*(x,c1),*(x,c2)) -> +(...,*(x,1+c1+c2)) (c1, c2 numeric())
886 // (same for (+,*) -> (*,^)
889 #if EXPAIRSEQ_USE_HASHTAB
890 combine_same_terms();
893 combine_same_terms_sorted_seq();
894 #endif // EXPAIRSEQ_USE_HASHTAB
898 /** Combine this expairseq with argument exvector.
899 * It cares for associativity as well as for special handling of numerics. */
900 void expairseq::make_flat(const exvector &v)
902 exvector::const_iterator cit;
904 // count number of operands which are of same expairseq derived type
905 // and their cumulative number of operands
910 while (cit!=v.end()) {
911 if (cit->bp->tinfo()==this->tinfo()) {
913 noperands += ex_to_expairseq(*cit).seq.size();
918 // reserve seq and coeffseq which will hold all operands
919 seq.reserve(v.size()+noperands-nexpairseqs);
921 // copy elements and split off numerical part
923 while (cit!=v.end()) {
924 if (cit->bp->tinfo()==this->tinfo()) {
925 const expairseq &subseqref = ex_to_expairseq(*cit);
926 combine_overall_coeff(subseqref.overall_coeff);
927 epvector::const_iterator cit_s = subseqref.seq.begin();
928 while (cit_s!=subseqref.seq.end()) {
929 seq.push_back(*cit_s);
933 if (is_ex_exactly_of_type(*cit,numeric))
934 combine_overall_coeff(*cit);
936 seq.push_back(split_ex_to_pair(*cit));
944 /** Combine this expairseq with argument epvector.
945 * It cares for associativity as well as for special handling of numerics. */
946 void expairseq::make_flat(const epvector &v)
948 epvector::const_iterator cit;
950 // count number of operands which are of same expairseq derived type
951 // and their cumulative number of operands
956 while (cit!=v.end()) {
957 if (cit->rest.bp->tinfo()==this->tinfo()) {
959 noperands += ex_to_expairseq((*cit).rest).seq.size();
964 // reserve seq and coeffseq which will hold all operands
965 seq.reserve(v.size()+noperands-nexpairseqs);
967 // copy elements and split off numerical part
969 while (cit!=v.end()) {
970 if (cit->rest.bp->tinfo()==this->tinfo() &&
971 this->can_make_flat(*cit)) {
972 const expairseq &subseqref = ex_to_expairseq((*cit).rest);
973 combine_overall_coeff(ex_to_numeric(subseqref.overall_coeff),
974 ex_to_numeric((*cit).coeff));
975 epvector::const_iterator cit_s = subseqref.seq.begin();
976 while (cit_s!=subseqref.seq.end()) {
977 seq.push_back(expair((*cit_s).rest,
978 ex_to_numeric((*cit_s).coeff).mul_dyn(ex_to_numeric((*cit).coeff))));
979 //seq.push_back(combine_pair_with_coeff_to_pair(*cit_s,
984 if (cit->is_canonical_numeric())
985 combine_overall_coeff(cit->rest);
995 /** Brings this expairseq into a sorted (canonical) form. */
996 void expairseq::canonicalize(void)
999 sort(seq.begin(),seq.end(),expair_is_less());
1004 /** Compact a presorted expairseq by combining all matching expairs to one
1005 * each. On an add object, this is responsible for 2*x+3*x+y -> 5*x+y, for
1007 void expairseq::combine_same_terms_sorted_seq(void)
1009 bool needs_further_processing = false;
1012 epvector::iterator itin1 = seq.begin();
1013 epvector::iterator itin2 = itin1+1;
1014 epvector::iterator itout = itin1;
1015 epvector::iterator last = seq.end();
1016 // must_copy will be set to true the first time some combination is
1017 // possible from then on the sequence has changed and must be compacted
1018 bool must_copy = false;
1019 while (itin2!=last) {
1020 if ((*itin1).rest.compare((*itin2).rest)==0) {
1021 (*itin1).coeff = ex_to_numeric((*itin1).coeff).
1022 add_dyn(ex_to_numeric((*itin2).coeff));
1023 if (expair_needs_further_processing(itin1))
1024 needs_further_processing = true;
1027 if (!ex_to_numeric((*itin1).coeff).is_zero()) {
1036 if (!ex_to_numeric((*itin1).coeff).is_zero()) {
1042 seq.erase(itout,last);
1045 if (needs_further_processing) {
1048 construct_from_epvector(v);
1053 #if EXPAIRSEQ_USE_HASHTAB
1055 unsigned expairseq::calc_hashtabsize(unsigned sz) const
1058 unsigned nearest_power_of_2 = 1 << log2(sz);
1059 // if (nearest_power_of_2 < maxhashtabsize/hashtabfactor) {
1060 // size = nearest_power_of_2*hashtabfactor;
1061 size = nearest_power_of_2/hashtabfactor;
1062 if (size<minhashtabsize)
1064 GINAC_ASSERT(hashtabsize<=0x8000000U); // really max size due to 31 bit hashing
1065 // hashtabsize must be a power of 2
1066 GINAC_ASSERT((1U << log2(size))==size);
1070 unsigned expairseq::calc_hashindex(const ex &e) const
1072 // calculate hashindex
1073 unsigned hash = e.gethash();
1075 if (is_a_numeric_hash(hash)) {
1076 hashindex = hashmask;
1078 hashindex = hash &hashmask;
1079 // last hashtab entry is reserved for numerics
1080 if (hashindex==hashmask) hashindex = 0;
1082 GINAC_ASSERT(hashindex>=0);
1083 GINAC_ASSERT((hashindex<hashtabsize)||(hashtabsize==0));
1087 void expairseq::shrink_hashtab(void)
1089 unsigned new_hashtabsize;
1090 while (hashtabsize!=(new_hashtabsize=calc_hashtabsize(seq.size()))) {
1091 GINAC_ASSERT(new_hashtabsize<hashtabsize);
1092 if (new_hashtabsize==0) {
1099 // shrink by a factor of 2
1100 unsigned half_hashtabsize = hashtabsize/2;
1101 for (unsigned i=0; i<half_hashtabsize-1; ++i)
1102 hashtab[i].merge(hashtab[i+half_hashtabsize],epp_is_less());
1103 // special treatment for numeric hashes
1104 hashtab[0].merge(hashtab[half_hashtabsize-1],epp_is_less());
1105 hashtab[half_hashtabsize-1] = hashtab[hashtabsize-1];
1106 hashtab.resize(half_hashtabsize);
1107 hashtabsize = half_hashtabsize;
1108 hashmask = hashtabsize-1;
1112 void expairseq::remove_hashtab_entry(epvector::const_iterator element)
1115 return; // nothing to do
1117 // calculate hashindex of element to be deleted
1118 unsigned hashindex = calc_hashindex((*element).rest);
1120 // find it in hashtab and remove it
1121 epplist &eppl = hashtab[hashindex];
1122 epplist::iterator epplit = eppl.begin();
1123 bool erased = false;
1124 while (epplit!=eppl.end()) {
1125 if (*epplit == element) {
1134 cout << "tried to erase " << element-seq.begin() << std::endl;
1135 cout << "size " << seq.end()-seq.begin() << std::endl;
1137 unsigned hashindex = calc_hashindex((*element).rest);
1138 epplist &eppl = hashtab[hashindex];
1139 epplist::iterator epplit=eppl.begin();
1141 while (epplit!=eppl.end()) {
1142 if (*epplit == element) {
1149 GINAC_ASSERT(erased);
1151 GINAC_ASSERT(erased);
1154 void expairseq::move_hashtab_entry(epvector::const_iterator oldpos,
1155 epvector::iterator newpos)
1157 GINAC_ASSERT(hashtabsize!=0);
1159 // calculate hashindex of element which was moved
1160 unsigned hashindex=calc_hashindex((*newpos).rest);
1162 // find it in hashtab and modify it
1163 epplist &eppl = hashtab[hashindex];
1164 epplist::iterator epplit = eppl.begin();
1165 while (epplit!=eppl.end()) {
1166 if (*epplit == oldpos) {
1172 GINAC_ASSERT(epplit!=eppl.end());
1175 void expairseq::sorted_insert(epplist &eppl, epp elem)
1177 epplist::iterator current = eppl.begin();
1178 while ((current!=eppl.end())&&((*(*current)).is_less(*elem))) {
1181 eppl.insert(current,elem);
1184 void expairseq::build_hashtab_and_combine(epvector::iterator &first_numeric,
1185 epvector::iterator &last_non_zero,
1186 std::vector<bool> &touched,
1187 unsigned &number_of_zeroes)
1189 epp current=seq.begin();
1191 while (current!=first_numeric) {
1192 if (is_ex_exactly_of_type((*current).rest,numeric)) {
1194 iter_swap(current,first_numeric);
1196 // calculate hashindex
1197 unsigned currenthashindex = calc_hashindex((*current).rest);
1199 // test if there is already a matching expair in the hashtab-list
1200 epplist &eppl=hashtab[currenthashindex];
1201 epplist::iterator epplit = eppl.begin();
1202 while (epplit!=eppl.end()) {
1203 if ((*current).rest.is_equal((*(*epplit)).rest))
1207 if (epplit==eppl.end()) {
1208 // no matching expair found, append this to end of list
1209 sorted_insert(eppl,current);
1212 // epplit points to a matching expair, combine it with current
1213 (*(*epplit)).coeff = ex_to_numeric((*(*epplit)).coeff).
1214 add_dyn(ex_to_numeric((*current).coeff));
1216 // move obsolete current expair to end by swapping with last_non_zero element
1217 // if this was a numeric, it is swapped with the expair before first_numeric
1218 iter_swap(current,last_non_zero);
1220 if (first_numeric!=last_non_zero) iter_swap(first_numeric,current);
1223 // test if combined term has coeff 0 and can be removed is done later
1224 touched[(*epplit)-seq.begin()]=true;
1230 void expairseq::drop_coeff_0_terms(epvector::iterator &first_numeric,
1231 epvector::iterator &last_non_zero,
1232 std::vector<bool> &touched,
1233 unsigned &number_of_zeroes)
1235 // move terms with coeff 0 to end and remove them from hashtab
1236 // check only those elements which have been touched
1237 epp current = seq.begin();
1239 while (current!=first_numeric) {
1243 } else if (!ex_to_numeric((*current).coeff).is_zero()) {
1247 remove_hashtab_entry(current);
1249 // move element to the end, unless it is already at the end
1250 if (current!=last_non_zero) {
1251 iter_swap(current,last_non_zero);
1253 bool numeric_swapped=first_numeric!=last_non_zero;
1254 if (numeric_swapped) iter_swap(first_numeric,current);
1255 epvector::iterator changed_entry;
1257 if (numeric_swapped)
1258 changed_entry = first_numeric;
1260 changed_entry = last_non_zero;
1265 if (first_numeric!=current) {
1267 // change entry in hashtab which referred to first_numeric or last_non_zero to current
1268 move_hashtab_entry(changed_entry,current);
1269 touched[current-seq.begin()] = touched[changed_entry-seq.begin()];
1278 GINAC_ASSERT(i==current-seq.begin());
1281 /** True if one of the coeffs vanishes, otherwise false.
1282 * This would be an invariant violation, so this should only be used for
1283 * debugging purposes. */
1284 bool expairseq::has_coeff_0(void) const
1286 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
1287 if ((*cit).coeff.is_zero())
1293 void expairseq::add_numerics_to_hashtab(epvector::iterator first_numeric,
1294 epvector::const_iterator last_non_zero)
1296 if (first_numeric==seq.end()) return; // no numerics
1298 epvector::iterator current = first_numeric;
1299 epvector::const_iterator last = last_non_zero+1;
1300 while (current!=last) {
1301 sorted_insert(hashtab[hashmask],current);
1306 void expairseq::combine_same_terms(void)
1308 // combine same terms, drop term with coeff 0, move numerics to end
1310 // calculate size of hashtab
1311 hashtabsize = calc_hashtabsize(seq.size());
1313 // hashtabsize is a power of 2
1314 hashmask = hashtabsize-1;
1318 hashtab.resize(hashtabsize);
1320 if (hashtabsize==0) {
1322 combine_same_terms_sorted_seq();
1323 GINAC_ASSERT(!has_coeff_0());
1327 // iterate through seq, move numerics to end,
1328 // fill hashtab and combine same terms
1329 epvector::iterator first_numeric = seq.end();
1330 epvector::iterator last_non_zero = seq.end()-1;
1332 std::vector<bool> touched;
1333 touched.reserve(seq.size());
1334 for (unsigned i=0; i<seq.size(); ++i) touched[i]=false;
1336 unsigned number_of_zeroes = 0;
1338 GINAC_ASSERT(!has_coeff_0());
1339 build_hashtab_and_combine(first_numeric,last_non_zero,touched,number_of_zeroes);
1341 cout << "in combine:" << std::endl;
1343 cout << "size=" << seq.end() - seq.begin() << std::endl;
1344 cout << "first_numeric=" << first_numeric - seq.begin() << std::endl;
1345 cout << "last_non_zero=" << last_non_zero - seq.begin() << std::endl;
1346 for (unsigned i=0; i<seq.size(); ++i) {
1347 if (touched[i]) cout << i << " is touched" << std::endl;
1349 cout << "end in combine" << std::endl;
1352 // there should not be any terms with coeff 0 from the beginning,
1353 // so it should be safe to skip this step
1354 if (number_of_zeroes!=0) {
1355 drop_coeff_0_terms(first_numeric,last_non_zero,touched,number_of_zeroes);
1357 cout << "in combine after drop:" << std::endl;
1359 cout << "size=" << seq.end() - seq.begin() << std::endl;
1360 cout << "first_numeric=" << first_numeric - seq.begin() << std::endl;
1361 cout << "last_non_zero=" << last_non_zero - seq.begin() << std::endl;
1362 for (unsigned i=0; i<seq.size(); ++i) {
1363 if (touched[i]) cout << i << " is touched" << std::endl;
1365 cout << "end in combine after drop" << std::endl;
1369 add_numerics_to_hashtab(first_numeric,last_non_zero);
1371 // pop zero elements
1372 for (unsigned i=0; i<number_of_zeroes; ++i) {
1376 // shrink hashtabsize to calculated value
1377 GINAC_ASSERT(!has_coeff_0());
1381 GINAC_ASSERT(!has_coeff_0());
1384 #endif // EXPAIRSEQ_USE_HASHTAB
1386 /** Check if this expairseq is in sorted (canonical) form. Useful mainly for
1387 * debugging or in assertions since being sorted is an invariance. */
1388 bool expairseq::is_canonical() const
1390 if (seq.size() <= 1)
1393 #if EXPAIRSEQ_USE_HASHTAB
1394 if (hashtabsize > 0) return 1; // not canoncalized
1395 #endif // EXPAIRSEQ_USE_HASHTAB
1397 epvector::const_iterator it = seq.begin();
1398 epvector::const_iterator it_last = it;
1399 for (++it; it!=seq.end(); it_last=it, ++it) {
1400 if (!((*it_last).is_less(*it) || (*it_last).is_equal(*it))) {
1401 if (!is_ex_exactly_of_type((*it_last).rest,numeric) ||
1402 !is_ex_exactly_of_type((*it).rest,numeric)) {
1403 // double test makes it easier to set a breakpoint...
1404 if (!is_ex_exactly_of_type((*it_last).rest,numeric) ||
1405 !is_ex_exactly_of_type((*it).rest,numeric)) {
1406 printpair(std::clog, *it_last, 0);
1408 printpair(std::clog, *it, 0);
1410 std::clog << "pair1:" << std::endl;
1411 (*it_last).rest.print(print_tree(std::clog));
1412 (*it_last).coeff.print(print_tree(std::clog));
1413 std::clog << "pair2:" << std::endl;
1414 (*it).rest.print(print_tree(std::clog));
1415 (*it).coeff.print(print_tree(std::clog));
1425 /** Member-wise expand the expairs in this sequence.
1427 * @see expairseq::expand()
1428 * @return pointer to epvector containing expanded pairs or zero pointer,
1429 * if no members were changed. */
1430 epvector * expairseq::expandchildren(unsigned options) const
1432 epvector::const_iterator last = seq.end();
1433 epvector::const_iterator cit = seq.begin();
1435 const ex &expanded_ex = (*cit).rest.expand(options);
1436 if (!are_ex_trivially_equal((*cit).rest,expanded_ex)) {
1438 // something changed, copy seq, eval and return it
1439 epvector *s = new epvector;
1440 s->reserve(seq.size());
1442 // copy parts of seq which are known not to have changed
1443 epvector::const_iterator cit2 = seq.begin();
1445 s->push_back(*cit2);
1448 // copy first changed element
1449 s->push_back(combine_ex_with_coeff_to_pair(expanded_ex,
1453 while (cit2!=last) {
1454 s->push_back(combine_ex_with_coeff_to_pair((*cit2).rest.expand(options),
1463 return 0; // signalling nothing has changed
1467 /** Member-wise evaluate the expairs in this sequence.
1469 * @see expairseq::eval()
1470 * @return pointer to epvector containing evaluated pairs or zero pointer,
1471 * if no members were changed. */
1472 epvector * expairseq::evalchildren(int level) const
1474 // returns a NULL pointer if nothing had to be evaluated
1475 // returns a pointer to a newly created epvector otherwise
1476 // (which has to be deleted somewhere else)
1481 if (level == -max_recursion_level)
1482 throw(std::runtime_error("max recursion level reached"));
1485 epvector::const_iterator last=seq.end();
1486 epvector::const_iterator cit=seq.begin();
1488 const ex &evaled_ex = (*cit).rest.eval(level);
1489 if (!are_ex_trivially_equal((*cit).rest,evaled_ex)) {
1491 // something changed, copy seq, eval and return it
1492 epvector *s = new epvector;
1493 s->reserve(seq.size());
1495 // copy parts of seq which are known not to have changed
1496 epvector::const_iterator cit2=seq.begin();
1498 s->push_back(*cit2);
1501 // copy first changed element
1502 s->push_back(combine_ex_with_coeff_to_pair(evaled_ex,
1506 while (cit2!=last) {
1507 s->push_back(combine_ex_with_coeff_to_pair((*cit2).rest.eval(level),
1516 return 0; // signalling nothing has changed
1520 /** Member-wise evaluate numerically all expairs in this sequence.
1522 * @see expairseq::evalf()
1523 * @return epvector with all entries evaluated numerically. */
1524 epvector expairseq::evalfchildren(int level) const
1529 if (level==-max_recursion_level)
1530 throw(std::runtime_error("max recursion level reached"));
1533 s.reserve(seq.size());
1536 for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
1537 s.push_back(combine_ex_with_coeff_to_pair((*it).rest.evalf(level),
1538 (*it).coeff.evalf(level)));
1544 /** Member-wise normalize all expairs in this sequence.
1546 * @see expairseq::normal()
1547 * @return epvector with all entries normalized. */
1548 epvector expairseq::normalchildren(int level) const
1553 if (level==-max_recursion_level)
1554 throw(std::runtime_error("max recursion level reached"));
1557 s.reserve(seq.size());
1560 for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
1561 s.push_back(combine_ex_with_coeff_to_pair((*it).rest.normal(level),
1568 /** Member-wise differentiate all expairs in this sequence.
1570 * @see expairseq::diff()
1571 * @return epvector with all entries differentiated. */
1572 epvector expairseq::diffchildren(const symbol &y) const
1575 s.reserve(seq.size());
1577 for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
1578 s.push_back(combine_ex_with_coeff_to_pair((*it).rest.diff(y),
1585 /** Member-wise substitute in this sequence.
1587 * @see expairseq::subs()
1588 * @return pointer to epvector containing pairs after application of subs or zero
1589 * pointer, if no members were changed. */
1590 epvector * expairseq::subschildren(const lst &ls, const lst &lr) const
1592 // returns a NULL pointer if nothing had to be substituted
1593 // returns a pointer to a newly created epvector otherwise
1594 // (which has to be deleted somewhere else)
1595 GINAC_ASSERT(ls.nops()==lr.nops());
1597 epvector::const_iterator last = seq.end();
1598 epvector::const_iterator cit = seq.begin();
1600 const ex &subsed_ex=(*cit).rest.subs(ls,lr);
1601 if (!are_ex_trivially_equal((*cit).rest,subsed_ex)) {
1603 // something changed, copy seq, subs and return it
1604 epvector *s = new epvector;
1605 s->reserve(seq.size());
1607 // copy parts of seq which are known not to have changed
1608 epvector::const_iterator cit2 = seq.begin();
1610 s->push_back(*cit2);
1613 // copy first changed element
1614 s->push_back(combine_ex_with_coeff_to_pair(subsed_ex,
1618 while (cit2!=last) {
1619 s->push_back(combine_ex_with_coeff_to_pair((*cit2).rest.subs(ls,lr),
1628 return 0; // signalling nothing has changed
1632 // static member variables
1637 unsigned expairseq::precedence = 10;
1639 #if EXPAIRSEQ_USE_HASHTAB
1640 unsigned expairseq::maxhashtabsize = 0x4000000U;
1641 unsigned expairseq::minhashtabsize = 0x1000U;
1642 unsigned expairseq::hashtabfactor = 1;
1643 #endif // EXPAIRSEQ_USE_HASHTAB
1645 } // namespace GiNaC