Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 16658

Math 152: Intro to Mathematical Software

2017-02-27

Kiran Kedlaya; University of California, San Diego

adapted from lectures by William Stein, University of Washington

Lecture 20: Numpy, Matplotlib, and Scipy (part 1): Overviews

Announcements:

  • Modified office hours this week: mine are today 3-4; Zonglin's are Tuesday 10-12.

  • As usual, homework due Tuesday 8pm. Peer grading due Thursday 8pm.

  • Advance warning: Peter and Zonglin and I will simultaneously be away at a conference during the first half of week 10 (the week starting March 13). We will thus have a modified schedule for that week, with the problem set and peer grading due later than usual. Details to follow. (Remember, there is no final exam for this course.)

10 minute intros to numpy, matplotlib and scipy.

  • Numpy, Matplotlib, and Scipy are (along with Cython) the foundation of the Scientific Python Stack.

  • Pretty much everything in the world of Python "data science" depends on them.

  • The community is unified around them as the foundation on which to build.

  • All are very polished and heavily used at this point.

  • Written in mostly C, C++ and some Fortran (scipy). Very highly optimized for speed. Lots of "vectorized operations".

We'll take a quick look at some tutorials for each of these packages. Later in the week, we'll demonstrate these in more details within SMC.

### [Numpy:](http://www.numpy.org/) provides an nn-dimensional array object and lots of functionality with it. > "NumPy is the fundamental package for scientific computing with Python."

You should spend 2 hours and go through the Numpy Tutorial.

### [Matplotlib:](http://matplotlib.org/) provides graphics much like matlab (and much more). > "matplotlib is a python 2D plotting library which produces publication quality figures in a variety of hardcopy formats and interactive environments across platforms."

You should spend an hour and go through this Matplotlib tutorial.

### [Scipy:](https://www.scipy.org/scipylib/index.html) provides many numerical algorithms beyonds just matrices... > "Scipy is the fundamental library for scientific computing. It provides many user-friendly and efficient numerical routines such as routines for numerical integration and optimization."

You should spend several hours on the Scipy tutorial.

%auto %python
import numpy as np a = np.arange(15).reshape(3, 5) a
array([[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14]])
a.shape
(3, 5)
a.dtype.name
'int64'
b = np.array([(1.5,2,3), (4,5)])
b[0]
(1.50000000000000, 2, 3)
import numpy as np X = np.linspace(-np.pi, np.pi, 10,endpoint=True) C,S = np.cos(X), np.sin(X)
import matplotlib.pyplot as plt plt.plot(X,C) plt.plot(X,S) plt.show()
[<matplotlib.lines.Line2D object at 0x7f79ec299c90>] [<matplotlib.lines.Line2D object at 0x7f79ec38a050>]
import matplotlib as mpl
from scipy import special def drumhead_height(n, k, distance, angle, t): kth_zero = special.jn_zeros(n, k)[-1] return np.cos(t) * np.cos(n*angle) * special.jn(n, distance*kth_zero) theta = np.r_[0:2*np.pi:50j] radius = np.r_[0:1:50j] x = np.array([r * np.cos(theta) for r in radius]) y = np.array([r * np.sin(theta) for r in radius]) z = np.array([drumhead_height(1, 1, r, theta, 0.5) for r in radius])
Error in lines 5-5 Traceback (most recent call last): File "/projects/sage/sage-7.5/local/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 982, in execute exec compile(block+'\n', '', 'single') in namespace, locals File "", line 1, in <module> File "/projects/sage/sage-7.5/local/lib/python2.7/site-packages/numpy/lib/index_tricks.py", line 278, in __getitem__ newobj = _nx.arange(start, stop, step) File "sage/rings/complex_number.pyx", line 1105, in sage.rings.complex_number.ComplexNumber.__float__ (/projects/sage/sage-7.5/src/build/cythonized/sage/rings/complex_number.c:11035) raise TypeError("unable to convert {!r} to float; use abs() or real_part() as desired".format(self)) TypeError: unable to convert -0.125663706143592*I to float; use abs() or real_part() as desired
import scipy.integrate as integrate import scipy.special as special result = integrate.quad(lambda x: special.jv(2.5,x), 0, 4.5) result
(1.1178179380783253, 7.866317182537226e-09)