{-# LANGUAGE CPP #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
module Numeric.Rounded.Hardware.Interval
  ( Interval(..)
  , increasing
  , maxI
  , minI
  , powInt
  , null
  , inf
  , sup
  , width
  , widthUlp
  , hull
  , intersection
  ) where
import           Control.DeepSeq (NFData (..))
import           Control.Monad
import           Control.Monad.ST
import qualified Data.Array.Base as A
import           Data.Coerce
import           Data.Ix
import           Data.Primitive
import qualified Data.Vector.Generic as VG
import qualified Data.Vector.Generic.Mutable as VGM
import qualified Data.Vector.Unboxed as VU
import qualified Data.Vector.Unboxed.Mutable as VUM
import           GHC.Float (expm1, log1mexp, log1p, log1pexp)
import           GHC.Generics (Generic)
import           Numeric.Rounded.Hardware.Internal
import qualified Numeric.Rounded.Hardware.Interval.Class as C
import qualified Numeric.Rounded.Hardware.Interval.NonEmpty as NE
import           Prelude hiding (null)

data Interval a
  = I !(Rounded 'TowardNegInf a) !(Rounded 'TowardInf a)
  | Empty
  deriving (Int -> Interval a -> ShowS
[Interval a] -> ShowS
Interval a -> String
(Int -> Interval a -> ShowS)
-> (Interval a -> String)
-> ([Interval a] -> ShowS)
-> Show (Interval a)
forall a. Show a => Int -> Interval a -> ShowS
forall a. Show a => [Interval a] -> ShowS
forall a. Show a => Interval a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall a. Show a => Int -> Interval a -> ShowS
showsPrec :: Int -> Interval a -> ShowS
$cshow :: forall a. Show a => Interval a -> String
show :: Interval a -> String
$cshowList :: forall a. Show a => [Interval a] -> ShowS
showList :: [Interval a] -> ShowS
Show,(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
$cfrom :: forall a x. Interval a -> Rep (Interval a) x
from :: forall x. Interval a -> Rep (Interval a) x
$cto :: forall a x. Rep (Interval a) x -> Interval a
to :: forall x. Rep (Interval a) x -> Interval a
Generic)

instance NFData a => NFData (Interval a)

increasing :: (forall r. Rounding r => Rounded r a -> Rounded r a) -> Interval a -> Interval a
increasing :: forall a.
(forall (r :: RoundingMode).
 Rounding r =>
 Rounded r a -> Rounded r a)
-> Interval a -> Interval a
increasing forall (r :: RoundingMode).
Rounding r =>
Rounded r a -> Rounded r a
f (I Rounded 'TowardNegInf a
a Rounded 'TowardInf a
b) = Rounded 'TowardNegInf a -> Rounded 'TowardInf a -> Interval a
forall a.
Rounded 'TowardNegInf a -> Rounded 'TowardInf a -> Interval a
I (Rounded 'TowardNegInf a -> Rounded 'TowardNegInf a
forall (r :: RoundingMode).
Rounding r =>
Rounded r a -> Rounded r a
f Rounded 'TowardNegInf a
a) (Rounded 'TowardInf a -> Rounded 'TowardInf a
forall (r :: RoundingMode).
Rounding r =>
Rounded r a -> Rounded r a
f Rounded 'TowardInf a
b)
increasing forall (r :: RoundingMode).
Rounding r =>
Rounded r a -> Rounded r a
_ Interval a
Empty   = Interval a
forall a. Interval a
Empty
{-# INLINE increasing #-}

instance (Num a, RoundedRing a) => Num (Interval a) where
  + :: Interval a -> Interval a -> Interval a
(+) = (Interval a -> Interval a -> Interval a)
-> Interval a -> Interval a -> Interval a
forall a.
(Interval a -> Interval a -> Interval a)
-> Interval a -> Interval a -> Interval a
liftBinaryNE Interval a -> Interval a -> Interval a
forall a. Num a => a -> a -> a
(+)
  (-) = (Interval a -> Interval a -> Interval a)
-> Interval a -> Interval a -> Interval a
forall a.
(Interval a -> Interval a -> Interval a)
-> Interval a -> Interval a -> Interval a
liftBinaryNE (-)
  negate :: Interval a -> Interval a
negate = (Interval a -> Interval a) -> Interval a -> Interval a
forall a. (Interval a -> Interval a) -> Interval a -> Interval a
liftUnaryNE Interval a -> Interval a
forall a. Num a => a -> a
negate
  * :: Interval a -> Interval a -> Interval a
(*) = (Interval a -> Interval a -> Interval a)
-> Interval a -> Interval a -> Interval a
forall a.
(Interval a -> Interval a -> Interval a)
-> Interval a -> Interval a -> Interval a
liftBinaryNE Interval a -> Interval a -> Interval a
forall a. Num a => a -> a -> a
(*)
  abs :: Interval a -> Interval a
abs = (Interval a -> Interval a) -> Interval a -> Interval a
forall a. (Interval a -> Interval a) -> Interval a -> Interval a
liftUnaryNE Interval a -> Interval a
forall a. Num a => a -> a
abs
  signum :: Interval a -> Interval a
signum = (Interval a -> Interval a) -> Interval a -> Interval a
forall a. (Interval a -> Interval a) -> Interval a -> Interval a
liftUnaryNE Interval a -> Interval a
forall a. Num a => a -> a
signum
  fromInteger :: Integer -> Interval a
fromInteger Integer
x = case Integer -> (Rounded 'TowardNegInf a, Rounded 'TowardInf a)
forall a.
RoundedRing a =>
Integer -> (Rounded 'TowardNegInf a, Rounded 'TowardInf a)
intervalFromInteger Integer
x of
                    (Rounded 'TowardNegInf a
y, Rounded 'TowardInf a
y') -> Rounded 'TowardNegInf a -> Rounded 'TowardInf a -> Interval a
forall a.
Rounded 'TowardNegInf a -> Rounded 'TowardInf a -> Interval a
I Rounded 'TowardNegInf a
y Rounded 'TowardInf a
y'
  {-# INLINE (+) #-}
  {-# INLINE (-) #-}
  {-# INLINE negate #-}
  {-# INLINE (*) #-}
  {-# INLINE abs #-}
  {-# INLINE signum #-}
  {-# INLINE fromInteger #-}

instance (Num a, RoundedFractional a) => Fractional (Interval a) where
  recip :: Interval a -> Interval a
recip = (Interval a -> Interval a) -> Interval a -> Interval a
forall a. (Interval a -> Interval a) -> Interval a -> Interval a
liftUnaryNE Interval a -> Interval a
forall a. Fractional a => a -> a
recip
  / :: Interval a -> Interval a -> Interval a
(/) = (Interval a -> Interval a -> Interval a)
-> Interval a -> Interval a -> Interval a
forall a.
(Interval a -> Interval a -> Interval a)
-> Interval a -> Interval a -> Interval a
liftBinaryNE Interval a -> Interval a -> Interval a
forall a. Fractional a => a -> a -> a
(/)
  fromRational :: Rational -> Interval a
fromRational Rational
x = case Rational -> (Rounded 'TowardNegInf a, Rounded 'TowardInf a)
forall a.
RoundedFractional a =>
Rational -> (Rounded 'TowardNegInf a, Rounded 'TowardInf a)
intervalFromRational Rational
x of
                     (Rounded 'TowardNegInf a
y, Rounded 'TowardInf a
y') -> Rounded 'TowardNegInf a -> Rounded 'TowardInf a -> Interval a
forall a.
Rounded 'TowardNegInf a -> Rounded 'TowardInf a -> Interval a
I Rounded 'TowardNegInf a
y Rounded 'TowardInf a
y'
  {-# INLINE recip #-}
  {-# INLINE (/) #-}
  {-# INLINE fromRational #-}

maxI :: Ord a => Interval a -> Interval a -> Interval a
maxI :: forall a. Ord a => Interval a -> Interval a -> Interval a
maxI (I Rounded 'TowardNegInf a
a Rounded 'TowardInf a
a') (I Rounded 'TowardNegInf a
b Rounded 'TowardInf a
b') = Rounded 'TowardNegInf a -> Rounded 'TowardInf a -> Interval a
forall a.
Rounded 'TowardNegInf a -> Rounded 'TowardInf a -> Interval a
I (Rounded 'TowardNegInf a
-> Rounded 'TowardNegInf a -> Rounded 'TowardNegInf a
forall a. Ord a => a -> a -> a
max Rounded 'TowardNegInf a
a Rounded 'TowardNegInf a
b) (Rounded 'TowardInf a
-> Rounded 'TowardInf a -> Rounded 'TowardInf a
forall a. Ord a => a -> a -> a
max Rounded 'TowardInf a
a' Rounded 'TowardInf a
b')
maxI Interval a
_ Interval a
_               = Interval a
forall a. Interval a
Empty
{-# SPECIALIZE maxI :: Interval Float -> Interval Float -> Interval Float #-}
{-# SPECIALIZE maxI :: Interval Double -> Interval Double -> Interval Double #-}

minI :: Ord a => Interval a -> Interval a -> Interval a
minI :: forall a. Ord a => Interval a -> Interval a -> Interval a
minI (I Rounded 'TowardNegInf a
a Rounded 'TowardInf a
a') (I Rounded 'TowardNegInf a
b Rounded 'TowardInf a
b') = Rounded 'TowardNegInf a -> Rounded 'TowardInf a -> Interval a
forall a.
Rounded 'TowardNegInf a -> Rounded 'TowardInf a -> Interval a
I (Rounded 'TowardNegInf a
-> Rounded 'TowardNegInf a -> Rounded 'TowardNegInf a
forall a. Ord a => a -> a -> a
min Rounded 'TowardNegInf a
a Rounded 'TowardNegInf a
b) (Rounded 'TowardInf a
-> Rounded 'TowardInf a -> Rounded 'TowardInf a
forall a. Ord a => a -> a -> a
min Rounded 'TowardInf a
a' Rounded 'TowardInf a
b')
minI Interval a
_ Interval a
_               = Interval a
forall a. Interval a
Empty
{-# SPECIALIZE minI :: Interval Float -> Interval Float -> Interval Float #-}
{-# SPECIALIZE minI :: Interval Double -> Interval Double -> Interval Double #-}

powInt :: (Ord a, Num a, RoundedRing a) => Interval a -> Int -> Interval a
powInt :: forall a.
(Ord a, Num a, RoundedRing a) =>
Interval a -> Int -> Interval a
powInt (I Rounded 'TowardNegInf a
a Rounded 'TowardInf a
a') Int
n | Int -> Bool
forall a. Integral a => a -> Bool
odd Int
n Bool -> Bool -> Bool
|| Rounded 'TowardNegInf a
0 Rounded 'TowardNegInf a -> Rounded 'TowardNegInf a -> Bool
forall a. Ord a => a -> a -> Bool
<= Rounded 'TowardNegInf a
a = Rounded 'TowardNegInf a -> Rounded 'TowardInf a -> Interval a
forall a.
Rounded 'TowardNegInf a -> Rounded 'TowardInf a -> Interval a
I (Rounded 'TowardNegInf a
aRounded 'TowardNegInf a -> Int -> Rounded 'TowardNegInf a
forall a b. (Num a, Integral b) => a -> b -> a
^Int
n) (Rounded 'TowardInf a
a'Rounded 'TowardInf a -> Int -> Rounded 'TowardInf a
forall a b. (Num a, Integral b) => a -> b -> a
^Int
n)
                  | Rounded 'TowardInf a
a' Rounded 'TowardInf a -> Rounded 'TowardInf a -> Bool
forall a. Ord a => a -> a -> Bool
<= Rounded 'TowardInf a
0 = Rounded 'TowardNegInf a -> Rounded 'TowardInf a -> Interval a
forall a.
Rounded 'TowardNegInf a -> Rounded 'TowardInf a -> Interval a
I ((Rounded 'TowardInf a -> Rounded 'TowardNegInf a
forall a b. Coercible a b => a -> b
coerce (Rounded 'TowardInf a -> Rounded 'TowardInf a
forall a. Num a => a -> a
abs Rounded 'TowardInf a
a'))Rounded 'TowardNegInf a -> Int -> Rounded 'TowardNegInf a
forall a b. (Num a, Integral b) => a -> b -> a
^Int
n) ((Rounded 'TowardNegInf a -> Rounded 'TowardInf a
forall a b. Coercible a b => a -> b
coerce (Rounded 'TowardNegInf a -> Rounded 'TowardNegInf a
forall a. Num a => a -> a
abs Rounded 'TowardNegInf a
a))Rounded 'TowardInf a -> Int -> Rounded 'TowardInf a
forall a b. (Num a, Integral b) => a -> b -> a
^Int
n)
                  | Bool
otherwise = Rounded 'TowardNegInf a -> Rounded 'TowardInf a -> Interval a
forall a.
Rounded 'TowardNegInf a -> Rounded 'TowardInf a -> Interval a
I Rounded 'TowardNegInf a
0 (Rounded 'TowardInf a
-> Rounded 'TowardInf a -> Rounded 'TowardInf a
forall a. Ord a => a -> a -> a
max ((Rounded 'TowardNegInf a -> Rounded 'TowardInf a
forall a b. Coercible a b => a -> b
coerce (Rounded 'TowardNegInf a -> Rounded 'TowardNegInf a
forall a. Num a => a -> a
abs Rounded 'TowardNegInf a
a))Rounded 'TowardInf a -> Int -> Rounded 'TowardInf a
forall a b. (Num a, Integral b) => a -> b -> a
^Int
n) (Rounded 'TowardInf a
a'Rounded 'TowardInf a -> Int -> Rounded 'TowardInf a
forall a b. (Num a, Integral b) => a -> b -> a
^Int
n))
powInt Interval a
Empty Int
_ = Interval a
forall a. Interval a
Empty
{-# SPECIALIZE powInt :: Interval Float -> Int -> Interval Float #-}
{-# SPECIALIZE powInt :: Interval Double -> Int -> Interval Double #-}

null :: Interval a -> Bool
null :: forall a. Interval a -> Bool
null Interval a
Empty = Bool
True
null Interval a
_     = Bool
False

inf :: Interval a -> Rounded 'TowardNegInf a
inf :: forall a. Interval a -> Rounded 'TowardNegInf a
inf (I Rounded 'TowardNegInf a
x Rounded 'TowardInf a
_) = Rounded 'TowardNegInf a
x
inf Interval a
_       = String -> Rounded 'TowardNegInf a
forall a. HasCallStack => String -> a
error String
"empty interval"

sup :: Interval a -> Rounded 'TowardInf a
sup :: forall a. Interval a -> Rounded 'TowardInf a
sup (I Rounded 'TowardNegInf a
_ Rounded 'TowardInf a
y) = Rounded 'TowardInf a
y
sup Interval a
_       = String -> Rounded 'TowardInf a
forall a. HasCallStack => String -> a
error String
"empty interval"

width :: (Num a, RoundedRing a) => Interval a -> Rounded 'TowardInf a
width :: forall a.
(Num a, RoundedRing a) =>
Interval a -> Rounded 'TowardInf a
width (I Rounded 'TowardNegInf a
x Rounded 'TowardInf a
y) = Rounded 'TowardInf a
y Rounded 'TowardInf a
-> Rounded 'TowardInf a -> Rounded 'TowardInf a
forall a. Num a => a -> a -> a
- Rounded 'TowardNegInf a -> Rounded 'TowardInf a
forall a b. Coercible a b => a -> b
coerce Rounded 'TowardNegInf a
x
width Interval a
Empty   = Rounded 'TowardInf a
0

widthUlp :: (RealFloat a) => Interval a -> Maybe Integer
widthUlp :: forall a. RealFloat a => Interval a -> Maybe Integer
widthUlp (I Rounded 'TowardNegInf a
x Rounded 'TowardInf a
y) = a -> a -> Maybe Integer
forall a. RealFloat a => a -> a -> Maybe Integer
distanceUlp (Rounded 'TowardNegInf a -> a
forall (r :: RoundingMode) a. Rounded r a -> a
getRounded Rounded 'TowardNegInf a
x) (Rounded 'TowardInf a -> a
forall (r :: RoundingMode) a. Rounded r a -> a
getRounded Rounded 'TowardInf a
y)
widthUlp Interval a
Empty   = Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
0

hull :: RoundedRing a => Interval a -> Interval a -> Interval a
hull :: forall a. RoundedRing a => Interval a -> Interval a -> Interval a
hull (I Rounded 'TowardNegInf a
x Rounded 'TowardInf a
y) (I Rounded 'TowardNegInf a
x' Rounded 'TowardInf a
y') = Rounded 'TowardNegInf a -> Rounded 'TowardInf a -> Interval a
forall a.
Rounded 'TowardNegInf a -> Rounded 'TowardInf a -> Interval a
I (Rounded 'TowardNegInf a
-> Rounded 'TowardNegInf a -> Rounded 'TowardNegInf a
forall a. Ord a => a -> a -> a
min Rounded 'TowardNegInf a
x Rounded 'TowardNegInf a
x') (Rounded 'TowardInf a
-> Rounded 'TowardInf a -> Rounded 'TowardInf a
forall a. Ord a => a -> a -> a
max Rounded 'TowardInf a
y Rounded 'TowardInf a
y')
hull Interval a
Empty Interval a
v           = Interval a
v
hull Interval a
u Interval a
Empty           = Interval a
u

intersection :: RoundedRing a => Interval a -> Interval a -> Interval a
intersection :: forall a. RoundedRing a => Interval a -> Interval a -> Interval a
intersection (I Rounded 'TowardNegInf a
x Rounded 'TowardInf a
y) (I Rounded 'TowardNegInf a
x' Rounded 'TowardInf a
y') | Rounded 'TowardNegInf a -> a
forall (r :: RoundingMode) a. Rounded r a -> a
getRounded Rounded 'TowardNegInf a
x'' a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= Rounded 'TowardInf a -> a
forall (r :: RoundingMode) a. Rounded r a -> a
getRounded Rounded 'TowardInf a
y'' = Rounded 'TowardNegInf a -> Rounded 'TowardInf a -> Interval a
forall a.
Rounded 'TowardNegInf a -> Rounded 'TowardInf a -> Interval a
I Rounded 'TowardNegInf a
x'' Rounded 'TowardInf a
y''
  where x'' :: Rounded 'TowardNegInf a
x'' = Rounded 'TowardNegInf a
-> Rounded 'TowardNegInf a -> Rounded 'TowardNegInf a
forall a. Ord a => a -> a -> a
max Rounded 'TowardNegInf a
x Rounded 'TowardNegInf a
x'
        y'' :: Rounded 'TowardInf a
y'' = Rounded 'TowardInf a
-> Rounded 'TowardInf a -> Rounded 'TowardInf a
forall a. Ord a => a -> a -> a
min Rounded 'TowardInf a
y Rounded 'TowardInf a
y'
intersection Interval a
_ Interval a
_ = Interval a
forall a. Interval a
Empty

liftUnaryNE :: (NE.Interval a -> NE.Interval a) -> Interval a -> Interval a
liftUnaryNE :: forall a. (Interval a -> Interval a) -> Interval a -> Interval a
liftUnaryNE Interval a -> Interval a
f (I Rounded 'TowardNegInf a
x Rounded 'TowardInf a
x') = case Interval a -> Interval a
f (Rounded 'TowardNegInf a -> Rounded 'TowardInf a -> Interval a
forall a.
Rounded 'TowardNegInf a -> Rounded 'TowardInf a -> Interval a
NE.I Rounded 'TowardNegInf a
x Rounded 'TowardInf a
x') of
                           NE.I Rounded 'TowardNegInf a
y Rounded 'TowardInf a
y' -> Rounded 'TowardNegInf a -> Rounded 'TowardInf a -> Interval a
forall a.
Rounded 'TowardNegInf a -> Rounded 'TowardInf a -> Interval a
I Rounded 'TowardNegInf a
y Rounded 'TowardInf a
y'
liftUnaryNE Interval a -> Interval a
_f Interval a
Empty = Interval a
forall a. Interval a
Empty
{-# INLINE [1] liftUnaryNE #-}

liftBinaryNE :: (NE.Interval a -> NE.Interval a -> NE.Interval a) -> Interval a -> Interval a -> Interval a
liftBinaryNE :: forall a.
(Interval a -> Interval a -> Interval a)
-> Interval a -> Interval a -> Interval a
liftBinaryNE Interval a -> Interval a -> Interval a
f (I Rounded 'TowardNegInf a
x Rounded 'TowardInf a
x') (I Rounded 'TowardNegInf a
y Rounded 'TowardInf a
y') = case Interval a -> Interval a -> Interval a
f (Rounded 'TowardNegInf a -> Rounded 'TowardInf a -> Interval a
forall a.
Rounded 'TowardNegInf a -> Rounded 'TowardInf a -> Interval a
NE.I Rounded 'TowardNegInf a
x Rounded 'TowardInf a
x') (Rounded 'TowardNegInf a -> Rounded 'TowardInf a -> Interval a
forall a.
Rounded 'TowardNegInf a -> Rounded 'TowardInf a -> Interval a
NE.I Rounded 'TowardNegInf a
y Rounded 'TowardInf a
y') of
                                     NE.I Rounded 'TowardNegInf a
z Rounded 'TowardInf a
z' -> Rounded 'TowardNegInf a -> Rounded 'TowardInf a -> Interval a
forall a.
Rounded 'TowardNegInf a -> Rounded 'TowardInf a -> Interval a
I Rounded 'TowardNegInf a
z Rounded 'TowardInf a
z'
liftBinaryNE Interval a -> Interval a -> Interval a
_f Interval a
_ Interval a
_ = Interval a
forall a. Interval a
Empty
{-# INLINE [1] liftBinaryNE #-}

instance (Num a, RoundedFractional a, RoundedSqrt a, Eq a, RealFloat a, RealFloatConstants a) => Floating (Interval a) where
  pi :: Interval a
pi = Rounded 'TowardNegInf a -> Rounded 'TowardInf a -> Interval a
forall a.
Rounded 'TowardNegInf a -> Rounded 'TowardInf a -> Interval a
I Rounded 'TowardNegInf a
forall a. RealFloatConstants a => Rounded 'TowardNegInf a
pi_down Rounded 'TowardInf a
forall a. RealFloatConstants a => Rounded 'TowardInf a
pi_up
  exp :: Interval a -> Interval a
exp = (Interval a -> Interval a) -> Interval a -> Interval a
forall a. (Interval a -> Interval a) -> Interval a -> Interval a
liftUnaryNE Interval a -> Interval a
forall a. Floating a => a -> a
exp
  log :: Interval a -> Interval a
log = (Interval a -> Interval a) -> Interval a -> Interval a
forall a. (Interval a -> Interval a) -> Interval a -> Interval a
liftUnaryNE Interval a -> Interval a
forall a. Floating a => a -> a
log
  sqrt :: Interval a -> Interval a
sqrt = (Interval a -> Interval a) -> Interval a -> Interval a
forall a. (Interval a -> Interval a) -> Interval a -> Interval a
liftUnaryNE Interval a -> Interval a
forall a. Floating a => a -> a
sqrt
  ** :: Interval a -> Interval a -> Interval a
(**) = (Interval a -> Interval a -> Interval a)
-> Interval a -> Interval a -> Interval a
forall a.
(Interval a -> Interval a -> Interval a)
-> Interval a -> Interval a -> Interval a
liftBinaryNE Interval a -> Interval a -> Interval a
forall a. Floating a => a -> a -> a
(**)
  logBase :: Interval a -> Interval a -> Interval a
logBase = (Interval a -> Interval a -> Interval a)
-> Interval a -> Interval a -> Interval a
forall a.
(Interval a -> Interval a -> Interval a)
-> Interval a -> Interval a -> Interval a
liftBinaryNE Interval a -> Interval a -> Interval a
forall a. Floating a => a -> a -> a
logBase
  sin :: Interval a -> Interval a
sin = (Interval a -> Interval a) -> Interval a -> Interval a
forall a. (Interval a -> Interval a) -> Interval a -> Interval a
liftUnaryNE Interval a -> Interval a
forall a. Floating a => a -> a
sin
  cos :: Interval a -> Interval a
cos = (Interval a -> Interval a) -> Interval a -> Interval a
forall a. (Interval a -> Interval a) -> Interval a -> Interval a
liftUnaryNE Interval a -> Interval a
forall a. Floating a => a -> a
cos
  tan :: Interval a -> Interval a
tan = (Interval a -> Interval a) -> Interval a -> Interval a
forall a. (Interval a -> Interval a) -> Interval a -> Interval a
liftUnaryNE Interval a -> Interval a
forall a. Floating a => a -> a
tan
  asin :: Interval a -> Interval a
asin = (Interval a -> Interval a) -> Interval a -> Interval a
forall a. (Interval a -> Interval a) -> Interval a -> Interval a
liftUnaryNE Interval a -> Interval a
forall a. Floating a => a -> a
asin
  acos :: Interval a -> Interval a
acos = (Interval a -> Interval a) -> Interval a -> Interval a
forall a. (Interval a -> Interval a) -> Interval a -> Interval a
liftUnaryNE Interval a -> Interval a
forall a. Floating a => a -> a
acos
  atan :: Interval a -> Interval a
atan = (Interval a -> Interval a) -> Interval a -> Interval a
forall a. (Interval a -> Interval a) -> Interval a -> Interval a
liftUnaryNE Interval a -> Interval a
forall a. Floating a => a -> a
atan
  sinh :: Interval a -> Interval a
sinh = (Interval a -> Interval a) -> Interval a -> Interval a
forall a. (Interval a -> Interval a) -> Interval a -> Interval a
liftUnaryNE Interval a -> Interval a
forall a. Floating a => a -> a
sinh
  cosh :: Interval a -> Interval a
cosh = (Interval a -> Interval a) -> Interval a -> Interval a
forall a. (Interval a -> Interval a) -> Interval a -> Interval a
liftUnaryNE Interval a -> Interval a
forall a. Floating a => a -> a
cosh
  tanh :: Interval a -> Interval a
tanh = (Interval a -> Interval a) -> Interval a -> Interval a
forall a. (Interval a -> Interval a) -> Interval a -> Interval a
liftUnaryNE Interval a -> Interval a
forall a. Floating a => a -> a
tanh
  asinh :: Interval a -> Interval a
asinh = (Interval a -> Interval a) -> Interval a -> Interval a
forall a. (Interval a -> Interval a) -> Interval a -> Interval a
liftUnaryNE Interval a -> Interval a
forall a. Floating a => a -> a
asinh
  acosh :: Interval a -> Interval a
acosh = (Interval a -> Interval a) -> Interval a -> Interval a
forall a. (Interval a -> Interval a) -> Interval a -> Interval a
liftUnaryNE Interval a -> Interval a
forall a. Floating a => a -> a
acosh
  atanh :: Interval a -> Interval a
atanh = (Interval a -> Interval a) -> Interval a -> Interval a
forall a. (Interval a -> Interval a) -> Interval a -> Interval a
liftUnaryNE Interval a -> Interval a
forall a. Floating a => a -> a
atanh
  log1p :: Interval a -> Interval a
log1p = (Interval a -> Interval a) -> Interval a -> Interval a
forall a. (Interval a -> Interval a) -> Interval a -> Interval a
liftUnaryNE Interval a -> Interval a
forall a. Floating a => a -> a
log1p
  expm1 :: Interval a -> Interval a
expm1 = (Interval a -> Interval a) -> Interval a -> Interval a
forall a. (Interval a -> Interval a) -> Interval a -> Interval a
liftUnaryNE Interval a -> Interval a
forall a. Floating a => a -> a
expm1
  log1pexp :: Interval a -> Interval a
log1pexp = (Interval a -> Interval a) -> Interval a -> Interval a
forall a. (Interval a -> Interval a) -> Interval a -> Interval a
liftUnaryNE Interval a -> Interval a
forall a. Floating a => a -> a
log1pexp
  log1mexp :: Interval a -> Interval a
log1mexp = (Interval a -> Interval a) -> Interval a -> Interval a
forall a. (Interval a -> Interval a) -> Interval a -> Interval a
liftUnaryNE Interval a -> Interval a
forall a. Floating a => a -> a
log1mexp
  {-# INLINE exp #-}
  {-# INLINE log #-}
  {-# INLINE sqrt #-}
  {-# INLINE (**) #-}
  {-# INLINE logBase #-}
  {-# INLINE sin #-}
  {-# INLINE cos #-}
  {-# INLINE tan #-}
  {-# INLINE asin #-}
  {-# INLINE acos #-}
  {-# INLINE atan #-}
  {-# INLINE sinh #-}
  {-# INLINE cosh #-}
  {-# INLINE tanh #-}
  {-# INLINE asinh #-}
  {-# INLINE acosh #-}
  {-# INLINE atanh #-}
  {-# INLINE log1p #-}
  {-# INLINE expm1 #-}
  {-# INLINE log1pexp #-}
  {-# INLINE log1mexp #-}

instance (Num a, RoundedRing a, RealFloat a) => C.IsInterval (Interval a) where
  type EndPoint (Interval a) = a
  makeInterval :: Rounded 'TowardNegInf (EndPoint (Interval a))
-> Rounded 'TowardInf (EndPoint (Interval a)) -> Interval a
makeInterval = Rounded 'TowardNegInf a -> Rounded 'TowardInf a -> Interval a
Rounded 'TowardNegInf (EndPoint (Interval a))
-> Rounded 'TowardInf (EndPoint (Interval a)) -> Interval a
forall a.
Rounded 'TowardNegInf a -> Rounded 'TowardInf a -> Interval a
I
  width :: Interval a -> Rounded 'TowardInf (EndPoint (Interval a))
width = Interval a -> Rounded 'TowardInf a
Interval a -> Rounded 'TowardInf (EndPoint (Interval a))
forall a.
(Num a, RoundedRing a) =>
Interval a -> Rounded 'TowardInf a
width
  withEndPoints :: (Rounded 'TowardNegInf (EndPoint (Interval a))
 -> Rounded 'TowardInf (EndPoint (Interval a)) -> Interval a)
-> Interval a -> Interval a
withEndPoints Rounded 'TowardNegInf (EndPoint (Interval a))
-> Rounded 'TowardInf (EndPoint (Interval a)) -> Interval a
f (I Rounded 'TowardNegInf a
x Rounded 'TowardInf a
y) = Rounded 'TowardNegInf (EndPoint (Interval a))
-> Rounded 'TowardInf (EndPoint (Interval a)) -> Interval a
f Rounded 'TowardNegInf a
Rounded 'TowardNegInf (EndPoint (Interval a))
x Rounded 'TowardInf a
Rounded 'TowardInf (EndPoint (Interval a))
y
  withEndPoints Rounded 'TowardNegInf (EndPoint (Interval a))
-> Rounded 'TowardInf (EndPoint (Interval a)) -> Interval a
_ Interval a
Empty   = Interval a
forall a. Interval a
Empty
  hull :: Interval a -> Interval a -> Interval a
hull = Interval a -> Interval a -> Interval a
forall a. RoundedRing a => Interval a -> Interval a -> Interval a
hull
  intersection :: Interval a -> Interval a -> Interval a
intersection = Interval a -> Interval a -> Interval a
forall a. RoundedRing a => Interval a -> Interval a -> Interval a
intersection
  maybeIntersection :: Interval a -> Interval a -> Maybe (Interval a)
maybeIntersection Interval a
x Interval a
y = case Interval a -> Interval a -> Interval a
forall a. RoundedRing a => Interval a -> Interval a -> Interval a
intersection Interval a
x Interval a
y of
                            Interval a
Empty -> Maybe (Interval a)
forall a. Maybe a
Nothing
                            Interval a
z     -> Interval a -> Maybe (Interval a)
forall a. a -> Maybe a
Just Interval a
z
  equalAsSet :: Interval a -> Interval a -> Bool
equalAsSet (I Rounded 'TowardNegInf a
x Rounded 'TowardInf a
y) (I Rounded 'TowardNegInf a
x' Rounded 'TowardInf a
y') = Rounded 'TowardNegInf a
x Rounded 'TowardNegInf a -> Rounded 'TowardNegInf a -> Bool
forall a. Eq a => a -> a -> Bool
== Rounded 'TowardNegInf a
x' Bool -> Bool -> Bool
&& Rounded 'TowardInf a
y Rounded 'TowardInf a -> Rounded 'TowardInf a -> Bool
forall a. Eq a => a -> a -> Bool
== Rounded 'TowardInf a
y'
  equalAsSet Interval a
Empty Interval a
Empty       = Bool
True
  equalAsSet Interval a
_ Interval a
_               = Bool
False
  subset :: Interval a -> Interval a -> Bool
subset (I Rounded 'TowardNegInf a
x Rounded 'TowardInf a
y) (I Rounded 'TowardNegInf a
x' Rounded 'TowardInf a
y') = Rounded 'TowardNegInf a
x' Rounded 'TowardNegInf a -> Rounded 'TowardNegInf a -> Bool
forall a. Ord a => a -> a -> Bool
<= Rounded 'TowardNegInf a
x Bool -> Bool -> Bool
&& Rounded 'TowardInf a
y Rounded 'TowardInf a -> Rounded 'TowardInf a -> Bool
forall a. Ord a => a -> a -> Bool
<= Rounded 'TowardInf a
y'
  subset Interval a
Empty Interval a
_           = Bool
True
  subset I{} Interval a
Empty         = Bool
False
  weaklyLess :: Interval a -> Interval a -> Bool
weaklyLess (I Rounded 'TowardNegInf a
x Rounded 'TowardInf a
y) (I Rounded 'TowardNegInf a
x' Rounded 'TowardInf a
y') = Rounded 'TowardNegInf a
x Rounded 'TowardNegInf a -> Rounded 'TowardNegInf a -> Bool
forall a. Ord a => a -> a -> Bool
<= Rounded 'TowardNegInf a
x' Bool -> Bool -> Bool
&& Rounded 'TowardInf a
y Rounded 'TowardInf a -> Rounded 'TowardInf a -> Bool
forall a. Ord a => a -> a -> Bool
<= Rounded 'TowardInf a
y'
  weaklyLess Interval a
Empty Interval a
Empty       = Bool
True
  weaklyLess Interval a
_ Interval a
_               = Bool
False
  precedes :: Interval a -> Interval a -> Bool
precedes (I Rounded 'TowardNegInf a
_ Rounded 'TowardInf a
y) (I Rounded 'TowardNegInf a
x' Rounded 'TowardInf a
_) = Rounded 'TowardInf a -> a
forall (r :: RoundingMode) a. Rounded r a -> a
getRounded Rounded 'TowardInf a
y a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= Rounded 'TowardNegInf a -> a
forall (r :: RoundingMode) a. Rounded r a -> a
getRounded Rounded 'TowardNegInf a
x'
  precedes Interval a
_ Interval a
_              = Bool
True
  interior :: Interval a -> Interval a -> Bool
interior (I Rounded 'TowardNegInf a
x Rounded 'TowardInf a
y) (I Rounded 'TowardNegInf a
x' Rounded 'TowardInf a
y') = Rounded 'TowardNegInf a -> a
forall (r :: RoundingMode) a. Rounded r a -> a
getRounded Rounded 'TowardNegInf a
x' a -> a -> Bool
forall {a}. RealFloat a => a -> a -> Bool
<# Rounded 'TowardNegInf a -> a
forall (r :: RoundingMode) a. Rounded r a -> a
getRounded Rounded 'TowardNegInf a
x Bool -> Bool -> Bool
&& Rounded 'TowardInf a -> a
forall (r :: RoundingMode) a. Rounded r a -> a
getRounded Rounded 'TowardInf a
y a -> a -> Bool
forall {a}. RealFloat a => a -> a -> Bool
<# Rounded 'TowardInf a -> a
forall (r :: RoundingMode) a. Rounded r a -> a
getRounded Rounded 'TowardInf a
y'
    where a
s <# :: a -> a -> Bool
<# a
t = a
s a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
t Bool -> Bool -> Bool
|| (a
s a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
t Bool -> Bool -> Bool
&& a -> Bool
forall a. RealFloat a => a -> Bool
isInfinite a
s)
  interior Interval a
Empty Interval a
_ = Bool
True
  interior I{} Interval a
Empty = Bool
False
  strictLess :: Interval a -> Interval a -> Bool
strictLess (I Rounded 'TowardNegInf a
x Rounded 'TowardInf a
y) (I Rounded 'TowardNegInf a
x' Rounded 'TowardInf a
y') = Rounded 'TowardNegInf a -> a
forall (r :: RoundingMode) a. Rounded r a -> a
getRounded Rounded 'TowardNegInf a
x a -> a -> Bool
forall {a}. RealFloat a => a -> a -> Bool
<# Rounded 'TowardNegInf a -> a
forall (r :: RoundingMode) a. Rounded r a -> a
getRounded Rounded 'TowardNegInf a
x' Bool -> Bool -> Bool
&& Rounded 'TowardInf a -> a
forall (r :: RoundingMode) a. Rounded r a -> a
getRounded Rounded 'TowardInf a
y a -> a -> Bool
forall {a}. RealFloat a => a -> a -> Bool
<# Rounded 'TowardInf a -> a
forall (r :: RoundingMode) a. Rounded r a -> a
getRounded Rounded 'TowardInf a
y'
    where a
s <# :: a -> a -> Bool
<# a
t = a
s a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
t Bool -> Bool -> Bool
|| (a
s a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
t Bool -> Bool -> Bool
&& a -> Bool
forall a. RealFloat a => a -> Bool
isInfinite a
s)
  strictLess Interval a
Empty Interval a
Empty = Bool
True
  strictLess Interval a
_ Interval a
_ = Bool
False
  strictPrecedes :: Interval a -> Interval a -> Bool
strictPrecedes (I Rounded 'TowardNegInf a
_ Rounded 'TowardInf a
y) (I Rounded 'TowardNegInf a
x' Rounded 'TowardInf a
_) = Rounded 'TowardInf a -> a
forall (r :: RoundingMode) a. Rounded r a -> a
getRounded Rounded 'TowardInf a
y a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< Rounded 'TowardNegInf a -> a
forall (r :: RoundingMode) a. Rounded r a -> a
getRounded Rounded 'TowardNegInf a
x'
  strictPrecedes Interval a
_ Interval a
_              = Bool
True
  disjoint :: Interval a -> Interval a -> Bool
disjoint (I Rounded 'TowardNegInf a
x Rounded 'TowardInf a
y) (I Rounded 'TowardNegInf a
x' Rounded 'TowardInf a
y') = Rounded 'TowardInf a -> a
forall (r :: RoundingMode) a. Rounded r a -> a
getRounded Rounded 'TowardInf a
y a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< Rounded 'TowardNegInf a -> a
forall (r :: RoundingMode) a. Rounded r a -> a
getRounded Rounded 'TowardNegInf a
x' Bool -> Bool -> Bool
|| Rounded 'TowardInf a -> a
forall (r :: RoundingMode) a. Rounded r a -> a
getRounded Rounded 'TowardInf a
y' a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< Rounded 'TowardNegInf a -> a
forall (r :: RoundingMode) a. Rounded r a -> a
getRounded Rounded 'TowardNegInf a
x
  disjoint Interval a
_ Interval a
_ = Bool
True

--
-- Instance for Data.Vector.Unboxed.Unbox
--

newtype instance VUM.MVector s (Interval a) = MV_Interval (VUM.MVector s (a, a))
newtype instance VU.Vector (Interval a) = V_Interval (VU.Vector (a, a))

intervalToPair :: Fractional a => Interval a -> (a, a)
intervalToPair :: forall a. Fractional a => Interval a -> (a, a)
intervalToPair (I (Rounded a
x) (Rounded a
y)) = (a
x, a
y)
intervalToPair Interval a
Empty                       = (a
1a -> a -> a
forall a. Fractional a => a -> a -> a
/a
0, -a
1a -> a -> a
forall a. Fractional a => a -> a -> a
/a
0)
{-# INLINE intervalToPair #-}

pairToInterval :: Ord a => (a, a) -> Interval a
pairToInterval :: forall a. Ord a => (a, a) -> Interval a
pairToInterval (a
x, a
y) | a
y a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
x = Interval a
forall a. Interval a
Empty
                      | Bool
otherwise = Rounded 'TowardNegInf a -> Rounded 'TowardInf a -> Interval a
forall a.
Rounded 'TowardNegInf a -> Rounded 'TowardInf a -> Interval a
I (a -> Rounded 'TowardNegInf a
forall (r :: RoundingMode) a. a -> Rounded r a
Rounded a
x) (a -> Rounded 'TowardInf a
forall (r :: RoundingMode) a. a -> Rounded r a
Rounded a
y)
{-# INLINE pairToInterval #-}

instance (VU.Unbox a, Ord a, Fractional a) => VGM.MVector VUM.MVector (Interval a) where
  basicLength :: forall s. MVector s (Interval a) -> Int
basicLength (MV_Interval MVector s (a, a)
mv) = MVector s (a, a) -> Int
forall s. MVector s (a, a) -> Int
forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
VGM.basicLength MVector s (a, a)
mv
  basicUnsafeSlice :: forall s.
Int -> Int -> MVector s (Interval a) -> MVector s (Interval a)
basicUnsafeSlice Int
i Int
l (MV_Interval MVector s (a, a)
mv) = MVector s (a, a) -> MVector s (Interval a)
forall s a. MVector s (a, a) -> MVector s (Interval a)
MV_Interval (Int -> Int -> MVector s (a, a) -> MVector s (a, a)
forall s. Int -> Int -> MVector s (a, a) -> MVector s (a, a)
forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
VGM.basicUnsafeSlice Int
i Int
l MVector s (a, a)
mv)
  basicOverlaps :: forall s. MVector s (Interval a) -> MVector s (Interval a) -> Bool
basicOverlaps (MV_Interval MVector s (a, a)
mv) (MV_Interval MVector s (a, a)
mv') = MVector s (a, a) -> MVector s (a, a) -> Bool
forall s. MVector s (a, a) -> MVector s (a, a) -> Bool
forall (v :: * -> * -> *) a s.
MVector v a =>
v s a -> v s a -> Bool
VGM.basicOverlaps MVector s (a, a)
mv MVector s (a, a)
mv'
  basicUnsafeNew :: forall s. Int -> ST s (MVector s (Interval a))
basicUnsafeNew Int
l = MVector s (a, a) -> MVector s (Interval a)
forall s a. MVector s (a, a) -> MVector s (Interval a)
MV_Interval (MVector s (a, a) -> MVector s (Interval a))
-> ST s (MVector s (a, a)) -> ST s (MVector s (Interval a))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> ST s (MVector s (a, a))
forall s. Int -> ST s (MVector s (a, a))
forall (v :: * -> * -> *) a s. MVector v a => Int -> ST s (v s a)
VGM.basicUnsafeNew Int
l
  basicInitialize :: forall s. MVector s (Interval a) -> ST s ()
basicInitialize (MV_Interval MVector s (a, a)
mv) = MVector s (a, a) -> ST s ()
forall s. MVector s (a, a) -> ST s ()
forall (v :: * -> * -> *) a s. MVector v a => v s a -> ST s ()
VGM.basicInitialize MVector s (a, a)
mv
  basicUnsafeReplicate :: forall s. Int -> Interval a -> ST s (MVector s (Interval a))
basicUnsafeReplicate Int
i Interval a
x = MVector s (a, a) -> MVector s (Interval a)
forall s a. MVector s (a, a) -> MVector s (Interval a)
MV_Interval (MVector s (a, a) -> MVector s (Interval a))
-> ST s (MVector s (a, a)) -> ST s (MVector s (Interval a))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> (a, a) -> ST s (MVector s (a, a))
forall s. Int -> (a, a) -> ST s (MVector s (a, a))
forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> a -> ST s (v s a)
VGM.basicUnsafeReplicate Int
i (Interval a -> (a, a)
forall a. Fractional a => Interval a -> (a, a)
intervalToPair Interval a
x)
  basicUnsafeRead :: forall s. MVector s (Interval a) -> Int -> ST s (Interval a)
basicUnsafeRead (MV_Interval MVector s (a, a)
mv) Int
i = (a, a) -> Interval a
forall a. Ord a => (a, a) -> Interval a
pairToInterval ((a, a) -> Interval a) -> ST s (a, a) -> ST s (Interval a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> MVector s (a, a) -> Int -> ST s (a, a)
forall s. MVector s (a, a) -> Int -> ST s (a, a)
forall (v :: * -> * -> *) a s.
MVector v a =>
v s a -> Int -> ST s a
VGM.basicUnsafeRead MVector s (a, a)
mv Int
i
  basicUnsafeWrite :: forall s. MVector s (Interval a) -> Int -> Interval a -> ST s ()
basicUnsafeWrite (MV_Interval MVector s (a, a)
mv) Int
i Interval a
x = MVector s (a, a) -> Int -> (a, a) -> ST s ()
forall s. MVector s (a, a) -> Int -> (a, a) -> ST s ()
forall (v :: * -> * -> *) a s.
MVector v a =>
v s a -> Int -> a -> ST s ()
VGM.basicUnsafeWrite MVector s (a, a)
mv Int
i (Interval a -> (a, a)
forall a. Fractional a => Interval a -> (a, a)
intervalToPair Interval a
x)
  basicClear :: forall s. MVector s (Interval a) -> ST s ()
basicClear (MV_Interval MVector s (a, a)
mv) = MVector s (a, a) -> ST s ()
forall s. MVector s (a, a) -> ST s ()
forall (v :: * -> * -> *) a s. MVector v a => v s a -> ST s ()
VGM.basicClear MVector s (a, a)
mv
  basicSet :: forall s. MVector s (Interval a) -> Interval a -> ST s ()
basicSet (MV_Interval MVector s (a, a)
mv) Interval a
x = MVector s (a, a) -> (a, a) -> ST s ()
forall s. MVector s (a, a) -> (a, a) -> ST s ()
forall (v :: * -> * -> *) a s. MVector v a => v s a -> a -> ST s ()
VGM.basicSet MVector s (a, a)
mv (Interval a -> (a, a)
forall a. Fractional a => Interval a -> (a, a)
intervalToPair Interval a
x)
  basicUnsafeCopy :: forall s.
MVector s (Interval a) -> MVector s (Interval a) -> ST s ()
basicUnsafeCopy (MV_Interval MVector s (a, a)
mv) (MV_Interval MVector s (a, a)
mv') = MVector s (a, a) -> MVector s (a, a) -> ST s ()
forall s. MVector s (a, a) -> MVector s (a, a) -> ST s ()
forall (v :: * -> * -> *) a s.
MVector v a =>
v s a -> v s a -> ST s ()
VGM.basicUnsafeCopy MVector s (a, a)
mv MVector s (a, a)
mv'
  basicUnsafeMove :: forall s.
MVector s (Interval a) -> MVector s (Interval a) -> ST s ()
basicUnsafeMove (MV_Interval MVector s (a, a)
mv) (MV_Interval MVector s (a, a)
mv') = MVector s (a, a) -> MVector s (a, a) -> ST s ()
forall s. MVector s (a, a) -> MVector s (a, a) -> ST s ()
forall (v :: * -> * -> *) a s.
MVector v a =>
v s a -> v s a -> ST s ()
VGM.basicUnsafeMove MVector s (a, a)
mv MVector s (a, a)
mv'
  basicUnsafeGrow :: forall s.
MVector s (Interval a) -> Int -> ST s (MVector s (Interval a))
basicUnsafeGrow (MV_Interval MVector s (a, a)
mv) Int
n = MVector s (a, a) -> MVector s (Interval a)
forall s a. MVector s (a, a) -> MVector s (Interval a)
MV_Interval (MVector s (a, a) -> MVector s (Interval a))
-> ST s (MVector s (a, a)) -> ST s (MVector s (Interval a))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> MVector s (a, a) -> Int -> ST s (MVector s (a, a))
forall s. MVector s (a, a) -> Int -> ST s (MVector s (a, a))
forall (v :: * -> * -> *) a s.
MVector v a =>
v s a -> Int -> ST s (v s a)
VGM.basicUnsafeGrow MVector s (a, a)
mv Int
n
  {-# INLINE basicLength #-}
  {-# INLINE basicUnsafeSlice #-}
  {-# INLINE basicOverlaps #-}
  {-# INLINE basicUnsafeNew #-}
  {-# INLINE basicInitialize #-}
  {-# INLINE basicUnsafeReplicate #-}
  {-# INLINE basicUnsafeRead #-}
  {-# INLINE basicUnsafeWrite #-}
  {-# INLINE basicClear #-}
  {-# INLINE basicSet #-}
  {-# INLINE basicUnsafeCopy #-}
  {-# INLINE basicUnsafeMove #-}
  {-# INLINE basicUnsafeGrow #-}

instance (VU.Unbox a, Ord a, Fractional a) => VG.Vector VU.Vector (Interval a) where
  basicUnsafeFreeze :: forall s.
Mutable Vector s (Interval a) -> ST s (Vector (Interval a))
basicUnsafeFreeze (MV_Interval MVector s (a, a)
mv) = Vector (a, a) -> Vector (Interval a)
forall a. Vector (a, a) -> Vector (Interval a)
V_Interval (Vector (a, a) -> Vector (Interval a))
-> ST s (Vector (a, a)) -> ST s (Vector (Interval a))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Mutable Vector s (a, a) -> ST s (Vector (a, a))
forall s. Mutable Vector s (a, a) -> ST s (Vector (a, a))
forall (v :: * -> *) a s. Vector v a => Mutable v s a -> ST s (v a)
VG.basicUnsafeFreeze Mutable Vector s (a, a)
MVector s (a, a)
mv
  basicUnsafeThaw :: forall s.
Vector (Interval a) -> ST s (Mutable Vector s (Interval a))
basicUnsafeThaw (V_Interval Vector (a, a)
v) = MVector s (a, a) -> MVector s (Interval a)
forall s a. MVector s (a, a) -> MVector s (Interval a)
MV_Interval (MVector s (a, a) -> MVector s (Interval a))
-> ST s (MVector s (a, a)) -> ST s (MVector s (Interval a))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Vector (a, a) -> ST s (Mutable Vector s (a, a))
forall s. Vector (a, a) -> ST s (Mutable Vector s (a, a))
forall (v :: * -> *) a s. Vector v a => v a -> ST s (Mutable v s a)
VG.basicUnsafeThaw Vector (a, a)
v
  basicLength :: Vector (Interval a) -> Int
basicLength (V_Interval Vector (a, a)
v) = Vector (a, a) -> Int
forall (v :: * -> *) a. Vector v a => v a -> Int
VG.basicLength Vector (a, a)
v
  basicUnsafeSlice :: Int -> Int -> Vector (Interval a) -> Vector (Interval a)
basicUnsafeSlice Int
i Int
l (V_Interval Vector (a, a)
v) = Vector (a, a) -> Vector (Interval a)
forall a. Vector (a, a) -> Vector (Interval a)
V_Interval (Int -> Int -> Vector (a, a) -> Vector (a, a)
forall (v :: * -> *) a. Vector v a => Int -> Int -> v a -> v a
VG.basicUnsafeSlice Int
i Int
l Vector (a, a)
v)
  basicUnsafeIndexM :: Vector (Interval a) -> Int -> Box (Interval a)
basicUnsafeIndexM (V_Interval Vector (a, a)
v) Int
i = (a, a) -> Interval a
forall a. Ord a => (a, a) -> Interval a
pairToInterval ((a, a) -> Interval a) -> Box (a, a) -> Box (Interval a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Vector (a, a) -> Int -> Box (a, a)
forall (v :: * -> *) a. Vector v a => v a -> Int -> Box a
VG.basicUnsafeIndexM Vector (a, a)
v Int
i
  basicUnsafeCopy :: forall s.
Mutable Vector s (Interval a) -> Vector (Interval a) -> ST s ()
basicUnsafeCopy (MV_Interval MVector s (a, a)
mv) (V_Interval Vector (a, a)
v) = Mutable Vector s (a, a) -> Vector (a, a) -> ST s ()
forall s. Mutable Vector s (a, a) -> Vector (a, a) -> ST s ()
forall (v :: * -> *) a s.
Vector v a =>
Mutable v s a -> v a -> ST s ()
VG.basicUnsafeCopy Mutable Vector s (a, a)
MVector s (a, a)
mv Vector (a, a)
v
  elemseq :: forall b. Vector (Interval a) -> Interval a -> b -> b
elemseq (V_Interval Vector (a, a)
_) Interval a
x b
y = Interval a
x Interval a -> b -> b
forall a b. a -> b -> b
`seq` b
y
  {-# INLINE basicUnsafeFreeze #-}
  {-# INLINE basicUnsafeThaw #-}
  {-# INLINE basicLength #-}
  {-# INLINE basicUnsafeSlice #-}
  {-# INLINE basicUnsafeIndexM #-}
  {-# INLINE basicUnsafeCopy #-}
  {-# INLINE elemseq #-}

instance (VU.Unbox a, Ord a, Fractional a) => VU.Unbox (Interval a)

--
-- Instances for Data.Array.Unboxed
--

instance (Prim a, Ord a, Fractional a) => A.MArray (A.STUArray s) (Interval a) (ST s) where
  getBounds :: forall i. Ix i => STUArray s i (Interval a) -> ST s (i, i)
getBounds (A.STUArray i
l i
u Int
_ MutableByteArray# s
_) = (i, i) -> ST s (i, i)
forall a. a -> ST s a
forall (m :: * -> *) a. Monad m => a -> m a
return (i
l, i
u)
  getNumElements :: forall i. Ix i => STUArray s i (Interval a) -> ST s Int
getNumElements (A.STUArray i
_ i
_ Int
n MutableByteArray# s
_) = Int -> ST s Int
forall a. a -> ST s a
forall (m :: * -> *) a. Monad m => a -> m a
return Int
n
  -- newArray: Use default
  unsafeNewArray_ :: forall i. Ix i => (i, i) -> ST s (STUArray s i (Interval a))
unsafeNewArray_ = (i, i) -> ST s (STUArray s i (Interval a))
forall i. Ix i => (i, i) -> ST s (STUArray s i (Interval a))
forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
(i, i) -> m (a i e)
A.newArray_
  newArray_ :: forall i. Ix i => (i, i) -> ST s (STUArray s i (Interval a))
newArray_ bounds :: (i, i)
bounds@(i
l,i
u) = do
    let n :: Int
n = (i, i) -> Int
forall a. Ix a => (a, a) -> Int
rangeSize (i, i)
bounds
    arr :: MutableByteArray s
arr@(MutableByteArray MutableByteArray# s
arr_) <- Int -> ST s (MutableByteArray (PrimState (ST s)))
forall (m :: * -> *).
PrimMonad m =>
Int -> m (MutableByteArray (PrimState m))
newByteArray (Int
2 Int -> Int -> Int
forall a. Num a => a -> a -> a
* a -> Int
forall a. Prim a => a -> Int
sizeOf (a
forall a. HasCallStack => a
undefined :: a) Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
n)
    MutableByteArray (PrimState (ST s)) -> Int -> Int -> a -> ST s ()
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutableByteArray (PrimState m) -> Int -> Int -> a -> m ()
setByteArray MutableByteArray s
MutableByteArray (PrimState (ST s))
arr Int
0 (Int
2 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
n) (a
0 :: a)
    STUArray s i (Interval a) -> ST s (STUArray s i (Interval a))
forall a. a -> ST s a
forall (m :: * -> *) a. Monad m => a -> m a
return (i -> i -> Int -> MutableByteArray# s -> STUArray s i (Interval a)
forall s i e.
i -> i -> Int -> MutableByteArray# s -> STUArray s i e
A.STUArray i
l i
u Int
n MutableByteArray# s
arr_)
  unsafeRead :: forall i.
Ix i =>
STUArray s i (Interval a) -> Int -> ST s (Interval a)
unsafeRead (A.STUArray i
_ i
_ Int
_ MutableByteArray# s
byteArr) Int
i = do
    a
x <- MutableByteArray (PrimState (ST s)) -> Int -> ST s a
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutableByteArray (PrimState m) -> Int -> m a
readByteArray (MutableByteArray# s -> MutableByteArray s
forall s. MutableByteArray# s -> MutableByteArray s
MutableByteArray MutableByteArray# s
byteArr) (Int
2 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
i)
    a
y <- MutableByteArray (PrimState (ST s)) -> Int -> ST s a
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutableByteArray (PrimState m) -> Int -> m a
readByteArray (MutableByteArray# s -> MutableByteArray s
forall s. MutableByteArray# s -> MutableByteArray s
MutableByteArray MutableByteArray# s
byteArr) (Int
2 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
    Interval a -> ST s (Interval a)
forall a. a -> ST s a
forall (m :: * -> *) a. Monad m => a -> m a
return ((a, a) -> Interval a
forall a. Ord a => (a, a) -> Interval a
pairToInterval (a
x, a
y))
  unsafeWrite :: forall i.
Ix i =>
STUArray s i (Interval a) -> Int -> Interval a -> ST s ()
unsafeWrite (A.STUArray i
_ i
_ Int
_ MutableByteArray# s
byteArr) Int
i Interval a
e = do
    let (a
x, a
y) = Interval a -> (a, a)
forall a. Fractional a => Interval a -> (a, a)
intervalToPair Interval a
e
    MutableByteArray (PrimState (ST s)) -> Int -> a -> ST s ()
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutableByteArray (PrimState m) -> Int -> a -> m ()
writeByteArray (MutableByteArray# s -> MutableByteArray s
forall s. MutableByteArray# s -> MutableByteArray s
MutableByteArray MutableByteArray# s
byteArr) (Int
2 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
i) a
x
    MutableByteArray (PrimState (ST s)) -> Int -> a -> ST s ()
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutableByteArray (PrimState m) -> Int -> a -> m ()
writeByteArray (MutableByteArray# s -> MutableByteArray s
forall s. MutableByteArray# s -> MutableByteArray s
MutableByteArray MutableByteArray# s
byteArr) (Int
2 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) a
y

instance (Prim a, Ord a, Fractional a) => A.IArray A.UArray (Interval a) where
  bounds :: forall i. Ix i => UArray i (Interval a) -> (i, i)
bounds (A.UArray i
l i
u Int
_ ByteArray#
_) = (i
l,i
u)
  numElements :: forall i. Ix i => UArray i (Interval a) -> Int
numElements (A.UArray i
_ i
_ Int
n ByteArray#
_) = Int
n
  unsafeArray :: forall i.
Ix i =>
(i, i) -> [(Int, Interval a)] -> UArray i (Interval a)
unsafeArray (i, i)
bounds [(Int, Interval a)]
el = (forall s. ST s (UArray i (Interval a))) -> UArray i (Interval a)
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (UArray i (Interval a))) -> UArray i (Interval a))
-> (forall s. ST s (UArray i (Interval a)))
-> UArray i (Interval a)
forall a b. (a -> b) -> a -> b
$ do
    STUArray s i (Interval a)
marr <- (i, i) -> ST s (STUArray s i (Interval a))
forall i. Ix i => (i, i) -> ST s (STUArray s i (Interval a))
forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
(i, i) -> m (a i e)
A.newArray_ (i, i)
bounds
    [(Int, Interval a)] -> ((Int, Interval a) -> ST s ()) -> ST s ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [(Int, Interval a)]
el (((Int, Interval a) -> ST s ()) -> ST s ())
-> ((Int, Interval a) -> ST s ()) -> ST s ()
forall a b. (a -> b) -> a -> b
$ \(Int
i,Interval a
e) -> STUArray s i (Interval a) -> Int -> Interval a -> ST s ()
forall i.
Ix i =>
STUArray s i (Interval a) -> Int -> Interval a -> ST s ()
forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> Int -> e -> m ()
A.unsafeWrite STUArray s i (Interval a)
marr Int
i Interval a
e
    STUArray s i (Interval a) -> ST s (UArray i (Interval a))
forall s i e. STUArray s i e -> ST s (UArray i e)
A.unsafeFreezeSTUArray STUArray s i (Interval a)
marr
  unsafeAt :: forall i. Ix i => UArray i (Interval a) -> Int -> Interval a
unsafeAt (A.UArray i
_ i
_ Int
_ ByteArray#
byteArr) Int
i =
    let x :: a
x = ByteArray -> Int -> a
forall a. Prim a => ByteArray -> Int -> a
indexByteArray (ByteArray# -> ByteArray
ByteArray ByteArray#
byteArr) (Int
2 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
i)
        y :: a
y = ByteArray -> Int -> a
forall a. Prim a => ByteArray -> Int -> a
indexByteArray (ByteArray# -> ByteArray
ByteArray ByteArray#
byteArr) (Int
2 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
    in (a, a) -> Interval a
forall a. Ord a => (a, a) -> Interval a
pairToInterval (a
x, a
y)
  -- unsafeReplace, unsafeAccum, unsafeAccumArray: Use default

#if !MIN_VERSION_base(4, 16, 0)
{-# RULES
"fromIntegral/a->Interval Float"
  fromIntegral = \x -> case intervalFromIntegral x of (l, u) -> I l u :: Interval Float
"fromIntegral/a->Interval Double"
  fromIntegral = \x -> case intervalFromIntegral x of (l, u) -> I l u :: Interval Double
  #-}
#endif