Exemplo n.º 1
0
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:
    #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:
Exemplo n.º 2
0
def test_midpoint_integral_of_linear_function():
    computed_ans = midpoint_integrate(f2, 0, 1, 10)  #N = 10
    assert abs(1.0 - computed_ans) < (1.0 / 10 + 1E-10)
    computed_ans = midpoint_integrate(f2, 0, 1, 1000000)  #N = 1000000
    assert abs(1.0 - computed_ans) < (1.0 / 1000000 + 1E-10)
Exemplo n.º 3
0
#!/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)
Exemplo n.º 4
0
def test_midpoint_integral_of_constant_function():
    f1 = vectorize(f)
    computed_ans = midpoint_integrate(f1, 0, 1, 10)  #N = 10
    assert abs(2.0 - computed_ans) < 1E-10
    computed_ans = midpoint_integrate(f1, 0, 1, 10000)  #N = 10000
    assert abs(2.0 - computed_ans) < 1E-10
#FOR INTEGRATE
for n in range(100, N, jump):
    I = 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 integrate"
            % (n))
        break
    else:
        if n >= N - jump:
            print("Didn't find a large enough N")

#FOR MIDPOINT INTEGRATE
for n in range(100, N, jump):
    I = 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 midpoint_integrate"
            % (n))
        break
    else:
        if n >= N - jump:
            print("Didn't find a large enough N")

#FOR NUMPY INTEGRATE
for n in range(100, N, jump):
    I = numpy_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_integrate"