Integration with different methods

How can i define my variable method in my function so that my integrate function can calculate the same integral via a chosen method ? Maybe i have to define an alias for the different functions?

import argparse 

def dummy_function(x_value): 
    return 4/(1+x_value**2)

def integrate(method,function,integration_range,n_slices): 
    method = ? 
    return integral_area

def riemann(function, integration_range, n_slices):
    x_start = integration_range[0]
    x_stop = integration_range[1]
    delta = (x_stop-x_start) / n_slices
    divisions = [x_start + j*delta for j in range(n_slices)]
    integral_area=0
    for x_value in divisions:
        integral_area += function(x_value)*delta
    return integral_area

def trapezoid(function, integration_range, n_slices):
    x_start = integration_range[0]
    x_stop = integration_range[1]
    delta = (x_stop-x_start) / n_slices
    divisions = [x_start + j*delta for j in range(n_slices)]
    integral_area=0
    for x_value in divisions:
        integral_area += (function(x_value)+function(x_value + 
        delta))*delta/2
    return integral_area

def simpson(function, integration_range, n_slices):
    x_start = integration_range[0]
    x_stop = integration_range[1]
    delta = (x_stop-x_start) / n_slices
    divisions = [x_start + j*delta for j in range(n_slices)]
    integral_area=0
    for x_value in divisions:
        integral_area += (function(x_value)+ 4*function(x_value + delta/2)
        + function(x_value + delta))*delta/6
    return integral_area

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Calculate Integral') 
    parser.add_argument('-s','--slices', type=int, default=1000000, help='Enter number of slices') 
    parser.add_argument('-m','--method', choices=['riemann','trapezoid','simpson'], default='riemann' ,help='Enter integration method') 
    args = parser.parse_args() 
    N_SLICES = args.slices 
    method = args.method 
    INTEGRAL_RANGE = [0, 1] 
    INTEGRAL_RESULT = integrate(method, dummy_function, INTEGRAL_RANGE, N_SLICES)
    print("Result:", INTEGRAL_RESULT)

i tried some ways but i get errors like ‘str’ object not callable etc.

>Solution :

Something like this should work:

def integrate(method,function,integration_range,n_slices): 
    methods = {
        'riemann': riemann,
        'trapezoid': trapezoid,
        'simpson': simpson
        } 
    return methods[method](function, integration_range, n_slices)

You could simplify this quite a bit though, no need to repeat code:

def integrate(method,function,integration_range,n_slices): 
    methods = {
        'riemann': riemann,
        'trapezoid': trapezoid,
        'simpson': simpson
        } 
    x_start, x_stop = integration_range
    delta = (x_stop-x_start) / n_slices
    divisions = [x_start + j*delta for j in range(n_slices)]
    return methods[method](function, divisions, delta)

def riemann(function, divisions, delta):
    return sum(function(x_value)*delta for x_value in divisions)

def trapezoid(function, divisions, delta):
    return sum((function(x_value)+function(x_value + 
        delta))*delta/2 for x_value in divisions)

def simpson(function, divisions, delta):
    return sum((function(x_value)+ 4*function(x_value + delta/2)
        + function(x_value + delta))*delta/6 for x_value in divisions)

Leave a Reply