(self)
Canonical way to choose an element in the set {e, -e} where
           e is any expression. If the canonical element is e, we have
           e.could_extract_minus_sign() == True, else
           e.could_extract_minus_sign() == False.

           For any expression, the set ``{e.could_extract_minus_sign(),
           (-e).could_extract_minus_sign()}`` must be ``{True, False}``.

           >>> from sympy.abc import x, y
           >>> (x-y).could_extract_minus_sign() != (y-x).could_extract_minus_sign()
           True
Canonical way to choose an element in the set {e, -e} where
           e is any expression. If the canonical element is e, we have
           e.could_extract_minus_sign() == True, else
           e.could_extract_minus_sign() == False.
Canonical way to choose an element in the set { e - e } where e is any expression . If the canonical element is e we have e . could_extract_minus_sign () == True else e . could_extract_minus_sign () == False .
def could_extract_minus_sign(self):
        """Canonical way to choose an element in the set {e, -e} where
           e is any expression. If the canonical element is e, we have
           e.could_extract_minus_sign() == True, else
           e.could_extract_minus_sign() == False.

           For any expression, the set ``{e.could_extract_minus_sign(),
           (-e).could_extract_minus_sign()}`` must be ``{True, False}``.

           >>> from sympy.abc import x, y
           >>> (x-y).could_extract_minus_sign() != (y-x).could_extract_minus_sign()
           True

        """
        negative_self = -self
        self_has_minus = (self.extract_multiplicatively(-1) is not None)
        negative_self_has_minus = (
            (negative_self).extract_multiplicatively(-1) is not None)
        if self_has_minus != negative_self_has_minus:
            return self_has_minus
        else:
            if self.is_Add:
                # We choose the one with less arguments with minus signs
                all_args = len(self.args)
                negative_args = len([False for arg in self.args if arg.could_extract_minus_sign()])
                positive_args = all_args - negative_args
                if positive_args > negative_args:
                    return False
                elif positive_args < negative_args:
                    return True
            elif self.is_Mul:
                # We choose the one with an odd number of minus signs
                num, den = self.as_numer_denom()
                args = Mul.make_args(num) + Mul.make_args(den)
                arg_signs = [arg.could_extract_minus_sign() for arg in args]
                negative_args = list(filter(None, arg_signs))
                return len(negative_args) % 2 == 1

            # As a last resort, we choose the one with greater value of .sort_key()
            return bool(self.sort_key() < negative_self.sort_key())
def could_extract_minus_sign ( self ) : negative_self = - self self_has_minus = ( self . extract_multiplicatively ( - 1 ) is not None ) negative_self_has_minus = ( ( negative_self ) . extract_multiplicatively ( - 1 ) is not None ) if self_has_minus != negative_self_has_minus : return self_has_minus else : if self . is_Add : # We choose the one with less arguments with minus signs all_args = len ( self . args ) negative_args = len ( [ False for arg in self . args if arg . could_extract_minus_sign ( ) ] ) positive_args = all_args - negative_args if positive_args > negative_args : return False elif positive_args < negative_args : return True elif self . is_Mul : # We choose the one with an odd number of minus signs num , den = self . as_numer_denom ( ) args = Mul . make_args ( num ) + Mul . make_args ( den ) arg_signs = [ arg . could_extract_minus_sign ( ) for arg in args ] negative_args = list ( filter ( None , arg_signs ) ) return len ( negative_args ) % 2 == 1 # As a last resort, we choose the one with greater value of .sort_key() return bool ( self . sort_key ( ) < negative_self . sort_key ( ) )