
    wg                     $    d Z ddlmZ dZd Zd Zy)z-Heuristic polynomial GCD algorithm (HEUGCD).    )HeuristicGCDFailed   c                    | j                   |j                   k(  r | j                   j                  j                  sJ | j                   }|j                  d   }|j                  }| j	                  |      \  }} }| j                         }|j                         } |dt        ||      z  dz         }t        t        |d|j                  |      z        dt        |t        | j                        z  |t        |j                        z        z  dz         }	t        dt              D ]  }
| j                  ||	      }|j                  ||	      }|r?|r<|j                  dk(  r|j                  ||      \  }}}nt!        ||      \  }}}t#        ||	|      }|j%                         d   }| j'                  |      \  }}|s.|j'                  |      \  }}|s|j)                  |      }|||fc S t#        ||	|      }| j'                  |      \  }}|s.|j'                  |      \  }}|s|j)                  |      }|||fc S t#        ||	|      }|j'                  |      \  }}|s.| j'                  |      \  }}|s|j)                  |      }|||fc S d|	z  |j                  |j                  |	            z  dz  }	 t+        d	      )
aH  
    Heuristic polynomial GCD in ``Z[X]``.

    Given univariate polynomials ``f`` and ``g`` in ``Z[X]``, returns
    their GCD and cofactors, i.e. polynomials ``h``, ``cff`` and ``cfg``
    such that::

          h = gcd(f, g), cff = quo(f, h) and cfg = quo(g, h)

    The algorithm is purely heuristic which means it may fail to compute
    the GCD. This will be signaled by raising an exception. In this case
    you will need to switch to another GCD method.

    The algorithm computes the polynomial GCD by evaluating polynomials
    ``f`` and ``g`` at certain points and computing (fast) integer GCD
    of those evaluations. The polynomial GCD is recovered from the integer
    image by interpolation. The evaluation process reduces f and g variable
    by variable into a large integer. The final step is to verify if the
    interpolated polynomial is the correct GCD. This gives cofactors of
    the input polynomials as a side effect.

    Examples
    ========

    >>> from sympy.polys.heuristicgcd import heugcd
    >>> from sympy.polys import ring, ZZ

    >>> R, x,y, = ring("x,y", ZZ)

    >>> f = x**2 + 2*x*y + y**2
    >>> g = x**2 + x*y

    >>> h, cff, cfg = heugcd(f, g)
    >>> h, cff, cfg
    (x + y, x + y, x)

    >>> cff*h == f
    True
    >>> cfg*h == g
    True

    References
    ==========

    .. [1] [Liao95]_

              c      r   iB  ii  zno luck)ringdomainis_ZZgensextract_groundmax_normminmaxsqrtabsLCrangeHEU_GCD_MAXevaluatengens	cofactorsheugcd_gcd_interpolate	primitivediv
mul_groundr   )fgr   x0r   gcdf_normg_normBxiffgghcffcfgcff_rcfg_s                      ]/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/sympy/polys/heuristicgcd.pyr   r      s   ` 66QVV 3 33366D	1B[[F  #ICAZZ\FZZ\FqVV$$r)*AC2fkk!n$%c&CI%CI%' ')*+	,A 1k" ,;ZZAZZA"zzQ$..r263$Rn3 At,Aa AeeAhGD!%%(aS)AdD=("340C55:DAq%%(aS)Ac4<'"340C55:DAq%%(aS)AdC<'!Gfkk&++a.11U:Y,;\ Y
''    c                 b   |j                   d}}|j                  dk(  r-| r}| |z  }||dz  kD  r||z  }| |z
  |z  } |r|||f<   |dz  }| r*nR| rP| j                  |      }| |z
  j                  |      } |r!|j	                         D ]  \  }}|||f|z   <    |dz  }| rP|j
                  dk  r| S |S )z-Interpolate polynomial GCD from integer GCD. r   r   r   )zeror   trunc_ground
quo_ground	itertermsr   )r+   r'   r   r    r(   r!   monomcoeffs           r1   r   r   x   s    99aqA zzQAA16z161Q1A 1$FA  q!AQ""1%A $%KKM ,LE5&+AqdUlO,FA  	ttaxr		r2   N)__doc__
polyerrorsr   r   r   r    r2   r1   <module>r=      s    3 *o(br2   