+
    iM                     n   R t ^ RIHtHtHtHtHtHtHtH	t	H
t
Ht ^ RIHtHtHtHtHtHtHtHtHtHtHtHt ^ RIHtHtHtHtHtHtH t H!t!H"t" ^ RI#H$t$H%t%H&t&H't'H(t(H)t) ^ RI*H+t+H,t, ^ RI-H.t.H/t/ R t0R t1R	 t2R
 t3R t4R t5R t6R t7R t8R t9R t:R t;RR lt<RR lt=RR lt>RR lt?RR lt@RR ltAR tBR tCR# )z8Square-free decomposition algorithms and related tools. )
dup_negdmp_negdup_subdmp_subdup_muldmp_muldup_quodmp_quodup_mul_grounddmp_mul_ground)	dup_stripdup_LCdmp_ground_LC
dmp_zero_p
dmp_ground
dup_degree
dmp_degreedmp_degree_indmp_degree_list	dmp_raise
dmp_injectdup_convert)	dup_diffdmp_diffdmp_diff_in	dup_shift	dmp_shift	dup_monicdmp_ground_monicdup_primitivedmp_ground_primitive)dup_inner_gcddmp_inner_gcddup_gcddmp_gcddmp_resultantdmp_primitive)gf_sqf_listgf_sqf_part)MultivariatePolynomialErrorDomainErrorc                N    \        R V 4       4      pV\        V 4      8X  g   Q hR# )z=Sanity check the degrees of a computed factorization in K[x].c              3   J   "   T F  w  rV\        V4      ,          x  K  	  R # 5i)N)r   ).0facks   &  w/Users/tonyclaw/.openclaw/workspace/skills/math-calculator/venv/lib/python3.14/site-packages/sympy/polys/sqfreetools.py	<genexpr>%_dup_check_degrees.<locals>.<genexpr>$   s     9&hsa*S/!!&s   !#N)sumr   )fresultdegs   && r0   _dup_check_degreesr7   "   s$    
9&9
9C*Q-    c                    ^ .V^,           ,          pV F;  w  rE\        WA4      p\        W64       UUu. uF  w  rxWuV,          ,           NK  	  pppK=  	  \        V4      \        W4      8X  g   Q hR# u uppi )z=Sanity check the degrees of a computed factorization in K[X].N)r   ziptuple)	r4   ur5   degsr.   r/   degs_facd1d2s	   &&&      r0   _dmp_check_degreesrA   (   sl    3!a%=D"3**-d*=>*=V*=>  ;/!//// ?s   A0c           
     b    V '       g   R# \        \        V \        V ^V4      V4      4      '       * # )z
Return ``True`` if ``f`` is a square-free polynomial in ``K[x]``.

Examples
========

>>> from sympy.polys import ring, ZZ
>>> R, x = ring("x", ZZ)

>>> R.dup_sqf_p(x**2 - 2*x + 1)
False
>>> R.dup_sqf_p(x**2 - 1)
True

T)r   r#   r   )r4   Ks   &&r0   	dup_sqf_prD   1   s*      ga!Q):A>???r8   c                    \        W4      '       d   R# \        V^,           4       FD  p\        V ^W1V4      p\        WA4      '       d   K$  \        WW4      p\	        WSV4      ^ 8w  g   KC   R# 	  R# )z
Return ``True`` if ``f`` is a square-free polynomial in ``K[X]``.

Examples
========

>>> from sympy.polys import ring, ZZ
>>> R, x,y = ring("x,y", ZZ)

>>> R.dmp_sqf_p(x**2 + 2*x*y + y**2)
False
>>> R.dmp_sqf_p(x**2 + y**2)
True

TF)r   ranger   r$   r   )r4   r<   rC   ifpgcds   &&&   r0   	dmp_sqf_prJ   G   se      !1Q3ZAqQ'baQ"#q(  r8   c                r   VP                   '       g   \        R4      h^ \        VP                  P	                  4       ^^ VP
                  4      r2 \        V ^ VRR7      w  rE\        W4^VP
                  4      p\        WaP
                  4      '       d   M!\        WP                  ) V4      V^,           r Kf  W V3# )a  
Find a shift of `f` in `K[x]` that has square-free norm.

The domain `K` must be an algebraic number field `k(a)` (see :ref:`QQ(a)`).

Returns `(s,g,r)`, such that `g(x)=f(x-sa)`, `r(x)=\text{Norm}(g(x))` and
`r` is a square-free polynomial over `k`.

Examples
========

We first create the algebraic number field `K=k(a)=\mathbb{Q}(\sqrt{3})`
and rings `K[x]` and `k[x]`:

>>> from sympy.polys import ring, QQ
>>> from sympy import sqrt

>>> K = QQ.algebraic_field(sqrt(3))
>>> R, x = ring("x", K)
>>> _, X = ring("x", QQ)

We can now find a square free norm for a shift of `f`:

>>> f = x**2 - 1
>>> s, g, r = R.dup_sqf_norm(f)

The choice of shift `s` is arbitrary and the particular values returned for
`g` and `r` are determined by `s`.

>>> s == 1
True
>>> g == x**2 - 2*sqrt(3)*x + 2
True
>>> r == X**4 - 8*X**2 + 4
True

The invariants are:

>>> g == f.shift(-s*K.unit)
True
>>> g.norm() == r
True
>>> r.is_squarefree
True

Explanation
===========

This is part of Trager's algorithm for factorizing polynomials over
algebraic number fields. In particular this function is algorithm
``sqfr_norm`` from [Trager76]_.

See Also
========

dmp_sqf_norm:
    Analogous function for multivariate polynomials over ``k(a)``.
dmp_norm:
    Computes the norm of `f` directly without any shift.
dup_ext_factor:
    Function implementing Trager's algorithm that uses this.
sympy.polys.polytools.sqf_norm:
    High-level interface for using this function.
ground domain must be algebraicTfront)is_Algebraicr*   r   modto_listdomr   r%   rD   r   unit)r4   rC   sgh_rs   &&     r0   dup_sqf_normrY   i   s    B >>>;<<iAquu5q
!Q.!155)QQ+QUq7Nr8   c           	   #    "   V^,           p^ .V,          pW@3x  VP                   p\        W4      p\        \        V\	        V^,           4      4      4       UUu. uF  w  rxV^ 8  g   K  VNK  	  p	ppV	 F@  pVP                  4       p
^W&   V
 Uu. uF  q) V,          NK  	  pp\        WW4      pW3x  KB  	  ^ p V^,          pV.V,          pV) V,          .V,          p\        V VW4      pVV3x  K;  u uppi u upi 5i)z9Generate a sequence of candidate shifts for dmp_sqf_norm.)rS   r   sortedr:   rF   copyr   )r4   r<   rC   ns0addirG   var_indicess1s1ia1f1jsjajfjs   &&&               r0   _dmp_sqf_norm_shiftsrk      s	     	
AA
qB
%K 	
A 	A"(Qac
);"<G"<Q11"<KG WWY "#bff#qa#f  	
A
	QS1WbdVaZq"a#"f# H $s%   ADC6*C60 DC<"ADc                   V'       g   \        W4      w  r4pV.WE3# VP                  '       g   \        R4      h\        VP                  P                  4       V^,           ^ VP                  4      p\        WV4       FQ  w  r0\        WVRR7      w  rg\        WFV^,           VP                  4      p\        WQVP                  4      '       g   KQ   M	  XV X3# )aD  
Find a shift of ``f`` in ``K[X]`` that has square-free norm.

The domain `K` must be an algebraic number field `k(a)` (see :ref:`QQ(a)`).

Returns `(s,g,r)`, such that `g(x_1,x_2,\cdots)=f(x_1-s_1 a, x_2 - s_2 a,
\cdots)`, `r(x)=\text{Norm}(g(x))` and `r` is a square-free polynomial over
`k`.

Examples
========

We first create the algebraic number field `K=k(a)=\mathbb{Q}(i)` and rings
`K[x,y]` and `k[x,y]`:

>>> from sympy.polys import ring, QQ
>>> from sympy import I

>>> K = QQ.algebraic_field(I)
>>> R, x, y = ring("x,y", K)
>>> _, X, Y = ring("x,y", QQ)

We can now find a square free norm for a shift of `f`:

>>> f = x*y + y**2
>>> s, g, r = R.dmp_sqf_norm(f)

The choice of shifts ``s`` is arbitrary and the particular values returned
for ``g`` and ``r`` are determined by ``s``.

>>> s
[0, 1]
>>> g == x*y - I*x + y**2 - 2*I*y - 1
True
>>> r == X**2*Y**2 + X**2 + 2*X*Y**3 + 2*X*Y + Y**4 + 2*Y**2 + 1
True

The required invariants are:

>>> g == f.shift_list([-si*K.unit for si in s])
True
>>> g.norm() == r
True
>>> r.is_squarefree
True

Explanation
===========

This is part of Trager's algorithm for factorizing polynomials over
algebraic number fields. In particular this function is a multivariate
generalization of algorithm ``sqfr_norm`` from [Trager76]_.

See Also
========

dup_sqf_norm:
    Analogous function for univariate polynomials over ``k(a)``.
dmp_norm:
    Computes the norm of `f` directly without any shift.
dmp_ext_factor:
    Function implementing Trager's algorithm that uses this.
sympy.polys.polytools.sqf_norm:
    High-level interface for using this function.
rL   TrM   )rY   rO   r*   r   rP   rQ   rR   rk   r   r%   rJ   )r4   r<   rC   rT   rU   rX   rV   rW   s   &&&     r0   dmp_sqf_normrm      s    D q$asAy>>>;<<!%%--/1q5!QUU3A$Q1-!.!Aquu-Q155!! . a7Nr8   c                   VP                   '       g   \        R4      h\        VP                  P	                  4       V^,           ^ VP
                  4      p\        WVRR7      w  rE\        W4V^,           VP
                  4      # )ai  
Norm of ``f`` in ``K[X]``, often not square-free.

The domain `K` must be an algebraic number field `k(a)` (see :ref:`QQ(a)`).

Examples
========

We first define the algebraic number field `K = k(a) = \mathbb{Q}(\sqrt{2})`:

>>> from sympy import QQ, sqrt
>>> from sympy.polys.sqfreetools import dmp_norm
>>> k = QQ
>>> K = k.algebraic_field(sqrt(2))

We can now compute the norm of a polynomial `p` in `K[x,y]`:

>>> p = [[K(1)], [K(1),K.unit]]                  # x + y + sqrt(2)
>>> N = [[k(1)], [k(2),k(0)], [k(1),k(0),k(-2)]] # x**2 + 2*x*y + y**2 - 2
>>> dmp_norm(p, 1, K) == N
True

In higher level functions that is:

>>> from sympy import expand, roots, minpoly
>>> from sympy.abc import x, y
>>> from math import prod
>>> a = sqrt(2)
>>> e = (x + y + a)
>>> e.as_poly([x, y], extension=a).norm()
Poly(x**2 + 2*x*y + y**2 - 2, x, y, domain='QQ')

This is equal to the product of the expressions `x + y + a_i` where the
`a_i` are the conjugates of `a`:

>>> pa = minpoly(a)
>>> pa
_x**2 - 2
>>> rs = roots(pa, multiple=True)
>>> rs
[sqrt(2), -sqrt(2)]
>>> n = prod(e.subs(a, r) for r in rs)
>>> n
(x + y - sqrt(2))*(x + y + sqrt(2))
>>> expand(n)
x**2 + 2*x*y + y**2 - 2

Explanation
===========

Given an algebraic number field `K = k(a)` any element `b` of `K` can be
represented as polynomial function `b=g(a)` where `g` is in `k[x]`. If the
minimal polynomial of `a` over `k` is `p_a` then the roots `a_1`, `a_2`,
`\cdots` of `p_a(x)` are the conjugates of `a`. The norm of `b` is the
product `g(a1) \times g(a2) \times \cdots` and is an element of `k`.

As in [Trager76]_ we extend this norm to multivariate polynomials over `K`.
If `b(x)` is a polynomial in `k(a)[X]` then we can think of `b` as being
alternately a function `g_X(a)` where `g_X` is an element of `k[X][y]` i.e.
a polynomial function with coefficients that are elements of `k[X]`. Then
the norm of `b` is the product `g_X(a1) \times g_X(a2) \times \cdots` and
will be an element of `k[X]`.

See Also
========

dmp_sqf_norm:
    Compute a shift of `f` so that the `\text{Norm}(f)` is square-free.
sympy.polys.polytools.Poly.norm:
    Higher-level function that calls this.
rL   TrM   )rO   r*   r   rP   rQ   rR   r   r%   )r4   r<   rC   rU   rV   rW   s   &&&   r0   dmp_normro   9  sc    P >>>;<<!%%--/1q5!QUU3AaAT*DAq1uaee,,r8   c                    \        WVP                  4      p \        WP                  VP                  4      p\        W!P                  V4      # )z3Compute square-free part of ``f`` in ``GF(p)[x]``. )r   rR   r(   rP   )r4   rC   rU   s   && r0   dup_gf_sqf_partrq     s7    A!%% AAuuaee$Aq%%##r8   c                    \        R4      h)z3Compute square-free part of ``f`` in ``GF(p)[X]``. +multivariate polynomials over finite fieldsNotImplementedErrorr4   r<   rC   s   &&&r0   dmp_gf_sqf_partrw         
K
LLr8   c                L   VP                   '       d   \        W4      # V '       g   V # VP                  \        W4      4      '       d   \	        W4      p \        V \        V ^V4      V4      p\        WV4      pVP                  '       d   \        W14      # \        W14      ^,          # )z
Returns square-free part of a polynomial in ``K[x]``.

Examples
========

>>> from sympy.polys import ring, ZZ
>>> R, x = ring("x", ZZ)

>>> R.dup_sqf_part(x**3 - 3*x - 2)
x**2 - x - 2

See Also
========

sympy.polys.polytools.Poly.sqf_part
)is_FiniteFieldrq   is_negativer   r   r#   r   r   is_Fieldr   r   )r4   rC   rI   sqfs   &&  r0   dup_sqf_partr~     s    $ 	q$$}}VA\""AM
!XaA&
*C
!!
Czzz  S$Q''r8   c                   V'       g   \        W4      # VP                  '       d   \        WV4      # \        W4      '       d   V # VP	                  \        WV4      4      '       d   \        WV4      p T p\        V^,           4       F  p\        V\        V ^WAV4      W4      pK  	  \        WW4      pVP                  '       d   \        WQV4      # \        WQV4      ^,          # )z
Returns square-free part of a polynomial in ``K[X]``.

Examples
========

>>> from sympy.polys import ring, ZZ
>>> R, x,y = ring("x,y", ZZ)

>>> R.dmp_sqf_part(x**3 + 2*x**2*y + x*y**2)
x**2 + x*y

)r~   rz   rw   r   r{   r   r   rF   r$   r   r	   r|   r   r    )r4   r<   rC   rI   rG   r}   s   &&&   r0   dmp_sqf_partr     s     A!!qQ''!}}]1+,,A!
C1Q3Zc;q!Q15q< 
!!
Czzz**#CA.q11r8   c                ,   T p\        WVP                  4      p \        WP                  VP                  VR7      w  rE\	        V4       F"  w  pw  r\        WP                  V4      V3WV&   K$  	  \        W54       VP                  WAP                  4      V3# )z<Compute square-free decomposition of ``f`` in ``GF(p)[x]``. all)r   rR   r'   rP   	enumerater7   convert)r4   rC   r   f_origcoefffactorsrG   r/   s   &&&     r0   dup_gf_sqf_listr     s}    FA!%% A EE155c:NEw'	6A!!UUA.2
 ( v'99UEE"G++r8   c                    \        R4      h)z<Compute square-free decomposition of ``f`` in ``GF(p)[X]``. rs   rt   )r4   r<   rC   r   s   &&&&r0   dmp_gf_sqf_listr     rx   r8   c                   VP                   '       d   \        WVR7      # T pVP                  '       d   \        W4      p\	        W4      p M;\        W4      w  r@VP                  \        W4      4      '       d   \        W4      p V) p\        V 4      ^ 8:  d   V. 3# . ^re\        V ^V4      p\        WV4      w  rp
 \        V	^V4      p\        WV4      pV'       g   VP                  W34       MD\        WV4      w  rp
V'       g   \        V4      ^ 8  d   VP                  W34       V^,          pKy  \        W54       WE3# )a  
Return square-free decomposition of a polynomial in ``K[x]``.

Uses Yun's algorithm from [Yun76]_.

Examples
========

>>> from sympy.polys import ring, ZZ
>>> R, x = ring("x", ZZ)

>>> f = 2*x**5 + 16*x**4 + 50*x**3 + 76*x**2 + 56*x + 16

>>> R.dup_sqf_list(f)
(2, [(x + 1, 2), (x + 2, 3)])
>>> R.dup_sqf_list(f, all=True)
(2, [(1, 1), (x + 1, 2), (x + 2, 3)])

See Also
========

dmp_sqf_list:
    Corresponding function for multivariate polynomials.
sympy.polys.polytools.sqf_list:
    High-level function for square-free factorization of expressions.
sympy.polys.polytools.Poly.sqf_list:
    Analogous method on :class:`~.Poly`.

References
==========

[Yun76]_
r   )rz   r   r|   r   r   r   r{   r   r   r   r!   r   appendr7   )r4   rC   r   r   r   r5   rG   rV   rU   pqr`   s   &&&         r0   dup_sqf_listr     s   D 	q--FzzzqaO &==&&AFE!}byAAAqAA!$GA!
Q1A!MM1&!a(a*Q-!#MM1&!	Qv&=r8   c                    \        WVR7      w  r4V'       dC   V^ ,          ^,          ^8X  d.   \        V^ ,          ^ ,          W14      pV^3.VR,          ,           # \        V.4      pV^3.V,           # )aU  
Return square-free decomposition of a polynomial in ``K[x]``.

Examples
========

>>> from sympy.polys import ring, ZZ
>>> R, x = ring("x", ZZ)

>>> f = 2*x**5 + 16*x**4 + 50*x**3 + 76*x**2 + 56*x + 16

>>> R.dup_sqf_list_include(f)
[(2, 1), (x + 1, 2), (x + 2, 3)]
>>> R.dup_sqf_list_include(f, all=True)
[(2, 1), (x + 1, 2), (x + 2, 3)]

r      NN)r   r
   r   )r4   rC   r   r   r   rU   s   &&&   r0   dup_sqf_list_includer   A  sj    $ "!C0NE71:a=A%71:a=%3Ax'"+%%ugAx'!!r8   c                   V'       g   \        WVR7      # VP                  '       d   \        WW#R7      # T pVP                  '       d   \	        WV4      p\        WV4      p M>\        WV4      w  rPVP                  \	        WV4      4      '       d   \        WV4      p V) p\        W4      pV^ 8  d   V. 3# \        WV4      w  rp/ pV^ 8w  d   \        V ^W4      p	\        W	W4      w  rp^p \        V^W4      p\        WW4      p	\        W4      '       d   WV&   M6\        WW4      w  rpV'       g   \        W4      ^ 8  d   WV&   V^,          pKf  \        Wq^,
          W#R7      w  ppW_,          pV F*  w  ppV.pW9   d   \!        W,          VW4      W&   K&  VW&   K,  	  \#        V4       Uu. uF  qV,          V3NK  	  pp\%        WAV4       WX3# u upi )a  
Return square-free decomposition of a polynomial in `K[X]`.

Examples
========

>>> from sympy.polys import ring, ZZ
>>> R, x,y = ring("x,y", ZZ)

>>> f = x**5 + 2*x**4*y + x**3*y**2

>>> R.dmp_sqf_list(f)
(1, [(x + y, 2), (x, 3)])
>>> R.dmp_sqf_list(f, all=True)
(1, [(1, 1), (x + y, 2), (x, 3)])

Explanation
===========

Uses Yun's algorithm for univariate polynomials from [Yun76]_ recursively.
The multivariate polynomial is treated as a univariate polynomial in its
leading variable. Then Yun's algorithm computes the square-free
factorization of the primitive and the content is factored recursively.

It would be better to use a dedicated algorithm for multivariate
polynomials instead.

See Also
========

dup_sqf_list:
    Corresponding function for univariate polynomials.
sympy.polys.polytools.sqf_list:
    High-level function for square-free factorization of expressions.
sympy.polys.polytools.Poly.sqf_list:
    Analogous method on :class:`~.Poly`.
r   )r   rz   r   r|   r   r   r    r{   r   r   r&   r   r"   r   r   dmp_sqf_listr   r[   rA   )r4   r<   rC   r   r   r   r6   contentr5   rV   rU   r   r   rG   r`   coeff_contentresult_contentr.   s   &&&&              r0   r   r   ]  s   L Ac**qQ00FzzzaA&Q1%'a0==qQ/00a AFE
Q
C
Qwby qQ'JGF
axQ1 a+aAq$Aa#A!q	#A!/GA!j&*q	FA$0A#q$J!M>	E !Qe;	35FIFI ! '-Vn5nay!nnF5v&)=	 6s   -Gc                   V'       g   \        WVR7      # \        WW#R7      w  rEV'       dD   V^ ,          ^,          ^8X  d/   \        V^ ,          ^ ,          WAV4      pV^3.VR,          ,           # \        WA4      pV^3.V,           # )a<  
Return square-free decomposition of a polynomial in ``K[x]``.

Examples
========

>>> from sympy.polys import ring, ZZ
>>> R, x,y = ring("x,y", ZZ)

>>> f = x**5 + 2*x**4*y + x**3*y**2

>>> R.dmp_sqf_list_include(f)
[(1, 1), (x + y, 2), (x, 3)]
>>> R.dmp_sqf_list_include(f, all=True)
[(1, 1), (x + y, 2), (x, 3)]

r   r   )r   r   r   r   )r4   r<   rC   r   r   r   rU   s   &&&&   r0   dmp_sqf_list_includer     sz    $ #Ac22!!3NE71:a=A%71:a=%A6Ax'"+%%u Ax'!!r8   c           
        V '       g   \        R4      h\        W4      p \        V 4      '       g   . # \        V \	        WP
                  V4      V4      p\        W!4      p\        V4       F2  w  pw  rV\        V\	        WQ! V4      ) V4      V4      pWV^,           3W4&   K4  	  \        WV4      p \        V 4      '       g   V# V ^3.V,           # )z
Compute greatest factorial factorization of ``f`` in ``K[x]``.

Examples
========

>>> from sympy.polys import ring, ZZ
>>> R, x = ring("x", ZZ)

>>> R.dup_gff_list(x**5 + 2*x**4 - x**3 - 2*x**2)
[(x, 1), (x + 2, 4)]

zDgreatest factorial factorization doesn't exist for a zero polynomial)

ValueErrorr   r   r#   r   onedup_gff_listr   r   r   )r4   rC   rU   HrG   rV   r/   s   &&     r0   r   r     s     _``!Aa==	AyEE1-q1"1IAv9Q1q115A1u:AD & A!!}}HF8a<r8   c                >    V'       g   \        W4      # \        V 4      h)z
Compute greatest factorial factorization of ``f`` in ``K[X]``.

Examples
========

>>> from sympy.polys import ring, ZZ
>>> R, x,y = ring("x,y", ZZ)

)r   r)   rv   s   &&&r0   dmp_gff_listr     s     A!!)!,,r8   N)F)D__doc__sympy.polys.densearithr   r   r   r   r   r   r   r	   r
   r   sympy.polys.densebasicr   r   r   r   r   r   r   r   r   r   r   r   sympy.polys.densetoolsr   r   r   r   r   r   r   r   r    sympy.polys.euclidtoolsr!   r"   r#   r$   r%   r&   sympy.polys.galoistoolsr'   r(   sympy.polys.polyerrorsr)   r*   r7   rA   rD   rJ   rY   rk   rm   ro   rq   rw   r~   r   r   r   r   r   r   r   r   r    r8   r0   <module>r      s    >$ $ $   ) ) )
" "
 0@,DOd%PSlN-b$M
!(H"2J, M
JZ"8iX">" J-r8   