def rs_square(p1, x, prec):
 R = p1.ring
 p = R.zero
 iv = R.gens.index(x)
 get = p.get
 items = list(p1.items())
 items.sort(key=(lambda e: e[0][iv]))
 monomial_mul = R.monomial_mul
 for i in range(len(items)):
  (exp1, v1) = items[i]
  for j in range(i):
   (exp2, v2) = items[j]
   if ((exp1[iv] + exp2[iv]) < prec):
    exp = monomial_mul(exp1, exp2)
    p[exp] = (get(exp, 0) + (v1 * v2))
   else:
    break
 p = p.imul_num(2)
 get = p.get
 for (expv, v) in p1.items():
  if ((2 * expv[iv]) < prec):
   e2 = monomial_mul(expv, expv)
   p[e2] = (get(e2, 0) + (v ** 2))
 p.strip_zero()
 return p

def rs_square ( p1 , x , prec ) : R = p1 . ring p = R . zero iv = R . gens . index ( x ) get = p . get items = list ( p1 . items ( ) ) items . sort ( key = ( lambda e : e [ 0 ] [ iv ] ) ) monomial_mul = R . monomial_mul for i in range ( len ( items ) ) : ( exp1 , v1 ) = items [ i ] for j in range ( i ) : ( exp2 , v2 ) = items [ j ] if ( ( exp1 [ iv ] + exp2 [ iv ] ) < prec ) : exp = monomial_mul ( exp1 , exp2 ) p [ exp ] = ( get ( exp , 0 ) + ( v1 * v2 ) ) else : break p = p . imul_num ( 2 ) get = p . get for ( expv , v ) in p1 . items ( ) : if ( ( 2 * expv [ iv ] ) < prec ) : e2 = monomial_mul ( expv , expv ) p [ e2 ] = ( get ( e2 , 0 ) + ( v ** 2 ) ) p . strip_zero ( ) return p
square the series modulo o examples .