Copyright | (c) ForSyDe Group KTH 2007-2019 |
---|---|
License | BSD-style (see the file LICENSE) |
Maintainer | forsyde-dev@kth.se |
Stability | experimental |
Portability | portable |
Safe Haskell | Safe |
Language | Haskell98 |
This module defines the data type Vector
and the corresponding
functions. It is a development of the module defined by
Reekie.
The Vector
data type is a shallow interpretation of arrays and is
used for quick prototyping of array algorithms and skeletons,
whereas in fact it is implemented as an infinite list itself. For a
type-checked fixed-size data type for representing vectors, see
FSVec or
REPA.
OBS: The lengths in the API documentation for function arguments are not type-safe, but rather suggestions for usage in designing vector algorithms or skeletons.
Synopsis
- data Vector a
- (<+>) :: Vector a -> Vector a -> Vector a
- (<:) :: Vector a -> a -> Vector a
- nullV :: Vector a -> Bool
- lengthV :: Vector a -> Int
- vector :: [a] -> Vector a
- fromVector :: Vector a -> [a]
- unitV :: a -> Vector a
- iterateV :: (Num a, Eq a) => a -> (b -> b) -> b -> Vector b
- generateV :: (Num a, Eq a) => a -> (b -> b) -> b -> Vector b
- copyV :: (Num a, Eq a) => a -> b -> Vector b
- mapV :: (a -> b) -> Vector a -> Vector b
- zipWithV :: (a -> b -> c) -> Vector a -> Vector b -> Vector c
- zipWith3V :: (a -> b -> c -> d) -> Vector a -> Vector b -> Vector c -> Vector d
- reduceV :: (a -> a -> a) -> Vector a -> a
- pipeV :: Vector (a -> a) -> a -> a
- foldlV :: (a -> b -> a) -> a -> Vector b -> a
- foldrV :: (b -> a -> a) -> a -> Vector b -> a
- scanlV :: (a -> b -> a) -> a -> Vector b -> Vector a
- scanrV :: (b -> a -> a) -> a -> Vector b -> Vector a
- atV :: Integral a => Vector b -> a -> b
- headV :: Vector a -> a
- tailV :: Vector a -> Vector a
- lastV :: Vector a -> a
- initV :: Vector a -> Vector a
- headsV :: Vector a -> Vector (Vector a)
- tailsV :: Vector a -> Vector (Vector a)
- takeV :: (Num a, Ord a) => a -> Vector b -> Vector b
- dropV :: (Num a, Ord a) => a -> Vector b -> Vector b
- selectV :: Int -> Int -> Int -> Vector a -> Vector a
- groupV :: Int -> Vector a -> Vector (Vector a)
- filterV :: (a -> Bool) -> Vector a -> Vector a
- stencilV :: Int -> Vector a -> Vector (Vector a)
- replaceV :: Vector a -> Int -> a -> Vector a
- zipV :: Vector a -> Vector b -> Vector (a, b)
- unzipV :: Vector (a, b) -> (Vector a, Vector b)
- concatV :: Vector (Vector a) -> Vector a
- reverseV :: Vector a -> Vector a
- shiftlV :: Vector a -> a -> Vector a
- shiftrV :: Vector a -> a -> Vector a
- rotrV :: Vector a -> Vector a
- rotlV :: Vector a -> Vector a
- rotateV :: Int -> Vector a -> Vector a
Documentation
The data type Vector
is modeled similar to a list. It has two data type constructors. NullV
constructs the empty vector, while :>
constructsa vector by adding an value to an existing vector..
Vector
is an instance of the classes Read
and Show
. This means that the vector
1:>2:>3:>NullV
is shown as
<1,2,3>
The operator <+>
concatenates two vectors.
The operator '(<:)' appends an element at the end of a vector.
Queries
Generators
fromVector :: Vector a -> [a] Source #
The function fromVector
converts a vector into a list.
:: (Num a, Eq a) | |
=> a | number of elements = |
-> (b -> b) | generator function ( |
-> b | initial element |
-> Vector b | generated vector; length = |
The function iterateV
generates a vector with a given number of
elements starting from an initial element using a supplied function
for the generation of elements.
>>>
iterateV 5 (+1) 1
<1,2,3,4,5>
The function copyV
generates a vector with a given number of
copies of the same element.
>>>
copyV 7 5
<5,5,5,5,5,5,5>
Functional skeletons
The higher-order function mapV
applies a function on all elements of a vector.
The higher-order function zipWithV
applies a function pairwise on two vectors.
:: (a -> b -> c -> d) | |
-> Vector a | length = |
-> Vector b | length = |
-> Vector c | length = |
-> Vector d | length = |
The higher-order function zipWithV3
applies a function 3-tuple-wise on three vectors.
reduceV :: (a -> a -> a) -> Vector a -> a Source #
Reduces a vector of elements to a single element based on a binary function.
>>>
reduceV (+) $ vector [1,2,3,4,5]
15
pipeV :: Vector (a -> a) -> a -> a Source #
Pipes an element through a vector of functions.
>>>
vector [(*2), (+1), (/3)] `pipeV` 3 -- is the same as ((*2) . (+1) . (/3)) 3
4.0
foldlV :: (a -> b -> a) -> a -> Vector b -> a Source #
The higher-order functions foldlV
folds a function from the
right to the left over a vector using an initial value.
>>>
foldlV (-) 8 $ vector [4,2,1] -- is the same as (((8 - 4) - 2) - 1)
1
foldrV :: (b -> a -> a) -> a -> Vector b -> a Source #
The higher-order functions foldrV
folds a function from the
left to the right over a vector using an initial value.
>>>
foldrV (-) 8 $ vector [4,2,1] -- is the same as (4 - (2 - (1 - 8)))
-5
:: (a -> b -> a) | funtion to generate next element |
-> a | initial element |
-> Vector b | input vector; length = |
-> Vector a | output vector; length = |
Performs the parallel prefix operation on a vector.
>>>
scanlV (+) 0 $ vector [1,1,1,1,1,1]
<1,2,3,4,5,6>
:: (b -> a -> a) | funtion to generate next element |
-> a | initial element |
-> Vector b | input vector; length = |
-> Vector a | output vector; length = |
Performs the parallel suffix operation on a vector.
>>>
scanrV (+) 0 $ vector [1,1,1,1,1,1]
<6,5,4,3,2,1>
Selectors
atV :: Integral a => Vector b -> a -> b Source #
The function atV
returns the n-th element in a vector, starting
from zero.
>>>
vector [1,2,3,4,5] `atV` 3
4
The functions tailV
returns all, but the first element of a vector.
The function initV
returns all but the last elements of a vector.
Returns a vector containing all the possible prefixes of an input vector.
>>>
let v = vector [1,2,3,4,5,6]
>>>
headsV v
<<1>,<1,2>,<1,2,3>,<1,2,3,4>,<1,2,3,4,5>,<1,2,3,4,5,6>,<1,2,3,4,5,6>>
Returns a vector containing all the possible suffixes of an input vector.
>>>
let v = vector [1,2,3,4,5,6]
>>>
tailsV v
<<1,2,3,4,5,6>,<2,3,4,5,6>,<3,4,5,6>,<4,5,6>,<5,6>,<6>,<>>
The function takeV
returns the first n
elements of a vector.
>>>
takeV 2 $ vector [1,2,3,4,5]
<1,2>
The function dropV
drops the first n
elements of a vector.
>>>
dropV 2 $ vector [1,2,3,4,5]
<3,4,5>
:: Int | the initial element, starting from zero |
-> Int | stepsize between elements |
-> Int | number of elements |
-> Vector a | length = |
-> Vector a | length |
The function selectV
selects elements in the vector based on a
regular stride.
The function groupV
groups a vector into a vector of vectors of
size n.
>>>
groupV 3 $ vector [1,2,3,4,5,6,7,8]
<<1,2,3>,<4,5,6>>
The higher-function filterV
takes a predicate function and a
vector and creates a new vector with the elements for which the
predicate is true.
>>>
filterV odd $ vector [1,2,3,4,5,6,7,8]
<1,3,5,7>
(*) however, the length is unknown, because it is dependent on
the data contained inside the vector. Try avoiding filterV
in
designs where the size of the data is crucial.
Returns a stencil of n
neighboring elements for each possible
element in a vector.
>>>
stencilV 3 $ vector [1..5]
<<1,2,3>,<2,3,4>,<3,4,5>>
Permutators
:: Vector a | input vector; length = |
-> Int | position of the element to be replaced |
-> a | new element |
-> Vector a | altered vector; length = |
The function replaceV
replaces an element in a vector.
>>>
replaceV (vector [1..5]) 2 100
<1,2,100,4,5>
The function zipV
zips two vectors into a vector of tuples.
The function unzipV
unzips a vector of tuples into two vectors.
concatV :: Vector (Vector a) -> Vector a Source #
The function concatV
transforms a vector of vectors to a single vector.
reverseV :: Vector a -> Vector a Source #
The function reverseV
reverses the order of elements in a vector.
shiftlV :: Vector a -> a -> Vector a Source #
The function shiftlV
shifts a value from the left into a vector.
>>>
vector [1..5] `shiftlV` 100
<100,1,2,3,4>
shiftrV :: Vector a -> a -> Vector a Source #
The function shiftrV
shifts a value from the right into a vector.
>>>
vector [1..5] `shiftrV` 100
<2,3,4,5,100>
rotrV :: Vector a -> Vector a Source #
The function rotrV
rotates a vector to the right. Note that
this fuction does not change the size of a vector.
>>>
rotrV $ vector [1..5]
<2,3,4,5,1>