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

Commit

Permalink
py3: get rid of .iteritems in combinat folder
Browse files Browse the repository at this point in the history
  • Loading branch information
Frédéric Chapoton committed Feb 3, 2017
1 parent 375d4ee commit 8fe2a8a
Show file tree
Hide file tree
Showing 34 changed files with 183 additions and 108 deletions.
3 changes: 2 additions & 1 deletion src/sage/combinat/combinat.py
Expand Up @@ -144,6 +144,7 @@
#*****************************************************************************
from __future__ import absolute_import

import six
from six.moves import range

from sage.interfaces.all import maxima
Expand Down Expand Up @@ -2702,7 +2703,7 @@ def bell_polynomial(n, k):
for p in Partitions(n, length=k):
factorial_product = 1
power_factorial_product = 1
for part, count in p.to_exp_dict().iteritems():
for part, count in six.iteritems(p.to_exp_dict()):
factorial_product *= factorial(count)
power_factorial_product *= factorial(part)**count
coefficient = factorial(n) // (factorial_product * power_factorial_product)
Expand Down
11 changes: 7 additions & 4 deletions src/sage/combinat/combinatorial_algebra.py
Expand Up @@ -65,6 +65,9 @@
from sage.categories.all import AlgebrasWithBasis
from sage.structure.element import Element

import six


# TODO: migrate this completely to the combinatorial free module + categories framework

# for backward compatibility
Expand Down Expand Up @@ -299,8 +302,8 @@ def product(self, left, right):

#Do the case where the user specifies how to multiply basis elements
if hasattr(self, '_multiply_basis'):
for (left_m, left_c) in left._monomial_coefficients.iteritems():
for (right_m, right_c) in right._monomial_coefficients.iteritems():
for (left_m, left_c) in six.iteritems(left._monomial_coefficients):
for (right_m, right_c) in six.iteritems(right._monomial_coefficients):
res = self._multiply_basis(left_m, right_m)
coeffprod = left_c * right_c
#Handle the case where the user returns a dictionary
Expand All @@ -313,7 +316,7 @@ def product(self, left, right):
else:
z_elt[res] = z_elt.get(res, ABRzero) + coeffprod
continue
for m, c in res.iteritems():
for m, c in six.iteritems(res):
z_elt[m] = z_elt.get(m, ABRzero) + coeffprod * c

#We assume that the user handles the multiplication correctly on
Expand All @@ -332,7 +335,7 @@ def product(self, left, right):
BR = self.base_ring()
zero = BR(0)
del_list = []
for m, c in z_elt.iteritems():
for m, c in six.iteritems(z_elt):
if c == zero:
del_list.append(m)
for m in del_list:
Expand Down
33 changes: 18 additions & 15 deletions src/sage/combinat/crystals/monomial_crystals.py
Expand Up @@ -112,6 +112,9 @@
from sage.matrix.matrix import is_Matrix
from sage.matrix.matrix_space import MatrixSpace

import six


class NakajimaMonomial(Element):
r"""
An element of the monomial crystal.
Expand Down Expand Up @@ -187,7 +190,7 @@ def _repr_Y(self):
if not self._Y:
return "1"

L = sorted(self._Y.iteritems(), key=lambda x: (x[0][0], x[0][1]))
L = sorted(six.iteritems(self._Y), key=lambda x: (x[0][0], x[0][1]))
exp = lambda e: "^{}".format(e) if e != 1 else ""
return ' '.join("Y({},{})".format(mon[0][0], mon[0][1]) + exp(mon[1])
for mon in L)
Expand All @@ -211,15 +214,15 @@ def _repr_A(self):
if not Y and not self._A:
return "1"

L = sorted(Y.iteritems(), key=lambda x: (x[0][0], x[0][1]))
L = sorted(six.iteritems(Y), key=lambda x: (x[0][0], x[0][1]))
exp = lambda e: "^{}".format(e) if e != 1 else ""
ret = ' '.join("Y({},{})".format(mon[0][0], mon[0][1]) + exp(mon[1])
for mon in L)
if not self._A:
return ret
if Y:
ret += ' '
L = sorted(self._A.iteritems(), key=lambda x: (x[0][0], x[0][1]))
L = sorted(six.iteritems(self._A), key=lambda x: (x[0][0], x[0][1]))
return ret + ' '.join("A({},{})".format(mon[0][0], mon[0][1]) + exp(mon[1])
for mon in L)

Expand All @@ -233,7 +236,7 @@ def __hash__(self):
4715601665014767730 # 64-bit
-512614286 # 32-bit
"""
return hash(frozenset(tuple(self._Y.iteritems())))
return hash(frozenset(tuple(six.iteritems(self._Y))))

def __eq__(self, other):
r"""
Expand Down Expand Up @@ -310,7 +313,7 @@ def _latex_Y(self):
if not self._Y:
return "\\boldsymbol{1}"

L = sorted(self._Y.iteritems(), key=lambda x:(x[0][0],x[0][1]))
L = sorted(six.iteritems(self._Y), key=lambda x:(x[0][0],x[0][1]))
return_str = ''
for x in L:
if x[1] != 1:
Expand Down Expand Up @@ -338,14 +341,14 @@ def _latex_A(self):
if not Y and not self._A:
return "\\boldsymbol{1}"

L = sorted(Y.iteritems(), key=lambda x:(x[0][0],x[0][1]))
L = sorted(six.iteritems(Y), key=lambda x:(x[0][0],x[0][1]))
return_str = ''
for x in L:
if x[1] != 1:
return_str += "Y_{%s,%s}"%(x[0][0],x[0][1]) + "^{%s} "%x[1]
else:
return_str += "Y_{%s,%s} "%(x[0][0],x[0][1])
L = sorted(self._A.iteritems(), key=lambda x:(x[0][0],x[0][1]))
L = sorted(six.iteritems(self._A), key=lambda x:(x[0][0],x[0][1]))
for x in L:
if x[1] != 1:
return_str += "A_{%s,%s}"%(x[0][0],x[0][1]) + "^{%s} "%x[1]
Expand All @@ -372,7 +375,7 @@ def _classical_weight(self):
"""
P = self.parent().weight_lattice_realization()
La = P.fundamental_weights()
return P(sum(v*La[k[0]] for k,v in self._Y.iteritems()))
return P(sum(v*La[k[0]] for k,v in six.iteritems(self._Y)))

def weight_in_root_lattice(self):
r"""
Expand All @@ -398,7 +401,7 @@ def weight_in_root_lattice(self):
"""
Q = RootSystem(self.parent().cartan_type()).root_lattice()
al = Q.simple_roots()
return Q.sum(e*al[k[0]] for k,e in self._A.iteritems())
return Q.sum(e*al[k[0]] for k,e in six.iteritems(self._A))

def weight(self):
r"""
Expand Down Expand Up @@ -476,7 +479,7 @@ def phi(self, i):
continue
else:
d[(i,a)] = 0
S = sorted((x for x in d.iteritems() if x[0][0] == i), key=lambda x: x[0][1])
S = sorted((x for x in six.iteritems(d) if x[0][0] == i), key=lambda x: x[0][1])
return max(sum(S[k][1] for k in range(s)) for s in range(1,len(S)+1))

def _ke(self, i):
Expand Down Expand Up @@ -508,7 +511,7 @@ def _ke(self, i):
d[(i,a)] = 0
total = ZZ.zero()
L = []
S = sorted((x for x in d.iteritems() if x[0][0] == i), key=lambda x: x[0][1])
S = sorted((x for x in six.iteritems(d) if x[0][0] == i), key=lambda x: x[0][1])
for var,exp in S:
total += exp
if total == phi:
Expand Down Expand Up @@ -541,7 +544,7 @@ def _kf(self, i):
continue
else:
d[(i,a)] = 0
S = sorted((x for x in d.iteritems() if x[0][0] == i), key=lambda x: x[0][1])
S = sorted((x for x in six.iteritems(d) if x[0][0] == i), key=lambda x: x[0][1])
sum = 0
phi = self.phi(i)
for var,exp in S:
Expand Down Expand Up @@ -611,7 +614,7 @@ def e(self, i):
if cm[j_index,i-shift] != 0:
Aik[(j, ke+c)] = cm[j_index,i-shift]
# Multiply by Aik
for key,value in Aik.iteritems():
for key,value in six.iteritems(Aik):
if key in newdict:
if newdict[key] == -value: # The result would be a 0 exponent
del newdict[key]
Expand Down Expand Up @@ -660,7 +663,7 @@ def f(self, i):
if cm[j_index,i-shift] != 0:
Aik[(j, kf+c)] = -cm[j_index,i-shift]
# Multiply by Aik
for key,value in Aik.iteritems():
for key,value in six.iteritems(Aik):
if key in newdict:
if newdict[key] == -value: # The result would be a 0 exponent
del newdict[key]
Expand Down Expand Up @@ -921,7 +924,7 @@ def _element_constructor_(self, Y=None, A=None):
if ct.is_finite():
shift = 1
Y = {}
for k,v in A.iteritems():
for k,v in six.iteritems(A):
Y[k] = Y.get(k, 0) + v
Y[(k[0],k[1]+1)] = Y.get((k[0],k[1]+1), 0) + v
for j_index,j in enumerate(I):
Expand Down
7 changes: 5 additions & 2 deletions src/sage/combinat/designs/block_design.py
Expand Up @@ -68,6 +68,9 @@
from sage.matrix.matrix_space import MatrixSpace


import six


BlockDesign = IncidenceStructure

### utility functions -------------------------------------------------------
Expand Down Expand Up @@ -273,7 +276,7 @@ def ProjectiveGeometryDesign(n, d, F, algorithm=None, point_coordinates=True, ch
blocks.append(b)
B = BlockDesign(len(points), blocks, name="ProjectiveGeometryDesign", check=check)
if point_coordinates:
B.relabel({i:p[0] for p,i in points.iteritems()})
B.relabel({i:p[0] for p,i in six.iteritems(points)})

elif algorithm == "gap": # Requires GAP's Design
from sage.interfaces.gap import gap
Expand Down Expand Up @@ -870,7 +873,7 @@ def AffineGeometryDesign(n, d, F, point_coordinates=True, check=True):
B = BlockDesign(len(points), blocks, name="AffineGeometryDesign", check=check)

if point_coordinates:
rd = {i:p[0][1:] for p,i in points.iteritems()}
rd = {i:p[0][1:] for p,i in six.iteritems(points)}
for v in rd.values(): v.set_immutable()
B.relabel(rd)

Expand Down
5 changes: 3 additions & 2 deletions src/sage/combinat/designs/database.py
Expand Up @@ -47,6 +47,7 @@
---------
"""
from __future__ import print_function, absolute_import
import six
from six.moves import range

from sage.combinat.designs.orthogonal_arrays import (OA_from_quasi_difference_matrix,
Expand Down Expand Up @@ -1684,7 +1685,7 @@ def OA_10_469():
blocks[len(B)].append(B)

# Product of each symmetric design with the OA
for b_size,symmetric_design in blocks.iteritems():
for b_size,symmetric_design in six.iteritems(blocks):
matrix = _reorder_matrix(symmetric_design)
OA.extend([[B[xx] for xx in R]
for R in incomplete_orthogonal_array(9,b_size,[1]*b_size)
Expand Down Expand Up @@ -2694,7 +2695,7 @@ def QDM_57_9_1_1_8():
(12,413) : ((0,1,436,546,977,467,242,3695,682,483,3026,461,1334), _ref_Abel_v_12_t),
}
# Translate all V(m,t) into (mt+1,m+2;1,0;t)-QDM constructors
for (m,t),(vec,source) in Vmt_vectors.iteritems():
for (m,t),(vec,source) in six.iteritems(Vmt_vectors):
n,k,lmbda,mu,u = (m*t+1,m+2,1,0,t)
if not (n+u,lmbda) in QDM:
QDM[n+u,lmbda] = {}
Expand Down
3 changes: 2 additions & 1 deletion src/sage/combinat/designs/difference_family.py
Expand Up @@ -51,6 +51,7 @@
from __future__ import division, print_function, absolute_import

from builtins import zip
import six
from six import itervalues
from six.moves import range

Expand Down Expand Up @@ -1537,7 +1538,7 @@ def difference_family(v, k, l=1, existence=False, explain_construction=False, ch
elif explain_construction:
return "The database contains a ({},{},{})-difference family".format(v,k,l)

vv, blocks = next(DF[v,k,l].iteritems())
vv, blocks = next(six.iteritems(DF[v,k,l]))

# Build the group
from sage.rings.finite_rings.integer_mod_ring import Zmod
Expand Down
3 changes: 2 additions & 1 deletion src/sage/combinat/designs/ext_rep.py
Expand Up @@ -43,6 +43,7 @@
import sys

# import compatible with py2 and py3
import six
from six.moves.urllib.request import urlopen
from six import string_types

Expand Down Expand Up @@ -871,7 +872,7 @@ def _start_element(self, name, attrs):
elif name == 'designs':
pass # self.outf.write(' <%s>\n' % name)
if self.in_item:
for k, v in attrs.iteritems():
for k, v in six.iteritems(attrs):
attrs[k] = _encode_attribute(v)
new_node = (name, attrs, [])
self.node_stack.append(new_node)
Expand Down
13 changes: 7 additions & 6 deletions src/sage/combinat/designs/incidence_structures.py
Expand Up @@ -40,6 +40,7 @@
#***************************************************************************
from __future__ import print_function

import six
from six import itervalues
from six.moves import range

Expand Down Expand Up @@ -483,8 +484,8 @@ def is_isomorphic(self, other, certificate=False):

if A == B:
if certificate:
B_canon_rev = {y:x for x,y in B_canon.iteritems()}
return {x:B_canon_rev[xint] for x,xint in A_canon.iteritems()}
B_canon_rev = {y:x for x,y in six.iteritems(B_canon)}
return {x:B_canon_rev[xint] for x,xint in six.iteritems(A_canon)}
else:
return True
else:
Expand Down Expand Up @@ -898,7 +899,7 @@ def degrees(self, size=None):
for s in combinations(b,size):
d[s]+=1
if self._point_to_index:
return {tuple([self._points[x] for x in s]):v for s,v in d.iteritems()}
return {tuple([self._points[x] for x in s]):v for s,v in six.iteritems(d)}
else:
return d

Expand Down Expand Up @@ -1426,7 +1427,7 @@ def packing(self, solver=None, verbose=0):
p.solve(log=verbose)

return [[self._points[x] for x in self._blocks[i]]
for i,v in p.get_values(b).iteritems() if v]
for i,v in six.iteritems(p.get_values(b)) if v]

def is_t_design(self, t=None, v=None, k=None, l=None, return_parameters=False):
r"""
Expand Down Expand Up @@ -1948,7 +1949,7 @@ def is_resolvable(self, certificate=False, solver=None, verbose=0, check=True):
else:
# each class is stored as the list of indices of its blocks
self._classes = [[] for _ in range(n_classes)]
for (t,i),v in p.get_values(b).iteritems():
for (t,i),v in six.iteritems(p.get_values(b)):
if v:
self._classes[t].append(self._blocks[i])

Expand Down Expand Up @@ -2060,7 +2061,7 @@ def coloring(self, k=None, solver=None, verbose=0):

col = [[] for i in range(k)]

for (x,i),v in p.get_values(b).iteritems():
for (x,i),v in six.iteritems(p.get_values(b)):
if v:
col[i].append(self._points[x])

Expand Down
5 changes: 4 additions & 1 deletion src/sage/combinat/designs/latin_squares.py
Expand Up @@ -128,6 +128,9 @@
from sage.categories.sets_cat import EmptySetError
from sage.misc.unknown import Unknown

import six


def are_mutually_orthogonal_latin_squares(l, verbose=False):
r"""
Check wether the list of matrices in ``l`` form mutually orthogonal latin
Expand Down Expand Up @@ -470,7 +473,7 @@ def latin_square_product(M,N,*others):
for jj in range(n)}

L = lambda i_j: i_j[0] * n + i_j[1]
D = {(L(c[0]),L(c[1])): L(v) for c,v in D.iteritems()}
D = {(L(c[0]),L(c[1])): L(v) for c,v in six.iteritems(D)}
P = Matrix(D)

if others:
Expand Down
7 changes: 4 additions & 3 deletions src/sage/combinat/designs/orthogonal_arrays.py
Expand Up @@ -58,6 +58,7 @@
from __future__ import print_function, absolute_import

from builtins import zip
import six
from six import itervalues
from six.moves import range

Expand Down Expand Up @@ -1375,7 +1376,7 @@ def incomplete_orthogonal_array(k,n,holes,resolvable=False, existence=False):

# From a quasi-difference matrix
elif number_of_holes==1 and any(uu==sum_of_holes and mu<=1 and lmbda==1 and k<=kk+1 for (nn,lmbda,mu,uu),(kk,_) in QDM.get((n,1),{}).iteritems()):
for (nn,lmbda,mu,uu),(kk,f) in QDM[n,1].iteritems():
for (nn,lmbda,mu,uu),(kk,f) in six.iteritems(QDM[n,1]):
if uu==sum_of_holes and mu<=1 and lmbda==1 and k<=kk+1:
break
G,M = f()
Expand Down Expand Up @@ -1806,8 +1807,8 @@ def OA_from_quasi_difference_matrix(M,G,add_col=True,fill_hole=True):

# A cache for addition in G
G_sum = [[0]*Gn for _ in range(Gn)]
for x,i in G_to_int.iteritems():
for xx,ii in G_to_int.iteritems():
for x,i in six.iteritems(G_to_int):
for xx,ii in six.iteritems(G_to_int):
G_sum[i][ii] = G_to_int[x+xx]

# Convert M to integers
Expand Down

0 comments on commit 8fe2a8a

Please sign in to comment.