$$ \newcommand{\tp}{\thinspace .} $$

Array computing and curve plotting

Hans Petter Langtangen [1, 2]

[1] Center for Biomedical Computing, Simula Research Laboratory
[2] Department of Informatics, University of Oslo

Jun 15, 2016


Table of contents

Vectors
      The vector concept
      Mathematical operations on vectors
      Vector arithmetics and vector functions
Arrays in Python programs
      Using lists for collecting function data
      Basics of numerical Python arrays
      Computing coordinates and function values
      Vectorization
Curve plotting
      MATLAB-style plotting with Matplotlib
      Matplotlib; pyplot prefix
      SciTools and Easyviz
      Making animations
      Making videos
      Curve plots in pure text
Plotting difficulties
      Piecewisely defined functions
      Rapidly varying functions
More advanced vectorization of functions
      Vectorization of StringFunction objects
      Vectorization of the Heaviside function
      Vectorization of a hat function
More on numerical Python arrays
      Copying arrays
      In-place arithmetics
      Allocating arrays
      Generalized indexing
      Testing for the array type
      Compact syntax for array generation
      Shape manipulation
High-performance computing with arrays
      Scalar implementation
      Vectorized implementation
      Memory-saving implementation
      Analysis of memory usage
      Analysis of the CPU time
Higher-dimensional arrays
      Matrices and arrays
      Two-dimensional numerical Python arrays
      Array computing
      Matrix objects
Some common linear algebra operations
      Inverse, determinant, and eigenvalues
      Products
      Norms
      Sum and extreme values
      Indexing
      Transpose and upper/lower triangular parts
      Solving linear systems
      Matrix row and column operations
      Computing the rank of a matrix
      Symbolic linear algebra
Plotting of scalar and vector fields
      Installation
      Surface plots
      Parameterized curve
      Contour lines
      The gradient vector field
Matplotlib
      Surface plots
      Contour plots
      Vector field plots
Mayavi
      Surface plots
      Contour plots
      Vector field plots
      A 3D scalar field and its gradient field
      Animations
Summary
      Chapter topics
      Example: Animating a function
Exercises
      Exercise 1: Fill lists with function values
      Exercise 2: Fill arrays; loop version
      Exercise 3: Fill arrays; vectorized version
      Exercise 4: Plot a function
      Exercise 5: Apply a function to a vector
      Exercise 6: Simulate by hand a vectorized expression
      Exercise 7: Demonstrate array slicing
      Exercise 8: Replace list operations by array computing
      Exercise 9: Plot a formula
      Exercise 10: Plot a formula for several parameters
      Exercise 11: Specify the extent of the axes in a plot
      Exercise 12: Plot exact and inexact Fahrenheit-Celsius conversion formulas
      Exercise 13: Plot the trajectory of a ball
      Exercise 14: Plot data in a two-column file
      Exercise 15: Write function data to file
      Exercise 16: Plot data from a file
      Exercise 17: Write table to file
      Exercise 18: Fit a polynomial to data points
      Exercise 19: Fit a polynomial to experimental data
      Exercise 20: Read acceleration data and find velocities
      Exercise 21: Read acceleration data and plot velocities
      Exercise 22: Plot a trip's path and velocity from GPS coordinates
      Exercise 23: Vectorize the Midpoint rule for integration
      Exercise 24: Vectorize a function for computing the area of a polygon
      Exercise 25: Implement Lagrange's interpolation formula
      Exercise 26: Plot Lagrange's interpolating polynomial
      Exercise 27: Investigate the behavior of Lagrange's interpolating polynomials
      Exercise 28: Plot a wave packet
      Exercise 29: Judge a plot
      Exercise 30: Plot the viscosity of water
      Exercise 31: Explore a complicated function graphically
      Exercise 32: Plot Taylor polynomial approximations to \( \sin x \)
      Exercise 33: Animate a wave packet
      Exercise 34: Animate a smoothed Heaviside function
      Exercise 35: Animate two-scale temperature variations
      Exercise 36: Use non-uniformly distributed coordinates for visualization
      Exercise 37: Animate a sequence of approximations to \( \pi \)
      Exercise 38: Animate a planet's orbit
      Exercise 39: Animate the evolution of Taylor polynomials
      Exercise 40: Plot the velocity profile for pipeflow
      Exercise 41: Plot sum-of-sines approximations to a function
      Exercise 42: Animate the evolution of a sum-of-sine approximation to a function
      Exercise 43: Plot functions from the command line
      Exercise 44: Improve command-line input
      Exercise 45: Demonstrate energy concepts from physics
      Exercise 46: Plot a w-like function
      Exercise 47: Plot a piecewise constant function
      Exercise 48: Vectorize a piecewise constant function
      Exercise 49: Visualize approximations in the Midpoint integration rule
      Exercise 50: Visualize approximations in the Trapezoidal integration rule
      Exercise 51: Experience overflow in a function
      Exercise 52: Apply a function to a rank 2 array
      Exercise 53: Explain why array computations fail
      Exercise 54: Verify linear algebra results
References

A list object is handy for storing tabular data, such as a sequence of objects or a table of objects. An array is very similar to a list, but less flexible and computationally much more efficient. When using the computer to perform mathematical calculations, we often end up with a huge amount of numbers and associated arithmetic operations. Storing numbers in lists may in such contexts lead to slow programs, while arrays can make the programs run much faster. This is crucial for many advanced applications of mathematics in industry and science, where computer programs may run for hours and days, or even weeks. Any clever idea that reduces the execution time by some factor is therefore paramount.

However, one can argue that programmers of mathematical software have traditionally paid too much attention to efficiency and "clever" program constructs. The resulting software often becomes very hard to maintain and extend. In this document we advocate a focus on clear, well-designed, and easy-to-understand programs that work correctly. Thereafter, one can start thinking about optimization for speed. Fortunately, arrays contribute to clear code, correctness and speed - all at once.

This document gives an introduction to arrays: how they are created and what they can be used for. Array computing usually ends up with a lot of numbers. It may be very hard to understand what these numbers mean by just looking at them. Since the human is a visual animal, a good way to understand numbers is to visualize them. In this document we concentrate on visualizing curves that reflect functions of one variable; i.e., curves of the form \( y=f(x) \). A synonym for curve is graph, and the image of curves on the screen is often called a plot. We will use arrays to store the information about points along the curve. In a nutshell, array computing demands visualization and visualization demands arrays.

All program examples in this document can be found as files in the folder src/plot.

This chapter is taken from the book A Primer on Scientific Programming with Python by H. P. Langtangen, 5th edition, Springer, 2016.