def __cmp__(cls, other): # If the other object is not a Basic subclass, then we are not equal to # it. if not isinstance(other, BasicType): return -1 n1 = cls.__name__ n2 = other.__name__ c = cmp(n1,n2) if not c: return 0 UNKNOWN = len(ordering_of_classes)+1 try: i1 = ordering_of_classes.index(n1) except ValueError: #print 'Add',n1,'to basic.ordering_of_classes list' #return c i1 = UNKNOWN try: i2 = ordering_of_classes.index(n2) except ValueError: #print 'Add',n2,'to basic.ordering_of_classes list' #return c i2 = UNKNOWN if i1 == UNKNOWN and i2 == UNKNOWN: return c return cmp(i1,i2)
def __cmp__(self, other): """Comparison of two GeometryEntities.""" n1 = self.__class__.__name__ n2 = other.__class__.__name__ c = cmp(n1, n2) if not c: return 0 i1 = -1 for cls in self.__class__.__mro__: try: i1 = ordering_of_classes.index(cls.__name__) break except ValueError: i1 = -1 if i1 == -1: return c i2 = -1 for cls in other.__class__.__mro__: try: i2 = ordering_of_classes.index(cls.__name__) break except ValueError: i2 = -1 if i2 == -1: return c return cmp(i1, i2)
def __cmp__(cls, other): # If the other object is not a Basic subclass, then we are not equal to # it. if not isinstance(other, BasicType): return -1 n1 = cls.__name__ n2 = other.__name__ c = cmp(n1, n2) if not c: return 0 UNKNOWN = len(ordering_of_classes) + 1 try: i1 = ordering_of_classes.index(n1) except ValueError: #print 'Add',n1,'to basic.ordering_of_classes list' #return c i1 = UNKNOWN try: i2 = ordering_of_classes.index(n2) except ValueError: #print 'Add',n2,'to basic.ordering_of_classes list' #return c i2 = UNKNOWN if i1 == UNKNOWN and i2 == UNKNOWN: return c return cmp(i1, i2)
def compare_pretty(a, b): """ Is a > b in the sense of ordering in printing? THIS FUNCTION IS DEPRECATED. Use ``default_sort_key`` instead. :: yes ..... return 1 no ...... return -1 equal ... return 0 Strategy: It uses Basic.compare as a fallback, but improves it in many cases, like ``x**3``, ``x**4``, ``O(x**3)`` etc. In those simple cases, it just parses the expression and returns the "sane" ordering such as:: 1 < x < x**2 < x**3 < O(x**4) etc. Examples ======== >>> from sympy.abc import x >>> from sympy import Basic, Number >>> Basic._compare_pretty(x, x**2) -1 >>> Basic._compare_pretty(x**2, x**2) 0 >>> Basic._compare_pretty(x**3, x**2) 1 >>> Basic._compare_pretty(Number(1, 2), Number(1, 3)) 1 >>> Basic._compare_pretty(Number(0), Number(-1)) 1 """ try: a = _sympify(a) except SympifyError: pass try: b = _sympify(b) except SympifyError: pass # both objects are non-SymPy if (not isinstance(a, Basic)) and (not isinstance(b, Basic)): return cmp(a, b) if not isinstance(a, Basic): return -1 # other < sympy if not isinstance(b, Basic): return +1 # sympy > other # now both objects are from SymPy, so we can proceed to usual comparison return cmp(a.sort_key(), b.sort_key())
def compare(self, other): """ Return -1, 0, 1 if the object is smaller, equal, or greater than other. Not in the mathematical sense. If the object is of a different type from the "other" then their classes are ordered according to the sorted_classes list. Examples ======== >>> from sympy.abc import x, y >>> x.compare(y) -1 >>> x.compare(x) 0 >>> y.compare(x) 1 """ # all redefinitions of __cmp__ method should start with the # following three lines: if self is other: return 0 c = cmp(self.__class__, other.__class__) if c: return c # st = self._hashable_content() ot = other._hashable_content() c = cmp(len(st), len(ot)) if c: return c for l, r in zip(st, ot): if isinstance(l, Basic): c = l.compare(r) elif isinstance(l, frozenset): c = 0 else: c = cmp(l, r) if c: return c return 0
def test_logic_cmp(): l1 = And('a', Not('b')) l2 = And('a', Not('b')) assert hash(l1) == hash(l2) assert (l1==l2) == T assert (l1!=l2) == F assert cmp(l1, l2) == 0 assert And('a','b','c') == And('b','a','c') assert And('a','b','c') == And('c','b','a') assert And('a','b','c') == And('c','a','b')
def sdp_add_term(f, term, u, O, K): """Add a single term using bisection method. """ M, c = term if not c: return f if not f: return [(M, c)] monoms = sdp_monoms(f) if cmp(O(M), O(monoms[ 0])) > 0: return [(M, c)] + f if cmp(O(M), O(monoms[-1])) < 0: return f + [(M, c)] lo, hi = 0, len(monoms)-1 while lo <= hi: i = (lo + hi) // 2 j = cmp(O(M), O(monoms[i])) if not j: coeff = f[i][1] + c if not coeff: return f[:i] + f[i+1:] else: return f[:i] + [(M, coeff)] + f[i+1:] else: if j > 0: hi = i - 1 else: lo = i + 1 else: return f[:i] + [(M, c)] + f[i+1:]
def sdp_sub_term(f, term, u, O, K): """Sub a single term using bisection method. """ M, c = term if not c: return f if not f: return [(M, -c)] monoms = sdp_monoms(f) if cmp(O(M), O(monoms[0])) > 0: return [(M, -c)] + f if cmp(O(M), O(monoms[-1])) < 0: return f + [(M, -c)] lo, hi = 0, len(monoms) - 1 while lo <= hi: i = (lo + hi) // 2 j = cmp(O(M), O(monoms[i])) if not j: coeff = f[i][1] - c if not coeff: return f[:i] + f[i + 1:] else: return f[:i] + [(M, coeff)] + f[i + 1:] else: if j > 0: hi = i - 1 else: lo = i + 1 else: return f[:i] + [(M, -c)] + f[i + 1:]
def _compare_pretty(a, b): from sympy.series.order import Order if isinstance(a, Order) and not isinstance(b, Order): return 1 if not isinstance(a, Order) and isinstance(b, Order): return -1 if a.is_Rational and b.is_Rational: return cmp(a.p*b.q, b.p*a.q) else: from sympy.core.symbol import Wild p1, p2, p3 = Wild("p1"), Wild("p2"), Wild("p3") r_a = a.match(p1 * p2**p3) if r_a and p3 in r_a: a3 = r_a[p3] r_b = b.match(p1 * p2**p3) if r_b and p3 in r_b: b3 = r_b[p3] c = Basic.compare(a3, b3) if c != 0: return c return Basic.compare(a,b)
def _compare_pretty(a, b): from sympy.series.order import Order if isinstance(a, Order) and not isinstance(b, Order): return 1 if not isinstance(a, Order) and isinstance(b, Order): return -1 if a.is_Rational and b.is_Rational: return cmp(a.p * b.q, b.p * a.q) else: from sympy.core.symbol import Wild p1, p2, p3 = Wild("p1"), Wild("p2"), Wild("p3") r_a = a.match(p1 * p2**p3) if r_a and p3 in r_a: a3 = r_a[p3] r_b = b.match(p1 * p2**p3) if r_b and p3 in r_b: b3 = r_b[p3] c = Basic.compare(a3, b3) if c != 0: return c return Basic.compare(a, b)
def monomial_grlex_cmp(a, b): return cmp(sum(a), sum(b)) or cmp(a, b)
def monomial_lex_cmp(a, b): return cmp(a, b)
def _what_known_about(self, k): """tries hard to give an answer to: what is known about fact `k` NOTE: You should not use this directly -- see make__get_assumption instead This function is called when a request is made to see what a fact value is. If we are here, it means that the asked-for fact is not known, and we should try to find a way to find its value. For this we use several techniques: 1. _eval_is_<fact> ------------------ first fact-evaluation function is tried, for example _eval_is_integer 2. relations ------------ if the first step did not succeeded (no such function, or its return is None) then we try related facts. For example means rational --> integer another example is joined rule: integer & !odd --> even so in the latter case if we are looking at what 'even' value is, 'integer' and 'odd' facts will be asked. 3. evalf() for comparable ------------------------- as a last resort for comparable objects we get their numerical value -- this helps to determine facts like 'positive' and 'negative' In all cases when we settle on some fact value, it is given to _learn_new_facts to deduce all its implications, and also the result is cached in ._assumptions for later quick access. """ if k not in _assume_defined: raise AttributeError('undefined assumption %r' % (k)) seen = self._a_inprogress if k in seen: raise CycleDetected seen.append(k) try: # First try the assumption evaluation function if it exists if hasattr(self, '_eval_is_' + k): try: a = getattr(self, '_eval_is_' + k)() except CycleDetected: pass else: if a is not None: self._learn_new_facts(((k, a), )) return a # Try assumption's prerequisites for pk in _assume_rules.prereq[k]: if hasattr(self, '_eval_is_' + pk): # cycle if pk in seen: continue a = getattr(self, 'is_' + pk) if a is not None: self._learn_new_facts(((pk, a), )) # it is possible that we either know or don't know k at # this point try: return self._assumptions[k] except KeyError: pass finally: seen.pop() # For positive/negative try to ask evalf if k in _real_ordering and self.is_comparable: e = self.evalf() if e.is_Number: a = _real_cmp0_table[k][cmp(e, 0)] if a is not None: self._learn_new_facts(((k, a), )) return a # No result -- unknown # cache it (NB ._learn_new_facts(k, None) to learn other properties, # and because assumptions may not be detached) self._learn_new_facts(((k, None), )) return None
def monomial_grevlex_cmp(a, b): return cmp(sum(a), sum(b)) or cmp(tuple(reversed(b)), tuple(reversed(a)))
def __cmp__(a, b): if type(a) is not type(b): return cmp( str(type(a)), str(type(b)) ) else: return cmp(a.args, b.args)
def __cmp__(a, b): if type(a) is not type(b): return cmp(str(type(a)), str(type(b))) else: return cmp(a.args, b.args)
def _compare_pretty(a, b): return cmp(str(a), str(b))
def _what_known_about(self, k): """tries hard to give an answer to: what is known about fact `k` NOTE: You should not use this directly -- see make__get_assumption instead This function is called when a request is made to see what a fact value is. If we are here, it means that the asked-for fact is not known, and we should try to find a way to find its value. For this we use several techniques: 1. _eval_is_<fact> ------------------ first fact-evaluation function is tried, for example _eval_is_integer 2. relations ------------ if the first step did not succeeded (no such function, or its return is None) then we try related facts. For example means rational --> integer another example is joined rule: integer & !odd --> even so in the latter case if we are looking at what 'even' value is, 'integer' and 'odd' facts will be asked. 3. evalf() for comparable ------------------------- as a last resort for comparable objects we get their numerical value -- this helps to determine facts like 'positive' and 'negative' In all cases when we settle on some fact value, it is given to _learn_new_facts to deduce all its implications, and also the result is cached in ._assumptions for later quick access. """ if k not in _assume_defined: raise AttributeError('undefined assumption %r' % (k)) seen = self._a_inprogress if k in seen: raise CycleDetected seen.append(k) try: # First try the assumption evaluation function if it exists if hasattr(self, '_eval_is_' + k): try: a = getattr(self, '_eval_is_' + k)() except CycleDetected: pass else: if a is not None: self._learn_new_facts( ((k, a),) ) return a # Try assumption's prerequisites for pk in _assume_rules.prereq[k]: if hasattr(self, '_eval_is_' + pk): # cycle if pk in seen: continue a = getattr(self, 'is_' + pk) if a is not None: self._learn_new_facts( ((pk,a),) ) # it is possible that we either know or don't know k at # this point try: return self._assumptions[k] except KeyError: pass finally: seen.pop() # For positive/negative try to ask evalf if k in _real_ordering and self.is_comparable: e = self.evalf(1) if e.is_Number: a = _real_cmp0_table[k][cmp(e, 0)] if a is not None: self._learn_new_facts( ((k,a),) ) return a # No result -- unknown # cache it (NB ._learn_new_facts(k, None) to learn other properties, # and because assumptions may not be detached) self._learn_new_facts( ((k,None),) ) return None