#NEWLINE# def real_root( arg , n = None ) : #NEWLINE# #INDENT# 'Return the real nth-root of arg if possible. If n is omitted then\n    all instances of (-n)**(1/odd) will be changed to -n**(1/odd); this\n    will only create a real root of a principle root -- the presence of\n    other factors may cause the result to not be real.\n\n    Examples\n    ========\n\n    >>> from sympy import root, real_root, Rational\n    >>> from sympy.abc import x, n\n\n    >>> real_root(-8, 3)\n    -2\n    >>> root(-8, 3)\n    2*(-1)**(1/3)\n    >>> real_root(_)\n    -2\n\n    If one creates a non-principle root and applies real_root, the\n    result will not be real (so use with caution):\n\n    >>> root(-8, 3, 2)\n    -2*(-1)**(2/3)\n    >>> real_root(_)\n    -2*(-1)**(2/3)\n\n\n    See Also\n    ========\n\n    sympy.polys.rootoftools.rootof\n    sympy.core.power.integer_nthroot\n    root, sqrt\n    ' #NEWLINE# from sympy import im , Piecewise #NEWLINE# if ( n is not None ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# n = as_int ( n ) #NEWLINE# arg = sympify ( arg ) #NEWLINE# if ( arg . is_positive or arg . is_negative ) : #NEWLINE# #INDENT# rv = root ( arg , n ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise ValueError #NEWLINE# #UNINDENT# #UNINDENT# except ValueError : #NEWLINE# #INDENT# return ( root ( arg , n ) * Piecewise ( ( S . One , ( ~ Equality ( im ( arg ) , 0 ) ) ) , ( ( Pow ( S . NegativeOne , ( S . One / n ) ) ** ( 2 * floor ( ( n / 2 ) ) ) ) , And ( Equality ( ( n % 2 ) , 1 ) , ( arg < 0 ) ) ) , ( S . One , True ) ) ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# rv = sympify ( arg ) #NEWLINE# #UNINDENT# n1pow = Transform ( ( lambda x : ( - ( ( - x . base ) ** x . exp ) ) ) , ( lambda x : ( x . is_Pow and x . base . is_negative and x . exp . is_Rational and ( x . exp . p == 1 ) and ( x . exp . q % 2 ) ) ) ) #NEWLINE# return rv . xreplace ( n )