# 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 import operator cdef class TropicalSemiringElement(RingElement): r""" An element in the tropical semiring. Either in R or \infty. An element in the tropical semiring over an ordered additive semigroup R. Either in R or \infty. Operators +, \cdot are defined as tropical operators \oplus, \odot respectively. """ cdef class TropicalSemiringElement(RingE sage: elt = T(2) sage: elt.__reduce__() (, (Tropical semiring over Rational Field, 2)) (Tropical semiring over Rational Field with min as addition, 2)) """ return (TropicalSemiringElement, (self.parent(), self._val)) cdef class TropicalSemiringElement(RingE cdef int _cmp_c_impl(left, Element right) except -2: """ Return -1 if exactly one of the numbers is NaN.  Return -1 if left is less than right, 0 if left and right are equal, and 1 if left is greater than right. Return -1 if left is less than right, 0 if left and right are equal, and 1 if left is greater than right. EXAMPLES:: cdef class TropicalSemiringElement(RingE True sage: T(4) <= T.infinity() True sage: T = TropicalSemiring(QQ, False) sage: T(2) == T(2) True sage: T(2) != T(4) True sage: T(2) < T(4) True sage: T(2) > T(4) False sage: T.infinity() == T.infinity() True sage: T(4) <= T.infinity() False """ cdef TropicalSemiringElement self, x self = left cdef class TropicalSemiringElement(RingE if self._val is None: if x._val is None: return 0 return 1 if self.parent()._use_min: return 1 else: return -1 if x._val is None: return -1 if self.parent()._use_min: return -1 else: return 1 if self._val < x._val: return -1 cdef class TropicalSemiringElement(RingE cpdef ModuleElement _add_(left, ModuleElement right): """ Add self to rhs. Add left to right. EXAMPLES:: cdef class TropicalSemiringElement(RingE sage: T = TropicalSemiring(QQ, False) sage: T(2) + T(4) 4 sage: T(2) + T.infinity() 2 sage: T.infinity() + T(2) 2 """ cdef TropicalSemiringElement self, rhs self = left cdef class TropicalSemiringElement(RingE def __neg__(self): """ Return the additive inverse which only exists for +\infty. Return the additive inverse, which only exists for +\infty. EXAMPLES:: cdef class TropicalSemiringElement(RingE cpdef RingElement _mul_(left, RingElement right): """ Multiply self and rhs. Multiply left and right. EXAMPLES:: cdef class TropicalSemiringElement(RingE cpdef RingElement _div_(left, RingElement right): """ Divide self by rhs. Divide left by right. EXAMPLES:: cdef class TropicalSemiringElement(RingE def __invert__(self): """ Return the inversion of self. Return the multiplicative inverse of self. EXAMPLES:: class TropicalSemiring(Parent, UniqueRep r""" The tropical semiring. Given a base ring R, we define the tropical semiring T = R \cup \{+\infty\} by defining tropical addition and multiplication as follows: Given an ordered additive semigroup R, we define the tropical semiring T = R \cup \{+\infty\} by defining tropical addition and multiplication as follows: .. MATH:: a \oplus b = \min(a, b), \quad \quad a \odot b = a + b. In particular, note that there are no (tropical) additive inverses and every element in R has a (tropical) multiplicative inverse. In particular, note that there are no (tropical) additive inverses (except of \infty), and every element in R has a (tropical) multiplicative inverse. There is an alternative definition where we define T = R \cup \{-\infty\} and alter tropical addition to be defined by class TropicalSemiring(Parent, UniqueRep a \oplus b = \max(a, b). To use the max definition, set the argument use_min = False. To use this \max definition, set the argument use_min = False. .. WARNING:: :meth:zero and :meth:one refer to the tropical additive and multiplciative identities respectively. and multiplicative identities respectively. No verification is done to ensure R actually is an ordered semigroup, nor are sanity checks performed on exponentiation (e. g., T(2)^(-3/7) would return -6/7 even if T was defined as the tropical semiring over N). INPUT: - base -- The base ring - base -- The ordered additive semigroup R. - use_min -- (Default: True) If True, then the semiring uses a \oplus b = \min(a, b); otherwise uses a \oplus b = \max(a, b) class TropicalSemiring(Parent, UniqueRep sage: T(2)^(-3/7) -6/7 Note that "zero" and "one" are the tropical equivalent. In other words, Note that "zero" and "one" are the additive and multiplicative identities of the tropical semiring. In other words, they are **not** 0 \in R and 1 \in R respectively, but instead the (tropical) additive and multiplicative identities +\infty and 0 respecitively:: respectively:: sage: T.zero() + T(3) == T(3) True class TropicalSemiring(Parent, UniqueRep TESTS:: sage: T = TropicalSemiring(QQ); T Tropical semiring over Rational Field Tropical semiring over Rational Field with min as addition sage: TestSuite(T).run() """ self._use_min = use_min class TropicalSemiring(Parent, UniqueRep EXAMPLES:: sage: TropicalSemiring(QQ) Tropical semiring over Rational Field Tropical semiring over Rational Field with min as addition sage: TropicalSemiring(QQ, False) Tropical semiring over Rational Field with max as addition """ return "Tropical semiring over %s"%self.base() cdef str minmax if self._use_min: minmax = "min" else: minmax = "max" return "Tropical semiring over %s with %s as addition" %(self.base(), minmax) def _latex_(self): r""" class TropicalSemiring(Parent, UniqueRep EXAMPLES:: sage: latex(TropicalSemiring(QQ)) \Bold{T} \Bold{T} \left( \Bold{Q} \right) """ return "\\Bold{T}" return "\\Bold{T} \\left( " + self.base()._latex_() + " \\right)" def _coerce_map_from_(self, S): """ class TropicalSemiring(Parent, UniqueRep Traceback (most recent call last): ... TypeError: no canonical coercion from Tropical semiring over Real Field with 53 bits of precision to Tropical semiring over Rational Field Real Field with 53 bits of precision with min as addition to Tropical semiring over Rational Field with min as addition """ if isinstance(S, TropicalSemiring) and self._use_min == S._use_min \ and self.base().has_coerce_map_from(S.base()): class TropicalSemiring(Parent, UniqueRep cdef class TropicalToTropical(Map): """ Map from the tropical semiring to itself (possibly with different bases). Used in coercion. Map between two tropical semirings. Used in coercion. """ cpdef TropicalSemiringElement _call_(self, x): """