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"
46 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(expairseq, basic,
47 print_func<print_context>(&expairseq::do_print).
48 print_func<print_tree>(&expairseq::do_print_tree))
58 bool operator()(const epp &lh, const epp &rh) const
60 return (*lh).is_less(*rh);
65 // default constructor
70 expairseq::expairseq()
79 expairseq::expairseq(const ex &lh, const ex &rh)
81 construct_from_2_ex(lh,rh);
82 GINAC_ASSERT(is_canonical());
85 expairseq::expairseq(const exvector &v)
87 construct_from_exvector(v);
88 GINAC_ASSERT(is_canonical());
91 expairseq::expairseq(const epvector &v, const ex &oc, bool do_index_renaming)
94 GINAC_ASSERT(is_a<numeric>(oc));
95 construct_from_epvector(v, do_index_renaming);
96 GINAC_ASSERT(is_canonical());
99 expairseq::expairseq(epvector && vp, const ex &oc, bool do_index_renaming)
102 GINAC_ASSERT(is_a<numeric>(oc));
103 construct_from_epvector(std::move(vp), do_index_renaming);
104 GINAC_ASSERT(is_canonical());
111 void expairseq::read_archive(const archive_node &n, lst &sym_lst)
113 inherited::read_archive(n, sym_lst);
114 auto first = n.find_first("rest");
115 auto last = n.find_last("coeff");
117 seq.reserve((last-first)/2);
119 for (auto loc = first; loc < last;) {
122 n.find_ex_by_loc(loc++, rest, sym_lst);
123 n.find_ex_by_loc(loc++, coeff, sym_lst);
124 seq.push_back(expair(rest, coeff));
127 n.find_ex("overall_coeff", overall_coeff, sym_lst);
130 GINAC_ASSERT(is_canonical());
133 void expairseq::archive(archive_node &n) const
135 inherited::archive(n);
136 for (auto & i : seq) {
137 n.add_ex("rest", i.rest);
138 n.add_ex("coeff", i.coeff);
140 n.add_ex("overall_coeff", overall_coeff);
145 // functions overriding virtual functions from base classes
150 void expairseq::do_print(const print_context & c, unsigned level) const
153 printseq(c, ',', precedence(), level);
157 void expairseq::do_print_tree(const print_tree & c, unsigned level) const
159 c.s << std::string(level, ' ') << class_name() << " @" << this
160 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
161 << ", nops=" << nops()
163 size_t num = seq.size();
164 for (size_t i=0; i<num; ++i) {
165 seq[i].rest.print(c, level + c.delta_indent);
166 seq[i].coeff.print(c, level + c.delta_indent);
168 c.s << std::string(level + c.delta_indent, ' ') << "-----" << std::endl;
170 if (!overall_coeff.is_equal(default_overall_coeff())) {
171 c.s << std::string(level + c.delta_indent, ' ') << "-----" << std::endl
172 << std::string(level + c.delta_indent, ' ') << "overall_coeff" << std::endl;
173 overall_coeff.print(c, level + c.delta_indent);
175 c.s << std::string(level + c.delta_indent,' ') << "=====" << std::endl;
178 bool expairseq::info(unsigned inf) const
181 case info_flags::expanded:
182 return (flags & status_flags::expanded);
183 case info_flags::has_indices: {
184 if (flags & status_flags::has_indices)
186 else if (flags & status_flags::has_no_indices)
188 for (auto & i : seq) {
189 if (i.rest.info(info_flags::has_indices)) {
190 this->setflag(status_flags::has_indices);
191 this->clearflag(status_flags::has_no_indices);
195 this->clearflag(status_flags::has_indices);
196 this->setflag(status_flags::has_no_indices);
200 return inherited::info(inf);
203 size_t expairseq::nops() const
205 if (overall_coeff.is_equal(default_overall_coeff()))
211 ex expairseq::op(size_t i) const
214 return recombine_pair_to_ex(seq[i]);
215 GINAC_ASSERT(!overall_coeff.is_equal(default_overall_coeff()));
216 return overall_coeff;
219 ex expairseq::map(map_function &f) const
222 v.reserve(seq.size()+1);
224 for (auto & it : seq)
225 v.push_back(split_ex_to_pair(f(recombine_pair_to_ex(it))));
227 if (overall_coeff.is_equal(default_overall_coeff()))
228 return thisexpairseq(std::move(v), default_overall_coeff(), true);
230 ex newcoeff = f(overall_coeff);
231 if(is_a<numeric>(newcoeff))
232 return thisexpairseq(std::move(v), newcoeff, true);
234 v.push_back(split_ex_to_pair(newcoeff));
235 return thisexpairseq(std::move(v), default_overall_coeff(), true);
240 /** Perform coefficient-wise automatic term rewriting rules in this class. */
241 ex expairseq::eval(int level) const
243 if ((level==1) && (flags &status_flags::evaluated))
246 epvector evaled = evalchildren(level);
248 return (new expairseq(std::move(evaled), overall_coeff))->setflag(status_flags::dynallocated | status_flags::evaluated);
253 epvector* conjugateepvector(const epvector&epv)
255 epvector *newepv = nullptr;
256 for (auto i=epv.begin(); i!=epv.end(); ++i) {
258 newepv->push_back(i->conjugate());
261 expair x = i->conjugate();
262 if (x.is_equal(*i)) {
265 newepv = new epvector;
266 newepv->reserve(epv.size());
267 for (epvector::const_iterator j=epv.begin(); j!=i; ++j) {
268 newepv->push_back(*j);
270 newepv->push_back(x);
275 ex expairseq::conjugate() const
277 std::unique_ptr<epvector> newepv(conjugateepvector(seq));
278 ex x = overall_coeff.conjugate();
280 return thisexpairseq(std::move(*newepv), x);
282 if (are_ex_trivially_equal(x, overall_coeff)) {
285 return thisexpairseq(seq, x);
288 bool expairseq::match(const ex & pattern, exmap & repl_lst) const
290 // This differs from basic::match() because we want "a+b+c+d" to
291 // match "d+*+b" with "*" being "a+c", and we want to honor commutativity
293 if (typeid(*this) == typeid(ex_to<basic>(pattern))) {
295 // Check whether global wildcard (one that matches the "rest of the
296 // expression", like "*" above) is present
297 bool has_global_wildcard = false;
299 for (size_t i=0; i<pattern.nops(); i++) {
300 if (is_exactly_a<wildcard>(pattern.op(i))) {
301 has_global_wildcard = true;
302 global_wildcard = pattern.op(i);
307 // Even if the expression does not match the pattern, some of
308 // its subexpressions could match it. For example, x^5*y^(-1)
309 // does not match the pattern $0^5, but its subexpression x^5
310 // does. So, save repl_lst in order to not add bogus entries.
311 exmap tmp_repl = repl_lst;
313 // Unfortunately, this is an O(N^2) operation because we can't
314 // sort the pattern in a useful way...
319 for (size_t i=0; i<nops(); i++)
320 ops.push_back(op(i));
322 // Now, for every term of the pattern, look for a matching term in
323 // the expression and remove the match
324 for (size_t i=0; i<pattern.nops(); i++) {
325 ex p = pattern.op(i);
326 if (has_global_wildcard && p.is_equal(global_wildcard))
328 auto it = ops.begin(), itend = ops.end();
329 while (it != itend) {
330 if (it->match(p, tmp_repl)) {
336 return false; // no match found
340 if (has_global_wildcard) {
342 // Assign all the remaining terms to the global wildcard (unless
343 // it has already been matched before, in which case the matches
345 size_t num = ops.size();
348 for (size_t i=0; i<num; i++)
349 vp.push_back(split_ex_to_pair(ops[i]));
350 ex rest = thisexpairseq(std::move(vp), default_overall_coeff());
351 for (auto & it : tmp_repl) {
352 if (it.first.is_equal(global_wildcard)) {
353 if (rest.is_equal(it.second)) {
361 repl_lst[global_wildcard] = rest;
366 // No global wildcard, then the match fails if there are any
367 // unmatched terms left
375 return inherited::match(pattern, repl_lst);
378 ex expairseq::subs(const exmap & m, unsigned options) const
380 epvector subsed = subschildren(m, options);
382 return ex_to<basic>(thisexpairseq(std::move(subsed), overall_coeff, (options & subs_options::no_index_renaming) == 0));
383 else if ((options & subs_options::algebraic) && is_exactly_a<mul>(*this))
384 return static_cast<const mul *>(this)->algebraic_subs_mul(m, options);
386 return subs_one_level(m, options);
391 int expairseq::compare_same_type(const basic &other) const
393 GINAC_ASSERT(is_a<expairseq>(other));
394 const expairseq &o = static_cast<const expairseq &>(other);
398 // compare number of elements
399 if (seq.size() != o.seq.size())
400 return (seq.size()<o.seq.size()) ? -1 : 1;
402 // compare overall_coeff
403 cmpval = overall_coeff.compare(o.overall_coeff);
407 auto cit1 = seq.begin(), last1 = seq.end();
408 auto cit2 = o.seq.begin(), last2 = o.seq.end();
409 for (; (cit1!=last1) && (cit2!=last2); ++cit1, ++cit2) {
410 cmpval = (*cit1).compare(*cit2);
411 if (cmpval!=0) return cmpval;
414 GINAC_ASSERT(cit1==last1);
415 GINAC_ASSERT(cit2==last2);
420 bool expairseq::is_equal_same_type(const basic &other) const
422 const expairseq &o = static_cast<const expairseq &>(other);
424 // compare number of elements
425 if (seq.size()!=o.seq.size())
428 // compare overall_coeff
429 if (!overall_coeff.is_equal(o.overall_coeff))
432 auto cit2 = o.seq.begin();
433 for (auto & cit1 : seq) {
434 if (!cit1.is_equal(*cit2))
442 unsigned expairseq::return_type() const
444 return return_types::noncommutative_composite;
447 unsigned expairseq::calchash() const
449 unsigned v = make_hash_seed(typeid(*this));
450 for (auto & i : seq) {
451 v ^= i.rest.gethash();
453 v ^= i.coeff.gethash();
456 v ^= overall_coeff.gethash();
458 // store calculated hash value only if object is already evaluated
459 if (flags &status_flags::evaluated) {
460 setflag(status_flags::hash_calculated);
467 ex expairseq::expand(unsigned options) const
469 epvector expanded = expandchildren(options);
470 if (!expanded.empty()) {
471 return thisexpairseq(std::move(expanded), overall_coeff);
473 return (options == 0) ? setflag(status_flags::expanded) : *this;
477 // new virtual functions which can be overridden by derived classes
482 /** Create an object of this type.
483 * This method works similar to a constructor. It is useful because expairseq
484 * has (at least) two possible different semantics but we want to inherit
485 * methods thus avoiding code duplication. Sometimes a method in expairseq
486 * has to create a new one of the same semantics, which cannot be done by a
487 * ctor because the name (add, mul,...) is unknown on the expairseq level. In
488 * order for this trick to work a derived class must of course override this
490 ex expairseq::thisexpairseq(const epvector &v, const ex &oc, bool do_index_renaming) const
492 return expairseq(v, oc, do_index_renaming);
495 ex expairseq::thisexpairseq(epvector && vp, const ex &oc, bool do_index_renaming) const
497 return expairseq(std::move(vp), oc, do_index_renaming);
500 void expairseq::printpair(const print_context & c, const expair & p, unsigned upper_precedence) const
503 p.rest.print(c, precedence());
505 p.coeff.print(c, precedence());
509 void expairseq::printseq(const print_context & c, char delim,
510 unsigned this_precedence,
511 unsigned upper_precedence) const
513 if (this_precedence <= upper_precedence)
515 auto it = seq.begin(), it_last = seq.end() - 1;
516 for (; it!=it_last; ++it) {
517 printpair(c, *it, this_precedence);
520 printpair(c, *it, this_precedence);
521 if (!overall_coeff.is_equal(default_overall_coeff())) {
523 overall_coeff.print(c, this_precedence);
526 if (this_precedence <= upper_precedence)
531 /** Form an expair from an ex, using the corresponding semantics.
532 * @see expairseq::recombine_pair_to_ex() */
533 expair expairseq::split_ex_to_pair(const ex &e) const
535 return expair(e,_ex1);
539 expair expairseq::combine_ex_with_coeff_to_pair(const ex &e,
542 GINAC_ASSERT(is_exactly_a<numeric>(c));
548 expair expairseq::combine_pair_with_coeff_to_pair(const expair &p,
551 GINAC_ASSERT(is_exactly_a<numeric>(p.coeff));
552 GINAC_ASSERT(is_exactly_a<numeric>(c));
554 return expair(p.rest,ex_to<numeric>(p.coeff).mul_dyn(ex_to<numeric>(c)));
558 /** Form an ex out of an expair, using the corresponding semantics.
559 * @see expairseq::split_ex_to_pair() */
560 ex expairseq::recombine_pair_to_ex(const expair &p) const
562 return lst(p.rest,p.coeff);
565 bool expairseq::expair_needs_further_processing(epp it)
570 ex expairseq::default_overall_coeff() const
575 void expairseq::combine_overall_coeff(const ex &c)
577 GINAC_ASSERT(is_exactly_a<numeric>(overall_coeff));
578 GINAC_ASSERT(is_exactly_a<numeric>(c));
579 overall_coeff = ex_to<numeric>(overall_coeff).add_dyn(ex_to<numeric>(c));
582 void expairseq::combine_overall_coeff(const ex &c1, const ex &c2)
584 GINAC_ASSERT(is_exactly_a<numeric>(overall_coeff));
585 GINAC_ASSERT(is_exactly_a<numeric>(c1));
586 GINAC_ASSERT(is_exactly_a<numeric>(c2));
587 overall_coeff = ex_to<numeric>(overall_coeff).
588 add_dyn(ex_to<numeric>(c1).mul(ex_to<numeric>(c2)));
591 bool expairseq::can_make_flat(const expair &p) const
598 // non-virtual functions in this class
601 void expairseq::construct_from_2_ex_via_exvector(const ex &lh, const ex &rh)
607 construct_from_exvector(v);
610 void expairseq::construct_from_2_ex(const ex &lh, const ex &rh)
612 if (typeid(ex_to<basic>(lh)) == typeid(*this)) {
613 if (typeid(ex_to<basic>(rh)) == typeid(*this)) {
614 if (is_a<mul>(lh) && lh.info(info_flags::has_indices) &&
615 rh.info(info_flags::has_indices)) {
616 ex newrh=rename_dummy_indices_uniquely(lh, rh);
617 construct_from_2_expairseq(ex_to<expairseq>(lh),
618 ex_to<expairseq>(newrh));
621 construct_from_2_expairseq(ex_to<expairseq>(lh),
622 ex_to<expairseq>(rh));
625 construct_from_expairseq_ex(ex_to<expairseq>(lh), rh);
628 } else if (typeid(ex_to<basic>(rh)) == typeid(*this)) {
629 construct_from_expairseq_ex(ex_to<expairseq>(rh),lh);
633 if (is_exactly_a<numeric>(lh)) {
634 if (is_exactly_a<numeric>(rh)) {
635 combine_overall_coeff(lh);
636 combine_overall_coeff(rh);
638 combine_overall_coeff(lh);
639 seq.push_back(split_ex_to_pair(rh));
642 if (is_exactly_a<numeric>(rh)) {
643 combine_overall_coeff(rh);
644 seq.push_back(split_ex_to_pair(lh));
646 expair p1 = split_ex_to_pair(lh);
647 expair p2 = split_ex_to_pair(rh);
649 int cmpval = p1.rest.compare(p2.rest);
651 p1.coeff = ex_to<numeric>(p1.coeff).add_dyn(ex_to<numeric>(p2.coeff));
652 if (!ex_to<numeric>(p1.coeff).is_zero()) {
653 // no further processing is necessary, since this
654 // one element will usually be recombined in eval()
671 void expairseq::construct_from_2_expairseq(const expairseq &s1,
674 combine_overall_coeff(s1.overall_coeff);
675 combine_overall_coeff(s2.overall_coeff);
677 auto first1 = s1.seq.begin(), last1 = s1.seq.end();
678 auto first2 = s2.seq.begin(), last2 = s2.seq.end();
680 seq.reserve(s1.seq.size()+s2.seq.size());
682 bool needs_further_processing=false;
684 while (first1!=last1 && first2!=last2) {
685 int cmpval = (*first1).rest.compare((*first2).rest);
689 const numeric &newcoeff = ex_to<numeric>(first1->coeff).
690 add(ex_to<numeric>(first2->coeff));
691 if (!newcoeff.is_zero()) {
692 seq.push_back(expair(first1->rest,newcoeff));
693 if (expair_needs_further_processing(seq.end()-1)) {
694 needs_further_processing = true;
699 } else if (cmpval<0) {
700 seq.push_back(*first1);
703 seq.push_back(*first2);
708 while (first1!=last1) {
709 seq.push_back(*first1);
712 while (first2!=last2) {
713 seq.push_back(*first2);
717 if (needs_further_processing) {
720 construct_from_epvector(std::move(v));
724 void expairseq::construct_from_expairseq_ex(const expairseq &s,
727 combine_overall_coeff(s.overall_coeff);
728 if (is_exactly_a<numeric>(e)) {
729 combine_overall_coeff(e);
734 auto first = s.seq.begin(), last = s.seq.end();
735 expair p = split_ex_to_pair(e);
737 seq.reserve(s.seq.size()+1);
738 bool p_pushed = false;
740 bool needs_further_processing=false;
742 // merge p into s.seq
743 while (first!=last) {
744 int cmpval = (*first).rest.compare(p.rest);
747 const numeric &newcoeff = ex_to<numeric>(first->coeff).
748 add(ex_to<numeric>(p.coeff));
749 if (!newcoeff.is_zero()) {
750 seq.push_back(expair(first->rest,newcoeff));
751 if (expair_needs_further_processing(seq.end()-1))
752 needs_further_processing = true;
757 } else if (cmpval<0) {
758 seq.push_back(*first);
768 // while loop exited because p was pushed, now push rest of s.seq
769 while (first!=last) {
770 seq.push_back(*first);
774 // while loop exited because s.seq was pushed, now push p
778 if (needs_further_processing) {
781 construct_from_epvector(std::move(v));
785 void expairseq::construct_from_exvector(const exvector &v)
787 // simplifications: +(a,+(b,c),d) -> +(a,b,c,d) (associativity)
788 // +(d,b,c,a) -> +(a,b,c,d) (canonicalization)
789 // +(...,x,*(x,c1),*(x,c2)) -> +(...,*(x,1+c1+c2)) (c1, c2 numeric)
790 // (same for (+,*) -> (*,^)
794 combine_same_terms_sorted_seq();
797 void expairseq::construct_from_epvector(const epvector &v, bool do_index_renaming)
799 // simplifications: +(a,+(b,c),d) -> +(a,b,c,d) (associativity)
800 // +(d,b,c,a) -> +(a,b,c,d) (canonicalization)
801 // +(...,x,*(x,c1),*(x,c2)) -> +(...,*(x,1+c1+c2)) (c1, c2 numeric)
802 // same for (+,*) -> (*,^)
804 make_flat(v, do_index_renaming);
806 combine_same_terms_sorted_seq();
809 void expairseq::construct_from_epvector(epvector &&v, bool do_index_renaming)
811 // simplifications: +(a,+(b,c),d) -> +(a,b,c,d) (associativity)
812 // +(d,b,c,a) -> +(a,b,c,d) (canonicalization)
813 // +(...,x,*(x,c1),*(x,c2)) -> +(...,*(x,1+c1+c2)) (c1, c2 numeric)
814 // same for (+,*) -> (*,^)
816 make_flat(std::move(v), do_index_renaming);
818 combine_same_terms_sorted_seq();
821 /** Combine this expairseq with argument exvector.
822 * It cares for associativity as well as for special handling of numerics. */
823 void expairseq::make_flat(const exvector &v)
825 // count number of operands which are of same expairseq derived type
826 // and their cumulative number of operands
829 bool do_idx_rename = false;
831 for (auto & cit : v) {
832 if (typeid(ex_to<basic>(cit)) == typeid(*this)) {
834 noperands += ex_to<expairseq>(cit).seq.size();
836 if (is_a<mul>(*this) && (!do_idx_rename) &&
837 cit.info(info_flags::has_indices))
838 do_idx_rename = true;
841 // reserve seq and coeffseq which will hold all operands
842 seq.reserve(v.size()+noperands-nexpairseqs);
844 // copy elements and split off numerical part
845 make_flat_inserter mf(v, do_idx_rename);
846 for (auto & cit : v) {
847 if (typeid(ex_to<basic>(cit)) == typeid(*this)) {
848 ex newfactor = mf.handle_factor(cit, _ex1);
849 const expairseq &subseqref = ex_to<expairseq>(newfactor);
850 combine_overall_coeff(subseqref.overall_coeff);
851 for (auto & cit_s : subseqref.seq) {
852 seq.push_back(cit_s);
855 if (is_exactly_a<numeric>(cit))
856 combine_overall_coeff(cit);
858 ex newfactor = mf.handle_factor(cit, _ex1);
859 seq.push_back(split_ex_to_pair(newfactor));
865 /** Combine this expairseq with argument epvector.
866 * It cares for associativity as well as for special handling of numerics. */
867 void expairseq::make_flat(const epvector &v, bool do_index_renaming)
869 // count number of operands which are of same expairseq derived type
870 // and their cumulative number of operands
873 bool really_need_rename_inds = false;
875 for (auto & cit : v) {
876 if (typeid(ex_to<basic>(cit.rest)) == typeid(*this)) {
878 noperands += ex_to<expairseq>(cit.rest).seq.size();
880 if ((!really_need_rename_inds) && is_a<mul>(*this) &&
881 cit.rest.info(info_flags::has_indices))
882 really_need_rename_inds = true;
884 do_index_renaming = do_index_renaming && really_need_rename_inds;
886 // reserve seq and coeffseq which will hold all operands
887 seq.reserve(v.size()+noperands-nexpairseqs);
888 make_flat_inserter mf(v, do_index_renaming);
890 // copy elements and split off numerical part
891 for (auto & cit : v) {
892 if (typeid(ex_to<basic>(cit.rest)) == typeid(*this) &&
893 this->can_make_flat(cit)) {
894 ex newrest = mf.handle_factor(cit.rest, cit.coeff);
895 const expairseq &subseqref = ex_to<expairseq>(newrest);
896 combine_overall_coeff(ex_to<numeric>(subseqref.overall_coeff),
897 ex_to<numeric>(cit.coeff));
898 for (auto & cit_s : subseqref.seq) {
899 seq.push_back(expair(cit_s.rest,
900 ex_to<numeric>(cit_s.coeff).mul_dyn(ex_to<numeric>(cit.coeff))));
903 if (cit.is_canonical_numeric())
904 combine_overall_coeff(mf.handle_factor(cit.rest, _ex1));
907 ex newrest = mf.handle_factor(rest, cit.coeff);
908 if (are_ex_trivially_equal(newrest, rest))
911 seq.push_back(expair(newrest, cit.coeff));
917 /** Brings this expairseq into a sorted (canonical) form. */
918 void expairseq::canonicalize()
920 std::sort(seq.begin(), seq.end(), expair_rest_is_less());
924 /** Compact a presorted expairseq by combining all matching expairs to one
925 * each. On an add object, this is responsible for 2*x+3*x+y -> 5*x+y, for
927 void expairseq::combine_same_terms_sorted_seq()
932 bool needs_further_processing = false;
934 auto itin1 = seq.begin();
935 auto itin2 = itin1 + 1;
937 auto last = seq.end();
938 // must_copy will be set to true the first time some combination is
939 // possible from then on the sequence has changed and must be compacted
940 bool must_copy = false;
941 while (itin2!=last) {
942 if (itin1->rest.compare(itin2->rest)==0) {
943 itin1->coeff = ex_to<numeric>(itin1->coeff).
944 add_dyn(ex_to<numeric>(itin2->coeff));
945 if (expair_needs_further_processing(itin1))
946 needs_further_processing = true;
949 if (!ex_to<numeric>(itin1->coeff).is_zero()) {
958 if (!ex_to<numeric>(itin1->coeff).is_zero()) {
964 seq.erase(itout,last);
966 if (needs_further_processing) {
969 construct_from_epvector(std::move(v));
973 /** Check if this expairseq is in sorted (canonical) form. Useful mainly for
974 * debugging or in assertions since being sorted is an invariance. */
975 bool expairseq::is_canonical() const
980 auto it = seq.begin(), itend = seq.end();
982 for (++it; it!=itend; it_last=it, ++it) {
983 if (!(it_last->is_less(*it) || it_last->is_equal(*it))) {
984 if (!is_exactly_a<numeric>(it_last->rest) ||
985 !is_exactly_a<numeric>(it->rest)) {
986 // double test makes it easier to set a breakpoint...
987 if (!is_exactly_a<numeric>(it_last->rest) ||
988 !is_exactly_a<numeric>(it->rest)) {
989 printpair(std::clog, *it_last, 0);
991 printpair(std::clog, *it, 0);
993 std::clog << "pair1:" << std::endl;
994 it_last->rest.print(print_tree(std::clog));
995 it_last->coeff.print(print_tree(std::clog));
996 std::clog << "pair2:" << std::endl;
997 it->rest.print(print_tree(std::clog));
998 it->coeff.print(print_tree(std::clog));
1007 /** Member-wise expand the expairs in this sequence.
1009 * @see expairseq::expand()
1010 * @return epvector containing expanded pairs, empty if no members
1011 * had to be changed. */
1012 epvector expairseq::expandchildren(unsigned options) const
1014 auto cit = seq.begin(), last = seq.end();
1016 const ex &expanded_ex = cit->rest.expand(options);
1017 if (!are_ex_trivially_equal(cit->rest,expanded_ex)) {
1019 // something changed, copy seq, eval and return it
1021 s.reserve(seq.size());
1023 // copy parts of seq which are known not to have changed
1024 auto cit2 = seq.begin();
1030 // copy first changed element
1031 s.push_back(combine_ex_with_coeff_to_pair(expanded_ex,
1036 while (cit2!=last) {
1037 s.push_back(combine_ex_with_coeff_to_pair(cit2->rest.expand(options),
1046 return epvector(); // empty signalling nothing has changed
1050 /** Member-wise evaluate the expairs in this sequence.
1052 * @see expairseq::eval()
1053 * @return epvector containing evaluated pairs, empty if no members
1054 * had to be changed. */
1055 epvector expairseq::evalchildren(int level) const
1057 if (likely(level==1))
1058 return epvector(); // nothing had to be evaluated
1060 if (level == -max_recursion_level)
1061 throw(std::runtime_error("max recursion level reached"));
1064 auto cit = seq.begin(), last = seq.end();
1066 const ex evaled_ex = cit->rest.eval(level);
1067 if (!are_ex_trivially_equal(cit->rest,evaled_ex)) {
1069 // something changed, copy seq, eval and return it
1071 s.reserve(seq.size());
1073 // copy parts of seq which are known not to have changed
1074 auto cit2 = seq.begin();
1080 // copy first changed element
1081 s.push_back(combine_ex_with_coeff_to_pair(evaled_ex,
1086 while (cit2!=last) {
1087 s.push_back(combine_ex_with_coeff_to_pair(cit2->rest.eval(level),
1091 return std::move(s);
1096 return epvector(); // signalling nothing has changed
1099 /** Member-wise substitute in this sequence.
1101 * @see expairseq::subs()
1102 * @return epvector containing expanded pairs, empty if no members
1103 * had to be changed. */
1104 epvector expairseq::subschildren(const exmap & m, unsigned options) const
1106 // When any of the objects to be substituted is a product or power
1107 // we have to recombine the pairs because the numeric coefficients may
1108 // be part of the search pattern.
1109 if (!(options & (subs_options::pattern_is_product | subs_options::pattern_is_not_product))) {
1111 // Search the list of substitutions and cache our findings
1112 for (auto & it : m) {
1113 if (is_exactly_a<mul>(it.first) || is_exactly_a<power>(it.first)) {
1114 options |= subs_options::pattern_is_product;
1118 if (!(options & subs_options::pattern_is_product))
1119 options |= subs_options::pattern_is_not_product;
1122 if (options & subs_options::pattern_is_product) {
1124 // Substitute in the recombined pairs
1125 auto cit = seq.begin(), last = seq.end();
1126 while (cit != last) {
1128 const ex &orig_ex = recombine_pair_to_ex(*cit);
1129 const ex &subsed_ex = orig_ex.subs(m, options);
1130 if (!are_ex_trivially_equal(orig_ex, subsed_ex)) {
1132 // Something changed, copy seq, subs and return it
1134 s.reserve(seq.size());
1136 // Copy parts of seq which are known not to have changed
1137 s.insert(s.begin(), seq.begin(), cit);
1139 // Copy first changed element
1140 s.push_back(split_ex_to_pair(subsed_ex));
1144 while (cit != last) {
1145 s.push_back(split_ex_to_pair(recombine_pair_to_ex(*cit).subs(m, options)));
1156 // Substitute only in the "rest" part of the pairs
1157 auto cit = seq.begin(), last = seq.end();
1158 while (cit != last) {
1160 const ex &subsed_ex = cit->rest.subs(m, options);
1161 if (!are_ex_trivially_equal(cit->rest, subsed_ex)) {
1163 // Something changed, copy seq, subs and return it
1165 s.reserve(seq.size());
1167 // Copy parts of seq which are known not to have changed
1168 s.insert(s.begin(), seq.begin(), cit);
1170 // Copy first changed element
1171 s.push_back(combine_ex_with_coeff_to_pair(subsed_ex, cit->coeff));
1175 while (cit != last) {
1176 s.push_back(combine_ex_with_coeff_to_pair(cit->rest.subs(m, options), cit->coeff));
1186 // Nothing has changed
1191 // static member variables
1194 } // namespace GiNaC