Learn Physics with Functional Programming

Book description

This book teaches you to solve physics problems using the functional programming paradigm. Ideal for first-time programmers and science aficionados alike, it introduces the Haskell programming language and encourages the writing of beautiful code to match the elegant ideas of theoretical physics.

Early chapters cover the basics of coding in Haskell, which has a powerful system of types capable of encoding important mathematical structures in physics, like vectors, derivatives, integrals, scalar fields, vector fields, and differential equations. Later sections of the book explore Newtonian mechanics and electromagnetics—two central pillars of theoretical physics. In addition, you’ll get a deep look into source code, and discover why Haskell’s high-order functions and referential transparency serve physics so well. Along the way, you’ll learn:

•How to write beautiful code that expresses fundamental physical principles
•How to make graphs and animations of interesting situations
•How to program in a language that looks like mathematics
•How types, high order functions, and referential transparency serve physics well

Table of contents

  1. Cover Page
  2. Title Page
  3. Copyright Page
  4. Dedication
  5. About the Author
  6. About the Technical Reviewer
  7. Brief Contents
  8. Contents in Detail
  9. ACKNOWLEDGMENTS
  10. INTRODUCTION
    1. Who This Book Is For
    2. Why Functional Programming, and Why Haskell?
    3. About This Book
  11. PART I A HASKELL PRIMER FOR PHYSICISTS
  12. 1 CALCULATING WITH HASKELL
    1. A Kinematics Problem
    2. The Interactive Compiler
    3. Numeric Functions
    4. Operators
      1. Precedence and Associativity
      2. The Application Operator
    5. Functions with Two Arguments
    6. Numbers in Haskell
      1. Negative Numbers in Haskell
      2. Decimal Numbers in Haskell
      3. Exponential Notation
    7. Approximate Calculation
    8. Errors
    9. Getting Help and Quitting
    10. More Information
    11. Summary
    12. Exercises
  13. 2 WRITING BASIC FUNCTIONS
    1. Constants, Functions, and Types
    2. How We Talk About Functions
    3. Anonymous Functions
    4. Composing Functions
    5. Variable Not in Scope Error
    6. Summary
    7. Exercises
  14. 3 TYPES AND ENTITIES
    1. Basic Types
      1. The Boolean Type
      2. The Character Type
      3. The String Type
      4. Numeric Types
    2. Function Types
    3. Summary
    4. Exercises
  15. 4 DESCRIBING MOTION
    1. Position and Velocity on an Air Track
    2. Types for Physical Quantities
    3. Introducing Derivatives
    4. Derivatives in Haskell
    5. Modeling the Car’s Position and Velocity
    6. Modeling Acceleration
    7. Approximate Algorithms and Finite Precision
    8. Summary
    9. Exercises
  16. 5 WORKING WITH LISTS
    1. List Basics
      1. Selecting an Element from a List
      2. Concatenating Lists
      3. Arithmetic Sequences
      4. List Types
      5. Functions for Lists of Numbers
      6. When Not to Use a List
    2. Type Variables
    3. Type Conversion
    4. The Length of Lists
    5. A String Is a List of Characters
    6. List Comprehensions
    7. Infinite Lists
    8. List Constructors and Pattern Matching
    9. Summary
    10. Exercises
  17. 6 HIGHER-ORDER FUNCTIONS
    1. How to Think About Functions with Parameters
    2. Mapping a Function Over a List
    3. Iteration and Recursion
    4. Anonymous Higher-Order Functions
    5. Operators as Higher-Order Functions
    6. Combinators
    7. Predicate-Based Higher-Order Functions
    8. Numerical Integration
      1. Introducing Integrators
      2. Digital Integration
      3. Implementing Antiderivatives
    9. Summary
    10. Exercises
  18. 7 GRAPHING FUNCTIONS
    1. Using Library Modules
      1. Standard Library Modules
      2. Other Library Modules
    2. Plotting
      1. Function Only
      2. Function and Module
      3. Function, Module, and Plot Definition
    3. Summary
    4. Exercises
  19. 8 TYPE CLASSES
    1. Type Classes and Numbers
    2. Type Classes from the Prelude
      1. The Eq Type Class
      2. The Show Type Class
      3. The Num Type Class
      4. The Integral Type Class
      5. The Ord Type Class
      6. The Fractional Type Class
      7. The Floating Type Class
    3. Exponentiation and Type Classes
    4. Sections
    5. Example of Type Classes and Plotting
    6. Summary
    7. Exercises
  20. 9 TUPLES AND TYPE CONSTRUCTORS
    1. Pairs
    2. Currying a Function of Two Variables
    3. Triples
    4. Comparing Lists and Tuples
    5. Maybe Types
    6. Lists of Pairs
    7. Tuples and List Comprehensions
    8. Type Constructors and Kinds
    9. Numerical Integration Redux
    10. Summary
    11. Exercises
  21. 10 DESCRIBING MOTION IN THREE DIMENSIONS
    1. Three-Dimensional Vectors
      1. Coordinate-Free Vectors
      2. Geometric Definition of Vector Addition
      3. Geometric Definition of Scaling a Vector
      4. Geometric Definition of Vector Subtraction
      5. Geometric Definition of Dot Product
      6. Geometric Definition of Cross Product
      7. Derivative of a Vector-Valued Function
    2. Coordinate Systems
      1. Vector Addition with Coordinate Components
      2. Vector Scaling with Coordinate Components
      3. Vector Subtraction with Coordinate Components
      4. Dot Product with Coordinate Components
      5. Cross Product with Coordinate Components
      6. Derivative with Coordinate Components
    3. Kinematics in 3D
      1. Defining Position, Velocity, and Acceleration
      2. Two Components of Acceleration
      3. Projectile Motion
    4. Making Your Own Data Type
      1. Single Data Constructor
      2. Multiple Data Constructors
    5. Defining a New Data Type for 3D Vectors
      1. Possible Implementations
      2. Data Type Definition for Vec
      3. Vec Functions
    6. Summary
    7. Exercises
  22. 11 CREATING GRAPHS
    1. Title and Axis Labels
    2. Other Labels
    3. Plotting Data
    4. Multiple Curves on One Set of Axes
    5. Controlling the Plot Ranges
    6. Making a Key
    7. Summary
    8. Exercises
  23. 12 CREATING STAND-ALONE PROGRAMS
    1. Using GHC to Make a Stand-Alone Program
      1. Hello, World!
      2. A Program That Imports Modules
    2. Using Cabal to Make a Stand-Alone Program
    3. Using Stack to Make a Stand-Alone Program
    4. Summary
    5. Exercises
  24. 13 CREATING 2D AND 3D ANIMATIONS
    1. 2D Animation
      1. Displaying a 2D Picture
      2. Making a 2D Animation
      3. Making a 2D Simulation
    2. 3D Animation
      1. Displaying a 3D Picture
      2. Making a 3D Animation
      3. Making a 3D Simulation
    3. Summary
    4. Exercises
  25. PART II EXPRESSING NEWTONIAN MECHANICS AND SOLVING PROBLEMS
  26. 14 NEWTON’S SECOND LAW AND DIFFERENTIAL EQUATIONS
    1. Newton’s First Law
    2. Newton’s Second Law in One Dimension
    3. Second Law with Constant Forces
    4. Second Law with Forces That Depend Only on Time
    5. Air Resistance
    6. Second Law with Forces That Depend Only on Velocity
      1. Euler Method by Hand
      2. Euler Method in Haskell
    7. The State of a Physical System
    8. Second Law with Forces That Depend on Time and Velocity
      1. Method 1: Produce a List of States
      2. Method 2: Produce a Velocity Function
    9. Example: Pedaling and Coasting with Air Resistance
      1. Euler Method by Hand
      2. Method 1: Produce a List of States
      3. Method 2: Produce a Velocity Function
    10. Summary
    11. Exercises
  27. 15 MECHANICS IN ONE DIMENSION
    1. Introductory Code
    2. Forces That Depend on Time, Position, and Velocity
      1. A General Strategy for Solving Mechanics Problems
      2. Solving with Euler’s Method
      3. Producing a List of States
      4. Position and Velocity Functions
    3. A Damped Harmonic Oscillator
      1. Euler Method by Hand
      2. Method 1: Producing a List of States
      3. Method 2: Producing Position and Velocity Functions
    4. Euler-Cromer Method
    5. Solving Differential Equations
      1. Generalizing the State Space
      2. Type Classes for State Spaces
      3. One More Numerical Method
      4. Comparison of Numerical Methods
    6. Summary
    7. Exercises
  28. 16 MECHANICS IN THREE DIMENSIONS
    1. Introductory Code
    2. Newton’s Second Law in Three Dimensions
    3. The State of One Particle
    4. Solving Newton’s Second Law
    5. One-Body Forces
      1. Earth Surface Gravity
      2. Gravity Produced by the Sun
      3. Air Resistance
      4. Wind Force
      5. Force from Uniform Electric and Magnetic Fields
    6. State Update for One Particle
    7. Preparing for Animation
      1. Two Helpful Animation Functions
      2. How the Functions Work
    8. Summary
    9. Exercises
  29. 17 SATELLITE, PROJECTILE, AND PROTON MOTION
    1. Satellite Motion
      1. State-Update Function
      2. Initial State
      3. Time-Scale Factor
      4. Animation Rate
      5. Display Function
    2. Projectile Motion with Air Resistance
      1. Calculating a Trajectory
      2. Finding the Angle for Maximum Range
      3. 2D Animation
      4. 3D Animation
    3. Proton in a Magnetic Field
    4. Summary
    5. Exercises
  30. 18 A VERY SHORT PRIMER ON RELATIVITY
    1. A Little Theory
    2. A Replacement for Newton’s Second Law
    3. Response to a Constant Force
    4. Proton in a Magnetic Field
    5. Summary
    6. Exercises
  31. 19 INTERACTING PARTICLES
    1. Newton’s Third Law
    2. Two-Body Forces
      1. Universal Gravity
      2. Constant Repulsive Force
      3. Linear Spring
      4. Central Force
      5. Elastic Billiard Interaction
    3. Internal and External Forces
    4. The State of a Multi-Particle System
    5. State Update for Multiple Particles
      1. Implementing Newton’s Second Law
      2. Numerical Methods for Multiple Particles
      3. Composite Functions
    6. Summary
    7. Exercises
  32. 20 SPRINGS, BILLIARD BALLS, AND A GUITAR STRING
    1. Introductory Code
    2. Two Masses and Two Springs
      1. Forces
      2. Animation Functions
      3. Stand-Alone Animation Program
      4. Using Mechanical Energy as a Guide to Numerical Accuracy
    3. A Collision
      1. Data Representations
      2. Spring Constant and Time Step
      3. Momentum and Energy Conservation
      4. Numerical Issues
      5. Animated Results
    4. Wave on a Guitar String
      1. Forces
      2. State-Update Function
      3. Initial State
      4. Stand-Alone Program
      5. Asynchronous Animation
    5. Summary
    6. Exercises
  33. PART III EXPRESSING ELECTROMAGNETIC THEORY AND SOLVING PROBLEMS
  34. 21 ELECTRICITY
    1. Electric Charge
    2. Coulomb’s Law
    3. Two Charges Interacting
      1. Looking at Extremes
      2. Modeling the Situation in Haskell
    4. Summary
    5. Exercises
  35. 22 COORDINATE SYSTEMS AND FIELDS
    1. Polar Coordinates
    2. Cylindrical Coordinates
    3. Spherical Coordinates
    4. Introductory Code
    5. A Type for Position
      1. Defining the New Type
      2. Making a Position
      3. Using a Position
    6. Displacement
    7. The Scalar Field
    8. The Vector Field
    9. Functions for Visualizing Scalar Fields
      1. 3D Visualization
      2. 2D Visualization
    10. Functions for Visualizing Vector Fields
      1. 3D Visualization
      2. 2D Visualization
      3. Gradient Visualization
    11. Summary
    12. Exercises
  36. 23 CURVES, SURFACES, AND VOLUMES
    1. Introductory Code
    2. Curves
      1. Parameterizing Curves
      2. Examples of Curves
    3. Surfaces
      1. Parameterizing Surfaces
      2. Examples of Surfaces
      3. Orientation
    4. Volumes
    5. Summary
    6. Exercises
  37. 24 ELECTRIC CHARGE
    1. Charge Distributions
    2. Introductory Code
    3. A Type for Charge Distribution
    4. Examples of Charge Distributions
    5. Total Charge
      1. Total Charge of a Line Charge
      2. Total Charge of a Surface Charge
      3. Total Charge of a Volume Charge
      4. Calculating Total Charge in Haskell
    6. Electric Dipole Moment
    7. Summary
    8. Exercises
  38. 25 ELECTRIC FIELD
    1. What Is an Electric Field?
    2. Introductory Code
    3. Charge Creates an Electric Field
      1. Electric Field Created by a Point Charge
      2. Electric Field Created by Multiple Charges
      3. Electric Field Created by a Line Charge
      4. Electric Field Created by a Surface Charge
      5. Electric Field Created by a Volume Charge
    4. Scalar Integrals
      1. Scalar Line Integral
      2. Scalar Surface Integral
      3. Scalar Volume Integral
    5. Approximating Curves, Surfaces, and Volumes
      1. Approximating a Curve
      2. Approximating a Surface
      3. Approximating a Volume
    6. Summary
    7. Exercises
  39. 26 ELECTRIC CURRENT
    1. Current Distributions
    2. Introductory Code
    3. A Type for Current Distribution
    4. Examples of Current Distributions
    5. Conservation of Charge and Constraints on Steady Current Distributions
    6. Magnetic Dipole Moment
    7. Summary
    8. Exercises
  40. 27 MAGNETIC FIELD
    1. A Simple Magnetic Effect
    2. Introductory Code
    3. Current Creates Magnetic Field
      1. Magnetic Field Created by a Line Current
      2. Magnetic Field Created by a Surface Current
      3. Magnetic Field Created by a Volume Current
    4. Summary
    5. Exercises
  41. 28 THE LORENTZ FORCE LAW
    1. Introductory Code
    2. Statics and Dynamics
    3. State of One Particle and Fields
    4. Lorentz Force Law
    5. Do We Really Need an Electric Field?
    6. State Update
    7. Animating a Particle in Electric and Magnetic Fields
      1. Uniform Fields
      2. Classical Hydrogen
    8. Summary
    9. Exercises
  42. 29 THE MAXWELL EQUATIONS
    1. Introductory Code
    2. The Maxwell Equations
      1. Relationships Between Electricity and Magnetism
      2. Connection to Coulomb’s Law and Biot-Savart Law
      3. State Update
      4. Spatial Derivatives and the Curl
      5. A Naive Method
    3. The FDTD Method
      1. The Yee Cell
      2. A Type for State
      3. FDTD and the Curl
      4. State Update
    4. Animation
      1. Current Density
      2. Grid Boundary
      3. Display Function
      4. Two Helping Functions
      5. Main Program
    5. Summary
    6. Exercises
  43. APPENDIX: INSTALLING HASKELL
    1. Installing GHC
    2. Installing a Text Editor
    3. Installing Gnuplot
    4. Installing Haskell Library Packages
      1. Using Cabal
      2. Using Stack
    5. Installing Gloss
    6. Installing Diagrams
    7. Setting Up Your Coding Environment
      1. What We Want in a Coding Environment
      2. All Code in One Directory
      3. One Way to Use Stack
    8. Summary
  44. BIBLIOGRAPHY
  45. INDEX

Product information

  • Title: Learn Physics with Functional Programming
  • Author(s): Scott N. Walck
  • Release date: January 2023
  • Publisher(s): No Starch Press
  • ISBN: 9781718501669