def bench_random(self,level=5): print print ' Shifting periodic functions' print '==============================' print ' size | optimized | naive' print '------------------------------' for size,repeat in [(100,1500),(1000,300), (256,1500), (512,1000), (1024,500), (2048,200), (2048*2,100), (2048*4,50), ]: print '%6s' % size, sys.stdout.flush() x = arange (size)*2*pi/size a = 1 if size<2000: f = sin(x)*cos(4*x)+exp(sin(3*x)) sf = sin(x+a)*cos(4*(x+a))+exp(sin(3*(x+a))) else: f = sin(x)*cos(4*x) sf = sin(x+a)*cos(4*(x+a)) assert_array_almost_equal(direct_shift(f,1),sf) assert_array_almost_equal(shift(f,1),sf) print '| %9.2f' % self.measure('shift(f,a)',repeat), sys.stdout.flush() print '| %9.2f' % self.measure('direct_shift(f,a)',repeat), sys.stdout.flush() print ' (secs for %s calls)' % (repeat)
def check_definition(self): for n in [18,17,64,127,32,2048,256]: x = arange(n)*2*pi/n for a in [0.1,3]: assert_array_almost_equal(shift(sin(x),a),direct_shift(sin(x),a)) assert_array_almost_equal(shift(sin(x),a),sin(x+a)) assert_array_almost_equal(shift(cos(x),a),cos(x+a)) assert_array_almost_equal(shift(cos(2*x)+sin(x),a), cos(2*(x+a))+sin(x+a)) assert_array_almost_equal(shift(exp(sin(x)),a),exp(sin(x+a))) assert_array_almost_equal(shift(sin(x),2*pi),sin(x)) assert_array_almost_equal(shift(sin(x),pi),-sin(x)) assert_array_almost_equal(shift(sin(x),pi/2),cos(x))