def test_integrator_comparison(): f = lambda x: np.sin(x) F = lambda x: -np.cos(x) expected_answer = F(np.pi) - F(0) a, b, N = 0, np.pi, 129000 #Lowest N for normal integrate. a, b, B = 0, np.pi, 90700 #Lowest N for mid integrate. assert (Integrator.integrate(f, a, b, N) - expected_answer < 1E-10) assert (Integrator.midpoint_integrate(f, a, b, B) - expected_answer < 1E-10) assert (numpy_integrator.integrator(f, a, b, N) - expected_answer < 1E-10) assert (numpy_integrator.midpoint_integrator(f, a, b, B) - expected_answer < 1E-10) assert (abs(numba(f, a, b, N) - expected_answer) < 1E-10) assert (abs(numba_integrate(f, a, b, B) - expected_answer) < 1E-10) assert (abs( cython_integrator.integrate_g(a, b, N) - expected_answer < 1E-10)) assert (abs( cython_integrator.cython_integrate_mid(a, b, B) - expected_answer < 1E-10)) with open('report6.txt', 'a') as out: out.write("Lowest N for normal integrate is: {}.\n".format(N)) out.write("Lowest N for mid integrate is: {}.\n".format( B)) #Printing result to report3.txt
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
def timings(): N = int(1e7) def a(x): return x**2 start_time = time.time() C = numba_integrate(a, 0, 1, N) E = time.time() - start_time print("Python with numba time: %f seconds. With N=%.0e" % (E, N)) start_time_ = time.time() A = integrate(a, 0, 1, N) E = time.time() - start_time print("Normal Python code time: %.5f. With N=%.0e" % (E, N)) start_time = time.time() B = numpy_integrate(a, 0, 1, N) E = time.time() - start_time print("Numpy code time: %f seconds. With N=%.0e" % (E, N))
approx = 0 i = 1 while abs(approx - float(2.0)) > 10e-10: #for i in range(1,3): #print ("here",i,numpy_integrate(f5,0, math.pi,i*1000)) approx = numpy_integrate(f5, 0, math.pi, i * 1000) i = i + 1 print("iterations required with numpy_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_integrate(f5, 0, math.pi, i * 1000) i = i + 1 print("iterations required with numba_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_integrate(f5, 0, math.pi, i * 1000) i = i + 1 print("iterations required with cython_integrate", i) approx = 0 i = 1 while abs(approx - float(2.0)) > 10e-10:
def test_numba_integrate_linear_function(): computed_ans = numba_integrate(f2, 0, 1, 10) #N = 10 assert abs(1.0 - computed_ans) < (1.0 / 10 + 1E-10) computed_ans = numba_integrate(f2, 0, 1, 1000000) #N = 1000000 assert abs(1.0 - computed_ans) < (1.0 / 1000000 + 1E-10)
def test_numba_integrate_constant_function(): computed_ans = numba_integrate(f, 0, 1, 10) #N = 10 assert abs(2.0 - computed_ans) < 1E-10 computed_ans = numba_integrate(f, 0, 1, 10000) #N = 10000 assert abs(2.0 - computed_ans) < 1E-10
#!/usr/bin/python """Import integrate function from integrator.py""" from math import pi, sin from integrator import integrate, midpoint_integrate from numpy_integrator import numpy_integrate, midpoint_numpy_integrate from numba_integrator import numba_integrate, midpoint_numba_integrate import cython_integrator #def comparison(f, a, b, N): a = 0 b = pi f = lambda x: sin(x) N = 100000 exact_answer = 2.0 integrate(f, a, b, N) midpoint_integrate(f, a, b, N) numpy_integrate(f, a, b, N) midpoint_numpy_integrate(f, a, b, N) numba_integrate(f, a, b, N) midpoint_numba_integrate(f, a, b, N) cython_integrator.cython_integrate(a, b, N) cython_integrator.midpoint_cython_integrate(a, b, N)
#FOR NUMPY MIDPOINT INTEGRATE for n in range(100, N, jump): I = numpy_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 numpy_midpoint_integrate" % (n)) #print("I = %f" % (I)) 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(