|
11 | 11 | from sympy.utilities.decorator import doctest_depends_on |
12 | 12 | from sympy.polys.polyerrors import NotReversible |
13 | 13 |
|
| 14 | +_require_flint_version = False |
14 | 15 |
|
15 | 16 | if TYPE_CHECKING: |
16 | 17 | from flint import fmpq_poly, fmpq_series, fmpz_poly, fmpz_series, ctx # type: ignore |
17 | 18 | from flint.utils.flint_exceptions import DomainError # type: ignore |
18 | 19 | elif GROUND_TYPES == "flint": |
19 | 20 | from flint import fmpq_poly, fmpq_series, fmpz_poly, fmpz_series, ctx |
20 | 21 | from flint.utils.flint_exceptions import DomainError |
| 22 | + |
| 23 | + # Required in specific cases where the 0.8 implementation provides faster |
| 24 | + # performance for working with Flint series methods. |
| 25 | + # This can be removed when python-flint 0.8 becomes the minimum supported version. |
| 26 | + import flint |
| 27 | + |
| 28 | + _major, _minor, *_ = flint.__version__.split(".") |
| 29 | + if (int(_major), int(_minor)) >= (0, 8): |
| 30 | + _require_flint_version = True |
21 | 31 | else: |
22 | 32 | fmpq_poly = fmpq_series = fmpz_poly = fmpz_series = ctx = None |
23 | 33 |
|
@@ -205,11 +215,23 @@ def from_list(self, coeffs: list[MPZ], prec: int | None = None) -> ZZSeries: |
205 | 215 | if len(coeffs) <= self._prec: |
206 | 216 | return fmpz_poly(coeffs) |
207 | 217 | prec = self._prec |
208 | | - else: |
209 | | - prec = min(prec, self._prec) |
210 | 218 |
|
211 | 219 | return fmpz_series(coeffs, prec=prec) |
212 | 220 |
|
| 221 | + def from_element(self, s: ZZSeries) -> ZZSeries: |
| 222 | + """Convert a power series element into the corresponding element of this ring.""" |
| 223 | + ring_prec = self._prec |
| 224 | + if isinstance(s, fmpz_poly): |
| 225 | + if s.degree() >= ring_prec: |
| 226 | + return fmpz_series(s, prec=ring_prec) |
| 227 | + return s |
| 228 | + |
| 229 | + prec = min(_get_series_precision(s), ring_prec) |
| 230 | + if _require_flint_version: |
| 231 | + return fmpz_series(s, prec=prec) |
| 232 | + else: |
| 233 | + return fmpz_series(s.coeffs(), prec=prec) |
| 234 | + |
213 | 235 | def to_list(self, s: ZZSeries) -> list[MPZ]: |
214 | 236 | """Returns the list of series coefficients.""" |
215 | 237 | return s.coeffs() |
@@ -652,11 +674,23 @@ def from_list(self, coeffs: list[MPQ], prec: int | None = None) -> QQSeries: |
652 | 674 | if len(coeffs) <= self._prec: |
653 | 675 | return fmpq_poly(coeffs) |
654 | 676 | prec = self._prec |
655 | | - else: |
656 | | - prec = min(prec, self._prec) |
657 | 677 |
|
658 | 678 | return fmpq_series(coeffs, prec=prec) |
659 | 679 |
|
| 680 | + def from_element(self, s: QQSeries) -> QQSeries: |
| 681 | + """Convert a power series element into the corresponding element of this ring.""" |
| 682 | + ring_prec = self._prec |
| 683 | + if isinstance(s, fmpq_poly): |
| 684 | + if s.degree() >= ring_prec: |
| 685 | + return fmpq_series(s, prec=ring_prec) |
| 686 | + return s |
| 687 | + |
| 688 | + prec = min(_get_series_precision(s), ring_prec) |
| 689 | + if _require_flint_version: |
| 690 | + return fmpq_series(s, prec=prec) |
| 691 | + else: |
| 692 | + return fmpq_series(s.coeffs(), prec=prec) |
| 693 | + |
660 | 694 | def to_list(self, s: QQSeries) -> list[MPQ]: |
661 | 695 | """Returns the list of series coefficients.""" |
662 | 696 | return s.coeffs() |
|
0 commit comments