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 152: Intro to Mathematical Software
2017-03-06
Kiran Kedlaya; University of California, San Diego
adapted from lectures by William Stein, University of Washington
** Lecture 23: SciPy: Additional tools for scientific computation **
Announcements:
This week's schedule is as usual: homework is due Tuesday at 8pm; peer review due Thursday at 8pm; sections and office hours meet as scheduled.
However, next week's schedule will be modified because Peter and Zonglin and I will all be at a conference for part of the week.
The last homework will be due Friday, March 17 at 8pm. Peer review will be due Sunday, March 19 at 8pm.
Sections will not be held on Monday, March 13.
Office hours will be rescheduled for Thursday and Friday. The exact times will be announced later this week.
There will be guest lecturers on Monday and Wednesday. Details to be announced later.
The window for course evaluations (CAPE) is open! Your detailed feedback will be greatly appreciated, and will help the math department with future course design.
Plan for the rest of the course:
Today: finish the unit on scientific computation by demonstrating some additional functionality provide by SciPy. We will loosely follow the SciPy Tutorial.
Rest of this week: abstract algebra and number theory.
Next week: cryptography (including a preview of Math 187B, which will also use SageMathCloud).
Special functions
In addition to the basic functions you encounter in calculus (trig functions, exponential/logarithm), mathematicians have isolated a number of other special functions that occur from time to time, often when solving differential equations. See this list.
Integration
In case you need something more sophisticated than Sage's built-in numerical integration, some options can be found here. Here I'll demonstrate one example: numerically solving an ordinary differential equation (ODE).
Consider the second-order equation [\frac{d^{2}w}{dz^{2}}-zw(z)=0] with initial conditions (w\left(0\right)=\frac{1}{\sqrt[3]{3^{2}}\Gamma\left(\frac{2}{3}\right)}) and (\left.\frac{dw}{dz}\right|_{z=0}=-\frac{1}{\sqrt[3]{3}\Gamma\left(\frac{1}{3}\right)}.) It is known that the solution to this differential equation with these boundary conditions is the Airy function [w=\textrm{Ai}\left(z\right);] let us compare the numerical result against the SciPy function special.airy
.
To use the SciPy solve, we must first convert this ODE into a first-order system by setting (\mathbf{y}=\left[\frac{dw}{dz},w\right]) and (t=z). Thus, the differential equation becomes []
In other words, [\mathbf{f}\left(\mathbf{y},t\right)=\mathbf{A}\left(t\right)\mathbf{y}.]
The required inputs to odeint
are the function defining the derivative, fprime
; the initial conditions vector, y0
; and the evaluation points at which to obtain a solution, t
(with the initial value point as the first element of this sequence). The output is a matrix where each row contains the solution vector at each requested evaluation point (the initial conditions are given in the first output row).
Fourier transforms
A one-dimensional discrete Fourier transform:
The FFT y[k] of length (N) of the length-(N) sequence is defined as [y[k] = \sum_{n=0}^{N-1} e^{-2 \pi j \frac{k n}{N} } x[n] , ,] and the inverse transform is defined as [x[n] = \frac{1}{N} \sum_{n=0}^{N-1} e^{2 \pi j \frac{k n}{N} } y[k] , .]
These transforms can be calculated by means of fft and ifft, respectively.
Consistency check: from the definitions, we must have [y[0] = \sum_{n=0}^{N-1} x[n] , .]
One typical reason to perform a Fourier transform is to pick out periodic contributions to a signal (i.e., frequencies). Let's try this with a sum of two sine functions.
Signal processing: filters
Here the basic ideal is that you have a "signal" consisting of a large array of numbers, but you only want to retain a smaller array that captures most of the essential information. A typical example is image processing. (The Snap IPO suggests there is big money in this!)