diff --git a/src/sage/arith/misc.py b/src/sage/arith/misc.py index ba100c4a83b..53d9683279b 100644 --- a/src/sage/arith/misc.py +++ b/src/sage/arith/misc.py @@ -4546,7 +4546,7 @@ def hilbert_symbol(a, b, p, algorithm="pari"): if algorithm == "pari": if p == -1: p = 0 - return ZZ(pari(a).hilbert(b,p)) + return ZZ(pari(a).hilbert(b, p)) elif algorithm == 'direct': if a == 0 or b == 0: @@ -4557,13 +4557,15 @@ def hilbert_symbol(a, b, p, algorithm="pari"): if p != -1: p_sqr = p**2 - while a%p_sqr == 0: a //= p_sqr - while b%p_sqr == 0: b //= p_sqr + while a % p_sqr == 0: + a //= p_sqr + while b % p_sqr == 0: + b //= p_sqr - if p != 2 and True in ( kronecker(x,p) == 1 for x in (a,b,a+b) ): + if p != 2 and True in (kronecker(x, p) == 1 for x in (a, b, a + b)): return one - if a%p == 0: - if b%p == 0: + if a % p == 0: + if b % p == 0: return hilbert_symbol(p,-(b//p),p)*hilbert_symbol(a//p,b,p) elif p == 2 and (b%4) == 3: if kronecker(a+b,p) == -1: diff --git a/src/sage/calculus/calculus.py b/src/sage/calculus/calculus.py index 7752baf17cb..9d0047dfe55 100644 --- a/src/sage/calculus/calculus.py +++ b/src/sage/calculus/calculus.py @@ -2210,7 +2210,7 @@ def symbolic_expression_from_maxima_string(x, equals_sub=False, maxima=maxima): #replace all instances of Maxima's scientific notation #with regular notation search = sci_not.search(s) - while not search is None: + while search is not None: (start, end) = search.span() r = create_RealNumber(s[start:end]).str(no_sci=2, truncate=True) s = s.replace(s[start:end], r) diff --git a/src/sage/calculus/desolvers.py b/src/sage/calculus/desolvers.py index 6bd78d2524a..449a116acb1 100644 --- a/src/sage/calculus/desolvers.py +++ b/src/sage/calculus/desolvers.py @@ -1054,7 +1054,8 @@ def eulers_method(f,x0,y0,h,x1,algorithm="table"): if algorithm=="table": print("%10s %20s %25s"%("x","y","h*f(x,y)")) n=int((1.0)*(x1-x0)/h) - x00=x0; y00=y0 + x00 = x0 + y00 = y0 soln = [[x00,y00]] for i in range(n+1): if algorithm=="table": @@ -1152,9 +1153,11 @@ def eulers_method_2x2(f,g, t0, x0, y0, h, t1,algorithm="table"): """ if algorithm=="table": print("%10s %20s %25s %20s %20s"%("t", "x","h*f(t,x,y)","y", "h*g(t,x,y)")) - n=int((1.0)*(t1-t0)/h) - t00 = t0; x00 = x0; y00 = y0 - soln = [[t00,x00,y00]] + n = int((1.0)*(t1-t0)/h) + t00 = t0 + x00 = x0 + y00 = y0 + soln = [[t00, x00, y00]] for i in range(n+1): if algorithm=="table": print("%10r %20r %25r %20r %20r"%(t00,x00,h*f(t00,x00,y00),y00,h*g(t00,x00,y00))) @@ -1381,27 +1384,32 @@ def desolve_rk4(de, dvar, ics=None, ivar=None, end_points=None, step=0.1, output sol.extend([[ics[0],ics[1]]]) sol.extend(sol_2) - if output=='list': + if output == 'list': return sol from sage.plot.plot import list_plot from sage.plot.plot_field import plot_slope_field - R = list_plot(sol,plotjoined=True,**kwds) - if output=='plot': + R = list_plot(sol, plotjoined=True, **kwds) + if output == 'plot': return R - if output=='slope_field': - XMIN=sol[0][0] - YMIN=sol[0][1] - XMAX=XMIN - YMAX=YMIN - for s,t in sol: - if s>XMAX:XMAX=s - if sYMAX:YMAX=t - if t XMAX: + XMAX = s + if s < XMIN: + XMIN = s + if t > YMAX: + YMAX = t + if t < YMIN: + YMIN = t + return plot_slope_field(de, (ivar,XMIN,XMAX), (dummy_dvar,YMIN,YMAX))+R raise ValueError("Option output should be 'list', 'plot' or 'slope_field'.") + def desolve_system_rk4(des, vars, ics=None, ivar=None, end_points=None, step=0.1): r""" Solve numerically a system of first-order ordinary differential @@ -1667,13 +1675,15 @@ def desolve_odeint(des, ics, times, dvars, ivar=None, compute_jac=False, args=() # one-dimensional systems: if is_SymbolicVariable(dvars): - func = fast_float(des,dvars,ivar) + func = fast_float(des, dvars, ivar) if not compute_jac: - Dfun=None + Dfun = None else: - J = diff(des,dvars) - J = fast_float(J,dvars,ivar) - Dfun = lambda y,t: [J(y,t)] + J = diff(des, dvars) + J = fast_float(J, dvars, ivar) + + def Dfun(y, t): + return [J(y, t)] # n-dimensional systems: else: diff --git a/src/sage/calculus/functions.py b/src/sage/calculus/functions.py index a2c2cae2af6..25a55c67f9b 100644 --- a/src/sage/calculus/functions.py +++ b/src/sage/calculus/functions.py @@ -77,7 +77,7 @@ def wronskian(*args): - Dan Drake (2008-03-12) """ - if len(args) == 0: + if not args: raise TypeError('wronskian() takes at least one argument (0 given)') elif len(args) == 1: # a 1x1 Wronskian is just its argument @@ -88,19 +88,21 @@ def wronskian(*args): # differentiate the other args v = args[-1] fs = args[0:-1] - row = lambda n: [diff(f, v, n) for f in fs] + + def row(n): + return [diff(f, v, n) for f in fs] else: - # if the last argument isn't a variable, just run + # if the last argument is not a variable, just run # .derivative on everything fs = args - row = lambda n: [diff(f, n) for f in fs] + + def row(n): + return [diff(f, n) for f in fs] # NOTE: I rewrote the below as two lines to avoid a possible subtle # memory management problem on some platforms (only VMware as far # as we know?). See trac #2990. # There may still be a real problem that this is just hiding for now. - A = matrix([row(_) for _ in range(len(fs))]) - return A.determinant() - #return matrix(map(row, range(len(fs)))).determinant() + return matrix([row(r) for r in range(len(fs))]).determinant() def jacobian(functions, variables): diff --git a/src/sage/homology/chain_complex.py b/src/sage/homology/chain_complex.py index 2181661f484..49aaff1a854 100644 --- a/src/sage/homology/chain_complex.py +++ b/src/sage/homology/chain_complex.py @@ -263,7 +263,7 @@ def ChainComplex(data=None, base_ring=None, grading_group=None, # make sure values in data_dict are appropriate matrices for n in list(data_dict): - if not n in grading_group: + if n not in grading_group: raise ValueError('one of the dictionary keys is not an element of the grading group') mat = data_dict[n] if not isinstance(mat, Matrix): @@ -2156,9 +2156,11 @@ def tensor(self, *factors, **kwds): ret = self if self._grading_group is ZZ: - scalar = lambda a: (-1)**(a * deg_diff) + def scalar(a): + return (-1)**(a * deg_diff) else: - scalar = lambda a: (-1)**(sum(a) * sum(deg_diff)) + def scalar(a): + return (-1)**(sum(a) * sum(deg_diff)) for D in factors: # Setup diff --git a/src/sage/homology/simplicial_complex_morphism.py b/src/sage/homology/simplicial_complex_morphism.py index 8db933b9fe7..acbe4adf937 100644 --- a/src/sage/homology/simplicial_complex_morphism.py +++ b/src/sage/homology/simplicial_complex_morphism.py @@ -173,7 +173,7 @@ def __init__(self,f,X,Y): for j in tup: fi.append(f[j]) v = Simplex(set(fi)) - if not v in Y_faces[v.dimension()]: + if v not in Y_faces[v.dimension()]: raise ValueError("f must be a dictionary from the vertices of X to the vertices of Y") self._vertex_dictionary = f Morphism.__init__(self, Hom(X,Y,SimplicialComplexes())) @@ -247,10 +247,10 @@ def __call__(self,x,orientation=False): ((0, 1), -1) """ dim = self.domain().dimension() - if not isinstance(x,Simplex) or x.dimension() > dim or not x in self.domain().faces()[x.dimension()]: + if not isinstance(x, Simplex) or x.dimension() > dim or x not in self.domain().faces()[x.dimension()]: raise ValueError("x must be a simplex of the source of f") - tup=x.tuple() - fx=[] + tup = x.tuple() + fx = [] for j in tup: fx.append(self._vertex_dictionary[j]) if orientation: diff --git a/src/sage/homology/simplicial_set_morphism.py b/src/sage/homology/simplicial_set_morphism.py index bbac4531136..7e2c5046ef9 100644 --- a/src/sage/homology/simplicial_set_morphism.py +++ b/src/sage/homology/simplicial_set_morphism.py @@ -453,7 +453,7 @@ def __init__(self, data=None, domain=None, codomain=None, if identity: if codomain is None: codomain = domain - elif not domain is codomain: + elif domain is not codomain: raise TypeError("identity map is only defined for endomorphism sets") self._is_identity = True Morphism.__init__(self, Hom(domain, codomain, SimplicialSets())) @@ -470,10 +470,10 @@ def __init__(self, data=None, domain=None, codomain=None, if identity: self._is_identity = True check = False - if not domain is codomain: + if domain is not codomain: raise TypeError("identity map is only defined for endomorphism sets") data = {} - for i in range(domain.dimension()+1): + for i in range(domain.dimension() + 1): for s in domain.n_cells(i): data[s] = s if constant is not None: diff --git a/src/sage/parallel/map_reduce.py b/src/sage/parallel/map_reduce.py index f8b8342f00d..d51d5d872ee 100644 --- a/src/sage/parallel/map_reduce.py +++ b/src/sage/parallel/map_reduce.py @@ -544,7 +544,7 @@ from collections import deque from threading import Thread -from sage.sets.recursively_enumerated_set import RecursivelyEnumeratedSet # _generic +from sage.sets.recursively_enumerated_set import RecursivelyEnumeratedSet # _generic from sage.misc.lazy_attribute import lazy_attribute import copy import sys @@ -882,7 +882,6 @@ def abort(self): # ActiveTaskCounter = ActiveTaskCounterDarwin # to debug Darwin implementation - class RESetMapReduce(object): r""" Map-Reduce on recursively enumerated sets. @@ -944,12 +943,18 @@ def __init__(self, if hasattr(forest, 'post_process'): self.post_process = forest.post_process else: - if roots is not None: self._roots = roots - if children is not None: self.children = children - if post_process is not None: self.post_process = post_process - if map_function is not None: self.map_function = map_function - if reduce_function is not None: self.reduce_function = reduce_function - if reduce_init is not None: self._reduce_init = reduce_init + if roots is not None: + self._roots = roots + if children is not None: + self.children = children + if post_process is not None: + self.post_process = post_process + if map_function is not None: + self.map_function = map_function + if reduce_function is not None: + self.reduce_function = reduce_function + if reduce_init is not None: + self._reduce_init = reduce_init self._profile = None @lazy_attribute @@ -973,7 +978,6 @@ def _forest(self): structure='forest', enumeration='depth') - def roots(self): r""" Return the roots of ``self``. @@ -1043,7 +1047,7 @@ def reduce_function(self, a, b): sage: S.reduce_function(4, 3) 12 """ - return a+b + return a + b def post_process(self, a): r""" @@ -1070,7 +1074,6 @@ def post_process(self, a): """ return a - _reduce_init = 0 def reduce_init(self): @@ -1159,7 +1162,8 @@ def start_workers(self): logger.debug("Starting processes") sys.stdout.flush() sys.stderr.flush() - for w in self._workers: w.start() + for w in self._workers: + w.start() def get_results(self, timeout=None): r""" @@ -1207,7 +1211,6 @@ def get_results(self, timeout=None): return res - def finish(self): r""" Destroy the workers and all the communication objects. @@ -1403,7 +1406,7 @@ def random_worker(self): sage: del EX._results, EX._active_tasks, EX._done, EX._workers """ - victim = random.randint(0, len(self._workers)-1) + victim = random.randint(0, len(self._workers) - 1) return self._workers[victim] def run(self, @@ -1464,7 +1467,7 @@ def run(self, Computation normally finished 40320*x^8 + 5040*x^7 + 720*x^6 + 120*x^5 + 24*x^4 + 6*x^3 + 2*x^2 + x + 1 """ - self._profile=profile + self._profile = profile self.setup_workers(max_proc, reduce_locally) self.start_workers() if timeout is not None: @@ -1518,11 +1521,12 @@ def print_communication_statistics(self, blocksize=16): res = [""] # classic trick to have a local variable shared with the # local function (see e.g: # https://stackoverflow.com/questions/2609518/python-nested-function-scopes). + def pstat(name, start, end, ist): res[0] += ("\n" + name + " ".join( "%4i" % (self._stats[i][ist]) for i in range(start, end))) for start in range(0, self._nprocess, blocksize): - end = min(start+blocksize, self._nprocess) + end = min(start + blocksize, self._nprocess) res[0] = ("#proc: " + " ".join("%4i" % (i) for i in range(start, end))) pstat("reqs sent: ", start, end, 0) @@ -1589,7 +1593,7 @@ def __init__(self, mapred, iproc, reduce_locally): # on the following Pipe. So there is no need to have a queue. self._read_task, self._write_task = mp.Pipe(duplex=False) self._mapred = mapred - self._stats = mp.RawArray('i', 4) + self._stats = mp.RawArray('i', 4) self._reduce_locally = reduce_locally def _thief(self): @@ -1602,7 +1606,7 @@ def _thief(self): try: for ireq in iter(self._request.get, AbortError): - reqs +=1 + reqs += 1 target = self._mapred._workers[ireq] logger.debug("Got a Steal request from %s" % target.name) self._mapred._signal_task_start() @@ -1741,7 +1745,7 @@ def run_myself(self): self._stats[0] = 0 self._stats[3] = 0 logger.debug("Launching thief") - self._thief = Thread(target = self._thief, name="Thief") + self._thief = Thread(target=self._thief, name="Thief") self._thief.start() self._res = reduce_init() @@ -1871,7 +1875,7 @@ class RESetMPExample(RESetMapReduce): .. SEEALSO:: This is an example of :class:`RESetMapReduce` """ - def __init__(self, maxl = 9): + def __init__(self, maxl=9): r""" TESTS:: @@ -1915,8 +1919,8 @@ def children(self, l): sage: RESetMPExample().children([1,0]) [[2, 1, 0], [1, 2, 0], [1, 0, 2]] """ - return [ l[:i] + [len(l)] + l[i:] - for i in range(len(l)+1) ] if len(l) < self.maxl else [] + return [l[:i] + [len(l)] + l[i:] + for i in range(len(l) + 1)] if len(l) < self.maxl else [] def map_function(self, l): r"""