Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Trac #18578: Add special function __truediv__() for Py3
Browse files Browse the repository at this point in the history
In Python 3 the division operator "/" calls the special function
__truediv__() instead of __div__() (as in Py2).
This change defines __truediv__() and assigns __div __ = __truediv__
to prepare for both "from __future__ import division" and Python 3.
  • Loading branch information
wluebbe committed Jun 2, 2015
1 parent f9ab830 commit 9701674
Show file tree
Hide file tree
Showing 20 changed files with 60 additions and 60 deletions.
4 changes: 3 additions & 1 deletion src/sage/algebras/weyl_algebra.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ def list(self):

# This is essentially copied from
# sage.combinat.free_module.CombinatorialFreeModuleElement
def __div__(self, x, self_on_left=False):
def __truediv__(self, x, self_on_left=False):
"""
Division by coefficients.
Expand All @@ -463,6 +463,8 @@ def __div__(self, x, self_on_left=False):
return self.__class__(F, D)

return self.__class__(F, {t: _divide_if_possible(D[t], x) for t in D})
# for Python 2 without from __future__ import division
__div__ = __truediv__

class DifferentialWeylAlgebra(Algebra, UniqueRepresentation):
r"""
Expand Down
4 changes: 3 additions & 1 deletion src/sage/categories/rings.py
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ def quotient_ring(self, I, names=None):
"""
return self.quotient(I,names=names)

def __div__(self, I):
def __truediv__(self, I):
"""
Since assigning generator names would not work properly,
the construction of a quotient ring using division syntax
Expand All @@ -703,6 +703,8 @@ def __div__(self, I):
TypeError: Use self.quo(I) or self.quotient(I) to construct the quotient ring.
"""
raise TypeError("Use self.quo(I) or self.quotient(I) to construct the quotient ring.")
# for Python 2 without from __future__ import division
__div__ = __truediv__

def __getitem__(self, arg):
"""
Expand Down
2 changes: 2 additions & 0 deletions src/sage/combinat/binary_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -2473,6 +2473,8 @@ def over(self, bt):
return B([self[0], self[1].over(bt)])

__div__ = over
# for Python 3 and for Python 2 with from __future__ import division
__truediv__ = over

@combinatorial_map(name="Under operation on Binary Trees")
def under(self, bt):
Expand Down
4 changes: 3 additions & 1 deletion src/sage/combinat/free_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -918,7 +918,7 @@ def _acted_upon_(self, scalar, self_on_left = False):
_lmul_ = _acted_upon_
_rmul_ = _acted_upon_

def __div__(self, x, self_on_left=False ):
def __truediv__(self, x, self_on_left=False ):
"""
Division by coefficients
Expand Down Expand Up @@ -950,6 +950,8 @@ def __div__(self, x, self_on_left=False ):
return F._from_dict( D, remove_zeros=False )
else:
return self.map_coefficients(lambda c: _divide_if_possible(c, x))
# for Python 2 without from __future__ import division
__div__ = __truediv__

def _divide_if_possible(x, y):
"""
Expand Down
4 changes: 3 additions & 1 deletion src/sage/combinat/partition.py
Original file line number Diff line number Diff line change
Expand Up @@ -1075,7 +1075,7 @@ def pp(self):
"""
print self.ferrers_diagram()

def __div__(self, p):
def __truediv__(self, p):
"""
Returns the skew partition ``self / p``.
Expand All @@ -1097,6 +1097,8 @@ def __div__(self, p):
raise ValueError("To form a skew partition p/q, q must be contained in p.")

return SkewPartition([self[:], p])
# for Python 2 without from __future__ import division
__div__ = __truediv__

def power(self, k):
r"""
Expand Down
4 changes: 3 additions & 1 deletion src/sage/combinat/tableau.py
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ def _latex_diagram(self):
from output import tex_from_array
return tex_from_array(self)

def __div__(self, t):
def __truediv__(self, t):
"""
Return the skew tableau ``self``/``t``, where ``t`` is a partition
contained in the shape of ``self``.
Expand Down Expand Up @@ -734,6 +734,8 @@ def __div__(self, t):

from sage.combinat.skew_tableau import SkewTableau
return SkewTableau(st)
# for Python 2 without from __future__ import division
__div__ = __truediv__

def __call__(self, *cell):
r"""
Expand Down
4 changes: 3 additions & 1 deletion src/sage/geometry/polyhedron/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2546,7 +2546,7 @@ def __neg__(self):
"""
return self.dilation(-1)

def __div__(self, scalar):
def __truediv__(self, scalar):
"""
Divide by a scalar factor.
Expand All @@ -2559,6 +2559,8 @@ def __div__(self, scalar):
(A vertex at (2/5, 4/5, 8/5), A vertex at (3/5, 9/5, 27/5))
"""
return self.dilation(1/scalar)
# for Python 2 without from __future__ import division
__div__ = __truediv__

@coerce_binop
def convex_hull(self, other):
Expand Down
2 changes: 2 additions & 0 deletions src/sage/groups/free_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -905,3 +905,5 @@ def quotient(self, relations):
return FinitelyPresentedGroup(self, tuple(map(self, relations) ) )

__div__ = quotient
# for Python 3 and for Python 2 with from __future__ import division
__truediv__ = quotient
4 changes: 3 additions & 1 deletion src/sage/misc/sage_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -1520,7 +1520,7 @@ def __mul__(self, other):
"""
return self._sie_binop('*', other)

def __div__(self, other):
def __truediv__(self, other):
r"""
Compute an expression tree for ``self / other``.
Expand All @@ -1533,6 +1533,8 @@ def __div__(self, other):
{binop:/ {atomic:3} {atomic:4}}
"""
return self._sie_binop('/', other)
# for Python 2 without from __future__ import division
__div__ = __truediv__

def __add__(self, other):
r"""
Expand Down
4 changes: 3 additions & 1 deletion src/sage/modular/abvar/abvar.py
Original file line number Diff line number Diff line change
Expand Up @@ -977,7 +977,7 @@ def quotient(self, other):
"""
return self.__div__(other)

def __div__(self, other):
def __truediv__(self, other):
"""
Compute the quotient of self and other, where other is either an
abelian subvariety of self or a finite subgroup of self.
Expand Down Expand Up @@ -1036,6 +1036,8 @@ def __div__(self, other):
return self._quotient_by_abelian_subvariety(other)
else:
raise TypeError("other must be a subgroup or abelian subvariety")
# for Python 2 without from __future__ import division
__div__ = __truediv__

def degeneracy_map(self, M_ls, t_ls):
"""
Expand Down
4 changes: 3 additions & 1 deletion src/sage/modules/fg_pid/fgp_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ def _repr_(self):
I = str(self.invariants()).replace(',)',')')
return "Finitely generated module V/W over %s with invariants %s"%(self.base_ring(), I)

def __div__(self, other):
def __truediv__(self, other):
"""
Return the quotient self/other, where other must be a
submodule of self.
Expand All @@ -463,6 +463,8 @@ def __div__(self, other):
if not other.is_submodule(self):
raise ValueError("other must be a submodule of self")
return self._module_constructor(self._V, other._V+self._W)
# for Python 2 without from __future__ import division
__div__ = __truediv__

def __eq__(self, other):
"""
Expand Down
8 changes: 6 additions & 2 deletions src/sage/modules/free_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -2984,7 +2984,7 @@ def quotient(self, sub, check=True):
else:
raise NotImplementedError("quotients of modules over rings other than fields or ZZ is not fully implemented")

def __div__(self, sub, check=True):
def __truediv__(self, sub, check=True):
"""
Return the quotient of self by the given submodule sub.
Expand All @@ -3000,6 +3000,8 @@ def __div__(self, sub, check=True):
Finitely generated module V/W over Integer Ring with invariants (4, 12)
"""
return self.quotient(sub, check)
# for Python 2 without from __future__ import division
__div__ = __truediv__

class FreeModule_generic_field(FreeModule_generic_pid):
"""
Expand Down Expand Up @@ -3821,7 +3823,7 @@ def linear_dependence(self, vectors, zeros='left', check=True):
A = sage.matrix.constructor.matrix(vectors) # as rows, so get left kernel
return A.left_kernel(basis=basis).basis()

def __div__(self, sub, check=True):
def __truediv__(self, sub, check=True):
"""
Return the quotient of self by the given subspace sub.
Expand All @@ -3847,6 +3849,8 @@ def __div__(self, sub, check=True):
(0.0)
"""
return self.quotient(sub, check)
# for Python 2 without from __future__ import division
__div__ = __truediv__

def quotient(self, sub, check=True):
"""
Expand Down
31 changes: 3 additions & 28 deletions src/sage/plot/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,7 @@ def __rmul__(self, left):
"""
return self * left

def __div__(self, right):
def __truediv__(self, right):
"""
Return a color whose RGB coordinates are this color's
coordinates divided by a scalar. This method is called for
Expand Down Expand Up @@ -812,33 +812,8 @@ def __div__(self, right):
TypeError: float() argument must be a string or a number
"""
return self * (float(1.0) / float(right))

def __truediv__(self, right):
"""
Return a color whose RGB coordinates are this color's
coordinates divided by a scalar. This method is called for
"true division."
INPUT:
- ``right`` - a float-convertible, non-zero number
OUTPUT:
- a **new** instance of :class:`Color`
EXAMPLES::
sage: from __future__ import division
sage: from sage.plot.colors import yellow, gold
sage: yellow / 4
RGB color (0.25, 0.25, 0.0)
sage: yellow.__truediv__(4)
RGB color (0.25, 0.25, 0.0)
sage: gold / pi + yellow * e
RGB color (0.51829585732141..., 0.49333037605210..., 0.0)
"""
return self.__div__(right)
# for Python 2 without from __future__ import division
__div__ = __truediv__

def __int__(self):
"""
Expand Down
17 changes: 2 additions & 15 deletions src/sage/quivers/representation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1501,21 +1501,6 @@ def _repr_(self):
"""
return "Representation with dimension vector {}".format(self.dimension_vector())

def __div__(self, sub):
"""
This and :meth:`__truediv__` below together overload the ``/``
operator.
TESTS::
sage: Q = DiGraph({1:{2:['a']}}).path_semigroup()
sage: P = Q.P(GF(3), 1)
sage: R = P.radical()
sage: (P/R).is_simple()
True
"""
return self.quotient(sub)

def __truediv__(self, sub):
"""
This and :meth:`__div__` above together overload the ``/`` operator.
Expand All @@ -1529,6 +1514,8 @@ def __truediv__(self, sub):
True
"""
return self.quotient(sub)
# for Python 2 without from __future__ import division
__div__ = __truediv__

def _submodule(self, spaces={}):
"""
Expand Down
4 changes: 3 additions & 1 deletion src/sage/rings/number_field/number_field_ideal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1802,7 +1802,7 @@ def prime_factors(self):
"""
return [x[0] for x in self.factor()]

def __div__(self, other):
def __truediv__(self, other):
"""
Return the quotient self / other.
Expand All @@ -1818,6 +1818,8 @@ def __div__(self, other):
True
"""
return self * other.__invert__()
# for Python 2 without from __future__ import division
__div__ = __truediv__

def __invert__(self):
"""
Expand Down
4 changes: 3 additions & 1 deletion src/sage/schemes/generic/scheme.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ def _point_homset(self, *args, **kwds):
"""
raise NotImplementedError

def __div__(self, Y):
def __truediv__(self, Y):
"""
Return the base extension of self to Y.
Expand All @@ -394,6 +394,8 @@ def __div__(self, Y):
Affine Space of dimension 3 over Finite Field of size 7
"""
return self.base_extend(Y)
# for Python 2 without from __future__ import division
__div__ = __truediv__

def base_ring(self):
"""
Expand Down
4 changes: 3 additions & 1 deletion src/sage/schemes/toric/chow_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -786,7 +786,7 @@ def _rational_equivalence_relations(self, V):
return V.span(relations)


def __div__(self, other):
def __truediv__(self, other):
r"""
Return the quotient of the Chow group by a subgroup.
Expand All @@ -804,6 +804,8 @@ def __div__(self, other):
NotImplementedError: Quotients of the Chow group are not implemented.
"""
raise NotImplementedError('Quotients of the Chow group are not implemented.')
# for Python 2 without from __future__ import division
__div__ = __truediv__


def _repr_(self):
Expand Down
4 changes: 3 additions & 1 deletion src/sage/structure/factorization.py
Original file line number Diff line number Diff line change
Expand Up @@ -1143,7 +1143,7 @@ def __invert__(self):
return Factorization([(p,-e) for p,e in reversed(self)],
cr=self._cr(), unit=self.unit()**(-1))

def __div__(self, other):
def __truediv__(self, other):
r"""
Return the quotient of two factorizations, which is obtained by
multiplying the first by the inverse of the second.
Expand All @@ -1166,6 +1166,8 @@ def __div__(self, other):
if not isinstance(other, Factorization):
return self / Factorization([(other, 1)])
return self * other**-1
# for Python 2 without from __future__ import division
__div__ = __truediv__

def value(self):
"""
Expand Down
4 changes: 3 additions & 1 deletion src/sage/tensor/modules/comp.py
Original file line number Diff line number Diff line change
Expand Up @@ -1492,7 +1492,7 @@ def __rmul__(self, other):
return result


def __div__(self, other):
def __truediv__(self, other):
r"""
Division (by a scalar).
Expand All @@ -1518,6 +1518,8 @@ def __div__(self, other):
for ind, val in self._comp.iteritems():
result._comp[ind] = val / other
return result
# for Python 2 without from __future__ import division
__div__ = __truediv__

def trace(self, pos1, pos2):
r"""
Expand Down
4 changes: 3 additions & 1 deletion src/sage/tensor/modules/free_module_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1817,7 +1817,7 @@ def __mul__(self, other):
return result


def __div__(self, other):
def __truediv__(self, other):
r"""
Division (by a scalar).
Expand All @@ -1843,6 +1843,8 @@ def __div__(self, other):
for basis in self._components:
result._components[basis] = self._components[basis] / other
return result
# for Python 2 without from __future__ import division
__div__ = __truediv__


def __call__(self, *args):
Expand Down

0 comments on commit 9701674

Please sign in to comment.