<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Today
Step1:  Q. Is the following call sequence acceptable?
Step2:  No! The following are all OK!
Step3:  Keyword arguments
Step4:  The first two arguments in this case are "positional arguments."
The second two are named "keyword arguments".
Keyword arguments must follow positional arguments in function calls.
Q. What will this do?
Step5:  $$f(t)=A \cdot e^{-at} \cdot \sin({\omega \cdot t})$$
Step6:  Q. What will this yield?
Step7:  $$e^x = \sum_{i=0}^{\infty} \frac{x^i}{i!}$$
$$e^x \approx \sum_{i=0}^{N} \frac{x^i}{i!}$$
Step8:  Q. What's missing? Have there been ambiguities in the functions we've written?
Step10:  Doc-strings (review)
Step12:  Q. So, what should doc strings contain in general?
Step14:  Functions as arguments to other functions
$$\frac{df(x)}{dx} \approx \frac{f(x-h) + f(x+h) - 2f(x)}{h^2}$$
Step15:  We'll work with the following function
Step16:  Q. What should this yield?
Step17:  Lambda functions
<ASSISTANT_TASK:>
Python Code:
from math import exp
# Could avoid this by using our constants.py module!
h = 6.626e-34  # MKS
k = 1.38e-23
c = 3.00e8
def intensity(wave, temp, mydefault=0):
    wavelength = wave / 1e10
    B = 2 * h * c**2 / (wavelength**5 * (exp(h * c / (wavelength * k * temp)) - 1))
    return B
Explanation: Today: More on functions
Final project posted.
Poll: When do you want Homework deadline? Friday lunch, to enable discussing HW on Friday? Or end of Friday?
End of explanation
mywave = 5000
intensity(mywave, temp=5800.0)
Explanation: Q. Is the following call sequence acceptable?
End of explanation
print(intensity(5000.0, temp=5800.0))
print(intensity(wave=5000.0, temp=5800.0))
print(intensity(5000.0, 5800.0))
intensity(5800.0, 5000.0)
Explanation: No! The following are all OK!
End of explanation
def testFunc(arg1, arg2, kwarg1=True, kwarg2=4.2):
    print(arg1, arg2, kwarg1, kwarg2)
Explanation: Keyword arguments
End of explanation
testFunc(1.0, 2.0)
testFunc(1.0, 2.0, 3.0)  # NOTE! I do not HAVE TO use the keyword access!
Explanation: The first two arguments in this case are "positional arguments."
The second two are named "keyword arguments".
Keyword arguments must follow positional arguments in function calls.
Q. What will this do?
End of explanation
from math import pi, exp, sin
tau = 2*pi
# t is positional argument, others are keyword arguments
def f(t, A=1, a=1, omega=tau):
    return A * exp(-a * t) * sin(omega * t)
v1 = f(0.01)  # Only the time is specified
v1
Explanation: $$f(t)=A \cdot e^{-at} \cdot \sin({\omega \cdot t})$$
End of explanation
v1 = f(A=2, t=0.01)
v1
Explanation: Q. What will this yield?
End of explanation
import math
def exponential(x, epsilon=1e-6):
    total = 1.0
    i = 1
    term = (x**i) / math.factorial(i)
    
    while abs(term) > epsilon:
        term = x**i / math.factorial(i)
        total += term
        i += 1
    
    return total, i
total, i = exponential(2.4, epsilon=1e-2)
print(exponential(2.4, epsilon=1e-4))
print(exponential(2.4))
print(math.exp(2.4))
Explanation: $$e^x = \sum_{i=0}^{\infty} \frac{x^i}{i!}$$
$$e^x \approx \sum_{i=0}^{N} \frac{x^i}{i!}$$
End of explanation
from math import exp
h = 6.626e-34  # MKS
k = 1.38e-23
c = 3.00e8
def intensity(wave, temp):
    wavelength = wave / 1e10
    inten = 2 * h * c**2 / (wavelength**5 * (exp(h * c / (wavelength * k * temp)) - 1))
    return inten
Explanation: Q. What's missing? Have there been ambiguities in the functions we've written?
End of explanation
import math
def exponential(x, epsilon=1e-6):  
    
    This function calculates exp(x) to a tolerance
    of epsilon.
    Parameters
    ----------
    x :  exponent
    epsilon :  tolerance
    returns :  exp(x), number of terms include (after 1.0)
    Example from interactive shell:
    >>> sum, i = exponential(0.15)
    >>> print i-1, sum
    5, 1.16183422656
    
    total = 1.0
    i = 1
    term = (x**i) / math.factorial(i)
    
    while abs(term) > epsilon:
        term = x**i / math.factorial(i)
        total += term
        i += 1
    
    return total, i
total, i = exponential(0.15)
i - 1, total
Explanation: Doc-strings (review)
End of explanation
print(exponential.__doc__)
# How handy!!
expontential.
help(exponential)
from math import exp
h = 6.626e-34  # MKS
k = 1.38e-23
c = 3.00e8
def intensity(wave, temp):
    
    Compute the value of the Planck function.
    Parameters
    ----------
    wave : int, float
        Wavelength at which to compute Planck function, in Angstroms.
    T : int, float
        Temperature of blackbody, in Kelvin.
    Example
    -------
    Radiance at 5000 Angstrom, 5800 K blackbody:
    >>> radiance = I(wave=5000., T=5800.) 
    Returns
    -------
    Radiance of blackbody in W / sr / m^3.
    
    
    wavelength = wave / 1e10
    inten = 2 * h * c**2 / (wavelength**5 * (exp(h * c / (wavelength * k * temp)) - 1))
    return inten
Explanation: Q. So, what should doc strings contain in general?
End of explanation
def diff2(f, x, h=1e-6):
    
    Calculates a second derivative.
    f:  the function (of one variable) to be differentiated
    x:  value at which the function is evaluated
    h:  small difference in x 
    
    r = (f(x-h) + f(x + h) - 2.0 * f(x)) / float(h**2)
    return r
Explanation: Functions as arguments to other functions
$$\frac{df(x)}{dx} \approx \frac{f(x-h) + f(x+h) - 2f(x)}{h^2}$$
End of explanation
def g(t):
    return t**2
t = 3.0
gPrimePrime = diff2(g, t)
Explanation: We'll work with the following function:
$g(t) = t^2$
End of explanation
print("Second derivative of g=t^4 evaluated at t=%g" % t)
"g(%f)=%.8g" % (t, gPrimePrime)
Explanation: Q. What should this yield?
End of explanation
g = lambda t: t**2
print(g)
g(2.0)
def g(t):
    return t**2
# This simply calculates the second derivative of t^2 evaluated at t=1.
test = diff2(lambda t: t**2, 56) 
# Recall the first argument to diff2 was the function
test
def cubed(x):
    return x**3
cubed(2)
y = lambda x: x**3
y(2)
s = 'hans_gruber'
s.split('_')
df.sort().groupby().do_stuff(lambda x: x.split('-')[0])
Explanation: Lambda functions
End of explanation
<END_TASK>