
    MZd$                        d Z ddlmZ ddlmZ ddlmZmZmZ ddl	m
Z
  G d de
      ZddlmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z- dd	l.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@mAZAmBZBmCZCmDZDmEZEmFZFmGZGmHZHmIZImJZJmKZKmLZLmMZM dd
lNmOZOmPZPmQZQmRZRmSZSmTZTmUZUmVZVmWZWmXZXmYZYmZZZm[Z[m\Z\ ddl]m^Z^m_Z_m`Z`maZambZbmcZcmdZdmeZemfZfmgZg ddlhmiZimjZjmkZkmlZlmmZmmnZnmoZo ddlpmqZqmrZrmsZsmtZt ddlumvZvmwZwmxZxmyZymzZzm{Z{m|Z|m}Z}m~Z~mZmZ ddlmZmZ d Z G d de
e      Zd Z G d de
e      Zd Z G d de
e      Zy)z1OO layer for several polynomial representations.     )oo)CantSympify)CoercionFailedNotReversibleNotInvertible)PicklableWithSlotsc                   2    e Zd ZdZd Zd Zd Zed        Zy)GenericPolyz5Base class for low-level polynomial representations. c                 T    | j                  | j                  j                               S zMake the ground domain a ring. )
set_domaindomget_ringfs    9/usr/lib/python3/dist-packages/sympy/polys/polyclasses.pyground_to_ringzGenericPoly.ground_to_ring   s    ||AEENN,--    c                 T    | j                  | j                  j                               S z Make the ground domain a field. )r   r   	get_fieldr   s    r   ground_to_fieldzGenericPoly.ground_to_field       ||AEEOO-..r   c                 T    | j                  | j                  j                               S zMake the ground domain exact. )r   r   	get_exactr   s    r   ground_to_exactzGenericPoly.ground_to_exact   r   r   c                 d    |r|\  }}D cg c]  \  }} | |      |f }}}|r|fS |S c c}}w N )perresultincludecoefffactorsgks          r   _perify_factorszGenericPoly._perify_factors   sD    #NE7,35DAqSVQK55'>!N 6s   ,N)	__name__
__module____qualname____doc__r   r   r   classmethodr(   r    r   r   r
   r
   
   s(    ?.// 	 	r   r
   )!dmp_validate
dup_normal
dmp_normaldup_convertdmp_convertdmp_from_sympy	dup_strip
dup_degreedmp_degree_indmp_degree_listdmp_negative_pdup_LCdmp_ground_LCdup_TCdmp_ground_TCdmp_ground_nthdmp_one
dmp_ground
dmp_zero_p	dmp_one_pdmp_ground_pdup_from_dictdmp_from_dictdmp_to_dictdmp_deflate
dmp_inject	dmp_ejectdmp_terms_gcddmp_list_termsdmp_excludedmp_slice_indmp_permutedmp_to_tuple)dmp_add_grounddmp_sub_grounddmp_mul_grounddmp_quo_grounddmp_exquo_grounddmp_absdup_negdmp_negdup_adddmp_adddup_subdmp_subdup_muldmp_muldmp_sqrdup_powdmp_powdmp_pdivdmp_premdmp_pquo
dmp_pexquodmp_divdup_remdmp_remdmp_quo	dmp_exquodmp_add_muldmp_sub_muldmp_max_normdmp_l1_normdmp_l2_norm_squared)dmp_clear_denomsdmp_integrate_indmp_diff_indmp_eval_in
dup_revertdmp_ground_truncdmp_ground_contentdmp_ground_primitivedmp_ground_monicdmp_composedup_decompose	dup_shiftdup_transformdmp_lift)
dup_half_gcdex	dup_gcdex
dup_invertdmp_subresultantsdmp_resultantdmp_discriminantdmp_inner_gcddmp_gcddmp_lcm
dmp_cancel)dup_gff_listdmp_norm	dmp_sqf_pdmp_sqf_normdmp_sqf_partdmp_sqf_listdmp_sqf_list_include)dup_cyclotomic_pdmp_irreducible_pdmp_factor_listdmp_factor_list_include)dup_isolate_real_roots_sqfdup_isolate_real_rootsdup_isolate_all_roots_sqfdup_isolate_all_rootsdup_refine_real_rootdup_count_real_rootsdup_count_complex_roots	dup_sturmdup_cauchy_upper_bounddup_cauchy_lower_bounddup_mignotte_sep_bound_squared)UnificationFailedPolynomialErrorc                 2    t        t        | ||      ||      S r   )DMPr0   )replevr   s      r   init_normal_DMPr      s    z#sC(#s33r   c                   F   e Zd ZdZdZddZd Zd Zd ZddZ	e
dd	       Ze
dd
       Ze
d        Ze
d        ZddZddZd Zd Zd Ze
d        Ze
dd       Zd Zd Zd Zd ZddZddZddZddZd Zd Zd Z d Z!d  Z"dd!Z#dd"Z$d# Z%d$ Z&d% Z'd& Z(d' Z)d( Z*d) Z+d* Z,d+ Z-d, Z.d- Z/d. Z0d/ Z1d0 Z2d1 Z3d2 Z4d3 Z5d4 Z6d5 Z7d6 Z8d7 Z9d8 Z:d9 Z;dd:Z<d; Z=d< Z>d= Z?d> Z@d? ZAd@ ZBdA ZCdB ZDdC ZEdD ZFdE ZGddFZHddGZIddHZJdI ZKdJ ZLdK ZMdL ZNdM ZOddNZPdO ZQdP ZRdQ ZSdR ZTddSZUdT ZVdU ZWdV ZXdW ZYdX ZZdY Z[dZ Z\d[ Z]d\ Z^d] Z_d^ Z`d_ Zad` Zbda Zcdb Zddc ZedddZfddeZgdf Zhdg ZiddhZjddiZkddjZlddkZmendl        Zoendm        Zpendn        Zqendo        Zrendp        Zsendq        Ztendr        Zuends        Zvendt        Zwendu        Zxendv        Zyendw        Zzdx Z{dy Z|dz Z}d{ Z~d| Zd} Zd~ Zd Zd Zd Zd Zd Zd Zd Zd Zd ZddZddZd Zd Zd Zd Zd Zd Zy)r   z)Dense Multivariate Polynomials over `K`. )r   r   r   ringNc                     |Kt        |      t        u rt        |||      }n:t        |t              s*t        |j                  |      |      }nt        |      \  }}|| _        || _	        || _
        || _        y r   )typedictrD   
isinstancelistr?   convertr.   r   r   r   r   )selfr   r   r   r   s        r   __init__zDMP.__init__   sj    ?CyD #Cc2T* S!137#C(HC	r   c                     | j                   j                  d| j                  d| j                  d| j                  dS N(, ))	__class__r)   r   r   r   r   s    r   __repr__zDMP.__repr__   s'    #$;;#7#7qvvNNr   c                     t        | j                  j                  | j                         | j                  | j
                  | j                  f      S r   )hashr   r)   to_tupler   r   r   r   s    r   __hash__zDMP.__hash__   s4    Q[[))1::<qvvNOOr   c                    t        |t              r| j                  |j                  k7  rt        d| d|      | j                  |j                  k(  rR| j
                  |j
                  k(  r9| j                  | j                  | j                  | j                  |j                  fS | j                  | j                  j                  |j                        }}| j
                  |j
                  *j                  |j
                        n|j
                  t        | j                  || j                  |      }t        |j                  ||j                  |      }||dffd	}|||||fS )z7Unify representations of two multivariate polynomials. Cannot unify  with Fc                 6    |r	|s| S |dz  }t        | ||      S N   r   )r   r   r   killr   s       r   r!   zDMP.unify.<locals>.per   s(    "
q3S$//r   )
r   r   r   r   r   r   r!   r   unifyr2   )r   r&   r   r   FGr!   r   s          @r   r   z	DMP.unify   s   !S!QUUaee^#A$FGG55AEE>aff.55!%%quu44uuaeekk!%%0C66Dvv!#::aff-D66DAEE3s3AAEE3s3A c 0 S!Q&&r   c                     | j                   }|r	|s|S |dz  }|| j                  }|| j                  }t        ||||      S z.Create a DMP out of the given representation. r   )r   r   r   r   )r   r   r   r   r   r   s         r   r!   zDMP.per   sL    ee
q;%%C<66D3S$''r   c                     t        d|||      S Nr   r   clsr   r   r   s       r   zerozDMP.zero       1c3%%r   c                     t        d|||      S r   r   r   s       r   onezDMP.one   r   r   c                 .     | t        ||d|      ||      S )zCCreate an instance of ``cls`` given a list of native coefficients. N)r2   r   r   r   r   s       r   	from_listzDMP.from_list   s     ;sCs3S#>>r   c                 ,     | t        |||      ||      S )zBCreate an instance of ``cls`` given a list of SymPy coefficients. )r3   r   s       r   from_sympy_listzDMP.from_sympy_list   s     >#sC0#s;;r   c                 \    t        | j                  | j                  | j                  |      S )AConvert ``f`` to a dict representation with native coefficients. r   )rE   r   r   r   )r   r   s     r   to_dictzDMP.to_dict   s    155!%%T::r   c                     t        | j                  | j                  | j                  |      }|j	                         D ]#  \  }}| j                  j                  |      ||<   % |S )@Convert ``f`` to a dict representation with SymPy coefficients. r   )rE   r   r   r   itemsto_sympy)r   r   r   r'   vs        r   to_sympy_dictzDMP.to_sympy_dict   sT    !%%D9IIK 	'DAqUU^^A&CF	' 
r   c                     | j                   S zAConvert ``f`` to a list representation with native coefficients. r   r   s    r   to_listzDMP.to_list       uur   c                 6      fd  j                         S )@Convert ``f`` to a list representation with SymPy coefficients. c                     g }| D ]T  }t        |t              r|j                   |             +|j                  j                  j	                  |             V |S r   )r   r   appendr   r   )r   outvalr   sympify_nested_lists      r   r   z.DMP.to_sympy_list.<locals>.sympify_nested_list  sS    C 4c4(JJ2378JJquu~~c23	4
 Jr   r   )r   r   s   `@r   to_sympy_listzDMP.to_sympy_list   s    	 #155))r   c                 B    t        | j                  | j                        S )x
        Convert ``f`` to a tuple representation with native coefficients.

        This is needed for hashing.
        )rN   r   r   r   s    r   r   zDMP.to_tuple  s     AEE155))r   c                 ,     | t        |||      ||      S )zBConstruct and instance of ``cls`` from a ``dict`` representation. )rD   r   s       r   	from_dictzDMP.from_dict  s     =c3/c::r   c           
      V    t        t        t        t        ||                  |||      S r   )r   r   r   zip)r   monomscoeffsr   r   r   s         r   from_monoms_coeffszDMP.from_monoms_coeffs  s$    4S012CdCCr   c                 T    | j                  | j                  j                               S r   )r   r   r   r   s    r   to_ringzDMP.to_ring  s    yy)**r   c                 T    | j                  | j                  j                               S r   )r   r   r   r   s    r   to_fieldzDMP.to_field!      yy*++r   c                 T    | j                  | j                  j                               S r   )r   r   r   r   s    r   to_exactzDMP.to_exact%  r   r   c                     | j                   |k(  r| S t        t        | j                  | j                  | j                   |      || j                        S )z$Convert the ground domain of ``f``. )r   r   r2   r   r   )r   r   s     r   r   zDMP.convert)  s<    55C<H{155!%%<c155IIr   c           
      |    | j                  t        | j                  |||| j                  | j                              S )z1Take a continuous subsequence of terms of ``f``. )r!   rL   r   r   r   )r   mnjs       r   slicez	DMP.slice0  s,    uu\!%%Aq!%%?@@r   c                     t        | j                  | j                  | j                  |      D cg c]  \  }}|	 c}}S c c}}w )z;Returns all non-zero coefficients from ``f`` in lex order. orderrJ   r   r   r   )r   r  _cs       r   r   z
DMP.coeffs4  1    -aeeQUUAEEOQtq!QQQ   Ac                     t        | j                  | j                  | j                  |      D cg c]  \  }}|	 c}}S c c}}w )z8Returns all non-zero monomials from ``f`` in lex order. r   r  )r   r  r   r  s       r   r   z
DMP.monoms8  r  r  c                 \    t        | j                  | j                  | j                  |      S )z4Returns all non-zero terms from ``f`` in lex order. r   r  )r   r  s     r   termsz	DMP.terms<  s    aeeQUUAEE??r   c                     | j                   s.| s| j                  j                  gS t        | j                        S t        d      )z%Returns all coefficients from ``f``. &multivariate polynomials not supported)r   r   r   r   r   r   r   s    r   
all_coeffszDMP.all_coeffs@  s6    uu

|#AEE{"!"JKKr   c                     | j                   sIt        | j                        }|dk  rdgS t        | j                        D cg c]  \  }}||z
  f c}}S t	        d      c c}}w )z"Returns all monomials from ``f``. r   r   r  )r   r5   r   	enumerater   r   r   ir  s       r   
all_monomszDMP.all_monomsJ  s[    uu155!A1uv.7.>@da!a%@@!"JKK As   A!c                     | j                   sat        | j                        }|dk  rd| j                  j                  fgS t        | j                        D cg c]  \  }}||z
  f|f c}}S t        d      c c}}w )z Returns all terms from a ``f``. r   r  r  )r   r5   r   r   r   r  r   r  s       r   	all_termszDMP.all_termsV  sn    uu155!A1uquuzz*++3<QUU3CE41a1q5(AEE!"JKK Fs   A9c                     | j                  t        | j                  | j                  | j                        | j                  j                        S )z-Convert algebraic coefficients to rationals. r   )r!   r{   r   r   r   r   s    r   liftzDMP.liftb  s2    uuXaeeQUUAEE2		uBBr   c                     t        | j                  | j                  | j                        \  }}|| j	                  |      fS )z2Reduce degree of `f` by mapping `x_i^m` to `y_i`. )rF   r   r   r   r!   r   Jr   s      r   deflatezDMP.deflatef  s2    155!%%/1!%%({r   c                     t        | j                  | j                  | j                  |      \  }}| j	                  || j                  j                  |      S )z,Inject ground domain generators into ``f``. front)rG   r   r   r   r   )r   r  r   r   s       r   injectz
DMP.injectk  s=    AEE155!%%u=3{{1aeeii--r   c                     t        | j                  | j                  ||      }| j                  ||| j                  t	        |j
                        z
        S )z2Eject selected generators into the ground domain. r  )rH   r   r   r   lensymbols)r   r   r  r   s       r   ejectz	DMP.ejectp  s?    aeeQUUCu5{{1c1553s{{+;#;<<r   c                     t        | j                  | j                  | j                        \  }}}|| j	                  || j                  |      fS )ao  
        Remove useless generators from ``f``.

        Returns the removed generators and the new excluded ``f``.

        Examples
        ========

        >>> from sympy.polys.polyclasses import DMP
        >>> from sympy.polys.domains import ZZ

        >>> DMP([[[ZZ(1)]], [[ZZ(1)], [ZZ(2)]]], ZZ).exclude()
        ([2], DMP([[1], [1, 2]], ZZ, None))

        )rK   r   r   r   r   )r   r  r   us       r   excludezDMP.excludeu  s@      aeeQUUAEE21a!++a***r   c                 x    | j                  t        | j                  || j                  | j                              S )a  
        Returns a polynomial in `K[x_{P(1)}, ..., x_{P(n)}]`.

        Examples
        ========

        >>> from sympy.polys.polyclasses import DMP
        >>> from sympy.polys.domains import ZZ

        >>> DMP([[[ZZ(2)], [ZZ(1), ZZ(0)]], [[]]], ZZ).permute([1, 0, 2])
        DMP([[[2], []], [[1, 0], []]], ZZ, None)

        >>> DMP([[[ZZ(2)], [ZZ(1), ZZ(0)]], [[]]], ZZ).permute([1, 2, 0])
        DMP([[[1], []], [[2, 0], []]], ZZ, None)

        )r!   rM   r   r   r   )r   Ps     r   permutezDMP.permute  s*    " uu[155!%%899r   c                     t        | j                  | j                  | j                        \  }}|| j	                  |      fS )z/Remove GCD of terms from the polynomial ``f``. )rI   r   r   r   r!   r  s      r   	terms_gcdzDMP.terms_gcd  s2    QUUAEE15511!%%({r   c                     | j                  t        | j                  | j                  j	                  |      | j
                  | j                              S )z.Add an element of the ground domain to ``f``. )r!   rO   r   r   r   r   r   r  s     r   
add_groundzDMP.add_ground  5    uu^AEE155==+;QUUAEEJKKr   c                     | j                  t        | j                  | j                  j	                  |      | j
                  | j                              S )z5Subtract an element of the ground domain from ``f``. )r!   rP   r   r   r   r   r-  s     r   
sub_groundzDMP.sub_ground  r/  r   c                     | j                  t        | j                  | j                  j	                  |      | j
                  | j                              S )z5Multiply ``f`` by a an element of the ground domain. )r!   rQ   r   r   r   r   r-  s     r   
mul_groundzDMP.mul_ground  r/  r   c                     | j                  t        | j                  | j                  j	                  |      | j
                  | j                              S )z8Quotient of ``f`` by a an element of the ground domain. )r!   rR   r   r   r   r   r-  s     r   
quo_groundzDMP.quo_ground  r/  r   c                     | j                  t        | j                  | j                  j	                  |      | j
                  | j                              S )z>Exact quotient of ``f`` by a an element of the ground domain. )r!   rS   r   r   r   r   r-  s     r   exquo_groundzDMP.exquo_ground  6    uu%aeeQUU]]1-=quuaeeLMMr   c                 v    | j                  t        | j                  | j                  | j                              S )z)Make all coefficients in ``f`` positive. )r!   rT   r   r   r   r   s    r   abszDMP.abs  &    uuWQUUAEE155122r   c                 v    | j                  t        | j                  | j                  | j                              S )"Negate all coefficients in ``f``. )r!   rV   r   r   r   r   s    r   negzDMP.neg  r;  r   c                 X    | j                  |      \  }}}}} |t        ||||            S )z2Add two multivariate polynomials ``f`` and ``g``. )r   rX   r   r&   r   r   r!   r   r   s          r   addzDMP.add  0    ggajS#q!71ac*++r   c                 X    | j                  |      \  }}}}} |t        ||||            S )z7Subtract two multivariate polynomials ``f`` and ``g``. )r   rZ   r@  s          r   subzDMP.sub  rB  r   c                 X    | j                  |      \  }}}}} |t        ||||            S )z7Multiply two multivariate polynomials ``f`` and ``g``. )r   r\   r@  s          r   mulzDMP.mul  rB  r   c                 v    | j                  t        | j                  | j                  | j                              S )z(Square a multivariate polynomial ``f``. )r!   r]   r   r   r   r   s    r   sqrzDMP.sqr  r;  r   c                     t        |t              r;| j                  t        | j                  || j
                  | j                              S t        dt        |      z        )+Raise ``f`` to a non-negative power ``n``. ``int`` expected, got %s)	r   intr!   r_   r   r   r   	TypeErrorr   r   r   s     r   powzDMP.pow  sG    a55155!%%8996a@AAr   c                 r    | j                  |      \  }}}}}t        ||||      \  }} ||       ||      fS )z/Polynomial pseudo-division of ``f`` and ``g``. )r   r`   	r   r&   r   r   r!   r   r   qrs	            r   pdivzDMP.pdiv  s@    ggajS#q!1c3'11vs1v~r   c                 X    | j                  |      \  }}}}} |t        ||||            S )z0Polynomial pseudo-remainder of ``f`` and ``g``. )r   ra   r@  s          r   premzDMP.prem  0    ggajS#q!8Aq#s+,,r   c                 X    | j                  |      \  }}}}} |t        ||||            S )z/Polynomial pseudo-quotient of ``f`` and ``g``. )r   rb   r@  s          r   pquozDMP.pquo  rW  r   c                 X    | j                  |      \  }}}}} |t        ||||            S )z5Polynomial exact pseudo-quotient of ``f`` and ``g``. )r   rc   r@  s          r   pexquoz
DMP.pexquo  s0    ggajS#q!:aC-..r   c                 r    | j                  |      \  }}}}}t        ||||      \  }} ||       ||      fS )z7Polynomial division with remainder of ``f`` and ``g``. )r   rd   rQ  s	            r   divzDMP.div  s@    ggajS#q!q!S#&11vs1v~r   c                 X    | j                  |      \  }}}}} |t        ||||            S )z2Computes polynomial remainder of ``f`` and ``g``. )r   rf   r@  s          r   remzDMP.rem  rB  r   c                 X    | j                  |      \  }}}}} |t        ||||            S )z1Computes polynomial quotient of ``f`` and ``g``. )r   rg   r@  s          r   quozDMP.quo  rB  r   c                     | j                  |      \  }}}}} |t        ||||            }| j                  r(|| j                  vrddlm}  || || j                        |S )z7Computes polynomial exact quotient of ``f`` and ``g``. r   ExactQuotientFailed)r   rh   r   sympy.polys.polyerrorsrd  )	r   r&   r   r   r!   r   r   resrd  s	            r   exquoz	DMP.exquo  s\    ggajS#q!)Aq#s+,66c'B%aAFF33
r   c                     t        |t              r!t        | j                  || j                        S t        dt        |      z        )z0Returns the leading degree of ``f`` in ``x_j``. rK  )r   rL  r6   r   r   rM  r   )r   r   s     r   degreez
DMP.degree  s8    a 155116a@AAr   c                 B    t        | j                  | j                        S )z$Returns a list of degrees of ``f``. )r7   r   r   r   s    r   degree_listzDMP.degree_list  s    quuaee,,r   c                 B    t        d | j                         D              S )z#Returns the total degree of ``f``. c              3   2   K   | ]  }t        |        y wr   sum).0r   s     r   	<genexpr>z#DMP.total_degree.<locals>.<genexpr>  s     .a3q6.s   )maxr   r   s    r   total_degreezDMP.total_degree  s    .188:...r   c                    | j                         }i }|t        | j                         d   d         k(  }| j                         D ][  }t        |d         }||k  r||z
  }nd}|r|d   ||d   |fz   <   0t	        |d         }||xx   |z  cc<   |d   |t        |      <   ] t        || j                  | j                  t        |      z   | j                        S )z&Return homogeneous polynomial of ``f``r   r   )rs  r!  r	  ro  r   tupler   r   r   rL  r   )	r   stdr"   
new_symboltermdr  ls	            r   
homogenizezDMP.homogenize  s    ^^3qwwy|A//
GGI 	+DDGA2vF)-atAw!~&aM!	#'7uQx 	+ 6155!%%#j/"9166BBr   c                     | j                   rt         S | j                         }t        |d         }|D ]  }t        |      }||k7  s y |S )z(Returns the homogeneous order of ``f``. r   N)is_zeror   r   ro  )r   r   tdegmonom_tdegs        r   homogeneous_orderzDMP.homogeneous_order&  sQ    993J6!9~ 	EJE}		 r   c                 X    t        | j                  | j                  | j                        S z*Returns the leading coefficient of ``f``. )r:   r   r   r   r   s    r   LCzDMP.LC6      QUUAEE15511r   c                 X    t        | j                  | j                  | j                        S z+Returns the trailing coefficient of ``f``. )r<   r   r   r   r   s    r   TCzDMP.TC:  r  r   c                     t        d |D              r,t        | j                  || j                  | j                        S t        d      )z+Returns the ``n``-th coefficient of ``f``. c              3   <   K   | ]  }t        |t                y wr   )r   rL  )rp  r   s     r   rq  zDMP.nth.<locals>.<genexpr>@  s     -az!S!-s   za sequence of integers expected)allr=   r   r   r   rM  )r   Ns     r   nthzDMP.nth>  s9    -1--!!%%AEE15599=>>r   c                 X    t        | j                  | j                  | j                        S )zReturns maximum norm of ``f``. )rk   r   r   r   r   s    r   max_normzDMP.max_normE  s    AEE155!%%00r   c                 X    t        | j                  | j                  | j                        S )zReturns l1 norm of ``f``. )rl   r   r   r   r   s    r   l1_normzDMP.l1_normI  s    155!%%//r   c                 X    t        | j                  | j                  | j                        S )z!Return squared l2 norm of ``f``. )rm   r   r   r   r   s    r   l2_norm_squaredzDMP.l2_norm_squaredM  s    "155!%%77r   c                     t        | j                  | j                  | j                        \  }}|| j	                  |      fS )z0Clear denominators, but keep the ground domain. )rn   r   r   r   r!   )r   r$   r   s      r   clear_denomszDMP.clear_denomsQ  s2    #AEE155!%%8qaeeAhr   c           	         t        |t              st        dt        |      z        t        |t              st        dt        |      z        | j	                  t        | j                  ||| j                  | j                              S )zEComputes the ``m``-th order indefinite integral of ``f`` in ``x_j``. rK  )	r   rL  rM  r   r!   ro   r   r   r   r   r   r   s      r   	integratezDMP.integrateV  si    !S!6a@AA!S!6a@AAuu%aeeQ155!%%@AAr   c           	         t        |t              st        dt        |      z        t        |t              st        dt        |      z        | j	                  t        | j                  ||| j                  | j                              S )z<Computes the ``m``-th order derivative of ``f`` in ``x_j``. rK  )	r   rL  rM  r   r!   rp   r   r   r   r  s      r   diffzDMP.diff`  sh    !S!6a@AA!S!6a@AAuu[1aeeQUU;<<r   c           	          t        |t              st        dt        |      z        | j	                  t        | j                  | j                  j                  |      || j                  | j                        d      S )z5Evaluates ``f`` at the given point ``a`` in ``x_j``. rK  Tr   )
r   rL  rM  r   r!   rq   r   r   r   r   )r   ar   s      r   evalzDMP.evalj  se    !S!6a@AAuu[EEMM!a/59  ; 	;r   c                     | j                  |      \  }}}}}|s t        |||      \  }} ||       ||      fS t        d      )z2Half extended Euclidean algorithm, if univariate. univariate polynomial expected)r   r|   
ValueError)	r   r&   r   r   r!   r   r   rv  hs	            r   
half_gcdexzDMP.half_gcdexr  sM    ggajS#q!!!Q,DAqq63q6>!=>>r   c                     | j                  |      \  }}}}}|s(t        |||      \  }}}	 ||       ||       ||	      fS t        d      )z-Extended Euclidean algorithm, if univariate. r  )r   r}   r  )
r   r&   r   r   r!   r   r   rv  tr  s
             r   gcdexz	DMP.gcdex|  sV    ggajS#q!1c*GAq!q63q63q6))=>>r   c                 p    | j                  |      \  }}}}}|s |t        |||            S t        d      )z(Invert ``f`` modulo ``g``, if possible. r  )r   r~   r  r@  s          r   invertz
DMP.invert  s=    ggajS#q!z!Q,--=>>r   c                     | j                   s0| j                  t        | j                  || j                              S t        d      )z"Compute ``f**(-1)`` mod ``x**n``. r  )r   r!   rr   r   r   r  rN  s     r   revertz
DMP.revert  s5    uu55AEE1aee455=>>r   c                 v    | j                  |      \  }}}}}t        ||||      }t        t        ||            S )z7Computes subresultant PRS sequence of ``f`` and ``g``. )r   r   r   map)r   r&   r   r   r!   r   r   Rs           r   subresultantszDMP.subresultants  s;    ggajS#q!aC-CQK  r   c                     | j                  |      \  }}}}}|r2t        |||||      \  }}	 ||d      t        t        ||	            fS  |t        ||||      d      S )z/Computes resultant of ``f`` and ``g`` via PRS. )
includePRSTr  )r   r   r   r  )
r   r&   r  r   r   r!   r   r   rf  r  s
             r   	resultantzDMP.resultant  sh    ggajS#q!"1acjIFCs&Sa[(999=AsC0t<<r   c                 z    | j                  t        | j                  | j                  | j                        d      S )z Computes discriminant of ``f``. Tr  )r!   r   r   r   r   r   s    r   discriminantzDMP.discriminant  s+    uu%aeeQUUAEE:uFFr   c                     | j                  |      \  }}}}}t        ||||      \  }}}	 ||       ||       ||	      fS )z4Returns GCD of ``f`` and ``g`` and their cofactors. )r   r   )
r   r&   r   r   r!   r   r   r  cffcfgs
             r   	cofactorszDMP.cofactors  sI    ggajS#q!#Aq#s331vs3xS))r   c                 X    | j                  |      \  }}}}} |t        ||||            S )z+Returns polynomial GCD of ``f`` and ``g``. )r   r   r@  s          r   gcdzDMP.gcd  rB  r   c                 X    | j                  |      \  }}}}} |t        ||||            S )z+Returns polynomial LCM of ``f`` and ``g``. )r   r   r@  s          r   lcmzDMP.lcm  rB  r   c                     | j                  |      \  }}}}}|rt        ||||d      \  }}nt        ||||d      \  }}	}} ||       ||      }}|r||fS 	||fS )z6Cancel common factors in a rational function ``f/g``. T)r#   F)r   r   )
r   r&   r#   r   r   r!   r   r   cFcGs
             r   cancelz
DMP.cancel  sx    ggajS#q!aCd;DAq%aCeDLBAq1vs1v1a4Kr1a<r   c                     | j                  t        | j                  | j                  j	                  |      | j
                  | j                              S )z&Reduce ``f`` modulo a constant ``p``. )r!   rs   r   r   r   r   )r   ps     r   truncz	DMP.trunc  r8  r   c                 v    | j                  t        | j                  | j                  | j                              S )z'Divides all coefficients by ``LC(f)``. )r!   rv   r   r   r   r   s    r   monicz	DMP.monic  s'    uu%aeeQUUAEE:;;r   c                 X    t        | j                  | j                  | j                        S )z(Returns GCD of polynomial coefficients. )rt   r   r   r   r   s    r   contentzDMP.content  s    !!%%66r   c                     t        | j                  | j                  | j                        \  }}|| j	                  |      fS )z/Returns content and a primitive form of ``f``. )ru   r   r   r   r!   )r   contr   s      r   	primitivezDMP.primitive  s2    &quuaeeQUU;aQUU1X~r   c                 X    | j                  |      \  }}}}} |t        ||||            S )z4Computes functional composition of ``f`` and ``g``. )r   rw   r@  s          r   composezDMP.compose  s0    ggajS#q!;q!S#.//r   c           	          | j                   s=t        t        | j                  t	        | j
                  | j                                    S t        d      )z,Computes functional decomposition of ``f``. r  )r   r   r  r!   rx   r   r   r  r   s    r   	decomposezDMP.decompose  s:    uuAEE=#>?@@=>>r   c                     | j                   sI| j                  t        | j                  | j                  j                  |      | j                              S t        d      )z/Efficiently compute Taylor shift ``f(x + a)``. r  )r   r!   ry   r   r   r   r  )r   r  s     r   shiftz	DMP.shift  sB    uu55155!%%--*:AEEBCC=>>r   c                 ,   | j                   rt        d      |j                  |      \  }}}}}| j                   ||||            \  }}}}} ||||      j                   ||||            \  }}}}}|s |t        ||||            S t        d      )z5Evaluate functional transformation ``q**n * f(p/q)``.r  )r   r  r   rz   )	r   r  rR  r   r   r!   r(  Qr   s	            r   	transformzDMP.transform  s    55=>>ggajS#q!ggc!S#&67S#q!!!S#.44SC5EFS#q!}Q1c233=>>r   c           	          | j                   s=t        t        | j                  t	        | j
                  | j                                    S t        d      )z&Computes the Sturm sequence of ``f``. r  )r   r   r  r!   r   r   r   r  r   s    r   sturmz	DMP.sturm  s:    uuAEE9QUUAEE#:;<<=>>r   c                 p    | j                   s t        | j                  | j                        S t	        d      )z7Computes the Cauchy upper bound on the roots of ``f``. r  )r   r   r   r   r  r   s    r   cauchy_upper_boundzDMP.cauchy_upper_bound  *    uu)!%%77=>>r   c                 p    | j                   s t        | j                  | j                        S t	        d      )z?Computes the Cauchy lower bound on the nonzero roots of ``f``. r  )r   r   r   r   r  r   s    r   cauchy_lower_boundzDMP.cauchy_lower_bound  r  r   c                 p    | j                   s t        | j                  | j                        S t	        d      )zBComputes the squared Mignotte bound on root separations of ``f``. r  )r   r   r   r   r  r   s    r   mignotte_sep_bound_squaredzDMP.mignotte_sep_bound_squared  s*    uu1!%%??=>>r   c                     | j                   sDt        | j                  | j                        D cg c]  \  }}| j	                  |      |f c}}S t        d      c c}}w )z4Computes greatest factorial factorization of ``f``. r  )r   r   r   r   r!   r  )r   r&   r'   s      r   gff_listzDMP.gff_list  sK    uu/;AEE155/IKtq!aeeAh]KK=>> Ls   Ac                     t        | j                  | j                  | j                        }| j	                  || j                  j                        S )zComputes ``Norm(f)``.r  )r   r   r   r   r!   )r   rS  s     r   normzDMP.norm  s7    QUUAEE155)uuQAEEIIu&&r   c                     t        | j                  | j                  | j                        \  }}}|| j	                  |      | j	                  || j                  j                        fS )z$Computes square-free norm of ``f``. r  )r   r   r   r   r!   )r   rv  r&   rS  s       r   sqf_normzDMP.sqf_norm"  sL    quuaeeQUU31a!%%(AEE!E333r   c                 v    | j                  t        | j                  | j                  | j                              S )z$Computes square-free part of ``f``. )r!   r   r   r   r   r   s    r   sqf_partzDMP.sqf_part'  s&    uu\!%%677r   c                     t        | j                  | j                  | j                  |      \  }}||D cg c]  \  }}| j	                  |      |f c}}fS c c}}w z0Returns a list of square-free factors of ``f``. )r   r   r   r   r!   )r   r  r$   r%   r&   r'   s         r   sqf_listzDMP.sqf_list+  sK    %aeeQUUAEE3?w';$!Qq1;;;;s   Ac                     t        | j                  | j                  | j                  |      }|D cg c]  \  }}| j	                  |      |f c}}S c c}}w r  )r   r   r   r   r!   )r   r  r%   r&   r'   s        r   sqf_list_includezDMP.sqf_list_include0  sB    &quuaeeQUUC@+2441a!%%(A444s   Ac                     t        | j                  | j                  | j                        \  }}||D cg c]  \  }}| j	                  |      |f c}}fS c c}}w z0Returns a list of irreducible factors of ``f``. )r   r   r   r   r!   )r   r$   r%   r&   r'   s        r   factor_listzDMP.factor_list5  sI    (quu=w';$!Qq1;;;;s   Ac                     t        | j                  | j                  | j                        }|D cg c]  \  }}| j	                  |      |f c}}S c c}}w r  )r   r   r   r   r!   )r   r%   r&   r'   s       r   factor_list_includezDMP.factor_list_include:  s@    )!%%>+2441a!%%(A444s   Ac                 d   | j                   s|sL|s%t        | j                  | j                  ||||      S t	        | j                  | j                  ||||      S |s%t        | j                  | j                  ||||      S t        | j                  | j                  ||||      S t        d      )z0Compute isolating intervals for roots of ``f``. )epsinfsupfastz1Cannot isolate roots of a multivariate polynomial)r   r   r   r   r   r   r   r   )r   r  r  r  r  r  sqfs          r   	intervalszDMP.intervals?  s    uu1!%%CSVY`dee5aeeQUUQTZ]dhii03CUX_cdd4QUUAEEsPSY\cghh!CE Er   c           	      |    | j                   s&t        | j                  ||| j                  |||      S t	        d      )zu
        Refine an isolating interval to the given precision.

        ``eps`` should be a rational number.

        )r  stepsr  z1Cannot refine a root of a multivariate polynomial)r   r   r   r   r   )r   rv  r  r  r  r  s         r   refine_rootzDMP.refine_rootP  s=     uu'q!QUU5W[\\!CE Er   c                 H    t        | j                  | j                  ||      S )z<Return the number of real roots of ``f`` in ``[inf, sup]``. r  r  )r   r   r   r   r  r  s      r   count_real_rootszDMP.count_real_roots]  s    #AEE155csCCr   c                 H    t        | j                  | j                  ||      S )z?Return the number of complex roots of ``f`` in ``[inf, sup]``. r  )r   r   r   r  s      r   count_complex_rootszDMP.count_complex_rootsa  s    &quuaee#FFr   c                 B    t        | j                  | j                        S )z0Returns ``True`` if ``f`` is a zero polynomial. r@   r   r   r   s    r   r~  zDMP.is_zeroe       !%%''r   c                 X    t        | j                  | j                  | j                        S )z0Returns ``True`` if ``f`` is a unit polynomial. )rA   r   r   r   r   s    r   is_onez
DMP.is_onej       quu--r   c                 D    t        | j                  d| j                        S )>Returns ``True`` if ``f`` is an element of the ground domain. N)rB   r   r   r   s    r   	is_groundzDMP.is_groundo  s     AEE4//r   c                 X    t        | j                  | j                  | j                        S )z7Returns ``True`` if ``f`` is a square-free polynomial. )r   r   r   r   r   s    r   is_sqfz
DMP.is_sqft  r  r   c                     | j                   j                  t        | j                  | j                  | j                               S )z=Returns ``True`` if the leading coefficient of ``f`` is one. )r   r  r:   r   r   r   s    r   is_moniczDMP.is_monicy  s,     uu||M!%%>??r   c                     | j                   j                  t        | j                  | j                  | j                               S )zAReturns ``True`` if the GCD of the coefficients of ``f`` is one. )r   r  rt   r   r   r   s    r   is_primitivezDMP.is_primitive~  s-     uu||.quuaeeQUUCDDr   c                     t        d t        | j                  | j                  | j                        j                         D              S )z:Returns ``True`` if ``f`` is linear in all its variables. c              3   8   K   | ]  }t        |      d k    yw)r   Nrn  rp  r  s     r   rq  z DMP.is_linear.<locals>.<genexpr>       Xu3u:?X   r  rE   r   r   r   keysr   s    r   	is_linearzDMP.is_linear  3     XAEE155!%%0P0U0U0WXXXr   c                     t        d t        | j                  | j                  | j                        j                         D              S )z=Returns ``True`` if ``f`` is quadratic in all its variables. c              3   8   K   | ]  }t        |      d k    yw)   Nrn  r  s     r   rq  z#DMP.is_quadratic.<locals>.<genexpr>  r  r  r  r   s    r   is_quadraticzDMP.is_quadratic  r  r   c                 :    t        | j                               dk  S )z8Returns ``True`` if ``f`` is zero or has only one term. r   )r!  r   r   s    r   is_monomialzDMP.is_monomial  s     199;1$$r   c                 &    | j                         duS )z7Returns ``True`` if ``f`` is a homogeneous polynomial. N)r  r   s    r   is_homogeneouszDMP.is_homogeneous  s     ""$D00r   c                 X    t        | j                  | j                  | j                        S )z:Returns ``True`` if ``f`` has no factors over its domain. )r   r   r   r   r   s    r   is_irreduciblezDMP.is_irreducible  s     !quu55r   c                 \    | j                   s t        | j                  | j                        S y)z6Returns ``True`` if ``f`` is a cyclotomic polynomial. F)r   r   r   r   r   s    r   is_cyclotomiczDMP.is_cyclotomic  s#     uu#AEE15511r   c                 "    | j                         S r   )r:  r   s    r   __abs__zDMP.__abs__      uuwr   c                 "    | j                         S r   r>  r   s    r   __neg__zDMP.__neg__  r%  r   c                    t        |t              s?	 | j                  t        | j                  j                  |      | j                              }| j                  |      S # t        $ r	 t        cY S t        t        f$ rI | j                  :	 | j                  j                  |      }n# t        t        f$ r t        cY cY S w xY wY xw xY wr   )r   r   r!   r?   r   r   r   rM  NotImplementedr   NotImplementedErrorr   rA  r   r&   s     r   __add__zDMP.__add__      !S!	.EE*QUU]]1%5quu=> uuQx  &%%"$78 .66%.FFNN1-*,?@ .--.	.;   >A! !C	2C	B)(C	)C>C	CC	C	c                 $    | j                  |      S r   r-  r,  s     r   __radd__zDMP.__radd__      yy|r   c                    t        |t              s?	 | j                  t        | j                  j                  |      | j                              }| j                  |      S # t        $ r	 t        cY S t        t        f$ rI | j                  :	 | j                  j                  |      }n# t        t        f$ r t        cY cY S w xY wY xw xY wr   )r   r   r!   r?   r   r   r   rM  r*  r   r+  r   rD  r,  s     r   __sub__zDMP.__sub__  r.  r/  c                 &    |  j                  |      S r   r1  r,  s     r   __rsub__zDMP.__rsub__      ||Ar   c                 Z   t        |t              r| j                  |      S 	 | j                  |      S # t        $ r	 t
        cY S t        t        f$ rW | j                  B	 | j                  | j                  j                  |            cY S # t        t        f$ r Y nw xY wt
        cY S w xY wr   )
r   r   rF  r3  rM  r*  r   r+  r   r   r,  s     r   __mul__zDMP.__mul__  s    a558O
&||A& &%%"$78 &66% uuQVV^^A%677*,?@ %%&9   4 B*B* )B	B*BB*B	B*)B*c                 Z   t        |t              r| j                  |      S 	 | j                  |      S # t        $ r	 t
        cY S t        t        f$ rW | j                  B	 | j                  | j                  j                  |            cY S # t        t        f$ r Y nw xY wt
        cY S w xY wr   )
r   r   rg  r3  rM  r*  r   r+  r   r   r,  s     r   __truediv__zDMP.__truediv__  s    a771:
&||A& &%%"$78 &66% wwqvv~~a'899*,?@ %%&r;  c                     t        |t              r|j                  |       S | j                  +	 | j                  j	                  |      j                  |       S t        S # t
        t        f$ r Y t        S w xY wr   )r   r   rg  r   r   r   r+  r*  r,  s     r   __rtruediv__zDMP.__rtruediv__  so    a771:VVvv~~a(..q11  #$78 s   )A A65A6c                 $    | j                  |      S r   r:  r,  s     r   __rmul__zDMP.__rmul__  r3  r   c                 $    | j                  |      S r   rO  rN  s     r   __pow__zDMP.__pow__      uuQxr   c                 $    | j                  |      S r   r]  r,  s     r   
__divmod__zDMP.__divmod__  rF  r   c                 $    | j                  |      S r   r_  r,  s     r   __mod__zDMP.__mod__  rF  r   c                     t        |t              r| j                  |      S 	 | j                  |      S # t        $ r	 t
        cY S w xY wr   )r   r   ra  r5  rM  r*  r,  s     r   __floordiv__zDMP.__floordiv__  sA    a558O&||A& &%%&s   4 AAc                     	 | j                  |      \  }}}}}| j                  |j                  k(  r||k(  S 	 y# t        $ r Y yw xY wNF)r   r   r   r   r&   r  r   r   s        r   __eq__z
DMP.__eq__  sW    	GGAJMAq!Quu~Av 
  ! 		s   49 	AAc                     | |k(   S r   r    r,  s     r   __ne__z
DMP.__ne__  s    6zr   c                 2    |s| |k(  S | j                  |      S r   )
_strict_eqr   r&   stricts      r   eqzDMP.eq  s    6M<<?"r   c                 *    | j                  ||       S )N)rX  )rY  rW  s      r   nezDMP.ne!  s    44&4)))r   c                     t        || j                        xrO | j                  |j                  k(  xr4 | j                  |j                  k(  xr | j                  |j                  k(  S r   )r   r   r   r   r   r,  s     r   rV  zDMP._strict_eq$  sP    !Q[[) aeequun 	r   c                 :    | j                  |      \  }}}}}||k  S r   r   rQ  s        r   __lt__z
DMP.__lt__)  "    
1aA1ur   c                 :    | j                  |      \  }}}}}||k  S r   r^  rQ  s        r   __le__z
DMP.__le__-  "    
1aAAvr   c                 :    | j                  |      \  }}}}}||kD  S r   r^  rQ  s        r   __gt__z
DMP.__gt__1  r`  r   c                 :    | j                  |      \  }}}}}||k\  S r   r^  rQ  s        r   __ge__z
DMP.__ge__5  rc  r   c                 D    t        | j                  | j                         S r   r   r   s    r   __bool__zDMP.__bool__9      aeeQUU+++r   NN)NFNr   Fr  )r   r   T)FNNNFF)NNF)r)   r*   r+   r,   	__slots__r   r   r   r   r!   r-   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r	  r  r  r  r  r  r  r#  r&  r)  r+  r.  r1  r3  r5  r7  r:  r>  rA  rD  rF  rH  rO  rT  rV  rY  r[  r]  r_  ra  rg  ri  rk  rs  r|  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  propertyr~  r  r  r	  r  r  r  r  r  r  r   r"  r$  r(  r-  r2  r5  r7  r:  r=  r?  rB  rE  rI  rL  rN  rR  rT  rY  r[  rV  r_  rb  re  rg  ri  r    r   r   r   r      s   3-IOP'<($ & & & & ? ? < <;** ; ; D D+,,JARR@L
L
LC
.
=
+&:&
LLLLN33,
,
,
3B-
-
/
,
,
B-/C& 22?108
B=;????!=G*,
,
  N<7
0
????????'
4
8<
5
<
5
E"EDG ( ( . . 0 0 . . @ @ E E Y Y Y Y % % 1 1 6 6  & & &	#*
,r   r   c                 J    t        t        | ||      t        |||      ||      S r   )DMFr0   )numdenr   r   s       r   init_normal_DMFrt  =  s*    z#sC(#sC(#s4 4r   c                   T   e Zd ZdZdZd,dZed,d       Zed-d       Zd Z	d Z
d	 Zd
 Zd.dZd/dZed-d       Zed-d       Zd Zd Zd Zd Zd Zd Zd Zd Zd ZeZd0dZed        Zed        Zd Zd Z d Z!d Z"d Z#d  Z$d! Z%d" Z&d# Z'd$ Z(d% Z)d& Z*d' Z+d( Z,d) Z-d* Z.d+ Z/y)1rq  z'Dense Multivariate Fractions over `K`. )rr  rs  r   r   r   Nc                     | j                  |||      \  }}}t        ||||      \  }}|| _        || _        || _        || _        || _        y r   )_parser   rr  rs  r   r   r   )r   r   r   r   r   rr  rs  s          r   r   zDMF.__init__G  sQ    Cc2S#c3S1S	r   c                     | j                  |||      \  }}}t        j                  |       }||_        ||_        ||_        ||_        ||_        |S r   )rw  object__new__rr  rs  r   r   r   )r   r   r   r   r   rr  rs  objs           r   newzDMF.newQ  sP    

3S1S#nnS!
r   c                    t        |t              r|\  }}|;t        |t              rt        |||      }t        |t              r=t        |||      }n/t	        |      \  }}t	        |      \  }}||k(  r|}nt        d      t        ||      rt        d      t        ||      rt        ||      }nt        |||      rt        |||      }t        |||      }nh|}|Jt        |t              rt        |||      }n:t        |t              s*t        |j                  |      |      }nt	        |      \  }}t        ||      }|||fS )Nzinconsistent number of levelszfraction denominator)r   ru  r   rD   r.   r  r@   ZeroDivisionErrorr>   r8   rV   r   r?   r   )r   r   r   r   rr  rs  num_levden_levs           r   rw  z
DMF._parse_  s:   c5!HCc4('S#6Cc4('S#6C+C0W+C0Wg%!C$%DEE#s#'(>??#s#c3'!#sC0!#sC0C!#sC0CCc4('S#6C#C.$S[[%5s;C',S#s#CC}r   c           
          | j                   j                  d| j                  d| j                  d| j                  d| j
                  d
S )Nz((r   z), r   )r   r)   rr  rs  r   r   r   s    r   r   zDMF.__repr__  s2    )*)=)=quuaee)*8 	8r   c                     t        | j                  j                  t        | j                  | j
                        t        | j                  | j
                        | j
                  | j                  | j                  f      S r   )	r   r   r)   rN   rr  r   rs  r   r   r   s    r   r   zDMF.__hash__  sU    Q[[))<quu+E&quuaff> ? 	?r   c                 B    t        |t              r j                  |j                  k7  rt        d d|       j                  |j                  k(  r^ j
                  |j
                  k(  rE j                   j                   j                   j                   j                  f|j                  fS  j                   j                  j                  |j                        c} j
                  |j
                  *j                  |j
                        n|j
                  t         j                  | j                        t         j                  | j                        f}t        |j                  ||j                        }dd|f fd	}||||fS )z0Unify a multivariate fraction and a polynomial. r   r   TFc                     |r|s| |z  S |dz
  }|rt        | ||      \  } }j                  j                  | |f|      S Nr   r   r   r   r|  rr  rs  r  r   r   r   r   r   s        r   r!   zDMF.poly_unify.<locals>.per  Q    "3w!Ag)#sC=HC{{Sz3$GGr   )r   r   r   r   r   r   r!   rr  rs  r   r   r2   r   r&   r   r   r   r!   r   r   s   `     @@r   
poly_unifyzDMF.poly_unify  s7   !S!QUUaee^#A$FGG55AEE>aff.EE155!%%!%%??uuaeekk!%%0HC66Dvv!#::aff-D66DQUUC4QUUC46A AEE3s3A%)3 
H S!Q&&r   c                     t        |t              r j                  |j                  k7  rt        d d|       j                  |j                  k(  rj j
                  |j
                  k(  rQ j                   j                   j                   j                   j                  f|j                  |j                  ffS  j                   j                  j                  |j                        c} j
                  |j
                  *j                  |j
                        n|j
                  t         j                  | j                        t         j                  | j                        f}t        |j                  ||j                        t        |j                  ||j                        f}dd|f fd	}||||fS )z5Unify representations of two multivariate fractions. r   r   TFc                     |r|s| |z  S |dz
  }|rt        | ||      \  } }j                  j                  | |f|      S r  r  r  s        r   r!   zDMF.frac_unify.<locals>.per  r  r   )r   rq  r   r   r   r   r!   rr  rs  r   r2   r  s   `     @@r   
frac_unifyzDMF.frac_unify  sc   !S!QUUaee^#A$FGG55AEE>aff.EE155!%%!%%*+%%9 9 uuaeekk!%%0HC66Dvv!#::aff-D66DQUUC4QUUC46A QUUC4QUUC46A &*3 
H S!Q&&r   c                     | j                   | j                  }}|r|s||z  S |dz  }|rt        ||||      \  }}|| j                  }| j                  j                  ||f|||      S )z.Create a DMF out of the given representation. r   r  )r   r   r   r   r   r|  )r   rr  rs  r  r   r   r   r   s           r   r!   zDMF.per  so    55!%%S3wq!#sC5HC<66D{{Sz3$??r   c                 ^    | j                   }|r	|s|S |dz  }t        || j                  |      S r   )r   r   r   )r   r   r   r   s       r   half_perzDMF.half_per  s2    ee
q3s##r   c                 ,    | j                  d|||      S )Nr   r  r|  r   s       r   r   zDMF.zero      wwq#sw..r   c                 ,    | j                  d|||      S r  r  r   s       r   r   zDMF.one  r  r   c                 8    | j                  | j                        S )z Returns the numerator of ``f``. )r  rr  r   s    r   numerz	DMF.numer      zz!%%  r   c                 8    | j                  | j                        S )z"Returns the denominator of ``f``. )r  rs  r   s    r   denomz	DMF.denom  r  r   c                 N    | j                  | j                  | j                        S )z4Remove common factors from ``f.num`` and ``f.den``. )r!   rr  rs  r   s    r   r  z
DMF.cancel	  s    uuQUUAEE""r   c                     | j                  t        | j                  | j                  | j                        | j
                  d      S )r=  Fr  )r!   rV   rr  r   r   rs  r   s    r   r>  zDMF.neg  s0    uuWQUUAEE1551155uGGr   c           	      8   t        |t              r,| j                  |      \  }}}\  }}}t        |||||      |}	}nV| j	                  |      \  }}}}
}|
|c\  }}\  }}t        t        ||||      t        ||||      ||      }t        ||||      }	 |||	      S )z0Add two multivariate fractions ``f`` and ``g``. )r   r   r  ri   r  rX   r\   r   r&   r   r   r!   F_numF_denr   rr  rs  r   G_numG_dens                r   rA  zDMF.add      a/0||A,Cc>E51"5%C=uC"#,,q/Cc1a-.*NUENUE'%S9!%S93EC%S1C3}r   c           	      8   t        |t              r,| j                  |      \  }}}\  }}}t        |||||      |}	}nV| j	                  |      \  }}}}
}|
|c\  }}\  }}t        t        ||||      t        ||||      ||      }t        ||||      }	 |||	      S )z5Subtract two multivariate fractions ``f`` and ``g``. )r   r   r  rj   r  rZ   r\   r  s                r   rD  zDMF.sub   r  r   c                    t        |t              r+| j                  |      \  }}}\  }}}t        ||||      |}	}n>| j	                  |      \  }}}}
}|
|c\  }}\  }}t        ||||      }t        ||||      }	 |||	      S )z5Multiply two multivariate fractions ``f`` and ``g``. )r   r   r  r\   r  r  s                r   rF  zDMF.mul/  s    a/0||A,Cc>E51uac2EC"#,,q/Cc1a-.*NUENUE%S1C%S1C3}r   c           	      @   t        |t              rx| j                  | j                  }}|dk  r||| }}}| j	                  t        ||| j                  | j                        t        ||| j                  | j                        d      S t        dt        |      z        )rJ  r   Fr  rK  )
r   rL  rr  rs  r!   r_   r   r   rM  r   )r   r   rr  rs  s       r   rO  zDMF.pow=  s    auuaeeC1u!3!S55a6 a6u  F F 6a@AAr   c                 r   t        |t              r+| j                  |      \  }}}\  }}}|t        ||||      }	}n>| j	                  |      \  }}}}
}|
|c\  }}\  }}t        ||||      }t        ||||      }	 |||	      }| j
                  (|| j
                  vrddlm}  || || j
                        |S )z0Computes quotient of fractions ``f`` and ``g``. r   rc  )r   r   r  r\   r  r   re  rd  )r   r&   r   r   r!   r  r  r   rr  rs  r   r  r  rf  rd  s                  r   ra  zDMF.quoH  s    a/0||A,Cc>E51geQS9C"#,,q/Cc1a-.*NUENUE%S1C%S1C#sm66#QVV"3B%aAFF33
r   c                     |r=| j                   1| j                   j                  |       st        | | j                         | j                  | j                  | j
                  d      }|S )z&Computes inverse of a fraction ``f``. Fr  )r   is_unitr   r!   rs  rr  )r   checkrf  s      r   r  z
DMF.invert\  sO    QVV'q0A166**eeAEE155e/
r   c                 B    t        | j                  | j                        S )z.Returns ``True`` if ``f`` is a zero fraction. r@   rr  r   r   s    r   r~  zDMF.is_zeroc  r  r   c                     t        | j                  | j                  | j                        xr+ t        | j                  | j                  | j                        S )z.Returns ``True`` if ``f`` is a unit fraction. )rA   rr  r   r   rs  r   s    r   r  z
DMF.is_oneh  s>     quu- +aeeQUUAEE*	+r   c                 "    | j                         S r   r'  r   s    r   r(  zDMF.__neg__n  r%  r   c                    t        |t        t        f      r| j                  |      S 	 | j                  | j	                  |            S # t
        $ r	 t        cY S t        t        f$ rW | j                  B	 | j                  | j                  j                  |            cY S # t        t        f$ r Y nw xY wt        cY S w xY wr   )r   r   rq  rA  r  rM  r*  r   r+  r   r   r,  s     r   r-  zDMF.__add__q      a#s$558O
	"55A'' 	"!! 34 	"vv!55!233&(;< !!	";   A	 	B?B?5)B!B?!B30B?2B33	B?>B?c                 $    | j                  |      S r   r1  r,  s     r   r2  zDMF.__radd__  r3  r   c                    t        |t        t        f      r| j                  |      S 	 | j                  | j	                  |            S # t
        $ r	 t        cY S t        t        f$ rW | j                  B	 | j                  | j                  j                  |            cY S # t        t        f$ r Y nw xY wt        cY S w xY wr   )r   r   rq  rD  r  rM  r*  r   r+  r   r   r,  s     r   r5  zDMF.__sub__  r  r  c                 &    |  j                  |      S r   r1  r,  s     r   r7  zDMF.__rsub__  r8  r   c                    t        |t        t        f      r| j                  |      S 	 | j                  | j	                  |            S # t
        $ r	 t        cY S t        t        f$ rW | j                  B	 | j                  | j                  j                  |            cY S # t        t        f$ r Y nw xY wt        cY S w xY wr   )r   r   rq  rF  r  rM  r*  r   r+  r   r   r,  s     r   r:  zDMF.__mul__  r  r  c                 $    | j                  |      S r   rA  r,  s     r   rB  zDMF.__rmul__  r3  r   c                 $    | j                  |      S r   rD  rN  s     r   rE  zDMF.__pow__  rF  r   c                    t        |t        t        f      r| j                  |      S 	 | j                  | j	                  |            S # t
        $ r	 t        cY S t        t        f$ rW | j                  B	 | j                  | j                  j                  |            cY S # t        t        f$ r Y nw xY wt        cY S w xY wr   )r   r   rq  ra  r  rM  r*  r   r+  r   r   r,  s     r   r=  zDMF.__truediv__  r  r  c                     | j                  d      |z  }| j                  r(|| j                  vrddlm}  ||| | j                        |S )NF)r  r   rc  )r  r   re  rd  )r   r&   rS  rd  s       r   r?  zDMF.__rtruediv__  sD    KKeK$Q&99$))+B%atyy99r   c                 d   	 t        |t              r[| j                  |      \  }}}\  }}}| j                  |j                  k(  r]t	        || j                  | j
                        xr ||k(  S | j                  |      \  }}}}}| j                  |j                  k(  r||k(  S y# t        $ r Y yw xY wrP  r   r   r  r   rA   r   r  r   r   r&   r  r  r  r   r   s          r   rR  z
DMF.__eq__  s    	!S!-.\\!_*1a%55AEE>$UAEE1559HeqjH !Q1aA55AEE>6M  ! 		s   A*B# -4B# #	B/.B/c                 f   	 t        |t              r\| j                  |      \  }}}\  }}}| j                  |j                  k(  r^t	        || j                  | j
                        xr ||k(   S | j                  |      \  }}}}}| j                  |j                  k(  r||k7  S y# t        $ r Y yw xY wNTr  r  s          r   rT  z
DMF.__ne__  s    	!S!-.\\!_*1a%55AEE> )% > M5A:NN !Q1aA55AEE>6M  ! 		s   A+B$ .4B$ $	B0/B0c                 :    | j                  |      \  }}}}}||k  S r   r  rQ  s        r   r_  z
DMF.__lt__  "    Q1aA1ur   c                 :    | j                  |      \  }}}}}||k  S r   r  rQ  s        r   rb  z
DMF.__le__  "    Q1aAAvr   c                 :    | j                  |      \  }}}}}||kD  S r   r  rQ  s        r   re  z
DMF.__gt__  r  r   c                 :    | j                  |      \  }}}}}||k\  S r   r  rQ  s        r   rg  z
DMF.__ge__  r  r   c                 D    t        | j                  | j                         S r   r  r   s    r   ri  zDMF.__bool__  rj  r   rk  r   )TFNrl  rm  )0r)   r*   r+   r,   rn  r   r-   r|  rw  r   r   r  r  r!   r  r   r   r  r  r  r>  rA  rD  rF  rO  ra  rg  r  ro  r~  r  r(  r-  r2  r5  r7  r:  rB  rE  r=  r?  rR  rT  r_  rb  re  rg  ri  r    r   r   rq  rq  B  s9   14I   ) )V8?!'F#'J@$
$ / / / /!!#H	B$ E ( ( + +
" " " " "",r   rq  c                 D    t        t        | |      t        ||      |      S r   )ANPr/   r   modr   s      r   init_normal_ANPr    s$    z#s##s#S* *r   c                   d   e Zd ZdZdZd Zd Zd Zd Zd1dZ	e
d	        Ze
d
        Zd Zd Zd Zd Zd Ze
d        Zd Zd Zd Zd Zd Zd Zd Zd ZeZd Zd Zed        Zed        Z ed        Z!d Z"d Z#d  Z$d! Z%d" Z&d# Z'd$ Z(d% Z)d& Z*d' Z+d( Z,d) Z-d* Z.d+ Z/d, Z0d- Z1d. Z2d/ Z3d0 Z4y)2r  z1Dense Algebraic Number Polynomials over a field. r  c                    t        |      t        u rt        ||      | _        nQt	        |t
              r|D cg c]  }|j                  |       }}n|j                  |      g}t        |      | _        t	        |t              r|j                  | _	        || _
        y t	        |t              rt        ||      | _	        || _
        y t        |      | _	        || _
        y c c}w r   )r   r   rC   r   r   r   r   r4   r   r  r   )r   r   r  r   r  s        r   r   zANP.__init__  s    9$S#.DH#t$/23!s{{1~33{{3'( ~DHc3wwDH  #t$(c2  %S> 4s   Cc                     | j                   j                  d| j                  d| j                  d| j                  dS r   )r   r)   r   r  r   r   s    r   r   zANP.__repr__  s'    #$;;#7#7quuMMr   c                     t        | j                  j                  | j                         t	        | j
                  d      | j                  f      S r   )r   r   r)   r   rN   r  r   r   s    r   r   zANP.__hash__  s8    Q[[))1::<aeeQ9OQRQVQVWXXr   c                    t        |t              r| j                  |j                  k7  rt        d| d|      | j                  |j                  k(  r9| j                  | j
                  | j                  |j                  | j                  fS | j                  j                  |j                        t        | j                  | j                        }t        |j                  |j                        }| j                  k7  r1|j                  k7  r"t        | j                  | j                        n(| j                  k(  r| j                  n|j                  fd}|||fS )z0Unify representations of two algebraic numbers. r   r   c                     t        |       S r   r  )r   r   r  s    r   <lambda>zANP.unify.<locals>.<lambda>5  s    c#sC0 r   )	r   r  r  r   r   r!   r   r   r1   )r   r&   r   r   r!   r   r  s        @@r   r   z	ANP.unify   s    !S!QUUaee^#A$FGG55AEE>55!%%quu44%%++aee$CAEE155#.AAEE155#.Aaee|quu!!%%4!%%<%%C%%C0CCAs""r   Nc                 T    t        ||xs | j                  |xs | j                        S r   )r  r  r   )r   r   r  r   s       r   r!   zANP.per9  s!    3quuclQUU33r   c                     t        d||      S r   r  r   r  r   s      r   r   zANP.zero<      1c3r   c                     t        d||      S r   r  r  s      r   r   zANP.one@  r  r   c                 D    t        | j                  d| j                        S )r   r   )rE   r   r   r   s    r   r   zANP.to_dictD  s    155!QUU++r   c                     t        | j                  d| j                        }|j                         D ]#  \  }}| j                  j	                  |      ||<   % |S )r   r   )rE   r   r   r   r   )r   r   r'   r   s       r   r   zANP.to_sympy_dictH  sN    !%%AEE*IIK 	'DAqUU^^A&CF	' 
r   c                     | j                   S r   r   r   s    r   r   zANP.to_listQ  r   r   c                 r    | j                   D cg c]  }| j                  j                  |       c}S c c}w )r   )r   r   r   r-  s     r   r   zANP.to_sympy_listU  s'    ,-EE3q"333s   "4c                 .    t        | j                  d      S )r   r   )rN   r   r   s    r   r   zANP.to_tupleY  s     AEE1%%r   c           
      h    t        t        t        t        |j                  |                  ||      S r   )r  r4   r   r  r   )r   r   r  r   s       r   r   zANP.from_lista  s&    9T#ckk3"7893DDr   c                 `    | j                  t        | j                  | j                              S r   )r!   rU   r   r   r   s    r   r>  zANP.nege  s     uuWQUUAEE*++r   c                 V    | j                  |      \  }}}}} |t        |||            S r   )r   rW   r   r&   r   r!   r   r   r  s          r   rA  zANP.addh  .    ggajS!Q71a%&&r   c                 V    | j                  |      \  }}}}} |t        |||            S r   )r   rY   r  s          r   rD  zANP.subl  r  r   c           	      l    | j                  |      \  }}}}} |t        t        |||      ||            S r   )r   re   r[   r  s          r   rF  zANP.mulp  s7    ggajS!Q771a-sC899r   c           	      Z   t        |t              r|dk  r/t        | j                  | j                  | j
                        | }}n| j                  }| j                  t        t        ||| j
                        | j                  | j
                              S t        dt        |      z        )rJ  r   rK  )r   rL  r~   r   r  r   r!   re   r^   rM  r   )r   r   r   s      r   rO  zANP.powt  s~    a1u!!%%61EE55Aquu!5quuaeeDEE6a@AAr   c                     | j                  |      \  }}}}} |t        t        |t        |||      |      ||            | j	                  ||      fS r   )r   re   r[   r~   r   r  s          r   r]  zANP.div  sU    ggajS!QGGAz!S#'>Dc3OPRSRXRXY\^aRbccr   c                     | j                  |      \  }}}}}t        |||      \  }}||j                  gk(  r| j                  ||      S t	        d      )Nzzero divisor)r   r|   r   r   r   )r   r&   r   r  r   r  rv  r  s           r   r_  zANP.rem  sU    GGAJQ1cac*1	>66#s##//r   c                     | j                  |      \  }}}}} |t        t        |t        |||      |      ||            S r   )r   re   r[   r~   r  s          r   ra  zANP.quo  s@    ggajS!Q771jC&=sCS#NOOr   c                 B    t        | j                  | j                        S r  )r9   r   r   r   s    r   r  zANP.LC      aeeQUU##r   c                 B    t        | j                  | j                        S r  )r;   r   r   r   s    r   r  zANP.TC  r  r   c                     |  S )z6Returns ``True`` if ``f`` is a zero algebraic number. r    r   s    r   r~  zANP.is_zero  s     ur   c                 J    | j                   | j                  j                  gk(  S )z6Returns ``True`` if ``f`` is a unit algebraic number. )r   r   r   r   s    r   r  z
ANP.is_one  s     uu##r   c                 P    | j                    xs t        | j                         dk(  S )r  r   )r   r!  r   s    r   r  zANP.is_ground  s!     55y+CJ!O+r   c                     | S r   r    r   s    r   __pos__zANP.__pos__  s    r   c                 "    | j                         S r   r'  r   s    r   r(  zANP.__neg__  r%  r   c                     t        |t              r| j                  |      S 	 | j                  | j                  |            S # t        t
        f$ r	 t        cY S w xY wr   )r   r  rA  r!   r   rM  r*  r,  s     r   r-  zANP.__add__  N    a558O&uuQUU1X&"I. &%%&   A AAc                 $    | j                  |      S r   r1  r,  s     r   r2  zANP.__radd__  r3  r   c                     t        |t              r| j                  |      S 	 | j                  | j                  |            S # t        t
        f$ r	 t        cY S w xY wr   )r   r  rD  r!   r   rM  r*  r,  s     r   r5  zANP.__sub__  r  r  c                 &    |  j                  |      S r   r1  r,  s     r   r7  zANP.__rsub__  r8  r   c                     t        |t              r| j                  |      S 	 | j                  | j                  |            S # t        t
        f$ r	 t        cY S w xY wr   )r   r  rF  r!   r   rM  r*  r,  s     r   r:  zANP.__mul__  r  r  c                 $    | j                  |      S r   rA  r,  s     r   rB  zANP.__rmul__  r3  r   c                 $    | j                  |      S r   rD  rN  s     r   rE  zANP.__pow__  rF  r   c                 $    | j                  |      S r   rH  r,  s     r   rI  zANP.__divmod__  rF  r   c                 $    | j                  |      S r   rK  r,  s     r   rL  zANP.__mod__  rF  r   c                     t        |t              r| j                  |      S 	 | j                  | j                  |            S # t        t
        f$ r	 t        cY S w xY wr   )r   r  ra  r!   r   rM  r*  r,  s     r   r=  zANP.__truediv__  r  r  c                 Z    	 | j                  |      \  }}}}}||k(  S # t        $ r Y yw xY wrP  r   r   rQ  s        r   rR  z
ANP.__eq__  s9    	GGAJMAq!Q6M  		    	**c                 Z    	 | j                  |      \  }}}}}||k7  S # t        $ r Y yw xY wr  r   rQ  s        r   rT  z
ANP.__ne__  s9    	GGAJMAq!Q6M  		r  c                 :    | j                  |      \  }}}}}||k  S r   r^  rQ  s        r   r_  z
ANP.__lt__  r`  r   c                 :    | j                  |      \  }}}}}||k  S r   r^  rQ  s        r   rb  z
ANP.__le__  rc  r   c                 :    | j                  |      \  }}}}}||kD  S r   r^  rQ  s        r   re  z
ANP.__gt__  r`  r   c                 :    | j                  |      \  }}}}}||k\  S r   r^  rQ  s        r   rg  z
ANP.__ge__  rc  r   c                 ,    t        | j                        S r   )boolr   r   s    r   ri  zANP.__bool__  s    AEE{r   rk  )5r)   r*   r+   r,   rn  r   r   r   r   r!   r-   r   r   r   r   r   r   r   r   r>  rA  rD  rF  rO  r]  r_  ra  rg  r  r  ro  r~  r  r  r  r(  r-  r2  r5  r7  r:  rB  rE  rI  rL  r=  rR  rT  r_  rb  re  rg  ri  r    r   r   r  r    sS   ;%I,NY#24        ,4& E E,'':
Bd0P E$$   $ $ , ,&&&&r   r  N)r,   sympy.core.numbersr   sympy.core.sympifyr   re  r   r   r   sympy.polys.polyutilsr   r
   sympy.polys.densebasicr.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   sympy.polys.densearithrO   rP   rQ   rR   rS   rT   rU   rV   rW   rX   rY   rZ   r[   r\   r]   r^   r_   r`   ra   rb   rc   rd   re   rf   rg   rh   ri   rj   rk   rl   rm   sympy.polys.densetoolsrn   ro   rp   rq   rr   rs   rt   ru   rv   rw   rx   ry   rz   r{   sympy.polys.euclidtoolsr|   r}   r~   r   r   r   r   r   r   r   sympy.polys.sqfreetoolsr   r   r   r   r   r   r   sympy.polys.factortoolsr   r   r   r   sympy.polys.rootisolationr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rt  rq  r  r  r    r   r   <module>r     s   7 " * O O 4$ 6        .        4      ( ( (. .$ $ $ $
4h,
k h,V4
u,
k u,p*
I
k Ir   