Skip to content

Commit

Permalink
typing(polys): Add a few type hints in polys
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarbenjamin committed Nov 3, 2024
1 parent d293133 commit ec9f663
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 42 deletions.
20 changes: 15 additions & 5 deletions sympy/polys/compatibility.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
"""Compatibility interface between dense and sparse polys. """

from __future__ import annotations

from typing import TYPE_CHECKING

if TYPE_CHECKING:
from sympy.core.expr import Expr
from sympy.polys.domains.domain import Domain
from sympy.polys.orderings import MonomialOrder
from sympy.polys.rings import PolyElement

from sympy.polys.densearith import dup_add_term
from sympy.polys.densearith import dmp_add_term
Expand Down Expand Up @@ -230,11 +239,12 @@

@public
class IPolys:
symbols = None
ngens = None
domain = None
order = None
gens = None

gens: tuple[PolyElement, ...]
symbols: tuple[Expr, ...]
ngens: int
domain: Domain
order: MonomialOrder

def drop(self, gen):
pass
Expand Down
14 changes: 10 additions & 4 deletions sympy/polys/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@
from sympy.core.symbol import Symbol
from sympy.core.sympify import CantSympify, sympify
from sympy.functions.elementary.exponential import ExpBase
from sympy.polys.domains.domain import Domain
from sympy.polys.domains.domainelement import DomainElement
from sympy.polys.domains.fractionfield import FractionField
from sympy.polys.domains.polynomialring import PolynomialRing
from sympy.polys.constructor import construct_domain
from sympy.polys.orderings import lex
from sympy.polys.orderings import lex, MonomialOrder
from sympy.polys.polyerrors import CoercionFailed
from sympy.polys.polyoptions import build_options
from sympy.polys.polyutils import _parallel_dict_from_expr
from sympy.polys.rings import PolyElement
from sympy.polys.rings import PolyRing, PolyElement
from sympy.printing.defaults import DefaultPrinting
from sympy.utilities import public
from sympy.utilities.iterables import is_sequence
Expand Down Expand Up @@ -104,8 +105,14 @@ def sfield(exprs, *symbols, **options):
class FracField(DefaultPrinting):
"""Multivariate distributed rational function field. """

ring: PolyRing
gens: tuple[FracElement, ...]
symbols: tuple[Expr, ...]
ngens: int
domain: Domain
order: MonomialOrder

def __new__(cls, symbols, domain, order=lex):
from sympy.polys.rings import PolyRing
ring = PolyRing(symbols, domain, order)
symbols = ring.symbols
ngens = ring.ngens
Expand Down Expand Up @@ -280,7 +287,6 @@ def to_domain(self):
return FractionField(self)

def to_ring(self):
from sympy.polys.rings import PolyRing
return PolyRing(self.symbols, self.domain, self.order)

class FracElement(DomainElement, DefaultPrinting, CantSympify):
Expand Down
3 changes: 3 additions & 0 deletions sympy/polys/polyclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ class DMP(CantSympify):

__slots__ = ()

lev: int
dom: Domain

def __new__(cls, rep, dom, lev=None):

if lev is None:
Expand Down
7 changes: 6 additions & 1 deletion sympy/polys/polyoptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

__all__ = ["Options"]

from sympy.core import Basic, sympify
from sympy.core.basic import Basic
from sympy.core.expr import Expr
from sympy.core.sympify import sympify
from sympy.polys.polyerrors import GeneratorsError, OptionError, FlagError
from sympy.utilities import numbered_symbols, topological_sort, public
from sympy.utilities.iterables import has_dups, is_sequence
Expand Down Expand Up @@ -123,6 +125,9 @@ class Options(dict):
__order__ = None
__options__: dict[str, type[Option]] = {}

gens: tuple[Expr, ...]
domain: sympy.polys.domains.Domain

def __init__(self, gens, args, flags=None, strict=False):
dict.__init__(self)

Expand Down
3 changes: 2 additions & 1 deletion sympy/polys/polytools.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""User-friendly public interface to polynomial functions. """

from __future__ import annotations

from functools import wraps, reduce
from operator import mul
Expand Down Expand Up @@ -164,7 +165,7 @@ class Poly(Basic):
is_Poly = True
_op_priority = 10.001

def __new__(cls, rep, *gens, **args):
def __new__(cls, rep, *gens, **args) -> Poly:
"""Create a new polynomial instance out of something useful. """
opt = options.build_options(gens, args)

Expand Down
11 changes: 9 additions & 2 deletions sympy/polys/rings.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
from sympy.polys.compatibility import IPolys
from sympy.polys.constructor import construct_domain
from sympy.polys.densebasic import ninf, dmp_to_dict, dmp_from_dict
from sympy.polys.domains.domain import Domain
from sympy.polys.domains.domainelement import DomainElement
from sympy.polys.domains.polynomialring import PolynomialRing
from sympy.polys.heuristicgcd import heugcd
from sympy.polys.monomials import MonomialOps
from sympy.polys.orderings import lex
from sympy.polys.orderings import lex, MonomialOrder
from sympy.polys.polyerrors import (
CoercionFailed, GeneratorsError,
ExactQuotientFailed, MultivariatePolynomialError)
Expand All @@ -33,7 +34,7 @@
from sympy.utilities.magic import pollute

@public
def ring(symbols, domain, order=lex):
def ring(symbols, domain, order: MonomialOrder|str = lex):
"""Construct a polynomial ring returning ``(ring, x_1, ..., x_n)``.
Parameters
Expand Down Expand Up @@ -196,6 +197,12 @@ def _parse_symbols(symbols):
class PolyRing(DefaultPrinting, IPolys):
"""Multivariate distributed polynomial ring. """

gens: tuple[PolyElement, ...]
symbols: tuple[Expr, ...]
ngens: int
domain: Domain
order: MonomialOrder

def __new__(cls, symbols, domain, order=lex):
symbols = tuple(_parse_symbols(symbols))
ngens = len(symbols)
Expand Down
1 change: 1 addition & 0 deletions sympy/polys/rootoftools.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Implementation of RootOf class and related tools. """



from sympy.core.basic import Basic
from sympy.core import (S, Expr, Integer, Float, I, oo, Add, Lambda,
symbols, sympify, Rational, Dummy)
Expand Down
2 changes: 1 addition & 1 deletion sympy/polys/subresultants_qq_zz.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ def sylvester(f, g, x, method = 1):
return M

# Sylvester's matrix of 1853 (a.k.a sylvester2)
if method >= 2:
else:
if len(fp) < len(gp):
h = []
for i in range(len(gp) - len(fp)):
Expand Down
Loading

0 comments on commit ec9f663

Please sign in to comment.