learn-physics-0.6.2: Haskell code for learning physics

Copyright (c) Scott N. Walck 2014-2018 BSD3 (see LICENSE) Scott N. Walck experimental Trustworthy Haskell98

Physics.Learn

Description

Functions for learning physics.

Synopsis

# Mechanics

type TheTime = Double Source #

Time (in s).

A time step (in s).

type Velocity = Vec Source #

Velocity of a particle (in m/s).

## Simple one-particle state

A simple one-particle state, to get started quickly with mechanics of one particle.

An acceleration function gives the particle's acceleration as a function of the particle's state. The specification of this function is what makes one single-particle mechanics problem different from another. In order to write this function, add all of the forces that act on the particle, and divide this net force by the particle's mass. (Newton's second law).

Arguments

 :: SimpleAccelerationFunction acceleration function for the particle -> DifferentialEquation SimpleState differential equation

Time derivative of state for a single particle with a constant mass.

Arguments

 :: SimpleAccelerationFunction acceleration function for the particle -> TimeStep time step -> SimpleState initial state -> SimpleState state after one time step

Single Runge-Kutta step

## One-particle state

data St Source #

The state of a single particle is given by the position of the particle and the velocity of the particle.

Constructors

 St Fieldsposition :: Position velocity :: Velocity

Instances

 Source # MethodsshowsPrec :: Int -> St -> ShowS #show :: St -> String #showList :: [St] -> ShowS # Source # Associated Typestype Diff St :: * Source # Methods(.-.) :: St -> St -> Diff St Source #(.+^) :: St -> Diff St -> St Source # type Diff St Source # type Diff St = DSt

data DSt Source #

The associated vector space for the state of a single particle.

Constructors

 DSt Vec Vec

Instances

 Source # MethodsshowsPrec :: Int -> DSt -> ShowS #show :: DSt -> String #showList :: [DSt] -> ShowS # Source # Associated Typestype Scalar DSt :: * # Methods(*^) :: Scalar DSt -> DSt -> DSt # Source # Methods(^+^) :: DSt -> DSt -> DSt #negateV :: DSt -> DSt #(^-^) :: DSt -> DSt -> DSt # type Scalar DSt Source # type Scalar DSt = Double

The state of a system of one particle is given by the current time, the position of the particle, and the velocity of the particle. Including time in the state like this allows us to have time-dependent forces.

An acceleration function gives the particle's acceleration as a function of the particle's state.

Arguments

 :: OneParticleAccelerationFunction acceleration function for the particle -> DifferentialEquation OneParticleSystemState differential equation

Time derivative of state for a single particle with a constant mass.

Arguments

 :: OneParticleAccelerationFunction acceleration function for the particle -> TimeStep time step -> OneParticleSystemState initial state -> OneParticleSystemState state after one time step

Single Runge-Kutta step

Arguments

 :: OneParticleAccelerationFunction acceleration function for the particle -> TimeStep time step -> OneParticleSystemState initial state -> [OneParticleSystemState] state after one time step

List of system states

## Two-particle state

type TwoParticleSystemState = (TheTime, St, St) Source #

The state of a system of two particles is given by the current time, the position and velocity of particle 1, and the position and velocity of particle 2.

An acceleration function gives a pair of accelerations (one for particle 1, one for particle 2) as a function of the system's state.

Arguments

 :: TwoParticleAccelerationFunction acceleration function for two particles -> DifferentialEquation TwoParticleSystemState differential equation

Time derivative of state for two particles with constant mass.

Arguments

 :: TwoParticleAccelerationFunction acceleration function -> TimeStep time step -> TwoParticleSystemState initial state -> TwoParticleSystemState state after one time step

Single Runge-Kutta step for two-particle system

## Many-particle state

type ManyParticleSystemState = (TheTime, [St]) Source #

The state of a system of many particles is given by the current time and a list of one-particle states.

An acceleration function gives a list of accelerations (one for each particle) as a function of the system's state.

Arguments

 :: ManyParticleAccelerationFunction acceleration function for many particles -> DifferentialEquation ManyParticleSystemState differential equation

Time derivative of state for many particles with constant mass.

Arguments

 :: ManyParticleAccelerationFunction acceleration function -> TimeStep time step -> ManyParticleSystemState initial state -> ManyParticleSystemState state after one time step

Single Runge-Kutta step for many-particle system

# E&M

## Charge

type Charge = Double Source #

Electric charge, in units of Coulombs (C)

A charge distribution is a point charge, a line charge, a surface charge, a volume charge, or a combination of these. The ScalarField describes a linear charge density, a surface charge density, or a volume charge density.

Constructors

 PointCharge Charge Position point charge LineCharge ScalarField Curve ScalarField is linear charge density (C/m) SurfaceCharge ScalarField Surface ScalarField is surface charge density (C/m^2) VolumeCharge ScalarField Volume ScalarField is volume charge density (C/m^3) MultipleCharges [ChargeDistribution] combination of charge distributions

Total charge (in C) of a charge distribution.

## Current

type Current = Double Source #

Electric current, in units of Amperes (A)

A current distribution is a line current (current through a wire), a surface current, a volume current, or a combination of these. The VectorField describes a surface current density or a volume current density.

Constructors

 LineCurrent Current Curve current through a wire SurfaceCurrent VectorField Surface VectorField is surface current density (A/m) VolumeCurrent VectorField Volume VectorField is volume current density (A/m^2) MultipleCurrents [CurrentDistribution] combination of current distributions

## Electric Field

The electric field produced by a charge distribution. This is the simplest way to find the electric field, because it works for any charge distribution (point, line, surface, volume, or combination).

## Electric Flux

The electric flux through a surface produced by a charge distribution.

## Electric Potential

Arguments

 :: Position position where electric potential is zero -> VectorField electric field -> ScalarField electric potential

Electric potential from electric field, given a position to be the zero of electric potential.

Electric potential produced by a charge distribution. The position where the electric potential is zero is taken to be infinity.

## Magnetic Field

The magnetic field produced by a current distribution. This is the simplest way to find the magnetic field, because it works for any current distribution (line, surface, volume, or combination).

## Magnetic Flux

The magnetic flux through a surface produced by a current distribution.

# Geometry

## Vectors

data Vec Source #

A type for vectors.

Instances

 Source # Methods(==) :: Vec -> Vec -> Bool #(/=) :: Vec -> Vec -> Bool # Source # MethodsshowsPrec :: Int -> Vec -> ShowS #show :: Vec -> String #showList :: [Vec] -> ShowS # Source # Associated Typestype Diff Vec :: * Source # Methods type Scalar Vec # type Scalar Vec = Double type Diff Vec Source # type Diff Vec = Vec

x component

y component

z component

Arguments

 :: Double x component -> Double y component -> Double z component -> Vec

Form a vector by giving its x, y, and z components.

(^+^) :: AdditiveGroup v => v -> v -> v infixl 6 #

(^-^) :: AdditiveGroup v => v -> v -> v infixl 6 #

Group subtraction

(*^) :: VectorSpace v => Scalar v -> v -> v infixr 7 #

Scale a vector

(^*) :: (VectorSpace v, (~) * s (Scalar v)) => v -> s -> v infixl 7 #

Vector multiplied by scalar

(^/) :: (VectorSpace v, (~) * s (Scalar v), Fractional s) => v -> s -> v infixr 7 #

Vector divided by scalar

(<.>) :: InnerSpace v => v -> v -> Scalar v infixr 7 #

Inner/dot product

(><) :: Vec -> Vec -> Vec infixl 7 Source #

Cross product.

magnitude :: (InnerSpace v, (~) * s (Scalar v), Floating s) => v -> s #

Length of a vector. See also magnitudeSq.

zeroV :: AdditiveGroup v => v #

The zero element: identity for '(^+^)'

negateV :: AdditiveGroup v => v -> v #

sumV :: (Foldable f, AdditiveGroup v) => f v -> v #

Sum over several vectors

Unit vector in the x direction.

Unit vector in the y direction.

Unit vector in the z direction.

## Position

data Position Source #

A type for position. Position is not a vector because it makes no sense to add positions.

Instances

 Source # MethodsshowList :: [Position] -> ShowS # Source # Position is not a vector, but displacement (difference in position) is a vector. Associated Typestype Diff Position :: * Source # Methods type Diff Position Source # type Diff Position = Vec

A displacement is a vector.

A scalar field associates a number with each position in space.

A vector field associates a vector with each position in space.

type Field v = Position -> v Source #

Sometimes we want to be able to talk about a field without saying whether it is a scalar field or a vector field.

type CoordinateSystem = (Double, Double, Double) -> Position Source #

A coordinate system is a function from three parameters to space.

The Cartesian coordinate system. Coordinates are (x,y,z).

The cylindrical coordinate system. Coordinates are (s,phi,z), where s is the distance from the z axis and phi is the angle with the x axis.

The spherical coordinate system. Coordinates are (r,theta,phi), where r is the distance from the origin, theta is the angle with the z axis, and phi is the azimuthal angle.

Arguments

 :: Double x coordinate -> Double y coordinate -> Double z coordinate -> Position

A helping function to take three numbers x, y, and z and form the appropriate position using Cartesian coordinates.

Arguments

 :: Double s coordinate -> Double phi coordinate -> Double z coordinate -> Position

A helping function to take three numbers s, phi, and z and form the appropriate position using cylindrical coordinates.

Arguments

 :: Double r coordinate -> Double theta coordinate -> Double phi coordinate -> Position

A helping function to take three numbers r, theta, and phi and form the appropriate position using spherical coordinates.

Returns the three Cartesian coordinates as a triple from a position.

Returns the three cylindrical coordinates as a triple from a position.

Returns the three spherical coordinates as a triple from a position.

Arguments

 :: Position source position -> Position target position -> Displacement

Displacement from source position to target position.

Shift a position by a displacement.

shiftObject :: Displacement -> (a -> Position) -> a -> Position Source #

An object is a map into Position.

shiftField :: Displacement -> (Position -> v) -> Position -> v Source #

A field is a map from Position.

addFields :: AdditiveGroup v => [Field v] -> Field v Source #

Add two scalar fields or two vector fields.

The vector field in which each point in space is associated with a unit vector in the direction of increasing spherical coordinate r, while spherical coordinates theta and phi are held constant. Defined everywhere except at the origin. The unit vector rHat points in different directions at different points in space. It is therefore better interpreted as a vector field, rather than a vector.

The vector field in which each point in space is associated with a unit vector in the direction of increasing spherical coordinate theta, while spherical coordinates r and phi are held constant. Defined everywhere except on the z axis.

The vector field in which each point in space is associated with a unit vector in the direction of increasing (cylindrical or spherical) coordinate phi, while cylindrical coordinates s and z (or spherical coordinates r and theta) are held constant. Defined everywhere except on the z axis.

The vector field in which each point in space is associated with a unit vector in the direction of increasing cylindrical coordinate s, while cylindrical coordinates phi and z are held constant. Defined everywhere except on the z axis.

The vector field in which each point in space is associated with a unit vector in the direction of increasing Cartesian coordinate x, while Cartesian coordinates y and z are held constant. Defined everywhere.

The vector field in which each point in space is associated with a unit vector in the direction of increasing Cartesian coordinate y, while Cartesian coordinates x and z are held constant. Defined everywhere.

The vector field in which each point in space is associated with a unit vector in the direction of increasing Cartesian coordinate z, while Cartesian coordinates x and y are held constant. Defined everywhere.

## Curves

data Curve Source #

Curve is a parametrized function into three-space, an initial limit, and a final limit.

Constructors

 Curve FieldscurveFunc :: Double -> Positionfunction from one parameter into spacestartingCurveParam :: Doublestarting value of the parameterendingCurveParam :: Doubleending value of the parameter

Reparametrize a curve from 0 to 1.

Arguments

 :: Curve go first along this curve -> Curve then along this curve -> Curve to produce this new curve

Concatenate two curves.

Concatenate a list of curves. Parametrizes curves equally.

Reverse a curve.

Arguments

 :: Curve the curve -> Double the parameter -> Position position of the point on the curve at that parameter

Evaluate the position of a curve at a parameter.

Arguments

 :: Displacement amount to shift -> Curve original curve -> Curve shifted curve

Shift a curve by a displacement.

Arguments

 :: Position starting position -> Position ending position -> Curve straight-line curve

The straight-line curve from one position to another.

## Line Integrals

Arguments

 :: (InnerSpace v, Scalar v ~ Double) => Int number of intervals -> Field v scalar or vector field -> Curve curve to integrate over -> v scalar or vector result

Calculates integral f dl over curve, where dl is a scalar line element.

Arguments

 :: Int number of half-intervals (one less than the number of function evaluations) -> VectorField vector field -> Curve curve to integrate over -> Double scalar result

A dotted line integral. Convenience function for compositeSimpsonDottedLineIntegral.

Arguments

 :: Int number of half-intervals (one less than the number of function evaluations) -> VectorField vector field -> Curve curve to integrate over -> Vec vector result

Calculates integral vf x dl over curve. Convenience function for compositeSimpsonCrossedLineIntegral.

## Surfaces

data Surface Source #

Surface is a parametrized function from two parameters to space, lower and upper limits on the first parameter, and lower and upper limits for the second parameter (expressed as functions of the first parameter).

Constructors

 Surface FieldssurfaceFunc :: (Double, Double) -> Positionfunction from two parameters (s,t) into spacelowerLimit :: Doubles_lupperLimit :: Doubles_ulowerCurve :: Double -> Doublet_l(s)upperCurve :: Double -> Doublet_u(s)

A unit sphere, centered at the origin.

A sphere with given radius centered at the origin.

Sphere with given radius and center.

The upper half of a unit sphere, centered at the origin.

A disk with given radius, centered at the origin.

Shift a surface by a displacement.

## Surface Integrals

Arguments

 :: (VectorSpace v, Scalar v ~ Double) => Int number of intervals for first parameter, s -> Int number of intervals for second parameter, t -> Field v the scalar or vector field to integrate -> Surface the surface over which to integrate -> v the resulting scalar or vector

A plane surface integral, in which area element is a scalar.

Arguments

 :: Int number of intervals for first parameter, s -> Int number of intervals for second parameter, t -> VectorField the vector field to integrate -> Surface the surface over which to integrate -> Double the resulting scalar

A dotted surface integral, in which area element is a vector.

## Volumes

data Volume Source #

Volume is a parametrized function from three parameters to space, lower and upper limits on the first parameter, lower and upper limits for the second parameter (expressed as functions of the first parameter), and lower and upper limits for the third parameter (expressed as functions of the first and second parameters).

Constructors

 Volume FieldsvolumeFunc :: (Double, Double, Double) -> Positionfunction from 3 parameters to spaceloLimit :: Doubles_aupLimit :: Doubles_bloCurve :: Double -> Doublet_a(s)upCurve :: Double -> Doublet_b(s)loSurf :: Double -> Double -> Doubleu_a(s,t)upSurf :: Double -> Double -> Doubleu_b(s,t)

A unit ball, centered at the origin.

A unit ball, centered at the origin. Specified in Cartesian coordinates.

A ball with given radius, centered at the origin.

Arguments

 :: Double radius -> Position center -> Volume ball with given radius and center

Ball with given radius and center.

Upper half ball, unit radius, centered at origin.

Cylinder with given radius and height. Circular base of the cylinder is centered at the origin. Circular top of the cylinder lies in plane z = h.

Shift a volume by a displacement.

## Volume Integral

Arguments

 :: (VectorSpace v, Scalar v ~ Double) => Int number of intervals for first parameter (s) -> Int number of intervals for second parameter (t) -> Int number of intervals for third parameter (u) -> Field v scalar or vector field -> Volume the volume -> v scalar or vector result

A volume integral

# Differential Equations

class (VectorSpace (Diff p), Fractional (Scalar (Diff p))) => StateSpace p where Source #

An instance of StateSpace is a data type that can serve as the state of some system. Alternatively, a StateSpace is a collection of dependent variables for a differential equation. A StateSpace has an associated vector space for the (time) derivatives of the state. The associated vector space is a linearized version of the StateSpace.

Minimal complete definition

Associated Types

type Diff p Source #

Associated vector space

Methods

(.-.) :: p -> p -> Diff p infix 6 Source #

Subtract points

(.+^) :: p -> Diff p -> p infixl 6 Source #

Point plus vector

Instances

 Source # Associated Typestype Diff Double :: * Source # Methods Source # Associated Typestype Diff Vec :: * Source # Methods Source # Position is not a vector, but displacement (difference in position) is a vector. Associated Typestype Diff Position :: * Source # Methods Source # Associated Typestype Diff St :: * Source # Methods(.-.) :: St -> St -> Diff St Source #(.+^) :: St -> Diff St -> St Source # StateSpace p => StateSpace [p] Source # Associated Typestype Diff [p] :: * Source # Methods(.-.) :: [p] -> [p] -> Diff [p] Source #(.+^) :: [p] -> Diff [p] -> [p] Source # (StateSpace p, StateSpace q, (~) * (Time p) (Time q)) => StateSpace (p, q) Source # Associated Typestype Diff (p, q) :: * Source # Methods(.-.) :: (p, q) -> (p, q) -> Diff (p, q) Source #(.+^) :: (p, q) -> Diff (p, q) -> (p, q) Source # (StateSpace p, StateSpace q, StateSpace r, (~) * (Time p) (Time q), (~) * (Time q) (Time r)) => StateSpace (p, q, r) Source # Associated Typestype Diff (p, q, r) :: * Source # Methods(.-.) :: (p, q, r) -> (p, q, r) -> Diff (p, q, r) Source #(.+^) :: (p, q, r) -> Diff (p, q, r) -> (p, q, r) Source #

(.-^) :: StateSpace p => p -> Diff p -> p infixl 6 Source #

Point minus vector

type Time p = Scalar (Diff p) Source #

The scalars of the associated vector space can be thought of as time intervals.

type DifferentialEquation state = state -> Diff state Source #

A differential equation expresses how the dependent variables (state) change with the independent variable (time). A differential equation is specified by giving the (time) derivative of the state as a function of the state. The (time) derivative of a state is an element of the associated vector space.

type InitialValueProblem state = (DifferentialEquation state, state) Source #

An initial value problem is a differential equation along with an initial state.

type EvolutionMethod state Source #

Arguments

 = DifferentialEquation state differential equation -> Time state time interval -> state initial state -> state evolved state

An evolution method is a way of approximating the state after advancing a finite interval in the independent variable (time) from a given state.

type SolutionMethod state = InitialValueProblem state -> [state] Source #

A (numerical) solution method is a way of converting an initial value problem into a list of states (a solution). The list of states need not be equally spaced in time.

stepSolution :: EvolutionMethod state -> Time state -> SolutionMethod state Source #

Given an evolution method and a time step, return the solution method which applies the evolution method repeatedly with with given time step. The solution method returned will produce an infinite list of states.

eulerMethod :: StateSpace state => EvolutionMethod state Source #

The Euler method is the simplest evolution method. It increments the state by the derivative times the time step.

rungeKutta4 :: StateSpace p => (p -> Diff p) -> Time p -> p -> p Source #

Take a single 4th-order Runge-Kutta step

integrateSystem :: StateSpace p => (p -> Diff p) -> Time p -> p -> [p] Source #

Solve a first-order system of differential equations with 4th-order Runge-Kutta

# Visualization

## Plotting

label :: String -> (Double, Double) -> Attribute Source #

An Attribute with a given label at a given position.

An Attribute that requests postscript output.

An Attribute giving the postscript file name.

## Gloss library

polarToCart :: (Float, Float) -> (Float, Float) Source #

assumes radians coming in

cartToPolar :: (Float, Float) -> (Float, Float) Source #

theta=0 is positive x axis, output angle in radians

Arguments

 :: Point location of base of arrow -> Point displacement vector -> Picture

An arrow

Arguments

 :: Float arrow thickness -> Point location of base of arrow -> Point displacement vector -> Picture

A think arrow