Module Vector

This module is undocumented. This is a list of its definitions.

(**) :: Ring a => VecComp a => a -> Vector a -> Vector a

s ** v multiplies all elements of the vector v by the scalar s.

The scalar goes on the left only. There is no right-hand variant, so v ** s does not typecheck.

** is the only multiplication vectors really have. The Ring instance of Vector is partial: one, (*) and fromInteger are not implemented and each of them fails at runtime with an "Unsupported operation" error rather than being rejected by the compiler. neg and - do work, as do zero and + from the Additive instance.

Example:

2 ** vector [1, 2, 3] = vector [2, 4, 6]
Ring_Vector_a_super0 :: VecComp a => Ring a => Additive (Vector a)
allVector :: (a -> <b> Boolean) -> Vector a -> <b> Boolean

Returns True if all elements of the vector satisfy the given predicate.

anyVector :: (a -> <b> Boolean) -> Vector a -> <b> Boolean

Returns True if at least one element of the vector satisfies the given predicate.

concatVector :: Vector a -> Vector a -> Vector a

Creates a vector containing the elements of the first vector followed by the elements of the second one.

containsVector :: a -> Vector a -> Boolean

containsVector x v returns True if the vector v contains the element x.

Mind the argument order: the element comes first and the vector second, which is the opposite way round from every other *Vector function in this module.

It also does not short-circuit. It folds over every index of the vector and combines the comparisons with ||, so a match in the first position still costs a full traversal, and the predicate is evaluated for every element.

Example:

containsVector 3 (vector [1, 2, 3]) = True
copyFromMVector :: MVector a -> Integer -> MVector a -> Integer -> Integer -> <Proc> ()

copyFromMVector src srcPos target targetPos length copies length elements of the mutable vector src, starting from index srcPos, into the mutable vector target, starting from index targetPos. The argument order is the same as in copyFromVector.

src and target may be the same vector. An overlapping copy behaves as if the source range were first copied to a temporary vector, so the elements still to be read are never overwritten before they are used.

Example:

> v = mvector [1, 2, 3, 4, 5]
> copyFromMVector v 0 v 1 4
> freezeMVector v
vector [1, 1, 2, 3, 4]
copyFromVector :: Vector a -> Integer -> MVector a -> Integer -> Integer -> <Proc> ()

copyFromVector src srcPos target targetPos length copies length elements of the immutable vector src, starting from index srcPos, into the mutable vector target, starting from index targetPos.

Four of the five arguments are indices or lengths, so the order is worth reading carefully: source, offset in the source, target, offset in the target, number of elements. The elements of target outside the written range keep their old values, and target must already be long enough.

Example:

> src = vector [1, 2, 3, 4, 5]
> target = mvector [0, 0, 0, 0, 0]
> copyFromVector src 1 target 3 2
> freezeMVector target
vector [0, 0, 0, 2, 3]
dotp :: Ring a => VecComp a => Vector a -> Vector a -> a

The dot product of the two vectors: the sum of the products of their corresponding elements. Only the leading elements up to the length of the shorter vector are used, so the tail of the longer one is discarded.

The operations of this module that combine two vectors each treat unequal lengths differently, and only the last of them reports an error:

  • dotp truncates to the shorter vector.
  • +, from the Additive instance, pads: it adds the common prefix and then copies the surplus tail of the longer vector into the result unchanged.
  • -, from the Ring instance, does the same, except that a surplus tail of the second vector is copied negated.
  • zipVectorsWith uses the length of its first vector and indexes the second one with the same indices, which fails if the second one is shorter.

Examples:

> dotp (vector [1, 2, 3]) (vector [10, 20, 30, 40, 50])
140
> vector [1, 2, 3] + vector [10, 20, 30, 40, 50]
vector [11, 22, 33, 40, 50]
> vector [1, 2, 3] - vector [10, 20, 30, 40, 50]
vector [-9, -18, -27, -40, -50]
> vector [10, 20, 30, 40, 50] - vector [1, 2, 3]
vector [9, 18, 27, 40, 50]
foldlVector :: (a -> b -> <c> a) -> a -> Vector b -> <c> a

Folds over all elements of the vector starting with the given initial value.

iterVector :: (a -> <c> b) -> Vector a -> <c> ()

Calls the given function with all elements of the vector.

l1Norm :: OrderedRing a => IndexedSequence b => Sequence (b a) => b a -> a

The 1-norm of the vector, i.e. the sum of the absolute values of its elements.

mapFirstVector :: (a -> <c> Maybe b) -> Vector a -> <c> Maybe b

Applies the given function to the elements of the vector until it returns Just and returns that result. Returns Nothing if the function returns Nothing for all elements.

Here "first" genuinely means first: a vector has a defined order, so the scan runs from index 0 upwards and stops at the first Just. This is unlike the mapFirst of the hash-based sets and maps, where the iteration order is not a contract and the result is nondeterministic if more than one value matches.

Example:

> mapFirstVector (\x -> if x > 2 then Just (x*100) else Nothing) (vector [1, 5, 3, 7])
Just 500
mapVector :: VecComp a => VecComp b => (a -> b) -> Vector a -> Vector b

Creates a vector containing the elements of the given vector mapped with the given function.

maxNorm :: OrderedRing a => Sequence (b a) => IndexedSequence b => b a -> a

The maximum norm of the vector, i.e. the greatest absolute value of its elements.

mvector :: VecComp a => [a] -> <Proc> MVector a

Creates a new mutable vector containing the elements of the given list. The result is a fresh vector; later changes to it are not visible in the list.

Example:

> v = mvector [1, 2, 3]
> v
mvector [1, 2, 3]
> setMVector v 1 99
> freezeMVector v
vector [1, 99, 3]
norm :: Sequence (a b) => IndexedSequence a => Real b => a b -> b

The Euclidean norm of the vector.

normSq :: IndexedSequence a => Ring b => Sequence (a b) => a b -> b

The sum of the squares of the elements of the vector, i.e. the square of its Euclidean norm.

singletonVector :: VecComp a => a -> Vector a

Creates a vector containing just the given element.

sortMVectorByte :: MVector Byte -> <Proc> ()

Sorts the given mutable vector of bytes into ascending order in place. See sortMVectorInt for an example.

sortMVectorCharacter :: MVector Character -> <Proc> ()

Sorts the given mutable vector of characters into ascending order in place. See sortMVectorInt for an example.

sortMVectorDouble :: MVector Double -> <Proc> ()

Sorts the given mutable vector of doubles into ascending order in place. See sortMVectorInt for an example.

sortMVectorFloat :: MVector Float -> <Proc> ()

Sorts the given mutable vector of floats into ascending order in place. See sortMVectorInt for an example.

sortMVectorInt :: MVector Integer -> <Proc> ()

Sorts the given mutable vector of integers into ascending order in place and returns (). The argument itself is modified; nothing is returned to hold on to, so the usual sequence is to sort and then take an immutable snapshot with freezeMVector.

There is one of these per primitive element type: sortMVectorByte, sortMVectorCharacter, sortMVectorDouble, sortMVectorFloat, sortMVectorInt, sortMVectorLong and sortMVectorShort. They differ only in the element type. There is no generic version, so a mutable vector of any other element type cannot be sorted with these.

Example:

> v = mvector [3, 1, 2]
> sortMVectorInt v
> freezeMVector v
vector [1, 2, 3]
sortMVectorLong :: MVector Long -> <Proc> ()

Sorts the given mutable vector of longs into ascending order in place. See sortMVectorInt for an example.

sortMVectorShort :: MVector Short -> <Proc> ()

Sorts the given mutable vector of shorts into ascending order in place. See sortMVectorInt for an example.

vector :: VecComp a => [a] -> Vector a

Creates a vector containing the elements of the given list.

A vector prints as the word vector followed by its elements as a list, which is how a vector is told apart from a list in the console. vectorToList goes back the other way.

Example:

> vector [1, 2, 3]
vector [1, 2, 3]
> vectorToList (vector [1, 2, 3])
[1, 2, 3]
vectorF :: VecComp a => Integer -> (Integer -> <b> a) -> <b> Vector a

vectorF n f creates a vector of length n whose element at index i is f i.

The length comes first and the generator function second. The indices are 0-based and the upper bound is exclusive, so f is called with 0, ..., n-1.

Example:

vectorF 4 (\i -> i*i) = vector [0, 1, 4, 9]
vectorToList :: Vector a -> [a]

The elements of the vector as a list.

zipVectorsWith :: VecComp a => VecComp b => VecComp c => (a -> b -> c) -> Vector a -> Vector b -> Vector c

Creates a vector whose element at index i is computed by the given function from the elements at index i of the two given vectors.

The length of the result is the length of the first vector, and the second vector is read at those same indices. A second vector that is longer is therefore silently truncated, and a second vector that is shorter is an error: the read runs off its end and raises ArrayIndexOutOfBoundsException.

This is a third, different rule from the other two ways this module treats vectors of unequal length. + pads to the longer vector and dotp truncates to the shorter one; only zipVectorsWith is asymmetric in its arguments. See dotp for the comparison.

Example:

> zipVectorsWith (*) (vector [1, 2, 3]) (vector [10, 20, 30, 40, 50])
vector [10, 40, 90]