##^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^Lance Simms, Stanford University 2009
#Examples of functions with the lambda syntax: Copied from 
#http://www.scipty.org/Cookbook/FittingData
#
#NOTE: ** is the squaring operator
#    : ^ does something extremely interesting.  It looks like it's 
#	 swapping indices according to the argument.
import sys,os
import pyfits
from numpy import *
from pylab import *

#Define the gaussian function
gaussian = lambda x : 3* exp(-(30-x)**2/20.)
#Implement it
data=gaussian(arange(100))
matplotlib.pylab.plot(data)
 
X = arange(data.size)			 	#Form an array of x values
x=sum(X*data)/sum(data) 			#First Moment
width = sqrt(abs(sum((X-x)**2*data)/sum(data))) #Second Moment

max = data.max()				#Get the maximum data value

fit = lambda t : max*exp(-(t-x)**2/(2*width**2))

matplotlib.pylab.plot(fit(X),'r')
#Only use show once per script as it forces graphics to the window
#and halts execution
show()
