module Numeric.Vector
(
Vector
, Vec2f, Vec3f, Vec4f, Vec2d, Vec3d, Vec4d
, (.*.), dot, (·)
, normL1, normL2, normLPInf, normLNInf, normLP
, normalized
, vec2, vec3, vec4
, det2, cross, (×)
, unpackV2, unpackV3, unpackV4
) where
import Numeric.Array.ElementWise
import Numeric.DataFrame.Type
import Numeric.Dimensions
import Numeric.Scalar
type Vector t (n :: Nat) = DataFrame t '[n]
type Vec2f = Vector Float 2
type Vec3f = Vector Float 3
type Vec4f = Vector Float 4
type Vec2d = Vector Double 2
type Vec3d = Vector Double 3
type Vec4d = Vector Double 4
(.*.) :: ( Num t
, Num (Vector t n)
, ElementWise (Idx '[n]) t (Vector t n)
)
=> Vector t n -> Vector t n -> Vector t n
(.*.) a b = broadcast . ewfoldl (const (+)) 0 $ a * b
infixl 7 .*.
dot :: ( Num t
, Num (Vector t n)
, ElementWise (Idx '[n]) t (Vector t n)
)
=> Vector t n -> Vector t n -> Scalar t
dot a b = scalar . ewfoldl (const (+)) 0 $ a * b
infixl 7 ·
(·) :: ( Num t
, Num (Vector t n)
, ElementWise (Idx '[n]) t (Vector t n)
)
=> Vector t n -> Vector t n -> Scalar t
(·) = dot
normL1 :: ( Num t
, ElementWise (Idx '[n]) t (Vector t n)
)
=> Vector t n -> Scalar t
normL1 = scalar . ewfoldr (const (\a -> (abs a +))) 0
normL2 :: ( Floating t
, ElementWise (Idx '[n]) t (Vector t n)
)
=> Vector t n -> Scalar t
normL2 = scalar . sqrt . ewfoldr (const (\a -> (a*a +))) 0
normalized :: ( Floating t
, Fractional (Vector t n)
, ElementWise (Idx '[n]) t (Vector t n)
)
=> Vector t n -> Vector t n
normalized v = v / n
where
n = broadcast . sqrt $ ewfoldr (const (\a -> (a*a +))) 0 v
normLPInf :: ( Ord t, Num t
, ElementWise (Idx '[n]) t (Vector t n)
)
=> Vector t n -> Scalar t
normLPInf = scalar . ewfoldr (const (max . abs)) 0
normLNInf :: ( Ord t, Num t
, ElementWise (Idx '[n]) t (Vector t n)
)
=> Vector t n -> Scalar t
normLNInf x = scalar $ ewfoldr (const (min . abs))
(abs $ x ! (1 :! Z)) x
normLP :: ( Floating t
, ElementWise (Idx '[n]) t (Vector t n)
)
=> Int -> Vector t n -> Scalar t
normLP i' = scalar . (**ri) . ewfoldr (const (\a -> (a**i +))) 0
where
i = fromIntegral i'
ri = recip i
vec2 :: ElementWise (Idx '[2]) t (Vector t 2) => t -> t -> Vector t 2
vec2 a b = ewgen f
where
f (1 :! Z) = a
f _ = b
det2 :: ( ElementWise (Idx '[2]) t (Vector t 2)
, Num t
) => Vector t 2 -> Vector t 2 -> Scalar t
det2 a b = scalar $ a ! (1 :! Z) * b ! (2 :! Z)
a ! (2 :! Z) * b ! (1 :! Z)
vec3 :: ElementWise (Idx '[3]) t (Vector t 3) => t -> t -> t -> Vector t 3
vec3 a b c = ewgen f
where
f (1 :! Z) = a
f (2 :! Z) = b
f _ = c
cross :: ( ElementWise (Idx '[3]) t (Vector t 3)
, Num t
) => Vector t 3 -> Vector t 3 -> Vector t 3
cross a b = vec3 ( a ! (2 :! Z) * b ! (3 :! Z)
a ! (3 :! Z) * b ! (2 :! Z) )
( a ! (3 :! Z) * b ! (1 :! Z)
a ! (1 :! Z) * b ! (3 :! Z) )
( a ! (1 :! Z) * b ! (2 :! Z)
a ! (2 :! Z) * b ! (1 :! Z) )
infixl 7 ×
(×) :: ( ElementWise (Idx '[3]) t (Vector t 3)
, Num t
) => Vector t 3 -> Vector t 3 -> Vector t 3
(×) = cross
vec4 :: ElementWise (Idx '[4]) t (Vector t 4)
=> t -> t -> t -> t -> Vector t 4
vec4 a b c d = ewgen f
where
f (1 :! Z) = a
f (2 :! Z) = b
f (3 :! Z) = c
f _ = d
unpackV2 :: ElementWise (Idx '[2]) t (Vector t 2)
=> Vector t 2 -> (t, t)
unpackV2 v = (v ! 1, v ! 2)
unpackV3 :: ElementWise (Idx '[3]) t (Vector t 3)
=> Vector t 3 -> (t, t, t)
unpackV3 v = (v ! 1, v ! 2, v ! 3)
unpackV4 :: ElementWise (Idx '[4]) t (Vector t 4)
=> Vector t 4 -> (t, t, t, t)
unpackV4 v = (v ! 1, v ! 2, v ! 3, v ! 4)