module Data.Vector.Class where
type Scalar = Double
class BasicVector v where
vmap :: (Scalar -> Scalar) -> (v -> v)
vzip :: (Scalar -> Scalar -> Scalar) -> (v -> v -> v)
vfold :: (Scalar -> Scalar -> Scalar) -> (v -> Scalar)
vpack :: [Scalar] -> Maybe v
vunpack :: v -> [Scalar]
vpromote :: Scalar -> v
class (BasicVector v, Num v, Fractional v) => Vector v where
(*|) :: Vector v => Scalar -> v -> v
Scalar
k *| :: forall v. Vector v => Scalar -> v -> v
*| v
v = (Scalar -> Scalar) -> v -> v
forall v. BasicVector v => (Scalar -> Scalar) -> v -> v
vmap (Scalar
kScalar -> Scalar -> Scalar
forall a. Num a => a -> a -> a
*) v
v
(|*) :: Vector v => v -> Scalar -> v
v
v |* :: forall v. Vector v => v -> Scalar -> v
|* Scalar
k = (Scalar -> Scalar) -> v -> v
forall v. BasicVector v => (Scalar -> Scalar) -> v -> v
vmap (Scalar
kScalar -> Scalar -> Scalar
forall a. Num a => a -> a -> a
*) v
v
(|/) :: Vector v => v -> Scalar -> v
v
v |/ :: forall v. Vector v => v -> Scalar -> v
|/ Scalar
k = v
v v -> Scalar -> v
forall v. Vector v => v -> Scalar -> v
|* (Scalar
1Scalar -> Scalar -> Scalar
forall a. Fractional a => a -> a -> a
/Scalar
k)
(/|) :: Vector v => Scalar -> v -> v
Scalar
k /| :: forall v. Vector v => Scalar -> v -> v
/| v
v = (Scalar
1Scalar -> Scalar -> Scalar
forall a. Fractional a => a -> a -> a
/Scalar
k) Scalar -> v -> v
forall v. Vector v => Scalar -> v -> v
*| v
v
infixl 7 *|
infixl 7 |*
infixl 7 /|
infixl 7 |/
vdot :: Vector v => v -> v -> Scalar
v
v1 vdot :: forall v. Vector v => v -> v -> Scalar
`vdot` v
v2 = (Scalar -> Scalar -> Scalar) -> v -> Scalar
forall v.
BasicVector v =>
(Scalar -> Scalar -> Scalar) -> v -> Scalar
vfold Scalar -> Scalar -> Scalar
forall a. Num a => a -> a -> a
(+) (v -> Scalar) -> v -> Scalar
forall a b. (a -> b) -> a -> b
$ (Scalar -> Scalar -> Scalar) -> v -> v -> v
forall v.
BasicVector v =>
(Scalar -> Scalar -> Scalar) -> v -> v -> v
vzip Scalar -> Scalar -> Scalar
forall a. Num a => a -> a -> a
(*) v
v1 v
v2
vmag :: Vector v => v -> Scalar
vmag :: forall v. Vector v => v -> Scalar
vmag v
v = Scalar -> Scalar
forall a. Floating a => a -> a
sqrt (v
v v -> v -> Scalar
forall v. Vector v => v -> v -> Scalar
`vdot` v
v)
vnormalise :: Vector v => v -> v
vnormalise :: forall v. Vector v => v -> v
vnormalise v
v =
let m :: Scalar
m = v -> Scalar
forall v. Vector v => v -> Scalar
vmag v
v
in if Scalar
m Scalar -> Scalar -> Bool
forall a. Ord a => a -> a -> Bool
< Scalar
1e-16 then v
v else v
v v -> Scalar -> v
forall v. Vector v => v -> Scalar -> v
|* (Scalar
1Scalar -> Scalar -> Scalar
forall a. Fractional a => a -> a -> a
/Scalar
m)
vlinear :: (Vector v) => Scalar -> v -> v -> v
vlinear :: forall v. Vector v => Scalar -> v -> v -> v
vlinear Scalar
t v
a v
b = (Scalar
1Scalar -> Scalar -> Scalar
forall a. Num a => a -> a -> a
-Scalar
t) Scalar -> v -> v
forall v. Vector v => Scalar -> v -> v
*| v
a v -> v -> v
forall a. Num a => a -> a -> a
+ Scalar
t Scalar -> v -> v
forall v. Vector v => Scalar -> v -> v
*| v
b