{-# LANGUAGE CPP #-}
{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE DeriveDataTypeable #-}
#if __GLASGOW_HASKELL__ >= 704
{-# LANGUAGE DeriveGeneric #-}
#endif
{-# OPTIONS_HADDOCK not-home #-}
module Numeric.Interval.NonEmpty.Internal
( Interval(..)
, (...)
, interval
, whole
, singleton
, member
, notMember
, elem
, notElem
, inf
, sup
, singular
, width
, midpoint
, distance
, intersection
, hull
, bisect
, bisectIntegral
, magnitude
, mignitude
, contains
, isSubsetOf
, certainly, (<!), (<=!), (==!), (/=!), (>=!), (>!)
, possibly, (<?), (<=?), (==?), (/=?), (>=?), (>?)
, clamp
, inflate, deflate
, scale, symmetric
, idouble
, ifloat
, iquot
, irem
, idiv
, imod
) where
import Control.Exception as Exception
import Data.Data
#if __GLASGOW_HASKELL__ >= 704
import GHC.Generics
#endif
import Prelude hiding (null, elem, notElem)
import qualified Data.Semigroup
data Interval a = I !a !a deriving
( Interval a -> Interval a -> Bool
(Interval a -> Interval a -> Bool)
-> (Interval a -> Interval a -> Bool) -> Eq (Interval a)
forall a. Eq a => Interval a -> Interval a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Interval a -> Interval a -> Bool
$c/= :: forall a. Eq a => Interval a -> Interval a -> Bool
== :: Interval a -> Interval a -> Bool
$c== :: forall a. Eq a => Interval a -> Interval a -> Bool
Eq, Eq (Interval a)
Eq (Interval a)
-> (Interval a -> Interval a -> Ordering)
-> (Interval a -> Interval a -> Bool)
-> (Interval a -> Interval a -> Bool)
-> (Interval a -> Interval a -> Bool)
-> (Interval a -> Interval a -> Bool)
-> (Interval a -> Interval a -> Interval a)
-> (Interval a -> Interval a -> Interval a)
-> Ord (Interval a)
Interval a -> Interval a -> Bool
Interval a -> Interval a -> Ordering
Interval a -> Interval a -> Interval a
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall a. Ord a => Eq (Interval a)
forall a. Ord a => Interval a -> Interval a -> Bool
forall a. Ord a => Interval a -> Interval a -> Ordering
forall a. Ord a => Interval a -> Interval a -> Interval a
min :: Interval a -> Interval a -> Interval a
$cmin :: forall a. Ord a => Interval a -> Interval a -> Interval a
max :: Interval a -> Interval a -> Interval a
$cmax :: forall a. Ord a => Interval a -> Interval a -> Interval a
>= :: Interval a -> Interval a -> Bool
$c>= :: forall a. Ord a => Interval a -> Interval a -> Bool
> :: Interval a -> Interval a -> Bool
$c> :: forall a. Ord a => Interval a -> Interval a -> Bool
<= :: Interval a -> Interval a -> Bool
$c<= :: forall a. Ord a => Interval a -> Interval a -> Bool
< :: Interval a -> Interval a -> Bool
$c< :: forall a. Ord a => Interval a -> Interval a -> Bool
compare :: Interval a -> Interval a -> Ordering
$ccompare :: forall a. Ord a => Interval a -> Interval a -> Ordering
$cp1Ord :: forall a. Ord a => Eq (Interval a)
Ord
, Typeable (Interval a)
DataType
Constr
Typeable (Interval a)
-> (forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Interval a -> c (Interval a))
-> (forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Interval a))
-> (Interval a -> Constr)
-> (Interval a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (Interval a)))
-> (forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Interval a)))
-> ((forall b. Data b => b -> b) -> Interval a -> Interval a)
-> (forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Interval a -> r)
-> (forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Interval a -> r)
-> (forall u. (forall d. Data d => d -> u) -> Interval a -> [u])
-> (forall u.
Int -> (forall d. Data d => d -> u) -> Interval a -> u)
-> (forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Interval a -> m (Interval a))
-> (forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Interval a -> m (Interval a))
-> (forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Interval a -> m (Interval a))
-> Data (Interval a)
Interval a -> DataType
Interval a -> Constr
(forall d. Data d => c (t d)) -> Maybe (c (Interval a))
(forall b. Data b => b -> b) -> Interval a -> Interval a
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Interval a -> c (Interval a)
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Interval a)
forall a. Data a => Typeable (Interval a)
forall a. Data a => Interval a -> DataType
forall a. Data a => Interval a -> Constr
forall a.
Data a =>
(forall b. Data b => b -> b) -> Interval a -> Interval a
forall a u.
Data a =>
Int -> (forall d. Data d => d -> u) -> Interval a -> u
forall a u.
Data a =>
(forall d. Data d => d -> u) -> Interval a -> [u]
forall a r r'.
Data a =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Interval a -> r
forall a r r'.
Data a =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Interval a -> r
forall a (m :: * -> *).
(Data a, Monad m) =>
(forall d. Data d => d -> m d) -> Interval a -> m (Interval a)
forall a (m :: * -> *).
(Data a, MonadPlus m) =>
(forall d. Data d => d -> m d) -> Interval a -> m (Interval a)
forall a (c :: * -> *).
Data a =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Interval a)
forall a (c :: * -> *).
Data a =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Interval a -> c (Interval a)
forall a (t :: * -> *) (c :: * -> *).
(Data a, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (Interval a))
forall a (t :: * -> * -> *) (c :: * -> *).
(Data a, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Interval a))
forall a.
Typeable a
-> (forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> Interval a -> u
forall u. (forall d. Data d => d -> u) -> Interval a -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Interval a -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Interval a -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Interval a -> m (Interval a)
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Interval a -> m (Interval a)
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Interval a)
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Interval a -> c (Interval a)
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (Interval a))
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Interval a))
$cI :: Constr
$tInterval :: DataType
gmapMo :: (forall d. Data d => d -> m d) -> Interval a -> m (Interval a)
$cgmapMo :: forall a (m :: * -> *).
(Data a, MonadPlus m) =>
(forall d. Data d => d -> m d) -> Interval a -> m (Interval a)
gmapMp :: (forall d. Data d => d -> m d) -> Interval a -> m (Interval a)
$cgmapMp :: forall a (m :: * -> *).
(Data a, MonadPlus m) =>
(forall d. Data d => d -> m d) -> Interval a -> m (Interval a)
gmapM :: (forall d. Data d => d -> m d) -> Interval a -> m (Interval a)
$cgmapM :: forall a (m :: * -> *).
(Data a, Monad m) =>
(forall d. Data d => d -> m d) -> Interval a -> m (Interval a)
gmapQi :: Int -> (forall d. Data d => d -> u) -> Interval a -> u
$cgmapQi :: forall a u.
Data a =>
Int -> (forall d. Data d => d -> u) -> Interval a -> u
gmapQ :: (forall d. Data d => d -> u) -> Interval a -> [u]
$cgmapQ :: forall a u.
Data a =>
(forall d. Data d => d -> u) -> Interval a -> [u]
gmapQr :: (r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Interval a -> r
$cgmapQr :: forall a r r'.
Data a =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Interval a -> r
gmapQl :: (r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Interval a -> r
$cgmapQl :: forall a r r'.
Data a =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Interval a -> r
gmapT :: (forall b. Data b => b -> b) -> Interval a -> Interval a
$cgmapT :: forall a.
Data a =>
(forall b. Data b => b -> b) -> Interval a -> Interval a
dataCast2 :: (forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Interval a))
$cdataCast2 :: forall a (t :: * -> * -> *) (c :: * -> *).
(Data a, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Interval a))
dataCast1 :: (forall d. Data d => c (t d)) -> Maybe (c (Interval a))
$cdataCast1 :: forall a (t :: * -> *) (c :: * -> *).
(Data a, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (Interval a))
dataTypeOf :: Interval a -> DataType
$cdataTypeOf :: forall a. Data a => Interval a -> DataType
toConstr :: Interval a -> Constr
$ctoConstr :: forall a. Data a => Interval a -> Constr
gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Interval a)
$cgunfold :: forall a (c :: * -> *).
Data a =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Interval a)
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Interval a -> c (Interval a)
$cgfoldl :: forall a (c :: * -> *).
Data a =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Interval a -> c (Interval a)
$cp1Data :: forall a. Data a => Typeable (Interval a)
Data
, Typeable
#if __GLASGOW_HASKELL__ >= 704
, (forall x. Interval a -> Rep (Interval a) x)
-> (forall x. Rep (Interval a) x -> Interval a)
-> Generic (Interval a)
forall x. Rep (Interval a) x -> Interval a
forall x. Interval a -> Rep (Interval a) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a x. Rep (Interval a) x -> Interval a
forall a x. Interval a -> Rep (Interval a) x
$cto :: forall a x. Rep (Interval a) x -> Interval a
$cfrom :: forall a x. Interval a -> Rep (Interval a) x
Generic
#if __GLASGOW_HASKELL__ >= 706
, (forall a. Interval a -> Rep1 Interval a)
-> (forall a. Rep1 Interval a -> Interval a) -> Generic1 Interval
forall a. Rep1 Interval a -> Interval a
forall a. Interval a -> Rep1 Interval a
forall k (f :: k -> *).
(forall (a :: k). f a -> Rep1 f a)
-> (forall (a :: k). Rep1 f a -> f a) -> Generic1 f
$cto1 :: forall a. Rep1 Interval a -> Interval a
$cfrom1 :: forall a. Interval a -> Rep1 Interval a
Generic1
#endif
#endif
)
instance Ord a => Data.Semigroup.Semigroup (Interval a) where
<> :: Interval a -> Interval a -> Interval a
(<>) = Interval a -> Interval a -> Interval a
forall a. Ord a => Interval a -> Interval a -> Interval a
hull
infix 3 ...
negInfinity :: Fractional a => a
negInfinity :: a
negInfinity = (-a
1)a -> a -> a
forall a. Fractional a => a -> a -> a
/a
0
{-# INLINE negInfinity #-}
posInfinity :: Fractional a => a
posInfinity :: a
posInfinity = a
1a -> a -> a
forall a. Fractional a => a -> a -> a
/a
0
{-# INLINE posInfinity #-}
signum' :: (Ord a, Num a) => a -> Ordering
signum' :: a -> Ordering
signum' a
x = a -> a -> Ordering
forall a. Ord a => a -> a -> Ordering
compare a
x a
0
periodic :: (Num a, Ord a) => a -> Interval a -> (a -> Ordering) -> (a -> a) -> Interval a -> Interval a
periodic :: a
-> Interval a
-> (a -> Ordering)
-> (a -> a)
-> Interval a
-> Interval a
periodic a
p Interval a
r a -> Ordering
_ a -> a
_ Interval a
x | Interval a -> a
forall a. Num a => Interval a -> a
width Interval a
x a -> a -> Bool
forall a. Ord a => a -> a -> Bool
> a
p = Interval a
r
periodic a
_ Interval a
r a -> Ordering
d a -> a
f (I a
a a
b) = Interval a -> Ordering -> Ordering -> a -> a -> Interval a
forall a.
Ord a =>
Interval a -> Ordering -> Ordering -> a -> a -> Interval a
periodic' Interval a
r (a -> Ordering
d a
a) (a -> Ordering
d a
b) (a -> a
f a
a) (a -> a
f a
b)
periodic' :: (Ord a) => Interval a -> Ordering -> Ordering -> a -> a -> Interval a
periodic' :: Interval a -> Ordering -> Ordering -> a -> a -> Interval a
periodic' Interval a
r Ordering
GT Ordering
GT a
a a
b | a
a a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
b = a -> a -> Interval a
forall a. a -> a -> Interval a
I a
a a
b
| Bool
otherwise = Interval a
r
periodic' Interval a
r Ordering
LT Ordering
LT a
a a
b | a
a a -> a -> Bool
forall a. Ord a => a -> a -> Bool
>= a
b = a -> a -> Interval a
forall a. a -> a -> Interval a
I a
b a
a
| Bool
otherwise = Interval a
r
periodic' Interval a
r Ordering
GT Ordering
_ a
a a
b = a -> a -> Interval a
forall a. a -> a -> Interval a
I (a -> a -> a
forall a. Ord a => a -> a -> a
min a
a a
b) (Interval a -> a
forall a. Interval a -> a
sup Interval a
r)
periodic' Interval a
r Ordering
LT Ordering
_ a
a a
b = a -> a -> Interval a
forall a. a -> a -> Interval a
I (Interval a -> a
forall a. Interval a -> a
inf Interval a
r) (a -> a -> a
forall a. Ord a => a -> a -> a
max a
a a
b)
periodic' Interval a
r Ordering
EQ Ordering
GT a
a a
b | a
a a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
b = a -> a -> Interval a
forall a. a -> a -> Interval a
I a
a a
b
| Bool
otherwise = Interval a
r
periodic' Interval a
r Ordering
EQ Ordering
LT a
a a
b | a
a a -> a -> Bool
forall a. Ord a => a -> a -> Bool
> a
b = a -> a -> Interval a
forall a. a -> a -> Interval a
I a
b a
a
| Bool
otherwise = Interval a
r
periodic' Interval a
_ Ordering
_ Ordering
_ a
a a
b = a
a a -> a -> Interval a
forall a. Ord a => a -> a -> Interval a
... a
b
(...) :: Ord a => a -> a -> Interval a
a
a ... :: a -> a -> Interval a
... a
b
| a
a a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
b = a -> a -> Interval a
forall a. a -> a -> Interval a
I a
a a
b
| Bool
otherwise = a -> a -> Interval a
forall a. a -> a -> Interval a
I a
b a
a
{-# INLINE (...) #-}
interval :: Ord a => a -> a -> Maybe (Interval a)
interval :: a -> a -> Maybe (Interval a)
interval a
a a
b
| a
a a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
b = Interval a -> Maybe (Interval a)
forall a. a -> Maybe a
Just (Interval a -> Maybe (Interval a))
-> Interval a -> Maybe (Interval a)
forall a b. (a -> b) -> a -> b
$ a -> a -> Interval a
forall a. a -> a -> Interval a
I a
a a
b
| Bool
otherwise = Maybe (Interval a)
forall a. Maybe a
Nothing
whole :: Fractional a => Interval a
whole :: Interval a
whole = a -> a -> Interval a
forall a. a -> a -> Interval a
I a
forall a. Fractional a => a
negInfinity a
forall a. Fractional a => a
posInfinity
{-# INLINE whole #-}
singleton :: a -> Interval a
singleton :: a -> Interval a
singleton a
a = a -> a -> Interval a
forall a. a -> a -> Interval a
I a
a a
a
{-# INLINE singleton #-}
inf :: Interval a -> a
inf :: Interval a -> a
inf (I a
a a
_) = a
a
{-# INLINE inf #-}
sup :: Interval a -> a
sup :: Interval a -> a
sup (I a
_ a
b) = a
b
{-# INLINE sup #-}
singular :: Ord a => Interval a -> Bool
singular :: Interval a -> Bool
singular (I a
a a
b) = a
a a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
b
{-# INLINE singular #-}
instance Show a => Show (Interval a) where
showsPrec :: Int -> Interval a -> ShowS
showsPrec Int
n (I a
a a
b) =
Bool -> ShowS -> ShowS
showParen (Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
3) (ShowS -> ShowS) -> ShowS -> ShowS
forall a b. (a -> b) -> a -> b
$
Int -> a -> ShowS
forall a. Show a => Int -> a -> ShowS
showsPrec Int
3 a
a ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
String -> ShowS
showString String
" ... " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
Int -> a -> ShowS
forall a. Show a => Int -> a -> ShowS
showsPrec Int
3 a
b
width :: Num a => Interval a -> a
width :: Interval a -> a
width (I a
a a
b) = a
b a -> a -> a
forall a. Num a => a -> a -> a
- a
a
{-# INLINE width #-}
magnitude :: (Num a, Ord a) => Interval a -> a
magnitude :: Interval a -> a
magnitude = Interval a -> a
forall a. Interval a -> a
sup (Interval a -> a) -> (Interval a -> Interval a) -> Interval a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Interval a -> Interval a
forall a. Num a => a -> a
abs
{-# INLINE magnitude #-}
mignitude :: (Num a, Ord a) => Interval a -> a
mignitude :: Interval a -> a
mignitude = Interval a -> a
forall a. Interval a -> a
inf (Interval a -> a) -> (Interval a -> Interval a) -> Interval a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Interval a -> Interval a
forall a. Num a => a -> a
abs
{-# INLINE mignitude #-}
instance (Num a, Ord a) => Num (Interval a) where
I a
a a
b + :: Interval a -> Interval a -> Interval a
+ I a
a' a
b' = (a
a a -> a -> a
forall a. Num a => a -> a -> a
+ a
a') a -> a -> Interval a
forall a. Ord a => a -> a -> Interval a
... (a
b a -> a -> a
forall a. Num a => a -> a -> a
+ a
b')
{-# INLINE (+) #-}
I a
a a
b - :: Interval a -> Interval a -> Interval a
- I a
a' a
b' = (a
a a -> a -> a
forall a. Num a => a -> a -> a
- a
b') a -> a -> Interval a
forall a. Ord a => a -> a -> Interval a
... (a
b a -> a -> a
forall a. Num a => a -> a -> a
- a
a')
{-# INLINE (-) #-}
I a
a a
b * :: Interval a -> Interval a -> Interval a
* I a
a' a
b' =
[a] -> a
forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
minimum [a
a a -> a -> a
forall a. Num a => a -> a -> a
* a
a', a
a a -> a -> a
forall a. Num a => a -> a -> a
* a
b', a
b a -> a -> a
forall a. Num a => a -> a -> a
* a
a', a
b a -> a -> a
forall a. Num a => a -> a -> a
* a
b']
a -> a -> Interval a
forall a. Ord a => a -> a -> Interval a
...
[a] -> a
forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
maximum [a
a a -> a -> a
forall a. Num a => a -> a -> a
* a
a', a
a a -> a -> a
forall a. Num a => a -> a -> a
* a
b', a
b a -> a -> a
forall a. Num a => a -> a -> a
* a
a', a
b a -> a -> a
forall a. Num a => a -> a -> a
* a
b']
{-# INLINE (*) #-}
abs :: Interval a -> Interval a
abs x :: Interval a
x@(I a
a a
b)
| a
a a -> a -> Bool
forall a. Ord a => a -> a -> Bool
>= a
0 = Interval a
x
| a
b a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
0 = Interval a -> Interval a
forall a. Num a => a -> a
negate Interval a
x
| Bool
otherwise = a
0 a -> a -> Interval a
forall a. Ord a => a -> a -> Interval a
... a -> a -> a
forall a. Ord a => a -> a -> a
max (- a
a) a
b
{-# INLINE abs #-}
signum :: Interval a -> Interval a
signum = (a -> a) -> Interval a -> Interval a
forall a b. (a -> b) -> Interval a -> Interval b
increasing a -> a
forall a. Num a => a -> a
signum
{-# INLINE signum #-}
fromInteger :: Integer -> Interval a
fromInteger Integer
i = a -> Interval a
forall a. a -> Interval a
singleton (Integer -> a
forall a. Num a => Integer -> a
fromInteger Integer
i)
{-# INLINE fromInteger #-}
bisect :: Fractional a => Interval a -> (Interval a, Interval a)
bisect :: Interval a -> (Interval a, Interval a)
bisect (I a
a a
b) = (a -> a -> Interval a
forall a. a -> a -> Interval a
I a
a a
m, a -> a -> Interval a
forall a. a -> a -> Interval a
I a
m a
b) where m :: a
m = a
a a -> a -> a
forall a. Num a => a -> a -> a
+ (a
b a -> a -> a
forall a. Num a => a -> a -> a
- a
a) a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
2
{-# INLINE bisect #-}
bisectIntegral :: Integral a => Interval a -> (Interval a, Interval a)
bisectIntegral :: Interval a -> (Interval a, Interval a)
bisectIntegral (I a
a a
b)
| a
a a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
m Bool -> Bool -> Bool
|| a
b a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
m = (a -> a -> Interval a
forall a. a -> a -> Interval a
I a
a a
a, a -> a -> Interval a
forall a. a -> a -> Interval a
I a
b a
b)
| Bool
otherwise = (a -> a -> Interval a
forall a. a -> a -> Interval a
I a
a a
m, a -> a -> Interval a
forall a. a -> a -> Interval a
I a
m a
b)
where m :: a
m = a
a a -> a -> a
forall a. Num a => a -> a -> a
+ (a
b a -> a -> a
forall a. Num a => a -> a -> a
- a
a) a -> a -> a
forall a. Integral a => a -> a -> a
`div` a
2
{-# INLINE bisectIntegral #-}
midpoint :: Fractional a => Interval a -> a
midpoint :: Interval a -> a
midpoint (I a
a a
b) = a
a a -> a -> a
forall a. Num a => a -> a -> a
+ (a
b a -> a -> a
forall a. Num a => a -> a -> a
- a
a) a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
2
{-# INLINE midpoint #-}
distance :: (Num a, Ord a) => Interval a -> Interval a -> a
distance :: Interval a -> Interval a -> a
distance Interval a
i1 Interval a
i2 = Interval a -> a
forall a. (Num a, Ord a) => Interval a -> a
mignitude (Interval a
i1 Interval a -> Interval a -> Interval a
forall a. Num a => a -> a -> a
- Interval a
i2)
member :: Ord a => a -> Interval a -> Bool
member :: a -> Interval a -> Bool
member a
x (I a
a a
b) = a
x a -> a -> Bool
forall a. Ord a => a -> a -> Bool
>= a
a Bool -> Bool -> Bool
&& a
x a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
b
{-# INLINE member #-}
notMember :: Ord a => a -> Interval a -> Bool
notMember :: a -> Interval a -> Bool
notMember a
x Interval a
xs = Bool -> Bool
not (a -> Interval a -> Bool
forall a. Ord a => a -> Interval a -> Bool
member a
x Interval a
xs)
{-# INLINE notMember #-}
elem :: Ord a => a -> Interval a -> Bool
elem :: a -> Interval a -> Bool
elem = a -> Interval a -> Bool
forall a. Ord a => a -> Interval a -> Bool
member
{-# INLINE elem #-}
{-# DEPRECATED elem "Use `member` instead." #-}
notElem :: Ord a => a -> Interval a -> Bool
notElem :: a -> Interval a -> Bool
notElem = a -> Interval a -> Bool
forall a. Ord a => a -> Interval a -> Bool
notMember
{-# INLINE notElem #-}
{-# DEPRECATED notElem "Use `notMember` instead." #-}
instance Real a => Real (Interval a) where
toRational :: Interval a -> Rational
toRational (I a
ra a
rb) = Rational
a Rational -> Rational -> Rational
forall a. Num a => a -> a -> a
+ (Rational
b Rational -> Rational -> Rational
forall a. Num a => a -> a -> a
- Rational
a) Rational -> Rational -> Rational
forall a. Fractional a => a -> a -> a
/ Rational
2 where
a :: Rational
a = a -> Rational
forall a. Real a => a -> Rational
toRational a
ra
b :: Rational
b = a -> Rational
forall a. Real a => a -> Rational
toRational a
rb
{-# INLINE toRational #-}
divNonZero :: (Fractional a, Ord a) => Interval a -> Interval a -> Interval a
divNonZero :: Interval a -> Interval a -> Interval a
divNonZero (I a
a a
b) (I a
a' a
b') =
[a] -> a
forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
minimum [a
a a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
a', a
a a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
b', a
b a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
a', a
b a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
b']
a -> a -> Interval a
forall a. Ord a => a -> a -> Interval a
...
[a] -> a
forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
maximum [a
a a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
a', a
a a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
b', a
b a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
a', a
b a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
b']
divPositive :: (Fractional a, Ord a) => Interval a -> a -> Interval a
divPositive :: Interval a -> a -> Interval a
divPositive x :: Interval a
x@(I a
a a
b) a
y
| a
a a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
0 Bool -> Bool -> Bool
&& a
b a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
0 = Interval a
x
| a
b a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
0 = a
forall a. Fractional a => a
negInfinity a -> a -> Interval a
forall a. Ord a => a -> a -> Interval a
... (a
b a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
y)
| a
a a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
0 = Interval a
forall a. Fractional a => Interval a
whole
| Bool
otherwise = (a
a a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
y) a -> a -> Interval a
forall a. Ord a => a -> a -> Interval a
... a
forall a. Fractional a => a
posInfinity
{-# INLINE divPositive #-}
divNegative :: (Fractional a, Ord a) => Interval a -> a -> Interval a
divNegative :: Interval a -> a -> Interval a
divNegative x :: Interval a
x@(I a
a a
b) a
y
| a
a a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
0 Bool -> Bool -> Bool
&& a
b a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
0 = - Interval a
x
| a
b a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
0 = (a
b a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
y) a -> a -> Interval a
forall a. Ord a => a -> a -> Interval a
... a
forall a. Fractional a => a
posInfinity
| a
a a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
0 = Interval a
forall a. Fractional a => Interval a
whole
| Bool
otherwise = a
forall a. Fractional a => a
negInfinity a -> a -> Interval a
forall a. Ord a => a -> a -> Interval a
... (a
a a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
y)
{-# INLINE divNegative #-}
divZero :: (Fractional a, Ord a) => Interval a -> Interval a
divZero :: Interval a -> Interval a
divZero x :: Interval a
x@(I a
a a
b)
| a
a a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
0 Bool -> Bool -> Bool
&& a
b a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
0 = Interval a
x
| Bool
otherwise = Interval a
forall a. Fractional a => Interval a
whole
{-# INLINE divZero #-}
instance (Fractional a, Ord a) => Fractional (Interval a) where
Interval a
x / :: Interval a -> Interval a -> Interval a
/ y :: Interval a
y@(I a
a a
b)
| a
0 a -> Interval a -> Bool
forall a. Ord a => a -> Interval a -> Bool
`notElem` Interval a
y = Interval a -> Interval a -> Interval a
forall a.
(Fractional a, Ord a) =>
Interval a -> Interval a -> Interval a
divNonZero Interval a
x Interval a
y
| Bool
iz Bool -> Bool -> Bool
&& Bool
sz = ArithException -> Interval a
forall a e. Exception e => e -> a
Exception.throw ArithException
DivideByZero
| Bool
iz = Interval a -> a -> Interval a
forall a. (Fractional a, Ord a) => Interval a -> a -> Interval a
divPositive Interval a
x a
a
| Bool
sz = Interval a -> a -> Interval a
forall a. (Fractional a, Ord a) => Interval a -> a -> Interval a
divNegative Interval a
x a
b
| Bool
otherwise = Interval a -> Interval a
forall a. (Fractional a, Ord a) => Interval a -> Interval a
divZero Interval a
x
where
iz :: Bool
iz = a
a a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
0
sz :: Bool
sz = a
b a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
0
fromRational :: Rational -> Interval a
fromRational Rational
r = let r' :: a
r' = Rational -> a
forall a. Fractional a => Rational -> a
fromRational Rational
r in a -> a -> Interval a
forall a. a -> a -> Interval a
I a
r' a
r'
{-# INLINE fromRational #-}
instance RealFrac a => RealFrac (Interval a) where
properFraction :: Interval a -> (b, Interval a)
properFraction Interval a
x = (b
b, Interval a
x Interval a -> Interval a -> Interval a
forall a. Num a => a -> a -> a
- b -> Interval a
forall a b. (Integral a, Num b) => a -> b
fromIntegral b
b)
where
b :: b
b = a -> b
forall a b. (RealFrac a, Integral b) => a -> b
truncate (Interval a -> a
forall a. Fractional a => Interval a -> a
midpoint Interval a
x)
{-# INLINE properFraction #-}
ceiling :: Interval a -> b
ceiling Interval a
x = a -> b
forall a b. (RealFrac a, Integral b) => a -> b
ceiling (Interval a -> a
forall a. Interval a -> a
sup Interval a
x)
{-# INLINE ceiling #-}
floor :: Interval a -> b
floor Interval a
x = a -> b
forall a b. (RealFrac a, Integral b) => a -> b
floor (Interval a -> a
forall a. Interval a -> a
inf Interval a
x)
{-# INLINE floor #-}
round :: Interval a -> b
round Interval a
x = a -> b
forall a b. (RealFrac a, Integral b) => a -> b
round (Interval a -> a
forall a. Fractional a => Interval a -> a
midpoint Interval a
x)
{-# INLINE round #-}
truncate :: Interval a -> b
truncate Interval a
x = a -> b
forall a b. (RealFrac a, Integral b) => a -> b
truncate (Interval a -> a
forall a. Fractional a => Interval a -> a
midpoint Interval a
x)
{-# INLINE truncate #-}
instance (RealFloat a, Ord a) => Floating (Interval a) where
pi :: Interval a
pi = a -> Interval a
forall a. a -> Interval a
singleton a
forall a. Floating a => a
pi
{-# INLINE pi #-}
exp :: Interval a -> Interval a
exp = (a -> a) -> Interval a -> Interval a
forall a b. (a -> b) -> Interval a -> Interval b
increasing a -> a
forall a. Floating a => a -> a
exp
{-# INLINE exp #-}
log :: Interval a -> Interval a
log (I a
a a
b) = (if a
a a -> a -> Bool
forall a. Ord a => a -> a -> Bool
> a
0 then a -> a
forall a. Floating a => a -> a
log a
a else a
forall a. Fractional a => a
negInfinity) a -> a -> Interval a
forall a. Ord a => a -> a -> Interval a
... (if a
b a -> a -> Bool
forall a. Ord a => a -> a -> Bool
> a
0 then a -> a
forall a. Floating a => a -> a
log a
b else a
forall a. Fractional a => a
negInfinity)
{-# INLINE log #-}
sin :: Interval a -> Interval a
sin = a
-> Interval a
-> (a -> Ordering)
-> (a -> a)
-> Interval a
-> Interval a
forall a.
(Num a, Ord a) =>
a
-> Interval a
-> (a -> Ordering)
-> (a -> a)
-> Interval a
-> Interval a
periodic (a
2 a -> a -> a
forall a. Num a => a -> a -> a
* a
forall a. Floating a => a
pi) (a -> Interval a
forall a. (Num a, Ord a) => a -> Interval a
symmetric a
1) (a -> Ordering
forall a. (Ord a, Num a) => a -> Ordering
signum' (a -> Ordering) -> (a -> a) -> a -> Ordering
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> a
forall a. Floating a => a -> a
cos) a -> a
forall a. Floating a => a -> a
sin
cos :: Interval a -> Interval a
cos = a
-> Interval a
-> (a -> Ordering)
-> (a -> a)
-> Interval a
-> Interval a
forall a.
(Num a, Ord a) =>
a
-> Interval a
-> (a -> Ordering)
-> (a -> a)
-> Interval a
-> Interval a
periodic (a
2 a -> a -> a
forall a. Num a => a -> a -> a
* a
forall a. Floating a => a
pi) (a -> Interval a
forall a. (Num a, Ord a) => a -> Interval a
symmetric a
1) (a -> Ordering
forall a. (Ord a, Num a) => a -> Ordering
signum' (a -> Ordering) -> (a -> a) -> a -> Ordering
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> a
forall a. Num a => a -> a
negate (a -> a) -> (a -> a) -> a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> a
forall a. Floating a => a -> a
sin) a -> a
forall a. Floating a => a -> a
cos
tan :: Interval a -> Interval a
tan = a
-> Interval a
-> (a -> Ordering)
-> (a -> a)
-> Interval a
-> Interval a
forall a.
(Num a, Ord a) =>
a
-> Interval a
-> (a -> Ordering)
-> (a -> a)
-> Interval a
-> Interval a
periodic a
forall a. Floating a => a
pi Interval a
forall a. Fractional a => Interval a
whole (Ordering -> a -> Ordering
forall a b. a -> b -> a
const Ordering
GT) a -> a
forall a. Floating a => a -> a
tan
asin :: Interval a -> Interval a
asin (I a
a a
b) = (a -> a
asin' a
a) a -> a -> Interval a
forall a. Ord a => a -> a -> Interval a
... (a -> a
asin' a
b)
where
asin' :: a -> a
asin' a
x | a
x a -> a -> Bool
forall a. Ord a => a -> a -> Bool
>= a
1 = a
halfPi
| a
x a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= -a
1 = -a
halfPi
| Bool
otherwise = a -> a
forall a. Floating a => a -> a
asin a
x
halfPi :: a
halfPi = a
forall a. Floating a => a
pi a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
2
{-# INLINE asin #-}
acos :: Interval a -> Interval a
acos (I a
a a
b) = (a -> a
forall p. (Ord p, Floating p) => p -> p
acos' a
a) a -> a -> Interval a
forall a. Ord a => a -> a -> Interval a
... (a -> a
forall p. (Ord p, Floating p) => p -> p
acos' a
b)
where
acos' :: p -> p
acos' p
x | p
x p -> p -> Bool
forall a. Ord a => a -> a -> Bool
>= p
1 = p
0
| p
x p -> p -> Bool
forall a. Ord a => a -> a -> Bool
<= -p
1 = p
forall a. Floating a => a
pi
| Bool
otherwise = p -> p
forall a. Floating a => a -> a
acos p
x
{-# INLINE acos #-}
atan :: Interval a -> Interval a
atan = (a -> a) -> Interval a -> Interval a
forall a b. (a -> b) -> Interval a -> Interval b
increasing a -> a
forall a. Floating a => a -> a
atan
{-# INLINE atan #-}
sinh :: Interval a -> Interval a
sinh = (a -> a) -> Interval a -> Interval a
forall a b. (a -> b) -> Interval a -> Interval b
increasing a -> a
forall a. Floating a => a -> a
sinh
{-# INLINE sinh #-}
cosh :: Interval a -> Interval a
cosh x :: Interval a
x@(I a
a a
b)
| a
b a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
0 = (a -> a) -> Interval a -> Interval a
forall a b. (a -> b) -> Interval a -> Interval b
decreasing a -> a
forall a. Floating a => a -> a
cosh Interval a
x
| a
a a -> a -> Bool
forall a. Ord a => a -> a -> Bool
>= a
0 = (a -> a) -> Interval a -> Interval a
forall a b. (a -> b) -> Interval a -> Interval b
increasing a -> a
forall a. Floating a => a -> a
cosh Interval a
x
| Bool
otherwise = a -> a -> Interval a
forall a. a -> a -> Interval a
I a
0 (a -> Interval a) -> a -> Interval a
forall a b. (a -> b) -> a -> b
$ a -> a
forall a. Floating a => a -> a
cosh (a -> a) -> a -> a
forall a b. (a -> b) -> a -> b
$ if - a
a a -> a -> Bool
forall a. Ord a => a -> a -> Bool
> a
b
then a
a
else a
b
{-# INLINE cosh #-}
tanh :: Interval a -> Interval a
tanh = (a -> a) -> Interval a -> Interval a
forall a b. (a -> b) -> Interval a -> Interval b
increasing a -> a
forall a. Floating a => a -> a
tanh
{-# INLINE tanh #-}
asinh :: Interval a -> Interval a
asinh = (a -> a) -> Interval a -> Interval a
forall a b. (a -> b) -> Interval a -> Interval b
increasing a -> a
forall a. Floating a => a -> a
asinh
{-# INLINE asinh #-}
acosh :: Interval a -> Interval a
acosh (I a
a a
b) = (a -> a
forall p. (Ord p, Floating p) => p -> p
acosh' a
a) a -> a -> Interval a
forall a. Ord a => a -> a -> Interval a
... (a -> a
forall p. (Ord p, Floating p) => p -> p
acosh' a
b)
where
acosh' :: p -> p
acosh' p
x | p
x p -> p -> Bool
forall a. Ord a => a -> a -> Bool
<= p
1 = p
0
| Bool
otherwise = p -> p
forall a. Floating a => a -> a
acosh p
x
{-# INLINE acosh #-}
atanh :: Interval a -> Interval a
atanh (I a
a a
b) = (a -> a
forall p. (Ord p, Floating p) => p -> p
atanh' a
a) a -> a -> Interval a
forall a. Ord a => a -> a -> Interval a
... (a -> a
forall p. (Ord p, Floating p) => p -> p
atanh' a
b)
where
atanh' :: p -> p
atanh' p
x | p
x p -> p -> Bool
forall a. Ord a => a -> a -> Bool
<= -p
1 = p
forall a. Fractional a => a
negInfinity
| p
x p -> p -> Bool
forall a. Ord a => a -> a -> Bool
>= p
1 = p
forall a. Fractional a => a
posInfinity
| Bool
otherwise = p -> p
forall a. Floating a => a -> a
atanh p
x
{-# INLINE atanh #-}
increasing :: (a -> b) -> Interval a -> Interval b
increasing :: (a -> b) -> Interval a -> Interval b
increasing a -> b
f (I a
a a
b) = b -> b -> Interval b
forall a. a -> a -> Interval a
I (a -> b
f a
a) (a -> b
f a
b)
decreasing :: (a -> b) -> Interval a -> Interval b
decreasing :: (a -> b) -> Interval a -> Interval b
decreasing a -> b
f (I a
a a
b) = b -> b -> Interval b
forall a. a -> a -> Interval a
I (a -> b
f a
b) (a -> b
f a
a)
instance RealFloat a => RealFloat (Interval a) where
floatRadix :: Interval a -> Integer
floatRadix = a -> Integer
forall a. RealFloat a => a -> Integer
floatRadix (a -> Integer) -> (Interval a -> a) -> Interval a -> Integer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Interval a -> a
forall a. Fractional a => Interval a -> a
midpoint
floatDigits :: Interval a -> Int
floatDigits = a -> Int
forall a. RealFloat a => a -> Int
floatDigits (a -> Int) -> (Interval a -> a) -> Interval a -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Interval a -> a
forall a. Fractional a => Interval a -> a
midpoint
floatRange :: Interval a -> (Int, Int)
floatRange = a -> (Int, Int)
forall a. RealFloat a => a -> (Int, Int)
floatRange (a -> (Int, Int)) -> (Interval a -> a) -> Interval a -> (Int, Int)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Interval a -> a
forall a. Fractional a => Interval a -> a
midpoint
decodeFloat :: Interval a -> (Integer, Int)
decodeFloat = a -> (Integer, Int)
forall a. RealFloat a => a -> (Integer, Int)
decodeFloat (a -> (Integer, Int))
-> (Interval a -> a) -> Interval a -> (Integer, Int)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Interval a -> a
forall a. Fractional a => Interval a -> a
midpoint
encodeFloat :: Integer -> Int -> Interval a
encodeFloat Integer
m Int
e = a -> Interval a
forall a. a -> Interval a
singleton (Integer -> Int -> a
forall a. RealFloat a => Integer -> Int -> a
encodeFloat Integer
m Int
e)
exponent :: Interval a -> Int
exponent = a -> Int
forall a. RealFloat a => a -> Int
exponent (a -> Int) -> (Interval a -> a) -> Interval a -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Interval a -> a
forall a. Fractional a => Interval a -> a
midpoint
significand :: Interval a -> Interval a
significand Interval a
x = a -> a -> a
forall a. Ord a => a -> a -> a
min a
a a
b a -> a -> Interval a
forall a. Ord a => a -> a -> Interval a
... a -> a -> a
forall a. Ord a => a -> a -> a
max a
a a
b
where
(Integer
_ ,Int
em) = a -> (Integer, Int)
forall a. RealFloat a => a -> (Integer, Int)
decodeFloat (Interval a -> a
forall a. Fractional a => Interval a -> a
midpoint Interval a
x)
(Integer
mi,Int
ei) = a -> (Integer, Int)
forall a. RealFloat a => a -> (Integer, Int)
decodeFloat (Interval a -> a
forall a. Interval a -> a
inf Interval a
x)
(Integer
ms,Int
es) = a -> (Integer, Int)
forall a. RealFloat a => a -> (Integer, Int)
decodeFloat (Interval a -> a
forall a. Interval a -> a
sup Interval a
x)
a :: a
a = Integer -> Int -> a
forall a. RealFloat a => Integer -> Int -> a
encodeFloat Integer
mi (Int
ei Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
em Int -> Int -> Int
forall a. Num a => a -> a -> a
- Interval a -> Int
forall a. RealFloat a => a -> Int
floatDigits Interval a
x)
b :: a
b = Integer -> Int -> a
forall a. RealFloat a => Integer -> Int -> a
encodeFloat Integer
ms (Int
es Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
em Int -> Int -> Int
forall a. Num a => a -> a -> a
- Interval a -> Int
forall a. RealFloat a => a -> Int
floatDigits Interval a
x)
scaleFloat :: Int -> Interval a -> Interval a
scaleFloat Int
n (I a
a a
b) = a -> a -> Interval a
forall a. a -> a -> Interval a
I (Int -> a -> a
forall a. RealFloat a => Int -> a -> a
scaleFloat Int
n a
a) (Int -> a -> a
forall a. RealFloat a => Int -> a -> a
scaleFloat Int
n a
b)
isNaN :: Interval a -> Bool
isNaN (I a
a a
b) = a -> Bool
forall a. RealFloat a => a -> Bool
isNaN a
a Bool -> Bool -> Bool
|| a -> Bool
forall a. RealFloat a => a -> Bool
isNaN a
b
isInfinite :: Interval a -> Bool
isInfinite (I a
a a
b) = a -> Bool
forall a. RealFloat a => a -> Bool
isInfinite a
a Bool -> Bool -> Bool
|| a -> Bool
forall a. RealFloat a => a -> Bool
isInfinite a
b
isDenormalized :: Interval a -> Bool
isDenormalized (I a
a a
b) = a -> Bool
forall a. RealFloat a => a -> Bool
isDenormalized a
a Bool -> Bool -> Bool
|| a -> Bool
forall a. RealFloat a => a -> Bool
isDenormalized a
b
isNegativeZero :: Interval a -> Bool
isNegativeZero (I a
a a
b) = Bool -> Bool
not (a
a a -> a -> Bool
forall a. Ord a => a -> a -> Bool
> a
0)
Bool -> Bool -> Bool
&& Bool -> Bool
not (a
b a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
0)
Bool -> Bool -> Bool
&& ( (a
b a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
0 Bool -> Bool -> Bool
&& (a
a a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
0 Bool -> Bool -> Bool
|| a -> Bool
forall a. RealFloat a => a -> Bool
isNegativeZero a
a))
Bool -> Bool -> Bool
|| (a
a a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
0 Bool -> Bool -> Bool
&& a -> Bool
forall a. RealFloat a => a -> Bool
isNegativeZero a
a)
Bool -> Bool -> Bool
|| (a
a a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
0 Bool -> Bool -> Bool
&& a
b a -> a -> Bool
forall a. Ord a => a -> a -> Bool
>= a
0))
isIEEE :: Interval a -> Bool
isIEEE Interval a
_ = Bool
False
atan2 :: Interval a -> Interval a -> Interval a
atan2 = String -> Interval a -> Interval a -> Interval a
forall a. HasCallStack => String -> a
error String
"unimplemented"
intersection :: Ord a => Interval a -> Interval a -> Maybe (Interval a)
intersection :: Interval a -> Interval a -> Maybe (Interval a)
intersection x :: Interval a
x@(I a
a a
b) y :: Interval a
y@(I a
a' a
b')
| Interval a
x Interval a -> Interval a -> Bool
forall a. Ord a => Interval a -> Interval a -> Bool
/=! Interval a
y = Maybe (Interval a)
forall a. Maybe a
Nothing
| Bool
otherwise = Interval a -> Maybe (Interval a)
forall a. a -> Maybe a
Just (Interval a -> Maybe (Interval a))
-> Interval a -> Maybe (Interval a)
forall a b. (a -> b) -> a -> b
$ a -> a -> Interval a
forall a. a -> a -> Interval a
I (a -> a -> a
forall a. Ord a => a -> a -> a
max a
a a
a') (a -> a -> a
forall a. Ord a => a -> a -> a
min a
b a
b')
{-# INLINE intersection #-}
hull :: Ord a => Interval a -> Interval a -> Interval a
hull :: Interval a -> Interval a -> Interval a
hull (I a
a a
b) (I a
a' a
b') = a -> a -> Interval a
forall a. a -> a -> Interval a
I (a -> a -> a
forall a. Ord a => a -> a -> a
min a
a a
a') (a -> a -> a
forall a. Ord a => a -> a -> a
max a
b a
b')
{-# INLINE hull #-}
(<!) :: Ord a => Interval a -> Interval a -> Bool
I a
_ a
bx <! :: Interval a -> Interval a -> Bool
<! I a
ay a
_ = a
bx a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
ay
{-# INLINE (<!) #-}
(<=!) :: Ord a => Interval a -> Interval a -> Bool
I a
_ a
bx <=! :: Interval a -> Interval a -> Bool
<=! I a
ay a
_ = a
bx a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
ay
{-# INLINE (<=!) #-}
(==!) :: Eq a => Interval a -> Interval a -> Bool
I a
ax a
bx ==! :: Interval a -> Interval a -> Bool
==! I a
ay a
by = a
bx a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
ay Bool -> Bool -> Bool
&& a
ax a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
by
{-# INLINE (==!) #-}
(/=!) :: Ord a => Interval a -> Interval a -> Bool
I a
ax a
bx /=! :: Interval a -> Interval a -> Bool
/=! I a
ay a
by = a
bx a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
ay Bool -> Bool -> Bool
|| a
ax a -> a -> Bool
forall a. Ord a => a -> a -> Bool
> a
by
{-# INLINE (/=!) #-}
(>!) :: Ord a => Interval a -> Interval a -> Bool
I a
ax a
_ >! :: Interval a -> Interval a -> Bool
>! I a
_ a
by = a
ax a -> a -> Bool
forall a. Ord a => a -> a -> Bool
> a
by
{-# INLINE (>!) #-}
(>=!) :: Ord a => Interval a -> Interval a -> Bool
I a
ax a
_ >=! :: Interval a -> Interval a -> Bool
>=! I a
_ a
by = a
ax a -> a -> Bool
forall a. Ord a => a -> a -> Bool
>= a
by
{-# INLINE (>=!) #-}
certainly :: Ord a => (forall b. Ord b => b -> b -> Bool) -> Interval a -> Interval a -> Bool
certainly :: (forall a. Ord a => a -> a -> Bool)
-> Interval a -> Interval a -> Bool
certainly forall a. Ord a => a -> a -> Bool
cmp Interval a
l Interval a
r
| Bool
lt Bool -> Bool -> Bool
&& Bool
eq Bool -> Bool -> Bool
&& Bool
gt = Bool
True
| Bool
lt Bool -> Bool -> Bool
&& Bool
eq = Interval a
l Interval a -> Interval a -> Bool
forall a. Ord a => Interval a -> Interval a -> Bool
<=! Interval a
r
| Bool
lt Bool -> Bool -> Bool
&& Bool
gt = Interval a
l Interval a -> Interval a -> Bool
forall a. Ord a => Interval a -> Interval a -> Bool
/=! Interval a
r
| Bool
lt = Interval a
l Interval a -> Interval a -> Bool
forall a. Ord a => Interval a -> Interval a -> Bool
<! Interval a
r
| Bool
eq Bool -> Bool -> Bool
&& Bool
gt = Interval a
l Interval a -> Interval a -> Bool
forall a. Ord a => Interval a -> Interval a -> Bool
>=! Interval a
r
| Bool
eq = Interval a
l Interval a -> Interval a -> Bool
forall a. Eq a => Interval a -> Interval a -> Bool
==! Interval a
r
| Bool
gt = Interval a
l Interval a -> Interval a -> Bool
forall a. Ord a => Interval a -> Interval a -> Bool
>! Interval a
r
| Bool
otherwise = Bool
False
where
lt :: Bool
lt = Bool -> Bool -> Bool
forall a. Ord a => a -> a -> Bool
cmp Bool
False Bool
True
eq :: Bool
eq = Bool -> Bool -> Bool
forall a. Ord a => a -> a -> Bool
cmp Bool
True Bool
True
gt :: Bool
gt = Bool -> Bool -> Bool
forall a. Ord a => a -> a -> Bool
cmp Bool
True Bool
False
{-# INLINE certainly #-}
contains :: Ord a => Interval a -> Interval a -> Bool
contains :: Interval a -> Interval a -> Bool
contains (I a
ax a
bx) (I a
ay a
by) = a
ax a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
ay Bool -> Bool -> Bool
&& a
by a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
bx
{-# INLINE contains #-}
isSubsetOf :: Ord a => Interval a -> Interval a -> Bool
isSubsetOf :: Interval a -> Interval a -> Bool
isSubsetOf = (Interval a -> Interval a -> Bool)
-> Interval a -> Interval a -> Bool
forall a b c. (a -> b -> c) -> b -> a -> c
flip Interval a -> Interval a -> Bool
forall a. Ord a => Interval a -> Interval a -> Bool
contains
{-# INLINE isSubsetOf #-}
(<?) :: Ord a => Interval a -> Interval a -> Bool
I a
ax a
_ <? :: Interval a -> Interval a -> Bool
<? I a
_ a
by = a
ax a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
by
{-# INLINE (<?) #-}
(<=?) :: Ord a => Interval a -> Interval a -> Bool
I a
ax a
_ <=? :: Interval a -> Interval a -> Bool
<=? I a
_ a
by = a
ax a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
by
{-# INLINE (<=?) #-}
(==?) :: Ord a => Interval a -> Interval a -> Bool
I a
ax a
bx ==? :: Interval a -> Interval a -> Bool
==? I a
ay a
by = a
ax a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
by Bool -> Bool -> Bool
&& a
bx a -> a -> Bool
forall a. Ord a => a -> a -> Bool
>= a
ay
{-# INLINE (==?) #-}
(/=?) :: Eq a => Interval a -> Interval a -> Bool
I a
ax a
bx /=? :: Interval a -> Interval a -> Bool
/=? I a
ay a
by = a
ax a -> a -> Bool
forall a. Eq a => a -> a -> Bool
/= a
by Bool -> Bool -> Bool
|| a
bx a -> a -> Bool
forall a. Eq a => a -> a -> Bool
/= a
ay
{-# INLINE (/=?) #-}
(>?) :: Ord a => Interval a -> Interval a -> Bool
I a
_ a
bx >? :: Interval a -> Interval a -> Bool
>? I a
ay a
_ = a
bx a -> a -> Bool
forall a. Ord a => a -> a -> Bool
> a
ay
{-# INLINE (>?) #-}
(>=?) :: Ord a => Interval a -> Interval a -> Bool
I a
_ a
bx >=? :: Interval a -> Interval a -> Bool
>=? I a
ay a
_ = a
bx a -> a -> Bool
forall a. Ord a => a -> a -> Bool
>= a
ay
{-# INLINE (>=?) #-}
possibly :: Ord a => (forall b. Ord b => b -> b -> Bool) -> Interval a -> Interval a -> Bool
possibly :: (forall a. Ord a => a -> a -> Bool)
-> Interval a -> Interval a -> Bool
possibly forall a. Ord a => a -> a -> Bool
cmp Interval a
l Interval a
r
| Bool
lt Bool -> Bool -> Bool
&& Bool
eq Bool -> Bool -> Bool
&& Bool
gt = Bool
True
| Bool
lt Bool -> Bool -> Bool
&& Bool
eq = Interval a
l Interval a -> Interval a -> Bool
forall a. Ord a => Interval a -> Interval a -> Bool
<=? Interval a
r
| Bool
lt Bool -> Bool -> Bool
&& Bool
gt = Interval a
l Interval a -> Interval a -> Bool
forall a. Eq a => Interval a -> Interval a -> Bool
/=? Interval a
r
| Bool
lt = Interval a
l Interval a -> Interval a -> Bool
forall a. Ord a => Interval a -> Interval a -> Bool
<? Interval a
r
| Bool
eq Bool -> Bool -> Bool
&& Bool
gt = Interval a
l Interval a -> Interval a -> Bool
forall a. Ord a => Interval a -> Interval a -> Bool
>=? Interval a
r
| Bool
eq = Interval a
l Interval a -> Interval a -> Bool
forall a. Ord a => Interval a -> Interval a -> Bool
==? Interval a
r
| Bool
gt = Interval a
l Interval a -> Interval a -> Bool
forall a. Ord a => Interval a -> Interval a -> Bool
>? Interval a
r
| Bool
otherwise = Bool
False
where
lt :: Bool
lt = Ordering -> Ordering -> Bool
forall a. Ord a => a -> a -> Bool
cmp Ordering
LT Ordering
EQ
eq :: Bool
eq = Ordering -> Ordering -> Bool
forall a. Ord a => a -> a -> Bool
cmp Ordering
EQ Ordering
EQ
gt :: Bool
gt = Ordering -> Ordering -> Bool
forall a. Ord a => a -> a -> Bool
cmp Ordering
GT Ordering
EQ
{-# INLINE possibly #-}
clamp :: Ord a => Interval a -> a -> a
clamp :: Interval a -> a -> a
clamp (I a
a a
b) a
x
| a
x a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
a = a
a
| a
x a -> a -> Bool
forall a. Ord a => a -> a -> Bool
> a
b = a
b
| Bool
otherwise = a
x
inflate :: (Num a, Ord a) => a -> Interval a -> Interval a
inflate :: a -> Interval a -> Interval a
inflate a
x Interval a
y = a -> Interval a
forall a. (Num a, Ord a) => a -> Interval a
symmetric a
x Interval a -> Interval a -> Interval a
forall a. Num a => a -> a -> a
+ Interval a
y
deflate :: (Fractional a, Ord a) => a -> Interval a -> Interval a
deflate :: a -> Interval a -> Interval a
deflate a
x i :: Interval a
i@(I a
a a
b) | a
a' a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
b' = a -> a -> Interval a
forall a. a -> a -> Interval a
I a
a' a
b'
| Bool
otherwise = a -> Interval a
forall a. a -> Interval a
singleton a
m
where
a' :: a
a' = a
a a -> a -> a
forall a. Num a => a -> a -> a
+ a
x
b' :: a
b' = a
b a -> a -> a
forall a. Num a => a -> a -> a
- a
x
m :: a
m = Interval a -> a
forall a. Fractional a => Interval a -> a
midpoint Interval a
i
scale :: (Fractional a, Ord a) => a -> Interval a -> Interval a
scale :: a -> Interval a -> Interval a
scale a
x Interval a
i = a
a a -> a -> Interval a
forall a. Ord a => a -> a -> Interval a
... a
b where
h :: a
h = a
x a -> a -> a
forall a. Num a => a -> a -> a
* Interval a -> a
forall a. Num a => Interval a -> a
width Interval a
i a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
2
mid :: a
mid = Interval a -> a
forall a. Fractional a => Interval a -> a
midpoint Interval a
i
a :: a
a = a
mid a -> a -> a
forall a. Num a => a -> a -> a
- a
h
b :: a
b = a
mid a -> a -> a
forall a. Num a => a -> a -> a
+ a
h
symmetric :: (Num a, Ord a) => a -> Interval a
symmetric :: a -> Interval a
symmetric a
x = a -> a
forall a. Num a => a -> a
negate a
x a -> a -> Interval a
forall a. Ord a => a -> a -> Interval a
... a
x
idouble :: Interval Double -> Interval Double
idouble :: Interval Double -> Interval Double
idouble = Interval Double -> Interval Double
forall a. a -> a
id
ifloat :: Interval Float -> Interval Float
ifloat :: Interval Float -> Interval Float
ifloat = Interval Float -> Interval Float
forall a. a -> a
id
default (Integer,Double)
iquot :: Integral a => Interval a -> Interval a -> Interval a
iquot :: Interval a -> Interval a -> Interval a
iquot (I a
l a
u) (I a
l' a
u') =
if a
l' a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
0 Bool -> Bool -> Bool
&& a
0 a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
u' then ArithException -> Interval a
forall a e. Exception e => e -> a
throw ArithException
DivideByZero else a -> a -> Interval a
forall a. a -> a -> Interval a
I
([a] -> a
forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
minimum [a
a a -> a -> a
forall a. Integral a => a -> a -> a
`quot` a
b | a
a <- [a
l,a
u], a
b <- [a
l',a
u']])
([a] -> a
forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
maximum [a
a a -> a -> a
forall a. Integral a => a -> a -> a
`quot` a
b | a
a <- [a
l,a
u], a
b <- [a
l',a
u']])
irem :: Integral a => Interval a -> Interval a -> Interval a
irem :: Interval a -> Interval a -> Interval a
irem (I a
l a
u) (I a
l' a
u') =
if a
l' a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
0 Bool -> Bool -> Bool
&& a
0 a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
u' then ArithException -> Interval a
forall a e. Exception e => e -> a
throw ArithException
DivideByZero else a -> a -> Interval a
forall a. a -> a -> Interval a
I
([a] -> a
forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
minimum [a
0, a -> a
forall a. Num a => a -> a
signum a
l a -> a -> a
forall a. Num a => a -> a -> a
* (a -> a
forall a. Num a => a -> a
abs a
u' a -> a -> a
forall a. Num a => a -> a -> a
- a
1), a -> a
forall a. Num a => a -> a
signum a
l a -> a -> a
forall a. Num a => a -> a -> a
* (a -> a
forall a. Num a => a -> a
abs a
l' a -> a -> a
forall a. Num a => a -> a -> a
- a
1)])
([a] -> a
forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
maximum [a
0, a -> a
forall a. Num a => a -> a
signum a
u a -> a -> a
forall a. Num a => a -> a -> a
* (a -> a
forall a. Num a => a -> a
abs a
u' a -> a -> a
forall a. Num a => a -> a -> a
- a
1), a -> a
forall a. Num a => a -> a
signum a
u a -> a -> a
forall a. Num a => a -> a -> a
* (a -> a
forall a. Num a => a -> a
abs a
l' a -> a -> a
forall a. Num a => a -> a -> a
- a
1)])
idiv :: Integral a => Interval a -> Interval a -> Interval a
idiv :: Interval a -> Interval a -> Interval a
idiv (I a
l a
u) (I a
l' a
u') =
if a
l' a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
0 Bool -> Bool -> Bool
&& a
0 a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
u' then ArithException -> Interval a
forall a e. Exception e => e -> a
throw ArithException
DivideByZero else a -> a -> Interval a
forall a. a -> a -> Interval a
I
(a -> a -> a
forall a. Ord a => a -> a -> a
min (a
l a -> a -> a
forall a. Integral a => a -> a -> a
`Prelude.div` a -> a -> a
forall a. Ord a => a -> a -> a
max a
1 a
l') (a
u a -> a -> a
forall a. Integral a => a -> a -> a
`Prelude.div` a -> a -> a
forall a. Ord a => a -> a -> a
min (-a
1) a
u'))
(a -> a -> a
forall a. Ord a => a -> a -> a
max (a
u a -> a -> a
forall a. Integral a => a -> a -> a
`Prelude.div` a -> a -> a
forall a. Ord a => a -> a -> a
max a
1 a
l') (a
l a -> a -> a
forall a. Integral a => a -> a -> a
`Prelude.div` a -> a -> a
forall a. Ord a => a -> a -> a
min (-a
1) a
u'))
imod :: Integral a => Interval a -> Interval a -> Interval a
imod :: Interval a -> Interval a -> Interval a
imod Interval a
_ (I a
l' a
u') =
if a
l' a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
0 Bool -> Bool -> Bool
&& a
0 a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
u' then ArithException -> Interval a
forall a e. Exception e => e -> a
throw ArithException
DivideByZero else
a -> a -> Interval a
forall a. a -> a -> Interval a
I (a -> a -> a
forall a. Ord a => a -> a -> a
min (a
l'a -> a -> a
forall a. Num a => a -> a -> a
+a
1) a
0) (a -> a -> a
forall a. Ord a => a -> a -> a
max a
0 (a
u'a -> a -> a
forall a. Num a => a -> a -> a
-a
1))