#NEWLINE# def cancel_primitive( b , c , n , DE ) : #NEWLINE# #INDENT# '\n    Poly Risch Differential Equation - Cancellation: Primitive case.\n\n    Given a derivation D on k[t], n either an integer or +oo, b in k, and\n    c in k[t] with Dt in k and b != 0, either raise\n    NonElementaryIntegralException, in which case the equation Dq + b*q == c\n    has no solution of degree at most n in k[t], or a solution q in k[t] of\n    this equation with deg(q) <= n.\n    ' #NEWLINE# from sympy.integrals.prde import is_log_deriv_k_t_radical_in_field #NEWLINE# with DecrementLevel ( DE ) : #NEWLINE# #INDENT# ( ba , bd ) = frac_in ( b , DE . t ) #NEWLINE# A = is_log_deriv_k_t_radical_in_field ( ba , bd , DE ) #NEWLINE# if ( A is not None ) : #NEWLINE# #INDENT# ( n , z ) = A #NEWLINE# if ( n == 1 ) : #NEWLINE# #INDENT# raise NotImplementedError ( 'is_deriv_in_field() is required to  solve this problem.' ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# if c . is_zero : #NEWLINE# #INDENT# return c #NEWLINE# #UNINDENT# if ( n < c . degree ( DE . t ) ) : #NEWLINE# #INDENT# raise NonElementaryIntegralException #NEWLINE# #UNINDENT# q = Poly ( 0 , DE . t ) #NEWLINE# while ( not c . is_zero ) : #NEWLINE# #INDENT# m = c . degree ( DE . t ) #NEWLINE# if ( n < m ) : #NEWLINE# #INDENT# raise NonElementaryIntegralException #NEWLINE# #UNINDENT# with DecrementLevel ( DE ) : #NEWLINE# #INDENT# ( a2a , a2d ) = frac_in ( c . LC ( ) , DE . t ) #NEWLINE# ( sa , sd ) = rischDE ( ba , bd , a2a , a2d , DE ) #NEWLINE# #UNINDENT# stm = Poly ( ( ( sa . as_expr ( ) / sd . as_expr ( ) ) * ( DE . t ** m ) ) , DE . t , expand = False ) #NEWLINE# q += stm #NEWLINE# n = ( m - 1 ) #NEWLINE# c -= ( ( b * stm ) + derivation ( stm , DE ) ) #NEWLINE# #UNINDENT# return q