-- |
-- Module      :  ListQuantizer
-- Copyright   :  (c) OleksandrZhabenko 2023
-- License     :  MIT
-- Stability   :  Experimental
-- Maintainer  :  oleksandr.zhabenko@yahoo.com
--
-- A module to provide the extended variants to convert a list with
-- some values to another one with the values from the pre-defined another list. Similar to 
-- the measurement of the quantum state observables with the discrete spectrum. Contrary to
-- TwoQuantizer module, the results  in every function  here depend not just on the two values, 
-- which the point is located in between, but on the whole list. Defined for just positive real numbers of 'Double' type.


{-# LANGUAGE NoImplicitPrelude #-}

module ListQuantizer where

import GHC.Base
import GHC.List
import GHC.Real
import GHC.Float
import GHC.Num
import Data.Maybe
import qualified TwoQuantizer as Q (meanF2)
import Data.MinMax (minMax11)

-- | A better suited variant for 'FoldableQuantizer.round2G' for lists. 
round2GL
 :: (Ord a) => Bool -- ^ If 'True' then the function rounds the result in the ambiguous situation to the greater value. The ambigous situation is defined by the second argument.
 -> ([a] -> a -> Ordering) 
 -> [a] 
 -> a 
 -> Maybe a -- ^ The @a@ value (in 'Just' case) can be equal just to the one of the two first @a@ arguments.
round2GL :: forall a.
Ord a =>
Bool -> ([a] -> a -> Ordering) -> [a] -> a -> Maybe a
round2GL Bool
bool [a] -> a -> Ordering
f [a]
xs a
z 
 | a
z forall a. Eq a => a -> [a] -> Bool
`elem` [a]
xs = forall a. a -> Maybe a
Just a
z
 | forall a. [a] -> Int
length [a]
xs forall a. Ord a => a -> a -> Bool
< Int
2 = forall a. Maybe a
Nothing
 | a
z forall a. Ord a => a -> a -> Bool
< a
x Bool -> Bool -> Bool
|| a
z forall a. Ord a => a -> a -> Bool
> a
y = forall a. Maybe a
Nothing
 | forall a. [a] -> Bool
null [a]
ts = forall a. a -> Maybe a
Just a
u
 | forall a. [a] -> Bool
null [a]
us = forall a. a -> Maybe a
Just a
t
 | Bool
otherwise = forall a. a -> Maybe a
Just (case [a] -> a -> Ordering
f [a]
xs a
z of { Ordering
GT -> a
u; Ordering
LT -> a
t; Ordering
EQ -> if Bool
bool then a
u else a
t })
     where (a
x, a
y) = forall a. HasCallStack => Maybe a -> a
fromJust forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a (t :: * -> *).
(Ord a, InsertLeft t a, Monoid (t a)) =>
t a -> Maybe (a, a)
minMax11 forall a b. (a -> b) -> a -> b
$ [a]
xs
           ([a]
ts,[a]
us) = forall a. (a -> Bool) -> [a] -> ([a], [a])
span (forall a. Ord a => a -> a -> Bool
<a
z) [a]
xs
           t :: a
t = forall a. [a] -> a
last [a]
ts
           u :: a
u = forall a. [a] -> a
head [a]
us

foldableQuantizerGL 
 :: (Ord a, Floating a) => Bool -- ^ If 'True' then the function rounds the result in the ambiguous situation to the greater value. The ambigous situation is defined by the second argument.
 -> ([a] -> a -> Ordering) 
 -> [a] 
 -> [a]
 -> [a]
foldableQuantizerGL :: forall a.
(Ord a, Floating a) =>
Bool -> ([a] -> a -> Ordering) -> [a] -> [a] -> [a]
foldableQuantizerGL Bool
ctrl [a] -> a -> Ordering
f [a]
needs [a]
xs = forall a b. (a -> b) -> [a] -> [b]
map (forall a. HasCallStack => Maybe a -> a
fromJust forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a.
Ord a =>
Bool -> ([a] -> a -> Ordering) -> [a] -> a -> Maybe a
round2GL Bool
ctrl [a] -> a -> Ordering
f [a]
needs) [a]
ys
  where k :: a
k = forall a. Floating a => [a] -> a -> a -> a
Q.meanF2 [a]
needs a
0 a
0 forall a. Fractional a => a -> a -> a
/ forall a. Floating a => [a] -> a -> a -> a
Q.meanF2 [a]
xs a
0 a
0
        ys :: [a]
ys = forall a b. (a -> b -> b) -> b -> [a] -> b
foldr (\a
t [a]
ts -> a
t forall a. Num a => a -> a -> a
* a
k forall a. a -> [a] -> [a]
: [a]
ts) [] [a]
xs

round2GML 
 :: (Ord a, Monad m) => Bool -- ^ If 'True' then the function rounds the result in the ambiguous situation to the greater value. The ambigous situation is defined by the second argument.
 -> ([a] -> a -> m Ordering) 
 -> [a] 
 -> a 
 -> m (Maybe a)
round2GML :: forall a (m :: * -> *).
(Ord a, Monad m) =>
Bool -> ([a] -> a -> m Ordering) -> [a] -> a -> m (Maybe a)
round2GML Bool
bool [a] -> a -> m Ordering
f [a]
xs a
z 
 | a
z forall a. Eq a => a -> [a] -> Bool
`elem` [a]
xs = forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ a
z
 | forall a. [a] -> Int
length [a]
xs forall a. Ord a => a -> a -> Bool
< Int
2 = forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
 | a
z forall a. Ord a => a -> a -> Bool
< a
x Bool -> Bool -> Bool
|| a
z forall a. Ord a => a -> a -> Bool
> a
y = forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
 | forall a. [a] -> Bool
null [a]
ts = forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
u
 | forall a. [a] -> Bool
null [a]
us = forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
t
 | Bool
otherwise = do
     Ordering
q <- [a] -> a -> m Ordering
f [a]
xs a
z
     case Ordering
q of { Ordering
GT -> forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
u; Ordering
LT -> forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
t; Ordering
EQ -> forall (m :: * -> *) a. Monad m => a -> m a
return (if Bool
bool then Maybe a
u else Maybe a
t)}
   where (a
x, a
y) = forall a. HasCallStack => Maybe a -> a
fromJust forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a (t :: * -> *).
(Ord a, InsertLeft t a, Monoid (t a)) =>
t a -> Maybe (a, a)
minMax11 forall a b. (a -> b) -> a -> b
$ [a]
xs
         ([a]
ts,[a]
us) = forall a. (a -> Bool) -> [a] -> ([a], [a])
span (forall a. Ord a => a -> a -> Bool
<a
z) [a]
xs
         t :: Maybe a
t = if forall a. [a] -> Bool
null [a]
ts then forall a. Maybe a
Nothing else forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. [a] -> a
last forall a b. (a -> b) -> a -> b
$ [a]
ts
         u :: Maybe a
u = if forall a. [a] -> Bool
null [a]
us then forall a. Maybe a
Nothing else forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. [a] -> a
head forall a b. (a -> b) -> a -> b
$ [a]
us

foldableQuantizerGML 
 :: (Ord a, Floating a, Monad m) => Bool -- ^ If 'True' then the function rounds the result in the ambiguous situation to the greater value. The ambigous situation is defined by the second argument.
 -> ([a] -> a -> m Ordering) 
 -> [a] 
 -> [a] 
 -> m [a]
foldableQuantizerGML :: forall a (m :: * -> *).
(Ord a, Floating a, Monad m) =>
Bool -> ([a] -> a -> m Ordering) -> [a] -> [a] -> m [a]
foldableQuantizerGML Bool
ctrl [a] -> a -> m Ordering
f [a]
needs [a]
xs = forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. HasCallStack => Maybe a -> a
fromJust forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a (m :: * -> *).
(Ord a, Monad m) =>
Bool -> ([a] -> a -> m Ordering) -> [a] -> a -> m (Maybe a)
round2GML Bool
ctrl [a] -> a -> m Ordering
f [a]
needs) [a]
ys
  where k :: a
k = forall a. Floating a => [a] -> a -> a -> a
Q.meanF2 [a]
needs a
0 a
0  forall a. Fractional a => a -> a -> a
/ forall a. Floating a => [a] -> a -> a -> a
Q.meanF2 [a]
xs a
0 a
0
        ys :: [a]
ys = forall a b. (a -> b -> b) -> b -> [a] -> b
foldr (\a
u [a]
us -> a
u forall a. Num a => a -> a -> a
* a
k forall a. a -> [a] -> [a]
: [a]
us) [] [a]
xs