def rs_series_reversion(p, x, n, y):
 if rs_is_puiseux(p, x):
  raise NotImplementedError
 R = p.ring
 nx = R.gens.index(x)
 y = R(y)
 ny = R.gens.index(y)
 if _has_constant_term(p, x):
  raise ValueError('p must not contain a constant term in the series variable')
 a = _coefficient_t(p, (nx, 1))
 zm = R.zero_monom
 assert ((zm in a) and (len(a) == 1))
 a = a[zm]
 r = (y / a)
 for i in range(2, n):
  sp = rs_subs(p, {x: r}, y, (i + 1))
  sp = (_coefficient_t(sp, (ny, i)) * (y ** i))
  r -= (sp / a)
 return r

def rs_series_reversion ( p , x , n , y ) : if rs_is_puiseux ( p , x ) : raise NotImplementedError R = p . ring nx = R . gens . index ( x ) y = R ( y ) ny = R . gens . index ( y ) if _has_constant_term ( p , x ) : raise ValueError ( 'p must not contain a constant term in the series variable' ) a = _coefficient_t ( p , ( nx , 1 ) ) zm = R . zero_monom assert ( ( zm in a ) and ( len ( a ) == 1 ) ) a = a [ zm ] r = ( y / a ) for i in range ( 2 , n ) : sp = rs_subs ( p , { x : r } , y , ( i + 1 ) ) sp = ( _coefficient_t ( sp , ( ny , i ) ) * ( y ** i ) ) r -= ( sp / a ) return r
reversion of a series .