| Copyright | (C) 2025 Alexey Tochin |
|---|---|
| License | BSD3 (see the file LICENSE) |
| Maintainer | Alexey Tochin <Alexey.Tochin@gmail.com> |
| Safe Haskell | None |
| Language | Haskell2010 |
Numeric.InfBackprop.Utils.Vector
Description
Utility functions for working with vectors.
Synopsis
- fromTuple :: forall v a input (length :: Nat). (Vector v a, IndexedListLiterals input length a) => input -> v a
- safeHead :: (Vector v a, MonadPlus m) => v a -> m a
- safeLast :: (Vector v a, MonadPlus m) => v a -> m a
- trimArrayHead :: (Vector v a, Eq a) => a -> v a -> v a
- trimArrayTail :: (Vector v a, Eq a) => a -> v a -> v a
- zipWith :: (Vector v a, Vector v b, Vector v c) => (a -> b -> c) -> (a -> c) -> (b -> c) -> v a -> v b -> v c
Documentation
fromTuple :: forall v a input (length :: Nat). (Vector v a, IndexedListLiterals input length a) => input -> v a Source #
Converts a tuple into a Vector (Vector).
Examples
>>>import GHC.Int (Int)>>>import qualified Data.Vector as DV
>>>fromTuple (1 :: Int, 2 :: Int, 3 :: Int) :: DV.Vector Int[1,2,3]
safeHead :: (Vector v a, MonadPlus m) => v a -> m a Source #
Returns the first element of a vector safely.
If the vector is empty, it returns Nothing.
Examples
>>>import GHC.Int (Int)>>>import Data.Vector (fromList)
>>>safeHead (fromList [1, 2, 3]) :: Maybe IntJust 1
>>>safeHead (fromList []) :: Maybe IntNothing
safeLast :: (Vector v a, MonadPlus m) => v a -> m a Source #
Returns the last element of a vector safely.
If the vector is empty, it returns Nothing.
Examples
>>>import GHC.Int (Int)>>>import Data.Vector (fromList, empty)
>>>safeLast (fromList [1, 2, 3]) :: Maybe IntJust 3
>>>safeLast empty :: Maybe IntNothing
trimArrayHead :: (Vector v a, Eq a) => a -> v a -> v a Source #
Removes elements from the beginning of the vector until the first element is not equal to the given value.
Examples
>>>import Data.Vector (fromList, empty)
>>>trimArrayHead 1 (fromList [1, 1, 1, 2, 3])[2,3]
>>>trimArrayHead 1 empty[]
trimArrayTail :: (Vector v a, Eq a) => a -> v a -> v a Source #
Removes elements from the end of the vector until the last element is not equal to the given value.
Examples
>>>import Data.Vector (fromList, empty)
>>>trimArrayTail 3 (fromList [1, 2, 3, 3, 3])[1,2]
>>>trimArrayTail 3 empty[]
zipWith :: (Vector v a, Vector v b, Vector v c) => (a -> b -> c) -> (a -> c) -> (b -> c) -> v a -> v b -> v c Source #
Combines two arrays of different lengths using a custom function. The resulting array has a length equal to the maximum of the two input vectors. The shorter array is padded with values generated by the provided functions.
Examples
>>>import Prelude (id, negate, (-), Int)>>>import qualified Data.Vector as DV
The following example demonstrates subtracting two arrays of different lengths. The shorter array is padded with zeros, and the remaining elements are processed using the provided functions.
>>>:{zipWith (-) -- Subtract corresponding elements from the two arrays id -- Keep the remaining elements of the first array unchanged negate -- Negate the remaining elements of the second array (DV.fromList [10, 20, 30]) -- First array (DV.fromList [1, 2]) -- Second array :} [9,18,30]
>>>import Prelude (id, negate, (-), Int)>>>import Data.Vector (fromList)
>>>let v0 :: DV.Vector Int = DV.fromList [10, 20, 30]>>>let v1 :: DV.Vector Int = DV.fromList [1, 2]>>>zipWith (-) id negate v0 v1[9,18,30]