Safe Haskell | Safe |
---|---|
Language | Haskell2010 |
- data SF k v = SF !(Map k v) !v
- constant :: a -> SF k a
- step :: k -> v -> v -> SF k v
- fromList :: Ord k => [(k, v)] -> v -> SF k v
- normalise :: Eq v => SF k v -> SF k v
- (!) :: Ord k => SF k v -> k -> v
- values :: SF k v -> [v]
- toDense :: SF a b -> SF a b
- fromDense :: Ord a => (a -> Maybe a) -> SF a b -> SF a b
- showSF :: (Show a, Show b) => SF a b -> String
- putSF :: (Show a, Show b) => SF a b -> IO ()
Step Function
Examples
>>>
let heaviside = step 0 (-1) 1 :: SF Int Int
>>>
putSF heaviside
\x -> if | x <= 0 -> -1 | otherwise -> 1
>>>
map (heaviside !) [-3, 0, 4]
[-1,-1,1]
Step function. Piecewise constant function, having finitely many pieces. See https://en.wikipedia.org/wiki/Step_function.
Note: this variant has discrete domain.
It's enough to have only <
$, without ≤
, as there is a next element
without any others in between.
describes a piecewise constant function \(f : k \to v\):SF
(fromList [(k1, v1), (k2, v2)]) v3 :: SF
k v
\[ f\,x = \begin{cases} v_1, \quad x \le k_1 \newline v_2, \quad k_1 < x \le k_2 \newline v_3, \quad k_2 < x \end{cases} \]
or as you would write in Haskell
f x | x <= k1 = v1 | x <= k2 = v2 | otherwise = v3
Constructor is exposed as you cannot construct non-valid SF
.
Show2 SF Source # | |
Ord k => Monad (SF k) Source # | |
Functor (SF k) Source # | |
Ord k => Applicative (SF k) Source # |
|
Foldable (SF k) Source # | |
Traversable (SF k) Source # | |
Show k => Show1 (SF k) Source # | |
(Eq v, Eq k) => Eq (SF k v) Source # | |
(Ord v, Ord k) => Ord (SF k v) Source # | |
(Show k, Show v) => Show (SF k v) Source # | |
(Ord k, Semigroup v) => Semigroup (SF k v) Source # | Piecewise
|
(Ord k, Monoid v) => Monoid (SF k v) Source # | |
(Ord k, Arbitrary k, Arbitrary v) => Arbitrary (SF k v) Source # | |
(NFData k, NFData v) => NFData (SF k v) Source # | |
Construction
step :: k -> v -> v -> SF k v Source #
Step function.
.step
k v1 v2 = \ x -> if x < k then v1 else v2
>>>
putSF $ step 1 2 3
\x -> if | x <= 1 -> 2 | otherwise -> 3
fromList :: Ord k => [(k, v)] -> v -> SF k v Source #
Create function from list of cases and default value.
>>>
let f = fromList [(1,2),(3,4)] 5
>>>
putSF f
\x -> if | x <= 1 -> 2 | x <= 3 -> 4 | otherwise -> 5
>>>
map (f !) [0..10]
[2,2,4,4,5,5,5,5,5,5,5]
Normalisation
Operators
Conversions
toDense :: SF a b -> SF a b Source #
Convert from discrete variant to more "dense"
>>>
SF.putSF $ toDense $ fromList [(1,2),(3,4)] 5
\x -> if | x <= 1 -> 2 | x <= 3 -> 4 | otherwise -> 5
Convert from "dense" variant. <= k
pieces will be converted to <
.
There might be less pieces in the ressult succ
kSF
, than in the original.
>>>
let f = SF.fromList [(SF.Open 1,2),(SF.Closed 3,4),(SF.Open 4,5)] 6
>>>
SF.putSF f
\x -> if | x < 1 -> 2 | x <= 3 -> 4 | x < 4 -> 5 | otherwise -> 6
>>>
putSF $ fromDense (Just . pred) f
\x -> if | x <= 0 -> 2 | x <= 3 -> 4 | otherwise -> 6