K im,dZddlmZddlmZddlmZddlmZddl m Z m Z m Z m Z mZddlmZddlmZdd lmZdd lmZdd lmZd Zd ZdZdZedZy)z0Tools for constructing domains for expressions. )prod)sympify) pure_complex)ordered)ZZQQZZ_IQQ_IEX) ComplexField) RealField) build_options)parallel_dict_from_basic)publicc2dx}x}x}}g}|jdurd}nd}|D]}|jr|jrd}|jr|ryd}|j |Ct |} | rxd}| \} } | jr'| jr| jr | jsd}d}| jr|j | | js|j | ||r|ryd}y|rt d|Dnd} |rt||\} }| |fS|r|r t| } n:|r t| } n+|s |jr|rtnt} n|rtnt} |Dcgc]}| j|}}| |fScc}w) z?Handle simple domains, e.g.: ZZ, QQ, RR and algebraic domains. FTc6|jxr |jSN is_number is_algebraiccoeffs ]/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/polys/constructor.pyz#_construct_simple..sU__%K9K9Kcy)NFrs rrz#_construct_simple..srNc34K|]}|jywr_prec.0cs r z$_construct_simple..>2q17725prec) extension is_Rational is_Integeris_Floatappendrmax_construct_algebraicr r fieldr rr r from_sympy)coeffsopt rationalsfloats complexes algebraics float_numbersrr is_complexxymax_precdomainresults r_construct_simpler@s277I77ZM }}K * !   ## ^^$$U+%e,J !1==Q]]LLQ\\$( !Fzz%,,Q/zz%,,Q/e$ ! C!J7Ds2M22H-fc: 6> i!x0F H-F #))&TBF&TBF8>?u&##E*?? 6>@s6Fc ddlm}t fd  |}tt |dd\}}t dt |D}tj|fjjc |Dcgc]#} jj|t%}}tt | fd |D cgc] }  |  } } | fScc}wcc} w)zDWe know that coefficients are algebraic so construct the extension. r)primitive_elementc2g}|D]}|jrdtj|f}nW|jrd|jf}n6|j rd|jf}nd|f}j ||j||S)NQ+*e)r+rr2is_Addargsis_Muladdr.)rItreesatree build_treesextss rrOz)_construct_algebraic..build_treesWs A}}R]]1-.[01[01Qx LL   rT)expolysc3,K|] \}}||zywrr)r"sexts rr$z'_construct_algebraic..js3Cqu3sc|\}}|dk(r"jj|gtS|dk(rtfd|DjS|dk(rt fd|DS|dk(r|St )NrDrEc3.K|] }|ywrrr"rM convert_trees rr$z=_construct_algebraic..convert_tree..v6A Q6rFc3.K|] }|ywrrrXs rr$z=_construct_algebraic..convert_tree..xrZr[rG)dtype from_listrsumzeror RuntimeError)rNoprIrYr>exts_mapgs rrYz*_construct_algebraic..convert_treeqs{D 9<<))4&!R8 8 3Y66 D D 3Y666 6 3YD> ! r)sympy.polys.numberfieldsrBsetlistrr_zipralgebraic_fieldrepto_listr]r^dict)r3r4rBrLspanHroothexts_domrNr?rOrYr>rPrcrds @@@@@@rr0r0Qs: 5D   E  D"4D=JAtQ 33tT?3 3D""At9-quu}}IFA:;%= 4s (C:%C?cgg}}|D]7}|j\}}|j||j|9t||z\}}|sy|j.s"Bcs}}1!1!11Bs$&TF)rc34K|]}|jywrrr!s rr$z'_construct_composite..r%r&r'r()as_numer_denomr.r compositeanyrf free_symbolslenr1rhitemsrKupdatergvaluesr+r,r-rr/r r r r rr poly_ringr2 frac_field)r3r4numersdenomsrnumerdenomrRgens all_symbolsrtsymbolsnk fractionszerosmonomr5r6r7r9r:r;r<r=groundr?r>s r_construct_compositersFF++- u e e  +6F?;KE4  }} BTB Be  'C&&GW$w&  ' D A E A A 2AYF 12YF yy  $q&5  E5zA~e!3   UF / %LE5%LE %  % u 5!$e  % % / 0LE5 MM$u||~. / MM$u||~. / 0&+*I*M0   ## ^^F   '%e,J% !1==Q]]LLQ\\$( !Fzz%,,Q/zz%,,Q/)0,7Ds2M22H )8, )  FF  F !!!4( )E %  8 u%007e  8 MM&- (  )" 6>#""D)/ 2LE5 %  8 u%007e  8!&  8 u%007e  8 MM&%0 1 2 6>rchtg}}|D]"}|j|j|$||fS)z6The last resort case, i.e. use the expression domain. )r r.r2)r3r4r>r?rs r_construct_expressionrs=FF0 f''./0 6>rc :t|}t|drFt|tr3|sgg}}n2t t t |j \}}n|}n|g}t tt|}t||}||dur|\}}nDt||\}}n4|jdurd}n t||}||\}}nt||\}}t|dr4t|tr |tt t |fS||fS||dfS)aT Construct a minimal domain for a list of expressions. Explanation =========== Given a list of normal SymPy expressions (of type :py:class:`~.Expr`) ``construct_domain`` will find a minimal :py:class:`~.Domain` that can represent those expressions. The expressions will be converted to elements of the domain and both the domain and the domain elements are returned. Parameters ========== obj: list or dict The expressions to build a domain for. **args: keyword arguments Options that affect the choice of domain. Returns ======= (K, elements): Domain and list of domain elements The domain K that can represent the expressions and the list or dict of domain elements representing the same expressions as elements of K. Examples ======== Given a list of :py:class:`~.Integer` ``construct_domain`` will return the domain :ref:`ZZ` and a list of integers as elements of :ref:`ZZ`. >>> from sympy import construct_domain, S >>> expressions = [S(2), S(3), S(4)] >>> K, elements = construct_domain(expressions) >>> K ZZ >>> elements [2, 3, 4] >>> type(elements[0]) # doctest: +SKIP >>> type(expressions[0]) If there are any :py:class:`~.Rational` then :ref:`QQ` is returned instead. >>> construct_domain([S(1)/2, S(3)/4]) (QQ, [1/2, 3/4]) If there are symbols then a polynomial ring :ref:`K[x]` is returned. >>> from sympy import symbols >>> x, y = symbols('x, y') >>> construct_domain([2*x + 1, S(3)/4]) (QQ[x], [2*x + 1, 3/4]) >>> construct_domain([2*x + 1, y]) (ZZ[x,y], [2*x + 1, y]) If any symbols appear with negative powers then a rational function field :ref:`K(x)` will be returned. >>> construct_domain([y/x, x/(1 - y)]) (ZZ(x,y), [y/x, -x/(y - 1)]) Irrational algebraic numbers will result in the :ref:`EX` domain by default. The keyword argument ``extension=True`` leads to the construction of an algebraic number field :ref:`QQ(a)`. >>> from sympy import sqrt >>> construct_domain([sqrt(2)]) (EX, [EX(sqrt(2))]) >>> construct_domain([sqrt(2)], extension=True) # doctest: +SKIP (QQ, [ANP([1, 0], [1, 0, -2], QQ)]) See also ======== Domain Expr __iter__NFr) rhasattr isinstancerlrgrhr}maprr@rryr)objrIr4monomsr3r?r>s rconstruct_domainr s"f  CsJ c4 !#R!%c4 +<&=!>F #gv& 'F vs +F   #NFF263?NFF ==E !F)&#6F  #NFF263?NFFsJ c4 4S%8 9:: :6> !vay  rN)__doc__mathr sympy.corersympy.core.evalfrsympy.core.sortingrsympy.polys.domainsrrr r r sympy.polys.domains.complexfieldr sympy.polys.domains.realfieldr sympy.polys.polyoptionsrsympy.polys.polyutilsrsympy.utilitiesrr@r0rrrrrrrsW6)&66931:"?D/dzzx!x!r