
    \hk                         S r SSKJr  SSKJr  SSKJr  SSKJr  SSK	J
r
JrJrJr  SSKJrJr  SSKJr  SS	KJrJrJrJrJrJrJr  S
 rS rSS jrS rSS jrSS jr S r!S r"S r#S r$S r%S r&SS jr'S r(g)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                 J   U R                   (       a  [        $ U[        X"5      :X  a%  U R                  U5      R	                  5       S   S   $ / nUnU R                  U5      nSnUR                   (       a?  UR                  XF45        XD-  nUS-  nU R                  U5      nUR                   (       a  M?  Sn[        SU5      n[        U5      S:w  aS  UR                  5       n	XS   -  n
U R                  U
5      nUR                   (       a	  XyS   -  nU
n[        U5      S:w  a  MS  U$ )a9  
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              K/var/www/auris/envauris/lib/python3.13/site-packages/sympy/integrals/rde.pyorder_atr+   )   s    	yy	DJyy| #A&& J	
B	b	AL
))2+,UEE"I	 )))
 	
A1ajG
j/Q
 8#EE(O99qMAG j/Q
 H    c                 v    U R                   (       a  [        $ UR                  U5      U R                  U5      -
  $ )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   s+     	yy	88A;!$$r,   Nc                    U=(       d    [        S5      n[        X5      u  pE[        XDR                  UR                  5      5      nUR                  U5      nUR                  [        Xv5      5      n[        UR                  U5      R                  UR                  5      UR                  UR                  5      U R                  UR                  5      5      u  pU [        X2R                  5      [        X5      -  -
  R                  UR                  5      R                  UR                  UR                  5      5      n[        X5      nUR                  R                  U5      (       d  [        SUR                  5      X44$ UR                  5        Vs/ s H  o[        ;   d  M  US:  d  M  UPM     nn[        [         U Vs/ s H1  n[        U [        XR                  5      [        X5      -  -
  U5      PM3     sn[        SUR                  5      5      n[        X5      nX-  UU-  -
  nX-  nUR#                  USS9u  nnUUU44$ s  snf s  snf )aP  
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   r   Tinclude)r   r   r   diffr!   quor   r   r   r   	resultantexprhas
real_rootsr	   r   r   r
   )r   r/   DEr2   dndsg
d_sqf_partd1a1br$   iNr&   qdqsnsds                      r*   weak_normalizerrI   `   s   ( 	
U3ZAFB 	BAJ	J*	+BaeeBi//5rzz"$$7G			"$$EB	
T!TT]:b--	-66rtt<FF


244	AQ
A66::a==Qv&&LLN8Nq2g!a%NA8sANAqST!TT]:b+===rBANQ	A 
A	B	
qtB	
BYYr4Y(FBBx= 	9Ns   I
&I
.I
8I
c                    [        X5      u  pV[        X45      u  pxUR                  U5      n	UR                  UR                  UR                  5      5      R	                  U	R                  U	R                  UR                  5      5      5      n
XZ-  nX-  nUR                  U5      S   (       a  [        eX-  nUR                  USS9u  pX-  U[        X5      -  U-  -
  nUR                  USS9u  nnXU4X4U
4$ )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   Tr3   )	r   r   r5   r!   r6   divr   r
   r   )fafdgagdr;   r<   r=   enesr    hr   ccacdbabds                    r*   normal_denomrX      s       FB FB
r
A
rwwrtt}!!!%%rtt"56A
A	AuuRy|,,	
BYYr4Y(FB	
:a$$R'	'BYYr4Y(FB Bx"1%%r,   c                 \	   US:X  a  UR                   nUS:X  a!  [        UR                  UR                  5      nOUS:X  a'  [        UR                  S-  S-   UR                  5      nOkUS;   aW  UR                  5       R	                  U5      nUR                  5       R	                  U5      n	XU	[        SUR                  5      4$ [        SU-  5      e[        XUR                  5      [        X'UR                  5      -
  n
[        X7UR                  5      [        XGUR                  5      -
  n[        SU[        SU
5      -
  5      nU
(       Gd  SS	KJ	n  US:X  a  UR                  R	                  [        UR                  UR                  5      5      n[        U5         [        UR                  S5      * UR                  S5      -  U R                  S5      -  UR                  5      u  nn[        XR                  5      u  nnU" UUUUU5      nUb  Uu  nnnUS:X  a  [        UU5      nS
S
S
5        GOUS:X  Ga  UR                  R	                  [        UR                  S-  S-   UR                  5      5      n[        U5         [        [        UR                  [        S5      5      * UR                  [        S5      5      -  U R                  [        S5      5      -  5      UR                  5      u  nn[        [!        UR                  [        S5      5      * UR                  [        S5      5      -  U R                  [        S5      5      -  5      UR                  5      u  nn[        XR                  5      u  nn[#        [        SUR                  5      U-  UU5      (       aS  U" U[        [        S5      UR                  5      -  U-  UU-  -   UU-  UUU5      nUb  Uu  nnnUS:X  a  [        UU5      nS
S
S
5        [%        SU
* X-
  5      nUU-  nX|* -  nU U-  nUUR	                  U5      -  [        XR                  5      U -  ['        Xu5      R	                  U5      -  U-  -   nUU-  U-  R	                  U5      n	UnUXU4$ ! , (       d  f       N= f! , (       d  f       N= f)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.
autoexptanr   r   )	primitivebasez@case must be one of {'exp', 'tan', 'primitive', 'base'}, not %s.r   parametric_log_derivN)caser   r!   to_fieldr6   
ValueErrorr+   minprder`   r/   r   r   evalr   r   r   r   maxr   )r   rV   rW   rT   rU   r;   rb   r    BCnbncr&   r`   dcoeffalphaaalphadetaaetadAQmr2   betaabetadrD   pNpnrR   s                                r*   special_denomry      s   . v~wwu}rtt	q1bdd#	&	&KKMb!KKMb!aa'' !%& ' 	' 
"	"!6	6B	"	"!6	6BArC2JA.5=TTXXd244./F#!("''!*RWWQZ)?q	)I244!P$VTT2
d(tRH=GAq!Av1I $# U]TTXXd244719bdd34F#!(RWWT"X->,>rwwtBx?P,PQRQWQWX\]_X`Qa,a)bdfdhdh!i&r27748+<*<RWWT"X=N*NqvvVZ[]V^O_*_'`bdbfbfgu$VTT2
d+DBDDM%,?KK,VDb2444H-H-NvV[|-[]cdi]ikoquwyzA}"#1a6 #Aq	A $ 	AsAFA	
AB	
BB	"A
266":Qa
1(9(=(=a(@@CCA	BrrA
A qQ<E $# $#s   BRE7R
R
R+c           	      	  ^ US:X  a  TR                   nU R                  TR                  5      nUR                  TR                  5      nU(       a  [        U4S jU 5       5      nOUR                  TR                  5      n[	        UR                  TR                  5      R                  5       R                  5       * U R                  TR                  5      R                  5       R                  5       -  5      n	US:X  aE  [        SU[        XvS-
  5      -
  5      n
XvS-
  :X  a   U	R                  (       a  [        SXU-
  5      n
U
$ US:X  Ga  Xv:  a  [        SX-
  5      n
O[        SX-
  S-   5      n
[        TR                  TR                  TR                  S-
     5      u  pTR                  n[        T5         [        U	TR                  5      u  pXvS-
  :X  aC  SSKJn   U" XX4/T5      u  u  nnn[!        U5      S:w  a  [#        S5      e[        U
US   5      n
OXv:X  a  SS	KJn  U" XT5      nUb  Uu  nnUS:X  a  U [)        UT5      R                  U5      -  UUR                  U5      -  -   R                  5       * UR                  5       U R                  5       -  -  n[        UTR                  5      u  nnSSKJn   U" UUX4/T5      u  u  nnn[!        U5      S:w  a  [#        S5      e[        U
US   R                  5       5      n
S
S
S
5        U
$ US:X  a  SSKJn  [        SU[        Xv5      -
  5      n
Xg:X  a  [        TR                  R-                  [/        TR                  TR                  5      5      TR                  TR                  S-
     5      u  p[        T5         [        U	TR                  5      u  pU" XXT5      nUb  Uu  n nnU S:X  a  [        U
U5      n
S
S
S
5        U
$  U
$ US;   a  TR                  R                  TR                  5      nTR                  R                  5       n[	        U	U-  5      n	[        SU[        UU-   S-
  U5      -
  5      n
XvU-   S-
  :X  a   U	R                  (       a  [        SXU-
  5      n
U
$ [#        SU-  5      e! [$         a     GNf = f! [$         a     GNf = f! , (       d  f       U
$ = f! , (       d  f       U
$ = f)a=  
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.
rZ   c              3   X   >#    U  H  oR                  TR                  5      v   M!     g 7fN)r.   r!   ).0rC   r;   s     r*   	<genexpr>bound_degree.<locals>.<genexpr>(  s     ,A"$$s   '*r^   r   r   r]   )limited_integratezLength of m should be 1!is_log_deriv_k_t_radical_in_fieldNr[   r_   )r\   other_nonlinearzScase must be one of {'exp', 'tan', 'primitive', 'other_nonlinear', 'base'}, not %s.)rb   r.   r!   rh   r
   r   LCas_expr
is_Integerr   r/   Tlevelr   rf   r   r   rd   r   r   r   r`   r6   r   )r   rB   cQr;   rb   
parametricdadbdcalphar&   rp   rq   t1rn   ro   r   zazdrt   r   rr   aar2   betaru   rv   r`   deltalams      `                          r*   bound_degreer     sv   ( v~ww	
"$$B	
"$$B ,,,YYrtt_AIIbddO&&(0022			"$$$$&' (E v~2BQ'(a<E,,Au2g&A\ HY 
	7ArwAArw{#ARTT2441#56
TTB$UBDD1NF!V|3%"3Fd\N#KHRa
 1v{()BCCAqtA
 D5fbI=EBQw!":a#4#<#<R#@!@aiimO",-/RT 23499;qttv3E G'.tRTT':u;7*;E5"&+4KHRa
  #1v{&01J&K K #Aqt||~ 6AK  F H9 
.2B#$8 $rttRTT*:!;RTT"((Q,=OPJD#!(!5(RH=  GAq!Av1I $, H1 0 H 
+	+BDD!ddggiuSy!2BJNB//0eaE$4$4Au2g&A H  2489 : 	:q 6 0  > ! !A  F H- $#, Hsa   ;'S#R$5CSR57S,>S$
R2.S1R22S5
S?SSS
S
S'c                    [        SUR                  5      n[        SUR                  5      n[        SUR                  5      n UR                  (       a  XUSXW4$ US:  SL a  [        eU R	                  U5      nUR                  U5      R                  (       d  [        eU R                  U5      UR                  U5      UR                  U5      p!n U R                  UR                  5      S:X  aC  UR                  5       R                  U 5      nUR                  5       R                  U 5      nXX6U4$ [        XU5      u  pU[        X5      -  nU
[        X5      -
  nX0R                  UR                  5      -  nXvU	-  -  nX`-  nGMD  )az  
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   r6   r.   rc   r   r   )r   rB   rS   r&   r;   zeror   r   r>   r$   r2   s              r*   spder     sE   " 244=DBDDME244=D
994..Ed?00EE!HuuQx00%%(AEE!HaeeAha88BDD>Q

  #A

  #A!D)) q)	Z
1!!	XXbdd^	
/ r,   c                 B   [        SUR                  5      nUR                  (       d  UR                  UR                  5      U R                  UR                  5      -
  nSUs=::  a  U::  d   [        e  [        e[        UR                  UR                  5      R                  5       U R                  UR                  5      R                  5       -  UR                  U-  -  UR                  SS9nXF-   nUS-
  nU[        Xc5      -
  X-  -
  nUR                  (       d  M  U$ )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   rB   rS   r&   r;   rE   rt   r    s          r*   no_cancel_b_larger     s     	QAiiHHRTTNQXXbdd^+A{{00 00244##%aiio&8&8&::2447BBDDEE
1!!AC' iii Hr,   c                    [        SUR                  5      nUR                  (       Gd;  US:X  a  SnODUR                  UR                  5      UR                  R                  UR                  5      -
  S-   nSUs=::  a  U::  d   [
        e  [
        eUS:  a  [        UR                  UR                  5      R                  5       XSR                  R                  UR                  5      R                  5       -  -  UR                  U-  -  UR                  SS9nGOU R                  UR                  5      UR                  UR                  5      :w  a  [
        eU R                  UR                  5      S:X  aV  X@R                  UR                  UR                  S-
     5      UR                  UR                  UR                  S-
     5      4$ [        UR                  UR                  5      R                  5       U R                  UR                  5      R                  5       -  UR                  SS9nXF-   nUS-
  nU[        Xc5      -
  X-  -
  nUR                  (       d  GM;  U$ )ah  
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iii6ARTT!22Q6AA{{00 00q5QYYrtt_'')1TT\\"$$-?-B-B-D+DEbddAgMU$A xx~"$$/44xx~"99RTT"((Q,%78IIbdd288a<013 3QYYrtt_'')!))BDD/*<*<*>>A EE
1!!AC'/ iii2 Hr,   c                    [        SUR                  5      n[        U R                  UR                  5      R	                  5       * UR
                  R                  UR                  5      R	                  5       -  5      nUR                  (       a  UR                  (       a  UnOSnUR                  (       Gd	  [        XaR                  UR                  5      UR
                  R                  UR                  5      -
  S-   5      nSUs=::  a  U::  d   [        e  [        e[        XsR
                  R                  UR                  5      R	                  5       -  U R                  UR                  5      R	                  5       -   5      nUR                  (       a  XGU4$ US:  aP  [        UR                  UR                  5      R	                  5       U-  UR                  U-  -  UR                  SS9n	OUR                  UR                  5      UR
                  R                  UR                  5      S-
  :w  a  [        eUR                  UR                  5      R	                  5       U R                  UR                  5      R	                  5       -  n	XI-   nUS-
  nU[        X5      -
  X	-  -
  nUR                  (       d  GM	  U$ )ai  
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   ra   r   Fr   )r   r!   r
   r   r   r/   r   is_positiver   rh   r.   r   r   )
rB   rS   r&   r;   rE   lcMrt   ur    s
             r*   no_cancel_equalr     s     	QA	244##%%bddll244&8&;&;&==	>B	}}iii88BDD>BDDKK$559:A{{00 001TT\\"$$'**,,qyy/A/A/CCD99!9q5QYYrtt_'')!+BDD!G3RTT%HAxx~RTT!2Q!6644IIbddO&&(244););)==EE
1!!AC'' iii* Hr,   c                 r   SSK Jn  [        U5         [        XR                  5      u  pVU" XVU5      nUb  Uu  p(US:X  a  [        S5      eSSS5        UR                  (       a  U$ X!R                  UR                  5      :  a  [        e[        SUR                  5      n	UR                  (       d  UR                  UR                  5      n
X*:  a  [        e[        U5         [        UR                  5       UR                  5      u  p[        WWXU5      u  pSSS5        [        WR                  5       WR                  5       -  UR                  U
-  -  UR                  SS9nX-  n	U
S-
  nXU-  [        X5      -   -  nUR                  (       d  M  U	$ ! , (       d  f       GNR= f! , (       d  f       N= f)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   )rf   r   r   r   r!   NotImplementedErrorr   r.   r   r   r   rischDEr   r   )rB   rS   r&   r;   r   rV   rW   rr   r2   rE   rt   a2aa2dsarH   stms                   r*   cancel_primitiver   .  sa    8		DD!-bb9=DAAv) ++ , , 
 	yy88BDD>,,QAiiHHRTTN500Bqttvrtt,HCRSr2FB   2::<

,RTT1W4bdd5I	E	sUZ((( iii HA 
	0  s   9F"7F(
F%(
F6c                    SSK Jn  UR                  R                  [	        UR
                  UR
                  5      5      R                  5       n[        U5         [        XSR
                  5      u  pg[        XR
                  5      u  pU" XXgU5      n
U
b  U
u  pnUS:X  a  [        S5      eSSS5        UR                  (       a  U$ X!R                  UR
                  5      :  a  [        e[	        SUR
                  5      nUR                  (       Gd7  UR                  UR
                  5      nX,:  a  [        eU R                  5       n[        U5         [        XR
                  5      u  nnUW-  WU-  [	        XR
                  5      -  -   nUU-  n[        UR                  5       UR
                  5      u  nn[        UUUUU5      u  nnSSS5        [	        WR                  5       WR                  5       -  UR
                  U-  -  UR
                  SS9nUU-  nUS-
  nXU-  [        UU5      -   -  nUR                  (       d  GM7  U$ ! , (       d  f       GN= f! , (       d  f       N= f)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   r_   Nz6is_deriv_in_field() is required to solve this problem.r   Fr   )rf   r`   r/   r6   r   r!   r   r   r   r   r   r.   r   r   r   r   )rB   rS   r&   r;   r`   etarp   rq   rV   rW   rr   r   rt   r2   rE   rA   a1aa1dr   r   r   rH   r   s                          r*   
cancel_expr   `  s    +
$$((4bdd#
$
,
,
.C		S$$'
DD! R8=GA!Av) +* + + 
 	yy88BDD>,,QAiiiHHRTTN500YY[Br44(HCd(T#Xd1ddm33Cd(Cqttvrtt,HCS#sC4FB   2::<

,RTT1W4bdd5I	SE	sUZR(((% iii& HS 
	8  s   AIA8I
I
I+c                    U R                   (       d  UR                  S:X  dO  U R                  UR                  5      [	        SUR
                  R                  UR                  5      S-
  5      :  a"  U(       a  SSKJn  U" XX#5      $ [        XX#5      $ U R                   (       dF  U R                  UR                  5      UR
                  R                  UR                  5      S-
  :  Ga  UR                  S:X  d)  UR
                  R                  UR                  5      S:  a  U(       a  SSKJ	n  U" XX#5      $ [        XX#5      n[        U[        5      (       a  U$ Uu  pn
[        U5         U	R                  UR                  5      U
R                  UR                  5      pU	c  [        S5      eU
c  [        S	5      e[!        XX#5      R                  UR                  5      nSSS5        X-   $ UR
                  R                  UR                  5      S:  Ga3  U R                  UR                  5      UR
                  R                  UR                  5      S-
  :X  a  X R                  UR                  5      R#                  5       * UR
                  R                  UR                  5      R#                  5       -  :  a  U R                  UR                  5      R#                  5       R$                  (       d  ['        S
5      eU(       a  [)        S5      e[+        XX#5      n[        U[        5      (       a  U$ Uu  pn[!        XX5      nX-   $ U R                   (       a  [)        S5      eUR                  S:X  a  U(       a  [)        S5      e[-        XX#5      $ UR                  S:X  a  U(       a  [)        S5      e[/        XX#5      $ [)        SUR                  -  5      e! , (       d  f       UW-   $ = f)z
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.
r^   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.r]   zBParametric RDE cancellation primitive case is not yet implemented.zBOther Poly (P)RDE cancellation cases are not yet implemented (%s).)r   rb   r.   r!   rh   r/   rf   r   r   r   r   
isinstancer   r   r   rd   solve_poly_rder   	is_number	TypeErrorr   r   r   r   )rB   r   r&   r;   r   r   r   RrR   b0c0yrt   rj   s                 r*   r   r     s!    99"''V+HHRTTNSBDDKK$5$9::4)!77 ..
))qxx~BDD(9A(==WW"$$++bdd"3q"84)!77aQ+aH IA2#BDD)2::bdd+;B:$%DEE;$%DEE"21199"$$? $ 5L	RTT	a	AHHRTTNbddkk"$$6G!6K$K244##%%bddll244&8&;&;&=== yy!!#--788% '     A1)aHGA!qQ+A5L 99% 'B C C ww%- /H I I!!//K'- /A B B'q55 * +:<>GG+D E Ee $# q5Ls   ,A7O
O.c                 6   [        XU5      u  nu  p[        XX#U5      u  nu  pxu  pn[        XgXX5      u  pp [        XX5      n[        XUUU5      u  pnnnUR                  (       a  UnO[        XUU5      nUU-  U-   X-  4$ ! [         a	    [
        n NRf = f)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.
)	rI   rX   ry   r   r   r   r   r   r   )rL   rM   rN   rO   r;   _r   rV   rW   rT   rU   hnrr   ri   rj   hsr&   rt   r   r   r   s                        r*   r   r     s     """-KAx ,RRR @Ax"2rr6KA! q%  aB/A!UDyy1B'!GdNBE""  
 s   B BBr|   )rZ   )rZ   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   rI   rX   ry   r   r   r   r   r   r   r   r   r    r,   r*   <module>r      s   .    # - - 9 9[ [ [(V	%0f"&JQhtn-^<+^,^/d9xZz'#r,   