+ if ( factor < 0 ) {
+ // This case must terminate, otherwise we would have division by
+ // zero.
+ int orderloop = 0;
+ do {
+ orderloop++;
+ real_ldegree = buf.series(r, orderloop, options).ldegree(sym);
+ } while (real_ldegree == orderloop);
+ } else {
+ // Here it is possible that buf does not have a ldegree, therefore
+ // check only if ldegree is negative, otherwise reconsider the case
+ // in the second round.
+ real_ldegree = buf.series(r, 0, options).ldegree(sym);
+ if (real_ldegree == 0)
+ flag_redo = true;
+ }
+ }
+
+ ldegrees.push_back(factor * real_ldegree);
+ ldegree_redo.push_back(flag_redo);
+ }
+
+ int degbound = order-std::accumulate(ldegrees.begin(), ldegrees.end(), 0);
+ // Second round: determine the remaining positive ldegrees by the series
+ // method.
+ // here we can ignore ldegrees larger than degbound
+ size_t j = 0;
+ for (auto & it : seq) {
+ if ( ldegree_redo[j] ) {
+ ex expon = it.coeff;
+ int factor = 1;
+ ex buf;
+ if (expon.info(info_flags::integer)) {
+ buf = it.rest;
+ factor = ex_to<numeric>(expon).to_int();
+ } else {
+ buf = recombine_pair_to_ex(it);
+ }
+ int real_ldegree = 0;