mcsp-algorithms-0.1.0: Algorithms for Minimum Common String Partition (MCSP) in Haskell.
Safe HaskellSafe-Inferred
LanguageGHC2021

MCSP.Algorithms.Vector

Description

Operations on vector of Default.

Synopsis

Documentation

type Default = Float Source #

Default type used in specialized vector operations.

Initialization

zeros :: (Unbox a, Num a) => Int -> Vector a Source #

Create a vector of zeros given the length.

>>> zeros 3
[0.0,0.0,0.0]
>>> zeros 0
[]

replicate :: Unbox a => Int -> a -> Vector a #

Element-wise Operations

map :: (Unbox a, Unbox b) => (a -> b) -> Vector a -> Vector b #

choose :: Unbox a => a -> a -> Vector Bool -> Vector a Source #

Case analysis for the Bool type.

choose x y p evaluates to x in every position that is False in p, and evaluates to y everywehere else. Works as a vector version of bool.

The name is taken from numpy.choose.

>>> choose 10 (-5) [True, False, False, True]
[-5.0,10.0,10.0,-5.0]

(.+) :: (Unbox a, Num a) => Vector a -> Vector a -> Vector a infixl 6 Source #

Element-wise addition.

>>> [1, 2, 3] .+ [5, 6, 7]
[6.0,8.0,10.0]

(.-) :: (Unbox a, Num a) => Vector a -> Vector a -> Vector a infixl 6 Source #

Element-wise subtraction.

>>> [1, 2, 3] .- [5, 6, 7]
[-4.0,-4.0,-4.0]

(.*) :: (Unbox a, Num a) => Vector a -> Vector a -> Vector a infixl 7 Source #

Element-wise multiplication.

>>> [1, 2, 3] .* [5, 6, 7]
[5.0,12.0,21.0]

(.*.) :: (Unbox a, Num a) => a -> Vector a -> Vector a infixl 7 Source #

Multiplication by a scalar.

>>> 3 .*. [5, 6, 7]
[15.0,18.0,21.0]

sum :: (Unbox a, Num a) => NonEmpty (Vector a) -> Vector a Source #

Element-wise sum of all vectors.

>>> sum [[1, 2], [3, 4], [5, 6]]
[9.0,12.0]

Sorting

sort :: (Unbox a, Ord a) => Vector a -> Vector a Source #

Sorts an array using the default comparison.

>>> sort [3, 1, 2]
[1.0,2.0,3.0]

sortOn :: (Unbox a, Ord b) => (a -> b) -> Vector a -> Vector a Source #

Sorts a vector by comparing the results of a key function applied to each element.

>>> sortOn (\x -> x * x) [-3, -1, 2]
[-1.0,2.0,-3.0]

sortBy :: Unbox a => (a -> a -> Ordering) -> Vector a -> Vector a Source #

Sorts a vector using a custom comparison.

>>> import Data.Ord (Ordering (..))
>>> sortBy (\x y -> if x * x < y * y then LT else GT) [-3, -1, 2]
[-1.0,2.0,-3.0]

argSort :: (Unbox a, Ord a) => Vector a -> Vector Int Source #

Returns the indices that would sort the vector.

>>> argSort [30, 10, 20]
[1,2,0]
>>> argSort $ argSort [30, 10, 20]
[2,0,1]

sortLike :: (Unbox a, Unbox b, Ord b) => Vector a -> Vector b -> Vector a Source #

Sort a vector based on the values of another array.

>>> sortLike [30, 10, 20] [0.2, 0.9, 0.1]
[20.0,30.0,10.0]

Statistics

normalized :: (Unbox a, Fractional a, Ord a) => Vector a -> Vector a Source #

Normalize a vector by its maximum absolute value.

>>> normalized [1, 2, 5, 10]
[0.1,0.2,0.5,1.0]
>>> normalized [1, 2, 5, -10]
[0.1,0.2,0.5,-1.0]
>>> normalized []
[]
>>> normalized [0]
[0.0]

standardized :: (Unbox a, Floating a, Eq a) => Vector a -> Vector a Source #

Adapt values for such that the mean becomes zero and standard deviation, one.

>>> standardized [1, 2, 5, 10]
[-1.0,-0.7142857142857143,0.14285714285714285,1.5714285714285714]
>>> standardized []
[]
>>> standardized [1, 1]
[0.0,0.0]

Monadic Operations

sumM :: (Unbox a, Num a, Monad m) => NonEmpty (m (Vector a)) -> m (Vector a) Source #

Lifted version of sum.

>>> import MCSP.System.Random (generateWith)
>>> generateWith (2,3) $ sumM [uniformN 4, uniformSN 4]
[0.17218197108856648,0.21998774703644852,-0.16158831286684616,1.0345635554897776]

replicateM :: (Monad m, Unbox a) => Int -> m a -> m (Vector a) #

Random Operations

uniformN :: (Unbox a, Variate a, Num a) => Int -> Random (Vector a) Source #

Generate multiple uniformly distributed values between [0,1].

Replicated version of uniform.

>>> import MCSP.System.Random (generateWith)
>>> generateWith (2,3) $ uniformN 3
[1.9765692737382712e-2,0.3673953156572515,0.5752284362246546]

uniformSN :: (Unbox a, Variate a, Num a) => Int -> Random (Vector a) Source #

Generate multiple uniformly distributed values between [-1,1].

Signed version of uniformN.

>>> import MCSP.System.Random (generateWith)
>>> generateWith (2,3) $ uniformSN 3
[-0.9604686145252346,-0.26520936868549705,0.15045687244930916]

uniformRN :: (Unbox a, Variate a) => a -> a -> Int -> Random (Vector a) Source #

Generate multiple uniformly distributed values in the given range.

Replicated version of uniformR.

>>> import MCSP.System.Random (generateWith)
>>> generateWith (2,3) $ uniformRN 5 50 3
[5.889456173182222,21.532789204576318,30.885279630109455]

weighted :: (Unbox a, Variate a, Num a) => a -> Vector a -> Random (Vector a) Source #

Multiplies the vector by a single random value between [0,maxWeight].

Randomized version of .*.. See also weightedN.

>>> import MCSP.System.Random (generateWith)
>>> generateWith (2,3) $ weighted 10 [1, 2, 10]
[0.19765692737382712,0.39531385474765424,1.9765692737382712]

weightedN :: (Unbox a, Variate a, Num a) => a -> Vector a -> Random (Vector a) Source #

Multiplies the vector by multiple random values between [0,maxWeight].

Randomized version of .*. See also weighted.

>>> import MCSP.System.Random (generateWith)
>>> generateWith (2,3) $ weightedN 10 [1, 2, 10]
[0.19765692737382712,7.34790631314503,57.52284362246546]

choice :: NonEmpty (Double, Random a) -> Random a Source #

Choose randomly between multiple Random monad, with probablity proportional to its given weight.

>>> import MCSP.System.Random (generateWith)
>>> generateWith (1,2) $ replicateM 10 $ choice [(1, pure 'a'), (2, pure 'b')]
"abbbaabbbb"
>>> generateWith (1,2) $ replicateM 10 $ choice [(1, uniformR (-1) 0), (2, uniformR 0 1)]
[-0.11816487538074749,0.5798377716767166,0.12231072251084052,0.754750234725723,0.5163453222019222,0.9673060222002038,-0.28900858364465354,0.609061325679456,-0.15187385001852494,0.4987697781636008]