Ticket #14507: trac_14507-tropical_semiring_suggestions-dg.patch

File trac_14507-tropical_semiring_suggestions-dg.patch, 9.0 KB (added by darij, 8 years ago)

version 2

  • sage/rings/semirings/tropical_semiring.pyx

    # HG changeset patch
    # User darij grinberg <darijgrinberg@gmail.com>
    # Date 1371210164 25200
    # Node ID 1e8c262a1ee0dcf475e5b9b3830b619c0d886629
    # Parent  750452dc2b30608dc958f0e8f9d61a09e0651ab4
    [mq]: tropical_mod.patch
    
    diff --git a/sage/rings/semirings/tropical_semiring.pyx b/sage/rings/semirings/tropical_semiring.pyx
    a b import operator 
    3535
    3636cdef class TropicalSemiringElement(RingElement):
    3737    r"""
    38     An element in the tropical semiring. Either in `R` or `\infty`.
     38    An element in the tropical semiring over an ordered additive semigroup
     39    `R`. Either in `R` or `\infty`.
    3940    Operators `+, \cdot` are defined as tropical operators `\oplus, \odot`
    4041    respectively.
    4142    """
    cdef class TropicalSemiringElement(RingE 
    7475            sage: elt = T(2)
    7576            sage: elt.__reduce__()
    7677            (<type 'sage.rings.semirings.tropical_semiring.TropicalSemiringElement'>,
    77              (Tropical semiring over Rational Field, 2))
     78             (Tropical semiring over Rational Field with min as addition, 2))
    7879        """
    7980        return (TropicalSemiringElement, (self.parent(), self._val))
    8081
    cdef class TropicalSemiringElement(RingE 
    176177
    177178    cdef int _cmp_c_impl(left, Element right) except -2:
    178179        """
    179         Return ``-1`` if exactly one of the numbers is ``NaN``.  Return ``-1``
    180         if ``left`` is less than ``right``, ``0`` if ``left`` and ``right``
    181         are equal, and ``1`` if ``left`` is greater than ``right``.
     180        Return ``-1`` if ``left`` is less than ``right``, ``0`` if
     181        ``left`` and ``right`` are equal, and ``1`` if ``left`` is
     182        greater than ``right``.
    182183
    183184        EXAMPLES::
    184185
    cdef class TropicalSemiringElement(RingE 
    195196            True
    196197            sage: T(4) <= T.infinity()
    197198            True
     199            sage: T = TropicalSemiring(QQ, False)
     200            sage: T(2) == T(2)
     201            True
     202            sage: T(2) != T(4)
     203            True
     204            sage: T(2) < T(4)
     205            True
     206            sage: T(2) > T(4)
     207            False
     208            sage: T.infinity() == T.infinity()
     209            True
     210            sage: T(4) <= T.infinity()
     211            False
    198212        """
    199213        cdef TropicalSemiringElement self, x
    200214        self = left
    cdef class TropicalSemiringElement(RingE 
    203217        if self._val is None:
    204218            if x._val is None:
    205219                return 0
    206             return 1
     220            if self.parent()._use_min:
     221                return 1
     222            else:
     223                return -1
    207224
    208225        if x._val is None:
    209             return -1
     226            if self.parent()._use_min:
     227                return -1
     228            else:
     229                return 1
    210230
    211231        if self._val < x._val:
    212232            return -1
    cdef class TropicalSemiringElement(RingE 
    216236
    217237    cpdef ModuleElement _add_(left, ModuleElement right):
    218238        """
    219         Add ``self`` to ``rhs``.
     239        Add ``left`` to ``right``.
    220240
    221241        EXAMPLES::
    222242
    cdef class TropicalSemiringElement(RingE 
    230250            sage: T = TropicalSemiring(QQ, False)
    231251            sage: T(2) + T(4)
    232252            4
     253            sage: T(2) + T.infinity()
     254            2
     255            sage: T.infinity() + T(2)
     256            2
    233257        """
    234258        cdef TropicalSemiringElement self, rhs
    235259        self = left
    cdef class TropicalSemiringElement(RingE 
    248272
    249273    def __neg__(self):
    250274        """
    251         Return the additive inverse which only exists for `+\infty`.
     275        Return the additive inverse, which only exists for `+\infty`.
    252276
    253277        EXAMPLES::
    254278
    cdef class TropicalSemiringElement(RingE 
    266290
    267291    cpdef RingElement _mul_(left, RingElement right):
    268292        """
    269         Multiply ``self`` and ``rhs``.
     293        Multiply ``left`` and ``right``.
    270294
    271295        EXAMPLES::
    272296
    cdef class TropicalSemiringElement(RingE 
    292316
    293317    cpdef RingElement _div_(left, RingElement right):
    294318        """
    295         Divide ``self`` by ``rhs``.
     319        Divide ``left`` by ``right``.
    296320
    297321        EXAMPLES::
    298322
    cdef class TropicalSemiringElement(RingE 
    317341
    318342    def __invert__(self):
    319343        """
    320         Return the inversion of ``self``.
     344        Return the multiplicative inverse of ``self``.
    321345
    322346        EXAMPLES::
    323347
    class TropicalSemiring(Parent, UniqueRep 
    376400    r"""
    377401    The tropical semiring.
    378402
    379     Given a base ring `R`, we define the tropical semiring `T = R \cup
    380     \{+\infty\}` by defining tropical addition and multiplication as follows:
     403    Given an ordered additive semigroup `R`, we define the tropical
     404    semiring `T = R \cup \{+\infty\}` by defining tropical addition
     405    and multiplication as follows:
    381406
    382407    .. MATH::
    383408
    384409        a \oplus b = \min(a, b), \quad \quad a \odot b = a + b.
    385410
    386     In particular, note that there are no (tropical) additive inverses and
    387     every element in `R` has a (tropical) multiplicative inverse.
     411    In particular, note that there are no (tropical) additive inverses
     412    (except of `\infty`), and every element in `R` has a (tropical)
     413    multiplicative inverse.
    388414
    389415    There is an alternative definition where we define `T = R \cup \{-\infty\}`
    390416    and alter tropical addition to be defined by
    class TropicalSemiring(Parent, UniqueRep 
    393419
    394420        a \oplus b = \max(a, b).
    395421
    396     To use the max definition, set the argument ``use_min = False``.
     422    To use this `\max` definition, set the argument ``use_min = False``.
    397423
    398424    .. WARNING::
    399425
    400426        :meth:`zero` and :meth:`one` refer to the tropical additive
    401         and multiplciative identities respectively.
     427        and multiplicative identities respectively.
     428       
     429        No verification is done to ensure `R` actually is an ordered
     430        semigroup, nor are sanity checks performed on exponentiation
     431        (e. g., ``T(2)^(-3/7)`` would return ``-6/7`` even if ``T``
     432        was defined as the tropical semiring over ``N``).
    402433
    403434    INPUT:
    404435
    405     - ``base`` -- The base ring
     436    - ``base`` -- The ordered additive semigroup `R`.
    406437    - ``use_min`` -- (Default: ``True``) If ``True``, then the semiring uses
    407438      `a \oplus b = \min(a, b)`; otherwise uses `a \oplus b = \max(a, b)`
    408439
    class TropicalSemiring(Parent, UniqueRep 
    431462        sage: T(2)^(-3/7)
    432463        -6/7
    433464
    434     Note that "zero" and "one" are the tropical equivalent. In other words,
     465    Note that "zero" and "one" are the additive and multiplicative
     466    identities of the tropical semiring. In other words,
    435467    they are **not** `0 \in R` and `1 \in R` respectively, but instead
    436468    the (tropical) additive and multiplicative identities `+\infty` and `0`
    437     respecitively::
     469    respectively::
    438470
    439471        sage: T.zero() + T(3) == T(3)
    440472        True
    class TropicalSemiring(Parent, UniqueRep 
    448480        TESTS::
    449481
    450482            sage: T = TropicalSemiring(QQ); T
    451             Tropical semiring over Rational Field
     483            Tropical semiring over Rational Field with min as addition
    452484            sage: TestSuite(T).run()
    453485        """
    454486        self._use_min = use_min
    class TropicalSemiring(Parent, UniqueRep 
    462494        EXAMPLES::
    463495
    464496            sage: TropicalSemiring(QQ)
    465             Tropical semiring over Rational Field
     497            Tropical semiring over Rational Field with min as addition
     498            sage: TropicalSemiring(QQ, False)
     499            Tropical semiring over Rational Field with max as addition
    466500        """
    467         return "Tropical semiring over %s"%self.base()
     501        cdef str minmax
     502        if self._use_min:
     503            minmax = "min"
     504        else:
     505            minmax = "max"
     506        return "Tropical semiring over %s with %s as addition" %(self.base(), minmax)
    468507
    469508    def _latex_(self):
    470509        r"""
    class TropicalSemiring(Parent, UniqueRep 
    473512        EXAMPLES::
    474513
    475514            sage: latex(TropicalSemiring(QQ))
    476             \Bold{T}
     515            \Bold{T} \left( \Bold{Q} \right)
    477516        """
    478         return "\\Bold{T}"
     517        return "\\Bold{T} \\left( " + self.base()._latex_() + " \\right)"
    479518
    480519    def _coerce_map_from_(self, S):
    481520        """
    class TropicalSemiring(Parent, UniqueRep 
    515554            Traceback (most recent call last):
    516555            ...
    517556            TypeError: no canonical coercion from Tropical semiring over
    518              Real Field with 53 bits of precision to Tropical semiring over Rational Field
     557             Real Field with 53 bits of precision with min as addition
     558             to Tropical semiring over Rational Field with min as addition
    519559        """
    520560        if isinstance(S, TropicalSemiring) and self._use_min == S._use_min \
    521561                and self.base().has_coerce_map_from(S.base()):
    class TropicalSemiring(Parent, UniqueRep 
    584624
    585625cdef class TropicalToTropical(Map):
    586626    """
    587     Map from the tropical semiring to itself (possibly with different bases).
    588     Used in coercion.
     627    Map between two tropical semirings. Used in coercion.
    589628    """
    590629    cpdef TropicalSemiringElement _call_(self, x):
    591630        """