learn-physics-0.6.0.0: Haskell code for learning physics

Copyright(c) Scott N. Walck 2014
LicenseBSD3 (see LICENSE)
MaintainerScott N. Walck <walck@lvc.edu>
Stabilityexperimental
Safe HaskellTrustworthy
LanguageHaskell98

Physics.Learn

Contents

Description

Functions for learning physics.

Synopsis

Mechanics

type TheTime = Double Source

Time (in s).

type TimeStep = Double Source

A time step (in s).

type Velocity = Vec Source

Velocity of a particle (in m/s).

Simple one-particle state

type SimpleState = (TheTime, Position, Velocity) Source

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

type SimpleAccelerationFunction = SimpleState -> Vec Source

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).

simpleStateDeriv Source

Arguments

:: SimpleAccelerationFunction

acceleration function for the particle

-> DifferentialEquation SimpleState

differential equation

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

simpleRungeKuttaStep Source

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 

data DSt Source

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

Constructors

DSt Vec Vec 

type OneParticleSystemState = (TheTime, St) Source

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.

type OneParticleAccelerationFunction = OneParticleSystemState -> Vec Source

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

oneParticleStateDeriv Source

Arguments

:: OneParticleAccelerationFunction

acceleration function for the particle

-> DifferentialEquation OneParticleSystemState

differential equation

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

oneParticleRungeKuttaStep Source

Arguments

:: OneParticleAccelerationFunction

acceleration function for the particle

-> TimeStep

time step

-> OneParticleSystemState

initial state

-> OneParticleSystemState

state after one time step

Single Runge-Kutta step

oneParticleRungeKuttaSolution Source

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.

type TwoParticleAccelerationFunction = TwoParticleSystemState -> (Vec, Vec) Source

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

twoParticleStateDeriv Source

Arguments

:: TwoParticleAccelerationFunction

acceleration function for two particles

-> DifferentialEquation TwoParticleSystemState

differential equation

Time derivative of state for two particles with constant mass.

twoParticleRungeKuttaStep Source

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.

type ManyParticleAccelerationFunction = ManyParticleSystemState -> [Vec] Source

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

manyParticleStateDeriv Source

Arguments

:: ManyParticleAccelerationFunction

acceleration function for many particles

-> DifferentialEquation ManyParticleSystemState

differential equation

Time derivative of state for many particles with constant mass.

manyParticleRungeKuttaStep Source

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)

data ChargeDistribution Source

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

totalCharge :: ChargeDistribution -> Charge Source

Total charge (in C) of a charge distribution.

Current

type Current = Double Source

Electric current, in units of Amperes (A)

data CurrentDistribution Source

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

eField :: ChargeDistribution -> VectorField Source

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

electricFlux :: Surface -> ChargeDistribution -> Double Source

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

Electric Potential

electricPotentialFromField Source

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.

electricPotentialFromCharge :: ChargeDistribution -> ScalarField Source

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

Magnetic Field

bField :: CurrentDistribution -> VectorField Source

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

magneticFlux :: Surface -> CurrentDistribution -> Double Source

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

Geometry

Vectors

data Vec Source

A type for vectors.

xComp :: Vec -> Double Source

x component

yComp :: Vec -> Double Source

y component

zComp :: Vec -> Double Source

z component

vec Source

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

Add vectors

(^-^) :: AdditiveGroup v => v -> v -> v

Group subtraction

(*^) :: VectorSpace v => Scalar v -> v -> v

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

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

Additive inverse

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

Sum over several vectors

iHat :: Vec Source

Unit vector in the x direction.

jHat :: Vec Source

Unit vector in the y direction.

kHat :: Vec Source

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

Show Position Source 
StateSpace Position Source

Position is not a vector, but displacement (difference in position) is a vector.

type Diff Position = Vec Source 

type Displacement = Vec Source

A displacement is a vector.

type ScalarField = Position -> Double Source

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

type VectorField = Position -> Vec Source

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.

cartesian :: CoordinateSystem Source

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

cylindrical :: CoordinateSystem Source

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.

spherical :: CoordinateSystem Source

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.

cart Source

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.

cyl Source

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.

sph Source

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.

cartesianCoordinates :: Position -> (Double, Double, Double) Source

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

cylindricalCoordinates :: Position -> (Double, Double, Double) Source

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

sphericalCoordinates :: Position -> (Double, Double, Double) Source

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

displacement Source

Arguments

:: Position

source position

-> Position

target position

-> Displacement 

Displacement from source position to target position.

shiftPosition :: Displacement -> Position -> Position Source

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.

rHat :: VectorField Source

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.

thetaHat :: VectorField Source

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.

phiHat :: VectorField Source

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.

sHat :: VectorField Source

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.

xHat :: VectorField Source

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.

yHat :: VectorField Source

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.

zHat :: VectorField Source

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 

Fields

curveFunc :: Double -> Position

function from one parameter into space

startingCurveParam :: Double

starting value of the parameter

endingCurveParam :: Double

ending value of the parameter

normalizeCurve :: Curve -> Curve Source

Reparametrize a curve from 0 to 1.

concatCurves Source

Arguments

:: Curve

go first along this curve

-> Curve

then along this curve

-> Curve

to produce this new curve

Concatenate two curves.

concatenateCurves :: [Curve] -> Curve Source

Concatenate a list of curves. Parametrizes curves equally.

reverseCurve :: Curve -> Curve Source

Reverse a curve.

evalCurve Source

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.

shiftCurve Source

Arguments

:: Displacement

amount to shift

-> Curve

original curve

-> Curve

shifted curve

Shift a curve by a displacement.

straightLine Source

Arguments

:: Position

starting position

-> Position

ending position

-> Curve

straight-line curve

The straight-line curve from one position to another.

Line Integrals

simpleLineIntegral Source

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.

dottedLineIntegral Source

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.

crossedLineIntegral Source

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 

Fields

surfaceFunc :: (Double, Double) -> Position

function from two parameters (s,t) into space

lowerLimit :: Double

s_l

upperLimit :: Double

s_u

lowerCurve :: Double -> Double

t_l(s)

upperCurve :: Double -> Double

t_u(s)

unitSphere :: Surface Source

A unit sphere, centered at the origin.

centeredSphere :: Double -> Surface Source

A sphere with given radius centered at the origin.

sphere :: Double -> Position -> Surface Source

Sphere with given radius and center.

northernHemisphere :: Surface Source

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

disk :: Double -> Surface Source

A disk with given radius, centered at the origin.

shiftSurface :: Displacement -> Surface -> Surface Source

Shift a surface by a displacement.

Surface Integrals

surfaceIntegral Source

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.

dottedSurfaceIntegral Source

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 

Fields

volumeFunc :: (Double, Double, Double) -> Position

function from 3 parameters to space

loLimit :: Double

s_a

upLimit :: Double

s_b

loCurve :: Double -> Double

t_a(s)

upCurve :: Double -> Double

t_b(s)

loSurf :: Double -> Double -> Double

u_a(s,t)

upSurf :: Double -> Double -> Double

u_b(s,t)

unitBall :: Volume Source

A unit ball, centered at the origin.

unitBallCartesian :: Volume Source

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

centeredBall :: Double -> Volume Source

A ball with given radius, centered at the origin.

ball Source

Arguments

:: Double

radius

-> Position

center

-> Volume

ball with given radius and center

Ball with given radius and center.

northernHalfBall :: Volume Source

Upper half ball, unit radius, centered at origin.

centeredCylinder :: Double -> Double -> Volume Source

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.

shiftVolume :: Displacement -> Volume -> Volume Source

Shift a volume by a displacement.

Volume Integral

volumeIntegral Source

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.

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

StateSpace Double Source 
StateSpace Vec Source 
StateSpace Position Source

Position is not a vector, but displacement (difference in position) is a vector.

StateSpace St Source 
StateSpace p => StateSpace [p] Source 
(StateSpace p, StateSpace q, (~) * (Time p) (Time q)) => StateSpace (p, q) Source 
(StateSpace p, StateSpace q, StateSpace r, (~) * (Time p) (Time q), (~) * (Time q) (Time r)) => StateSpace (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.

postscript :: Attribute Source

An Attribute that requests postscript output.

psFile :: FilePath -> Attribute Source

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

arrow Source

Arguments

:: Point

location of base of arrow

-> Point

displacement vector

-> Picture 

An arrow

thickArrow Source

Arguments

:: Float

arrow thickness

-> Point

location of base of arrow

-> Point

displacement vector

-> Picture 

A think arrow

Vis library

v3FromVec :: Vec -> V3 Double Source

Make a V3 object from a Vec.

v3FromPos :: Position -> V3 Double Source

Make a V3 object from a Position.

visVec :: Color -> Vec -> VisObject Double Source

A VisObject arrow from a vector

oneVector :: Color -> Position -> Vec -> VisObject Double Source

Place a vector at a particular position.

displayVectorField Source

Arguments

:: Color

color for the vector field

-> Double

scale factor

-> [Position]

list of positions to show the field

-> VectorField

vector field to display

-> VisObject Double

the displayable object

Display a vector field.

curveObject :: Color -> Curve -> VisObject Double Source

A displayable VisObject for a curve.