# 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 
35  35  
36  36  cdef class TropicalSemiringElement(RingElement): 
37  37  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`. 
39  40  Operators `+, \cdot` are defined as tropical operators `\oplus, \odot` 
40  41  respectively. 
41  42  """ 
… 
… 
cdef class TropicalSemiringElement(RingE 
74  75  sage: elt = T(2) 
75  76  sage: elt.__reduce__() 
76  77  (<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)) 
78  79  """ 
79  80  return (TropicalSemiringElement, (self.parent(), self._val)) 
80  81  
… 
… 
cdef class TropicalSemiringElement(RingE 
176  177  
177  178  cdef int _cmp_c_impl(left, Element right) except 2: 
178  179  """ 
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``. 
182  183  
183  184  EXAMPLES:: 
184  185  
… 
… 
cdef class TropicalSemiringElement(RingE 
195  196  True 
196  197  sage: T(4) <= T.infinity() 
197  198  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 
198  212  """ 
199  213  cdef TropicalSemiringElement self, x 
200  214  self = left 
… 
… 
cdef class TropicalSemiringElement(RingE 
203  217  if self._val is None: 
204  218  if x._val is None: 
205  219  return 0 
206   return 1 
 220  if self.parent()._use_min: 
 221  return 1 
 222  else: 
 223  return 1 
207  224  
208  225  if x._val is None: 
209   return 1 
 226  if self.parent()._use_min: 
 227  return 1 
 228  else: 
 229  return 1 
210  230  
211  231  if self._val < x._val: 
212  232  return 1 
… 
… 
cdef class TropicalSemiringElement(RingE 
216  236  
217  237  cpdef ModuleElement _add_(left, ModuleElement right): 
218  238  """ 
219   Add ``self`` to ``rhs``. 
 239  Add ``left`` to ``right``. 
220  240  
221  241  EXAMPLES:: 
222  242  
… 
… 
cdef class TropicalSemiringElement(RingE 
230  250  sage: T = TropicalSemiring(QQ, False) 
231  251  sage: T(2) + T(4) 
232  252  4 
 253  sage: T(2) + T.infinity() 
 254  2 
 255  sage: T.infinity() + T(2) 
 256  2 
233  257  """ 
234  258  cdef TropicalSemiringElement self, rhs 
235  259  self = left 
… 
… 
cdef class TropicalSemiringElement(RingE 
248  272  
249  273  def __neg__(self): 
250  274  """ 
251   Return the additive inverse which only exists for `+\infty`. 
 275  Return the additive inverse, which only exists for `+\infty`. 
252  276  
253  277  EXAMPLES:: 
254  278  
… 
… 
cdef class TropicalSemiringElement(RingE 
266  290  
267  291  cpdef RingElement _mul_(left, RingElement right): 
268  292  """ 
269   Multiply ``self`` and ``rhs``. 
 293  Multiply ``left`` and ``right``. 
270  294  
271  295  EXAMPLES:: 
272  296  
… 
… 
cdef class TropicalSemiringElement(RingE 
292  316  
293  317  cpdef RingElement _div_(left, RingElement right): 
294  318  """ 
295   Divide ``self`` by ``rhs``. 
 319  Divide ``left`` by ``right``. 
296  320  
297  321  EXAMPLES:: 
298  322  
… 
… 
cdef class TropicalSemiringElement(RingE 
317  341  
318  342  def __invert__(self): 
319  343  """ 
320   Return the inversion of ``self``. 
 344  Return the multiplicative inverse of ``self``. 
321  345  
322  346  EXAMPLES:: 
323  347  
… 
… 
class TropicalSemiring(Parent, UniqueRep 
376  400  r""" 
377  401  The tropical semiring. 
378  402  
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: 
381  406  
382  407  .. MATH:: 
383  408  
384  409  a \oplus b = \min(a, b), \quad \quad a \odot b = a + b. 
385  410  
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. 
388  414  
389  415  There is an alternative definition where we define `T = R \cup \{\infty\}` 
390  416  and alter tropical addition to be defined by 
… 
… 
class TropicalSemiring(Parent, UniqueRep 
393  419  
394  420  a \oplus b = \max(a, b). 
395  421  
396   To use the max definition, set the argument ``use_min = False``. 
 422  To use this `\max` definition, set the argument ``use_min = False``. 
397  423  
398  424  .. WARNING:: 
399  425  
400  426  :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``). 
402  433  
403  434  INPUT: 
404  435  
405    ``base``  The base ring 
 436   ``base``  The ordered additive semigroup `R`. 
406  437   ``use_min``  (Default: ``True``) If ``True``, then the semiring uses 
407  438  `a \oplus b = \min(a, b)`; otherwise uses `a \oplus b = \max(a, b)` 
408  439  
… 
… 
class TropicalSemiring(Parent, UniqueRep 
431  462  sage: T(2)^(3/7) 
432  463  6/7 
433  464  
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, 
435  467  they are **not** `0 \in R` and `1 \in R` respectively, but instead 
436  468  the (tropical) additive and multiplicative identities `+\infty` and `0` 
437   respecitively:: 
 469  respectively:: 
438  470  
439  471  sage: T.zero() + T(3) == T(3) 
440  472  True 
… 
… 
class TropicalSemiring(Parent, UniqueRep 
448  480  TESTS:: 
449  481  
450  482  sage: T = TropicalSemiring(QQ); T 
451   Tropical semiring over Rational Field 
 483  Tropical semiring over Rational Field with min as addition 
452  484  sage: TestSuite(T).run() 
453  485  """ 
454  486  self._use_min = use_min 
… 
… 
class TropicalSemiring(Parent, UniqueRep 
462  494  EXAMPLES:: 
463  495  
464  496  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 
466  500  """ 
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) 
468  507  
469  508  def _latex_(self): 
470  509  r""" 
… 
… 
class TropicalSemiring(Parent, UniqueRep 
473  512  EXAMPLES:: 
474  513  
475  514  sage: latex(TropicalSemiring(QQ)) 
476   \Bold{T} 
 515  \Bold{T} \left( \Bold{Q} \right) 
477  516  """ 
478   return "\\Bold{T}" 
 517  return "\\Bold{T} \\left( " + self.base()._latex_() + " \\right)" 
479  518  
480  519  def _coerce_map_from_(self, S): 
481  520  """ 
… 
… 
class TropicalSemiring(Parent, UniqueRep 
515  554  Traceback (most recent call last): 
516  555  ... 
517  556  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 
519  559  """ 
520  560  if isinstance(S, TropicalSemiring) and self._use_min == S._use_min \ 
521  561  and self.base().has_coerce_map_from(S.base()): 
… 
… 
class TropicalSemiring(Parent, UniqueRep 
584  624  
585  625  cdef class TropicalToTropical(Map): 
586  626  """ 
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. 
589  628  """ 
590  629  cpdef TropicalSemiringElement _call_(self, x): 
591  630  """ 