{-# OPTIONS_GHC -Wall #-}
{-# LANGUAGE TypeSynonymInstances, FlexibleInstances #-}
{-# LANGUAGE Safe #-}
module Physics.Learn.Position
( Position
, Displacement
, ScalarField
, VectorField
, Field
, CoordinateSystem
, cartesian
, cylindrical
, spherical
, cart
, cyl
, sph
, cartesianCoordinates
, cylindricalCoordinates
, sphericalCoordinates
, displacement
, shiftPosition
, shiftObject
, shiftField
, addFields
, rHat
, thetaHat
, phiHat
, sHat
, xHat
, yHat
, zHat
)
where
import Data.VectorSpace
( AdditiveGroup
)
import Physics.Learn.CarrotVec
( Vec
, vec
, xComp
, yComp
, zComp
, iHat
, jHat
, kHat
, sumV
, magnitude
, (^/)
)
data Position = Cart Double Double Double
deriving (Int -> Position -> ShowS
[Position] -> ShowS
Position -> String
(Int -> Position -> ShowS)
-> (Position -> String) -> ([Position] -> ShowS) -> Show Position
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Position -> ShowS
showsPrec :: Int -> Position -> ShowS
$cshow :: Position -> String
show :: Position -> String
$cshowList :: [Position] -> ShowS
showList :: [Position] -> ShowS
Show)
type Displacement = Vec
type ScalarField = Position -> Double
type VectorField = Position -> Vec
type Field v = Position -> v
type CoordinateSystem = (Double,Double,Double) -> Position
addFields :: AdditiveGroup v => [Field v] -> Field v
addFields :: forall v. AdditiveGroup v => [Field v] -> Field v
addFields [Field v]
flds Position
r = [v] -> v
forall (f :: * -> *) v. (Foldable f, AdditiveGroup v) => f v -> v
sumV [Field v
fld Position
r | Field v
fld <- [Field v]
flds]
cartesian :: CoordinateSystem
cartesian :: CoordinateSystem
cartesian (Double
x,Double
y,Double
z) = Double -> Double -> Double -> Position
Cart Double
x Double
y Double
z
cylindrical :: CoordinateSystem
cylindrical :: CoordinateSystem
cylindrical (Double
s,Double
phi,Double
z) = Double -> Double -> Double -> Position
Cart (Double
s Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double -> Double
forall a. Floating a => a -> a
cos Double
phi) (Double
s Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double -> Double
forall a. Floating a => a -> a
sin Double
phi) Double
z
spherical :: CoordinateSystem
spherical :: CoordinateSystem
spherical (Double
r,Double
th,Double
phi) = Double -> Double -> Double -> Position
Cart (Double
r Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double -> Double
forall a. Floating a => a -> a
sin Double
th Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double -> Double
forall a. Floating a => a -> a
cos Double
phi) (Double
r Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double -> Double
forall a. Floating a => a -> a
sin Double
th Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double -> Double
forall a. Floating a => a -> a
sin Double
phi) (Double
r Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double -> Double
forall a. Floating a => a -> a
cos Double
th)
cart :: Double
-> Double
-> Double
-> Position
cart :: Double -> Double -> Double -> Position
cart = Double -> Double -> Double -> Position
Cart
cyl :: Double
-> Double
-> Double
-> Position
cyl :: Double -> Double -> Double -> Position
cyl Double
s Double
phi Double
z = Double -> Double -> Double -> Position
Cart (Double
s Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double -> Double
forall a. Floating a => a -> a
cos Double
phi) (Double
s Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double -> Double
forall a. Floating a => a -> a
sin Double
phi) Double
z
sph :: Double
-> Double
-> Double
-> Position
sph :: Double -> Double -> Double -> Position
sph Double
r Double
theta Double
phi = Double -> Double -> Double -> Position
Cart (Double
r Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double -> Double
forall a. Floating a => a -> a
sin Double
theta Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double -> Double
forall a. Floating a => a -> a
cos Double
phi) (Double
r Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double -> Double
forall a. Floating a => a -> a
sin Double
theta Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double -> Double
forall a. Floating a => a -> a
sin Double
phi) (Double
r Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double -> Double
forall a. Floating a => a -> a
cos Double
theta)
cartesianCoordinates :: Position -> (Double,Double,Double)
cartesianCoordinates :: Position -> (Double, Double, Double)
cartesianCoordinates (Cart Double
x Double
y Double
z) = (Double
x,Double
y,Double
z)
cylindricalCoordinates :: Position -> (Double,Double,Double)
cylindricalCoordinates :: Position -> (Double, Double, Double)
cylindricalCoordinates (Cart Double
x Double
y Double
z) = (Double
s,Double
phi,Double
z)
where
s :: Double
s = Double -> Double
forall a. Floating a => a -> a
sqrt(Double
xDouble -> Double -> Double
forall a. Floating a => a -> a -> a
**Double
2 Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
yDouble -> Double -> Double
forall a. Floating a => a -> a -> a
**Double
2)
phi :: Double
phi = Double -> Double -> Double
forall a. RealFloat a => a -> a -> a
atan2 Double
y Double
x
sphericalCoordinates :: Position -> (Double,Double,Double)
sphericalCoordinates :: Position -> (Double, Double, Double)
sphericalCoordinates (Cart Double
x Double
y Double
z) = (Double
r,Double
theta,Double
phi)
where
r :: Double
r = Double -> Double
forall a. Floating a => a -> a
sqrt(Double
xDouble -> Double -> Double
forall a. Floating a => a -> a -> a
**Double
2 Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
yDouble -> Double -> Double
forall a. Floating a => a -> a -> a
**Double
2 Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
zDouble -> Double -> Double
forall a. Floating a => a -> a -> a
**Double
2)
theta :: Double
theta = Double -> Double -> Double
forall a. RealFloat a => a -> a -> a
atan2 Double
s Double
z
s :: Double
s = Double -> Double
forall a. Floating a => a -> a
sqrt(Double
xDouble -> Double -> Double
forall a. Floating a => a -> a -> a
**Double
2 Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
yDouble -> Double -> Double
forall a. Floating a => a -> a -> a
**Double
2)
phi :: Double
phi = Double -> Double -> Double
forall a. RealFloat a => a -> a -> a
atan2 Double
y Double
x
displacement :: Position
-> Position
-> Displacement
displacement :: Position -> Position -> Vec
displacement (Cart Double
x' Double
y' Double
z') (Cart Double
x Double
y Double
z) = Double -> Double -> Double -> Vec
vec (Double
xDouble -> Double -> Double
forall a. Num a => a -> a -> a
-Double
x') (Double
yDouble -> Double -> Double
forall a. Num a => a -> a -> a
-Double
y') (Double
zDouble -> Double -> Double
forall a. Num a => a -> a -> a
-Double
z')
shiftPosition :: Displacement -> Position -> Position
shiftPosition :: Vec -> Position -> Position
shiftPosition Vec
v (Cart Double
x Double
y Double
z) = Double -> Double -> Double -> Position
Cart (Double
x Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Vec -> Double
xComp Vec
v) (Double
y Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Vec -> Double
yComp Vec
v) (Double
z Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Vec -> Double
zComp Vec
v)
shiftObject :: Displacement -> (a -> Position) -> (a -> Position)
shiftObject :: forall a. Vec -> (a -> Position) -> a -> Position
shiftObject Vec
d a -> Position
f = Vec -> Position -> Position
shiftPosition Vec
d (Position -> Position) -> (a -> Position) -> a -> Position
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Position
f
shiftField :: Displacement -> (Position -> v) -> (Position -> v)
shiftField :: forall v. Vec -> (Position -> v) -> Position -> v
shiftField Vec
d Position -> v
f = Position -> v
f (Position -> v) -> (Position -> Position) -> Position -> v
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vec -> Position -> Position
shiftPosition Vec
d
rHat :: VectorField
rHat :: Position -> Vec
rHat Position
rv = Vec
d Vec -> Double -> Vec
forall v s.
(VectorSpace v, s ~ Scalar v, Fractional s) =>
v -> s -> v
^/ Vec -> Double
forall v s. (InnerSpace v, s ~ Scalar v, Floating s) => v -> s
magnitude Vec
d
where
d :: Vec
d = Position -> Position -> Vec
displacement (Double -> Double -> Double -> Position
cart Double
0 Double
0 Double
0) Position
rv
thetaHat :: VectorField
thetaHat :: Position -> Vec
thetaHat Position
r = Double -> Double -> Double -> Vec
vec (Double -> Double
forall a. Floating a => a -> a
cos Double
theta Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double -> Double
forall a. Floating a => a -> a
cos Double
phi) (Double -> Double
forall a. Floating a => a -> a
cos Double
theta Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double -> Double
forall a. Floating a => a -> a
sin Double
phi) (-Double -> Double
forall a. Floating a => a -> a
sin Double
theta)
where
(Double
_,Double
theta,Double
phi) = Position -> (Double, Double, Double)
sphericalCoordinates Position
r
phiHat :: VectorField
phiHat :: Position -> Vec
phiHat Position
r = Double -> Double -> Double -> Vec
vec (-Double -> Double
forall a. Floating a => a -> a
sin Double
phi) (Double -> Double
forall a. Floating a => a -> a
cos Double
phi) Double
0
where
(Double
_,Double
phi,Double
_) = Position -> (Double, Double, Double)
cylindricalCoordinates Position
r
sHat :: VectorField
sHat :: Position -> Vec
sHat Position
r = Double -> Double -> Double -> Vec
vec (Double -> Double
forall a. Floating a => a -> a
cos Double
phi) (Double -> Double
forall a. Floating a => a -> a
sin Double
phi) Double
0
where
(Double
_,Double
phi,Double
_) = Position -> (Double, Double, Double)
cylindricalCoordinates Position
r
xHat :: VectorField
xHat :: Position -> Vec
xHat = Vec -> Position -> Vec
forall a b. a -> b -> a
const Vec
iHat
yHat :: VectorField
yHat :: Position -> Vec
yHat = Vec -> Position -> Vec
forall a b. a -> b -> a
const Vec
jHat
zHat :: VectorField
zHat :: Position -> Vec
zHat = Vec -> Position -> Vec
forall a b. a -> b -> a
const Vec
kHat