Skip to content

Commit

Permalink
combinatorics/permutations: Use floor division
Browse files Browse the repository at this point in the history
Some code was using the ambiguous division operator on integers. This
produces floats by default in Python 3 (1.0 as opposed to 1) which
cannot be used as list indices. Fix by using floor division (//) where
appropriate. A doctest was also failing, it expected an implicit cast to
int (1.0 to 1 automatically).

Also remove some redundant casts to int, factorial() returns an int
always.
  • Loading branch information
vperic committed Aug 19, 2011
1 parent f732c4e commit 49f0686
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions sympy/combinatorics/permutations.py
Expand Up @@ -295,7 +295,7 @@ def unrank_nonlex(self, n, r):
while n > 1:
id_perm[n-1],id_perm[r % n] = id_perm[r % n], id_perm[n-1]
n -= 1
r = r/n
r = r//n
return Permutation(id_perm)

def rank_nonlex(self, inv_perm = None, n = 0):
Expand Down Expand Up @@ -345,7 +345,7 @@ def rank(self):
rank = 0
rho = self.array_form[:]
for j in xrange(self.size - 1):
rank += (rho[j])*int(factorial(self.size - j - 1))
rank += (rho[j])*factorial(self.size - j - 1)
for i in xrange(j + 1, self.size):
if rho[i] > rho[j]:
rho[i] = rho[i] - 1
Expand Down Expand Up @@ -774,7 +774,7 @@ def get_precedence_distance(self, other):
continue
if self_prec_mat[i, j] * other_prec_mat[i, j] == 1:
n_prec += 1
d = self.size * (self.size - 1)/2 - n_prec
d = self.size * (self.size - 1)//2 - n_prec
return d

def get_adjacency_matrix(self):
Expand Down Expand Up @@ -966,8 +966,8 @@ def unrank_lex(self, size, rank):
perm_array = [0] * size
perm_array[size - 1] = 1
for i in xrange(size - 1):
d = (rank % int(factorial(i + 1))) / int(factorial(i))
rank = rank - d*int(factorial(i))
d = (rank % factorial(i + 1)) // factorial(i)
rank = rank - d*factorial(i)
perm_array[size - i - 1] = d + 1
for j in xrange(size - i, size):
if perm_array[j] > d:
Expand Down

0 comments on commit 49f0686

Please sign in to comment.