def test_integral_of_constant_function(): # test integrate assert abs(integrate(f1,0,1,1)-2)<= 1E-15 #print ("lol",abs(integrate(f1,0,1,10000)-float(2))) assert abs(integrate(f1,0,1,10)-2)<= 1E-12 assert abs(integrate(f1,0,1,100)-2)<= 1E-12 assert abs(integrate(f1,0,1,1000)-2)<= 1E-12 assert abs(integrate(f1,0,1,10000)-2)<= 1E-12 # test numpy integrate assert abs(numpy_integrate(f1,0,1,1)-2)<= 1E-15 #print ("lol",numpy_integrate(f1,0,1,10),abs(numpy_integrate(f1,0,1,10)-float(2))) assert abs(numpy_integrate(f1,0,1,10)-2)<= 1E-12 assert abs(numpy_integrate(f1,0,1,100)-2)<= 1E-12 assert abs(numpy_integrate(f1,0,1,1000)-2)<= 1E-12 assert abs(numpy_integrate(f1,0,1,10000)-2)<= 1E-12 # test Cython assert abs(cython_integrate(f1,0,1,1)-2)<= 1E-15 #print ("lol",numpy_integrate(f1,0,1,10),abs(numpy_integrate(f1,0,1,10)-float(2))) assert abs(cython_integrate(f1,0,1,10)-2)<= 1E-12 assert abs(cython_integrate(f1,0,1,100)-2)<= 1E-12 assert abs(cython_integrate(f1,0,1,1000)-2)<= 1E-12 assert abs(cython_integrate(f1,0,1,10000)-2)<= 1E-12
def test_integral_of_linear_function(): # test integrate N1=10 #print ("here",abs(1-integrate(f2,0,1,N1)-(float(1)/float(N1)))) assert abs(1-integrate(f2,0,1,N1)-(float(1)/float(N1))) <= 1E-15 N2=100 assert abs(1-integrate(f2,0,1,N2)-(float(1)/float(N2))) <= 1E-15 N3=1000 assert abs(1-integrate(f2,0,1,N3)-(float(1)/float(N3))) <= 1E-15 N4=10000 assert abs(1-integrate(f2,0,1,N4)-(float(1)/float(N4))) <= 1E-15 # test numpy integrate N1=10 assert abs(1-numpy_integrate(f2,0,1,N1)-(float(1)/float(N1))) <= 1E-15 N2=100 assert abs(1-numpy_integrate(f2,0,1,N2)-(float(1)/float(N2))) <= 1E-15 N3=1000 assert abs(1-numpy_integrate(f2,0,1,N3)-(float(1)/float(N3))) <= 1E-15 N4=10000 assert abs(1-numpy_integrate(f2,0,1,N4)-(float(1)/float(N4))) <= 1E-15 # test Cython N1=10 assert abs(1-cython_integrate(f2,0,1,N1)-(float(1)/float(N1))) <= 1E-15 N2=100 assert abs(1-cython_integrate(f2,0,1,N2)-(float(1)/float(N2))) <= 1E-15 N3=1000 assert abs(1-cython_integrate(f2,0,1,N3)-(float(1)/float(N3))) <= 1E-15 N4=10000 assert abs(1-cython_integrate(f2,0,1,N4)-(float(1)/float(N4))) <= 1E-15
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 test_cython_integrals_of_linear_function(N,c=1): #c scales error (order of 1/N) f = lambda x: 2*x computed_answer = cython_integrate(f,0,1,N) expected_answer = float(1) error = float(c)/N assert abs(computed_answer-expected_answer) < error
def test_cython_integral_of_constant_function(N): f = lambda x: 2 #Test function computed_answer = cython_integrate(f,0,1,N) #print(computed_answer) expected_answer = 2 assert abs(computed_answer-expected_answer) < 1**(-20)
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: #for i in range(1,10): #print ("here",i,integrate(f5,0, math.pi,i*1000)) 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:
#!/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)
from integrator import integrate import numpy from cython_integrator import cython_integrate import time if __name__ == "__main__": report = open('report5.txt', 'w') f = lambda x: x * x a = 0 b = 1 N = 10000 start1 = time.time() integral = cython_integrate(f, a, b, N) #print(integral) end1 = time.time() report.write("time taken for N = %i using cython is %f \n" % (N, end1 - start1)) start2 = time.time() integral = integrate(f, a, b, N) end2 = time.time() report.write("time taken for N = %i NOT using cython is %f \n" % (N, end2 - start2)) report.write("the difference of time2-time1 if %f \n" % ((end2 - start2) - (end1 - start1)))