Learn Physics with Functional Programming: A Hands-on Guide to Exploring Physics with Haskell

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

Deepen your understanding of physics by learning to use the Haskell functional programming language.

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. 

Haskell’s powerful system of types is capable of encoding important mathematical structures like vectors, derivatives, integrals, scalar fields, and differential equations. In addition, you’ll explore Newtonian mechanics and electromagnetics, analyze source code, and discover why Haskell’s high-order functions and referential transparency serve physics so well.

Author(s): Scott N. Walck
Edition: 1
Publisher: No Starch Press
Year: 2023

Language: English
Commentary: Yet Another EPUB
Pages: 648
City: San Francisco, CA
Tags: Physics; Functional Programming; Haskell; Newtonian Mechanics; Electromagnetics; Vectors; Derivatives; Integrals; Scalar Fields; Differential Equations

Cover Page
Title Page
Copyright Page
Dedication
About the Author
About the Technical Reviewer
Brief Contents
Contents in Detail
ACKNOWLEDGMENTS
INTRODUCTION

Who This Book Is For
Why Functional Programming, and Why Haskell?
About This Book

PART I A HASKELL PRIMER FOR PHYSICISTS
1 CALCULATING WITH HASKELL

A Kinematics Problem
The Interactive Compiler
Numeric Functions
Operators
Precedence and Associativity
The Application Operator
Functions with Two Arguments
Numbers in Haskell
Negative Numbers in Haskell
Decimal Numbers in Haskell
Exponential Notation
Approximate Calculation
Errors
Getting Help and Quitting
More Information
Summary
Exercises

2 WRITING BASIC FUNCTIONS

Constants, Functions, and Types
How We Talk About Functions
Anonymous Functions
Composing Functions
Variable Not in Scope Error
Summary
Exercises

3 TYPES AND ENTITIES

Basic Types
The Boolean Type
The Character Type
The String Type
Numeric Types
Function Types
Summary
Exercises

4 DESCRIBING MOTION

Position and Velocity on an Air Track
Types for Physical Quantities
Introducing Derivatives
Derivatives in Haskell
Modeling the Car’s Position and Velocity
Modeling Acceleration
Approximate Algorithms and Finite Precision
Summary
Exercises

5 WORKING WITH LISTS

List Basics
Selecting an Element from a List
Concatenating Lists
Arithmetic Sequences
List Types
Functions for Lists of Numbers
When Not to Use a List
Type Variables
Type Conversion
The Length of Lists
A String Is a List of Characters
List Comprehensions
Infinite Lists
List Constructors and Pattern Matching
Summary
Exercises

6 HIGHER-ORDER FUNCTIONS

How to Think About Functions with Parameters
Mapping a Function Over a List
Iteration and Recursion
Anonymous Higher-Order Functions
Operators as Higher-Order Functions
Combinators
Predicate-Based Higher-Order Functions
Numerical Integration
Introducing Integrators
Digital Integration
Implementing Antiderivatives
Summary
Exercises

7 GRAPHING FUNCTIONS

Using Library Modules
Standard Library Modules
Other Library Modules
Plotting
Function Only
Function and Module
Function, Module, and Plot Definition
Summary
Exercises

8 TYPE CLASSES

Type Classes and Numbers
Type Classes from the Prelude
The Eq Type Class
The Show Type Class
The Num Type Class
The Integral Type Class
The Ord Type Class
The Fractional Type Class
The Floating Type Class
Exponentiation and Type Classes
Sections
Example of Type Classes and Plotting
Summary
Exercises

9 TUPLES AND TYPE CONSTRUCTORS

Pairs
Currying a Function of Two Variables
Triples
Comparing Lists and Tuples
Maybe Types
Lists of Pairs
Tuples and List Comprehensions
Type Constructors and Kinds
Numerical Integration Redux
Summary
Exercises

10 DESCRIBING MOTION IN THREE DIMENSIONS

Three-Dimensional Vectors
Coordinate-Free Vectors
Geometric Definition of Vector Addition
Geometric Definition of Scaling a Vector
Geometric Definition of Vector Subtraction
Geometric Definition of Dot Product
Geometric Definition of Cross Product
Derivative of a Vector-Valued Function
Coordinate Systems
Vector Addition with Coordinate Components
Vector Scaling with Coordinate Components
Vector Subtraction with Coordinate Components
Dot Product with Coordinate Components
Cross Product with Coordinate Components
Derivative with Coordinate Components
Kinematics in 3D
Defining Position, Velocity, and Acceleration
Two Components of Acceleration
Projectile Motion
Making Your Own Data Type
Single Data Constructor
Multiple Data Constructors
Defining a New Data Type for 3D Vectors
Possible Implementations
Data Type Definition for Vec
Vec Functions
Summary
Exercises

11 CREATING GRAPHS

Title and Axis Labels
Other Labels
Plotting Data
Multiple Curves on One Set of Axes
Controlling the Plot Ranges
Making a Key
Summary
Exercises

12 CREATING STAND-ALONE PROGRAMS

Using GHC to Make a Stand-Alone Program
Hello, World!
A Program That Imports Modules
Using Cabal to Make a Stand-Alone Program
Using Stack to Make a Stand-Alone Program
Summary
Exercises

13 CREATING 2D AND 3D ANIMATIONS

2D Animation
Displaying a 2D Picture
Making a 2D Animation
Making a 2D Simulation
3D Animation
Displaying a 3D Picture
Making a 3D Animation
Making a 3D Simulation
Summary
Exercises

PART II EXPRESSING NEWTONIAN MECHANICS AND SOLVING PROBLEMS
14 NEWTON’S SECOND LAW AND DIFFERENTIAL EQUATIONS

Newton’s First Law
Newton’s Second Law in One Dimension
Second Law with Constant Forces
Second Law with Forces That Depend Only on Time
Air Resistance
Second Law with Forces That Depend Only on Velocity
Euler Method by Hand
Euler Method in Haskell
The State of a Physical System
Second Law with Forces That Depend on Time and Velocity
Method 1: Produce a List of States
Method 2: Produce a Velocity Function
Example: Pedaling and Coasting with Air Resistance
Euler Method by Hand
Method 1: Produce a List of States
Method 2: Produce a Velocity Function
Summary
Exercises

15 MECHANICS IN ONE DIMENSION

Introductory Code
Forces That Depend on Time, Position, and Velocity
A General Strategy for Solving Mechanics Problems
Solving with Euler’s Method
Producing a List of States
Position and Velocity Functions
A Damped Harmonic Oscillator
Euler Method by Hand
Method 1: Producing a List of States
Method 2: Producing Position and Velocity Functions
Euler-Cromer Method
Solving Differential Equations
Generalizing the State Space
Type Classes for State Spaces
One More Numerical Method
Comparison of Numerical Methods
Summary
Exercises

16 MECHANICS IN THREE DIMENSIONS

Introductory Code
Newton’s Second Law in Three Dimensions
The State of One Particle
Solving Newton’s Second Law
One-Body Forces
Earth Surface Gravity
Gravity Produced by the Sun
Air Resistance
Wind Force
Force from Uniform Electric and Magnetic Fields
State Update for One Particle
Preparing for Animation
Two Helpful Animation Functions
How the Functions Work
Summary
Exercises

17 SATELLITE, PROJECTILE, AND PROTON MOTION

Satellite Motion
State-Update Function
Initial State
Time-Scale Factor
Animation Rate
Display Function
Projectile Motion with Air Resistance
Calculating a Trajectory
Finding the Angle for Maximum Range
2D Animation
3D Animation
Proton in a Magnetic Field
Summary
Exercises

18 A VERY SHORT PRIMER ON RELATIVITY

A Little Theory
A Replacement for Newton’s Second Law
Response to a Constant Force
Proton in a Magnetic Field
Summary
Exercises

19 INTERACTING PARTICLES

Newton’s Third Law
Two-Body Forces
Universal Gravity
Constant Repulsive Force
Linear Spring
Central Force
Elastic Billiard Interaction
Internal and External Forces
The State of a Multi-Particle System
State Update for Multiple Particles
Implementing Newton’s Second Law
Numerical Methods for Multiple Particles
Composite Functions
Summary
Exercises

20 SPRINGS, BILLIARD BALLS, AND A GUITAR STRING

Introductory Code
Two Masses and Two Springs
Forces
Animation Functions
Stand-Alone Animation Program
Using Mechanical Energy as a Guide to Numerical Accuracy
A Collision
Data Representations
Spring Constant and Time Step
Momentum and Energy Conservation
Numerical Issues
Animated Results
Wave on a Guitar String
Forces
State-Update Function
Initial State
Stand-Alone Program
Asynchronous Animation
Summary
Exercises

PART III EXPRESSING ELECTROMAGNETIC THEORY AND SOLVING PROBLEMS
21 ELECTRICITY

Electric Charge
Coulomb’s Law
Two Charges Interacting
Looking at Extremes
Modeling the Situation in Haskell
Summary
Exercises

22 COORDINATE SYSTEMS AND FIELDS

Polar Coordinates
Cylindrical Coordinates
Spherical Coordinates
Introductory Code
A Type for Position
Defining the New Type
Making a Position
Using a Position
Displacement
The Scalar Field
The Vector Field
Functions for Visualizing Scalar Fields
3D Visualization
2D Visualization
Functions for Visualizing Vector Fields
3D Visualization
2D Visualization
Gradient Visualization
Summary
Exercises

23 CURVES, SURFACES, AND VOLUMES

Introductory Code
Curves
Parameterizing Curves
Examples of Curves
Surfaces
Parameterizing Surfaces
Examples of Surfaces
Orientation
Volumes
Summary
Exercises

24 ELECTRIC CHARGE

Charge Distributions
Introductory Code
A Type for Charge Distribution
Examples of Charge Distributions
Total Charge
Total Charge of a Line Charge
Total Charge of a Surface Charge
Total Charge of a Volume Charge
Calculating Total Charge in Haskell
Electric Dipole Moment
Summary
Exercises

25 ELECTRIC FIELD

What Is an Electric Field?
Introductory Code
Charge Creates an Electric Field
Electric Field Created by a Point Charge
Electric Field Created by Multiple Charges
Electric Field Created by a Line Charge
Electric Field Created by a Surface Charge
Electric Field Created by a Volume Charge
Scalar Integrals
Scalar Line Integral
Scalar Surface Integral
Scalar Volume Integral
Approximating Curves, Surfaces, and Volumes
Approximating a Curve
Approximating a Surface
Approximating a Volume
Summary
Exercises

26 ELECTRIC CURRENT

Current Distributions
Introductory Code
A Type for Current Distribution
Examples of Current Distributions
Conservation of Charge and Constraints on Steady Current Distributions
Magnetic Dipole Moment
Summary
Exercises

27 MAGNETIC FIELD

A Simple Magnetic Effect
Introductory Code
Current Creates Magnetic Field
Magnetic Field Created by a Line Current
Magnetic Field Created by a Surface Current
Magnetic Field Created by a Volume Current
Summary
Exercises

28 THE LORENTZ FORCE LAW

Introductory Code
Statics and Dynamics
State of One Particle and Fields
Lorentz Force Law
Do We Really Need an Electric Field?
State Update
Animating a Particle in Electric and Magnetic Fields
Uniform Fields
Classical Hydrogen
Summary
Exercises

29 THE MAXWELL EQUATIONS

Introductory Code
The Maxwell Equations
Relationships Between Electricity and Magnetism
Connection to Coulomb’s Law and Biot-Savart Law
State Update
Spatial Derivatives and the Curl
A Naive Method
The FDTD Method
The Yee Cell
A Type for State
FDTD and the Curl
State Update
Animation
Current Density
Grid Boundary
Display Function
Two Helping Functions
Main Program
Summary
Exercises

APPENDIX: INSTALLING HASKELL

Installing GHC
Installing a Text Editor
Installing Gnuplot
Installing Haskell Library Packages
Using Cabal
Using Stack
Installing Gloss
Installing Diagrams
Setting Up Your Coding Environment
What We Want in a Coding Environment
All Code in One Directory
One Way to Use Stack
Summary

BIBLIOGRAPHY
INDEX