コード例 #1
0
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
コード例 #2
0
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
コード例 #3
0
ファイル: integrator.py プロジェクト: Alexanderws/inf4331
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
コード例 #4
0
def test_integral_of_linear_function():

	expected_answer = 1
	f = lambda x: 2*x; a = 0; b = 1; N = 1000000
	computed_answer = numpy_integrate(f, a, b, N)
	#assert abs(computed_answer - expected_answer) < 1E-20, "Computational error to large!"
	if abs(computed_answer - expected_answer) < (1./N):
		print ("Computation ok")
	else:
		print ("Computational error to large, error = %f" % (1./N))
コード例 #5
0
def test_integral_of_constant_function():

	#abs(computed_answer - expected_answer) < 1E-20
	expected_answer = 2
	f = lambda x: 2; a = 0; b = 1; N = 1000000
	#vetorize f to avoid function turning to constant
	f = np.vectorize(f)
	computed_answer = numpy_integrate(f, a, b, N)
	print (computed_answer)
	#assert abs(computed_answer - expected_answer) < 1E-20, "Computational error to large!"
	if abs(computed_answer - expected_answer) < 1E-10:
		print ("Computation ok")
	else:
		print ("Computational error to large!")
コード例 #6
0
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))
コード例 #7
0
def test_integral_with_numpy_contest():

    computed_answer1 = numpy_integrate(
        lambda x: ((1 / np.pi) * (np.sin(x) / x) * (np.sin(
            (x / 3)) / (x / 3)) * (np.sin((x / 5)) / (x / 5))), 0, 50, 10000)
    print("første: ", computed_answer1)

    computed_answer2 = numpy_integrate(
        lambda x: ((1 / np.pi) * (np.sin(x) / x) * (np.sin(
            (x / 3)) / (x / 3)) * (np.sin((x / 5)) / (x / 5)) * (np.sin(
                (x / 7)) / (x / 7))), 0, 50, 10000)
    print("andre: ", computed_answer2)

    computed_answer3 = numpy_integrate(
        lambda x: ((1 / np.pi) * (np.sin(x) / x) * (np.sin(
            (x / 3)) / (x / 3)) * (np.sin((x / 5)) / (x / 5)) * (np.sin(
                (x / 7)) / (x / 7)) * (np.sin((x / 9)) / (x / 9)) * (np.sin(
                    (x / 11)) / (x / 11))), 0, 50, 10000)
    print("tredje: ", computed_answer3)

    computed_answer4 = numpy_integrate(
        lambda x: ((1 / np.pi) * (np.sin(x) / x) * (np.sin(
            (x / 3)) / (x / 3)) * (np.sin((x / 5)) / (x / 5)) * (np.sin(
                (x / 7)) / (x / 7)) * (np.sin((x / 9)) / (x / 9)) * (np.sin(
                    (x / 11)) / (x / 11)) * (np.sin((x / 13)) / (x / 13))), 0,
        50, 10000)
    print("fjerde: ", computed_answer4)

    computed_answer5 = numpy_integrate(
        lambda x: ((1 / np.pi) * (np.sin(x) / x) * (np.sin(
            (x / 3)) / (x / 3)) * (np.sin((x / 5)) / (x / 5)) * (np.sin(
                (x / 7)) / (x / 7)) * (np.sin((x / 9)) / (x / 9)) * (np.sin(
                    (x / 11)) / (x / 11)) * (np.sin((x / 13)) / (x / 13)) *
                   (np.sin((x / 15)) / (x / 15))), 0, 50, 10000)
    print("femte: ", computed_answer5)

    computed_answer6 = numpy_integrate(
        lambda x: ((1 / np.pi) * (np.sin(x) / x) * (np.sin(
            (x / 4)) / (x / 4)) * (np.sin((x / 4)) / (x / 4)) * (np.sin(
                (x / 7)) / (x / 7)) * (np.sin((x / 7)) / (x / 7)) * (np.sin(
                    (x / 9)) / (x / 9)) * (np.sin((x / 9)) / (x / 9))), 0, 50,
        10000)
    print("sjette: ", computed_answer6)
コード例 #8
0
def test_numpy_integral_of_linear_function():
    def f(x):
        return 2 * x

    for i in range(1, 4):
        assert numpy_integrate(f, 0, 1, 10**i) - 2 < 1E20
コード例 #9
0
def test_numpy_integral_of_constant_function():
    def f(x):
        return 2

    for i in range(1, 4):
        assert numpy_integrate(f, 0, 1, 10**i) - 2 < 1E20
コード例 #10
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 = integrate(f5, 0, math.pi, i * 1000)
    i = i + 1
print("iterations required with integrate", i)

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:
コード例 #11
0
from integrator import integrate
from numpy_integrator import numpy_integrate


def f(x):
    return x**2


print(integrate(f, 0, 1, 10000))
print(numpy_integrate(f, 0, 1, 10000))
コード例 #12
0
def test_numpy_integrate_linear_function():
    computed_ans = numpy_integrate(f2, 0, 1, 10)  #N = 10
    assert abs(1.0 - computed_ans) < (1.0 / 10 + 1E-10)
    computed_ans = numpy_integrate(f2, 0, 1, 1000000)  #N = 1000000
    assert abs(1.0 - computed_ans) < (1.0 / 1000000 + 1E-10)
コード例 #13
0
def test_numpy_integrate_constant_function():
    f1 = vectorize(f)
    computed_ans = numpy_integrate(f1, 0, 1, 10)  #N = 10
    assert abs(2.0 - computed_ans) < 1E-10
    computed_ans = numpy_integrate(f1, 0, 1, 10000)  #N = 10000
    assert abs(2.0 - computed_ans) < 1E-10
コード例 #14
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)
コード例 #15
0
def test_numpy_integral_of_constant_function(N):
	f = lambda x: 2 #Test function
	computed_answer = numpy_integrate(f,0,1,N)
	expected_answer = 2
	assert abs(computed_answer-expected_answer) < 1**(-20)
コード例 #16
0
def test_numpy_integrals_of_linear_function(N,c=1): #c scales error (order of 1/N)
	f = lambda x: 2*x
	computed_answer = numpy_integrate(f,0,1,N)
	expected_answer = float(1)
	error = float(c)/N
	assert abs(computed_answer-expected_answer) < error
コード例 #17
0
#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"
            % (n))
        #print("I = %f" % (I))
        break
    else:
        if n >= N - 1:
            print("Didn't find a large enough 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(