Some code for the characteristic function:
import numpy as np from scipy.integrate import quad import matplotlib.pyplot as plt # characteristic function of interval [a,b] def Char(x,a,b): x=np.asarray(x) y=np.zeros(x.shape) y+= ((x>=a)&(x<b)) *1 y+=(( x<a)&(x>b)) *0 return y # integrate the characteristic function of [-1,1] u = lambda x: Char(x,-1,1) integral= quad( u ,-2,2)[0] print "The integral is %r." %integral # plot the characteristic function of [-1,1,5] x = np.arange(-2.0, 2.0, .01) y= Char(x,-1,1.5) plt.plot(x,y) plt.xlim( (-2,2) ) plt.ylim( (-1,2) ) plt.axhline(linewidth=1,color="black") plt.axvline(linewidth=1,color="black") plt.show()