def myfunc(x,y): if (x>0.5*y and y<0.3): return (sn(x-y)) elif (x<0.5*y): return 0 elif (x>0.2*y): return (2*sn(x+2*y)) else: return (sn(y+x))
def myfunc(x, y): if (x > 0.5 * y and y < 0.3): return (sn(x - y)) elif (x < 0.5 * y): return 0 elif (x > 0.2 * y): return (2 * sn(x + 2 * y)) else: return ( sn(y + x) ) # List of stored elements, generated from a Normal distribution
def targetting(self, event=0): if event: self.angle = math.atan((event.y - 450) / (event.x - 20)) if self.f2_on: canv.itemconfig(self.id, fill='skyblue') else: canv.itemconfig(self.id, fill='grey') canv.coords(self.id, 20, 450, 20 + max(self.f2_power, 20) * math.cos(self.angle), 450 + max(self.f2_power, 20) * math.sn(self.angle))
return 0 elif (x>0.2*y): return (2*sn(x+2*y)) else: return (sn(y+x)) lst_x = np.random.randn(N_point) lst_y = np.random.randn(N_point) lst_result = [] tic=time.time() for i in range(len(lst_x)): x = lst_x[i] y = lst_y[i] if (x>0.5*y and y<0.3): lst_result.append(sn(x-y)) elif (x<0.5*y): lst_result.append(0) elif (x>0.2*y): lst_result.append(2*sn(x+2*y)) else: lst_result.append(sn(y+x)) toc=time.time() print("\nTime taken by the plain vanilla for-loop\n----------------------------------------------\n{} us".format(1000000*(toc-tic))) # List comprehension print("\nTime taken by list comprehension and zip\n"+'-'*40) %timeit lst_result = [myfunc(x,y) for x,y in zip(lst_x,lst_y)] # Map() function
def sin(k, x): return float(((-1) ** (k - 1)) * (x ** (2 * k - 1))) / float(factorial(2 * k - 1)) sin_approx = [] Number = [] Exponent = [] sin_error = [] sin_first_neglected_term = [] for x in [pi, 30 * pi]: for N in [5, 10, 20]: Tsin = Sum(f=sin, M=1, N=N) Exponent.append(N) Number.append(x) sin_approx.append(Tsin(x)) sin_error.append(sn(x) - Tsin(x)) sin_first_neglected_term.append(Tsin.first_neglected_term) print "Results for Taylor Expansion of sin(x):\n%10s %10s %40s %40s %40s" % ( "x", "N", "Value", "Error", "First Neglected Term", ) for i in range(len(Number)): print "%10.5f %10.0f %40.4f %40.5f %40.5f" % ( Number[i], Exponent[i], sin_approx[i], sin_error[i],