Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Project: math480-2016
Views: 989

Math 480 - Homework DUE 2016-05-20: numpy, scipy, and matplotlib

Due 6pm on May 20, 2016

There are THREE problems. All problems have equal weight.

Problem 1 -- A comparison of 3d plots.

As you know, Sage has its own 3d plotting functionality built in. A significant drawback of Sage's 3d plotting (right now -- you could change this someday!) is that there is no support for generating publication quality pdf 3d plots using Sage's built-in 3d plotting. This problem is about several distinct ways to draw 3d plots using Sage.

Your goal is to create several files -- hat1.png, hat2.png, and hat3.pdf -- containing plots of the bright red Mexican hat function ψ(x,y)=1πσ4(1x2+y22σ2)e(x2+y2)/2σ2\displaystyle \psi(x,y) = \frac{1}{\pi\sigma^4}\left(1-\frac{x^2+y^2}{2\sigma^2}\right) \mathrm{e}^{-(x^2+y^2)/2\sigma^2}
in the following ways, where σ=0.6\sigma = 0.6 and 2x,y2-2\leq x, y \leq 2. In each case, you should attempt to label the zz-axis with the function definition (the formula right above) using nice-looking LaTeX.

There are 3 parts to this problem.

(Problem 1a.) Plotting using Sage:

  • Use Sage's plot3d and text3d commands. Your text should NOT look good since Sage 3d plotting doesn't support LaTeX properly yet. The output of this problem should be the standard interactive 3d graph that you can rotate around. Don't forget to set the color and please set mesh=True in your plot3d command.

  • Reminder: the surface should be red and the axes ranges are -2 to 2.

  • Create a file called hat1.png that contains a picture of your 3d plot. The only current way to do this is to use your computer's screen capture functionality, then upload the resulting graphic using +New. So do that.

# Solution 1a. ︠12319094-ec6a-43a2-8784-22cf0b1124b9i︠ %md #### (Problem 1b.) Using Sage 3d ray tracer. - Render your Sage 3d graphic from above, but instead using Sage's [Tachyon 3d raytracer](http://jedi.ks.uiuc.edu/~johns/raytracer/), so you get a static png image in a file. Here is an example of how to render using Tachyon in a worksheet: `icosahedron().save('example_icosahedron.png'); smc.file('example_icosahedron.png')` - The image file that you create should be called `hat2.png` and should end up in the current directory (same directory as your worksheet). - Set the color of your plot to be red and make the range of the x and y axis be from -2 to 2. - Label the plot with a beautiful latex formula... which will **NOT** actually work or look beautiful, due to shortcomings in Sage.

(Problem 1b.) Using Sage 3d ray tracer.

  • Render your Sage 3d graphic from above, but instead using Sage's Tachyon 3d raytracer, so you get a static png image in a file. Here is an example of how to render using Tachyon in a worksheet: icosahedron().save('example_icosahedron.png'); smc.file('example_icosahedron.png')

  • The image file that you create should be called hat2.png and should end up in the current directory (same directory as your worksheet).

  • Set the color of your plot to be red and make the range of the x and y axis be from -2 to 2.

  • Label the plot with a beautiful latex formula... which will NOT actually work or look beautiful, due to shortcomings in Sage.

# Solution 1b. ︠2e18d1a0-19e5-4636-a9c8-d253110a0993i︠ %md #### (Problem 1c.) Using [plot_surface](http://matplotlib.org/mpl_toolkits/mplot3d/tutorial.html#surface-plots) from matplotlib to plot the Mexican hat. This is more difficult than the other approaches above... but **it looks great**! - Look at the source code for the first [example](http://matplotlib.org/mpl_examples/mplot3d/surface3d_demo.py) and change it appropriately. - Remember to set the color of your plot to be red and make the range of the x and y axis be from -2 to 2. - Remember to label the title with a beautiful latex formula... - Remember to have matplotlib save the resulting image to the file `hat3.pdf`, in addition to displaying it.

(Problem 1c.) Using plot_surface from matplotlib to plot the Mexican hat. This is more

difficult than the other approaches above... but it looks great!

  • Look at the source code for the first example and change it appropriately.

  • Remember to set the color of your plot to be red and make the range of the x and y axis be from -2 to 2.

  • Remember to label the title with a beautiful latex formula...

  • Remember to have matplotlib save the resulting image to the file hat3.pdf, in addition to displaying it.

# Solution 1c. %python ae7c4944-c502-4ad7-aa83-737f19b71406i︠ %md ### Problem 2 -- histograms and interact The following function draws a histogram using Matplotlib. Try running it to verify that it works for you.

Problem 2 -- histograms and interact

The following function draws a histogram using Matplotlib. Try running it to verify that it works for you.

def hist(): import numpy as np import matplotlib.pyplot as plt mu, sigma = 100, 15 x = mu + sigma * np.random.randn(1000) # the histogram of the data n, bins, patches = plt.hist(x, 50, normed=1, facecolor='g', alpha=0.75) plt.xlabel('Smarts') plt.ylabel('Probability') plt.title('Histogram of IQ') plt.text(60, .025, r'$\mu=100,\ \sigma=15$') plt.axis([40, 160, 0, 0.03]) plt.grid(True) plt.show() hist()

(Problem 2.a) Making your histogram function have inputs.

  • Copy hist and make a new version that takes as input several relevant inputs: def hist_a(samples=1000, mu=100, sigma=15, bins=50, facecolor='g', alpha=0.75) so that you can instead call hist_a with different inputs.

  • Call hist_a with the defaults, and also call it with every single input changed to something else in such as way that you can easily verify with your eyes that you correctly modified the body of the function to use the inputs to the function.

  • The plot should correctly explain what mu and sigma are. Also, ensure that no matter what the inputs, the plot and text isn't partly cut off or missing from the figure.

# Solution 2.a def hist_a(...): import numpy as np import matplotlib.pyplot as plt # ... Your code goes here plt.show() ︠1ab14ff8-a5fa-4174-b464-edf0be765233i︠ %md #### (Problem 2.b) Making your histogram function **interactive**. - Make a copy of `hist_a` called `hist_b` and put `@interact` on the line before `def hist_b(...)` and evaluate. - You will see input boxes for each of samples, mu, sigma, etc. - Try editing some of them and hitting enter, and you'll see the output get updated as a result. - Evaluate interact? in another worksheet (or cell) and look through some examples and docs involving `@interact` (you can also look at https://wiki.sagemath.org/interact for more examples.) - Make it so each of the inputs to hist_b uses either a slider, dropdown, color selector, or buttons to select from a reasonable range of values (up to you). - E.g., replace `facecolor='g'` by `facecolor=Color('red')` to get a color selector. You will have to figure out how to convert something like `Color('red')`, which is a Sage color, into something that is valid input as the facecolor to matplotlib! As usual, you will have to search docs and Google.

(Problem 2.b) Making your histogram function interactive.

  • Make a copy of hist_a called hist_b and put @interact on the line before def hist_b(...) and evaluate.

  • You will see input boxes for each of samples, mu, sigma, etc.

  • Try editing some of them and hitting enter, and you'll see the output get updated as a result.

  • Evaluate interact? in another worksheet (or cell) and look through some examples and docs involving @interact (you can also look at https://wiki.sagemath.org/interact for more examples.)

  • Make it so each of the inputs to hist_b uses either a slider, dropdown, color selector, or buttons to select from a reasonable range of values (up to you).

  • E.g., replace facecolor='g' by facecolor=Color('red') to get a color selector. You will have to figure out how to convert something like Color('red'), which is a Sage color, into something that is valid input as the facecolor to matplotlib! As usual, you will have to search docs and Google.

# Solution 2.b @interact def hist_b(...): import numpy as np import matplotlib.pyplot as plt # ... Your code goes here plt.show() ︠70580a16-fbdc-4c59-b6c4-3309fe39648bi︠ %md #### (Problem 2.c) Adding a button to choose a distribution: - Make a copy of `hist_b` above (with the interact) and call it `hist_c`. Add another button called dist, with values 'randn' and a at least 2 other distributions **of your choice**. Type `np.random.[tab key]` and read the documentation to find some other distributions (up to you which to add). - Modify the code inside of `hist_c` so that it properly plots a histogram for sampling from the given distribution. - In particular, you will have to change the line `x = mu + sigma * np.random.randn(samples)` to a more complicated if statement. (NOTE: You may just parameterize your distribution by mu and sigma still, even if that doesn't really make sense.)

Problem 3: Image compression with singular value decomposition

Read about singular value decomposition:

There are 4 parts to this problem. Use the code below to assist you.

# Working code to get you going... - you'll have to change the image name, and some parameters. from scipy.ndimage import imread import matplotlib.pyplot as plt imgmat = imread("example_lion.jpg", flatten=True) U, sigma, V = np.linalg.svd(imgmat) k = 5 # Your parameter to set reconstruct_img = np.matrix(U[:, :k]) * np.diag(sigma[:k]) * np.matrix(V[:k, :]) lion = plt.imshow(reconstruct_img) plt.show()

(Problem 3.a) Computing the SVD

  • Upload your favorite image (preferably between 100 and 1000 pixels wide and tall)

  • Use scipy.ndimage.imread to turn your image into a numpy matrix of grayscale entries. (When you call np.shape on this matrix, it should have the same dimensions as your image).

  • Use numpy to compute the singular value decomposition (U, Sigma, and V) of your image.

  • Use the first k=10k = 10 vectors of the SVD to re-create the original matrix.

  • Use matplotlib's imshow command (making sure to set cmap to "gray", otherwise you'll get a very colorful picture!) to display the new matrix, which is a compressed version of the original image.

︠ba17f37e-5813-4db5-a1d7-a72016aaff41i︠ %md #### (Problem 3.b) Examining the results - Qualitatively, how well do you think the SVD compression with 10 vectors does at preserving the original image? - Can you still tell what it's a picture of?

(Problem 3.b) Examining the results

  • Qualitatively, how well do you think the SVD compression with 10 vectors does at preserving the original image?

  • Can you still tell what it's a picture of?

%md 51f717ba-c985-4ced-83b5-f1b65600469di︠ %md #### (Problem 3.c) Further analysis - What's the total combined size (number of entries) of the parts of U, Sigma and V that were used to reconstruct the image in part 1? - What's the total size (number of entries) of the original matrix? What's the compression ratio?

(Problem 3.c) Further analysis

  • What's the total combined size (number of entries) of the parts of U, Sigma and V that were used to reconstruct the image in part 1?

  • What's the total size (number of entries) of the original matrix? What's the compression ratio?

# Solution 3.c # compressed = ... # uncompressed = ... ︠229c1b5b-a227-41ff-8965-ee698871fe80i︠ %md #### (Problem 3.d) Messing around - Try fiddling with the value of $k$ (using more or less of the SVD vectors to reconstruct the image). - How many of the SVD vectors do you need to use before the compressed version is indistinguishable from the original?

(Problem 3.d) Messing around

  • Try fiddling with the value of kk (using more or less of the SVD vectors to reconstruct the image).

  • How many of the SVD vectors do you need to use before the compressed version is indistinguishable from the original?