{-# 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
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Position] -> ShowS
$cshowList :: [Position] -> ShowS
show :: Position -> String
$cshow :: Position -> String
showsPrec :: Int -> Position -> ShowS
$cshowsPrec :: Int -> 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 = 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 forall a. Num a => a -> a -> a
* forall a. Floating a => a -> a
cos Double
phi) (Double
s forall a. Num a => a -> a -> a
* 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 forall a. Num a => a -> a -> a
* forall a. Floating a => a -> a
sin Double
th forall a. Num a => a -> a -> a
* forall a. Floating a => a -> a
cos Double
phi) (Double
r forall a. Num a => a -> a -> a
* forall a. Floating a => a -> a
sin Double
th forall a. Num a => a -> a -> a
* forall a. Floating a => a -> a
sin Double
phi) (Double
r forall a. Num a => a -> a -> a
* 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 forall a. Num a => a -> a -> a
* forall a. Floating a => a -> a
cos Double
phi) (Double
s forall a. Num a => a -> a -> a
* 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 forall a. Num a => a -> a -> a
* forall a. Floating a => a -> a
sin Double
theta forall a. Num a => a -> a -> a
* forall a. Floating a => a -> a
cos Double
phi) (Double
r forall a. Num a => a -> a -> a
* forall a. Floating a => a -> a
sin Double
theta forall a. Num a => a -> a -> a
* forall a. Floating a => a -> a
sin Double
phi) (Double
r forall a. Num a => a -> a -> a
* 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 = forall a. Floating a => a -> a
sqrt(Double
xforall a. Floating a => a -> a -> a
**Double
2 forall a. Num a => a -> a -> a
+ Double
yforall a. Floating a => a -> a -> a
**Double
2)
phi :: Double
phi = 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 = forall a. Floating a => a -> a
sqrt(Double
xforall a. Floating a => a -> a -> a
**Double
2 forall a. Num a => a -> a -> a
+ Double
yforall a. Floating a => a -> a -> a
**Double
2 forall a. Num a => a -> a -> a
+ Double
zforall a. Floating a => a -> a -> a
**Double
2)
theta :: Double
theta = forall a. RealFloat a => a -> a -> a
atan2 Double
s Double
z
s :: Double
s = forall a. Floating a => a -> a
sqrt(Double
xforall a. Floating a => a -> a -> a
**Double
2 forall a. Num a => a -> a -> a
+ Double
yforall a. Floating a => a -> a -> a
**Double
2)
phi :: Double
phi = 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
xforall a. Num a => a -> a -> a
-Double
x') (Double
yforall a. Num a => a -> a -> a
-Double
y') (Double
zforall 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 forall a. Num a => a -> a -> a
+ Vec -> Double
xComp Vec
v) (Double
y forall a. Num a => a -> a -> a
+ Vec -> Double
yComp Vec
v) (Double
z 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 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 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 forall v s.
(VectorSpace v, s ~ Scalar v, Fractional s) =>
v -> s -> v
^/ 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 (forall a. Floating a => a -> a
cos Double
theta forall a. Num a => a -> a -> a
* forall a. Floating a => a -> a
cos Double
phi) (forall a. Floating a => a -> a
cos Double
theta forall a. Num a => a -> a -> a
* forall a. Floating a => a -> a
sin Double
phi) (-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 (-forall a. Floating a => a -> a
sin Double
phi) (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 (forall a. Floating a => a -> a
cos Double
phi) (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 = forall a b. a -> b -> a
const Vec
iHat
yHat :: VectorField
yHat :: Position -> Vec
yHat = forall a b. a -> b -> a
const Vec
jHat
zHat :: VectorField
zHat :: Position -> Vec
zHat = forall a b. a -> b -> a
const Vec
kHat