- o.push_back(overall_coeff.bp->normal(sym_lst, repl_lst, level-1));
-
- // o is now a vector of {numerator, denominator} lists
+ ex n = overall_coeff.bp->normal(sym_lst, repl_lst, level-1);
+ nums.push_back(n.op(0));
+ dens.push_back(n.op(1));
+ GINAC_ASSERT(nums.size() == dens.size());
+
+ // Now, nums is a vector of all numerators and dens is a vector of
+ // all denominators
+//std::clog << "add::normal uses " << nums.size() << " summands:\n";
+
+ // Add fractions sequentially
+ exvector::const_iterator num_it = nums.begin(), num_itend = nums.end();
+ exvector::const_iterator den_it = dens.begin(), den_itend = dens.end();
+//std::clog << " num = " << *num_it << ", den = " << *den_it << endl;
+ ex num = *num_it++, den = *den_it++;
+ while (num_it != num_itend) {
+//std::clog << " num = " << *num_it << ", den = " << *den_it << endl;
+ ex next_num = *num_it++, next_den = *den_it++;
+
+ // Trivially add sequences of fractions with identical denominators
+ while ((den_it != den_itend) && next_den.is_equal(*den_it)) {
+ next_num += *num_it;
+ num_it++; den_it++;
+ }