Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Adds scipy and phonenums supporting files.

  • Loading branch information...
commit 4f547270a65698417ec7aabe6202d65e0f6cea6d 1 parent a91be39
@katyhuff katyhuff authored
View
29 SciPy/constants.py
@@ -0,0 +1,29 @@
+#The Hacker Within: Python Boot Camp 2010 - Session 07 - Using SciPy.
+#Presented by Anthony Scopatz.
+#
+#SciPy constants, Crawl before you walk!
+
+#A plethora of important fundamental constants can be found in
+import scipy.constants
+#NOTE: this module is not automatically included when you "import scipy"
+
+#Some very basic pieces of information are given as module attributes
+print("SciPy thinks that pi = %.16f"%scipy.constants.pi)
+import math
+print("While math thinks that pi = %.16f"%math.pi)
+print("SciPy also thinks that the speed of light is c = %.1F"%scipy.constants.c)
+print("")
+
+#But the real value of SciPy constants is its enormous physical constant database
+print("SciPy physical constants are of the form:")
+print(" scipy.constants.physical_constants[name] = (value, units, uncertainty)")
+print("")
+
+print("For example the mass of an alpha particle is %s"%str(scipy.constants.physical_constants["alpha particle mass"]))
+print("But buyer beware! Let's look at the speed of light again.")
+print("c = %s"%str(scipy.constants.physical_constants["speed of light in vacuum"]))
+print("The uncertainty in c should not be zero!")
+print("")
+
+print("Check http://docs.scipy.org/doc/scipy/reference/constants.html for a complete listing.")
+
View
37 SciPy/image_tricks.py
@@ -0,0 +1,37 @@
+#The Hacker Within: Python Boot Camp 2010 - Session 07 - Using SciPy.
+#Presented by Anthony Scopatz.
+#
+#SciPy Image Tricks, fly before you....You can do that?!
+
+#For some reason that has yet to be explained to me, SciPy has the ability to treat 2D & 3D arrays
+#as images. You can even convert PIL images or read in external files as numpy arrays!
+#From here, you can fool around with the raw image data at will. Naturally, this functionality
+#is buried within the 'miscellaneous' module.
+import scipy.misc
+
+#First let's read in an image file. For now, make it a JPEG.
+img = scipy.misc.imread("image.jpg")
+#Note that this really is an array!
+print(str(img))
+
+#We can now apply some basic filters...
+img = scipy.misc.imfilter(img, 'blur')
+
+#We can even rotate the image, counter-clockwise by degrees.
+img = scipy.misc.imrotate(img, 45)
+
+#And then, we can rewrite the array to an image file.
+scipy.misc.imsave("image1.jpg", img)
+
+#Because the array takes integer values from 0 - 255, we can easily define our own filters as well!
+def InverseImage(imgarr):
+ return 255 - imgarr
+
+#Starting fresh we get...
+img = scipy.misc.imread("image.jpg")
+img = scipy.misc.imrotate(img, 330)
+img = InverseImage(img)
+scipy.misc.imsave("image2.jpg", img)
+
+#Check out http://docs.scipy.org/doc/scipy/reference/misc.html for a complete listing.
+
View
97 SciPy/integrate.py
@@ -0,0 +1,97 @@
+#The Hacker Within: Python Boot Camp 2010 - Session 07 - Using SciPy.
+#Presented by Anthony Scopatz.
+#
+#SciPy Integration, run before you glide.
+
+#Tools used to calculate numerical, definite integrals may be found in the 'integrate' module.
+import scipy.integrate
+#For kicks, let's also grab
+import scipy.special
+import numpy
+
+#There are two basic ways you can integrate in SciPy:
+# 1. Integrate a function, or
+# 2. Integrate piecewise data.
+
+#First Let's deal with integration of functions.
+#Recall that in Python, functions are also objects.
+#Therefore you can pass functions as arguments to other functions!
+#Just make sure that the function that you want to integrate returns a float,
+#or, at the very least, an object that has a __float__() method.
+
+#The simplest way to compute a functions definite integral is via the quad(...) function.
+def CrazyFunc(x):
+ return (scipy.special.i1(x) - 1)**3
+
+print("Try integrating CrazyFunc on the range [-5, 10]...")
+
+val, err = scipy.integrate.quad(CrazyFunc, -5, 10)
+
+print("A Crazy Function integrates to %.8E"%val)
+print("And with insanely low error of %.8E"%err)
+print("")
+
+#You can also use scipy.integrate.Inf for infinity in the limits of integration
+print("Now try integrating e^x on [-inf, 0]")
+print("(val, err) = " + str( scipy.integrate.quad(scipy.exp, -scipy.integrate.Inf, 0.0) ))
+print("")
+
+#2D integrations follows similarly,
+def dA_Sphere(phi, theta):
+ return scipy.sin(phi)
+
+print("Integrate the surface area of the unit sphere...")
+val, err = scipy.integrate.dblquad(dA_Sphere, 0.0, 2.0*scipy.pi, lambda theta: 0.0, lambda theta: scipy.pi )
+print("val = %.8F"%val)
+print("err = %.8E"%err)
+print("")
+
+def dV_Sphere(phi, theta, r):
+ return r * r * dA_Sphere(phi, theta)
+
+print("Integrate the volume of a sphere with r=3.5...")
+val, err = scipy.integrate.tplquad(dV_Sphere, 0.0, 3.5, lambda r: 0.0, lambda r: 2.0*scipy.pi, lambda x, y: 0.0, lambda x, y: scipy.pi)
+print("val = %.8F"%val)
+print("err = %.8E"%err)
+print("")
+
+#Now, only very rarely will scientists (and even more rarely engineers) will truely 'know'
+#the function that they wish to integrate. Much more often we'll have piecewise data
+#that we wish numerically integrate (ie sum an array y(x), biased by array x).
+#This can be done in SciPy through the trapz function.
+
+y = range(0, 11)
+print("Trapazoidally integrate y = x on [0,10]...")
+val = scipy.integrate.trapz(y)
+print("val = %F"%val)
+print("")
+
+#You can also define a domain to integrate over.
+x = numpy.arange(0.0, 20.5, 0.5)
+y = x * x
+print("Trapazoidally integrate y = x^2 on [0,20] with half steps...")
+val = scipy.integrate.trapz(y, x)
+print("val = %F"%val)
+print("")
+
+print("Trapazoidally integrate y = x^2 with dx=0.5...")
+val = scipy.integrate.trapz(y, dx=0.5)
+print("val = %F"%val)
+print("")
+
+def dDecay(y, t, lam):
+ return -lam*y
+
+#Of course, sometimes we have simple ODEs that we want to integrate over time for...
+#These are generally of the form:
+# dy / dt = f(y, t)
+#For example take the decay equation...
+# f(y, t) = - lambda * y
+#We can integrate this using SciPy's 'odeint' This is of the form:
+# odeint( f, y0, [t0, t1, ...])
+#Let's try it...
+vals = scipy.integrate.odeint( lambda y, t: dDecay(y, t, 0.2), 1.0, [0.0, 10.0] )
+print("If you start with a mass of y(0) = %F"%vals[0][0])
+print("you'll only have y(t=10) = %F left."%vals[1][0])
+
+#Check out http://docs.scipy.org/doc/scipy/reference/integrate.html for a complete listing.
View
53 SciPy/pade1.py
@@ -0,0 +1,53 @@
+#The Hacker Within: Python Boot Camp 2010 - Session 07 - Using SciPy.
+#Presented by Anthony Scopatz.
+#
+#SciPy Pade, glide before you fly!
+
+#As you have seen, SciPy has some really neat functionality that comes stock.
+#Oddly, some of the best stuff is in the 'miscelaneous' module.
+import scipy.misc
+
+#Most people are familar with the polynomial expansions of a function:
+# f(x) = a + bx + cx^2 + ...
+#Or a Taylor expansion:
+# f(x) = sum( d^n f(a) / dx^n (x-a)^n /n! )
+#However, there exists the lesser known, more exact Pade approximation.
+#This basically splits up a function into a numerator and a denominator.
+# f(x) = p(x) / q(x)
+#Then, you can approximate p(x) and q(x) using a power series.
+#A more complete treatment is available in Section 5.12 in 'Numerical Recipes' by W. H. Press, et al.
+
+
+#The stregnth of this method is demonstated though figures...
+from pylab import *
+
+#Let's expand e^x to fith order and record the coefficents
+e_exp = [1.0, 1.0, 1.0/2.0, 1.0/6.0, 1.0/24.0, 1.0/120.0]
+
+#The Pade coefficients are given simply by,
+p, q = scipy.misc.pade(e_exp, 2)
+#p and q are of numpy's polynomial class
+#So the Pade approximation is given by
+def PadeAppx(x):
+ return p(x) / q(x)
+
+#Let's test it...
+x = arange(0.0, 3.1, 0.1)
+
+e_exp.reverse()
+e_poly = poly1d(e_exp)
+
+plot(x, PadeAppx(x), 'k--', label="Pade Approximation")
+plot(x, scipy.e**x, 'k-', label=r'$e^x$')
+plot(x, e_poly(x), 'r-', label="Power Series")
+
+#axis([0, 10, -2, 1.25])
+xlabel(r'$x$')
+ylabel("Exponential Functions")
+
+legend(loc=0)
+
+show()
+
+#Check out http://docs.scipy.org/doc/scipy/reference/misc.html for a complete listing.
+
View
44 SciPy/pade2.py
@@ -0,0 +1,44 @@
+#The Hacker Within: Python Boot Camp 2010 - Session 07 - Using SciPy.
+#Presented by Anthony Scopatz.
+#
+#SciPy Pade, glide before you fly!
+
+#As you have seen, SciPy has some really neat functionality that comes stock.
+#Oddly, some of the best stuff is in the 'miscelaneous' module.
+import scipy.misc
+from pylab import *
+
+#So our exponential pade approimation didn't give us great gains,
+#But let's try approximating a rougher function.
+def f(x):
+ return (7.0 + (1+x)**(4.0/3.0))**(1.0/3.0)
+
+#Through someone else's labors we know the expansion to be...
+f_exp = [2.0, 1.0/9.0, 1.0/81.0, -49.0/8748.0, 175.0/78732.0]
+
+#The Pade coefficients are given simply by,
+p, q = scipy.misc.pade(f_exp, (5-1)/2)
+#p and q are of numpy's polynomial class
+#So the Pade approximation is given by
+def PadeAppx(x):
+ return p(x) / q(x)
+
+#Let's test it...
+x = arange(0.0, 10.01, 0.01)
+
+f_exp.reverse()
+f_poly = poly1d(f_exp)
+
+plot(x, PadeAppx(x), 'k--', label="Pade Approximation")
+plot(x, f(x), 'k-', label=r'$f(x)$')
+plot(x, f_poly(x), 'r-', label="Power Series")
+
+xlabel(r'$x$')
+ylabel("Polynomial Function")
+
+legend(loc=0)
+
+show()
+
+#Check out http://docs.scipy.org/doc/scipy/reference/misc.html for a complete listing.
+
View
35 SciPy/special_functions.py
@@ -0,0 +1,35 @@
+#The Hacker Within: Python Boot Camp 2010 - Session 07 - Using SciPy.
+#Presented by Anthony Scopatz.
+#
+#SciPy special functions, walk before you run!
+
+#Code that numerically approximates common (and some not-so-common) special functions can be found in 'scipy.special'
+from scipy.special import *
+
+#Here you can find things like error functions, gamma functions, Legendre polynomials, etc.
+#But as a example let's focus on my favorites: Bessel functions.
+#Time for some graphs...
+from pylab import *
+
+x = arange(0.0, 10.1, 0.1)
+
+for n in range(4):
+ j = jn(n, x)
+ plot(x, j, 'k-')
+ text(x[10*(n+1)+1], j[10*(n+1)], r'$J_%r$'%n)
+
+for n in range(3):
+ y = yn(n, x)
+ plot(x, y, 'k--')
+ text(x[10*(n)+6], y[10*(n)+5], r'$Y_%r$'%n)
+
+axis([0, 10, -2, 1.25])
+xlabel(r'$x$')
+ylabel("Bessel Functions")
+
+show()
+
+#Check out http://docs.scipy.org/doc/scipy/reference/special.html for a complete listing.
+
+#Note that the figure that was created here is a reproduction of
+#Figure 6.5.1 in 'Numerical Recipes' by W. H. Press, et al.
View
19 phonenums/phonenums.txt
@@ -0,0 +1,19 @@
+608-204-4653
+608-213-2233
+203-221-1950
+203-221-9902
+773-223-1104
+773-239-6924
+608-263-8071
+608-293-6924
+800-314-4258
+203-333-3232
+608-381-6668
+608-420-0040
+773-437-7950
+800-444-0800
+608-444-2222
+203-444-3333
+800-444-4444
+608-444-5555
+800-483-2975
Please sign in to comment.
Something went wrong with that request. Please try again.