Skip to content

Commit b43e994

Browse files
committed
Fix conversion from SymbolicSeries to LaurentSeries
1 parent 8a8453f commit b43e994

File tree

2 files changed

+26
-51
lines changed

2 files changed

+26
-51
lines changed

src/sage/rings/laurent_series_ring.py

+26
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
from sage.rings.infinity import infinity
4343
from sage.rings.integer_ring import ZZ
4444
from sage.rings.laurent_series_ring_element import LaurentSeries
45+
from sage.structure.element import Expression
4546
from sage.structure.parent import Parent
4647
from sage.structure.unique_representation import UniqueRepresentation
4748

@@ -542,6 +543,19 @@ def _element_constructor_(self, x, n=0, prec=infinity):
542543
sage: P.<x> = LaurentSeriesRing(QQ)
543544
sage: P({-3: 1})
544545
x^-3
546+
547+
Check that :issue:`39839` is fixed::
548+
549+
sage: var("x")
550+
x
551+
sage: f = (1/x+sqrt(x+1)).series(x, 5); f
552+
1*x^(-1) + 1 + 1/2*x + (-1/8)*x^2 + 1/16*x^3 + (-5/128)*x^4 + Order(x^5)
553+
sage: LaurentSeriesRing(QQ, "x")(f)
554+
x^-1 + 1 + 1/2*x - 1/8*x^2 + 1/16*x^3 - 5/128*x^4 + O(x^5)
555+
sage: PowerSeriesRing(QQ, "x")(f)
556+
Traceback (most recent call last):
557+
...
558+
ValueError: cannot return dense coefficient list with negative valuation
545559
"""
546560
from sage.rings.fraction_field_element import FractionFieldElement
547561
from sage.rings.lazy_series import LazyPowerSeries, LazyLaurentSeries
@@ -589,6 +603,18 @@ def _element_constructor_(self, x, n=0, prec=infinity):
589603
x = x.add_bigoh(self.default_prec())
590604
else:
591605
x = x.add_bigoh(prec)
606+
elif isinstance(x, Expression):
607+
from sage.symbolic.expression import SymbolicSeries
608+
if isinstance(x, SymbolicSeries):
609+
v = x.default_variable()
610+
if str(v) == self.variable_name():
611+
R = self.base_ring()
612+
g = self.gen()
613+
return sum(
614+
(R(a)*g**ZZ(e) for a, e in x.coefficients(v, sparse=True)), self.zero()
615+
).add_bigoh(x.degree(x.default_variable()))
616+
else:
617+
raise TypeError("can only convert series into ring with same variable name")
592618
return self.element_class(self, x, n).add_bigoh(prec)
593619

594620
def random_element(self, algorithm='default'):

src/sage/symbolic/series_impl.pxi

-51
Original file line numberDiff line numberDiff line change
@@ -199,57 +199,6 @@ cdef class SymbolicSeries(Expression):
199199
cdef Expression ex = new_Expression_from_GEx(self._parent, x)
200200
return ex
201201

202-
def coefficients(self, x=None, sparse=True):
203-
r"""
204-
Return the coefficients of this symbolic series as a list of pairs.
205-
206-
INPUT:
207-
208-
- ``x`` -- (optional) variable
209-
210-
- ``sparse`` -- boolean (default: ``True``); if ``False`` return a list
211-
with as much entries as the order of the series
212-
213-
OUTPUT: depending on the value of ``sparse``,
214-
215-
- A list of pairs ``(expr, n)``, where ``expr`` is a symbolic
216-
expression and ``n`` is a power (``sparse=True``, default)
217-
218-
- A list of expressions where the ``n``-th element is the coefficient of
219-
``x^n`` when ``self`` is seen as polynomial in ``x`` (``sparse=False``).
220-
221-
EXAMPLES::
222-
223-
sage: s = (1/(1-x)).series(x,6); s
224-
1 + 1*x + 1*x^2 + 1*x^3 + 1*x^4 + 1*x^5 + Order(x^6)
225-
sage: s.coefficients()
226-
[[1, 0], [1, 1], [1, 2], [1, 3], [1, 4], [1, 5]]
227-
sage: s.coefficients(x, sparse=False)
228-
[1, 1, 1, 1, 1, 1]
229-
sage: x,y = var("x,y")
230-
sage: s = (1/(1-y*x-x)).series(x,3); s
231-
1 + (y + 1)*x + ((y + 1)^2)*x^2 + Order(x^3)
232-
sage: s.coefficients(x, sparse=False)
233-
[1, y + 1, (y + 1)^2]
234-
"""
235-
if x is None:
236-
x = self.default_variable()
237-
l = [[self.coefficient(x, d), d] for d in range(self.degree(x))]
238-
if sparse:
239-
return l
240-
241-
from sage.rings.integer_ring import ZZ
242-
if any(not c[1] in ZZ for c in l):
243-
raise ValueError("cannot return dense coefficient list with noninteger exponents")
244-
val = l[0][1]
245-
if val < 0:
246-
raise ValueError("cannot return dense coefficient list with negative valuation")
247-
deg = l[-1][1]
248-
ret = [ZZ(0)] * int(deg+1)
249-
for c in l:
250-
ret[c[1]] = c[0]
251-
return ret
252-
253202
def power_series(self, base_ring):
254203
"""
255204
Return the algebraic power series associated to this symbolic series.

0 commit comments

Comments
 (0)