Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix conversion from SymbolicSeries to LaurentSeries #39841

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/sage/rings/laurent_series_ring.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
from sage.rings.infinity import infinity
from sage.rings.integer_ring import ZZ
from sage.rings.laurent_series_ring_element import LaurentSeries
from sage.structure.element import Expression
from sage.structure.parent import Parent
from sage.structure.unique_representation import UniqueRepresentation

Expand Down Expand Up @@ -542,6 +543,19 @@
sage: P.<x> = LaurentSeriesRing(QQ)
sage: P({-3: 1})
x^-3

Check that :issue:`39839` is fixed::

sage: var("x")
x
sage: f = (1/x+sqrt(x+1)).series(x, 5); f
1*x^(-1) + 1 + 1/2*x + (-1/8)*x^2 + 1/16*x^3 + (-5/128)*x^4 + Order(x^5)
sage: LaurentSeriesRing(QQ, "x")(f)
x^-1 + 1 + 1/2*x - 1/8*x^2 + 1/16*x^3 - 5/128*x^4 + O(x^5)
sage: PowerSeriesRing(QQ, "x")(f)
Traceback (most recent call last):
...
ValueError: cannot return dense coefficient list with negative valuation
"""
from sage.rings.fraction_field_element import FractionFieldElement
from sage.rings.lazy_series import LazyPowerSeries, LazyLaurentSeries
Expand Down Expand Up @@ -589,6 +603,18 @@
x = x.add_bigoh(self.default_prec())
else:
x = x.add_bigoh(prec)
elif isinstance(x, Expression):
from sage.symbolic.expression import SymbolicSeries
if isinstance(x, SymbolicSeries):
v = x.default_variable()
if str(v) == self.variable_name():
R = self.base_ring()
g = self.gen()
return sum(
(R(a)*g**ZZ(e) for a, e in x.coefficients(v, sparse=True)), self.zero()
).add_bigoh(x.degree(x.default_variable()))
else:
raise TypeError("can only convert series into ring with same variable name")

Check warning on line 617 in src/sage/rings/laurent_series_ring.py

View check run for this annotation

Codecov / codecov/patch

src/sage/rings/laurent_series_ring.py#L617

Added line #L617 was not covered by tests
return self.element_class(self, x, n).add_bigoh(prec)

def random_element(self, algorithm='default'):
Expand Down
51 changes: 0 additions & 51 deletions src/sage/symbolic/series_impl.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -199,57 +199,6 @@ cdef class SymbolicSeries(Expression):
cdef Expression ex = new_Expression_from_GEx(self._parent, x)
return ex

def coefficients(self, x=None, sparse=True):
r"""
Return the coefficients of this symbolic series as a list of pairs.

INPUT:

- ``x`` -- (optional) variable

- ``sparse`` -- boolean (default: ``True``); if ``False`` return a list
with as much entries as the order of the series

OUTPUT: depending on the value of ``sparse``,

- A list of pairs ``(expr, n)``, where ``expr`` is a symbolic
expression and ``n`` is a power (``sparse=True``, default)

- A list of expressions where the ``n``-th element is the coefficient of
``x^n`` when ``self`` is seen as polynomial in ``x`` (``sparse=False``).

EXAMPLES::

sage: s = (1/(1-x)).series(x,6); s
1 + 1*x + 1*x^2 + 1*x^3 + 1*x^4 + 1*x^5 + Order(x^6)
sage: s.coefficients()
[[1, 0], [1, 1], [1, 2], [1, 3], [1, 4], [1, 5]]
sage: s.coefficients(x, sparse=False)
[1, 1, 1, 1, 1, 1]
sage: x,y = var("x,y")
sage: s = (1/(1-y*x-x)).series(x,3); s
1 + (y + 1)*x + ((y + 1)^2)*x^2 + Order(x^3)
sage: s.coefficients(x, sparse=False)
[1, y + 1, (y + 1)^2]
"""
if x is None:
x = self.default_variable()
l = [[self.coefficient(x, d), d] for d in range(self.degree(x))]
if sparse:
return l

from sage.rings.integer_ring import ZZ
if any(not c[1] in ZZ for c in l):
raise ValueError("cannot return dense coefficient list with noninteger exponents")
val = l[0][1]
if val < 0:
raise ValueError("cannot return dense coefficient list with negative valuation")
deg = l[-1][1]
ret = [ZZ(0)] * int(deg+1)
for c in l:
ret[c[1]] = c[0]
return ret

def power_series(self, base_ring):
"""
Return the algebraic power series associated to this symbolic series.
Expand Down
Loading