(expr, assumptions)
Handler for instances of Pow.

    >>> from sympy import Symbol, Q
    >>> from sympy.assumptions.refine import refine_Pow
    >>> from sympy.abc import x,y,z
    >>> refine_Pow((-1)**x, Q.real(x))
    >>> refine_Pow((-1)**x, Q.even(x))
    1
    >>> refine_Pow((-1)**x, Q.odd(x))
    -1

    For powers of -1, even parts of the exponent can be simplified:

    >>> refine_Pow((-1)**(x+y), Q.even(x))
    (-1)**y
    >>> refine_Pow((-1)**(x+y+z), Q.odd(x) & Q.odd(z))
    (-1)**y
    >>> refine_Pow((-1)**(x+y+2), Q.odd(x))
    (-1)**(y + 1)
    >>> refine_Pow((-1)**(x+3), True)
    (-1)**(x + 1)
Handler for instances of Pow.
Handler for instances of Pow .
def refine_Pow(expr, assumptions):
    """
    Handler for instances of Pow.

    >>> from sympy import Symbol, Q
    >>> from sympy.assumptions.refine import refine_Pow
    >>> from sympy.abc import x,y,z
    >>> refine_Pow((-1)**x, Q.real(x))
    >>> refine_Pow((-1)**x, Q.even(x))
    1
    >>> refine_Pow((-1)**x, Q.odd(x))
    -1

    For powers of -1, even parts of the exponent can be simplified:

    >>> refine_Pow((-1)**(x+y), Q.even(x))
    (-1)**y
    >>> refine_Pow((-1)**(x+y+z), Q.odd(x) & Q.odd(z))
    (-1)**y
    >>> refine_Pow((-1)**(x+y+2), Q.odd(x))
    (-1)**(y + 1)
    >>> refine_Pow((-1)**(x+3), True)
    (-1)**(x + 1)

    """
    from sympy.core import Pow, Rational
    from sympy.functions.elementary.complexes import Abs
    from sympy.functions import sign
    if isinstance(expr.base, Abs):
        if ask(Q.real(expr.base.args[0]), assumptions) and \
                ask(Q.even(expr.exp), assumptions):
            return expr.base.args[0] ** expr.exp
    if ask(Q.real(expr.base), assumptions):
        if expr.base.is_number:
            if ask(Q.even(expr.exp), assumptions):
                return abs(expr.base) ** expr.exp
            if ask(Q.odd(expr.exp), assumptions):
                return sign(expr.base) * abs(expr.base) ** expr.exp
        if isinstance(expr.exp, Rational):
            if type(expr.base) is Pow:
                return abs(expr.base.base) ** (expr.base.exp * expr.exp)

        if expr.base is S.NegativeOne:
            if expr.exp.is_Add:

                old = expr

                # For powers of (-1) we can remove
                #  - even terms
                #  - pairs of odd terms
                #  - a single odd term + 1
                #  - A numerical constant N can be replaced with mod(N,2)

                coeff, terms = expr.exp.as_coeff_add()
                terms = set(terms)
                even_terms = set([])
                odd_terms = set([])
                initial_number_of_terms = len(terms)

                for t in terms:
                    if ask(Q.even(t), assumptions):
                        even_terms.add(t)
                    elif ask(Q.odd(t), assumptions):
                        odd_terms.add(t)

                terms -= even_terms
                if len(odd_terms) % 2:
                    terms -= odd_terms
                    new_coeff = (coeff + S.One) % 2
                else:
                    terms -= odd_terms
                    new_coeff = coeff % 2

                if new_coeff != coeff or len(terms) < initial_number_of_terms:
                    terms.add(new_coeff)
                    expr = expr.base**(Add(*terms))

                # Handle (-1)**((-1)**n/2 + m/2)
                e2 = 2*expr.exp
                if ask(Q.even(e2), assumptions):
                    if e2.could_extract_minus_sign():
                        e2 *= expr.base
                if e2.is_Add:
                    i, p = e2.as_two_terms()
                    if p.is_Pow and p.base is S.NegativeOne:
                        if ask(Q.integer(p.exp), assumptions):
                            i = (i + 1)/2
                            if ask(Q.even(i), assumptions):
                                return expr.base**p.exp
                            elif ask(Q.odd(i), assumptions):
                                return expr.base**(p.exp + 1)
                            else:
                                return expr.base**(p.exp + i)

                if old != expr:
                    return expr
def refine_Pow ( expr , assumptions ) : from sympy . core import Pow , Rational from sympy . functions . elementary . complexes import Abs from sympy . functions import sign if isinstance ( expr . base , Abs ) : if ask ( Q . real ( expr . base . args [ 0 ] ) , assumptions ) and ask ( Q . even ( expr . exp ) , assumptions ) : return expr . base . args [ 0 ] ** expr . exp if ask ( Q . real ( expr . base ) , assumptions ) : if expr . base . is_number : if ask ( Q . even ( expr . exp ) , assumptions ) : return abs ( expr . base ) ** expr . exp if ask ( Q . odd ( expr . exp ) , assumptions ) : return sign ( expr . base ) * abs ( expr . base ) ** expr . exp if isinstance ( expr . exp , Rational ) : if type ( expr . base ) is Pow : return abs ( expr . base . base ) ** ( expr . base . exp * expr . exp ) if expr . base is S . NegativeOne : if expr . exp . is_Add : old = expr # For powers of (-1) we can remove #  - even terms #  - pairs of odd terms #  - a single odd term + 1 #  - A numerical constant N can be replaced with mod(N,2) coeff , terms = expr . exp . as_coeff_add ( ) terms = set ( terms ) even_terms = set ( [ ] ) odd_terms = set ( [ ] ) initial_number_of_terms = len ( terms ) for t in terms : if ask ( Q . even ( t ) , assumptions ) : even_terms . add ( t ) elif ask ( Q . odd ( t ) , assumptions ) : odd_terms . add ( t ) terms -= even_terms if len ( odd_terms ) % 2 : terms -= odd_terms new_coeff = ( coeff + S . One ) % 2 else : terms -= odd_terms new_coeff = coeff % 2 if new_coeff != coeff or len ( terms ) < initial_number_of_terms : terms . add ( new_coeff ) expr = expr . base ** ( Add ( * terms ) ) # Handle (-1)**((-1)**n/2 + m/2) e2 = 2 * expr . exp if ask ( Q . even ( e2 ) , assumptions ) : if e2 . could_extract_minus_sign ( ) : e2 *= expr . base if e2 . is_Add : i , p = e2 . as_two_terms ( ) if p . is_Pow and p . base is S . NegativeOne : if ask ( Q . integer ( p . exp ) , assumptions ) : i = ( i + 1 ) / 2 if ask ( Q . even ( i ) , assumptions ) : return expr . base ** p . exp elif ask ( Q . odd ( i ) , assumptions ) : return expr . base ** ( p . exp + 1 ) else : return expr . base ** ( p . exp + i ) if old != expr : return expr