K i3dZddlmZddlmZddlmZddlmZddl m Z ddl m Z ddl mZdd lmZdd lmZdd lmZd Zd ZdZedZeGddZedZdZeddZy)z'Utilities for algebraic number theory. )sympify) factorint)QQ)ZZ) DMRankError)minpoly)IntervalPrinter)public)lambdify)mpc~t|txs,tj|xst j|S)z Test whether an argument is of an acceptable type to be used as a rational number. Explanation =========== Returns ``True`` on any argument of type ``int``, :ref:`ZZ`, or :ref:`QQ`. See Also ======== is_int ) isinstanceintrof_typercs h/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/polys/numberfields/utilities.pyis_ratrs+, a  ?A ?"**Q-?cPt|txstj|S)z Test whether an argument is of an acceptable type to be used as an integer. Explanation =========== Returns ``True`` on any argument of type ``int`` or :ref:`ZZ`. See Also ======== is_rat )rrrrrs ris_intr)s$ a  .A.rcHt|}|j|jfS)z Given any argument on which :py:func:`~.is_rat` is ``True``, return the numerator and denominator of this number. See Also ======== is_rat )r numerator denominator)rrs r get_num_denomr>s  1A ;; %%rcx|dzdvr td|dk(riddifS|dk(riifSt|}i}i}d}|jD]9\}}|dzdk(r$d||<|dzdk(r|dz }|dk\s&|dz dz||<2|dz||<;d|v}|s|dzdk(r&|d}|dkDsJ|dk(r|d=n|dz |d<|rdnd|d<||fS)a Extract a fundamental discriminant from an integer *a*. Explanation =========== Given any rational integer *a* that is 0 or 1 mod 4, write $a = d f^2$, where $d$ is either 1 or a fundamental discriminant, and return a pair of dictionaries ``(D, F)`` giving the prime factorizations of $d$ and $f$ respectively, in the same format returned by :py:func:`~.factorint`. A fundamental discriminant $d$ is different from unity, and is either 1 mod 4 and squarefree, or is 0 mod 4 and such that $d/4$ is squarefree and 2 or 3 mod 4. This is the same as being the discriminant of some quadratic field. Examples ======== >>> from sympy.polys.numberfields.utilities import extract_fundamental_discriminant >>> print(extract_fundamental_discriminant(-432)) ({3: 1, -1: 1}, {2: 2, 3: 1}) For comparison: >>> from sympy import factorint >>> print(factorint(-432)) {2: 4, 3: 3, -1: 1} Parameters ========== a: int, must be 0 or 1 mod 4 Returns ======= Pair ``(D, F)`` of dictionaries. Raises ====== ValueError If *a* is not 0 or 1 mod 4. References ========== .. [1] Cohen, H. *A Course in Computational Algebraic Number Theory.* (See Prop. 5.1.3) )rzATo extract fundamental discriminant, number must be 0 or 1 mod 4.rr) ValueErrorritems) a a_factorsDF num_3_mod_4peevene2s r extract_fundamental_discriminantr-Ms"l 1uF\]]AvAq6zAv2v ! I A AK!1 q5A:AaD1uzq AvA!|!6AaD 6D {Q!# qTAv v 7!6AaDqa! a4Krc6eZdZdZd dZdZdZdZdZdZ y) AlgIntPowersa Compute the powers of an algebraic integer. Explanation =========== Given an algebraic integer $\theta$ by its monic irreducible polynomial ``T`` over :ref:`ZZ`, this class computes representations of arbitrarily high powers of $\theta$, as :ref:`ZZ`-linear combinations over $\{1, \theta, \ldots, \theta^{n-1}\}$, where $n = \deg(T)$. The representations are computed using the linear recurrence relations for powers of $\theta$, derived from the polynomial ``T``. See [1], Sec. 4.2.2. Optionally, the representations may be reduced with respect to a modulus. Examples ======== >>> from sympy import Poly, cyclotomic_poly >>> from sympy.polys.numberfields.utilities import AlgIntPowers >>> T = Poly(cyclotomic_poly(5)) >>> zeta_pow = AlgIntPowers(T) >>> print(zeta_pow[0]) [1, 0, 0, 0] >>> print(zeta_pow[1]) [0, 1, 0, 0] >>> print(zeta_pow[4]) # doctest: +SKIP [-1, -1, -1, -1] >>> print(zeta_pow[24]) # doctest: +SKIP [-1, -1, -1, -1] References ========== .. [1] Cohen, H. *A Course in Computational Algebraic Number Theory.* Nc||_||_|j|_t |j j Dcgc]}| |z c}ddg|_|j|_ycc}w)a- Parameters ========== T : :py:class:`~.Poly` The monic irreducible polynomial over :ref:`ZZ` defining the algebraic integer. modulus : int, None, optional If not ``None``, all representations will be reduced w.r.t. this. N) Tmodulusdegreenreversedrepto_listpowers_n_and_up max_so_far)selfr2r3rs r__init__zAlgIntPowers.__init__sc 4)r@)r;others r__rmod__zAlgIntPowers.__rmod__sxxrc l|j}||kry|j}|j}|d}t|dz|dzD]d}||dz |z |dz }|j |d|z|zgtd|Dcgc]}||dz |z |dz |||zz|z!c}zf||_ycc}w)Nrr)r:r5r9rangeappend) r;r*mr5rrkbis rcompute_up_throughzAlgIntPowers.compute_up_throughs OO 66 FF   aDqsAaC A!A#a%1 A HH1a$=B1a[#89Qqs1uXac]QqT!V+t3#   #s:$B1 c|j}|dkr td||kr t|Dcgc] }||k(rdnd c}S|j||j||z Scc}w)NrzExponent must be non-negative.r)r5r"rErKr9)r;r*r5rJs rgetzAlgIntPowers.getsm FF q5=> > U05a91aAQ&9 9  # #A &''A. .:sA%c$|j|Sr>)rM)r;items r __getitem__zAlgIntPowers.__getitem__sxx~rr>) __name__ __module__ __qualname____doc__r<r@rCrKrMrPrrr/r/s'%N!&C /rr/c#K|}|g|z} ||k(s ||vs| |vr|dd|dz }||| k(r|dz}||| k(r||xxdzcc<t|dz|D]}|||< t|D] }||dk7s n |dz }|g|z}~w)a[ Generate coefficients for searching through polynomials. Explanation =========== Lead coeff is always non-negative. Explore all combinations with coeffs bounded in absolute value before increasing the bound. Skip the all-zero list, and skip any repeats. See examples. Examples ======== >>> from sympy.polys.numberfields.utilities import coeff_search >>> cs = coeff_search(2, 1) >>> C = [next(cs) for i in range(13)] >>> print(C) [[1, 1], [1, 0], [1, -1], [0, 1], [2, 2], [2, 1], [2, 0], [2, -1], [2, -2], [1, 2], [1, -2], [0, 2], [3, 3]] Parameters ========== m : int Length of coeff list. R : int Initial max abs val for coeffs (will increase as search proceeds). Returns ======= generator Infinite generator of lists of coefficients. Nrr)rE)rGRR0rjrJs r coeff_searchrZsJ B aA  7a1faA$J Edqbj FAdqbj ! q1ua AAaD q Atqy  FAaA s;B >> from sympy.polys.matrices import DM >>> from sympy import QQ, FF >>> from sympy.polys.numberfields.utilities import supplement_a_subspace >>> M = DM([[1, 7, 0], [2, 3, 4]], QQ).transpose() >>> print(supplement_a_subspace(M).to_Matrix().transpose()) Matrix([[1, 7, 0], [2, 3, 4], [1, 0, 0]]) >>> M2 = M.convert_to(FF(7)) >>> print(M2.to_Matrix().transpose()) Matrix([[1, 0, 0], [2, 3, -3]]) >>> print(supplement_a_subspace(M2).to_Matrix().transpose()) Matrix([[1, 0, 0], [2, 3, -3], [0, 1, 0]]) Parameters ========== M : :py:class:`~.DomainMatrix` The columns give the basis for the subspace. Returns ======= :py:class:`~.DomainMatrix` This matrix is invertible and its first $r$ columns equal *M*. Raises ====== DMRankError If *M* was not of maximal rank. References ========== .. [1] Cohen, H. *A Course in Computational Algebraic Number Theory* (See Sec. 2.3.2.) NzM was not of maximal rank) shapehstackeyedomainrreftuplerErinv)Mr5rMaugrWpivotsABs rsupplement_a_subspacerh=sF 77DAq 88AEE!QXX& 'D IAv bqzU58_$566 !QR%A A HrNct|}|jr||fS|js tdt d|dt }t |d}|jd}tjd}} |sP|}|D](\}} ||jks|j| ks&d}ntxjd zc_ |sP|t_ ||j || \}}  fS#|t_ wxYw) a Find a rational isolating interval for a real algebraic number. Examples ======== >>> from sympy import isolate, sqrt, Rational >>> print(isolate(sqrt(2))) # doctest: +SKIP (1, 2) >>> print(isolate(sqrt(2), eps=Rational(1, 100))) (24/17, 17/12) Parameters ========== alg : str, int, :py:class:`~.Expr` The algebraic number to be isolated. Must be a real number, to use this particular function. However, see also :py:meth:`.Poly.intervals`, which isolates complex roots when you pass ``all=True``. eps : positive element of :ref:`QQ`, None, optional (default=None) Precision to be passed to :py:meth:`.Poly.refine_root` fast : boolean, optional (default=False) Say whether fast refinement procedure should be used. (Will be passed to :py:meth:`.Poly.refine_root`.) Returns ======= Pair of rational numbers defining an isolating interval for the given algebraic number. See Also ======== .Poly.intervals z+complex algebraic numbers are not supportedrUmpmath)modulesprinterT)polys)sqfFr )epsfast) r is_Rationalis_realNotImplementedErrorr r r intervalsr dpsr$rI refine_root) algrorpfuncpolyrtrudoner$rIs risolater{sN #,C Sz [[! 9; ; BX7H ID 3d #D4(IC &C! 1:#%%1*D  !  1#D91 q6M s< C7C7- C77 D)NF)rTsympy.core.sympifyrsympy.ntheory.factor_r!sympy.polys.domains.rationalfieldrsympy.polys.domains.integerringrsympy.polys.matrices.exceptionsr sympy.polys.numberfields.minpolyrsympy.printing.lambdareprr sympy.utilities.decoratorr sympy.utilities.lambdifyr rjr rrrr-r/rZrhr{rUrrrs-&+0.745,-@2/* &UUp[[[|44nT nEEr