module Data.Number.DyadicInterval(Interval,
fromBallA, fromBall, make, makeA,
below, contains, includes,
intersectA, intersect,
neg, add, mul, sub, div, sqrt, exp, log,
compareI, maxI, minI,
center, radius, lower, upper, width,
fromDyadic, fromString, fromInt, fromWord,
toString)
where
import qualified Data.Number.Ball as B
import qualified Data.Number.Dyadic as D
import Data.Order
import Prelude hiding (div, sqrt, exp, log)
import Data.Word(Word)
import Control.Monad
type Interval = Maybe B.Ball
data Inclusion = Above | Below | NoInclusion
fromBallA :: D.Precision -> B.Ball -> Interval
fromBallA p b = Just (B.normalizeBall p b)
fromBall :: B.Ball -> Interval
fromBall b = Just b
makeA :: D.Precision
-> D.Dyadic
-> D.Dyadic
-> Interval
makeA p l u = Just (B.makeA p l u)
make :: D.Dyadic -> D.Dyadic -> Interval
make l u = Just (B.make l u)
below :: Interval -> Interval -> Bool
below (Just b) (Just b') = B.below b b'
below Nothing _ = True
below _ Nothing = False
contains :: Interval -> D.Dyadic -> Bool
contains Nothing _ = True
contains (Just b) d = B.contains b d
includes :: Interval -> Interval -> Inclusion
includes i i' | below i i' = Below
| below i' i = Above
| otherwise = NoInclusion
intersectA :: D.Precision -> Interval -> Interval -> Interval
intersectA p (Just b) (Just b') = B.intersectA p b b'
intersectA _ Nothing i = i
intersectA _ i Nothing = i
intersect :: Interval -> Interval -> Interval
intersect (Just b) (Just b') = B.intersect b b'
intersect Nothing x = x
intersect x Nothing = x
neg :: D.Precision -> Interval -> Interval
neg p i = do b <- i
return $! B.neg p b
wrap :: (D.Precision -> B.Ball -> B.Ball -> B.Ball)
-> D.Precision -> Interval -> Interval -> Interval
wrap f p i i' = do !b <- i
!b' <- i'
return $! f p b b'
add :: D.Precision -> Interval -> Interval -> Interval
add = wrap B.add
mul :: D.Precision -> Interval -> Interval -> Interval
mul = wrap B.mul
sub :: D.Precision -> Interval -> Interval -> Interval
sub = wrap B.sub
div :: D.Precision -> Interval -> Interval -> Interval
div p i i' = do !b <- i
!b' <- i'
B.div p b b'
sqrt :: D.Precision -> Interval -> Interval
sqrt p i = do !b <- i
B.sqrt p b
log :: D.Precision -> Interval -> Interval
log p i = do !b <- i
B.log p b
exp :: D.Precision -> Interval -> Interval
exp p i = do !b <- i
return $! (B.exp p b)
compareI :: Interval -> Interval -> POrdering
compareI (Just b) (Just b') = B.compareB b b'
compareI _ _ = Incomparable
maxI :: D.Precision -> Interval -> Interval -> Interval
maxI = wrap B.maxB
minI :: D.Precision -> Interval -> Interval -> Interval
minI = wrap B.minB
center :: (Monad m) => Interval -> m D.Dyadic
center Nothing = fail "center of _|_ is not defined"
center (Just (B.Ball c _)) = return c
radius :: (Monad m) => Interval -> m D.Dyadic
radius Nothing = fail "radius of _|_ is infinity"
radius (Just (B.Ball _ r)) = return r
lower :: (Monad m) => Interval -> m D.Dyadic
lower Nothing = fail "lower bound of _|_ is -infinity"
lower (Just b) = return (B.lower_ b)
upper :: (Monad m) => Interval -> m D.Dyadic
upper Nothing = fail "upper bound of _|_ is +infinity"
upper (Just b) = return (B.upper_ b)
width :: (Monad m) => Interval -> m D.Dyadic
width Nothing = fail "width of _|_ is infinity"
width (Just b) = return (B.width b)
fromDyadic :: D.Precision -> D.Dyadic -> Interval
fromDyadic p d = Just (B.fromDyadic p d)
fromString :: D.Precision -> String -> Interval
fromString p s = Just (B.fromString p s)
fromInt :: D.Precision -> Int -> Interval
fromInt p d = Just (B.fromInt p d)
fromWord :: D.Precision -> Word -> Interval
fromWord p d = Just (B.fromWord p d)
toString :: Interval -> String
toString Nothing = "_|_"
toString (Just b) = show b