Skip to content

Commit

Permalink
Trac #21448: Avoid underscored arithmetic methods in Python
Browse files Browse the repository at this point in the history
In categories, it is better to write `x + y` instead of `x._add_(y)`
since the latter can be a lot slower if `_add_` is implemented in
Cython.

We also remove several redundant implementations of `_sub_` where they
coincide with the default implementation from `ModuleElement`.

URL: https://trac.sagemath.org/21448
Reported by: jdemeyer
Ticket author(s): Jeroen Demeyer
Reviewer(s): Nicolas M. Thiéry
  • Loading branch information
Release Manager authored and vbraun committed Oct 3, 2016
2 parents 1a20fd9 + ca0f7ba commit 2a67d26
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 73 deletions.
4 changes: 2 additions & 2 deletions src/sage/categories/additive_magmas.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ def summation(self, x, y):
.. TODO:: Add an example.
"""
return x._add_(y)
return x + y

summation_from_element_class_add = summation

Expand Down Expand Up @@ -819,7 +819,7 @@ def _sub_(left, right):
sage: C.one() - C.one()
(0, 0)
"""
return left._add_(-right)
return left + (-right)

def __neg__(self):
"""
Expand Down
4 changes: 2 additions & 2 deletions src/sage/categories/magmas.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ def _div_(left, right):
sage: c1._div_.__module__
'sage.categories.magmas'
"""
return left._mul_(~right)
return left * ~right

class SubcategoryMethods:

Expand Down Expand Up @@ -785,7 +785,7 @@ def product(self, x, y):
From: (S x S)
To: S
"""
return x._mul_(y)
return x * y

product_from_element_class_mul = product

Expand Down
26 changes: 6 additions & 20 deletions src/sage/rings/fraction_field_element.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,12 @@ cdef class FractionFieldElement(FieldElement):
(y + 1)/(x*y)
sage: Frac(CDF['x']).gen() + 3
x + 3.0
Subtraction is implemented by adding the negative::
sage: K.<t> = Frac(GF(7)['t'])
sage: t - 1/t # indirect doctest
(t^2 + 6)/t
"""
rnum = self.__numerator
rden = self.__denominator
Expand Down Expand Up @@ -563,26 +569,6 @@ cdef class FractionFieldElement(FieldElement):
return self.__class__(self._parent, rnum*sden + rden*snum, rden*sden,
coerce=False, reduce=False)

cpdef _sub_(self, right):
"""
Computes the difference of ``self`` and ``right``.
INPUT:
- ``right`` - ``ModuleElement`` to subtract from ``self``
OUTPUT:
- Difference of ``self`` and ``right``
EXAMPLES::
sage: K.<t> = Frac(GF(7)['t'])
sage: t - 1/t # indirect doctest
(t^2 + 6)/t
"""
return self._add_(-right)

cpdef _mul_(self, right):
"""
Computes the product of ``self`` and ``right``.
Expand Down
32 changes: 14 additions & 18 deletions src/sage/rings/infinity.py
Original file line number Diff line number Diff line change
Expand Up @@ -1281,6 +1281,20 @@ def _add_(self, other):
Traceback (most recent call last):
...
SignError: cannot add positive finite value to negative finite value
Subtraction is implemented by adding the negative::
sage: P = InfinityRing
sage: 4 - oo # indirect doctest
-Infinity
sage: 5 - -oo
+Infinity
sage: P(44) - P(4)
Traceback (most recent call last):
...
SignError: cannot add positive finite value to negative finite value
sage: P(44) - P(-1)
A positive finite number
"""
if isinstance(other, InfinityElement):
return other
Expand Down Expand Up @@ -1343,24 +1357,6 @@ def _div_(self, other):
"""
return self * ~other

def _sub_(self, other):
"""
EXAMPLES::
sage: P = InfinityRing
sage: 4 - oo # indirect doctest
-Infinity
sage: 5 - -oo
+Infinity
sage: P(44) - P(4)
Traceback (most recent call last):
...
SignError: cannot add positive finite value to negative finite value
sage: P(44) - P(-1)
A positive finite number
"""
return self._add_(-other)

def __invert__(self):
"""
EXAMPLES::
Expand Down
8 changes: 4 additions & 4 deletions src/sage/rings/universal_cyclotomic_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,7 @@ def _add_(self, other):
1/2*E(3) - 1/2*E(3)^2
"""
P = self.parent()
return P.element_class(P, self._obj._add_(other._obj))
return P.element_class(P, self._obj + other._obj)

def _sub_(self, other):
r"""
Expand All @@ -839,7 +839,7 @@ def _sub_(self, other):
-E(15)^2 - E(15)^11 + E(15)^13 - E(15)^14
"""
P = self.parent()
return P.element_class(P, self._obj._sub_(other._obj))
return P.element_class(P, self._obj - other._obj)

def __neg__(self):
r"""
Expand All @@ -865,7 +865,7 @@ def _mul_(self, other):
3*E(4)
"""
P = self.parent()
return P.element_class(P, self._obj._mul_(other._obj))
return P.element_class(P, self._obj * other._obj)

def _div_(self, other):
r"""
Expand All @@ -878,7 +878,7 @@ def _div_(self, other):
"""
P = self.parent()
try:
return P.element_class(P, self._obj._div_(other._obj))
return P.element_class(P, self._obj / other._obj)
except ValueError:
raise ZeroDivisionError("division by zero")

Expand Down
44 changes: 17 additions & 27 deletions src/sage/tensor/differential_form_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -651,31 +651,7 @@ def _add_(self, other):
...
TypeError: Cannot add forms of degree 1 and 2
"""

if self.is_zero():
return other
if other.is_zero():
return self

if self._degree != other._degree:
raise TypeError("Cannot add forms of degree %s and %s" % \
(self._degree, other._degree))

sumform = DifferentialForm(self.parent(), self._degree)
sumform._components = self._components.copy()
for comp, fun in other._components.items():
sumform[comp] += fun

sumform._cleanup()
return sumform


def _sub_(self, other):
r"""
Subtract other from self.
EXAMPLES::
Subtraction is implemented by adding the negative::
sage: x, y, z = var('x, y, z')
sage: F = DifferentialForms()
Expand All @@ -699,10 +675,24 @@ def _sub_(self, other):
Traceback (most recent call last):
...
TypeError: Cannot add forms of degree 1 and 2
"""
return self._add_(-other)

if self.is_zero():
return other
if other.is_zero():
return self

if self._degree != other._degree:
raise TypeError("Cannot add forms of degree %s and %s" % \
(self._degree, other._degree))

sumform = DifferentialForm(self.parent(), self._degree)
sumform._components = self._components.copy()
for comp, fun in other._components.items():
sumform[comp] += fun

sumform._cleanup()
return sumform

def _cleanup(self):
r"""
Expand Down

0 comments on commit 2a67d26

Please sign in to comment.