From b136d7031b59c1861bc98da95620bb4695ab2e78 Mon Sep 17 00:00:00 2001 From: Travis Scrimshaw Date: Mon, 26 Jul 2021 11:49:06 +1000 Subject: [PATCH] Improve speed of inv by using the cache. --- .../data_structures/coefficient_stream.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/sage/data_structures/coefficient_stream.py b/src/sage/data_structures/coefficient_stream.py index 7f72354ba02..bdb72be2d4f 100644 --- a/src/sage/data_structures/coefficient_stream.py +++ b/src/sage/data_structures/coefficient_stream.py @@ -587,18 +587,19 @@ def iterate_coefficients(self): """ Return the generator for the coefficients of the multiplicative inverse of the ``series``. """ - n = self._offset + v = self._approximate_valuation # shorthand name + n = 0 # Counts the number of places from the valuation + yield self._ainv + # Note that first entry of the cache will correspond to z^v while True: - v = self._approximate_valuation - if n == v: - yield self._ainv - n += 1 - continue + n += 1 c = self._zero - for k in range(v, n): - c += self[k] * self._series[n - v - k] + m = min(len(self._cache), n) + for k in range(m): + c += self._cache[k] * self._series[n - k] + for k in range(m, n): + c += self[v+k] * self._series[n - k] yield -c * self._ainv - n += 1 class CoefficientStream_apply_coeff(LazyLaurentSeries_unary):