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

Data.FiniteSupportStream

Description

This module provides functionality for working with infinite streams that have finite support (i.e., only finitely many non-zero elements). The streams are internally represented as arrays for efficient computation.

Any linear functional on an ordinary stream (Stream) can be represented as a finite support stream. Inversely, any finite support stream can be represented as a linear functional on an ordinary stream.

Synopsis

The type of finite support streams

newtype FiniteSupportStream a Source #

A stream with finite support, represented as a vector. Elements beyond the vector's length are implicitly zero. The vector may contain trailing zeros, which can be removed using optimize.

The type parameter a typically has an Additive instance with a zero element.

Examples

Expand
>>> import GHC.Base (Int, Float, Bool(False, True))
>>> import Data.Vector (fromList)
>>> MkFiniteSupportStream $ fromList [0, 1, 2, 3] :: FiniteSupportStream Int
[0,1,2,3,0,0,0,...
>>> MkFiniteSupportStream $ fromList [0, 1, 2, 3] :: FiniteSupportStream Float
[0.0,1.0,2.0,3.0,0.0,0.0,0.0,...
>>> MkFiniteSupportStream $ fromList [False, True] :: FiniteSupportStream Bool
[False,True,False,False,False,...

Constructors

MkFiniteSupportStream 

Fields

Instances

Instances details
Foldable FiniteSupportStream Source # 
Instance details

Defined in Data.FiniteSupportStream

Methods

fold :: Monoid m => FiniteSupportStream m -> m #

foldMap :: Monoid m => (a -> m) -> FiniteSupportStream a -> m #

foldMap' :: Monoid m => (a -> m) -> FiniteSupportStream a -> m #

foldr :: (a -> b -> b) -> b -> FiniteSupportStream a -> b #

foldr' :: (a -> b -> b) -> b -> FiniteSupportStream a -> b #

foldl :: (b -> a -> b) -> b -> FiniteSupportStream a -> b #

foldl' :: (b -> a -> b) -> b -> FiniteSupportStream a -> b #

foldr1 :: (a -> a -> a) -> FiniteSupportStream a -> a #

foldl1 :: (a -> a -> a) -> FiniteSupportStream a -> a #

toList :: FiniteSupportStream a -> [a] #

null :: FiniteSupportStream a -> Bool #

length :: FiniteSupportStream a -> Int #

elem :: Eq a => a -> FiniteSupportStream a -> Bool #

maximum :: Ord a => FiniteSupportStream a -> a #

minimum :: Ord a => FiniteSupportStream a -> a #

sum :: Num a => FiniteSupportStream a -> a #

product :: Num a => FiniteSupportStream a -> a #

ExtandableMap a b c d => ExtandableMap a b (FiniteSupportStream c) (FiniteSupportStream d) Source #

FiniteSupportStream instance of ExtandableMap typeclass.

Instance details

Defined in Data.FiniteSupportStream

AlgebraicPower b a => AlgebraicPower b (FiniteSupportStream a) Source #

FiniteSupportStreaminstance of AlgebraicPower typeclass for raising FiniteSupportStream to powers.

Instance details

Defined in Data.FiniteSupportStream

MultiplicativeAction a b => MultiplicativeAction a (FiniteSupportStream b) Source #

FiniteSupportStream instance of MultiplicativeAction.

Instance details

Defined in Data.FiniteSupportStream

(Show a, Eq a, Additive a) => Show (FiniteSupportStream a) Source #

Show instance of FiniteSupportStream.

Instance details

Defined in Data.FiniteSupportStream

(Eq a, Additive a) => Eq (FiniteSupportStream a) Source #

Eq instance of FiniteSupportStream.

Instance details

Defined in Data.FiniteSupportStream

AutoDifferentiableArgument a => AutoDifferentiableArgument (FiniteSupportStream a) Source #

FiniteSupportStream instance for AutoDifferentiableArgument typeclass.

Instance details

Defined in Numeric.InfBackprop.Core

AutoDifferentiableValue a => AutoDifferentiableValue (FiniteSupportStream a) Source #

FiniteSupportStream instance for AutoDifferentiableValue typeclass.

Instance details

Defined in Numeric.InfBackprop.Core

Additive a => Additive (FiniteSupportStream a) Source #

Additive instance for FiniteSupportStream.

Examples

Expand
>>> import GHC.Base (Int)
>>> (unsafeFromList [1, 2, 3]) + (unsafeFromList [10, 20]) :: FiniteSupportStream Int
[11,22,3,0,0,0,...
>>> (unsafeFromList [1, 2, 3]) + empty
[1,2,3,0,0,0,...
Instance details

Defined in Data.FiniteSupportStream

Subtractive a => Subtractive (FiniteSupportStream a) Source #

Subtractive instance for FiniteSupportStream.

Examples

Expand
>>> unsafeFromList [10, 20, 30] - unsafeFromList [1, 2]
[9,18,30,0,0,0,...
>>> unsafeFromList [1, 2, 3] - unsafeFromList [1, 2, 3]
[0,0,0,...
Instance details

Defined in Data.FiniteSupportStream

(Convolution a b c, Additive c) => Convolution (Stream a) (FiniteSupportStream b) c Source #

Convolution instance for Stream and FiniteSupportStream.

Instance details

Defined in Data.FiniteSupportStream

Methods

(|*|) :: Stream a -> FiniteSupportStream b -> c

(Convolution a b c, Additive c) => Convolution (FiniteSupportStream a) (Stream b) c Source #

Convolution instance for FiniteSupportStream and Stream.

Instance details

Defined in Data.FiniteSupportStream

Methods

(|*|) :: FiniteSupportStream a -> Stream b -> c

(Convolution a b c, Additive c) => Convolution (FiniteSupportStream a) (FiniteSupportStream b) c Source #

Convolution instance for FiniteSupportStream and FiniteSupportStream.

Instance details

Defined in Data.FiniteSupportStream

type DerivativeArg (FiniteSupportStream a) Source # 
Instance details

Defined in Numeric.InfBackprop.Core

type DerivativeCoarg (FiniteSupportStream a) Source # 
Instance details

Defined in Numeric.InfBackprop.Core

type DerivativeRoot (FiniteSupportStream a) Source # 
Instance details

Defined in Numeric.InfBackprop.Core

type DerivativeValue (FiniteSupportStream a) Source # 
Instance details

Defined in Numeric.InfBackprop.Core

type Dual (FiniteSupportStream a) Source # 
Instance details

Defined in Numeric.InfBackprop.Core

type Tangent (FiniteSupportStream a) Source # 
Instance details

Defined in Numeric.InfBackprop.Core

Basic functions

supportLength :: FiniteSupportStream a -> Natural Source #

Returns the length of the stream's support (the vector length after optimization). Trailing zeros are not counted in the support length.

Examples

Expand
>>> import GHC.Base (Int)
>>> supportLength $ unsafeFromList [0, 1, 2, 3]
4
>>> supportLength $ unsafeFromList [0, 1, 2, 3, 0, 0]
6

null :: FiniteSupportStream a -> Bool Source #

Checks if the finite support stream is empty.

Examples

Expand
>>> null $ unsafeFromList [0, 1, 2]
False
>>> null $ unsafeFromList []
True
>>> null $ unsafeFromList [0, 0, 0]
False

head :: Additive a => FiniteSupportStream a -> a Source #

Returns the first element of the finite support stream. If the stream is empty, it returns zero.

Examples

Expand
>>> import GHC.Base (Int, Bool)
>>> head $ unsafeFromList [1, 2, 3]
1
>>> head $ empty :: Int
0
>>> head $ empty :: Bool
False

tail :: FiniteSupportStream a -> FiniteSupportStream a Source #

Removes the first element of the finite support stream. If the stream is empty, it returns an empty stream.

Examples

Expand
>>> import GHC.Base (Int)
>>> tail $ unsafeFromList [1, 2, 3]
[2,3,0,0,0,...
>>> tail $ empty :: FiniteSupportStream Int
[0,0,0,...

cons :: a -> FiniteSupportStream a -> FiniteSupportStream a Source #

Adds an element to the front of the finite support stream. The inner array size is increased by exactly one. The head element of the array is not checked for zero elements.

Examples

Expand
>>> cons 42 (unsafeFromList [1, 2, 3])
[42,1,2,3,0,0,0,...
>>> toVector $ cons 0 empty
[0]

cons' :: (Additive a, Eq a) => a -> FiniteSupportStream a -> FiniteSupportStream a Source #

Adds an element to the front of the finite support stream. The inner array size is increased by exactly one if the head element is not zero. Otherwise, if the finite support stream is empty, the output is also the empty stream.

Examples

Expand
>>> cons' 42 (unsafeFromList [1, 2, 3])
[42,1,2,3,0,0,0,...
>>> toVector $ cons' 0 empty
[]

streamsConvolution :: Distributive a => Stream a -> FiniteSupportStream a -> a Source #

Convolves a stream with a finite support stream, producing a single value. The result is the sum of element-wise products.

This operation is equivalent to applying the stream as a linear functional to the finite support stream.

Examples

Expand
>>> import GHC.Base (Float, Int)
>>> import GHC.Real ((/))
>>> import Data.Stream (iterate, take, Stream)
>>> import Data.HashMap.Internal.Array (fromList')
>>> s1 = iterate (+1) 0 :: Stream Int
>>> Data.Stream.take 5 s1
[0,1,2,3,4]
>>> fss1 = unsafeFromList [0, 0, 1] :: FiniteSupportStream Int
>>> streamsConvolution s1 fss1
2
>>> s2 = iterate (/2) (1 :: Float) :: Stream Float
>>> Data.Stream.take 5 s2
[1.0,0.5,0.25,0.125,6.25e-2]
>>> fss2 = unsafeFromList $ Data.List.replicate 10 1 :: FiniteSupportStream Float
>>> streamsConvolution s2 fss2
1.9980469

finiteSupportStreamSum :: Additive a => FiniteSupportStream a -> a Source #

Computes the sum of all elements the finite support stream.

Examples

Expand
>>> import GHC.Base (Int)
>>> finiteSupportStreamSum $ unsafeFromList [1, 2, 3, 0] :: Int
6
>>> finiteSupportStreamSum empty :: Int
0

unsafeMap :: (a -> b) -> FiniteSupportStream a -> FiniteSupportStream b Source #

Lifts a function to work with finite support streams. This function applies the provided function to each element of the stream support. The function is usafe because it is not checked that the argument function maps zero to zero, which is expected.

Examples

Expand
>>> unsafeMap (*2) (MkFiniteSupportStream $ DV.fromList [0, 1, 2, 3])
[0,2,4,6,0,0,0,...
>>> unsafeMap (+1) (MkFiniteSupportStream $ DV.fromList [0, 1, 2, 3])
[1,2,3,4,0,0,0,...

Transformations

optimize :: (Eq a, Additive a) => FiniteSupportStream a -> FiniteSupportStream a Source #

Removes trailing elements of the finite support stream's inner array if they are zeros. The resulting stream is represented in its minimal form.

Examples

Expand
>>> optimize $ unsafeFromList [0, 1, 0, 3, 0, 0]
[0,1,0,3,0,0,0,...

unsafeZip :: (Additive a, Additive b) => FiniteSupportStream a -> FiniteSupportStream b -> FiniteSupportStream (a, b) Source #

Zips two finite support streams.

Examples

Expand
>>> import GHC.Base (Int)
>>> unsafeZip (unsafeFromList [1, 2, 3]) (unsafeFromList [4, 5]) :: FiniteSupportStream (Int, Int)
[(1,4),(2,5),(3,0),(0,0),(0,0),(0,0),...

unsafeZipWith Source #

Arguments

:: (a -> b -> c)

Binary operation for overlapping elements

-> (a -> c)

Operation for excess elements in first stream

-> (b -> c)

Operation for excess elements in second stream

-> FiniteSupportStream a 
-> FiniteSupportStream b 
-> FiniteSupportStream c 

Applies an element-wise binary operation to two streams.

Parameters: * f - Binary operation for overlapping elements * g - Unary operation for excess elements in first stream * h - Unary operation for excess elements in second stream

The resulting stream's length is the maximum of the input lengths, with trailing elements transformed by g or h as appropriate.

Examples

Expand
>>> import GHC.Base (Int)
>>> let xs = unsafeFromList [10, 20, 30]
>>> let ys = unsafeFromList [1,2]
>>> unsafeZipWith (-) id negate xs ys
[9,18,30,0,0,0,...

Construction

mkFiniteSupportStream' :: (Eq a, Additive a) => Vector a -> FiniteSupportStream a Source #

Creates a finite support stream from a array, removing trailing zeros in the tail. This is a constructor that ensures the minimal representation.

Examples

Expand
>>> import Data.Vector (fromList)
>>> toVector $ mkFiniteSupportStream' $ fromList [0, 1, 2, 3, 0]
[0,1,2,3]

empty :: FiniteSupportStream a Source #

Empty finite support stream. The stream contains only zeros.

Examples

Expand
>>> import GHC.Base (Int, Bool)
>>> empty :: FiniteSupportStream Int
[0,0,0,...
>>> empty :: FiniteSupportStream Bool
[False,False,False,...

singleton :: a -> FiniteSupportStream a Source #

Creates a finite support stream with exactly one element. The element is not checked for being zero.

Examples

Expand
>>> toVector $ singleton 42
[42]
>>> toVector $ singleton 0
[0]
>>> singleton 42
[42,0,0,0,...
>>> singleton 0
[0,0,0,...
>>> toVector $ singleton "a"
["a"]

singleton' :: (Additive a, Eq a) => a -> FiniteSupportStream a Source #

Creates a finite support stream with exactly one non-zero element if the provided element is not zero. Returns the empty stream otherwise.

Examples

Expand
>>> toVector $ singleton' 42
[42]
>>> toVector $ singleton' 0
[]
>>> singleton' 42
[42,0,0,0,...
>>> singleton' 0
[0,0,0,...

replicate :: Natural -> a -> FiniteSupportStream a Source #

Creates a finite support stream with a constant value along the support. It does not check whether the provided value is zero. In this case, the inner array contains only zeros.

Examples

Expand
>>> replicate 3 42
[42,42,42,0,0,0,...
>>> replicate 2 0
[0,0,0,...
>>> toVector $ replicate 2 0
[0,0]

replicate' :: (Additive a, Eq a) => Natural -> a -> FiniteSupportStream a Source #

Creates a finite support stream with a constant value along the support. It checks whether the provided value is zero. In this case, the inner array is empty.

Examples

Expand
>>> replicate' 3 42
[42,42,42,0,0,0,...
>>> replicate' 2 0
[0,0,0,...
>>> toVector $ replicate' 2 0
[]

unsafeFromList :: [a] -> FiniteSupportStream a Source #

Converts a finite list to a FiniteSupportStream. The list is assumed to be finite. Trailing zero elements are not checked, and the inner array is not trimmed.

Examples

Expand
>>> import GHC.Base (Int, Float, Bool(False, True))
>>> unsafeFromList [0, 1, 2, 3] :: FiniteSupportStream Int
[0,1,2,3,0,0,0,...
>>> unsafeFromList [0, 1, 2, 3] :: FiniteSupportStream Float
[0.0,1.0,2.0,3.0,0.0,0.0,0.0,...
>>> unsafeFromList [False, True]
[False,True,False,False,False,...

fromTuple :: forall input (length :: Nat) a. IndexedListLiterals input length a => input -> FiniteSupportStream a Source #

Converts a tuple into a FiniteSupportStream. Trailing zero elements are not checked, and the inner array is not trimmed.

Examples

Expand
>>> import GHC.Base (Int, Float, Bool(False, True))
>>> import GHC.Integer (Integer)
>>> fromTuple (0, 1, 2, 3) :: FiniteSupportStream Integer
[0,1,2,3,0,0,0,...
>>> fromTuple (0 :: Float, 1 :: Float, 2 :: Float, 3 :: Float) :: FiniteSupportStream Float
[0.0,1.0,2.0,3.0,0.0,0.0,0.0,...
>>> fromTuple (False, True) :: FiniteSupportStream Bool
[False,True,False,False,False,...

finiteSupportStreamBasis :: a -> a -> Natural -> FiniteSupportStream a Source #

Creates a finite support stream basis vector. The values of the zero and unit elements are provided as arguments.

Examples

Expand
>>> finiteSupportStreamBasis 0 1 3
[0,0,0,1,0,0,0,...

Conversion

multiplicativeAction :: Multiplicative a => Stream a -> FiniteSupportStream a -> FiniteSupportStream a Source #

Applies the multiplicative action of the stream on the finite support stream. The resulting stream's support length is less than or equal to the stream's support length in the argument.

Examples

Expand
>>> import GHC.Base (Int)
>>> multiplicativeAction (DS.fromList [0 ..]) (unsafeFromList [1, 1, 0, 1])
[0,1,0,3,0,0,0,...

takeArray :: Additive a => Natural -> FiniteSupportStream a -> Vector a Source #

Takes the first n elements of the finite support stream in the form of an array. If n is greater than the length of the stream, the result is padded with zeros. The resulting array is not trimmed.

Examples

Expand
>>> takeArray 5 $ unsafeFromList [1, 2, 3]
[1,2,3,0,0]

takeList :: Additive a => Natural -> FiniteSupportStream a -> [a] Source #

Takes the first n elements of the finite support stream in the form of a list. If n is greater than the length of the stream, the result is padded with zeros.

Examples

Expand
>>> takeList 5 $ unsafeFromList [1, 2, 3]
[1,2,3,0,0]

toList :: FiniteSupportStream a -> [a] Source #

Converts a finite support stream to a finite list. The resulting list includes all elements of the stream, including any trailing zeros.

Examples

Expand
>>> toList $ unsafeFromList [1, 2, 3]
[1,2,3]
>>> toList $ unsafeFromList [1, 2, 3, 0]
[1,2,3,0]

toInfiniteList :: Additive a => FiniteSupportStream a -> [a] Source #

Converts a finite support stream to an infinite list. The resulting list contains all elements of the stream, followed by an infinite sequence of zeros.

Examples

Expand
>>> import Data.List (take)
>>> take 5 $ toInfiniteList $ unsafeFromList [1, 2, 3]
[1,2,3,0,0]

Fold tools

foldlWithStream :: Foldable t => (b -> a -> c -> b) -> b -> t a -> Stream c -> b Source #

Lazy left fold over a foldable type t and a Stream.

Examples

Expand
>>> foldlWithStream (\acc x y -> acc + x * y) 0 (unsafeFromList [1,1,1]) (DS.iterate (+1) 0)
3

foldlWithStream' :: Foldable t => (b -> a -> c -> b) -> b -> t a -> Stream c -> b Source #

Strinct left fold over a foldable type t and a Stream.

Examples

Expand
>>> foldlWithStream (\acc x y -> acc + x * y) 0 (unsafeFromList [1,1,1]) (DS.iterate (+1) 0)
3