o
    ohO                     @   s`  d Z ddlZddlmZ ddlmZ ddlmZmZm	Z	m
Z
mZ ddlmZmZmZmZ ddlmZmZmZmZmZmZmZmZ ddlmZmZmZmZ dd	lm Z! dd
l"m#Z# e!j$Z$e!j%Z%dd Z&dd Z'd3ddZ(dd Z)dd Z*dd Z+dd Z,dd Z-dd Z.dd Z/d d! Z0d"d# Z1d$d% Z2d&d' Z3d4d(d)Z4d*d+ Z5d,d- Z6d5d/d0Z7d6d1d2Z8dS )7a  
Algorithms for solving Parametric Risch Differential Equations.

The methods used for solving Parametric Risch Differential Equations parallel
those for solving Risch Differential Equations.  See the outline in the
docstring of rde.py for more information.

The Parametric Risch Differential Equation problem is, given f, g1, ..., gm in
K(t), to determine if there exist y in K(t) and c1, ..., cm in Const(K) such
that Dy + f*y == Sum(ci*gi, (i, 1, m)), and to find such y and ci if they exist.

For the algorithms here G is a list of tuples of factions of the terms on the
right hand side of the equation (i.e., gi in k(t)), and Q is a list of terms on
the right hand side of the equation (i.e., qi in k[t]).  See the docstring of
each function for more information.
    N)reduce)ilcm)DummyAddMulPowS)order_atorder_at_ooweak_normalizerbound_degree)gcdex_diophantinefrac_in
derivationresidue_reducesplitfactorresidue_reduce_derivationDecrementLevelrecognize_log_derivative)Polylcmcancelsqf_list)
PolyMatrix)solvec                    s   t ||\}}tt| \}}tdd |td|j}t ||\}	}
||	}|	|	|j|||j}|| }||  ||  |t	|| |  }|j
|dd\}} fdd|D }|||f||fS )a  
    Parametric Risch Differential Equation - Normal part of the denominator.

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

    Given a derivation D on k[t] and f, g1, ..., gm in k(t) with f weakly
    normalized with respect to t, return the tuple (a, b, G, h) such that
    a, h in k[t], b in k<t>, G = [g1, ..., gm] in k(t)^m, and for any solution
    c1, ..., cm in Const(k) and y in k(t) of Dy + f*y == Sum(ci*gi, (i, 1, m)),
    q == y*h in k<t> satisfies a*Dq + b*q == Sum(ci*Gi, (i, 1, m)).
    c                 S   
   |  |S Nr   ij r!   h/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/sympy/integrals/prde.py<lambda>2      
 z#prde_normal_denom.<locals>.<lambda>   Tincludec                    "   g | ]\}} | j |d dqS Tr&   r   ).0ADcr!   r"   
<listcomp>>      " z%prde_normal_denom.<locals>.<listcomp>)r   listzipr   r   tgcddiffquor   r   )fafdGDEdndsGasGdsgdenesphababdr!   r.   r"   prde_normal_denom#   s   
&rH   c                 C   s   | | }|  | } dd | D }dd | D }tdd |D }tdd |D }dd |  D }dd |  D }td	d |D }	td
d |D }
|	| |
|   ||
| |	|   |f} || ||   |}| d | d |fS )a  
    Helper function, to get the real and imaginary part of a rational function
    evaluated at sqrt(-1) without actually evaluating it at sqrt(-1).

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

    Separates the even and odd power terms by checking the degree of terms wrt
    mod 4. Returns a tuple (ba[0], ba[1], bd) where ba[0] is real part
    of the numerator ba[1] is the imaginary part and bd is the denominator
    of the rational function.
    c                 S   >   g | ]\}}|d  d d kr|n|d  d dkr| nd qS r         r!   r+   keyvaluer!   r!   r"   r0   Q      > zreal_imag.<locals>.<listcomp>c                 S   >   g | ]\}}|d  d dkr|n|d  d dkr| nd qS r   rK   r%      r!   rM   r!   r!   r"   r0   R   rP   c                 s       | ]}|V  qd S r   r!   r+   rr!   r!   r"   	<genexpr>S       zreal_imag.<locals>.<genexpr>c                 s   rT   r   r!   rU   r!   r!   r"   rW   T   rX   c                 S   rI   rJ   r!   rM   r!   r!   r"   r0   U   rP   c                 S   rQ   rR   r!   rM   r!   r!   r"   r0   V   rP   c                 s   rT   r   r!   rU   r!   r!   r"   rW   W   rX   c                 s   rT   r   r!   rU   r!   r!   r"   rW   X   rX   r   r%   )as_polyas_dictitemssum)rF   rG   gen
denom_real
denom_imagbd_realbd_imagnum_realnum_imagba_realba_imagr!   r!   r"   	real_imagB   s   ,rf   autoc                    s  |dkr j }|dkrt j jn)|dkr#t jd d  jn|dv r6||}| ||td jfS td| t| jt| j }t fdd	|D }td
|td
| }	|s6|dkr jt j j}
t @ t	|
d
 |
d
 | 
d
  j\}}t	|
 j\}}t|||| }|dur|\}}}|dkrt|	|}	W d   n1 sw   Y  nw|dkr6 jt jd d  j}
t V t|||   j\}}}|}t	|
 j\}}ttd j| | r&t|||| }t|||| }|dur&|dur&|\}}}|dkr&t|	|d }	W d   n	1 s1w   Y  td
| }| |	  |  }|| t|	 j|  t    }fdd|D }}||||fS )ab  
    Parametric Risch Differential Equation - Special part of the denominator.

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

    Case is one of {'exp', 'tan', 'primitive'} for the hyperexponential,
    hypertangent, and primitive cases, respectively.  For the hyperexponential
    (resp. hypertangent) case, given a derivation D on k[t] and a in k[t],
    b in k<t>, and g1, ..., gm in k(t) with Dt/t in k (resp. Dt/(t**2 + 1) in
    k, sqrt(-1) not in k), a != 0, and gcd(a, t) == 1 (resp.
    gcd(a, t**2 + 1) == 1), return the tuple (A, B, GG, h) such that A, B, h in
    k[t], GG = [gg1, ..., ggm] in k(t)^m, and for any solution c1, ..., cm in
    Const(k) and q in k<t> of a*Dq + b*q == Sum(ci*gi, (i, 1, m)), r == q*h in
    k[t] satisfies A*Dr + B*r == Sum(ci*ggi, (i, 1, m)).

    For case == 'primitive', k<t> == k[t], so it returns (a, b, G, 1) in this
    case.
    rg   exptanrL   r%   )	primitivebasez@case must be one of {'exp', 'tan', 'primitive', 'base'}, not %s.c                 3   s0    | ]\}}t | jt | j V  qd S r   )r	   r4   r+   GaGd)r;   rC   r!   r"   rW      s   . z%prde_special_denom.<locals>.<genexpr>r   Nc                    s&   g | ]\}}|   j |d dqS r)   r*   rl   )pNpnr!   r"   r0         & z&prde_special_denom.<locals>.<listcomp>)caser   r4   r7   
ValueErrorr	   mindr   r   evalparametric_log_derivrf   r   maxr   )rE   rF   rG   r:   r;   rr   Bnbncndcoeffalphaaalphadetaaetadr,   QmzbetaabetadsNrD   r!   )r;   rC   ro   rp   r"   prde_special_denom^   sh   

,







2r   c                    s   t |}tt| \}}tdd |tddfdd|D tdd D sEt fd	dD }t|d
 |fdd j}ntd|g  j}tt \}	}
|	|fS )a  
    Parametric Risch Differential Equation - Generate linear constraints on the constants.

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

    Given a derivation D on k[t], a, b, in k[t] with gcd(a, b) == 1, and
    G = [g1, ..., gm] in k(t)^m, return Q = [q1, ..., qm] in k[t]^m and a
    matrix M with entries in k(t) such that for any solution c1, ..., cm in
    Const(k) and p in k[t] of a*Dp + b*p == Sum(ci*gi, (i, 1, m)),
    (c1, ..., cm) is a solution of Mx == 0, and p and the ci satisfy
    a*Dp + b*p == Sum(ci*qi, (i, 1, m)).

    Because M has entries in k(t), and because Matrix does not play well with
    Poly, M will be a Matrix of Basic expressions.
    c                 S   r   r   r   r   r!   r!   r"   r#      r$   z)prde_linear_constraints.<locals>.<lambda>Tfieldc                    s$   g | ]\}}|  |  qS r!   )r7   divr+   gar@   ru   r!   r"   r0         $ z+prde_linear_constraints.<locals>.<listcomp>c                 s   s    | ]\}}|j V  qd S r   is_zeror+   _rir!   r!   r"   rW      s    z*prde_linear_constraints.<locals>.<genexpr>c                 3   s     | ]\}}|  jV  qd S r   degreer4   r   r;   r!   r"   rW          r%   c                    s    | d  | S Nr%   nthr   r   r!   r"   r#          r   )	lenr2   r3   r   r   allrx   Matrixr4   )rE   br:   r;   r   Gnsr?   r   Mqsr   r!   )r;   r   ru   r"   prde_linear_constraints   s   r   c                    s   t | }t fdd| D  \}tdd D s5tdd D }t|d |fdd j}||fS td	|g  j}||fS )
aB  
    Given p = [p1, ..., pm] in k[t]^m and d in k[t], return
    q = [q1, ..., qm] in k[t]^m and a matrix M with entries in k such
    that Sum(ci*pi, (i, 1, m)), for c1, ..., cm in k, is divisible
    by d if and only if (c1, ..., cm) is a solution of Mx = 0, in
    which case the quotient is Sum(ci*qi, (i, 1, m)).
    c                       g | ]}|  qS r!   )r   )r+   pir   r!   r"   r0          z+poly_linear_constraints.<locals>.<listcomp>c                 s       | ]}|j V  qd S r   r   r+   r   r!   r!   r"   rW          z*poly_linear_constraints.<locals>.<genexpr>c                 s   s    | ]}|  V  qd S r   )r   r   r!   r!   r"   rW      s    r%   c                        |  | S r   r   r   )rV   r!   r"   r#          z)poly_linear_constraints.<locals>.<lambda>r   )r   r3   r   rx   r   gens)rC   ru   r   qr|   r   r!   )ru   rV   r"   poly_linear_constraints   s   r   c                    s  | s| |fS |  |}| \}}|ddddf |dddf } }fdd tt| jt| jD ]T\}}| ||f jjj	 r| |ddf } | ||f |
 fdd} ||  }	| dd|f }
| |
|  } ||
|	  }| |} |t|	g|j}q6| |fS )a  
    Generate a system for the constant solutions.

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

    Given a differential field (K, D) with constant field C = Const(K), a Matrix
    A, and a vector (Matrix) u with coefficients in K, returns the tuple
    (B, v, s), where B is a Matrix with coefficients in C and v is a vector
    (Matrix) such that either v has coefficients in C, in which case s is True
    and the solutions in C of Ax == u are exactly all the solutions of Bx == v,
    or v has a non-constant coefficient, in which case s is False Ax == u has no
    constant solution.

    This algorithm is used both in solving parametric problems and in
    determining if an element a of K is a derivative of an element of K or the
    logarithmic derivative of a K-radical using the structure theorem approach.

    Because Poly does not play well with Matrix yet, this algorithm assumes that
    all matrix entries are Basic expressions.
    Nc                    s   t |  ddS )NTbasicr   xr   r!   r"   r#     r   z!constant_system.<locals>.<lambda>c                    s    |  S r   r!   r   )r-   DAijr!   r"   r#     s    )row_joinrref	itertoolsproductrangecolsrowsexprhasT	applyfunccol_joinr   r   )r,   ur;   Aur   r    r   RiRm1um1Ajr!   )r-   r   r;   r"   constant_system   s&   
& 
r   c                    sp   t tfdd|D  \}}}t  } fddt||D }	t |}| j }
|||	||
fS )a(  
    Special Polynomial Differential Equation algorithm: Parametric Version.

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

    Given a derivation D on k[t], an integer n, and a, b, q1, ..., qm in k[t]
    with deg(a) > 0 and gcd(a, b) == 1, return (A, B, Q, R, n1), with
    Qq = [q1, ..., qm] and R = [r1, ..., rm], such that for any solution
    c1, ..., cm in Const(k) and q in k[t] of degree at most n of
    a*Dq + b*q == Sum(ci*gi, (i, 1, m)), p = (q - Sum(ci*ri, (i, 1, m)))/a has
    degree at most n1 and satisfies A*Dp + B*p == Sum(ci*qi, (i, 1, m))
    c                    s   g | ]}t  |qS r!   )r   r+   qi)rE   r   r!   r"   r0   7      zprde_spde.<locals>.<listcomp>c                    s   g | ]\}}|t |  qS r!   r   )r+   r   zir   r!   r"   r0   ;      )r2   r3   r   r   r4   )rE   r   r   r|   r;   RZr,   ry   Qqn1r!   )r;   rE   r   r"   	prde_spde)  s    r   c                    sF  |   j}t}td jg| }tt|ddt|D ]3\}}| || |   }	t|	 j|   j}
|| |
 ||< | t	|
  | |
  |< qt
dd D r_d}nt fddD }t|d |fdd j}t|t|d d j \}}t| j}|t|j| j|| }||fS )	aB  
    Parametric Poly Risch Differential Equation - No cancellation: deg(b) large enough.

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

    Given a derivation D on k[t], n in ZZ, and b, q1, ..., qm in k[t] with
    b != 0 and either D == d/dt or deg(b) > max(0, deg(D) - 1), returns
    h1, ..., hr in k[t] and a matrix A with coefficients in Const(k) such that
    if c1, ..., cm in Const(k) and q in k[t] satisfy deg(q) <= n and
    Dq + b*q == Sum(ci*qi, (i, 1, m)), then q = Sum(dj*hj, (j, 1, r)), where
    d1, ..., dr in Const(k) and A*Matrix([[c1, ..., cm, d1, ..., dr]]).T == 0.
    r   r   c                 s   r   r   r   r   r!   r!   r"   rW   Z  r   z)prde_no_cancel_b_large.<locals>.<genexpr>c                 3       | ]	}|  jV  qd S r   r   r   r   r!   r"   rW   ]      r%   c                    r   r   r   r   r   r!   r"   r#   ^  r   z(prde_no_cancel_b_large.<locals>.<lambda>)r   r4   r   r   r   r   r   r   LCr   r   rx   r   r   zeroseyer   r   r   )r   r   r|   r;   dbr   Hr   r   sisitndcr   r,   r   r/   r!   )r;   r   r"   prde_no_cancel_b_largeB  s      $r   c                    sh  t }td jg| }tt|ddt|D ]=\}}| | j j d | j	   }t| j|   j}	|| |	 ||< | t
|	  | |	  |< q|  jdkrt|D ],}t| |  j| 	   j}|| | ||< | t
|  | |  |< qctdd D rd}
nt fddD }
t|
d |fdd j}t|t|
d d j \}}t| j}|t|j| j|| }||fS  j jd	kr.t &  jt| d
d\}}fddD }t||| \}}W d   n	1 sw   Y  fdd|D }t| }ntdd
dg}tdd D tjg g j}t fddD }|dkrpt||fdd j}t|t|d j \}}ntd|g  j}t |}t| j}|t|j||  j}|t|j| j}|t|| j| }|| |||fS )a=  
    Parametric Poly Risch Differential Equation - No cancellation: deg(b) small enough.

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

    Given a derivation D on k[t], n in ZZ, and b, q1, ..., qm in k[t] with
    deg(b) < deg(D) - 1 and either D == d/dt or deg(D) >= 2, returns
    h1, ..., hr in k[t] and a matrix A with coefficients in Const(k) such that
    if c1, ..., cm in Const(k) and q in k[t] satisfy deg(q) <= n and
    Dq + b*q == Sum(ci*qi, (i, 1, m)) then q = Sum(dj*hj, (j, 1, r)) where
    d1, ..., dr in Const(k) and A*Matrix([[c1, ..., cm, d1, ..., dr]]).T == 0.
    r   r   r%   c                 s   r   r   r   r   r!   r!   r"   rW     r   z)prde_no_cancel_b_small.<locals>.<genexpr>c                 3   r   r   r   r   r   r!   r"   rW     r   c                    r   r   r   r   r   r!   r"   r#     r   z(prde_no_cancel_b_small.<locals>.<lambda>rk   Tr   c                    s   g | ]}t |  d dqS Tr   )r   TCr   )t0r!   r"   r0     r   z*prde_no_cancel_b_small.<locals>.<listcomp>Nc                    s*   g | ]\}}t | |   d dqS r   )r   as_exprr+   r8   r9   )r4   r!   r"   r0     s    c                 S   s   g | ]}|  qS r!   )r   r   r!   r!   r"   r0         c                 3   r   r   r   r   r   r!   r"   rW     r   c                    s    |  | d S r   r   r   r   r!   r"   r#     r   )r   r   r4   r   r   r   r   ru   r   r   r   r   rx   r   r   r   r   r   r   r   rr   r   r   param_rischDEfrom_Matrix	to_Matrixr   Zero)r   r   r|   r;   r   r   r   r   r   r   r   r   r,   r   r/   rF   rG   Q0fry   ru   r   rV   ICr!   )r;   r   r4   r   r"   prde_no_cancel_b_smallf  sZ    . & $

 

r   c              	      s  g } j dkr%t  t|  jdd\}}W d   n1 s w   Y  t|ddD ] j dkr_t ! t| t j  j  | j  jdd\}}W d   n1 sZw   Y  t   fdd|D }t||| \}}	W d   n1 sw   Y   fd	d|D }|		 j}	t
|}
|kr|	}n|	|t|j|
 j}dg|
 dg|
 }}t|
D ]!}||  j || j }|||< t| | |   ||< q||7 }|| }q+||fS )
z
    Pg, 237.
    rj   Tr   Nr   rh   c                    s"   g | ]}t | jd dqS r   )r   r   r4   )r+   r   r;   r   r!   r"   r0     r1   z+prde_cancel_liouvillian.<locals>.<listcomp>c                    s,   g | ]\}}t | |   jd dqS r   )r   r   r4   r   r   r!   r"   r0     s     )rr   r   r   r4   r   r   rY   r   r   set_gensr   r   r   r   r   )r   r   r|   r;   r   rF   rG   QyfiAir   r   Fihir    hjir!   r   r"   prde_cancel_liouvillian  s@   



"


r   c                    s(  t }|dk rEtdd D rg td| jfS t fddD }t|d |fdd j}t|t|jd j \}}	g |fS jrψ	 |
fdd	D }|jsx jd
ksq| td j d krxt|| S |js|  j d k r jd
ks j dkrt|| S  j dkr|  j d kr|| 	   j 	  krtd jdv rt|| S tdjjg| }
|dkr	t|| \}}}fdd	t|
|D }
9 |}|jsn|dkst|\}}t|t|jd j \}}	| }|s-g t| jfS t|gfdd	|D }t|
gfdd	|D }t||||| \}}|fdd	|D  }t| j }|D ]}||}qm|t|t | j}|t|j| j|}||fS )a5  Polynomial solutions of a parametric Risch differential equation.

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

    Given a derivation D in k[t], a, b in k[t] relatively prime, and q
    = [q1, ..., qm] in k[t]^m, return h = [h1, ..., hr] in k[t]^r and
    a matrix A with m + r columns and entries in Const(k) such that
    a*Dp + b*p = Sum(ci*qi, (i, 1, m)) has a solution p of degree <= n
    in k[t] with c1, ..., cm in Const(k) if and only if p = Sum(dj*hj,
    (j, 1, r)) where d1, ..., dr are in Const(k) and (c1, ..., cm,
    d1, ..., dr) is a solution of Ax == 0.
    r   c                 s   r   r   r   r   r!   r!   r"   rW     r   z%param_poly_rischDE.<locals>.<genexpr>r%   c                 3   r   r   r   r   r   r!   r"   rW     r   c                    r   r   r   r   r   r!   r"   r#     r   z$param_poly_rischDE.<locals>.<lambda>c                    r   r!   )
quo_groundr   )rE   r!   r"   r0     r   z&param_poly_rischDE.<locals>.<listcomp>rk   rL   z0prde_no_cancel_b_equal() is not yet implemented.)rj   rh   z?non-linear and hypertangent cases have not yet been implementedc                    s   g | ]
\}}| |  qS r!   r!   )r+   betair   alphar!   r"   r0   7      c                       g | ]} | d  qS r   r!   r+   vj)Mqqr!   r"   r0   `  r   c                    r   r   r!   r  )Mbetar!   r"   r0   g  r   c                    s   g | ]} | qS r!   r!   )r+   gkr   r!   r"   r0   w  r   ) r   r   r   r4   rx   r   r   r   	is_groundr   r   r   rr   r   ru   r   r   rY   NotImplementedErrorr   onezeror   r3   r5   r   	nullspacer   param_poly_rischDEr7   r   r   )rE   r   r   r|   r;   r   r   r   r,   r   betarV   ru   qqVr   gry   rD   r  r!   )r;   r  r  rE   r   r   r"   r    sd    




 r  c                    s4  t |t| ||\\} }fdd|D }t| |||\}\}}}}|9 t|||||\}}	}}
|
9 ||	||	fdd|D }}t|||\}t|t|j	d|j
|\}}| }|syg t|j
fS tg  fdd|D }zt||||dd}W n ty   d}Y nw t|||||\}}	t|j
 }|D ]}||}q|tt ||j
}|t|	j	|j
|	}| }t |t | f}fd	d|D }d
d |D }tg |||j
R  }| }tdd |D |j
}fdd|D |fS )ad  
    Solve a Parametric Risch Differential Equation: Dy + f*y == Sum(ci*Gi, (i, 1, m)).

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

    Given a derivation D in k(t), f in k(t), and G
    = [G1, ..., Gm] in k(t)^m, return h = [h1, ..., hr] in k(t)^r and
    a matrix A with m + r columns and entries in Const(k) such that
    Dy + f*y = Sum(ci*Gi, (i, 1, m)) has a solution y
    in k(t) with c1, ..., cm in Const(k) if and only if y = Sum(dj*hj,
    (j, 1, r)) where d1, ..., dr are in Const(k) and (c1, ..., cm,
    d1, ..., dr) is a solution of Ax == 0.

    Elements of k(t) are tuples (a, d) with a and d in k[t].
    c                    r(   r)   r*   r   r   r!   r"   r0     r1   z!param_rischDE.<locals>.<listcomp>c                    s"   g | ]\}}|j |  d dqS r)   r*   )r+   giagid)r  r!   r"   r0     s    r%   c                    r   r   r!   r  )Mqr!   r"   r0     r   T)
parametric   c                    s&   g | ]}|d   | d   qS r   r!   )r+   wl)r   vr!   r"   r0     rq   c                 S   s   g | ]	}|D ]}|qqS r!   r!   )r+   rower!   r!   r"   r0         c                 S   s   g | ]}|d d  qS r   r!   )r+   nir!   r!   r"   r0     r   c                    s   g | ]	}|j  d dqS r)   r*   )r+   hk)gammar!   r"   r0     r  )r   r   rH   r   r5   r7   r   r   r   r   r4   r
  r   r   r   r  r  r   r   )r8   r9   r:   r;   rE   rF   rG   hnr,   ry   hsr   r   r   r  rV   r|   rD   r  Wshapeelementsr[   r   r   r!   )r  r  r  r   r   r  r"   r     sN   
	

r   c                    s>  t | \}} fdd|D }tt| \}}tdd |f| }	|	|	 jt  }
d} jdv rtdd |f| }| |
t|  	|8 }
t
t| | jt
 fdd	|D } j| j tdd
 j j |  }ntfdd|D }|
| |  j|dd|fS )a  
    Simpler version of step 1 & 2 for the limited integration problem.

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

    Given a derivation D on k(t) and f, g1, ..., gn in k(t), return
    (a, b, h, N, g, V) such that a, b, h in k[t], N is a non-negative integer,
    g in k(t), V == [v1, ..., vm] in k(t)^m, and for any solution v in k(t),
    c1, ..., cm in C of f == Dv + Sum(ci*wi, (i, 1, m)), p = v*h is in k<t>, and
    p and the ci satisfy a*Dp + b*p == g + Sum(ci*vi, (i, 1, m)).  Furthermore,
    if S1irr == Sirr, then p is in k[t], and if t is nonlinear or Liouvillian
    over k, then deg(p) <= N.

    So that the special part is always computed, this function calls the more
    general prde_special_denom() automatically if it cannot determine that
    S1irr == Sirr.  Furthermore, it will automatically call bound_degree() when
    t is linear and non-Liouvillian, which for the transcendental case, implies
    that Dt == a*t + b with for some a, b in k*.
    c                    s   g | ]	\}}t | qS r!   )r   )r+   r   r@   r   r!   r"   r0   !  r  z,limited_integrate_reduce.<locals>.<listcomp>c                 S   r   r   r   r   r!   r!   r"   r#   #  r$   z*limited_integrate_reduce.<locals>.<lambda>r   )rk   rj   rh   ri   c                 S   r   r   r   r   r!   r!   r"   r#   ,  r$   c                 3   s"    | ]\}}t || jV  qd S r   )r
   r4   r   r   r!   r"   rW   /  s    z+limited_integrate_reduce.<locals>.<genexpr>r%   c                    s(   g | ]\}}   | j |d dqS r)   r*   r   )rE   r  r!   r"   r0   9  s   ( Tr&   )r   r2   r3   r   r5   r6   r4   r   rr   r7   rt   r
   r   rx   ru   r  r   )r8   r9   r:   r;   r<   r=   EEnEsr/   r   r   r  mur  r!   )r;   rE   r  r"   limited_integrate_reduce  s&   

6"r&  c                    s,  | t d|  |j | } }t d|j}t d|j}| |fg| }t||||\ }| }dd |D }|s<dS |d d }|d |  t }	t|	 d tdd  }
t fddt	|	D  }|
 \}}t ||jt ||j}}|t d|  |j | f}||
fS )zQ
    Solves the limited integration problem:  f = Dv + Sum(ci*wi, (i, 1, n))
    r%   r   c                 S   s   g | ]
}|d  d kr|qS r   r!   )r+   r  r!   r!   r"   r0   I  r   z%limited_integrate.<locals>.<listcomp>Nc                 3   s@    | ]}d  |   | d     | d     V  qdS )r%   r   N)r   r+   r   rD   r   r  r!   r"   rW   T  s    8z$limited_integrate.<locals>.<genexpr>)r   r   r4   monicr   r
  r   r2   r\   r   as_numer_denom)r8   r9   r:   r;   FaFdr,   r  c0rV   r   yy_numy_denYaYdYr!   r(  r"   limited_integrate=  s*   "
 r4  c                    s   pt d | |\}||\}tdt|j||jd }t|j|j}	|j|kr fddt|d |	d D }
t|
 }|rV|  jsXdS |  	 \}}|
j}|
j}||  | || |  }|| }t|||d}|du rdS |\}}|js|jrdS || || |fS |j|krdS t|
|j |
|j }| | t||j }t||\}}||||j }||jsdS | || |\}||| |\} fddt||jD }
t|
 }|r|  jsdS |  	 \}}|
|j|  | |
|j| |  }|| }t|||}|du rHdS |\}}|jsT|jrVdS || || |fS )	aH  
    Parametric logarithmic derivative heuristic.

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

    Given a derivation D on k[t], f in k(t), and a hyperexponential monomial
    theta over k(t), raises either NotImplementedError, in which case the
    heuristic failed, or returns None, in which case it has proven that no
    solution exists, or returns a solution (n, m, v) of the equation
    n*f == Dv/v + m*Dtheta/theta, with v in k(t)* and n, m in ZZ with n != 0.

    If this heuristic fails, the structure theorem approach will need to be
    used.

    The argument w == Dtheta/theta
    c1r   r%   c                    $   g | ]} |  |  qS r!   r   r'  )r5  rC   r   r!   r"   r0   x  r   z,parametric_log_deriv_heu.<locals>.<listcomp>Nrg   c                    r6  r!   r   r'  )r5  r1r2r!   r"   r0     r   )r   r   rx   r   r4   r   r   r   is_Rationalr*  rY   r   !is_log_deriv_k_t_radical_in_fieldr   r   r   r)  r   r   r5   r6   r   r7   )r8   r9   wawdr;   r5  rE   r   ry   r   eqsr   r   r   M_polyN_polynfmwanfmwdQvr   r  r/   llnlsr   u1u2r!   )r5  rC   r   r7  r8  r"   parametric_log_deriv_heu\  s\   $
""
(
rH  c                 C   s   t | ||||}|S r   )rH  )r8   r9   r;  r<  r;   r,   r!   r!   r"   rw     s   rw   c                    sv  |t |   | t |   ||  }}|j|dd\}}t jt jkrEdd  jD s=dd  jD t d rAtdtd	 fd
d dD } fdd dD }t	 }t
|| g|}t
| |  g|}	t||	 \}
}| }|
rt fdd|D sdS tdd |D std fdd dD  fdd dD  }tt||}tdd |D  } fdd dD  fdd dD  }g }g }t||D ]>\}| \}}t|\}}|tt|gfdd|D    t|\}}|tt|gfdd|D    qt|  |  t|  t|  }|||fS )a-  
    Checks if Df/f is the derivative of an element of k(t).

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

    a in k(t) is the derivative of an element of k(t) if there exists b in k(t)
    such that a = Db.  Either returns (ans, u), such that Df/f == Du, or None,
    which means that Df/f is not the derivative of an element of k(t).  ans is
    a list of tuples such that Add(*[i*j for i, j in ans]) == u.  This is useful
    for seeing exactly which elements of k(t) produce u.

    This function uses the structure theorem approach, which says that for any
    f in K, Df/f is the derivative of a element of K if and only if there are ri
    in QQ such that::

            ---               ---       Dt
            \    r  * Dt   +  \    r  *   i      Df
            /     i     i     /     i   ---   =  --.
            ---               ---        t        f
         i in L            i in E         i
               K/C(x)            K/C(x)


    Where C = Const(K), L_K/C(x) = { i in {1, ..., n} such that t_i is
    transcendental over C(x)(t_1, ..., t_i-1) and Dt_i = Da_i/a_i, for some a_i
    in C(x)(t_1, ..., t_i-1)* } (i.e., the set of all indices of logarithmic
    monomials of K over C(x)), and E_K/C(x) = { i in {1, ..., n} such that t_i
    is transcendental over C(x)(t_1, ..., t_i-1) and Dt_i/t_i = Da_i, for some
    a_i in C(x)(t_1, ..., t_i-1) } (i.e., the set of all indices of
    hyperexponential monomials of K over C(x)).  If K is an elementary extension
    over C(x), then the cardinality of L_K/C(x) U E_K/C(x) is exactly the
    transcendence degree of K over C(x).  Furthermore, because Const_D(K) ==
    Const_D(C(x)) == C, deg(Dt_i) == 1 when t_i is in E_K/C(x) and
    deg(Dt_i) == 0 when t_i is in L_K/C(x), implying in particular that E_K/C(x)
    and L_K/C(x) are disjoint.

    The sets L_K/C(x) and E_K/C(x) must, by their nature, be computed
    recursively using this same function.  Therefore, it is required to pass
    them as indices to D (or T).  E_args are the arguments of the
    hyperexponentials indexed by E_K (i.e., if i is in E_K, then T[i] ==
    exp(E_args[i])).  This is needed to compute the final answer u such that
    Df/f == Du.

    log(f) will be the same as u up to a additive constant.  This is because
    they will both behave the same as monomials. For example, both log(x) and
    log(2*x) == log(x) + log(2) satisfy Dt == 1/x, because log(2) is constant.
    Therefore, the term const is returned.  const is such that
    log(const) + f == u.  This is calculated by dividing the arguments of one
    logarithm from the other.  Therefore, it is necessary to pass the arguments
    of the logarithmic terms in L_args.

    To handle the case where we are given Df/f, not f, use is_deriv_k_in_field().

    See also
    ========
    is_log_deriv_k_t_radical_in_field, is_log_deriv_k_t_radical

    Tr&   c                 S      g | ]}|d kr|qS ri   r!   r'  r!   r!   r"   r0     r   zis_deriv_k.<locals>.<listcomp>c                 S      h | ]}|d kr|qS rj   r!   r'  r!   r!   r"   	<setcomp>  r   zis_deriv_k.<locals>.<setcomp>logXReal version of the structure theorems with hypertangent support is not yet implemented.ANonelementary extensions not supported in the structure theorems.c                    2   g | ]} j | t j|  j|  qS r!   r-   r7   r   r   r   r'  r   r!   r"   r0        2 rh   c                       g | ]	} j |  qS r!   r-   r   r'  r   r!   r"   r0     r  c                 3        | ]}t | d djV  qdS Tr   Nr   r   r'  r   r!   r"   rW     r   zis_deriv_k.<locals>.<genexpr>Nc                 s   r   r   r9  r'  r!   r!   r"   rW   #  r   8Cannot work with non-rational coefficients in this case.c                       g | ]} j | qS r!   extargsr'  r   r!   r"   r0   '  r   c                    r[  r!   r   r'  r   r!   r"   r0   (  r   c                 S      g | ]	\}}t ||qS r!   r   r+   r   r    r!   r!   r"   r0   *  r  c                    r[  r!   r^  r'  r   r!   r"   r0   +  r   c                    r[  r!   r\  r'  r   r!   r"   r0   ,  r   c                       g | ]\}}t ||  qS r!   r   r+   r   r  r    r!   r"   r0   5  r   c                    rb  r!   rc  rd  re  r!   r"   r0   7  r   )r   r   r   extsr-   casessetindicesr  r   r   r   r   r   r   r2   r3   r   r*  r   appendr   r   )r8   r9   r;   dfadfdE_partL_partdumlhsrhsr,   r   termsansresultargtermsrC  ldr   ru   icoeffitermsr}   dtermsconstr!   )r;   r    r"   
is_deriv_k  sN   &=(*$
r{  Tc                    s  |r|t |   | t |   j|d dd\}}n| |}}t jt jkrHdd  jD s@dd  jD t d rDtd	td
 fdd dD } fdd dD }t	 }t
|| g|}	t
| |  g|}
t|	|
 \}}| }|rt fdd|D sdS tdd |D stdtjttdd |D  |9 } fdd dD  fdd dD  }tt||}tdd |D  } fdd dD  fdd dD  }t|  |  tfddt||D   }|||fS )aE  
    Checks if Df is the logarithmic derivative of a k(t)-radical.

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

    b in k(t) can be written as the logarithmic derivative of a k(t) radical if
    there exist n in ZZ and u in k(t) with n, u != 0 such that n*b == Du/u.
    Either returns (ans, u, n, const) or None, which means that Df cannot be
    written as the logarithmic derivative of a k(t)-radical.  ans is a list of
    tuples such that Mul(*[i**j for i, j in ans]) == u.  This is useful for
    seeing exactly what elements of k(t) produce u.

    This function uses the structure theorem approach, which says that for any
    f in K, Df is the logarithmic derivative of a K-radical if and only if there
    are ri in QQ such that::

            ---               ---       Dt
            \    r  * Dt   +  \    r  *   i
            /     i     i     /     i   ---   =  Df.
            ---               ---        t
         i in L            i in E         i
               K/C(x)            K/C(x)


    Where C = Const(K), L_K/C(x) = { i in {1, ..., n} such that t_i is
    transcendental over C(x)(t_1, ..., t_i-1) and Dt_i = Da_i/a_i, for some a_i
    in C(x)(t_1, ..., t_i-1)* } (i.e., the set of all indices of logarithmic
    monomials of K over C(x)), and E_K/C(x) = { i in {1, ..., n} such that t_i
    is transcendental over C(x)(t_1, ..., t_i-1) and Dt_i/t_i = Da_i, for some
    a_i in C(x)(t_1, ..., t_i-1) } (i.e., the set of all indices of
    hyperexponential monomials of K over C(x)).  If K is an elementary extension
    over C(x), then the cardinality of L_K/C(x) U E_K/C(x) is exactly the
    transcendence degree of K over C(x).  Furthermore, because Const_D(K) ==
    Const_D(C(x)) == C, deg(Dt_i) == 1 when t_i is in E_K/C(x) and
    deg(Dt_i) == 0 when t_i is in L_K/C(x), implying in particular that E_K/C(x)
    and L_K/C(x) are disjoint.

    The sets L_K/C(x) and E_K/C(x) must, by their nature, be computed
    recursively using this same function.  Therefore, it is required to pass
    them as indices to D (or T).  L_args are the arguments of the logarithms
    indexed by L_K (i.e., if i is in L_K, then T[i] == log(L_args[i])).  This is
    needed to compute the final answer u such that n*f == Du/u.

    exp(f) will be the same as u up to a multiplicative constant.  This is
    because they will both behave the same as monomials.  For example, both
    exp(x) and exp(x + 1) == E*exp(x) satisfy Dt == t. Therefore, the term const
    is returned.  const is such that exp(const)*f == u.  This is calculated by
    subtracting the arguments of one exponential from the other.  Therefore, it
    is necessary to pass the arguments of the exponential terms in E_args.

    To handle the case where we are given Df, not f, use
    is_log_deriv_k_t_radical_in_field().

    See also
    ========

    is_log_deriv_k_t_radical_in_field, is_deriv_k

    rL   Tr&   c                 S   rI  rJ  r!   r'  r!   r!   r"   r0     r   z,is_log_deriv_k_t_radical.<locals>.<listcomp>c                 S   rK  rL  r!   r'  r!   r!   r"   rM    r   z+is_log_deriv_k_t_radical.<locals>.<setcomp>rN  rO  rP  c                    rQ  r!   rR  r'  r   r!   r"   r0     rS  rh   c                    rT  r!   rU  r'  r   r!   r"   r0     r  c                 3   rV  rW  rX  r'  r   r!   r"   rW     r   z+is_log_deriv_k_t_radical.<locals>.<genexpr>Nc                 s   r   r   rY  r'  r!   r!   r"   rW     r   rZ  c                 S      g | ]}|  d  qS r%   r*  r'  r!   r!   r"   r0     r   c                    r[  r!   r^  r'  r   r!   r"   r0     r   c                    r[  r!   r\  r'  r   r!   r"   r0     r   c                 S   r_  r!   rc  ra  r!   r!   r"   r0     r  c                    r[  r!   r\  r'  r   r!   r"   r0     r   c                    r[  r!   r^  r'  r   r!   r"   r0     r   c                    s   g | ]\}}t ||  qS r!   r`  ra  r|   r!   r"   r0     r   )r   r   r   rf  r-   rg  rh  ri  r  r   r   r   r   r   r   r   Oner   r   r2   r3   r   r   )r8   r9   r;   Dfrk  rl  rm  rn  ro  rp  rq  r,   r   rr  rs  rt  ru  rz  r!   )r;   r|   r"   is_log_deriv_k_t_radical=  sL   ="
r  c                    sJ  | j |dd\} }t||\}|js	 ptdt| ||d\ }|s(dS dd  D }tdd	 |D s:dS tt| pCg g g\} fd
dtt	 D }	t | 
 |
  t | }
|
|j}
|
du rqdS |
|jtd|j|jkrdS |dkr|j}|dkrt|j|j t|j|jdd\}}t|$ t|
|jdd\}}t||f|j\}}t|||||}W d   n1 sw   Y  |du rdS |\}}||j| 9 }n|dkrt| t|
|j\}}t|||dd}W d   n	1 sw   Y  |du rdS |\}nK|dkrJ|jr+|  | kr-dS tjttdd |	D d tfdd|	D  }|fS |dkrStd|dv r^td| td| tjttdd dd |	D D g d fdd|	D }	 }| krtdt || tdd |	D   }|fS )a  
    Checks if f can be written as the logarithmic derivative of a k(t)-radical.

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

    It differs from is_log_deriv_k_t_radical(fa, fd, DE, Df=False)
    for any given fa, fd, DE in that it finds the solution in the
    given field not in some (possibly unspecified extension) and
    "in_field" with the function name is used to indicate that.

    f in k(t) can be written as the logarithmic derivative of a k(t) radical if
    there exist n in ZZ and u in k(t) with n, u != 0 such that n*f == Du/u.
    Either returns (n, u) or None, which means that f cannot be written as the
    logarithmic derivative of a k(t)-radical.

    case is one of {'primitive', 'exp', 'tan', 'auto'} for the primitive,
    hyperexponential, and hypertangent cases, respectively.  If case is 'auto',
    it will attempt to determine the type of the derivation automatically.

    See also
    ========
    is_log_deriv_k_t_radical, is_deriv_k

    Tr&   r   )r   Nc                 S   s   g | ]
\}}||  fqS r!   )
real_roots)r+   r   r   r!   r!   r"   r0     r   z5is_log_deriv_k_t_radical_in_field.<locals>.<listcomp>c                 s   s6    | ]\}}t || kotd d |D V  qdS )c                 s   r   r   rY  )r+   kr!   r!   r"   rW     r   z>is_log_deriv_k_t_radical_in_field.<locals>.<genexpr>.<genexpr>N)r   r   r   ra  r!   r!   r"   rW     s    *z4is_log_deriv_k_t_radical_in_field.<locals>.<genexpr>c                    s2   g | ]}| D ]} | d   ||fqqS r}  )subs)r+   r    r   )r   residuesr   r!   r"   r0     s
    r%   rg   rh   r*   rj   )rr   rk   c                 S   s   g | ]
\}}|  d  qS r}  r~  )r+   r   r   r!   r!   r"   r0     r   c                    rb  r!   rc  ra  r  r!   r"   r0     r   ri   zTThe hypertangent case is not yet implemented for is_log_deriv_k_t_radical_in_field())other_linearother_nonlinearz.The %s case is not supported in this function.zGcase must be one of {'primitive', 'exp', 'tan', 'base', 'auto'}, not %sc                 S   r|  r}  r~  r'  r!   r!   r"   r0   -  r   c                 S   s   g | ]\}}|qS r!   r!   )r+   r   r    r!   r!   r"   r0   -  r   c                    s   g | ]
\}}||  fqS r!   r!   ra  )common_denomr!   r"   r0   /  r   zInexact divisionc                 S   r_  r!   rc  ra  r!   r!   r"   r0   3  r  ) r   r   is_oner   r   r   r2   r3   r   r   r   r   rY   r4   r   rx   ru   rr   r   r   r   r   rw   r:  is_sqfr   r  r   r   r   r  rs   )r8   r9   r;   rr   r   r   r   rootsrespolysresiduetermsrC   r;  r<  papdr,   r  r   r   r!   )r   r  r|   r  r   r"   r:    s     $








r:  )rg   r   )T)rg   N)9__doc__r   	functoolsr   sympy.core.intfuncr   
sympy.corer   r   r   r   r   sympy.integrals.rder	   r
   r   r   sympy.integrals.rischr   r   r   r   r   r   r   r   sympy.polysr   r   r   r   sympy.polys.polymatrixr   r   sympy.solversr   r   r   rH   rf   r   r   r   r   r   r   r   r   r  r   r&  r4  rH  rw   r{  r  r:  r!   r!   r!   r"   <module>   sB    (
Q!F$d0 
 	2
[
z{