def integrate(f, a, b, n, method="default", implementation="default"): sum_of_integral = 0 if implementation == "default": if method == "default": sum_of_integral = integrate_default(f, a, b, n) elif method == "midpoint": sum_of_integral = midpoint_integrate(f, a, b, n) if implementation == "numpy": if method == "default": sum_of_integral = numpy_integrator.numpy_integrate(f, a, b, n) elif method == "midpoint": sum_of_integral = numpy_integrator.numpy_midpoint_integrate( f, a, b, n) if implementation == "numba": if method == "default": sum_of_integral = numba_integrator.numba_integrate(f, a, b, n) elif method == "midpoint": sum_of_integral = numba_integrator.numba_midpoint_integrate( f, a, b, n) if implementation == "cython": if method == "default": sum_of_integral = cython_integrator.cython_integrate(f, a, b, n) elif method == "midpoint": sum_of_integral = cython_integrator.cython_midpoint_integrate( f, a, b, n) return sum_of_integral
approx = midpoint_integrate(f5, 0, math.pi, i * 1000) i = i + 1 print("iterations required with midpoint_integrate", i) approx = 0 i = 1 while abs(approx - float(2.0)) > 10e-10: #for i in range(1,10): #print ("here",i,integrate(f5,0, math.pi,i*1000)) approx = numpy_midpoint_integrate(f5, 0, math.pi, i * 1000) i = i + 1 print("iterations required with numpy_midpoint_integrate", i) approx = 0 i = 1 while abs(approx - float(2.0)) > 10e-10: #for i in range(1,10): #print ("here",i,integrate(f5,0, math.pi,i*1000)) approx = numba_midpoint_integrate(f5, 0, math.pi, i * 1000) i = i + 1 print("iterations required with numba_midpoint_integrate", i) approx = 0 i = 1 while abs(approx - float(2.0)) > 10e-10: #for i in range(1,10): #print ("here",i,integrate(f5,0, math.pi,i*1000)) approx = cython_midpoint_integrate(f5, 0, math.pi, i * 1000) i = i + 1 print("iterations required with cython_midpoint_integrate", i)
def test_numba_midpoint_integrate_linear_function(): computed_ans = numba_midpoint_integrate(f2, 0, 1, 10) #N = 10 assert abs(1.0 - computed_ans) < (1.0 / 10 + 1E-10) computed_ans = numba_midpoint_integrate(f2, 0, 1, 1000000) #N = 1000000 assert abs(1.0 - computed_ans) < (1.0 / 1000000 + 1E-10)
def test_numba_midpoint_integrate_constant_function(): computed_ans = numba_midpoint_integrate(f, 0, 1, 10) #N = 10 assert abs(2.0 - computed_ans) < 1E-10 computed_ans = numba_midpoint_integrate(f, 0, 1, 10000) #N = 10000 assert abs(2.0 - computed_ans) < 1E-10
break else: if n >= N - 1: print("Didn't find a large enough N") #FOR NUMBA INTERGATE for n in range(100, N, jump): I = numba_integrate(f, 0, pi, n) if abs(I - 2.0) < 10**(-10): print( "N needs to be %d or higher to have an error less than 10^(-10) \nwhen integrating with numba_integrate" % (n)) print("I = %f" % (I)) break else: if n >= N - jump: print("Didn't find a large enough N") #FOR NUMBA MIDPOINT INTEGRATE for n in range(100, N, jump): I = numba_midpoint_integrate(f, 0, pi, n) if abs(I - 2.0) < 10**(-10): print( "N needs to be %d or higher to have an error less than 10^(-10) \nwhen integrating with numba_midpoint_integrate" % (n)) print("I = %f" % (I)) break else: if n >= N - jump: print("Didn't find a large enough N")