{-# OPTIONS_GHC -Wall #-}
{-# LANGUAGE Safe #-}
module Physics.Learn.CommonVec
( Vec(..)
, R
, vec
, (><)
, iHat
, jHat
, kHat
)
where
infixl 7 ><
type R = Double
data Vec = Vec { Vec -> R
xComp :: R
, Vec -> R
yComp :: R
, Vec -> R
zComp :: R
} deriving (Vec -> Vec -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Vec -> Vec -> Bool
$c/= :: Vec -> Vec -> Bool
== :: Vec -> Vec -> Bool
$c== :: Vec -> Vec -> Bool
Eq)
instance Show Vec where
show :: Vec -> String
show (Vec R
x R
y R
z) = String
"vec " forall a. [a] -> [a] -> [a]
++ R -> String
showDouble R
x forall a. [a] -> [a] -> [a]
++ String
" "
forall a. [a] -> [a] -> [a]
++ R -> String
showDouble R
y forall a. [a] -> [a] -> [a]
++ String
" "
forall a. [a] -> [a] -> [a]
++ R -> String
showDouble R
z
showDouble :: Double -> String
showDouble :: R -> String
showDouble R
x
| R
x forall a. Ord a => a -> a -> Bool
< R
0 = String
"(" forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show R
x forall a. [a] -> [a] -> [a]
++ String
")"
| Bool
otherwise = forall a. Show a => a -> String
show R
x
vec :: R
-> R
-> R
-> Vec
vec :: R -> R -> R -> Vec
vec = R -> R -> R -> Vec
Vec
(><) :: Vec -> Vec -> Vec
Vec R
ax R
ay R
az >< :: Vec -> Vec -> Vec
>< Vec R
bx R
by R
bz = R -> R -> R -> Vec
Vec (R
ayforall a. Num a => a -> a -> a
*R
bz forall a. Num a => a -> a -> a
- R
azforall a. Num a => a -> a -> a
*R
by) (R
azforall a. Num a => a -> a -> a
*R
bx forall a. Num a => a -> a -> a
- R
axforall a. Num a => a -> a -> a
*R
bz) (R
axforall a. Num a => a -> a -> a
*R
by forall a. Num a => a -> a -> a
- R
ayforall a. Num a => a -> a -> a
*R
bx)
iHat, jHat, kHat :: Vec
iHat :: Vec
iHat = R -> R -> R -> Vec
vec R
1 R
0 R
0
jHat :: Vec
jHat = R -> R -> R -> Vec
vec R
0 R
1 R
0
kHat :: Vec
kHat = R -> R -> R -> Vec
vec R
0 R
0 R
1