To "vectorize" using numpy
, all this means is that instead of doing an explicit loop like,
for i in range(1, n):
c = c + f(i)
Then instead you should make i
into a numpy array, and simply take its sum:
i = np.arange(1,n)
c = i.sum()
And numpy automatically does the vectorization for you. The reason this is faster is because numpy loops are done in a better optimized way than a plain python loop, for a variety of reasons. Generally speaking, the longer the loop/array, the better the advantage. Here is your trapezoidal integration implemented:
import numpy as np
def f1(x):
return 2*x + 1
# Here's your original function modified just a little bit:
def integ(f,a,b,n):
h = (b-a)/n
a1 = (h/2)*f(a)
b1 = (h/2)*f(b)
c1 = 0
for i in range(1,n,1):
c1 = f((a+i*h))+c1
return a1 + b1 + h*c1
# Here's the 'vectorized' function:
def vinteg(f, a, b, n):
h = (b-a) / n
ab = 0.5 * h * (f(a)+f(b)) #only divide h/2 once
# use numpy to make `i` a 1d array:
i = np.arange(1, n)
# then, passing a numpy array to `f()` means that `f` returns an array
c = f(a + h*i) # now c is a numpy array
return ab + c.sum() # ab + np.sum(c) is equivalent
Here I will import what I named tmp.py
into an ipython
session for easier timing than using time.time
:
import trap
f = trap.f1
a = 0
b = 100
n = 1000
timeit trap.integ(f, a, b, n)
#1000 loops, best of 3: 378 us per loop
timeit trap.vinteg(f, a, b, n)
#10000 loops, best of 3: 51.6 us per loop
Wow, seven times faster.
See if it helps much for smaller n
n = 10
timeit trap.integ(f, a, b, n)
#100000 loops, best of 3: 6 us per loop
timeit trap.vinteg(f, a, b, n)
#10000 loops, best of 3: 43.4 us per loop
Nope, much slower for small loops! What about very large n
?
n = 10000
timeit trap.integ(f, a, b, n)
#100 loops, best of 3: 3.69 ms per loop
timeit trap.vinteg(f, a, b, n)
#10000 loops, best of 3: 111 us per loop
Thirty times faster!