1 /** @file expairseq.cpp
3 * Implementation of sequences of expression pairs. */
6 * GiNaC Copyright (C) 1999-2015 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "expairseq.h"
28 #include "relational.h"
31 #include "operators.h"
33 #include "hash_seed.h"
47 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(expairseq, basic,
48 print_func<print_context>(&expairseq::do_print).
49 print_func<print_tree>(&expairseq::do_print_tree))
59 bool operator()(const epp &lh, const epp &rh) const
61 return (*lh).is_less(*rh);
66 // default constructor
71 expairseq::expairseq()
80 expairseq::expairseq(const ex &lh, const ex &rh)
82 construct_from_2_ex(lh,rh);
83 GINAC_ASSERT(is_canonical());
86 expairseq::expairseq(const exvector &v)
88 construct_from_exvector(v);
89 GINAC_ASSERT(is_canonical());
92 expairseq::expairseq(const epvector &v, const ex &oc, bool do_index_renaming)
95 GINAC_ASSERT(is_a<numeric>(oc));
96 construct_from_epvector(v, do_index_renaming);
97 GINAC_ASSERT(is_canonical());
100 expairseq::expairseq(epvector && vp, const ex &oc, bool do_index_renaming)
103 GINAC_ASSERT(is_a<numeric>(oc));
104 construct_from_epvector(std::move(vp), do_index_renaming);
105 GINAC_ASSERT(is_canonical());
112 void expairseq::read_archive(const archive_node &n, lst &sym_lst)
114 inherited::read_archive(n, sym_lst);
115 auto first = n.find_first("rest");
116 auto last = n.find_last("coeff");
118 seq.reserve((last-first)/2);
120 for (auto loc = first; loc < last;) {
123 n.find_ex_by_loc(loc++, rest, sym_lst);
124 n.find_ex_by_loc(loc++, coeff, sym_lst);
125 seq.push_back(expair(rest, coeff));
128 n.find_ex("overall_coeff", overall_coeff, sym_lst);
131 GINAC_ASSERT(is_canonical());
134 void expairseq::archive(archive_node &n) const
136 inherited::archive(n);
137 for (auto & i : seq) {
138 n.add_ex("rest", i.rest);
139 n.add_ex("coeff", i.coeff);
141 n.add_ex("overall_coeff", overall_coeff);
146 // functions overriding virtual functions from base classes
151 void expairseq::do_print(const print_context & c, unsigned level) const
154 printseq(c, ',', precedence(), level);
158 void expairseq::do_print_tree(const print_tree & c, unsigned level) const
160 c.s << std::string(level, ' ') << class_name() << " @" << this
161 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
162 << ", nops=" << nops()
164 size_t num = seq.size();
165 for (size_t i=0; i<num; ++i) {
166 seq[i].rest.print(c, level + c.delta_indent);
167 seq[i].coeff.print(c, level + c.delta_indent);
169 c.s << std::string(level + c.delta_indent, ' ') << "-----" << std::endl;
171 if (!overall_coeff.is_equal(default_overall_coeff())) {
172 c.s << std::string(level + c.delta_indent, ' ') << "-----" << std::endl
173 << std::string(level + c.delta_indent, ' ') << "overall_coeff" << std::endl;
174 overall_coeff.print(c, level + c.delta_indent);
176 c.s << std::string(level + c.delta_indent,' ') << "=====" << std::endl;
179 bool expairseq::info(unsigned inf) const
182 case info_flags::expanded:
183 return (flags & status_flags::expanded);
184 case info_flags::has_indices: {
185 if (flags & status_flags::has_indices)
187 else if (flags & status_flags::has_no_indices)
189 for (auto & i : seq) {
190 if (i.rest.info(info_flags::has_indices)) {
191 this->setflag(status_flags::has_indices);
192 this->clearflag(status_flags::has_no_indices);
196 this->clearflag(status_flags::has_indices);
197 this->setflag(status_flags::has_no_indices);
201 return inherited::info(inf);
204 size_t expairseq::nops() const
206 if (overall_coeff.is_equal(default_overall_coeff()))
212 ex expairseq::op(size_t i) const
215 return recombine_pair_to_ex(seq[i]);
216 GINAC_ASSERT(!overall_coeff.is_equal(default_overall_coeff()));
217 return overall_coeff;
220 ex expairseq::map(map_function &f) const
223 v.reserve(seq.size()+1);
225 for (auto & it : seq)
226 v.push_back(split_ex_to_pair(f(recombine_pair_to_ex(it))));
228 if (overall_coeff.is_equal(default_overall_coeff()))
229 return thisexpairseq(std::move(v), default_overall_coeff(), true);
231 ex newcoeff = f(overall_coeff);
232 if(is_a<numeric>(newcoeff))
233 return thisexpairseq(std::move(v), newcoeff, true);
235 v.push_back(split_ex_to_pair(newcoeff));
236 return thisexpairseq(std::move(v), default_overall_coeff(), true);
241 /** Perform coefficient-wise automatic term rewriting rules in this class. */
242 ex expairseq::eval(int level) const
244 if ((level==1) && (flags &status_flags::evaluated))
247 const epvector evaled = evalchildren(level);
249 return dynallocate<expairseq>(std::move(evaled), overall_coeff).setflag(status_flags::evaluated);
254 epvector* conjugateepvector(const epvector&epv)
256 epvector *newepv = nullptr;
257 for (auto i=epv.begin(); i!=epv.end(); ++i) {
259 newepv->push_back(i->conjugate());
262 expair x = i->conjugate();
263 if (x.is_equal(*i)) {
266 newepv = new epvector;
267 newepv->reserve(epv.size());
268 for (epvector::const_iterator j=epv.begin(); j!=i; ++j) {
269 newepv->push_back(*j);
271 newepv->push_back(x);
276 ex expairseq::conjugate() const
278 std::unique_ptr<epvector> newepv(conjugateepvector(seq));
279 ex x = overall_coeff.conjugate();
281 return thisexpairseq(std::move(*newepv), x);
283 if (are_ex_trivially_equal(x, overall_coeff)) {
286 return thisexpairseq(seq, x);
289 bool expairseq::match(const ex & pattern, exmap & repl_lst) const
291 // This differs from basic::match() because we want "a+b+c+d" to
292 // match "d+*+b" with "*" being "a+c", and we want to honor commutativity
294 if (typeid(*this) == typeid(ex_to<basic>(pattern))) {
296 // Check whether global wildcard (one that matches the "rest of the
297 // expression", like "*" above) is present
298 bool has_global_wildcard = false;
300 for (size_t i=0; i<pattern.nops(); i++) {
301 if (is_exactly_a<wildcard>(pattern.op(i))) {
302 has_global_wildcard = true;
303 global_wildcard = pattern.op(i);
308 // Even if the expression does not match the pattern, some of
309 // its subexpressions could match it. For example, x^5*y^(-1)
310 // does not match the pattern $0^5, but its subexpression x^5
311 // does. So, save repl_lst in order to not add bogus entries.
312 exmap tmp_repl = repl_lst;
314 // Unfortunately, this is an O(N^2) operation because we can't
315 // sort the pattern in a useful way...
320 for (size_t i=0; i<nops(); i++)
321 ops.push_back(op(i));
323 // Now, for every term of the pattern, look for a matching term in
324 // the expression and remove the match
325 for (size_t i=0; i<pattern.nops(); i++) {
326 ex p = pattern.op(i);
327 if (has_global_wildcard && p.is_equal(global_wildcard))
329 auto it = ops.begin(), itend = ops.end();
330 while (it != itend) {
331 if (it->match(p, tmp_repl)) {
337 return false; // no match found
341 if (has_global_wildcard) {
343 // Assign all the remaining terms to the global wildcard (unless
344 // it has already been matched before, in which case the matches
346 size_t num = ops.size();
349 for (size_t i=0; i<num; i++)
350 vp.push_back(split_ex_to_pair(ops[i]));
351 ex rest = thisexpairseq(std::move(vp), default_overall_coeff());
352 for (auto & it : tmp_repl) {
353 if (it.first.is_equal(global_wildcard)) {
354 if (rest.is_equal(it.second)) {
362 repl_lst[global_wildcard] = rest;
367 // No global wildcard, then the match fails if there are any
368 // unmatched terms left
376 return inherited::match(pattern, repl_lst);
379 ex expairseq::subs(const exmap & m, unsigned options) const
381 epvector subsed = subschildren(m, options);
383 return ex_to<basic>(thisexpairseq(std::move(subsed), overall_coeff, (options & subs_options::no_index_renaming) == 0));
384 else if ((options & subs_options::algebraic) && is_exactly_a<mul>(*this))
385 return static_cast<const mul *>(this)->algebraic_subs_mul(m, options);
387 return subs_one_level(m, options);
392 int expairseq::compare_same_type(const basic &other) const
394 GINAC_ASSERT(is_a<expairseq>(other));
395 const expairseq &o = static_cast<const expairseq &>(other);
399 // compare number of elements
400 if (seq.size() != o.seq.size())
401 return (seq.size()<o.seq.size()) ? -1 : 1;
403 // compare overall_coeff
404 cmpval = overall_coeff.compare(o.overall_coeff);
408 auto cit1 = seq.begin(), last1 = seq.end();
409 auto cit2 = o.seq.begin(), 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);
421 bool expairseq::is_equal_same_type(const basic &other) const
423 const expairseq &o = static_cast<const expairseq &>(other);
425 // compare number of elements
426 if (seq.size()!=o.seq.size())
429 // compare overall_coeff
430 if (!overall_coeff.is_equal(o.overall_coeff))
433 auto cit2 = o.seq.begin();
434 for (auto & cit1 : seq) {
435 if (!cit1.is_equal(*cit2))
443 unsigned expairseq::return_type() const
445 return return_types::noncommutative_composite;
448 unsigned expairseq::calchash() const
450 unsigned v = make_hash_seed(typeid(*this));
451 for (auto & i : seq) {
452 v ^= i.rest.gethash();
454 v ^= i.coeff.gethash();
457 v ^= overall_coeff.gethash();
459 // store calculated hash value only if object is already evaluated
460 if (flags &status_flags::evaluated) {
461 setflag(status_flags::hash_calculated);
468 ex expairseq::expand(unsigned options) const
470 epvector expanded = expandchildren(options);
471 if (!expanded.empty()) {
472 return thisexpairseq(std::move(expanded), overall_coeff);
474 return (options == 0) ? setflag(status_flags::expanded) : *this;
478 // new virtual functions which can be overridden by derived classes
483 /** Create an object of this type.
484 * This method works similar to a constructor. It is useful because expairseq
485 * has (at least) two possible different semantics but we want to inherit
486 * methods thus avoiding code duplication. Sometimes a method in expairseq
487 * has to create a new one of the same semantics, which cannot be done by a
488 * ctor because the name (add, mul,...) is unknown on the expairseq level. In
489 * order for this trick to work a derived class must of course override this
491 ex expairseq::thisexpairseq(const epvector &v, const ex &oc, bool do_index_renaming) const
493 return expairseq(v, oc, do_index_renaming);
496 ex expairseq::thisexpairseq(epvector && vp, const ex &oc, bool do_index_renaming) const
498 return expairseq(std::move(vp), oc, do_index_renaming);
501 void expairseq::printpair(const print_context & c, const expair & p, unsigned upper_precedence) const
504 p.rest.print(c, precedence());
506 p.coeff.print(c, precedence());
510 void expairseq::printseq(const print_context & c, char delim,
511 unsigned this_precedence,
512 unsigned upper_precedence) const
514 if (this_precedence <= upper_precedence)
516 auto it = seq.begin(), it_last = seq.end() - 1;
517 for (; it!=it_last; ++it) {
518 printpair(c, *it, this_precedence);
521 printpair(c, *it, this_precedence);
522 if (!overall_coeff.is_equal(default_overall_coeff())) {
524 overall_coeff.print(c, this_precedence);
527 if (this_precedence <= upper_precedence)
532 /** Form an expair from an ex, using the corresponding semantics.
533 * @see expairseq::recombine_pair_to_ex() */
534 expair expairseq::split_ex_to_pair(const ex &e) const
536 return expair(e,_ex1);
540 expair expairseq::combine_ex_with_coeff_to_pair(const ex &e,
543 GINAC_ASSERT(is_exactly_a<numeric>(c));
549 expair expairseq::combine_pair_with_coeff_to_pair(const expair &p,
552 GINAC_ASSERT(is_exactly_a<numeric>(p.coeff));
553 GINAC_ASSERT(is_exactly_a<numeric>(c));
555 return expair(p.rest,ex_to<numeric>(p.coeff).mul_dyn(ex_to<numeric>(c)));
559 /** Form an ex out of an expair, using the corresponding semantics.
560 * @see expairseq::split_ex_to_pair() */
561 ex expairseq::recombine_pair_to_ex(const expair &p) const
563 return lst{p.rest, p.coeff};
566 bool expairseq::expair_needs_further_processing(epp it)
571 ex expairseq::default_overall_coeff() const
576 void expairseq::combine_overall_coeff(const ex &c)
578 GINAC_ASSERT(is_exactly_a<numeric>(overall_coeff));
579 GINAC_ASSERT(is_exactly_a<numeric>(c));
580 overall_coeff = ex_to<numeric>(overall_coeff).add_dyn(ex_to<numeric>(c));
583 void expairseq::combine_overall_coeff(const ex &c1, const ex &c2)
585 GINAC_ASSERT(is_exactly_a<numeric>(overall_coeff));
586 GINAC_ASSERT(is_exactly_a<numeric>(c1));
587 GINAC_ASSERT(is_exactly_a<numeric>(c2));
588 overall_coeff = ex_to<numeric>(overall_coeff).
589 add_dyn(ex_to<numeric>(c1).mul(ex_to<numeric>(c2)));
592 bool expairseq::can_make_flat(const expair &p) const
599 // non-virtual functions in this class
602 void expairseq::construct_from_2_ex_via_exvector(const ex &lh, const ex &rh)
608 construct_from_exvector(v);
611 void expairseq::construct_from_2_ex(const ex &lh, const ex &rh)
613 if (typeid(ex_to<basic>(lh)) == typeid(*this)) {
614 if (typeid(ex_to<basic>(rh)) == typeid(*this)) {
615 if (is_a<mul>(lh) && lh.info(info_flags::has_indices) &&
616 rh.info(info_flags::has_indices)) {
617 ex newrh=rename_dummy_indices_uniquely(lh, rh);
618 construct_from_2_expairseq(ex_to<expairseq>(lh),
619 ex_to<expairseq>(newrh));
622 construct_from_2_expairseq(ex_to<expairseq>(lh),
623 ex_to<expairseq>(rh));
626 construct_from_expairseq_ex(ex_to<expairseq>(lh), rh);
629 } else if (typeid(ex_to<basic>(rh)) == typeid(*this)) {
630 construct_from_expairseq_ex(ex_to<expairseq>(rh),lh);
634 if (is_exactly_a<numeric>(lh)) {
635 if (is_exactly_a<numeric>(rh)) {
636 combine_overall_coeff(lh);
637 combine_overall_coeff(rh);
639 combine_overall_coeff(lh);
640 seq.push_back(split_ex_to_pair(rh));
643 if (is_exactly_a<numeric>(rh)) {
644 combine_overall_coeff(rh);
645 seq.push_back(split_ex_to_pair(lh));
647 expair p1 = split_ex_to_pair(lh);
648 expair p2 = split_ex_to_pair(rh);
650 int cmpval = p1.rest.compare(p2.rest);
652 p1.coeff = ex_to<numeric>(p1.coeff).add_dyn(ex_to<numeric>(p2.coeff));
653 if (!ex_to<numeric>(p1.coeff).is_zero()) {
654 // no further processing is necessary, since this
655 // one element will usually be recombined in eval()
672 void expairseq::construct_from_2_expairseq(const expairseq &s1,
675 combine_overall_coeff(s1.overall_coeff);
676 combine_overall_coeff(s2.overall_coeff);
678 auto first1 = s1.seq.begin(), last1 = s1.seq.end();
679 auto first2 = s2.seq.begin(), last2 = s2.seq.end();
681 seq.reserve(s1.seq.size()+s2.seq.size());
683 bool needs_further_processing=false;
685 while (first1!=last1 && first2!=last2) {
686 int cmpval = (*first1).rest.compare((*first2).rest);
690 const numeric &newcoeff = ex_to<numeric>(first1->coeff).
691 add(ex_to<numeric>(first2->coeff));
692 if (!newcoeff.is_zero()) {
693 seq.push_back(expair(first1->rest,newcoeff));
694 if (expair_needs_further_processing(seq.end()-1)) {
695 needs_further_processing = true;
700 } else if (cmpval<0) {
701 seq.push_back(*first1);
704 seq.push_back(*first2);
709 while (first1!=last1) {
710 seq.push_back(*first1);
713 while (first2!=last2) {
714 seq.push_back(*first2);
718 if (needs_further_processing) {
719 // Clear seq and start over.
720 epvector v = std::move(seq);
721 construct_from_epvector(std::move(v));
725 void expairseq::construct_from_expairseq_ex(const expairseq &s,
728 combine_overall_coeff(s.overall_coeff);
729 if (is_exactly_a<numeric>(e)) {
730 combine_overall_coeff(e);
735 auto first = s.seq.begin(), last = s.seq.end();
736 expair p = split_ex_to_pair(e);
738 seq.reserve(s.seq.size()+1);
739 bool p_pushed = false;
741 bool needs_further_processing=false;
743 // merge p into s.seq
744 while (first!=last) {
745 int cmpval = (*first).rest.compare(p.rest);
748 const numeric &newcoeff = ex_to<numeric>(first->coeff).
749 add(ex_to<numeric>(p.coeff));
750 if (!newcoeff.is_zero()) {
751 seq.push_back(expair(first->rest,newcoeff));
752 if (expair_needs_further_processing(seq.end()-1))
753 needs_further_processing = true;
758 } else if (cmpval<0) {
759 seq.push_back(*first);
769 // while loop exited because p was pushed, now push rest of s.seq
770 while (first!=last) {
771 seq.push_back(*first);
775 // while loop exited because s.seq was pushed, now push p
779 if (needs_further_processing) {
780 // Clear seq and start over.
781 epvector v = std::move(seq);
782 construct_from_epvector(std::move(v));
786 void expairseq::construct_from_exvector(const exvector &v)
788 // simplifications: +(a,+(b,c),d) -> +(a,b,c,d) (associativity)
789 // +(d,b,c,a) -> +(a,b,c,d) (canonicalization)
790 // +(...,x,*(x,c1),*(x,c2)) -> +(...,*(x,1+c1+c2)) (c1, c2 numeric)
791 // (same for (+,*) -> (*,^)
795 combine_same_terms_sorted_seq();
798 void expairseq::construct_from_epvector(const epvector &v, bool do_index_renaming)
800 // simplifications: +(a,+(b,c),d) -> +(a,b,c,d) (associativity)
801 // +(d,b,c,a) -> +(a,b,c,d) (canonicalization)
802 // +(...,x,*(x,c1),*(x,c2)) -> +(...,*(x,1+c1+c2)) (c1, c2 numeric)
803 // same for (+,*) -> (*,^)
805 make_flat(v, do_index_renaming);
807 combine_same_terms_sorted_seq();
810 void expairseq::construct_from_epvector(epvector &&v, bool do_index_renaming)
812 // simplifications: +(a,+(b,c),d) -> +(a,b,c,d) (associativity)
813 // +(d,b,c,a) -> +(a,b,c,d) (canonicalization)
814 // +(...,x,*(x,c1),*(x,c2)) -> +(...,*(x,1+c1+c2)) (c1, c2 numeric)
815 // same for (+,*) -> (*,^)
817 make_flat(std::move(v), do_index_renaming);
819 combine_same_terms_sorted_seq();
822 /** Combine this expairseq with argument exvector.
823 * It cares for associativity as well as for special handling of numerics. */
824 void expairseq::make_flat(const exvector &v)
826 // count number of operands which are of same expairseq derived type
827 // and their cumulative number of operands
830 bool do_idx_rename = false;
832 for (auto & cit : v) {
833 if (typeid(ex_to<basic>(cit)) == typeid(*this)) {
835 noperands += ex_to<expairseq>(cit).seq.size();
837 if (is_a<mul>(*this) && (!do_idx_rename) &&
838 cit.info(info_flags::has_indices))
839 do_idx_rename = true;
842 // reserve seq and coeffseq which will hold all operands
843 seq.reserve(v.size()+noperands-nexpairseqs);
845 // copy elements and split off numerical part
846 make_flat_inserter mf(v, do_idx_rename);
847 for (auto & cit : v) {
848 if (typeid(ex_to<basic>(cit)) == typeid(*this)) {
849 ex newfactor = mf.handle_factor(cit, _ex1);
850 const expairseq &subseqref = ex_to<expairseq>(newfactor);
851 combine_overall_coeff(subseqref.overall_coeff);
852 for (auto & cit_s : subseqref.seq) {
853 seq.push_back(cit_s);
856 if (is_exactly_a<numeric>(cit))
857 combine_overall_coeff(cit);
859 ex newfactor = mf.handle_factor(cit, _ex1);
860 seq.push_back(split_ex_to_pair(newfactor));
866 /** Combine this expairseq with argument epvector.
867 * It cares for associativity as well as for special handling of numerics. */
868 void expairseq::make_flat(const epvector &v, bool do_index_renaming)
870 // count number of operands which are of same expairseq derived type
871 // and their cumulative number of operands
874 bool really_need_rename_inds = false;
876 for (auto & cit : v) {
877 if (typeid(ex_to<basic>(cit.rest)) == typeid(*this)) {
879 noperands += ex_to<expairseq>(cit.rest).seq.size();
881 if ((!really_need_rename_inds) && is_a<mul>(*this) &&
882 cit.rest.info(info_flags::has_indices))
883 really_need_rename_inds = true;
885 do_index_renaming = do_index_renaming && really_need_rename_inds;
887 // reserve seq and coeffseq which will hold all operands
888 seq.reserve(v.size()+noperands-nexpairseqs);
889 make_flat_inserter mf(v, do_index_renaming);
891 // copy elements and split off numerical part
892 for (auto & cit : v) {
893 if (typeid(ex_to<basic>(cit.rest)) == typeid(*this) &&
894 this->can_make_flat(cit)) {
895 ex newrest = mf.handle_factor(cit.rest, cit.coeff);
896 const expairseq &subseqref = ex_to<expairseq>(newrest);
897 combine_overall_coeff(ex_to<numeric>(subseqref.overall_coeff),
898 ex_to<numeric>(cit.coeff));
899 for (auto & cit_s : subseqref.seq) {
900 seq.push_back(expair(cit_s.rest,
901 ex_to<numeric>(cit_s.coeff).mul_dyn(ex_to<numeric>(cit.coeff))));
904 if (cit.is_canonical_numeric())
905 combine_overall_coeff(mf.handle_factor(cit.rest, _ex1));
908 ex newrest = mf.handle_factor(rest, cit.coeff);
909 if (are_ex_trivially_equal(newrest, rest))
912 seq.push_back(expair(newrest, cit.coeff));
918 /** Brings this expairseq into a sorted (canonical) form. */
919 void expairseq::canonicalize()
921 std::sort(seq.begin(), seq.end(), expair_rest_is_less());
925 /** Compact a presorted expairseq by combining all matching expairs to one
926 * each. On an add object, this is responsible for 2*x+3*x+y -> 5*x+y, for
928 void expairseq::combine_same_terms_sorted_seq()
933 bool needs_further_processing = false;
935 auto itin1 = seq.begin();
936 auto itin2 = itin1 + 1;
938 auto last = seq.end();
939 // must_copy will be set to true the first time some combination is
940 // possible from then on the sequence has changed and must be compacted
941 bool must_copy = false;
942 while (itin2!=last) {
943 if (itin1->rest.compare(itin2->rest)==0) {
944 itin1->coeff = ex_to<numeric>(itin1->coeff).
945 add_dyn(ex_to<numeric>(itin2->coeff));
946 if (expair_needs_further_processing(itin1))
947 needs_further_processing = true;
950 if (!ex_to<numeric>(itin1->coeff).is_zero()) {
959 if (!ex_to<numeric>(itin1->coeff).is_zero()) {
965 seq.erase(itout,last);
967 if (needs_further_processing) {
968 // Clear seq and start over.
969 epvector v = std::move(seq);
970 construct_from_epvector(std::move(v));
974 /** Check if this expairseq is in sorted (canonical) form. Useful mainly for
975 * debugging or in assertions since being sorted is an invariance. */
976 bool expairseq::is_canonical() const
981 auto it = seq.begin(), itend = seq.end();
983 for (++it; it!=itend; it_last=it, ++it) {
984 if (!(it_last->is_less(*it) || it_last->is_equal(*it))) {
985 if (!is_exactly_a<numeric>(it_last->rest) ||
986 !is_exactly_a<numeric>(it->rest)) {
987 // double test makes it easier to set a breakpoint...
988 if (!is_exactly_a<numeric>(it_last->rest) ||
989 !is_exactly_a<numeric>(it->rest)) {
990 printpair(std::clog, *it_last, 0);
992 printpair(std::clog, *it, 0);
994 std::clog << "pair1:" << std::endl;
995 it_last->rest.print(print_tree(std::clog));
996 it_last->coeff.print(print_tree(std::clog));
997 std::clog << "pair2:" << std::endl;
998 it->rest.print(print_tree(std::clog));
999 it->coeff.print(print_tree(std::clog));
1008 /** Member-wise expand the expairs in this sequence.
1010 * @see expairseq::expand()
1011 * @return epvector containing expanded pairs, empty if no members
1012 * had to be changed. */
1013 epvector expairseq::expandchildren(unsigned options) const
1015 auto cit = seq.begin(), last = seq.end();
1017 const ex expanded_ex = cit->rest.expand(options);
1018 if (!are_ex_trivially_equal(cit->rest,expanded_ex)) {
1020 // something changed, copy seq, eval and return it
1022 s.reserve(seq.size());
1024 // copy parts of seq which are known not to have changed
1025 s.insert(s.begin(), seq.begin(), cit);
1027 // copy first changed element
1028 s.push_back(expair(expanded_ex, cit->coeff));
1032 while (cit != last) {
1033 s.push_back(expair(cit->rest.expand(options), cit->coeff));
1042 return epvector(); // empty signalling nothing has changed
1046 /** Member-wise evaluate the expairs in this sequence.
1048 * @see expairseq::eval()
1049 * @return epvector containing evaluated pairs, empty if no members
1050 * had to be changed. */
1051 epvector expairseq::evalchildren(int level) const
1053 if (level == -max_recursion_level)
1054 throw(std::runtime_error("max recursion level reached"));
1056 auto cit = seq.begin(), last = seq.end();
1058 const ex evaled_rest = level==1 ? cit->rest : cit->rest.eval(level-1);
1059 const expair evaled_pair = combine_ex_with_coeff_to_pair(evaled_rest, cit->coeff);
1060 if (unlikely(!evaled_pair.is_equal(*cit))) {
1062 // something changed: copy seq, eval and return it
1064 s.reserve(seq.size());
1066 // copy parts of seq which are known not to have changed
1067 s.insert(s.begin(), seq.begin(), cit);
1069 // copy first changed element
1070 s.push_back(evaled_pair);
1074 while (cit != last) {
1075 const ex evaled_rest = level==1 ? cit->rest : cit->rest.eval(level-1);
1076 s.push_back(combine_ex_with_coeff_to_pair(evaled_rest, cit->coeff));
1085 return epvector(); // signalling nothing has changed
1088 /** Member-wise substitute in this sequence.
1090 * @see expairseq::subs()
1091 * @return epvector containing expanded pairs, empty if no members
1092 * had to be changed. */
1093 epvector expairseq::subschildren(const exmap & m, unsigned options) const
1095 // When any of the objects to be substituted is a product or power
1096 // we have to recombine the pairs because the numeric coefficients may
1097 // be part of the search pattern.
1098 if (!(options & (subs_options::pattern_is_product | subs_options::pattern_is_not_product))) {
1100 // Search the list of substitutions and cache our findings
1101 for (auto & it : m) {
1102 if (is_exactly_a<mul>(it.first) || is_exactly_a<power>(it.first)) {
1103 options |= subs_options::pattern_is_product;
1107 if (!(options & subs_options::pattern_is_product))
1108 options |= subs_options::pattern_is_not_product;
1111 if (options & subs_options::pattern_is_product) {
1113 // Substitute in the recombined pairs
1114 auto cit = seq.begin(), last = seq.end();
1115 while (cit != last) {
1117 const ex &orig_ex = recombine_pair_to_ex(*cit);
1118 const ex &subsed_ex = orig_ex.subs(m, options);
1119 if (!are_ex_trivially_equal(orig_ex, subsed_ex)) {
1121 // Something changed: copy seq, subs and return it
1123 s.reserve(seq.size());
1125 // Copy parts of seq which are known not to have changed
1126 s.insert(s.begin(), seq.begin(), cit);
1128 // Copy first changed element
1129 s.push_back(split_ex_to_pair(subsed_ex));
1133 while (cit != last) {
1134 s.push_back(split_ex_to_pair(recombine_pair_to_ex(*cit).subs(m, options)));
1145 // Substitute only in the "rest" part of the pairs
1146 auto cit = seq.begin(), last = seq.end();
1147 while (cit != last) {
1149 const ex subsed_rest = cit->rest.subs(m, options);
1150 const expair subsed_pair = combine_ex_with_coeff_to_pair(subsed_rest, cit->coeff);
1151 if (!subsed_pair.is_equal(*cit)) {
1153 // Something changed: copy seq, subs and return it
1155 s.reserve(seq.size());
1157 // Copy parts of seq which are known not to have changed
1158 s.insert(s.begin(), seq.begin(), cit);
1160 // Copy first changed element
1161 s.push_back(subsed_pair);
1165 while (cit != last) {
1166 s.push_back(combine_ex_with_coeff_to_pair(cit->rest.subs(m, options), cit->coeff));
1176 // Nothing has changed
1181 // static member variables
1184 } // namespace GiNaC