Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
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
in the following ways, where and . In each case, you should attempt to label the -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.
(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.
(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.
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.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.
(Problem 2.b) Making your histogram function interactive.
Make a copy of
hist_a
calledhist_b
and put@interact
on the line beforedef 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'
byfacecolor=Color('red')
to get a color selector. You will have to figure out how to convert something likeColor('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 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.
(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 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.
(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.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.d) Messing around
Try fiddling with the value of (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?