Repository: sympy/sympy
File: sympy/polys/series/ring.py (Python)

Code before:
            return self.from_element(s)

    def from_list(
        self, lst: list[Er], prec: int | None = None
    ) -> PowerSeriesElement[Er]:
        """Create a power series element from a list of coefficients."""
        R = self.ring
        s = R.from_list(lst, prec)
        return self.from_element(s)

    def from_element(self, element: TSeriesElement[Er]) -> PowerSeriesElement[Er]:
        """Convert a lower power series element to a PowerSeriesElement."""
        return PowerSeriesElement(self, element)

    def from_powerserieselement(
        self, element: PowerSeriesElement[Er]
    ) -> PowerSeriesElement[Er]:
        """Convert a power series element to a power series element."""
        R = self.ring
        sprec = element.prec

        if sprec is None:
            s = R(R.to_list(element.series))
        else:
            prec = min(self.prec, sprec)
            s = R(R.to_list(element.series), prec)

        return self.from_element(s)

    def from_int(self, arg: int) -> PowerSeriesElement[Er]:
        """Convert an integer to a power series element."""
        g = self.domain_new(arg)
        return self.from_ground(g)

    def from_ground(self, arg: Er) -> PowerSeriesElement[Er]:
        """Convert a ground element to a power series element."""
        R = self.ring
        s = R.from_list([arg])
        return self.from_element(s)

    def to_expr(self, element: PowerSeriesElement[Er]) -> Expr:
        """Convert a power series element to an expression."""
        dom: Domain[Er] = self.domain
        coeffs: list[Er] = self.ring.to_list(element.series)
        prec: int | None = self.ring.series_prec(element.series)
        result: list[Expr] = []
        x: Expr = self.symbol

        if coeffs and coeffs[0] != 0:
            result.append(dom.to_sympy(coeffs[0]))


Review comment (performance):
The problem with adding a method like `__call__` that allows all types is that then the temptation is to use it everywhere. If we know that we have a list then we should use `from_list`.

Why is this converting to a list at all though? That would be very inefficient if the underlying series type is coming from python-flint.

Code after:
            return self.from_element(s)

    def from_list(
        self, lst: list[Er], prec: int | None = None
    ) -> PowerSeriesElement[Er]:
        """Create a power series element from a list of coefficients."""
        R = self.ring
        s = R.from_list(lst, prec)
        return self.from_element(s)

    def from_element(self, element: TSeriesElement[Er]) -> PowerSeriesElement[Er]:
        """Convert a lower power series element to a PowerSeriesElement."""
        return PowerSeriesElement(self, element)

    def from_powerserieselement(
        self, element: PowerSeriesElement[Er]
    ) -> PowerSeriesElement[Er]:
        """Convert a power series element into the corresponding element of this ring."""
        R = self.ring
        s = R.from_element(element.series)
        return self.from_element(s)

    def from_int(self, arg: int) -> PowerSeriesElement[Er]:
        """Convert an integer to a power series element."""
        g = self.domain_new(arg)
        return self.from_ground(g)

    def from_ground(self, arg: Er) -> PowerSeriesElement[Er]:
        """Convert a ground element to a power series element."""
        R = self.ring
        s = R.from_list([arg])
        return self.from_element(s)

    def to_expr(self, element: PowerSeriesElement[Er]) -> Expr:
        """Convert a power series element to an expression."""
        dom: Domain[Er] = self.domain
        coeffs: list[Er] = self.ring.to_list(element.series)
        prec: int | None = self.ring.series_prec(element.series)
        result: list[Expr] = []
        x: Expr = self.symbol

        if coeffs and coeffs[0] != 0:
            result.append(dom.to_sympy(coeffs[0]))

        for i, coeff in enumerate(coeffs[1:], start=1):
            if coeff == 0:
                continue
            result.append(Mul(dom.to_sympy(coeff), Pow(x, i)))

        if prec is not None:
            result.append(Order(x**prec))