In [ ]:
!jupyter nbconvert --to slides --post serve Presentation.ipynb

Teaching Applied Mathematics with the Jupyter Notebook

MTLE-4720

Instructor


Dan Lewis
Associate Professor School of Engineering
Department of Materials Science and Engineering
Rensselaer Polytechnic Institute

lucentdan@gmail.com
lewisd2@rpi.edu

Course Outline


  • What you will find out:
    • What my class is and why I teach it.
    • Why I think the notebook ecosystem is important.
    • Demonstrate some key features of my teaching style.
    • Show a use case.
  • Presentation Outline:
    • MTLE-4720 - Course Intent
    • The Ecosystem
    • The Notebook
    • Python
    • Markdown
    • Key Libraries
    • Jupyter
    • Live Reveal
    • Slideshow
    • Classroom Management
    • The "All in One" Approach
    • Short Example Lecture

Part 1: Presentation of Methods


  • Pillars of Modern Engineering Education
  • Importance of the Notebook Approach
  • The Key Elements of The Teaching Method
  • The Course
  • The Key Components Explained
  • An Example Lecture

The Pillars of Modern Engineering Education


  • The Physical Sciences
    (Chemistry, Physics, Earth and Environmental Science, etc.)
  • The Formal Sciences
    (Mathematics, Logic and Reasoning, etc.)
  • The Social Sciences
    (Comprehension, Critical Thinking, Communication, etc.)

The Engineer uses science to build tools to improve others' quality of life.

  • What supports our ability to transfer our knowledge and practice of engineering disciplines?
  • What basis do we have for communicating and encoding our discipline?
  • What can we count on new students to have internalized before we see them for the first class?

In general I think we need more emphasis on computational literacy. Our students don't know how to use computers like engineers would in practice.

No significant development in course content has been made in the area of computational literacy since I was an undergraduate. This may reflect the scarce nature of computers in the early 1990's, but since I've been in univeristy our undergraduate's course looks just like the one I took many years ago.

(Disclaimer: this is a personal observation and in the area of computational materials research I'm left teaching students a lot of the information and methods myself. Their computational training is not commensurate with their mathematical, chemical, physical, reading, writing, etc. training.)

MTLE-4720

Undergraduate course that applies mathematical techniques to understand materials engineering topics such as materials structure, symmetry, diffusion, mechanics and physics of solids. The class uses examples from the materials science and engineering core courses to introduce mathematical concepts and materials-related problem solving skills. Topics include linear algebra, eigenvalues and eigenvectors, quadratic forms, tensor operations, symmetry operations, calculus of several variables, introduction to complex analysis, ordinary and partial differential equations, theory of distributions, and Fourier analysis, transformations, and calculus of variations .

Fractures in thought

  • Do we teach software packages or foundations of applied mathematics ?

MTLE-4720 Course Intent

More completely synthesize and augment students' abilities in the
area of mathematics and computational thinking. We attempt to
achieve this in the following manner:

  • Choose a suitable model problem (e.g. diffusion)
  • Teach foundations for solving the problem in different contexts.
  • Solve the problem as many ways as possible.

Emphasis

The course attempts to provide equal emphasis on:

  • mathematics,
  • representation and abstraction,
  • visualization,
  • and communication.

Course Goals


The course seeks to help students develop an ability:

  • conceptualize, formulate and solve mathematical problems in materials science and engineering;
  • use the techniques, skills and modern engineering tools necessary for engineering practice including symbolic computation tools and numerical solvers.

Course Structure


The Ecosystem

  • Python programming language
  • Jupyter Notebook
  • Markdown (incl. LaTeX for equations)
  • Key libraries (Numpy, Matplotlib, SymPy)

Python

  • A high level, interpreted language
  • A multi-paradigm programming language
  • Supports object oriented, functional and structured programming
  • Open source
  • Dynamically typed
  • Runs on many common operating systems

Why Python?

My main reasons for using Python with in the classroom setting are pragmatic.

Computer literacy is not a barrier.

Knowing that engineering students are generally used to "apps" and "office" suites, setting up a new programming/coding workflow on a student's computer is a barrier. Python distributions (Anaconda and Enthought, specifically) provide a one-click install that provides the student with the necessary tools to start working in my classroom. These environments are available for Linux, Windows and OSX. With some modest tutorials I can get the students into a notebook environment and reading my course notes with little effort. Approximately 2 out of 15 (or so) students will need direct intervention to help them get started on their personal computers. I can usually sort this out on the first day of class. This approach works well at schools where students all have (or are required to have) a laptop computer.

Alternatively, a shared filesystem and computer lab can be used. I worked with a very good team of computer technicians and was able to create the environment on a set of Windows PCs in two days.

Python is dynamically typed.

In some languages (e.g. C, Fortran) you must declare the type of the variable to allocate storage. In Python this happens dynamically. In the early stages of learning computation this is beneficial. I can postpone the discussion on representation of real numbers and memory allocation to later lectures where we discuss how to speed up code.

Python is interpreted rather than compiled.

This, plus the notebook format help the student make rapid progress in development of code and understanding of course material. Incremental, statement-by-statment development and assembly of complex functions from functioning parts can be used as a development paradigm right in the classroom.

The Jupyter Notebook is Immersive.

The documentation and interactive nature of Python/IPython/Notebook help the learning and development occur directly in the browser. You never have to take your eyes off the main window. (I will provide an example of this later.)

The Jupyter Notebook


  • Web-application running in browser
  • Provides document creation and distribution with other tools
  • Synthesis of code, visualizations, equations, and text
  • Enables a seamless interface to IPython, Python, documentation and libraries
  • Projection of live-coding in class and real-time problem solving
  • Add other stuff

The Jupyter Notebook

The notebook provides a single interface for me and the student to interact with the course notes, presentation materials, and homework development. This is consciously part of what I call my "all-in-one" materials distribution strategy. The Notebook is a recent and still evolving project and that can be a bit of a challenge when major updates are released requiring minor tweaks to my code. In defense of the project, my code was the problem each time!

The Notebook works in the browser and this provides a familarity for the student as it is my impression that students spend a lot of their "idle" computer time in-app or in-browser. (E-mail, document creation, commerce, etc.)

Some good "notebook habits" have to be developed by the students, especially if they are used to programming in an IDE or with a compiled languge. The notebook launches a kernel for the selected language (there is more than just Python) and acts like a "state machine" where all the variables and symbols are accessed. This can be a problem at times, but can be remedied easily by re-launching and re-executing the cells when things go wrong.

A major strength of the Notebook is related to the synthesis of text input, equations, visualization and coding. I think students (and we) are used to compartmentalizing each of these activities. One familiar workflow might be to write a document in a word processor, manipulate data in a spreadsheet, edit equations in a plug-in, write code in a code editor or IDE and visualize data in a stand-alone visualization package.

I feel that this compartmentalization de-emphasizes the need of an engineer or researcher to explain their approach and methods, data collected, calculations, and intellectual contributions. To use the Notebook to develop a solution to a problem puts all of the options in front of the user - and the conspicuous absence of these elements could (should?) prompt the author to act to improve their communication.

Lastly, I use features that permit me to project my live-coding to the class. I feel that students' observation of my problem solving abilities and methods is instructive. Although it can be challenging for a student to see the meta-activity if they are focused on the results.

Markdown


  • Software for displaying content on the web changes rapidly.
  • Traditional paper publishing creates books that are very expensive for students.
  • Web pages written in HTML can be difficult to maintain.
  • Markdown is a humane markup language.

What is Markdown?

A markup language uses a command syntax inline with the content of a document. You are probably most familiar with HTML, but there are others.

These languages can be challenging to use if you are starting out or if you are not used to programming. HTML, in partcular, looks more like computer code then what you (ultimately) want to see on a webpage. This is off-putting for content developers.

Attitudes now favor writing the content of documents so that they are human readable, and then permitting software (that other people write) to transform your document into something that looks different (usually nicer or more professional) or displays the content in a different type of software where the software controls the formatting. LaTeX is a very mature example of a markup language that scientists and engineers use to prepare formal reports and journal articles. For the most part, a LaTeX document can easily be read by a non-LaTeX programmer.

Recently the idea of more humane markup languages has emerged. These languages can easily be read in raw form but they can also be translated by other computer programs into other types of documents.

If you've ever sent a text message and used quotes: " ", asterisks: , or underlines: around a word, then you've already written in a type of "markdown". Software intended for the web generally will have some form of markdown available.

We feel that focusing on human readable content is an appropriate activity for faculty. Sticking to markdown syntax makes it possible for other software packages to interpret your content and create interactive notebooks, slides, homework assignments, course notes, etc.

For example:

**Hello World!**

_Hello World!_

produces:

Hello World!

Hello World!

* Item 1
* Item 2
* Item 3

  • Item 1
  • Item 2
  • Item 3

And equations:

$$ \frac{\partial c}{\partial t} = D \frac{\partial^2 c}{\partial x^2} $$

renders as:

$$ \frac{\partial c}{\partial t} = D \frac{\partial^2 c}{\partial x^2} $$

Key Libraries


  • NumPy - provides an array object, matrix operations, efficient access
  • Matplotlib - provides 2D Python plotting library
  • SymPy - provides symbolic mathematics aiming towards a full-featured CAS

In [6]:
import numpy as np

# Array
myArray = np.array([1,2,3,4,5])
myArray[1::]


Out[6]:
array([2, 3, 4, 5])

In [ ]:
# Broadcasting
5*myArray

In [9]:
# Slicing
myArray


Out[9]:
array([1, 2, 3, 4, 5])

Broadcasting and slicing reduce/eliminate the need for slow loops in Python. All the looping is done in C and with appropriate optimization using additional Python tools can be made nearly equivalent to native C/Fortran.


In [12]:
# Plotting
import matplotlib.pyplot as plt

x = np.linspace(0, 5, 10)

y = x ** 2

fig = plt.figure()

axes1 = fig.add_axes([0.1, 0.1, 0.8, 0.8]) # main axes
axes2 = fig.add_axes([0.2, 0.5, 0.4, 0.3]) # inset axes

# main figure
axes1.plot(x, y, 'r')
axes1.set_xlabel('x')
axes1.set_ylabel('y')
axes1.set_title('title')

# insert
axes2.plot(y, x, 'g')
axes2.set_xlabel('y')
axes2.set_ylabel('x')
axes2.set_title('insert title');

plt.show()



In [13]:
import sympy as sp
sp.init_printing(use_latex=True)
x = sp.symbols('x')

# Symbolic Series Representation
sp.series(sp.sin(x),x,x0=0,n=10)


Out[13]:
$$x - \frac{x^{3}}{6} + \frac{x^{5}}{120} - \frac{x^{7}}{5040} + \frac{x^{9}}{362880} + \mathcal{O}\left(x^{10}\right)$$

In [16]:
# Calculus
sp.diff(sp.sin(2*x)*sp.cos(x),x,3)


Out[16]:
$$13 \sin{\left (x \right )} \sin{\left (2 x \right )} - 14 \cos{\left (x \right )} \cos{\left (2 x \right )}$$

NBConvert


  • Enables conversion to other formats
  • LaTeX, HTML, PDF, Markdown, and others.
  • I use this to create slideshows for class
  • Create homework solutions in other classes

In [ ]:
# For example, if I were not already in a slide show:

!jupyter nbconvert --to slides --post serve Presentation.ipynb

Live Reveal


  • Jupyter provides extensions that increase the functionality
  • LiveReveal enables a presentation with live execution of code
  • I solve problems and demonstrate code in class
  • Can nudge student solutions along with DIY problems (discussed later)

In [20]:
# Example of Live Reveal

# Compute the squares from 0 to 9
[x**2 for x in range(10)]


Out[20]:
$$\left [ 0, \quad 1, \quad 4, \quad 9, \quad 16, \quad 25, \quad 36, \quad 49, \quad 64, \quad 81\right ]$$

Classroom Management


  • Students read before class
  • Brief lecture content
  • Live coding during lecture
  • DIY problems done in-class
  • Challenging homework problems (major assessment)
  • Require writing and visualizations
  • Final project as exposition of a mathematical concept (major assessment)
  • Run a weekly Fight Club

The All in One Approach


  • There is one set of notebooks.
  • The notebooks are used in presentaton mode
  • The notebooks are used for the student notes to complete the materials for the class
  • Live links are provided to supplement lecture materials
  • Optional and freely available texts are recommended
  • Attempts to minimize time required to curate materials