learn-physics-0.4.2: Haskell code for learning physics

Stabilityexperimental
MaintainerScott N. Walck <walck@lvc.edu>
Safe HaskellTrustworthy

Physics.Learn

Contents

Description

Functions for learning physics.

Synopsis

Mechanics

type TheTime = DoubleSource

Time (in s).

type TimeStep = DoubleSource

A time step (in s).

type Velocity = VecSource

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 -> VecSource

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

simpleStateDerivSource

Arguments

:: SimpleAccelerationFunction

acceleration function for the particle

-> DifferentialEquation SimpleState

differential equation

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

simpleRungeKuttaStepSource

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 

Instances

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 -> VecSource

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

oneParticleStateDerivSource

Arguments

:: OneParticleAccelerationFunction

acceleration function for the particle

-> DifferentialEquation OneParticleSystemState

differential equation

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

oneParticleRungeKuttaStepSource

Arguments

:: OneParticleAccelerationFunction

acceleration function for the particle

-> TimeStep

time step

-> OneParticleSystemState

initial state

-> OneParticleSystemState

state after one time step

Single Runge-Kutta step

oneParticleRungeKuttaSolutionSource

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.

twoParticleStateDerivSource

Arguments

:: TwoParticleAccelerationFunction

acceleration function for two particles

-> DifferentialEquation TwoParticleSystemState

differential equation

Time derivative of state for two particles with constant mass.

twoParticleRungeKuttaStepSource

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.

manyParticleStateDerivSource

Arguments

:: ManyParticleAccelerationFunction

acceleration function for many particles

-> DifferentialEquation ManyParticleSystemState

differential equation

Time derivative of state for many particles with constant mass.

manyParticleRungeKuttaStepSource

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 = DoubleSource

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 -> ChargeSource

Total charge (in C) of a charge distribution.

Current

type Current = DoubleSource

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 -> VectorFieldSource

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 -> DoubleSource

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

Electric Potential

electricPotentialFromFieldSource

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 -> ScalarFieldSource

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

Magnetic Field

bField :: CurrentDistribution -> VectorFieldSource

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 -> DoubleSource

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

Geometry

Vectors

data Vec Source

A type for vectors.

xComp :: Vec -> DoubleSource

x component

yComp :: Vec -> DoubleSource

y component

zComp :: Vec -> DoubleSource

z component

vecSource

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

Vector multiplied by scalar

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

Vector divided by scalar

(<.>) :: InnerSpace v => v -> v -> Scalar v

Inner/dot product

(><) :: Vec -> Vec -> VecSource

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 :: VecSource

Unit vector in the x direction.

jHat :: VecSource

Unit vector in the y direction.

kHat :: VecSource

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 
StateSpace Position

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

type Displacement = VecSource

A displacement is a vector.

type ScalarField = Position -> DoubleSource

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

type VectorField = Position -> VecSource

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

type Field v = Position -> vSource

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

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

cartesian :: CoordinateSystemSource

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

cylindrical :: CoordinateSystemSource

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 :: CoordinateSystemSource

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.

cartSource

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.

cylSource

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.

sphSource

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.

displacementSource

Arguments

:: Position

source position

-> Position

target position

-> Displacement 

Displacement from source position to target position.

shiftPosition :: Displacement -> Position -> PositionSource

Shift a position by a displacement.

shiftObject :: Displacement -> (a -> Position) -> a -> PositionSource

An object is a map into Position.

shiftField :: Displacement -> (Position -> v) -> Position -> vSource

A field is a map from Position.

addFields :: AdditiveGroup v => [Field v] -> Field vSource

Add two scalar fields or two vector fields.

rHat :: VectorFieldSource

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 :: VectorFieldSource

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 :: VectorFieldSource

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 :: VectorFieldSource

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 :: VectorFieldSource

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 :: VectorFieldSource

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 :: VectorFieldSource

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 -> CurveSource

Reparametrize a curve from 0 to 1.

concatCurvesSource

Arguments

:: Curve

go first along this curve

-> Curve

then along this curve

-> Curve

to produce this new curve

Concatenate two curves.

concatenateCurves :: [Curve] -> CurveSource

Concatenate a list of curves. Parametrizes curves equally.

reverseCurve :: Curve -> CurveSource

Reverse a curve.

evalCurveSource

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.

shiftCurveSource

Arguments

:: Displacement

amount to shift

-> Curve

original curve

-> Curve

shifted curve

Shift a curve by a displacement.

straightLineSource

Arguments

:: Position

starting position

-> Position

ending position

-> Curve

straight-line curve

The straight-line curve from one position to another.

Line Integrals

simpleLineIntegralSource

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.

dottedLineIntegralSource

Arguments

:: Int

number of intervals

-> VectorField

vector field

-> Curve

curve to integrate over

-> Double

scalar result

A dotted line integral.

crossedLineIntegralSource

Arguments

:: Int

number of intervals

-> VectorField

vector field

-> Curve

curve to integrate over

-> Vec

vector result

Calculates integral vf x dl over curve.

compositeSimpsonDottedLineIntegralSource

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

Quadratic approximation to vector field. Quadratic approximation to curve. Composite strategy. Dotted line integral.

compositeSimpsonCrossedLineIntegralSource

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

Quadratic approximation to vector field. Quadratic approximation to curve. Composite strategy. Crossed line integral.

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 :: SurfaceSource

A unit sphere, centered at the origin.

centeredSphere :: Double -> SurfaceSource

A sphere with given radius centered at the origin.

sphere :: Double -> Position -> SurfaceSource

Sphere with given radius and center.

northernHemisphere :: SurfaceSource

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

disk :: Double -> SurfaceSource

A disk with given radius, centered at the origin.

shiftSurface :: Displacement -> Surface -> SurfaceSource

Shift a surface by a displacement.

Surface Integrals

surfaceIntegralSource

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.

dottedSurfaceIntegralSource

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 :: VolumeSource

A unit ball, centered at the origin.

unitBallCartesian :: VolumeSource

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

centeredBall :: Double -> VolumeSource

A ball with given radius, centered at the origin.

ballSource

Arguments

:: Double

radius

-> Position

center

-> Volume

ball with given radius and center

Ball with given radius and center.

northernHalfBall :: VolumeSource

Upper half ball, unit radius, centered at origin.

centeredCylinder :: Double -> Double -> VolumeSource

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 -> VolumeSource

Shift a volume by a displacement.

Volume Integral

volumeIntegralSource

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 whereSource

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 pSource

Subtract points

(.+^) :: p -> Diff p -> pSource

Point plus vector

Instances

StateSpace Double 
StateSpace Vec 
StateSpace Position

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

StateSpace St 
StateSpace p => StateSpace [p] 
(StateSpace p, StateSpace q, ~ * (Time p) (Time q)) => StateSpace (p, q) 
(StateSpace p, StateSpace q, StateSpace r, ~ * (Time p) (Time q), ~ * (Time q) (Time r)) => StateSpace (p, q, r) 

(.-^) :: StateSpace p => p -> Diff p -> pSource

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 stateSource

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 stateSource

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 stateSource

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 stateSource

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 -> pSource

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

An Attribute with a given label at a given position.

postscript :: AttributeSource

An Attribute that requests postscript output.

psFile :: FilePath -> AttributeSource

An Attribute giving the postscript file name.

Vis library

xyzFromVec :: Vec -> Xyz DoubleSource

Make an Xyz object from a Vec.

xyzFromPos :: Position -> Xyz DoubleSource

Make an Xyz object from a Position.

visVec :: Color -> Vec -> VisObject DoubleSource

A VisObject arrow from a vector

oneVector :: Color -> Position -> Vec -> VisObject DoubleSource

Place a vector at a particular position.

displayVectorFieldSource

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 DoubleSource

A displayable VisObject for a curve.