diff --git a/src/sage/coding/binary_code.pyx b/src/sage/coding/binary_code.pyx index 83dbefdfaf3..1b8b1d17ae1 100644 --- a/src/sage/coding/binary_code.pyx +++ b/src/sage/coding/binary_code.pyx @@ -617,7 +617,7 @@ cdef codeword *expand_to_ortho_basis(BinaryCode B, int n) noexcept: i = k word = 1 << k k += 1 - else: # NOTE THIS WILL NEVER HAPPEN AS CURRENTLY SET UP! + else: # NOTE THIS WILL NEVER HAPPEN AS CURRENTLY SET UP! temp = (1 << k) - 1 i = k word = 1 << k @@ -813,7 +813,7 @@ cdef class BinaryCode: combination ^= (1 << j) word ^= self_basis[j] - else: # isinstance(arg1, BinaryCode) + else: # isinstance(arg1, BinaryCode) other_basis = other.basis for i from 0 <= i < nrows-1: self_basis[i] = other_basis[i] @@ -2423,9 +2423,12 @@ cdef class PartitionStack: cdef int *self_wd_lvls = self.wd_lvls cdef int *self_wd_ents = self.wd_ents while True: - if CG.is_one(self_wd_ents[wd_ptr], col): i += 1 - if self_wd_lvls[wd_ptr] > k: wd_ptr += 1 - else: break + if CG.is_one(self_wd_ents[wd_ptr], col): + i += 1 + if self_wd_lvls[wd_ptr] > k: + wd_ptr += 1 + else: + break return i def _wd_degree(self, C, wd, col_ptr, k): @@ -3347,15 +3350,17 @@ cdef class BinaryCodeClassifier: if qzb > 0: zb__Lambda_rho[k] = Lambda[k] state = 3 - elif state == 3: # attempt to rule out automorphisms while moving down the tree + elif state == 3: # attempt to rule out automorphisms while moving down the tree # if k > hzf, then we know that nu currently does not look like zeta, the first # terminal node encountered, thus there is no automorphism to discover. If qzb < 0, # i.e. Lambda[k] < zb[k], then the indicator is not maximal, and we can't reach a # canonical leaf. If neither of these is the case, then proceed to state 4. - if hzf__h_zeta <= k or qzb >= 0: state = 4 - else: state = 6 + if hzf__h_zeta <= k or qzb >= 0: + state = 4 + else: + state = 6 - elif state == 4: # at this point we have -not- ruled out the presence of automorphisms + elif state == 4: # at this point we have -not- ruled out the presence of automorphisms if nu.is_discrete(k): state = 7 continue # we have a terminal node, so process it @@ -3365,17 +3370,18 @@ cdef class BinaryCodeClassifier: # equal to its minimum element v[k] = nu.new_first_smallest_nontrivial(k, W, self.Phi_size * k) if not nu.sat_225(k): hh = k + 1 - e[k] = 0 # see state 12 and 17 - state = 2 # continue down the tree + e[k] = 0 # see state 12 and 17 + state = 2 # continue down the tree - elif state == 5: # same as state 3, but in the case where we haven't yet defined zeta - # i.e. this is our first time down the tree. Once we get to the bottom, - # we will have zeta = nu = rho, so we do: + elif state == 5: + # same as state 3, but in the case where we haven't yet defined zeta + # i.e. this is our first time down the tree. Once we get to the bottom, + # we will have zeta = nu = rho, so we do: zf__Lambda_zeta[k] = Lambda[k] zb__Lambda_rho[k] = Lambda[k] state = 4 - elif state == 6: # at this stage, there is no reason to continue downward, so backtrack + elif state == 6: # at this stage, there is no reason to continue downward, so backtrack j = k # return to the longest ancestor nu[i] of nu that could have a @@ -3396,9 +3402,10 @@ cdef class BinaryCodeClassifier: else: k = hh-1 # TODO: is the following line necessary? - if k == -1: k = 0 + if k == -1: + k = 0 - if hb > k:# update hb since we are backtracking + if hb > k: # update hb since we are backtracking hb = k # if j == hh, then all nodes lower than our current position are equivalent, so bail out if j == hh: @@ -3465,7 +3472,7 @@ cdef class BinaryCodeClassifier: state = 10 - elif state == 9: # nu is a better guess at the canonical label than rho + elif state == 9: # nu is a better guess at the canonical label than rho rho = PartitionStack(nu) k_rho = k qzb = 0 @@ -3475,7 +3482,7 @@ cdef class BinaryCodeClassifier: zb__Lambda_rho[k+1] = -1 state = 6 - elif state == 10: # we have an automorphism to process + elif state == 10: # we have an automorphism to process # increment l if l < self.L-1: l += 1 # store information about the automorphism to Omega and Phi @@ -3496,8 +3503,8 @@ cdef class BinaryCodeClassifier: Omega[ii] ^= (1<1: return self.polynomial()._latex_() - else: - return str(self) + return str(self) def __pari__(self, var=None): r""" @@ -623,10 +622,11 @@ cdef class FinitePolyExtElement(FiniteRingElement): from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing R = PolynomialRing(self.parent().prime_subfield(), var) return R(self.__pari__().charpoly('x').lift()) - elif algorithm == 'matrix': + + if algorithm == 'matrix': return self.matrix().charpoly(var) - else: - raise ValueError("unknown algorithm '%s'" % algorithm) + + raise ValueError("unknown algorithm '%s'" % algorithm) def norm(self): """ @@ -656,10 +656,7 @@ cdef class FinitePolyExtElement(FiniteRingElement): """ f = self.charpoly('x') n = f[0] - if f.degree() % 2: - return -n - else: - return n + return -n if f.degree() % 2 else n def trace(self): """ @@ -921,20 +918,22 @@ cdef class FinitePolyExtElement(FiniteRingElement): """ if self.is_zero(): if n <= 0: - if all: return [] - else: raise ValueError - if all: return [self] - else: return self + if all: + return [] + raise ValueError + return [self] if all else self if n < 0: self = ~self n = -n elif n == 0: if self == 1: - if all: return [a for a in self.parent().list() if a != 0] - else: return self + if all: + return [a for a in self.parent().list() if a != 0] + return self else: - if all: return [] - else: raise ValueError + if all: + return [] + raise ValueError if extend: raise NotImplementedError n = Integer(n) diff --git a/src/sage/rings/finite_rings/integer_mod.pyx b/src/sage/rings/finite_rings/integer_mod.pyx index 19525b40937..9a3abc6f963 100644 --- a/src/sage/rings/finite_rings/integer_mod.pyx +++ b/src/sage/rings/finite_rings/integer_mod.pyx @@ -1505,17 +1505,18 @@ cdef class IntegerMod_abstract(FiniteRingElement): n = Integer(n) if n == 0: if self == 1: - if all: return [K(a) for a in range(1,K.order())] - else: return self + if all: + return [K(a) for a in range(1, K.order())] + return self else: - if all: return [] - else: raise ValueError + if all: + return [] + raise ValueError F = K.factored_order() if len(F) == 0: if all: return [self] - else: - return self + return self if len(F) != 1: if all: # we should probably do a first pass to see if there are any solutions so that we don't get giant intermediate lists and waste time... @@ -1533,33 +1534,31 @@ cdef class IntegerMod_abstract(FiniteRingElement): p, k = F[0] if self.is_zero(): if n < 0: - if all: return [] - else: raise ValueError + if all: + return [] + raise ValueError if all: if k == 1: return [self] - else: - minval = max(1, (k/n).ceil()) - return [K(a*p**minval) for a in range(p**(k-minval))] - else: - return self + minval = max(1, (k/n).ceil()) + return [K(a*p**minval) for a in range(p**(k-minval))] + return self if n < 0: try: self = ~self except ZeroDivisionError: - if all: return [] - else: raise ValueError + if all: + return [] + raise ValueError n = -n if p == 2 and k == 1: - if all: return [self] - else: return self + return [self] if all else self if k > 1: pval, upart = self.lift().val_unit(p) if not n.divides(pval): if all: return [] - else: - raise ValueError("no nth root") + raise ValueError("no nth root") if pval > 0: if all: return [K(a.lift()*p**(pval // n) + p**(k - (pval - pval//n)) * b) for a in mod(upart, p**(k-pval)).nth_root(n, all=True, algorithm=algorithm) for b in range(p**(pval - pval//n))] @@ -1571,27 +1570,30 @@ cdef class IntegerMod_abstract(FiniteRingElement): sign = [1] if self % 4 == 3: if n % 2 == 0: - if all: return [] - else: raise ValueError("no nth root") + if all: + return [] + raise ValueError("no nth root") else: sign = [-1] self = -self elif n % 2 == 0: if k > 2 and self % 8 == 5: - if all: return [] - else: raise ValueError("no nth root") + if all: + return [] + raise ValueError("no nth root") sign = [1, -1] if k == 2: - if all: return [K(s) for s in sign[:2]] - else: return K(sign[0]) - if all: modp = [mod(self,8)] - else: modp = mod(self,8) + if all: + return [K(s) for s in sign[:2]] + return K(sign[0]) + modp = [mod(self, 8)] if all else mod(self, 8) else: sign = [1] modp = self % p self = self / K(R.teichmuller(modp)) modp = modp.nth_root(n, all=all, algorithm=algorithm) - # now self is congruent to 1 mod 4 or 1 mod p (for odd p), so the power series for p-adic log converges. + # now self is congruent to 1 mod 4 or 1 mod p (for odd p), + # so the power series for p-adic log converges. # Hensel lifting is probably better, but this is easier at the moment. plog = R(self).log() nval = n.valuation(p) @@ -1599,11 +1601,11 @@ cdef class IntegerMod_abstract(FiniteRingElement): if self == 1: if all: return [s*K(p*a+m.lift()) for a in range(p**(k-(2 if p==2 else 1))) for m in modp for s in sign] - else: - return K(modp.lift()) + return K(modp.lift()) else: - if all: return [] - else: raise ValueError("no nth root") + if all: + return [] + raise ValueError("no nth root") if all: ans = [plog // n + p**(k - nval) * i for i in range(p**nval)] ans = [s*K(R.teichmuller(m) * a.exp()) for a in ans for m in modp for s in sign] @@ -3193,10 +3195,12 @@ cdef int_fast32_t mod_pow_int(int_fast32_t base, int_fast32_t exp, int_fast32_t if exp == 4: return (prod * prod) % n pow2 = base - if exp % 2: prod = base - else: prod = 1 + if exp % 2: + prod = base + else: + prod = 1 exp = exp >> 1 - while(exp != 0): + while exp != 0: pow2 = pow2 * pow2 if pow2 >= INTEGER_MOD_INT32_LIMIT: pow2 = pow2 % n if exp % 2: @@ -3855,10 +3859,12 @@ cdef int_fast64_t mod_pow_int64(int_fast64_t base, int_fast64_t exp, int_fast64_ if exp == 4: return (prod * prod) % n pow2 = base - if exp % 2: prod = base - else: prod = 1 + if exp % 2: + prod = base + else: + prod = 1 exp = exp >> 1 - while(exp != 0): + while exp != 0: pow2 = pow2 * pow2 if pow2 >= INTEGER_MOD_INT64_LIMIT: pow2 = pow2 % n if exp % 2: diff --git a/src/sage/rings/polynomial/polynomial_element.pyx b/src/sage/rings/polynomial/polynomial_element.pyx index 4d2e607b37d..aac651235f7 100644 --- a/src/sage/rings/polynomial/polynomial_element.pyx +++ b/src/sage/rings/polynomial/polynomial_element.pyx @@ -1175,8 +1175,7 @@ cdef class Polynomial(CommutativePolynomial): cdef Py_ssize_t d = self.degree() if 0 <= i <= d: return self.get_unsafe(i) - else: - return self._parent._base.zero() + return self._parent._base.zero() cdef get_unsafe(self, Py_ssize_t i): """ @@ -1822,13 +1821,14 @@ cdef class Polynomial(CommutativePolynomial): cdef Polynomial _right = right if self.is_term(): return _right._mul_term(self, term_on_right=False) - elif _right.is_term(): + + if _right.is_term(): return self._mul_term(_right, term_on_right=True) - elif self._parent.is_exact(): + if self._parent.is_exact(): return self._mul_karatsuba(right) - else: - return self._mul_generic(right) + + return self._mul_generic(right) cpdef Polynomial _mul_trunc_(self, Polynomial right, long n): r""" @@ -6809,13 +6809,17 @@ cdef class Polynomial(CommutativePolynomial): e = self.exponents() c = self.coefficients() - if len(e) == 0: return [] + if len(e) == 0: + return [] if len(e) == 1: - if e[0] == 0: return [] - else: return [(infinity.infinity, e[0])] + if e[0] == 0: + return [] + return [(infinity.infinity, e[0])] - if e[0] == 0: slopes = [] - else: slopes = [(infinity.infinity, e[0])] + if e[0] == 0: + slopes = [] + else: + slopes = [(infinity.infinity, e[0])] points = [(e[0], c[0].valuation(p)), (e[1], c[1].valuation(p))] slopes.append((-(c[1].valuation(p)-c[0].valuation(p))/(e[1] - e[0]), e[1]-e[0])) @@ -6826,8 +6830,8 @@ cdef class Polynomial(CommutativePolynomial): slopes = slopes[:-1] points = points[:-1] s = -(v-points[-1][1])/(e[i]-points[-1][0]) - slopes.append((s,e[i]-points[-1][0])) - points.append((e[i],v)) + slopes.append((s, e[i]-points[-1][0])) + points.append((e[i], v)) return slopes