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

Code before:

    def to_dense(self, element: PowerSeriesElement[Er]) -> dup[Er]:
        """Returns a dense list coefficients of a power series."""
        return self.ring.to_dense(element.series)

    def domain_new(self, arg: Er | int) -> Er:
        """Convert arg to the element of ground domain of ring."""
        return self.domain.convert(arg, self.domain)

    def ring_new(
        self, arg: PowerSeriesElement[Er] | Expr | Er | int
    ) -> PowerSeriesElement[Er]:
        """Create a power series element from various types."""
        if isinstance(arg, PowerSeriesElement):
            R = self.ring
            if R == arg.ring:
                return arg

            sprec = arg.prec
            if sprec is None:
                prec = self.prec
            else:
                prec = min(self.prec, sprec)

            s = R(R.to_list(arg.series), prec)
            return self.from_element(s)

        elif isinstance(arg, Expr):
            return self.from_expr(arg)
        elif isinstance(arg, int):
            return self.from_int(arg)
        elif self.domain.of_type(arg):
            return self.from_ground(arg)
        else:
            raise TypeError(f"{arg.__class__.__name__} type not supported")

    __call__ = ring_new

    def square(self, s: PowerSeriesElement[Er]) -> PowerSeriesElement[Er]:
        """
        Compute the square of a power series.

        Examples
        ========

        >>> from sympy import ZZ
        >>> from sympy.polys.series import PowerSeriesRingRing
        >>> R = PowerSeriesRingRing(ZZ, "x", 5)
        >>> x = R.gen
        >>> R.square(1 + x + x**2)
        1 + 2*x + 3*x**2 + 2*x**3 + x**4

Review comment (suggestion):
It would be better if this calls a method called something like `from_powerserieselement`. There should be some method that can be called when it is known that arg is a PowerSeriesElement.

The handling of prec here seems potentially inconsistent. It is possible that the series precision may exceed the ring precision. If the series is already of the same ring then it is returned with its precision unchanged. If the series is coming from a different ring then its precision is potentially changed.

I'm not sure what is a consistent way of handling this.

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

        return Add(*result)

    def to_list(self, element: PowerSeriesElement[Er]) -> list[Er]:
        """Returns a list of coefficients of a power series."""
        return self.ring.to_list(element.series)

    def to_dense(self, element: PowerSeriesElement[Er]) -> dup[Er]:
        """Returns a dense list coefficients of a power series."""
        return self.ring.to_dense(element.series)

    def domain_new(self, arg: Er | int) -> Er:
        """Convert arg to the element of ground domain of ring."""
        return self.domain.convert(arg, self.domain)

    def ring_new(
        self, arg: PowerSeriesElement[Er] | Expr | Er | int
    ) -> PowerSeriesElement[Er]:
        """Create a power series element from various types."""
        if isinstance(arg, PowerSeriesElement):
            return self.from_powerserieselement(arg)
        elif isinstance(arg, Expr):
            return self.from_expr(arg)
        elif isinstance(arg, int):
            return self.from_int(arg)
        elif self.domain.of_type(arg):
            return self.from_ground(arg)
        else:
            raise TypeError(f"{arg.__class__.__name__} type not supported")

    __call__ = ring_new

    def square(self, s: PowerSeriesElement[Er]) -> PowerSeriesElement[Er]:
        """
        Compute the square of a power series.

        Examples
        ========

        >>> from sympy import ZZ
        >>> from sympy.polys.series import PowerSeriesRingRing
        >>> R = PowerSeriesRingRing(ZZ, "x", 5)
        >>> x = R.gen
        >>> R.square(1 + x + x**2)
        1 + 2*x + 3*x**2 + 2*x**3 + x**4

        """
        R = self.ring
        series = R.square(s.series)