(p1, x, prec)
return p
square modulo ``O(x**prec)``

    Examples
    ========

    >>> from sympy.polys.domains import QQ
    >>> from sympy.polys.rings import ring
    >>> from sympy.polys.ring_series import rs_square
    >>> R, x = ring('x', QQ)
    >>> p = x**2 + 2*x + 1
    >>> rs_square(p, x, 3)
    6*x**2 + 4*x + 1
square modulo ``O(x**prec)``
square modulo O ( x ** prec )
def rs_square(p1, x, prec):
    """
    square modulo ``O(x**prec)``

    Examples
    ========

    >>> from sympy.polys.domains import QQ
    >>> from sympy.polys.rings import ring
    >>> from sympy.polys.ring_series import rs_square
    >>> R, x = ring('x', QQ)
    >>> p = x**2 + 2*x + 1
    >>> rs_square(p, x, 3)
    6*x**2 + 4*x + 1
    """
    ring = p1.ring
    p = ring.zero
    iv = ring.gens.index(x)
    get = p.get
    items = list(p1.items())
    items.sort(key=lambda e: e[0][iv])
    monomial_mul = ring.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 ) : ring = p1 . ring p = ring . zero iv = ring . gens . index ( x ) get = p . get items = list ( p1 . items ( ) ) items . sort ( key = lambda e : e [ 0 ] [ iv ] ) monomial_mul = ring . 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