{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# OPTIONS_GHC -fno-warn-warnings-deprecations #-}
module Data.Vector3
( Vector3
, vector3
, vector3X
, vector3Y
, vector3Z
, vector3XYZ
, vector3Spherical
, vector3Rho
, vector3Theta
, vector3Phi
, vector3RhoThetaPhi
, vector3Rotate
)
where
import Control.DeepSeq (NFData(..))
import Data.VectorSpace
data Vector3 a = RealFloat a => Vector3 !a !a !a
deriving instance Eq a => Eq (Vector3 a)
deriving instance Show a => Show (Vector3 a)
instance NFData a => NFData (Vector3 a) where
rnf :: Vector3 a -> ()
rnf (Vector3 a
x a
y a
z) = forall a. NFData a => a -> ()
rnf a
x seq :: forall a b. a -> b -> b
`seq` forall a. NFData a => a -> ()
rnf a
y seq :: forall a b. a -> b -> b
`seq` forall a. NFData a => a -> ()
rnf a
z seq :: forall a b. a -> b -> b
`seq` ()
vector3 :: RealFloat a => a -> a -> a -> Vector3 a
vector3 :: forall a. RealFloat a => a -> a -> a -> Vector3 a
vector3 = forall a. RealFloat a => a -> a -> a -> Vector3 a
Vector3
vector3X :: RealFloat a => Vector3 a -> a
vector3X :: forall a. RealFloat a => Vector3 a -> a
vector3X (Vector3 a
x a
_ a
_) = a
x
vector3Y :: RealFloat a => Vector3 a -> a
vector3Y :: forall a. RealFloat a => Vector3 a -> a
vector3Y (Vector3 a
_ a
y a
_) = a
y
vector3Z :: RealFloat a => Vector3 a -> a
vector3Z :: forall a. RealFloat a => Vector3 a -> a
vector3Z (Vector3 a
_ a
_ a
z) = a
z
vector3XYZ :: RealFloat a => Vector3 a -> (a, a, a)
vector3XYZ :: forall a. RealFloat a => Vector3 a -> (a, a, a)
vector3XYZ (Vector3 a
x a
y a
z) = (a
x, a
y, a
z)
vector3Spherical :: RealFloat a => a -> a -> a -> Vector3 a
vector3Spherical :: forall a. RealFloat a => a -> a -> a -> Vector3 a
vector3Spherical a
rho a
theta a
phi =
forall a. RealFloat a => a -> a -> a -> Vector3 a
Vector3 (a
rhoSinPhi forall a. Num a => a -> a -> a
* forall a. Floating a => a -> a
cos a
theta) (a
rhoSinPhi forall a. Num a => a -> a -> a
* forall a. Floating a => a -> a
sin a
theta) (a
rho forall a. Num a => a -> a -> a
* forall a. Floating a => a -> a
cos a
phi)
where
rhoSinPhi :: a
rhoSinPhi = a
rho forall a. Num a => a -> a -> a
* forall a. Floating a => a -> a
sin a
phi
vector3Rho :: RealFloat a => Vector3 a -> a
vector3Rho :: forall a. RealFloat a => Vector3 a -> a
vector3Rho (Vector3 a
x a
y a
z) = forall a. Floating a => a -> a
sqrt (a
x forall a. Num a => a -> a -> a
* a
x forall a. Num a => a -> a -> a
+ a
y forall a. Num a => a -> a -> a
* a
y forall a. Num a => a -> a -> a
+ a
z forall a. Num a => a -> a -> a
* a
z)
vector3Theta :: RealFloat a => Vector3 a -> a
vector3Theta :: forall a. RealFloat a => Vector3 a -> a
vector3Theta (Vector3 a
x a
y a
_) = forall a. RealFloat a => a -> a -> a
atan2 a
y a
x
vector3Phi :: RealFloat a => Vector3 a -> a
vector3Phi :: forall a. RealFloat a => Vector3 a -> a
vector3Phi v :: Vector3 a
v@(Vector3 a
_ a
_ a
z) = forall a. Floating a => a -> a
acos (a
z forall a. Fractional a => a -> a -> a
/ forall a. RealFloat a => Vector3 a -> a
vector3Rho Vector3 a
v)
vector3RhoThetaPhi :: RealFloat a => Vector3 a -> (a, a, a)
vector3RhoThetaPhi :: forall a. RealFloat a => Vector3 a -> (a, a, a)
vector3RhoThetaPhi (Vector3 a
x a
y a
z) = (a
rho, a
theta, a
phi)
where
rho :: a
rho = forall a. Floating a => a -> a
sqrt (a
x forall a. Num a => a -> a -> a
* a
x forall a. Num a => a -> a -> a
+ a
y forall a. Num a => a -> a -> a
* a
y forall a. Num a => a -> a -> a
+ a
z forall a. Num a => a -> a -> a
* a
z)
theta :: a
theta = forall a. RealFloat a => a -> a -> a
atan2 a
y a
x
phi :: a
phi = forall a. Floating a => a -> a
acos (a
z forall a. Fractional a => a -> a -> a
/ a
rho)
instance RealFloat a => VectorSpace (Vector3 a) a where
zeroVector :: Vector3 a
zeroVector = forall a. RealFloat a => a -> a -> a -> Vector3 a
Vector3 a
0 a
0 a
0
a
a *^ :: a -> Vector3 a -> Vector3 a
*^ (Vector3 a
x a
y a
z) = forall a. RealFloat a => a -> a -> a -> Vector3 a
Vector3 (a
a forall a. Num a => a -> a -> a
* a
x) (a
a forall a. Num a => a -> a -> a
* a
y) (a
a forall a. Num a => a -> a -> a
* a
z)
(Vector3 a
x a
y a
z) ^/ :: Vector3 a -> a -> Vector3 a
^/ a
a = forall a. RealFloat a => a -> a -> a -> Vector3 a
Vector3 (a
x forall a. Fractional a => a -> a -> a
/ a
a) (a
y forall a. Fractional a => a -> a -> a
/ a
a) (a
z forall a. Fractional a => a -> a -> a
/ a
a)
negateVector :: Vector3 a -> Vector3 a
negateVector (Vector3 a
x a
y a
z) = forall a. RealFloat a => a -> a -> a -> Vector3 a
Vector3 (-a
x) (-a
y) (-a
z)
(Vector3 a
x1 a
y1 a
z1) ^+^ :: Vector3 a -> Vector3 a -> Vector3 a
^+^ (Vector3 a
x2 a
y2 a
z2) =
forall a. RealFloat a => a -> a -> a -> Vector3 a
Vector3 (a
x1 forall a. Num a => a -> a -> a
+ a
x2) (a
y1 forall a. Num a => a -> a -> a
+ a
y2) (a
z1 forall a. Num a => a -> a -> a
+ a
z2)
(Vector3 a
x1 a
y1 a
z1) ^-^ :: Vector3 a -> Vector3 a -> Vector3 a
^-^ (Vector3 a
x2 a
y2 a
z2) =
forall a. RealFloat a => a -> a -> a -> Vector3 a
Vector3 (a
x1 forall a. Num a => a -> a -> a
- a
x2) (a
y1 forall a. Num a => a -> a -> a
- a
y2) (a
z1 forall a. Num a => a -> a -> a
- a
z2)
(Vector3 a
x1 a
y1 a
z1) dot :: Vector3 a -> Vector3 a -> a
`dot` (Vector3 a
x2 a
y2 a
z2) = a
x1 forall a. Num a => a -> a -> a
* a
x2 forall a. Num a => a -> a -> a
+ a
y1 forall a. Num a => a -> a -> a
* a
y2 forall a. Num a => a -> a -> a
+ a
z1 forall a. Num a => a -> a -> a
* a
z2
vector3Rotate :: RealFloat a => a -> a -> Vector3 a -> Vector3 a
vector3Rotate :: forall a. RealFloat a => a -> a -> Vector3 a -> Vector3 a
vector3Rotate a
theta' a
phi' Vector3 a
v =
forall a. RealFloat a => a -> a -> a -> Vector3 a
vector3Spherical (forall a. RealFloat a => Vector3 a -> a
vector3Rho Vector3 a
v)
(forall a. RealFloat a => Vector3 a -> a
vector3Theta Vector3 a
v forall a. Num a => a -> a -> a
+ a
theta')
(forall a. RealFloat a => Vector3 a -> a
vector3Phi Vector3 a
v forall a. Num a => a -> a -> a
+ a
phi')