module Feldspar.Vector where
import qualified Prelude
import Control.Arrow ((&&&))
import qualified Data.List
import Feldspar.Prelude
import Feldspar.Range
import Feldspar.Core.Expr
import Feldspar.Core
type Ix = Int
data Vector a = Indexed
{ length :: Data Length
, index :: Data Ix -> a
}
type DVector a = Vector (Data a)
freezeVector :: Storable a => Vector (Data a) -> Data [a]
freezeVector (Indexed l ixf) = parallel l ixf
unfreezeVector :: Storable a => Data Length -> Data [a] -> Vector (Data a)
unfreezeVector l arr = Indexed l (getIx arr)
memorize :: Storable a => Vector (Data a) -> Vector (Data a)
memorize vec = unfreezeVector (length vec) $ freezeVector vec
indexed :: Data Length -> (Data Ix -> a) -> Vector a
indexed = Indexed
vector :: Storable a => [a] -> Vector (Data a)
vector as = unfreezeVector l (value as)
where
l = value $ Prelude.length as
modifyLength :: (Data Length -> Data Length) -> Vector a -> Vector a
modifyLength f vec = vec {length = f (length vec)}
setLength :: Data Length -> Vector a -> Vector a
setLength = modifyLength . const
boundVector :: Int -> Vector a -> Vector a
boundVector maxLen = modifyLength (cap r)
where
r = negativeRange + singletonRange (fromIntegral maxLen) + 1
instance Storable a => Computable (Vector (Data a))
where
type Internal (Vector (Data a)) = (Length, [Internal (Data a)])
internalize vec =
internalize (length vec, freezeVector $ map internalize vec)
externalize l_a = map externalize $ unfreezeVector l a
where
l = externalize $ get21 l_a
a = externalize $ get22 l_a
instance Storable a => Computable (Vector (Vector (Data a)))
where
type Internal (Vector (Vector (Data a))) =
(Length, [Length], [[Internal (Data a)]])
internalize vec = internalize
( length vec
, freezeVector $ map length vec
, freezeVector $ map (freezeVector . map internalize) vec
)
externalize inp
= map (map externalize . uncurry unfreezeVector)
$ zip l2sV (unfreezeVector l1 a)
where
l1 = externalize $ get31 inp
l2s = externalize $ get32 inp
a = externalize $ get33 inp
l2sV = unfreezeVector l1 l2s
instance RandomAccess (Vector a)
where
type Element (Vector a) = a
(!) = index
(++) :: Computable a => Vector a -> Vector a -> Vector a
Indexed l1 ixf1 ++ Indexed l2 ixf2 = Indexed (l1+l2) ixf
where
ixf i = ifThenElse (i < l1) ixf1 (ixf2 . subtract l1) i
infixr 5 ++
take :: Data Int -> Vector a -> Vector a
take n (Indexed l ixf) = Indexed (min n l) ixf
drop :: Data Int -> Vector a -> Vector a
drop n (Indexed l ixf) = Indexed (max 0 (ln)) (\x -> ixf (x+n))
dropWhile :: (a -> Data Bool) -> Vector a -> Vector a
dropWhile cont vec = drop i vec
where
i = while ((< length vec) &&* (cont . (vec !))) (+1) 0
splitAt :: Data Int -> Vector a -> (Vector a, Vector a)
splitAt n vec = (take n vec, drop n vec)
head :: Vector a -> a
head = (!0)
last :: Vector a -> a
last vec = vec ! (length vec 1)
tail :: Vector a -> Vector a
tail = drop 1
init :: Vector a -> Vector a
init vec = take (length vec 1) vec
tails :: Vector a -> Vector (Vector a)
tails vec = Indexed (length vec + 1) (\n -> drop n vec)
inits :: Vector a -> Vector (Vector a)
inits vec = Indexed (length vec + 1) (\n -> take n vec)
inits1 :: Vector a -> Vector (Vector a)
inits1 = tail . inits
permute :: (Data Length -> Data Ix -> Data Ix) -> (Vector a -> Vector a)
permute perm (Indexed l ixf) = Indexed l (ixf . perm l)
reverse :: Vector a -> Vector a
reverse = permute $ \l i -> l1i
replicate :: Data Int -> a -> Vector a
replicate n a = Indexed n (const a)
enumFromTo :: Data Int -> Data Int -> Vector (Data Int)
enumFromTo m n = Indexed (nm+1) (+m)
(...) :: Data Int -> Data Int -> Vector (Data Int)
(...) = enumFromTo
zip :: Vector a -> Vector b -> Vector (a,b)
zip (Indexed l1 ixf1) (Indexed l2 ixf2) = Indexed (min l1 l2) (ixf1 &&& ixf2)
unzip :: Vector (a,b) -> (Vector a, Vector b)
unzip (Indexed l ixf) = (Indexed l (fst.ixf), Indexed l (snd.ixf))
map :: (a -> b) -> Vector a -> Vector b
map f (Indexed l ixf) = Indexed l (f . ixf)
zipWith :: (a -> b -> c) -> Vector a -> Vector b -> Vector c
zipWith f aVec bVec = map (uncurry f) $ zip aVec bVec
fold :: Computable a => (a -> b -> a) -> a -> Vector b -> a
fold f x (Indexed l ixf) = for 0 (l1) x (\i s -> f s (ixf i))
fold1 :: Computable a => (a -> a -> a) -> Vector a -> a
fold1 f a = fold f (head a) a
sum :: Numeric a => Vector (Data a) -> Data a
sum = fold (+) 0
maximum :: Ord a => Vector (Data a) -> Data a
maximum = fold1 max
minimum :: Ord a => Vector (Data a) -> Data a
minimum = fold1 min
scalarProd :: Numeric a => Vector (Data a) -> Vector (Data a) -> Data a
scalarProd a b = sum (zipWith (*) a b)