Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
""" Comparison of Symbolic Expressions
There are two useful ways to compare symbolic expressions:
* :func:`print_order` is how the terms are ordered. This is always defined. If you need a fast comparison, this is it.
* :func:`math_order` is the "mathematical" comparison. This may raise an exception if the answer is unknown (to Sage) or cannot, in principle, evaluated to a boolean (for example, if it involves symbolic variables). Can be very slow as it potentially calls Maxima to prove the inequality.
There is also a mixed version:
* :func:`mixed_order` which is print order if variables are present, and mathematical/numeric if not. This should enable quick and correct results. """
from cpython cimport *
from sage.libs.pynac.pynac cimport * from sage.symbolic.ring import SR from sage.symbolic.expression cimport is_Expression
cdef int print_order_c(Expression lhs, Expression rhs): """ Print comparison.
See :meth:`print_order` for details. """
cpdef int print_order(lhs, rhs) except -2: """ Comparison in the print order
INPUT:
- ``lhs``, ``rhs`` -- two symbolic expressions or something that can be converted to one.
OUTPUT:
Either `-1`, `0`, or `+1` indicating the comparison. An exception is raised if the arguments cannot be converted into the symbolic ring.
EXAMPLES::
sage: from sage.symbolic.comparison import print_order sage: print_order(1, oo) 1 sage: print_order(e, oo) -1 sage: print_order(pi, oo) 1 sage: print_order(1, sqrt(2)) 1
Check that :trac:`12967` is fixed::
sage: print_order(SR(oo), sqrt(2)) 1 """
class _print_key(object):
def __init__(self, ex): """ Sort key to sort in print order.
INPUT:
- ``ex`` -- symbolic expression or something that can be converted into one.
EXAMPLES::
sage: from sage.symbolic.comparison import _print_key sage: _print_key(1) <sage.symbolic.comparison._print_key object at 0x...> """
def __lt__(self, other): """ Implement "less than" to make the key comparable.
INPUT:
- ``other`` -- another :class:`_print_key` instance.
OUTPUT:
Boolean.
EXAMPLES::
sage: from sage.symbolic.comparison import print_order, _print_key sage: print_order(1, 2) -1 sage: _print_key(1) < _print_key(2) True sage: print_order(1, sqrt(2)) 1 sage: _print_key(1) < _print_key(sqrt(2)) False """
cpdef print_sorted(expressions): """ Sort a list in print order
INPUT:
- ``expressions`` -- a list/tuple/iterable of symbolic expressions, or something that can be converted to one.
OUTPUT:
The list sorted by :meth:`print_order`.
EXAMPLES::
sage: from sage.symbolic.comparison import print_sorted sage: print_sorted([SR(1), SR(e), SR(pi), sqrt(2)]) [e, sqrt(2), pi, 1] """
class _math_key(object):
def __init__(self, ex): """ Sort key to sort in "Mathematics" order.
INPUT:
- ``ex`` -- symbolic expression or something that can be converted into one.
EXAMPLES::
sage: from sage.symbolic.comparison import _math_key sage: _math_key(1) <sage.symbolic.comparison._math_key object at 0x...> """
def __lt__(self, other): """ Implement "less than" to make the key comparable.
INPUT:
- ``other`` -- another :class:`_print_key` instance.
OUTPUT:
Boolean. A ``ValueError`` is raised if we do not know how to perform the comparison.
EXAMPLES::
sage: from sage.symbolic.comparison import _math_key sage: _math_key(1) < _math_key(2) True sage: _math_key(1) < _math_key(sqrt(2)) True
Check that :trac:`12967` is fixed::
sage: _math_key(1) < _math_key(oo) True """ else: assert False # unreachable else: else: raise ValueError('cannot compare {0} and {1}'.format(self.ex, other.ex))
cpdef math_sorted(expressions): """ Sort a list of symbolic numbers in the "Mathematics" order
INPUT:
- ``expressions`` -- a list/tuple/iterable of symbolic expressions, or something that can be converted to one.
OUTPUT:
The list sorted by ascending (real) value. If an entry does not define a real value (or plus/minus infinity), or if the comparison is not known, a ``ValueError`` is raised.
EXAMPLES::
sage: from sage.symbolic.comparison import math_sorted sage: math_sorted([SR(1), SR(e), SR(pi), sqrt(2)]) [1, sqrt(2), e, pi] """
cpdef int mixed_order(lhs, rhs) except -2: """ Comparison in the mixed order
INPUT:
- ``lhs``, ``rhs`` -- two symbolic expressions or something that can be converted to one.
OUTPUT:
Either `-1`, `0`, or `+1` indicating the comparison. An exception is raised if the arguments cannot be converted into the symbolic ring.
EXAMPLES::
sage: from sage.symbolic.comparison import mixed_order sage: mixed_order(1, oo) -1 sage: mixed_order(e, oo) -1 sage: mixed_order(pi, oo) -1 sage: mixed_order(1, sqrt(2)) -1 sage: mixed_order(x + x^2, x*(x+1)) -1
Check that :trac:`12967` is fixed::
sage: mixed_order(SR(oo), sqrt(2)) 1 """ else:
class _mixed_key(object):
def __init__(self, ex): """ Sort key to sort in mixed order.
Mixed order is print order if variables are present, mathematical/numeric if not. This should enable quick and correct results.
INPUT:
- ``ex`` -- symbolic expression or something that can be converted into one.
EXAMPLES::
sage: from sage.symbolic.comparison import _mixed_key sage: _mixed_key(1) <sage.symbolic.comparison._mixed_key object at 0x...> """
def __lt__(self, other): """ Implement "less than" to make the key comparable.
INPUT:
- ``other`` -- another :class:`_mixed_key` instance.
OUTPUT:
Boolean. A ``ValueError`` is raised if we do not know how to perform the comparison.
EXAMPLES::
sage: from sage.symbolic.comparison import _mixed_key sage: _mixed_key(1) < _mixed_key(2) True sage: _mixed_key(1) < _mixed_key(sqrt(2)) True
Check that :trac:`12967` is fixed::
sage: _mixed_key(1) < _mixed_key(oo) True """ else: else:
# no variables involved from here on raise ValueError('cannot compare {0} and {1}'.format(self.ex, other.ex))
except (TypeError, AttributeError): raise ValueError('cannot compare {0} and {1}'.format(self.ex, other.ex)) else: else:
# here we have expressions containing functions except (TypeError, AttributeError): raise ValueError('cannot compare {0} and {1}'.format(self.ex, other.ex)) else:
cpdef mixed_sorted(expressions): """ Sort a list of symbolic numbers in the "Mixed" order
INPUT:
- ``expressions`` -- a list/tuple/iterable of symbolic expressions, or something that can be converted to one.
OUTPUT:
In the list the numeric values are sorted by ascending (real) value, and the expressions with variables according to print order. If an entry does not define a real value (or plus/minus infinity), or if the comparison is not known, a ``ValueError`` is raised.
EXAMPLES::
sage: from sage.symbolic.comparison import mixed_sorted sage: mixed_sorted([SR(1), SR(e), SR(pi), sqrt(2), x, sqrt(x), sin(1/x)]) [1, sqrt(2), e, pi, sin(1/x), sqrt(x), x] """
|