
    Ed k                         d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
mZmZmZ ddlmZmZ ddlmZ dd	lmZmZmZmZmZmZmZ d
 Zd ZddZd ZddZddZ d Z!d Z"d Z#d Z$d Z%d Z&ddZ'd Z(dS )a  
Algorithms for solving the Risch differential equation.

Given a differential field K of characteristic 0 that is a simple
monomial extension of a base field k and f, g in K, the Risch
Differential Equation problem is to decide if there exist y in K such
that Dy + f*y == g and to find one if there are some.  If t is a
monomial over k and the coefficients of f and g are in k(t), then y is
in k(t), and the outline of the algorithm here is given as:

1. Compute the normal part n of the denominator of y.  The problem is
then reduced to finding y' in k<t>, where y == y'/n.
2. Compute the special part s of the denominator of y.   The problem is
then reduced to finding y'' in k[t], where y == y''/(n*s)
3. Bound the degree of y''.
4. Reduce the equation Dy + f*y == g to a similar equation with f, g in
k[t].
5. Find the solutions in k[t] of bounded degree of the reduced equation.

See Chapter 6 of "Symbolic Integration I: Transcendental Functions" by
Manuel Bronstein.  See also the docstring of risch.py.
    )mul)reduce)oo)Dummy)PolygcdZZcancel)imre)sqrt)gcdex_diophantinefrac_in
derivationsplitfactorNonElementaryIntegralExceptionDecrementLevelrecognize_log_derivativec                 n   | j         rt          S |t          ||          k    r3|                     |                                          d         d         S g }|}|                     |          }d}|j         r=|                    ||f           ||z  }|dz  }|                     |          }|j         =d}t          d|          }t          |          dk    r[|                                }	||	d         z  }
|                     |
          }|j         r||	d         z  }|
}t          |          dk    [|S )aY  
    Computes the order of a at p, with respect to t.

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

    For a, p in k[t], the order of a at p is defined as nu_p(a) = max({n
    in Z+ such that p**n|a}), where a != 0.  If a == 0, nu_p(a) = +oo.

    To compute the order at a rational function, a/b, use the fact that
    nu_p(a/b) == nu_p(a) - nu_p(b).
    r         )	is_zeror   r   as_polyETremappendlenpop)apt
power_listp1rtracks_powernproductfinalproductfs              3lib/python3.11/site-packages/sympy/integrals/rde.pyorder_atr+   )   sG    	y 	DAJJ 'yy||  #A&& J	
B	b		AL
) 2l+,,,UEE"II	 ) 
 	
A1ajjG
j//Q
   58#EE(OO9 	qMAG j//Q
  H    c                 t    | j         rt          S |                    |          |                     |          z
  S )z
    Computes the order of a/d at oo (infinity), with respect to t.

    For f in k(t), the order or f at oo is defined as deg(d) - deg(a), where
    f == a/d.
    )r   r   degree)r   dr!   s      r*   order_at_oor0   T   s2     	y 	88A;;!$$r,   Nc                 v    |pt          d          }t          |          \  }}t          ||                    j                            }|                    |          }|                    t          ||                    t          |                                                  j                                      j                                       j                            \  }}	 t          |j                  t                    z  z
                      j                  
                                        j                            }
t          |
|          }
|
j                            |          st          dj                   |ffS d |
                                D             }t          t           fd|D             t          dj                            }t          |          }| z  ||z  z
  }||z  }|                    |d          \  }}|||ffS )a  
    Weak normalization.

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

    Given a derivation D on k[t] and f == a/d in k(t), return q in k[t]
    such that f - Dq/q is weakly normalized with respect to t.

    f in k(t) is said to be "weakly normalized" with respect to t if
    residue_p(f) is not a positive integer for any normal irreducible p
    in k[t] such that f is in R_p (Definition 6.1.1).  If f has an
    elementary integral, this is equivalent to no logarithm of
    integral(f) whose argument depends on t has a positive integer
    coefficient, where the arguments of the logarithms not in k(t) are
    in k[t].

    Returns (q, f - Dq/q)
    zr   c                 0    g | ]}|t           v |d k    |S )r   )r	   ).0is     r*   
<listcomp>z#weak_normalizer.<locals>.<listcomp>   s*    888qa2g8!a%8888r,   c           
      ~    g | ]9}t          t          |j                  t                    z  z
            :S  )r   r   r!   r   )r4   r&   DEr   d1s     r*   r6   z#weak_normalizer.<locals>.<listcomp>   sA    NNNqST!RT]]:b"+=+===rBBNNNr,   Tinclude)r   r   r   diffr!   quor   r   r   r   	resultantexprhas
real_rootsr   r   r
   )r   r/   r9   r2   dndsg
d_sqf_parta1br$   Nqdqsnsdr:   s   ` `             @r*   weak_normalizerrN   `   s   ( 	
U3ZZABFB 	BAJ	J**	+	+BaeeBii//55rzz"$7G7G			"$ EB	
T!RT]]:b"---	-66rt<<FF


24	 	AQ

A6::a== 'Q1v&&88ALLNN888AsNNNNNNANNNQ	 	A 
Ar		B	
1qtB	
1BYYr4Y((FBBx=r,   c                 d   t          ||          \  }}t          ||          \  }}|                    |          }	|                    |                    |j                                                |	                    |	                    |j                                      }
||
z  }||
z  }|                    |          d         rt          ||z  }|                    |d          \  }}|| z  |t          |
|          z  |z  z
  }|                    |d          \  }}|||f||f|
fS )a  
    Normal part of the denominator.

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

    Given a derivation D on k[t] and f, g in k(t) with f weakly
    normalized with respect to t, either raise NonElementaryIntegralException,
    in which case the equation Dy + f*y == g has no solution in k(t), or the
    quadruplet (a, b, c, h) such that a, h in k[t], b, c in k<t>, and for any
    solution y in k(t) of Dy + f*y == g, q = y*h in k<t> satisfies
    a*Dq + b*q == c.

    This constitutes step 1 in the outline given in the rde.py docstring.
    r   Tr;   )	r   r   r=   r!   r>   divr   r
   r   )fafdgagdr9   rC   rD   enesr    hr   ccacdbabds                    r*   normal_denomr]      s      R  FBR  FB
r

A
rwwrt}}!!!%%rt"5"566A
1A	!AuuRyy| -,,	
2BYYr4Y((FB	
2:a$$$R'	'BYYr4Y((FB Bx"b1%%r,   autoc                 
   |dk    r|j         }|dk    rt          |j        |j                  }n|dk    r!t          |j        dz  dz   |j                  }n}|dv rg|                                                    |          }|                                                    |          }	| ||	t          d|j                  fS t          d|z            t          |||j                  t          |||j                  z
  }
t          |||j                  t          |||j                  z
  }t          d|t          d|
          z
            }|
sdd	lm	} |dk    r|j
                            t          |j        |j                            }t          |          5  t          |                    d           |                    d          z  |                     d          z  |j                  \  }}t          ||j                  \  }} ||||||          }||\  }}}|dk    rt          ||          }d
d
d
           n# 1 swxY w Y   n|dk    r|j
                            t          |j        dz  dz   |j                            }t          |          5  t          t          |                    t          d                     |                    t          d                    z  |                     t          d                    z            |j                  \  }}t          t!          |                    t          d                     |                    t          d                    z  |                     t          d                    z            |j                  \  }}t          ||j                  \  }}t#          t          d|j                  |z  ||          r\ ||t          t          d          |j                  z  |z  ||z  z   ||z  |||          }||\  }}}|dk    rt          ||          }d
d
d
           n# 1 swxY w Y   t%          d|
 ||z
            }||z  }|| z  }| |z  }||                    |          z  t          ||j                  | z  t'          ||                              |          z  |z  z   }||z  |z                      |          }	|}|||	|fS )a  
    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, c, 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 quadruplet (A, B, C, 1/h) such that
    A, B, C, h in k[t] and for any solution q in k<t> of a*Dq + b*q == c,
    r = qh in k[t] satisfies A*Dr + B*r == C.

    For ``case == 'primitive'``, k<t> == k[t], so it returns (a, b, c, 1) in
    this case.

    This constitutes step 2 of the outline given in the rde.py docstring.
    r^   exptanr   r   )	primitivebasez@case must be one of {'exp', 'tan', 'primitive', 'base'}, not %s.r   parametric_log_derivN)caser   r!   to_fieldr>   
ValueErrorr+   minprdere   r/   r   r   evalr   r   r   r   maxr   )r   r[   r\   rY   rZ   r9   rg   r    BCnbncr&   re   dcoeffalphaaalphadetaaetadAQmr2   betaabetadrI   pNpnrW   s                                r*   special_denomr~      sJ   . v~ wu} 
'rt	 'q1bd##	&	& 'KKMMb!!KKMMb!!1aa'' !%& ' ' 	' 
"a		"a!6!6	6B	"a		"a!6!6	6BArC2JJA )......5= 	)TXXd24..//F## & &!("''!**RWWQZZ)?q		)I24!P!P$VRT22
d((tRHH &GAq!Av &1II& & & & & & & & & & & & & & & U] 	)TXXd24719bd3344F## ) )!(RWWT"XX->->,>rwwtBxx?P?P,PQRQWQWX\]_X`X`QaQa,a)b)bdfdh!i!i&r277488+<+<*<RWWT"XX=N=N*NqvvVZ[]V^V^O_O_*_'`'`bdbfggu$VRT22
d+DBDMM%,?KK ),,VDb244H4H-H-NvV[|-[]cdi]ikoquwyzzA )!"wq!Qq& )"1ayy1) ) ) ) ) ) ) ) ) ) ) ) ) ) ) 	AsAFA	
AB	
QBB	"A
266"::Qa
1b(9(9(=(=a(@(@@CCA	BrrA
A q!Q<s&   BIII#F4Q##Q'*Q'Fc           	      
   |dk    rj         }|                     j                  }|                    j                  }|rt          fd|D                       }n|                    j                  }t	          |                    j                                                                                   |                     j                                                                                  z            }	|dk    rJt          d|t          ||dz
            z
            }
||dz
  k    r|	j        rt          d|	||z
            }
n|dk    rK||k    rt          d||z
            }
nt          d||z
  dz             }
t          j
        j        j        dz
                     \  }}j        }t                    5  t          |	j                  \  }}||dz
  k    riddlm} 	  |||||fg          \  \  }}}t!          |          dk    rt#          d          t          |
|d                   }
nG# t$          $ r Y n:w xY w||k    r/dd	lm}  |||          }||\  }}|dk    r| t)          |                              |          z  ||                    |          z  z                                    |                                |                                 z  z  }t          |j                  \  }}ddlm} 	  |||||fg          \  \  }}}t!          |          dk    rt#          d          t          |
|d                                                   }
n# t$          $ r Y nw xY wd
d
d
           n# 1 swxY w Y   n|dk    rddlm} t          d|t          ||          z
            }
||k    rt          j
                            t/          j        j                            j        j        dz
                     \  }}t                    5  t          |	j                  \  }} |||||          }||\  } }}| dk    rt          |
|          }
d
d
d
           n# 1 swxY w Y   n|dv rj
                            j                  }j
                                        }t	          |	|z            }	t          d|t          ||z   dz
  |          z
            }
|||z   dz
  k    r|	j        rt          d|	||z
            }
nt#          d|z            |
S )am  
    Bound on polynomial solutions.

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

    Given a derivation D on k[t] and ``a``, ``b``, ``c`` in k[t] with ``a != 0``, return
    n in ZZ such that deg(q) <= n for any solution q in k[t] of
    a*Dq + b*q == c, when parametric=False, or deg(q) <= n for any solution
    c1, ..., cm in Const(k) and q in k[t] of a*Dq + b*q == Sum(ci*gi, (i, 1, m))
    when parametric=True.

    For ``parametric=False``, ``cQ`` is ``c``, a ``Poly``; for ``parametric=True``, ``cQ`` is Q ==
    [q1, ..., qm], a list of Polys.

    This constitutes step 3 of the outline given in the rde.py docstring.
    r^   c                 D    g | ]}|                     j                  S r8   )r.   r!   )r4   r5   r9   s     r*   r6   z bound_degree.<locals>.<listcomp>(  s%    ---Q!((24..---r,   rc   r   r   rb   )limited_integratezLength of m should be 1!is_log_deriv_k_t_radical_in_fieldNr`   rd   )ra   other_nonlinearzScase must be one of {'exp', 'tan', 'primitive', 'other_nonlinear', 'base'}, not %s.)rg   r.   r!   rm   r
   r   LCas_expr
is_Integerr   r/   Tlevelr   rk   r   r   ri   r   r   r   re   r>   r   )r   rH   cQr9   rg   
parametricdadbdcalphar&   ru   rv   t1rs   rt   r   zazdry   r   rw   aar2   betarz   r{   re   deltalams      `                          r*   bound_degreer     sH   ( v~ w	
"$B	
"$B  ----"---..YYrt__AIIbdOO&&((00222			"$$$&&' ( (E v~ O:2BQ'((a< 	'E, 	'Aub2g&&A		 J:7 	$ArBwAAArBw{##ART241#566
dTB %	7 %	7$UBD11NFFR!V| #7333333%"3"3FFdD\N# #KHRa
 1vv{ D()BCCCAqtAA 6   D r 7
 DCCCCC55ffbII 7EBQw 7!":a#4#4#<#<R#@#@!@aiimmO",-/RTT 23499;;qttvv3E G'.tRT':':u;;;;;;7*;*;E5"&+4 +4KHRa
  #1vv{ L&01J&K&K K #Aqt||~~ 6 6AA  > ! ! ! D!A%	7 %	7 %	7 %	7 %	7 %	7 %	7 %	7 %	7 %	7 %	7 %	7 %	7 %	7 %	7N 
 :......2B#$$8 
	& $rtRT*:*:!;!;RT"(Q,=OPPJD$## & &!(!5!5((tRHH &  GAq!Av &1II& & & & & & & & & & & & & & & 
+	+ 
:BD!!dggiiuSy!!2BJNB///00ea 	'E$4 	'Aub2g&&A  2489 : : 	: Hso   (N7I:N
INICNM?4AN?
N	NNNN"N ARRRc                 X   t          d|j                  }t          d|j                  }t          d|j                  }	 |j        r||d||fS |dk     du rt          |                     |          }|                    |          j        st          |                     |          |                    |          |                    |          }}} |                     |j                  dk    rU|                                                    |           }|                                                    |           }|||||fS t          || |          \  }	}
|t          | |          z  }|
t          |	|          z
  }||                     |j                  z  }|||	z  z  }|| z  }k)a  
    Rothstein's Special Polynomial Differential Equation algorithm.

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

    Given a derivation D on k[t], an integer n and ``a``,``b``,``c`` in k[t] with
    ``a != 0``, either raise NonElementaryIntegralException, in which case the
    equation a*Dq + b*q == c has no solution of degree at most ``n`` in
    k[t], or return the tuple (B, C, m, alpha, beta) such that B, C,
    alpha, beta in k[t], m in ZZ, and any solution q in k[t] of degree
    at most n of a*Dq + b*q == c must be of the form
    q == alpha*h + beta, where h in k[t], deg(h) <= m, and Dh + B*h == C.

    This constitutes step 4 of the outline given in the rde.py docstring.
    r   r   T)r   r!   r   r   r   r   r>   r.   rh   r   r   )r   rH   rX   r&   r9   zeror   r   rE   r$   r2   s              r*   spder     s   " 24==DBDMME24==D9 	/$4..Ed? 	100EE!HHuuQxx 	100%%((AEE!HHaeeAhha188BD>>Q 	*

  ##A

  ##Aq!UD)) Aq))1	Z2
1b!!!	QXXbd^^	
/r,   c                    t          d|j                  }|j        s|                    |j                  |                     |j                  z
  }d|cxk    r|k    s	n t          t          |                    |j                                                  |                     |j                                                  z  |j        |z  z  |j        d          }||z   }|dz
  }|t          ||          z
  | |z  z
  }|j        |S )a  
    Poly Risch Differential Equation - No cancellation: deg(b) large enough.

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

    Given a derivation D on k[t], ``n`` either an integer or +oo, and ``b``,``c``
    in k[t] with ``b != 0`` and either D == d/dt or
    deg(b) > max(0, deg(D) - 1), either raise NonElementaryIntegralException, in
    which case the equation ``Dq + b*q == c`` has no solution of degree at
    most n in k[t], or a solution q in k[t] of this equation with
    ``deg(q) < n``.
    r   Fexpandr   )r   r!   r   r.   r   r   r   r   rH   rX   r&   r9   rJ   ry   r    s          r*   no_cancel_b_larger     s    	QAi 	(HHRTNNQXXbd^^+A 	1 	1 	1 	1 	1 	1 	1 	10024##%%aiioo&8&8&:&::247BBD  EE
1b!!!AaC' i 	( Hr,   c                    t          d|j                  }|j        s*|dk    rd}n=|                    |j                  |j                            |j                  z
  dz   }d|cxk    r|k    s	n t
          |dk    rt          |                    |j                                                  ||j                            |j                                                  z  z  |j        |z  z  |j        d          }n|                     |j                  |                    |j                  k    rt
          |                     |j                  dk    rQ||                     |j        |j	        dz
                     |                    |j        |j	        dz
                     fS t          |                    |j                                                  |                     |j                                                  z  |j        d          }||z   }|dz
  }|t          ||          z
  | |z  z
  }|j        *|S )a  
    Poly Risch Differential Equation - No cancellation: deg(b) small enough.

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

    Given a derivation D on k[t], ``n`` either an integer or +oo, and ``b``,``c``
    in k[t] with deg(b) < deg(D) - 1 and either D == d/dt or
    deg(D) >= 2, either raise NonElementaryIntegralException, in which case the
    equation Dq + b*q == c has no solution of degree at most n in k[t],
    or a solution q in k[t] of this equation with deg(q) <= n, or the
    tuple (h, b0, c0) such that h in k[t], b0, c0, in k, and for any
    solution q in k[t] of degree at most n of Dq + bq == c, y == q - h
    is a solution in k of Dy + b0*y == c0.
    r   r   Fr   )r   r!   r   r.   r/   r   r   r   r   r   r   r   s          r*   no_cancel_b_smallr     s$     	QAi (6 	7AART!2!22Q6AA 	1 	1 	1 	1 	1 	1 	1 	100q5 
	QYYrt__''))1RT\\"$-?-?-B-B-D-D+DEbdAgMU$ $ $AA xx~~"$/ 544xx~~" 3199RT"(Q,%788IIbd28a<0113 3QYYrt__''))!))BD//*<*<*>*>>  A EE
1b!!!AaC'/ i (2 Hr,   c                 2   t          d|j                  }t          |                     |j                                                   |j                            |j                                                  z            }|j        r
|j        r|}nd}|j        st          ||
                    |j                  |j        
                    |j                  z
  dz             }d|cxk    r|k    s	n t          t          ||j                            |j                                                  z  |                     |j                                                  z             }|j        r|||fS |dk    rPt          |                    |j                                                  |z  |j        |z  z  |j        d          }	n|
                    |j                  |j        
                    |j                  dz
  k    rt          |                    |j                                                  |                     |j                                                  z  }	||	z   }|dz
  }|t          |	|          z
  | |	z  z
  }|j        |S )a  
    Poly Risch Differential Equation - No cancellation: deg(b) == deg(D) - 1

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

    Given a derivation D on k[t] with deg(D) >= 2, n either an integer
    or +oo, and b, c in k[t] with deg(b) == deg(D) - 1, either raise
    NonElementaryIntegralException, in which case the equation Dq + b*q == c has
    no solution of degree at most n in k[t], or a solution q in k[t] of
    this equation with deg(q) <= n, or the tuple (h, m, C) such that h
    in k[t], m in ZZ, and C in k[t], and for any solution q in k[t] of
    degree at most n of Dq + b*q == c, y == q - h is a solution in k[t]
    of degree at most m of Dy + b*y == C.
    r   rf   r   Fr   )r   r!   r
   r   r   r/   r   is_positiver   rm   r.   r   r   )
rH   rX   r&   r9   rJ   lcMry   ur    s
             r*   no_cancel_equalr     sL     	QA	24##%%%bdll24&8&8&;&;&=&==	>	>B	}  i (188BD>>BDKK$5$559::A 	1 	1 	1 	1 	1 	1 	1 	1001RT\\"$''**,,,qyy/A/A/C/CCDD9 	q!9q5 	>QYYrt__''))!+BD!G3RT%HHHAAxx~~RT!2!2Q!66 >44IIbdOO&&((24););)=)==EE
1b!!!AaC'' i (* Hr,   c                 |   ddl m} t          |          5  t          | |j                  \  }} ||||          }||\  }}|dk    rt          d          ddd           n# 1 swxY w Y   |j        r|S ||                    |j                  k     rt          t          d|j                  }	|j        s|                    |j                  }
||
k     rt          t          |          5  t          |
                                |j                  \  }}t          |||||          \  }}ddd           n# 1 swxY w Y   t          |                                |                                z  |j        |
z  z  |j        d          }|	|z  }	|
dz
  }|| |z  t          ||          z   z  }|j        |	S )a  
    Poly Risch Differential Equation - Cancellation: Primitive case.

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

    Given a derivation D on k[t], n either an integer or +oo, ``b`` in k, and
    ``c`` in k[t] with Dt in k and ``b != 0``, either raise
    NonElementaryIntegralException, in which case the equation Dq + b*q == c
    has no solution of degree at most n in k[t], or a solution q in k[t] of
    this equation with deg(q) <= n.
    r   r   Nz7is_deriv_in_field() is required to  solve this problem.r   Fr   )rk   r   r   r   r!   NotImplementedErrorr   r.   r   r   r   rischDEr   r   )rH   rX   r&   r9   r   r[   r\   rw   r2   rJ   ry   a2aa2dsarM   stms                   r*   cancel_primitiver   .  sU    877777			 , ,BD!!B--b"b99 	,DAqAv ,) ++ , , ,, , , , , , , , , , , , , , , 	y 188BD>> -,,QAi 
)HHRTNNq5 	100B 	3 	3qttvvrt,,HCRS#r22FB	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 2::<<

,RT1W4bd5III	SE	QsUZR(((( i 
) Hs%   AA$$A(+A(0AD==EEc                    ddl m} |j                            t	          |j        |j                                                            }t          |          5  t          ||j                  \  }}t          | |j                  \  }}	 |||	|||          }
|
|
\  }}}|dk    rt          d          ddd           n# 1 swxY w Y   |j
        r|S ||                    |j                  k     rt          t	          d|j                  }|j
        sT|                    |j                  }||k     rt          |                                 }t          |          5  t          ||j                  \  }}||z  ||z  t	          ||j                  z  z   }||z  }t          |                                |j                  \  }}t          |||||          \  }}ddd           n# 1 swxY w Y   t	          |                                |                                z  |j        |z  z  |j        d          }||z  }|dz
  }|| |z  t          ||          z   z  }|j
        T|S )a  
    Poly Risch Differential Equation - Cancellation: Hyperexponential case.

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

    Given a derivation D on k[t], n either an integer or +oo, ``b`` in k, and
    ``c`` in k[t] with Dt/t in k and ``b != 0``, either raise
    NonElementaryIntegralException, in which case the equation Dq + b*q == c
    has no solution of degree at most n in k[t], or a solution q in k[t] of
    this equation with deg(q) <= n.
    r   rd   Nz6is_deriv_in_field() is required to solve this problem.r   Fr   )rk   re   r/   r>   r   r!   r   r   r   r   r   r.   r   r   r   r   )rH   rX   r&   r9   re   etaru   rv   r[   r\   rw   r   ry   r2   rJ   rG   a1aa1dr   r   r   rM   r   s                          r*   
cancel_expr   `  s    +*****
$((4bd##
$
$
,
,
.
.C			 + +S"$''
dBD!!B  RtR88 	+GAq!Av +) +* + + ++ + + + + + + + + + + + + + + 	y 188BD>> -,,QAi )HHRTNNq5 	100YY[[B 	5 	5r24((HCd(T#Xd1bdmm33Cd(Cqttvvrt,,HCS#sC44FB	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 2::<<

,RT1W4bd5III	SE	QsUZR((((% i )& Hs&   ACC
C#A?G..G25G2c                    | j         s|j        dk    sL|                     |j                  t	          d|j                            |j                  dz
            k    r(|rddlm}  || |||          S t          | |||          S | j         s?|                     |j                  |j                            |j                  dz
  k     r|j        dk    s#|j                            |j                  dk    r|rddlm	}  || |||          S t          | |||          }t          |t                    r|S |\  }}	}
t          |          5  |	                    |j                  |
                    |j                  }
}	|	t          d          |
t          d	          t!          |	|
||                              |j                  }ddd           n# 1 swxY w Y   ||z   S |j                            |j                  dk    r8|                     |j                  |j                            |j                  dz
  k    r||                     |j                                                   |j                            |j                                                  z  k    r|                     |j                                                  j        st'          d
          |rt)          d          t+          | |||          }t          |t                    r|S |\  }}}t!          | |||          }||z   S | j         rt)          d          |j        dk    r#|rt)          d          t-          | |||          S |j        dk    r#|rt)          d          t/          | |||          S t)          d|j        z            )a  
    Solve a Polynomial Risch Differential Equation with degree bound ``n``.

    This constitutes step 4 of the outline given in the rde.py docstring.

    For parametric=False, cQ is c, a Poly; for parametric=True, cQ is Q ==
    [q1, ..., qm], a list of Polys.
    rc   r   r   )prde_no_cancel_b_larger   )prde_no_cancel_b_smallNzb0 should be a non-Null valuezc0 should be a non-Null valuezResult should be a numberz0prde_no_cancel_b_equal() is not yet implemented.zWRemaining cases for Poly (P)RDE are not yet implemented (is_deriv_in_field() required).r`   zIParametric RDE cancellation hyperexponential case is not yet implemented.rb   zBParametric RDE cancellation primitive case is not yet implemented.zBOther Poly (P)RDE cancellation cases are not yet implemented (%s).)r   rg   r.   r!   rm   r/   rk   r   r   r   r   
isinstancer   r   r   ri   solve_poly_rder   	is_number	TypeErrorr   r   r   r   )rH   r   r&   r9   r   r   r   RrW   b0c0yry   ro   s                 r*   r   r     s    9 P"'V+ PHHRTNNSBDKK$5$5$9:::P  	8444444))!RB777 B2...
) Gqxx~~BD(9(9A(== GWG"$$++bd"3"3q"8G  	8444444))!RB777aQ++a 	H IAr2## @ @BD))2::bd+;+;B F$%DEEE F$%DEEE"2r1b1199"$??@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ q5L	RT		a	 0AHHRTNNbdkk"$6G6G!6K$K 024##%%%bdll24&8&8&;&;&=&===0
 yy!!##- 	97888 	 % '       Ar1b))a 	HGAq!q!Q++Aq5L 9 	E% 'B C C C w% E I- /H I I I!!RB///K' E B- /A B B B'2q"555 * +:<>G+D E E Es   BGG #G c                 x   t          | ||          \  }\  } }t          | ||||          \  }\  }}\  }	}
}t          ||||	|
|          \  }}}}	 t          ||||          }n# t          $ r
 t
          }Y nw xY wt          |||||          \  }}}}}|j        r|}nt          ||||          }||z  |z   ||z  fS )a  
    Solve a Risch Differential Equation: Dy + f*y == g.

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

    See the outline in the docstring of rde.py for more information
    about the procedure used.  Either raise NonElementaryIntegralException, in
    which case there is no solution y in the given differential field,
    or return y in k(t) satisfying Dy + f*y == g, or raise
    NotImplementedError, in which case, the algorithms necessary to
    solve the given Risch Differential Equation have not yet been
    implemented.
    )	rN   r]   r~   r   r   r   r   r   r   )rQ   rR   rS   rT   r9   _r   r[   r\   rY   rZ   hnrw   rn   ro   hsr&   ry   r   r   r   s                        r*   r   r     s    ""b"--KAxB ,RRR @ @AxB"b22r2r266KAq!R Aq"%%   
   1aB//Aq!UDy (1aB''!GdNBrE""s   A# #A76A7)N)r^   )r^   F)F))__doc__operatorr   	functoolsr   
sympy.corer   sympy.core.symbolr   sympy.polysr   r   r	   r
   $sympy.functions.elementary.complexesr   r   (sympy.functions.elementary.miscellaneousr   sympy.integrals.rischr   r   r   r   r   r   r   r+   r0   rN   r]   r~   r   r   r   r   r   r   r   r   r   r8   r,   r*   <module>r      s   .                   # # # # # # - - - - - - - - - - - - 9 9 9 9 9 9 9 9 9 9 9 9 9 9[ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [( ( (V	% 	% 	%0 0 0 0f"& "& "&JQ Q Q Qht t t tn- - -^  <+ + +^, , ,^/ / /d9 9 9xZ Z Z Zz'# '# '# '# '#r,   