{-# LANGUAGE FlexibleInstances    #-}
{-# LANGUAGE ScopedTypeVariables  #-}
{-# LANGUAGE UndecidableInstances #-}

-- | This module defines commonly useful functions that are not specifically related with vectors and matrices.
module FPNLA.Utils (
    iif,
    mapPair,
    splitSlice,

    Truncable(..),

    prevEnum,
    nextEnum,
    enumerate

) where

import Data.Complex (Complex ((:+)))

-- | Takes two functions @f@ and @g@, and a pair, and applies @f@ to the first element and @g@ to the second element of the pair.
mapPair :: (a -> b) -- ^ @f@
    -> (c -> d) -- ^ @g@
    -> (a, c) -- ^ Input pair @(a,b)@
    -> (b, d) -- ^ Output pair @(f a, g b)@
mapPair :: forall a b c d. (a -> b) -> (c -> d) -> (a, c) -> (b, d)
mapPair a -> b
f c -> d
g (a
a, c
c) = (a -> b
f a
a,  c -> d
g c
c)

-- | It's equivalent to @if cond then e1 else e2@, but defined as a function.
iif :: Bool -- ^ @cond@
    -> a -- ^ @e1@
    -> a -- ^ @e2@
    -> a -- ^ @if cond then e1 else e2@
iif :: forall a. Bool -> a -> a -> a
iif Bool
cond a
expTrue a
expFalse = if Bool
cond then a
expTrue else a
expFalse


-- | Splits a list into pieces of size @n@.
-- The last piece could be smaller if the length of the list is not multiple of @n@.
splitSlice :: Int -- ^ @n@
    -> [a] -- ^ The input list
    -> [[a]] -- ^ The input list divided in pieces of size @n@ 
splitSlice :: forall a. Int -> [a] -> [[a]]
splitSlice Int
0 [a]
_ = [Char] -> [[a]]
forall a. HasCallStack => [Char] -> a
error [Char]
"n must be greater than 0"
splitSlice Int
_ [] = []
splitSlice Int
n [a]
ls = [a]
sl [a] -> [[a]] -> [[a]]
forall a. a -> [a] -> [a]
: Int -> [a] -> [[a]]
forall a. Int -> [a] -> [[a]]
splitSlice Int
n [a]
sls
    where ([a]
sl, [a]
sls) = Int -> [a] -> ([a], [a])
forall a. Int -> [a] -> ([a], [a])
splitAt Int
n [a]
ls

-- | This class is used to compare two float point numbers.
-- With float point numbers, two different computations that theoretically returns the same value in fact returns different values. Even if the difference is so small that is ignorable, if we test using equality the test fail.
-- For this, instead to check for equality we define that two float point numbers are the same if the distance between them is less than some epsilon value.
class Truncable a where
    -- | Check if the distance between two float point values is less than some epsilon value.
    inEpsilonRange :: Real e => e -- ^ Epsilon
        -> a -- ^ A float point value
        -> a -- ^ Another float point value
        -> Bool

-- | We provide a default instance of 'Truncable' for any data type that has 'Floating' and 'RealFrac' instances.
instance (Floating n, RealFrac n) => Truncable n where
    inEpsilonRange :: forall e. Real e => e -> n -> n -> Bool
inEpsilonRange e
epsilon n
n1 n
n2 = Integer
expN1 Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
expN2 Bool -> Bool -> Bool
&& n -> n
forall a. Num a => a -> a
abs(n
mantN1 n -> n -> n
forall a. Num a => a -> a -> a
- n
mantN2) n -> n -> Bool
forall a. Ord a => a -> a -> Bool
<= n
eps
        where
            eps :: n
eps = Rational -> n
forall a. Fractional a => Rational -> a
fromRational (Rational -> n) -> Rational -> n
forall a b. (a -> b) -> a -> b
$ e -> Rational
forall a. Real a => a -> Rational
toRational e
epsilon
            shift :: a -> a -> a
shift a
n a
d1 = a
n a -> a -> a
forall a. Num a => a -> a -> a
* (a
10 a -> a -> a
forall a. Floating a => a -> a -> a
** a -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral a
d1)
            getExponent :: a -> a
getExponent a
r
                    | a
r a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
0 = a
0
                    | a
r a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
0 = a -> a
getExponent (a -> a
forall a. Num a => a -> a
abs a
r)
                    | a
r a -> a -> Bool
forall a. Ord a => a -> a -> Bool
> a
10 = a
1 a -> a -> a
forall a. Num a => a -> a -> a
+ a -> a
getExponent (a
r a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
10)
                    | a
r a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
1 = (-a
1) a -> a -> a
forall a. Num a => a -> a -> a
+ a -> a
getExponent (a
r a -> a -> a
forall a. Num a => a -> a -> a
* a
10)
                    | Bool
otherwise = a
0
            Integer
expN1 :: Integer = n -> Integer
forall {a} {a}. (Num a, Ord a, Fractional a) => a -> a
getExponent n
n1
            Integer
expN2 :: Integer = n -> Integer
forall {a} {a}. (Num a, Ord a, Fractional a) => a -> a
getExponent n
n2
            mantN1 :: n
mantN1 = n -> Integer -> n
forall {a} {a}. (Floating a, Integral a) => a -> a -> a
shift n
n1 (-Integer
expN1)
            mantN2 :: n
mantN2 = n -> Integer -> n
forall {a} {a}. (Floating a, Integral a) => a -> a -> a
shift n
n2 (-Integer
expN2)

-- | We define a specific instance of 'Truncable' for the 'Complex' data type.
instance (RealFrac n, Truncable n) => Truncable (Complex n) where
    inEpsilonRange :: forall e. Real e => e -> Complex n -> Complex n -> Bool
inEpsilonRange e
epsilon (n
r1:+n
i1) (n
r2:+n
i2) = e -> n -> n -> Bool
forall e. Real e => e -> n -> n -> Bool
forall a e. (Truncable a, Real e) => e -> a -> a -> Bool
inEpsilonRange e
epsilon n
r1 n
r2 Bool -> Bool -> Bool
&& e -> n -> n -> Bool
forall e. Real e => e -> n -> n -> Bool
forall a e. (Truncable a, Real e) => e -> a -> a -> Bool
inEpsilonRange e
epsilon n
i1 n
i2


-- | Returns the predecessor of a enumerated value.
-- The operation is cyclic so, the predecessor of the first value is the last value.
prevEnum :: (Eq a, Bounded a, Enum a) => a -> a
prevEnum :: forall a. (Eq a, Bounded a, Enum a) => a -> a
prevEnum a
a | a
a a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
forall a. Bounded a => a
minBound = a
forall a. Bounded a => a
maxBound
           | Bool
otherwise = a -> a
forall a. Enum a => a -> a
pred a
a

-- | Returns the successor of a enumerated value.
-- The operation is cyclic so, the successor of the last value is the first value.
nextEnum :: (Eq a, Bounded a, Enum a) => a -> a
nextEnum :: forall a. (Eq a, Bounded a, Enum a) => a -> a
nextEnum a
a | a
a a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
forall a. Bounded a => a
maxBound = a
forall a. Bounded a => a
minBound
           | Bool
otherwise = a -> a
forall a. Enum a => a -> a
succ a
a

-- | Returls a list containing all the values of an enumerated.
enumerate :: forall a. (Enum a, Bounded a) => [a]
enumerate :: forall a. (Enum a, Bounded a) => [a]
enumerate = (Int -> a) -> [Int] -> [a]
forall a b. (a -> b) -> [a] -> [b]
map Int -> a
forall a. Enum a => Int -> a
toEnum [a -> Int
forall a. Enum a => a -> Int
fromEnum (a
forall a. Bounded a => a
minBound :: a) .. a -> Int
forall a. Enum a => a -> Int
fromEnum (a
forall a. Bounded a => a
maxBound :: a)]