inf-backprop-0.2.0.0: Automatic differentiation and backpropagation.
Copyright(C) 2025 Alexey Tochin
LicenseBSD3 (see the file LICENSE)
MaintainerAlexey Tochin <Alexey.Tochin@gmail.com>
Safe HaskellNone
LanguageHaskell2010

Numeric.InfBackprop.Utils.Vector

Description

Utility functions for working with vectors.

Synopsis

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

Expand
>>> 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

Expand
>>> import GHC.Int (Int)
>>> import Data.Vector (fromList)
>>> safeHead (fromList [1, 2, 3]) :: Maybe Int
Just 1
>>> safeHead (fromList []) :: Maybe Int
Nothing

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

Expand
>>> import GHC.Int (Int)
>>> import Data.Vector (fromList, empty)
>>> safeLast (fromList [1, 2, 3]) :: Maybe Int
Just 3
>>> safeLast empty :: Maybe Int
Nothing

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

Expand
>>> 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

Expand
>>> 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

Expand
>>> 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]