]> www.ginac.de Git - ginac.git/blobdiff - ginac/expairseq.cpp
[BUGFIX] Fix crash in parser.
[ginac.git] / ginac / expairseq.cpp
index 7ce15cb2c581dc84c2b6a7b346a3808a3747a0e1..cef087153c5fb61facc66ce1e95df40e7e1187d5 100644 (file)
@@ -3,7 +3,7 @@
  *  Implementation of sequences of expression pairs. */
 
 /*
- *  GiNaC Copyright (C) 1999-2018 Johannes Gutenberg University Mainz, Germany
+ *  GiNaC Copyright (C) 1999-2024 Johannes Gutenberg University Mainz, Germany
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
@@ -112,17 +112,15 @@ expairseq::expairseq(epvector && vp, const ex &oc, bool do_index_renaming)
 void expairseq::read_archive(const archive_node &n, lst &sym_lst) 
 {
        inherited::read_archive(n, sym_lst);
-       auto first = n.find_first("rest");
-       auto last = n.find_last("coeff");
-       ++last;
-       seq.reserve((last-first)/2);
+       auto range = n.find_property_range("rest", "coeff");
+       seq.reserve((range.end-range.begin)/2);
 
-       for (auto loc = first; loc < last;) {
+       for (auto loc = range.begin; loc < range.end;) {
                ex rest;
                ex coeff;
                n.find_ex_by_loc(loc++, rest, sym_lst);
                n.find_ex_by_loc(loc++, coeff, sym_lst);
-               seq.push_back(expair(rest, coeff));
+               seq.emplace_back(expair(rest, coeff));
        }
 
        n.find_ex("overall_coeff", overall_coeff, sym_lst);
@@ -565,6 +563,11 @@ ex expairseq::recombine_pair_to_ex(const expair &p) const
 
 bool expairseq::expair_needs_further_processing(epp it)
 {
+       if (is_exactly_a<numeric>(it->rest) &&
+           it->coeff.is_equal(_ex1)) {
+               // the pair {<n>, 1} has yet to be absorbed into overall_coeff
+               return true;
+       }
        return false;
 }
 
@@ -888,8 +891,7 @@ void expairseq::make_flat(const epvector &v, bool do_index_renaming)
                    this->can_make_flat(cit)) {
                        ex newrest = mf.handle_factor(cit.rest, cit.coeff);
                        const expairseq &subseqref = ex_to<expairseq>(newrest);
-                       combine_overall_coeff(ex_to<numeric>(subseqref.overall_coeff),
-                                             ex_to<numeric>(cit.coeff));
+                       combine_overall_coeff(subseqref.overall_coeff, cit.coeff);
                        for (auto & cit_s : subseqref.seq) {
                                seq.push_back(expair(cit_s.rest,
                                                     ex_to<numeric>(cit_s.coeff).mul_dyn(ex_to<numeric>(cit.coeff))));