Final project posted.
Poll: When do you want Homework deadline? Friday lunch, to enable discussing HW on Friday? Or end of Friday?
In [61]:
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
In [62]:
mywave = 5000
In [64]:
intensity(mywave, temp=5800.0)
Out[64]:
No! The following are all OK!
In [65]:
print(intensity(5000.0, temp=5800.0))
print(intensity(wave=5000.0, temp=5800.0))
print(intensity(5000.0, 5800.0))
In [66]:
intensity(5800.0, 5000.0)
Out[66]:
In [74]:
def testFunc(arg1, arg2, kwarg1=True, kwarg2=4.2):
print(arg1, arg2, kwarg1, kwarg2)
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.
In [76]:
testFunc(1.0, 2.0)
testFunc(1.0, 2.0, 3.0) # NOTE! I do not HAVE TO use the keyword access!
In [77]:
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
Out[77]:
In [78]:
v1 = f(A=2, t=0.01)
v1
Out[78]:
In [79]:
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
In [80]:
total, i = exponential(2.4, epsilon=1e-2)
print(exponential(2.4, epsilon=1e-4))
print(exponential(2.4))
print(math.exp(2.4))
In [ ]:
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
In [83]:
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
In [81]:
total, i = exponential(0.15)
i - 1, total
Out[81]:
In [84]:
print(exponential.__doc__)
# How handy!!
In [ ]:
expontential.
In [85]:
help(exponential)
In [86]:
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
In [87]:
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
We'll work with the following function:
$g(t) = t^2$
In [88]:
def g(t):
return t**2
In [ ]:
t = 3.0
gPrimePrime = diff2(g, t)
In [ ]:
print("Second derivative of g=t^4 evaluated at t=%g" % t)
"g(%f)=%.8g" % (t, gPrimePrime)
In [89]:
g = lambda t: t**2
print(g)
g(2.0)
Out[89]:
In [ ]:
def g(t):
return t**2
In [90]:
# 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
Out[90]:
In [91]:
def cubed(x):
return x**3
In [92]:
cubed(2)
Out[92]:
In [93]:
y = lambda x: x**3
In [94]:
y(2)
Out[94]:
In [96]:
s = 'hans_gruber'
In [97]:
s.split('_')
Out[97]:
In [ ]:
df.sort().groupby().do_stuff(lambda x: x.split('-')[0])