
    wgj                         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(y)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                   rt        S |t        ||      k(  r%| j                  |      j	                         d   d   S g }|}| j                  |      }d}|j                   r;|j                  ||f       ||z  }|dz  }| j                  |      }|j                   r;d}t        d|      }t        |      dk7  rN|j                         }	||	d   z  }
| j                  |
      }|j                   r
||	d   z  }|
}t        |      dk7  rN|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              X/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/sympy/integrals/rde.pyorder_atr+   )   s    	yy	DAJyy| #A&& J	
B	b	AL
))2l+,UEE"I	 ))
 	
A1ajG
j/Q
 58#EE(O99qMAG j/Q
 H    c                 l    | j                   rt        S |j                  |      | j                  |      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   s+     	yy	88A;!$$r,   Nc                    |xs t        d      }t        ||      \  }}t        ||j                  |j                              }|j                  |      }|j                  t        ||            }t        |j                  |      j                  |j                        |j                  |j                        | j                  |j                              \  }	}
| t        ||j                        t        ||      z  z
  j                  |j                        j                  |j                  |j                              }t        ||      }|j                  j                  |      st        d|j                        | |ffS |j                         D cg c]  }|t        v s|dkD  s| }}t        t         |D cg c]2  }t        | t        ||j                        t        ||      z  z
  |      4 c}t        d|j                              }t        ||      }|| z  ||z  z
  }||z  }|j#                  |d      \  }}|||ffS c c}w c c}w )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   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BFB 	BAJ	J*	+BaeeBi//5rzz"$$7G			"$$EB	
T!RTT]:b"--	-66rtt<FF


244	AQ
A66::a=Q1v&&LLN8qa2g!a%8A8sANqST!RTT]:b"+===rBNQ	A 
Ar	B	
1qtB	
1BYYr4Y(FBBx= 	9Ns   I"I(I<7I
c                    t        ||      \  }}t        ||      \  }}|j                  |      }	|j                  |j                  |j                              j	                  |	j                  |	j                  |j                                    }
||
z  }||
z  }|j                  |      d   rt        ||z  }|j                  |d      \  }}|| z  |t        |
|      z  |z  z
  }|j                  |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   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      R FBR FB
r
A
rwwrtt}!!!%%rtt"56A
1A	!AuuRy|,,	
2BYYr4Y(FB	
2:a$$R'	'BYYr4Y(FB Bx"b1%%r,   c                 6	   |dk(  r|j                   }|dk(  r!t        |j                  |j                        }n|dk(  r't        |j                  dz  dz   |j                        }nj|dv rX|j                         j	                  |      }|j                         j	                  |      }	| ||	t        d|j                        fS t        d|z        t        |||j                        t        |||j                        z
  }
t        |||j                        t        |||j                        z
  }t        d|t        d|
      z
        }|
sdd	lm	} |dk(  r|j                  j	                  t        |j                  |j                              }t        |      5  t        |j                  d       |j                  d      z  | j                  d      z  |j                        \  }}t        ||j                        \  }} ||||||      }||\  }}}|dk(  rt        ||      }d
d
d
       n|dk(  r|j                  j	                  t        |j                  dz  dz   |j                              }t        |      5  t        t        |j                  t        d             |j                  t        d            z  | j                  t        d            z        |j                        \  }}t        t!        |j                  t        d             |j                  t        d            z  | j                  t        d            z        |j                        \  }}t        ||j                        \  }}t#        t        d|j                        |z  ||      rQ ||t        t        d      |j                        z  |z  ||z  z   ||z  |||      }||\  }}}|dk(  rt        ||      }d
d
d
       t%        d|
 ||z
        }||z  }|| z  }| |z  }||j	                  |      z  t        ||j                        | z  t'        ||      j	                  |      z  |z  z   }||z  |z  j	                  |      }	|}|||	|fS # 1 sw Y   xY w# 1 sw Y   xY w)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!1aa'' !%& ' 	' 
"a	"a!6	6B	"a	"a!6	6BArC2JA.5=TTXXd244./F# &!("''!*RWWQZ)?q	)I244!P$VRTT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$VRTT2
d+DBDDM%,?K,VDb2444H-H-NvV[|-[]cdi]ikoquwyzA}!"wq!Qq&"1ay1) 	AsAFA	
AB	
QBB	"A
266":Qa
1b(9(=(=a(@@CCA	BrrA
A q!Q<E& &) )s   BR=E1RRRc           	      	   |dk(  rj                   }| j                  j                        }|j                  j                        }|rt        fd|D              }n|j                  j                        }t	        |j                  j                        j                         j                          | j                  j                        j                         j                         z        }	|dk(  rBt        d|t        ||dz
        z
        }
||dz
  k(  r|	j                  rt        d|	||z
        }
|
S |dk(  r||kD  rt        d||z
        }
nt        d||z
  dz         }
t        j                  j                  j                  dz
           \  }}j                  }t              5  t        |	j                        \  }}||dz
  k(  rDddlm} 	  |||||fg      \  \  }}}t!        |      dk7  rt#        d      t        |
|d         }
n||k(  rdd	lm}  |||      }||\  }}|dk(  r| t)        |      j                  |      z  ||j                  |      z  z   j                          |j                         | j                         z  z  }t        |j                        \  }}ddlm} 	  |||||fg      \  \  }}}t!        |      dk7  rt#        d      t        |
|d   j                               }
d
d
d
       |
S |dk(  rddlm} t        d|t        ||      z
        }
||k(  rt        j                  j-                  t/        j                  j                              j                  j                  dz
           \  }}t              5  t        |	j                        \  }} |||||      }||\  } }}| dk(  rt        |
|      }
d
d
d
       |
S |
S |dv rj                  j                  j                        }j                  j                         }t	        |	|z        }	t        d|t        ||z   dz
  |      z
        }
|||z   dz
  k(  r|	j                  rt        d|	||z
        }
|
S t#        d|z        # t$        $ r Y w xY w# t$        $ r Y w xY w# 1 sw Y   |
S xY w# 1 sw Y   |
S xY w)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.
    rZ   c              3   T   K   | ]  }|j                  j                         ! y wN)r.   r!   ).0rC   r;   s     r*   	<genexpr>z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     s   ( v~ww	
"$$B	
"$$B ,,,YYrtt_AIIbddO&&(0022			"$$$$&' (E v~2BQ'(a<E,,Aub2g&A\ HY 
	7ArBwAArBw{#ARTT2441#56
dTTB %	7$UBDD1NFFR!V|3%"3FFdD\N#KHRa
 1v{()BCCAqtAr
 D5ffb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%	7F H9 
.2B#$8 $rttRTT*:!;RTT"((Q,=OPJD$# &!(!5(tRH=  GAq!Av1I&, H1H 
+	+BDD!ddggiuSy!2BJNB//0eaE$4$4Aub2g&A H  2489 : 	:q 6 0  > ! !A%	7F H-&, Hsa   2(R7R/CR7=R'6R7%?S	R$ R7#R$$R7'	R40R73R44R77SSc                    t        d|j                        }t        d|j                        }t        d|j                        }	 |j                  r||d||fS |dk  du rt        | j	                  |      }|j                  |      j                  st        | j                  |      |j                  |      |j                  |      }}} | j                  |j                        dk(  rE|j                         j                  |       }|j                         j                  |       }|||||fS t        || |      \  }	}
|t        | |      z  }|
t        |	|      z
  }|| j                  |j                        z  }|||	z  z  }|| z  }C)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   r6   r.   rc   r   r   )r   rB   rS   r&   r;   zeror   r   r>   r$   r2   s              r*   spder     s]   " 244=DBDDME244=D
99$4..Ed?00EE!HuuQx00%%(AEE!HaeeAha188BDD>Q

  #A

  #Aq!UD)) Aq)1	Z2
1b!!	QXXbdd^	
/ r,   c                 2   t        d|j                        }|j                  s|j                  |j                        | j                  |j                        z
  }d|cxk  r
|k  st         t        t        |j                  |j                        j                         | j                  |j                        j                         z  |j                  |z  z  |j                  d      }||z   }|dz
  }|t        ||      z
  | |z  z
  }|j                  s|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   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
1b!!AaC' ii Hr,   c                    t        d|j                        }|j                  s9|dk(  rd}nD|j                  |j                        |j                  j                  |j                        z
  dz   }d|cxk  r
|k  st
         t
        |dkD  rt        |j                  |j                        j                         ||j                  j                  |j                        j                         z  z  |j                  |z  z  |j                  d      }n| j                  |j                        |j                  |j                        k7  rt
        | j                  |j                        dk(  rW|| j                  |j                  |j                  dz
           |j                  |j                  |j                  dz
           fS t        |j                  |j                        j                         | j                  |j                        j                         z  |j                  d      }||z   }|dz
  }|t        ||      z
  | |z  z
  }|j                  s9|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ii6ARTT!22Q6AA{{00 00q5QYYrtt_'')1RTT\\"$$-?-B-B-D+DEbddAgMU$A xx~"$$/44xx~"199RTT"((Q,%78IIbdd288a<013 3QYYrtt_'')!))BDD/*<*<*>>A EE
1b!!AaC'/ ii2 Hr,   c                 X   t        d|j                        }t        | j                  |j                        j	                          |j
                  j                  |j                        j	                         z        }|j                  r|j                  r|}nd}|j                  st        ||j                  |j                        |j
                  j                  |j                        z
  dz         }d|cxk  r
|k  st         t        t        ||j
                  j                  |j                        j	                         z  | j                  |j                        j	                         z         }|j                  r|||fS |dkD  rSt        |j                  |j                        j	                         |z  |j                  |z  z  |j                  d      }	n|j                  |j                        |j
                  j                  |j                        dz
  k7  rt        |j                  |j                        j	                         | j                  |j                        j	                         z  }	||	z   }|dz
  }|t        |	|      z
  | |	z  z
  }|j                  s|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   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	}}ii188BDD>BDDKK$559:A{{00 001RTT\\"$$'**,,qyy/A/A/CCD99q!9q5QYYrtt_'')!+BDD!G3RTT%HAxx~RTT!2Q!6644IIbddO&&(244););)==EE
1b!!AaC'' ii* Hr,   c                 T   ddl m} t        |      5  t        | |j                        \  }} ||||      }||\  }}|dk(  rt        d      ddd       |j                  r|S ||j                  |j                        k  rt        t        d|j                        }	|j                  s|j                  |j                        }
||
k  rt        t        |      5  t        |j                         |j                        \  }}t        |||      \  }}ddd       t        j                         j                         z  |j                  |
z  z  |j                  d      }|	|z  }	|
dz
  }|| |z  t        ||      z   z  }|j                  s|	S # 1 sw Y   FxY w# 1 sw Y   xY w)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   .  s    8		 ,BDD!B-b"b9=DAqAv) ++ , ,, 	yy188BDD>,,QAiiHHRTTNq500B 	3qttvrtt,HCRS#r2FB	3 2::<

,RTT1W4bdd5I	SE	QsUZR((( ii HA, ,0	3 	3s   ;F:FFF'c                    ddl m} |j                  j                  t	        |j
                  |j
                              j                         }t        |      5  t        ||j
                        \  }}t        | |j
                        \  }}	 |||	|||      }
|
|
\  }}}|dk(  rt        d      ddd       |j                  r|S ||j                  |j
                        k  rt        t	        d|j
                        }|j                  s7|j                  |j
                        }||k  rt        | j                         }t        |      5  t        ||j
                        \  }}|z  |z  t	        ||j
                        z  z   }||z  }t        |j                         |j
                        \  }}t        |||||      \  }}ddd       t	        j                         j                         z  |j
                  |z  z  |j
                  d      }||z  }|dz
  }|| |z  t        ||      z   z  }|j                  s7|S # 1 sw Y   xY w# 1 sw Y   xY w)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"$$'
dBDD!B RtR8=GAq!Av) +* + ++ 	yy188BDD>,,QAiiHHRTTNq500YY[B 	5r244(HCd(T#Xd1bddm33Cd(Cqttvrtt,HCS#sC4FB	5 2::<

,RTT1W4bdd5I	SE	QsUZR(((% ii& HS+ +8	5 	5s   AIA:IIIc                    | j                   s~|j                  dk(  sN| j                  |j                        t	        d|j
                  j                  |j                        dz
        kD  r!|rddlm}  || |||      S t        | |||      S | j                   sE| j                  |j                        |j
                  j                  |j                        dz
  k  r |j                  dk(  s(|j
                  j                  |j                        dk\  r|rddlm	}  || |||      S t        | |||      }t        |t              r|S |\  }}	}
t        |      5  |	j                  |j                        |
j                  |j                        }
}	|	t        d      |
t        d	      t!        |	|
||      j                  |j                        }ddd       ||z   S |j
                  j                  |j                        dk\  r)| j                  |j                        |j
                  j                  |j                        dz
  k(  r|| j                  |j                        j#                          |j
                  j                  |j                        j#                         z  kD  r| j                  |j                        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        # 1 sw Y   |z   S xY w)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.
    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     sP    99"''V+HHRTTNSBDDKK$5$9::4)!RB77 B2..
))qxx~BDD(9A(==WW"$$++bdd"3q"84)!RB77aQ+aH IAr2# @BDD)2::bdd+;B:$%DEE;$%DEE"2r1b199"$$?@ q5L	RTT	a	AHHRTTNbddkk"$$6G!6K$K244##%%bddll244&8&;&;&=== yy!!#--788% '     Ar1b)aHGAq!q!Q+Aq5L 99% 'B C C ww%- /H I I!!RB//K'- /A B B'2q"55 * +:<>GG+D E Ee@ q5Ls   A8N11N>c                 L   t        | ||      \  }\  } }t        | ||||      \  }\  }}\  }	}
}t        ||||	|
|      \  }}}}	 t        ||||      }t        |||||      \  }}}}}|j                  r|}nt        ||||      }||z  |z   ||z  fS # t        $ r	 t
        }Y Pw xY w)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     ""b"-KAxB ,RRR @AxB"b22r2r26KAq!R Aq"%  1aB/Aq!UDyy1aB'!GdNBrE""  
 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,   