computing integrals vectorization the functions midpoint and trapezoid usually run fast in python and compute an integral to satisfactory precision within fraction of second howeverlong loops in python may run slowly in more complicated implementations to increase the speedthe loops can be replaced by vectorized code the integration functions constitute simple and good example to illustrate how to vectorize loops we have already seen simple examples on vectorization in sect when we could evaluate mathematical function xfor large number of values stored in an array basicallywe can write def ( )return exp(- )*sin( * from numpy import expsinlinspace linspace( coordinates from intervals on [ (xall points evaluated at once the result is the array that would be computed if we ran for loop over the individual values and called for each value vectorization essentially eliminates this loop in python ( the looping over and application of to each value are instead performed in library with fastcompiled codevectorizing the midpoint rule the aim of vectorizing the midpoint and trapezoidal functions is also to remove the explicit loop in python we start with vectorizing the midpoint function since trapezoid is not equally straightforward the fundamental ideas of the vectorized algorithm are to compute all the evaluation points in one array call (xto produce an array of corresponding function values use the sum function to sum the (xvalues the evaluation points in the midpoint method are xi /hi that isn uniformly distributed coordinates between = and = such coordinates can be calculated by linspace( + / - / ngiven that the python implementation of the mathematical function works with an array argumentwhich is very often the case in pythonf(xwill produce all the function values in an array the array elements are then summed up by sumsum( ( )this sum is to be multiplied by the rectangle width to produce the integral value the complete function is listed below from numpy import linspacesum def midpoint(fabn) float( - )/ linspace( / / nreturn *sum( ( )the code is found in the file integration_methods_vec py