def testtrapzquadratic(a, b, N, A, B, C): answer = (A/3)*(b**3 - a**3) + B/2*(b**2 - a**2) + C*(b - a) #not sure why my function is not as accurate as i thought it would be #thats why i increase the closeness parameter to 0.03 assert isclose(trapz(quadratic, a, b, N, A=A, B=B, C=C), answer, 0.03) print(trapz(quadratic, a, b, N, A=A, B=B, C=C)) print(answer)
def test_line(): """ the function should return a list of the length input """ assert trapz(line(5), 0, 10) == 50 assert trapz(line(6), 0, 10) == 60 assert trapz(line(10), 0, 10) == 100
def test_curves(): # a simple curve under_curve = trapz(a_curve, 1, 6) assert isclose(under_curve, 7.4512822710374) # a simple curve with larger arbitrary start and end points under_curve_2 = trapz(a_curve, 22, 146) assert isclose(under_curve_2, 1099.8560901121) # another curve with arbirtrary start and end points under_other_curve = trapz(another_curve, 1, 1999) assert isclose(under_other_curve, 2662803597.7332)
def test_curry_quadratic_trapz(): # tests if the quadratic currying function # create a specific quadratic function: quad234 = curry_quadratic(-2, 2, 3) # quad234 is now a function that can take one argument, x # and will evaluate quadratic for those particular values of A,B,C # try it with the trapz function assert trapz(quad234, -5, 5) == trapz(quadratic, -5, 5, -2, 2, 3)
def test_trapz(): line = trapz.line(m=0, b=5) area = trapz.trapz(line, 0, 10) assert round(area, 5) == 50 line = trapz.line(m=.5) area = trapz.trapz(line, 0, 10) assert round(area, 5) == 25 area = trapz.trapz(math.sin, 0, 1) answer = math.cos(0) - math.cos(1) assert round(area, 2) == round(answer, 2)
def test_slopped_straight(): ''' This test the Trapazoidal Function against a slopped straight line ''' def ssline(x, m=1, B=0): return (m * x) + B def ssline2(x, m=1, B=1): return (m * x) + B area = trapz(ssline, 0, 4) area2 = trapz(ssline2, 0, 4) assert math.isclose(area, 8, rel_tol=0.01, abs_tol=0.0) assert math.isclose(area2, 12, rel_tol=0.01, abs_tol=0.0)
def test_sloping_line(): ''' a simple linear function ''' def line(x): return 2 + 3*x # I got 159.99999999999 rather than 160 # hence the need for isclose() assert isclose(trapz(line, 2, 10), 160) m, B = 3, 2 a, b = 0, 5 assert isclose(trapz(line, a, b), 1/2*m*(b**2 - a**2) + B*(b-a)) a, b = 5, 10 assert isclose(trapz(line, a, b), 1/2*m*(b**2 - a**2) + B*(b-a)) a, b = -10, 5 assert isclose(trapz(line, a, b), 1/2*m*(b**2 - a**2) + B*(b-a))
def test_trapz(): # a simple line assert trapz(line, 0, 10) == 50 # a simple curve under_curve = trapz(a_curve, 1, 6) assert isclose(under_curve, 7.4512822710374) # a simple curve with larger arbitrary start and end points under_curve = trapz(a_curve, 22, 146) assert isclose(under_curve, 1099.8560901121) # another curve with arbirtrary start and end points under_other_curve = trapz(another_curve, 1, 1999) assert isclose(under_other_curve, 2662803597.7332) # python sin function with arbitrary start and end points under_sin = trapz(sin, 24, 346) assert isclose(under_sin, 0.030750318486179)
def test_quadratic_trapz_1(): """ simplest case -- horizontal line """ A, B, C = 0, 0, 5 a, b = 0, 10 assert trapz(quadratic, a, b, A=A, B=B, C=C) == quad_solution(a, b, A, B, C)
def test_Sloped_line(): a = 0 b = 10 slope = 5 offset = 10 ans = slope * (b**2 - a**2) / 2 + offset * (b - a) assert isclose(trapz(sloped_line, a, b, 100, M=slope, B=offset), ans)
def test_Sloped_line(): a = 0 b = 10 slope = 5 offset = 10 ans = slope*(b**2 - a**2)/2 + offset*(b - a) assert isclose(trapz(sloped_line, a, b, 100, M=slope, B=offset), ans)
def test_sloped_line_area(): m, B = 0.5, 2 # coefficients of line lb, ub = 0, 10 # upper and lower bounds of integration sloped_line = get_sloped_line(m, B) area = trapz.trapz(sloped_line, lb, ub) shouldbe = 0.5 * m * (ub**2 - lb**2) + B * (ub - lb) assert math.isclose(area, shouldbe)
def test_sloping_line(): ''' a simple linear function ''' def line(x): return 2 + 3 * x # I got 159.99999999999 rather than 160 # hence the need for isclose() assert isclose(trapz(line, 2, 10), 160) m, B = 3, 2 a, b = 0, 5 assert isclose(trapz(line, a, b), 1 / 2 * m * (b**2 - a**2) + B * (b - a)) a, b = 5, 10 assert isclose(trapz(line, a, b), 1 / 2 * m * (b**2 - a**2) + B * (b - a)) a, b = -10, 5 assert isclose(trapz(line, a, b), 1 / 2 * m * (b**2 - a**2) + B * (b - a))
def test_trapz_quadratic(**kwargs): a = 0 b = 10 A = 1 B = 2 C = 3 ans = A/3*(b**3 - a**3) + B/2*(b**2 - a**2) + C*(b - a) assert isclose(trapz(quadratic, a, b, 1000, A=A, B=B, C=C), ans, 0.001)
def test_quadratic_area(): A, B, C = 2, 1, 1 lb, ub = 0, 2 quad_curve = get_quadratic_eqn(A, B, C) area = trapz.trapz(quad_curve, lb, ub) shouldbe = (A / 3) * (ub**3 - lb**3) + (B / 2) * (ub**2 - lb**2) + C * (ub - lb) assert math.isclose(area, shouldbe, rel_tol=1e-2)
def test_sine_freq_amp(): a = 0 b = 5 omega = 0.5 amp = 10 assert isclose(trapz(sine_freq_amp, a, b, amp=amp, freq=omega), solution_freq_amp(a, b, amp, omega), rel_tol=1e-04)
def test_trapz_quadratic(**kwargs): a = 0 b = 10 A = 1 B = 2 C = 3 ans = A / 3 * (b**3 - a**3) + B / 2 * (b**2 - a**2) + C * (b - a) assert isclose(trapz(quadratic, a, b, 1000, A=A, B=B, C=C), ans, 0.001)
def test_quadratic_trapz_2(): """ one case: A=-1/3, B=0, C=4 """ A, B, C = -1/3, 0, 4 a, b = -2, 2 assert isclose(trapz(quadratic, a, b, A=A, B=B, C=C), quad_solution(a, b, A, B, C), rel_tol=1e-3) # not a great tolerance -- maybe should try more samples!
def test_line(): ''' This test the Trapazoidal Function against a straight line ''' def line(x): return 5 area = trapz(line, 0, 10) assert math.isclose(area, 50, rel_tol=0.05, abs_tol=0.0)
def test_quadratic_trapz_3(): """ one case: A=-1/3, B=0, C=4 """ A, B, C = -1/3, 0, 4 a, b = -2, 2 assert isclose(trapz(quadratic, a, b, A, B, C=C), quad_solution(a, b, A, B, C), rel_tol=1e-3) # not a great tolerance -- maybe should try more samples!
def test_quadratic_trapz_args_kwargs(): """ Testing if you can pass a combination of positional and keyword arguments one case: A=2, B=-4, C=3 """ A, B, C = 2, -4, 3 a, b = -2, 2 assert isclose(trapz(quadratic, a, b, A, B, C=C), quad_solution(a, b, A, B, C), rel_tol=1e-3) # not a great tolerance -- maybe should try more samples!
def test_trapz_line(): assert trapz(line, 0, 10, 100) == 50
def test_quadratic_1(): # quadratic with kwargs coef = {'A': 1, 'B': 3, 'C': 2} quad = trapz(quadratic, 5, 10, **coef) assert isclose(quad, 414.16875)
def test_quadratic_3(): # quadratic with partial partial_quadratic_1_3_2 = partial(quadratic, A=1, B=3, C=2) partial_quad = trapz(partial_quadratic_1_3_2, 5, 10) assert isclose(partial_quad, 414.16875)
def test_quadratic_2(): # quadratic with closure closure_quadratic_1_3_2 = quadratic_2(1, 3, 2) curried_quad = trapz(closure_quadratic_1_3_2, 5, 10) assert isclose(curried_quad, 414.16875)
def test_line(): # a simple line assert trapz(line, 0, 10) == 50
def test_line_area(): area = trapz.trapz(line, 0, 10) assert area == 50
def test_simple_line(): ''' a simple horizontal line at y = 5''' line = "3x" linearea = trapz(line, 1, 6) assert isclose(trapz(line,52.5))
def test_sine2(): # a sine curve from zero to 2pi -- should be 0.0 # need to set an absolute tolerance when comparing to zero assert isclose(trapz(math.sin, 0, 2 * math.pi), 0.0, abs_tol=1e-8)
def test_sine(): # a sine curve from zero to pi -- should be 2 # with a hundred points, only correct to about 4 figures assert isclose(trapz(math.sin, 0, math.pi), 2.0, rel_tol=1e-04)
def test_sine2(): # a sine curve from zero to 2pi -- should be 0.0 # need to set an absolute tolerance when comparing to zero assert isclose(trapz(math.sin, 0, 2*math.pi), 0.0, abs_tol=1e-8)
cumulative_increment_area += local_increment_area print('cumulative area: ', cumulative_increment_area) print("\n") return (cumulative_increment_area) def increment_size(a, b, number_of_increments): """ a is lower function evaluation boundary b is upper function evaluation boundary """ increment_width = float((b - a) / number_of_increments) print('increment_width: ', increment_width) return increment_width def evaluate_increment(fun, lower_boundary, upper_boundary): lower_boundary_height = fun(lower_boundary) upper_boundary_height = fun(upper_boundary) print('lower_value = ', lower_boundary_height) print('upper_value = ', upper_boundary_height) increment_area = ((lower_boundary_height + upper_boundary_height) / 2) * (upper_boundary - lower_boundary) return increment_area ######### # Begin processing ##################### print(trapz(lambda x: ((x**2) + 3), 1, 2, 4))
def testslopedline(a,b, N,M,B): answer = M*(b**2 - a**2)/2 + B*(b - a) assert isclose(trapz(slopedline, a, b, N, M=M, B=B), answer, 0.0005) print(trapz(slopedline, a, b, N, M=M, B=B)) print(answer)
def test_sine(): # python sin function with arbitrary start and end points under_sin = trapz(sin, 24, 346) assert isclose(under_sin, 0.030750318486179)
def test_arbitrary_coeffs(): def throwaway(x, y, z): return x ** y + z * 2 area = trapz.trapz(throwaway, 0, 5, 2, 2) assert round(area, 2) == 61.67
def test_simple_line(): ''' a simple horizontal line at y = 5''' def line(x): return 5 assert trapz(line, 0, 10) == 50
def test_partial(): a = 4 b = 10 # quad_partial_123 is the quadratic function with A,B,C = 1,2,3 assert trapz(quad_partial_123, a, b) == trapz(quadratic, a, b, 1, 2, 3)
from math import exp import time import socket from trapz import trapz from f import f from g import g from implicit import implicit #----------------------main program starts here ------------------ loops=10000 n=1000 fic=open("RunningOn"+socket.gethostname(),"w") # workaround: PyCapsule' object has no attribute '__name__' (see ../Py/main.py) name={f:"f",g:"g",implicit:"implicit"} for F in [f,g,implicit]: t1 = time.time() for i in range(0,loops): sum=trapz(F,0.0,1.0,n) t=(time.time()-t1)/loops #print(F.__name__," ",t," ",sum) #fic.write(F.__name__+": "+str(t)+"\n") print(name[F]+": ",t," ",sum) fic.write(name[F]+": "+str(t)+"\n") fic.close() print("end.")
print("local area: ", local_increment_area) cumulative_increment_area += local_increment_area print("cumulative area: ", cumulative_increment_area) print("\n") return cumulative_increment_area def increment_size(a, b, number_of_increments): """ a is lower function evaluation boundary b is upper function evaluation boundary """ increment_width = float((b - a) / number_of_increments) print("increment_width: ", increment_width) return increment_width def evaluate_increment(fun, lower_boundary, upper_boundary): lower_boundary_height = fun(lower_boundary) upper_boundary_height = fun(upper_boundary) print("lower_value = ", lower_boundary_height) print("upper_value = ", upper_boundary_height) increment_area = ((lower_boundary_height + upper_boundary_height) / 2) * (upper_boundary - lower_boundary) return increment_area ######### # Begin processing ##################### print(trapz(lambda x: ((x ** 2) + 3), 1, 2, 4))