{-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE TypeFamilies #-} module Linear.Vector where import Data.Vector.Fixed ( Dim, N1, N4, N5, S, Vector(..), fromList, toList, ) import qualified Data.Vector.Fixed as V ( (!), length, ) import Data.Vector.Fixed.Boxed type Vec1 = Vec N1 type Vec4 = Vec N4 type Vec5 = Vec N5 -- | Safe indexing. -- -- Examples: -- -- >>> import Data.Vector.Fixed (mk3) -- >>> let v1 = mk3 1 2 3 :: Vec3 Int -- >>> v1 !? 2 -- Just 3 -- >>> v1 !? 3 -- Nothing -- (!?) :: (Vector v a) => v a -> Int -> Maybe a (!?) v1 idx | idx < 0 || idx >= V.length v1 = Nothing | otherwise = Just $ v1 V.! idx -- | Remove an element of the given vector. -- -- Examples: -- -- >>> import Data.Vector.Fixed (mk3) -- >>> let b = mk3 1 2 3 :: Vec3 Int -- >>> delete b 1 :: Vec2 Int -- fromList [1,3] -- delete :: (Vector v a, Vector w a, Dim v ~ S (Dim w)) => v a -> Int -> w a delete v1 idx = fromList (lhalf ++ rhalf') where (lhalf, rhalf) = splitAt idx (toList v1) rhalf' = tail rhalf