Skip to content

Commit

Permalink
Trac #27429: some little code cleanup
Browse files Browse the repository at this point in the history
in two files, one about polyhedron, the other about toric varieties

various changes, for cleanness, speed or pep8

URL: https://trac.sagemath.org/27429
Reported by: chapoton
Ticket author(s): Frédéric Chapoton
Reviewer(s): Travis Scrimshaw
  • Loading branch information
Release Manager authored and vbraun committed Mar 5, 2019
2 parents 5b5d06a + 86fabb7 commit 063963f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 54 deletions.
44 changes: 18 additions & 26 deletions src/sage/geometry/polyhedron/misc.py
@@ -1,26 +1,19 @@
r"""
Miscellaneous helper functions
"""


########################################################################
# **********************************************************************
# Copyright (C) 2008 Marshall Hampton <hamptonio@gmail.com>
# Copyright (C) 2011 Volker Braun <vbraun.name@gmail.com>
#
# Distributed under the terms of the GNU General Public License (GPL)
#
# http://www.gnu.org/licenses/
########################################################################




# https://www.gnu.org/licenses/
# **********************************************************************


#########################################################################
def _to_space_separated_string(l):
"""
Converts a container to a space-separated string.
Convert a container to a space-separated string.
INPUT:
Expand All @@ -36,14 +29,9 @@ def _to_space_separated_string(l):
sage: P._to_space_separated_string([2,3])
'2 3'
"""
s = '';
for x in l:
if len(s)>0: s += ' '
s += repr(x)
return s
return ' '.join(repr(x) for x in l)


#########################################################################
def _set_to_None_if_empty(x):
"""
Helper function to clean up arguments: Returns None if x==None or
Expand All @@ -57,13 +45,14 @@ def _set_to_None_if_empty(x):
sage: P._set_to_None_if_empty([1])
[1]
"""
if x is None: return x
if x is None:
return x
x = list(x)
if len(x)==0: return None
if not x:
return None
return x


#########################################################################
def _make_listlist(x):
"""
Helper function to clean up arguments.
Expand All @@ -86,11 +75,11 @@ def _make_listlist(x):
sage: P._make_listlist([(1,2),[3,4]])
[[1, 2], [3, 4]]
"""
if x is None: return []
if x is None:
return []
return [list(y) for y in x]


#########################################################################
def _common_length_of(l1, l2=None, l3=None):
"""
The arguments are containers or ``None``. The function applies
Expand All @@ -107,10 +96,13 @@ def _common_length_of(l1, l2=None, l3=None):
sage: P._common_length_of([[1,2,3],[1,3,34]])
(2, 3)
"""
args = [];
if l1 is not None: args.append(l1)
if l2 is not None: args.append(l2)
if l3 is not None: args.append(l3)
args = []
if l1 is not None:
args.append(l1)
if l2 is not None:
args.append(l2)
if l3 is not None:
args.append(l3)

length = None
num = 0
Expand Down
50 changes: 22 additions & 28 deletions src/sage/schemes/toric/chow_group.py
Expand Up @@ -69,7 +69,7 @@
(Z, C7, C2 x C2 x Z^5, Z)
sage: A.degree(2).ngens()
7
sage: a = sum( A.gen(i) * (i+1) for i in range(0,A.ngens()) ) # an element of A
sage: a = sum( A.gen(i) * (i+1) for i in range(A.ngens()) ) # an element of A
sage: a # long time (2s on sage.math, 2011)
( 3 | 1 mod 7 | 0 mod 2, 1 mod 2, 4, 5, 6, 7, 8 | 9 )
Expand Down Expand Up @@ -116,17 +116,17 @@
( 1 | 4 mod 7 | 1 mod 2, 1 mod 2, 1, 1, 1, 1, 1 | 1 )
sage: mixed.project_to_degree(1)
( 0 | 4 mod 7 | 0 mod 2, 0 mod 2, 0, 0, 0, 0, 0 | 0 )
sage: sum( mixed.project_to_degree(i) for i in range(0,X.dimension()+1) ) == mixed
sage: sum( mixed.project_to_degree(i) for i in range(X.dimension()+1) ) == mixed
True
"""

#*****************************************************************************
# ****************************************************************************
# Copyright (C) 2010 Volker Braun <vbraun.name@gmail.com>
#
# Distributed under the terms of the GNU General Public License (GPL)
#
# http://www.gnu.org/licenses/
#*****************************************************************************
# https://www.gnu.org/licenses/
# ****************************************************************************

from sage.misc.all import flatten
from sage.misc.fast_methods import WithEqualityById
Expand Down Expand Up @@ -193,7 +193,6 @@ def __init__(self, parent, v, check=True):
"""
FGP_Element.__init__(self, parent, v, check)


def _repr_(self):
r"""
Return a string representation of the Chow cycle.
Expand All @@ -217,30 +216,29 @@ def _repr_(self):
sage: A = X.Chow_group()
sage: A.degree()
(Z, 0, C2 x Z^5, Z)
sage: sum( A.gen(i) * (i+1) for i in range(0,A.ngens()) )
sage: sum( A.gen(i) * (i+1) for i in range(A.ngens()) )
( 2 || 1 mod 2, 3, 4, 5, 6, 7 | 8 )
"""
A = self.parent()
s = '('
for degree in range(0,A.scheme().dimension()+1):
if degree>0:
for degree in range(A.scheme().dimension() + 1):
if degree:
s += '|'
generators = A.gens(degree=degree)
coefficients = A.coordinate_vector(self, degree=degree)
if len(generators)>0:
if generators:
s += ' '
for i, gen in enumerate(generators):
if i>0:
if i > 0:
s += ', '
s += str(coefficients[i])
if gen.order() != Infinity:
s += ' mod '+str(gen.order())
if len(generators)>0:
s += ' mod ' + str(gen.order())
if generators:
s += ' '
s += ')'
return s


def degree(self):
r"""
The degree of the Chow cycle.
Expand Down Expand Up @@ -296,7 +294,7 @@ def project_to_degree(self, degree):
"""
ambient_dim = self.parent()._variety.dimension()
v = list(self.lift())
for i in range(0,len(v)):
for i in range(len(v)):
cone = self.parent()._cones[i]
if cone.dim() != ambient_dim-degree:
v[i] = 0
Expand Down Expand Up @@ -951,7 +949,7 @@ def degree(self, k=None):
pass

self._degree = tuple(ChowGroup_degree_class(self,d)
for d in range(0,self._variety.dimension()+1))
for d in range(self._variety.dimension() + 1))
return self._degree


Expand Down Expand Up @@ -1153,27 +1151,26 @@ def _repr_(self):
'Q'
"""
invariants = self._module.invariants()
if len(invariants)==0:
if not invariants:
return '0'

free = [x for x in invariants if x==0]
tors = [x for x in invariants if x> 0]
free = [x for x in invariants if x == 0]
tors = [x for x in invariants if x > 0]

if self._Chow_group.base_ring()==ZZ:
if self._Chow_group.base_ring() is ZZ:
ring = 'Z'
elif self._Chow_group.base_ring()==QQ:
elif self._Chow_group.base_ring() is QQ:
ring = 'Q'
else:
raise NotImplementedError('Base ring must be ZZ or QQ.')

s = ['C' + str(x) for x in tors]
if len(free)==1:
if len(free) == 1:
s.append(ring)
if len(free)>1:
elif len(free) > 1:
s.append(ring + '^' + str(len(free)))
return ' x '.join(s)


def module(self):
"""
Return the submodule of the toric Chow group generated.
Expand All @@ -1191,7 +1188,6 @@ def module(self):
"""
return self._module


def ngens(self):
"""
Return the number of generators.
Expand All @@ -1209,7 +1205,6 @@ def ngens(self):
"""
return len(self._gens)


def gen(self, i):
"""
Return the ``i``-th generator of the Chow group of fixed
Expand All @@ -1233,7 +1228,6 @@ def gen(self, i):
"""
return self._gens[i]


def gens(self):
"""
Return the generators of the Chow group of fixed degree.
Expand Down Expand Up @@ -1268,7 +1262,7 @@ def is_ChowGroup(x):
EXAMPLES::
sage: P2=toric_varieties.P2()
sage: P2 = toric_varieties.P2()
sage: A = P2.Chow_group()
sage: from sage.schemes.toric.chow_group import is_ChowGroup
sage: is_ChowGroup(A)
Expand Down

0 comments on commit 063963f

Please sign in to comment.