{-# OPTIONS_GHC -Wno-orphans #-}
{-|
    Module      :  AERN2.MP.Ball.Comparisons
    Description :  Comparisons of arbitrary precision dyadic balls
    Copyright   :  (c) Michal Konecny
    License     :  BSD3

    Maintainer  :  mikkonecny@gmail.com
    Stability   :  experimental
    Portability :  portable

    Comparisons of arbitrary precision dyadic balls
-}
module AERN2.MP.Ball.Comparisons
(
  -- * Auxiliary types
  module AERN2.Norm
  -- * Ball operations (see also instances)
  , reducePrecionIfInaccurate
  -- * Helpers for constructing ball functions
  , byEndpointsMP
  -- * intersection and hull
  , intersectCNMPBall
  , hullMPBall
)
where

import MixedTypesNumPrelude
-- import qualified Prelude as P

import qualified Control.CollectErrors as CE
import Control.CollectErrors
    ( CollectErrors(getMaybeValue), CanBeErrors )
import qualified Numeric.CollectErrors as CN

import AERN2.Kleenean
import AERN2.Norm
import AERN2.MP.Dyadic (Dyadic)
import AERN2.MP.Float (MPFloat)
-- import AERN2.MP.Float.Operators
import AERN2.MP.Precision

import AERN2.MP.Ball.Type
import AERN2.MP.Ball.Conversions ()

{- comparisons -}

instance HasEqAsymmetric MPBall MPBall where
  type EqCompareType MPBall MPBall = Kleenean
  MPBall
b1 equalTo :: MPBall -> MPBall -> EqCompareType MPBall MPBall
`equalTo` MPBall
b2 =   MPBall
b1 MPBall -> MPBall -> OrderCompareType MPBall MPBall
forall a b.
HasOrderAsymmetric a b =>
a -> b -> OrderCompareType a b
>= MPBall
b2 Kleenean -> Kleenean -> AndOrType Kleenean Kleenean
forall a b. CanAndOrAsymmetric a b => a -> b -> AndOrType a b
&& MPBall
b1 MPBall -> MPBall -> OrderCompareType MPBall MPBall
forall a b.
HasOrderAsymmetric a b =>
a -> b -> OrderCompareType a b
<= MPBall
b2

instance HasEqAsymmetric MPBall Integer where
  type EqCompareType MPBall Integer = Kleenean
  MPBall
b1 equalTo :: MPBall -> Integer -> EqCompareType MPBall Integer
`equalTo` Integer
b2 =   MPBall
b1 MPBall -> Integer -> OrderCompareType MPBall Integer
forall a b.
HasOrderAsymmetric a b =>
a -> b -> OrderCompareType a b
>= Integer
b2 Kleenean -> Kleenean -> AndOrType Kleenean Kleenean
forall a b. CanAndOrAsymmetric a b => a -> b -> AndOrType a b
&& MPBall
b1 MPBall -> Integer -> OrderCompareType MPBall Integer
forall a b.
HasOrderAsymmetric a b =>
a -> b -> OrderCompareType a b
<= Integer
b2
instance HasEqAsymmetric Integer MPBall where
  type EqCompareType Integer MPBall = Kleenean
  Integer
b1 equalTo :: Integer -> MPBall -> EqCompareType Integer MPBall
`equalTo` MPBall
b2 =   Integer
b1 Integer -> MPBall -> OrderCompareType Integer MPBall
forall a b.
HasOrderAsymmetric a b =>
a -> b -> OrderCompareType a b
>= MPBall
b2 Kleenean -> Kleenean -> AndOrType Kleenean Kleenean
forall a b. CanAndOrAsymmetric a b => a -> b -> AndOrType a b
&& Integer
b1 Integer -> MPBall -> OrderCompareType Integer MPBall
forall a b.
HasOrderAsymmetric a b =>
a -> b -> OrderCompareType a b
<= MPBall
b2

instance HasEqAsymmetric MPBall Int where
  type EqCompareType MPBall Int = Kleenean
  MPBall
b1 equalTo :: MPBall -> Int -> EqCompareType MPBall Int
`equalTo` Int
b2 =   MPBall
b1 MPBall -> Int -> OrderCompareType MPBall Int
forall a b.
HasOrderAsymmetric a b =>
a -> b -> OrderCompareType a b
>= Int
b2 Kleenean -> Kleenean -> AndOrType Kleenean Kleenean
forall a b. CanAndOrAsymmetric a b => a -> b -> AndOrType a b
&& MPBall
b1 MPBall -> Int -> OrderCompareType MPBall Int
forall a b.
HasOrderAsymmetric a b =>
a -> b -> OrderCompareType a b
<= Int
b2
instance HasEqAsymmetric Int MPBall where
  type EqCompareType Int MPBall = Kleenean
  Int
b1 equalTo :: Int -> MPBall -> EqCompareType Int MPBall
`equalTo` MPBall
b2 =   Int
b1 Int -> MPBall -> OrderCompareType Int MPBall
forall a b.
HasOrderAsymmetric a b =>
a -> b -> OrderCompareType a b
>= MPBall
b2 Kleenean -> Kleenean -> AndOrType Kleenean Kleenean
forall a b. CanAndOrAsymmetric a b => a -> b -> AndOrType a b
&& Int
b1 Int -> MPBall -> OrderCompareType Int MPBall
forall a b.
HasOrderAsymmetric a b =>
a -> b -> OrderCompareType a b
<= MPBall
b2

instance HasEqAsymmetric MPBall Rational where
  type EqCompareType MPBall Rational = Kleenean
  MPBall
b1 equalTo :: MPBall -> Rational -> EqCompareType MPBall Rational
`equalTo` Rational
b2 =   MPBall
b1 MPBall -> Rational -> OrderCompareType MPBall Rational
forall a b.
HasOrderAsymmetric a b =>
a -> b -> OrderCompareType a b
>= Rational
b2 Kleenean -> Kleenean -> AndOrType Kleenean Kleenean
forall a b. CanAndOrAsymmetric a b => a -> b -> AndOrType a b
&& MPBall
b1 MPBall -> Rational -> OrderCompareType MPBall Rational
forall a b.
HasOrderAsymmetric a b =>
a -> b -> OrderCompareType a b
<= Rational
b2
instance HasEqAsymmetric Rational MPBall where
  type EqCompareType Rational MPBall = Kleenean
  Rational
b1 equalTo :: Rational -> MPBall -> EqCompareType Rational MPBall
`equalTo` MPBall
b2 =   Rational
b1 Rational -> MPBall -> OrderCompareType Rational MPBall
forall a b.
HasOrderAsymmetric a b =>
a -> b -> OrderCompareType a b
>= MPBall
b2 Kleenean -> Kleenean -> AndOrType Kleenean Kleenean
forall a b. CanAndOrAsymmetric a b => a -> b -> AndOrType a b
&& Rational
b1 Rational -> MPBall -> OrderCompareType Rational MPBall
forall a b.
HasOrderAsymmetric a b =>
a -> b -> OrderCompareType a b
<= MPBall
b2

instance HasEqAsymmetric MPBall Dyadic where
  type EqCompareType MPBall Dyadic = Kleenean
  MPBall
b1 equalTo :: MPBall -> Dyadic -> EqCompareType MPBall Dyadic
`equalTo` Dyadic
b2 =   MPBall
b1 MPBall -> Dyadic -> OrderCompareType MPBall Dyadic
forall a b.
HasOrderAsymmetric a b =>
a -> b -> OrderCompareType a b
>= Dyadic
b2 Kleenean -> Kleenean -> AndOrType Kleenean Kleenean
forall a b. CanAndOrAsymmetric a b => a -> b -> AndOrType a b
&& MPBall
b1 MPBall -> Dyadic -> OrderCompareType MPBall Dyadic
forall a b.
HasOrderAsymmetric a b =>
a -> b -> OrderCompareType a b
<= Dyadic
b2
instance HasEqAsymmetric Dyadic MPBall where
  type EqCompareType Dyadic MPBall = Kleenean
  Dyadic
b1 equalTo :: Dyadic -> MPBall -> EqCompareType Dyadic MPBall
`equalTo` MPBall
b2 =   Dyadic
b1 Dyadic -> MPBall -> OrderCompareType Dyadic MPBall
forall a b.
HasOrderAsymmetric a b =>
a -> b -> OrderCompareType a b
>= MPBall
b2 Kleenean -> Kleenean -> AndOrType Kleenean Kleenean
forall a b. CanAndOrAsymmetric a b => a -> b -> AndOrType a b
&& Dyadic
b1 Dyadic -> MPBall -> OrderCompareType Dyadic MPBall
forall a b.
HasOrderAsymmetric a b =>
a -> b -> OrderCompareType a b
<= MPBall
b2

instance
  (HasEqAsymmetric MPBall b
  , IsBool (EqCompareType MPBall b)
  , CanBeErrors es)
  =>
  HasEqAsymmetric MPBall (CollectErrors es b)
  where
  type EqCompareType MPBall (CollectErrors es b) =
    CollectErrors es (EqCompareType MPBall b)
  equalTo :: MPBall
-> CollectErrors es b -> EqCompareType MPBall (CollectErrors es b)
equalTo = (MPBall -> b -> EqCompareType MPBall b)
-> MPBall
-> CollectErrors es b
-> CollectErrors es (EqCompareType MPBall b)
forall es a b c.
Monoid es =>
(a -> b -> c) -> a -> CollectErrors es b -> CollectErrors es c
CE.liftT1 MPBall -> b -> EqCompareType MPBall b
forall a b. HasEqAsymmetric a b => a -> b -> EqCompareType a b
equalTo

instance
  (HasEqAsymmetric a MPBall
  , IsBool (EqCompareType a MPBall)
  , CanBeErrors es)
  =>
  HasEqAsymmetric (CollectErrors es a) MPBall
  where
  type EqCompareType (CollectErrors es a) MPBall =
    CollectErrors es (EqCompareType a MPBall)
  equalTo :: CollectErrors es a
-> MPBall -> EqCompareType (CollectErrors es a) MPBall
equalTo = (a -> MPBall -> EqCompareType a MPBall)
-> CollectErrors es a
-> MPBall
-> CollectErrors es (EqCompareType a MPBall)
forall es a b c.
Monoid es =>
(a -> b -> c) -> CollectErrors es a -> b -> CollectErrors es c
CE.lift1T a -> MPBall -> EqCompareType a MPBall
forall a b. HasEqAsymmetric a b => a -> b -> EqCompareType a b
equalTo

instance HasOrderAsymmetric MPBall MPBall where
  type OrderCompareType MPBall MPBall = Kleenean
  lessThan :: MPBall -> MPBall -> OrderCompareType MPBall MPBall
lessThan MPBall
b1 MPBall
b2
    | MPFloat
r1 MPFloat -> MPFloat -> OrderCompareType MPFloat MPFloat
forall a b.
HasOrderAsymmetric a b =>
a -> b -> OrderCompareType a b
< MPFloat
l2 = OrderCompareType MPBall MPBall
Kleenean
CertainTrue
    | MPFloat
r2 MPFloat -> MPFloat -> OrderCompareType MPFloat MPFloat
forall a b.
HasOrderAsymmetric a b =>
a -> b -> OrderCompareType a b
<= MPFloat
l1 = OrderCompareType MPBall MPBall
Kleenean
CertainFalse
    | Bool
otherwise = OrderCompareType MPBall MPBall
Kleenean
TrueOrFalse
    where
    (MPFloat
l1, MPFloat
r1) = MPBall -> (IntervalEndpoint MPBall, IntervalEndpoint MPBall)
forall i.
IsInterval i =>
i -> (IntervalEndpoint i, IntervalEndpoint i)
endpoints MPBall
b1
    (MPFloat
l2, MPFloat
r2) = MPBall -> (IntervalEndpoint MPBall, IntervalEndpoint MPBall)
forall i.
IsInterval i =>
i -> (IntervalEndpoint i, IntervalEndpoint i)
endpoints MPBall
b2
  leq :: MPBall -> MPBall -> OrderCompareType MPBall MPBall
leq MPBall
b1 MPBall
b2
    | MPFloat
r1 MPFloat -> MPFloat -> OrderCompareType MPFloat MPFloat
forall a b.
HasOrderAsymmetric a b =>
a -> b -> OrderCompareType a b
<= MPFloat
l2 = OrderCompareType MPBall MPBall
Kleenean
CertainTrue
    | MPFloat
r2 MPFloat -> MPFloat -> OrderCompareType MPFloat MPFloat
forall a b.
HasOrderAsymmetric a b =>
a -> b -> OrderCompareType a b
< MPFloat
l1 = OrderCompareType MPBall MPBall
Kleenean
CertainFalse
    | Bool
otherwise = OrderCompareType MPBall MPBall
Kleenean
TrueOrFalse
    where
    (MPFloat
l1, MPFloat
r1) = MPBall -> (IntervalEndpoint MPBall, IntervalEndpoint MPBall)
forall i.
IsInterval i =>
i -> (IntervalEndpoint i, IntervalEndpoint i)
endpoints MPBall
b1
    (MPFloat
l2, MPFloat
r2) = MPBall -> (IntervalEndpoint MPBall, IntervalEndpoint MPBall)
forall i.
IsInterval i =>
i -> (IntervalEndpoint i, IntervalEndpoint i)
endpoints MPBall
b2

instance HasOrderAsymmetric Integer MPBall where
  type OrderCompareType Integer MPBall = Kleenean
  lessThan :: Integer -> MPBall -> OrderCompareType Integer MPBall
lessThan = (MPBall -> MPBall -> Kleenean) -> Integer -> MPBall -> Kleenean
forall a b c.
ConvertibleExactly a b =>
(b -> b -> c) -> a -> b -> c
convertFirst MPBall -> MPBall -> Kleenean
forall a b.
HasOrderAsymmetric a b =>
a -> b -> OrderCompareType a b
lessThan
  leq :: Integer -> MPBall -> OrderCompareType Integer MPBall
leq = (MPBall -> MPBall -> Kleenean) -> Integer -> MPBall -> Kleenean
forall a b c.
ConvertibleExactly a b =>
(b -> b -> c) -> a -> b -> c
convertFirst MPBall -> MPBall -> Kleenean
forall a b.
HasOrderAsymmetric a b =>
a -> b -> OrderCompareType a b
leq
instance HasOrderAsymmetric MPBall Integer where
  type OrderCompareType MPBall Integer = Kleenean
  lessThan :: MPBall -> Integer -> OrderCompareType MPBall Integer
lessThan = (MPBall -> MPBall -> Kleenean) -> MPBall -> Integer -> Kleenean
forall b a c.
ConvertibleExactly b a =>
(a -> a -> c) -> a -> b -> c
convertSecond MPBall -> MPBall -> Kleenean
forall a b.
HasOrderAsymmetric a b =>
a -> b -> OrderCompareType a b
lessThan
  leq :: MPBall -> Integer -> OrderCompareType MPBall Integer
leq = (MPBall -> MPBall -> Kleenean) -> MPBall -> Integer -> Kleenean
forall b a c.
ConvertibleExactly b a =>
(a -> a -> c) -> a -> b -> c
convertSecond MPBall -> MPBall -> Kleenean
forall a b.
HasOrderAsymmetric a b =>
a -> b -> OrderCompareType a b
leq

instance HasOrderAsymmetric Int MPBall where
  type OrderCompareType Int MPBall = Kleenean
  lessThan :: Int -> MPBall -> OrderCompareType Int MPBall
lessThan = (MPBall -> MPBall -> Kleenean) -> Int -> MPBall -> Kleenean
forall a b c.
ConvertibleExactly a b =>
(b -> b -> c) -> a -> b -> c
convertFirst MPBall -> MPBall -> Kleenean
forall a b.
HasOrderAsymmetric a b =>
a -> b -> OrderCompareType a b
lessThan
  leq :: Int -> MPBall -> OrderCompareType Int MPBall
leq = (MPBall -> MPBall -> Kleenean) -> Int -> MPBall -> Kleenean
forall a b c.
ConvertibleExactly a b =>
(b -> b -> c) -> a -> b -> c
convertFirst MPBall -> MPBall -> Kleenean
forall a b.
HasOrderAsymmetric a b =>
a -> b -> OrderCompareType a b
leq
instance HasOrderAsymmetric MPBall Int where
  type OrderCompareType MPBall Int = Kleenean
  lessThan :: MPBall -> Int -> OrderCompareType MPBall Int
lessThan = (MPBall -> MPBall -> Kleenean) -> MPBall -> Int -> Kleenean
forall b a c.
ConvertibleExactly b a =>
(a -> a -> c) -> a -> b -> c
convertSecond MPBall -> MPBall -> Kleenean
forall a b.
HasOrderAsymmetric a b =>
a -> b -> OrderCompareType a b
lessThan
  leq :: MPBall -> Int -> OrderCompareType MPBall Int
leq = (MPBall -> MPBall -> Kleenean) -> MPBall -> Int -> Kleenean
forall b a c.
ConvertibleExactly b a =>
(a -> a -> c) -> a -> b -> c
convertSecond MPBall -> MPBall -> Kleenean
forall a b.
HasOrderAsymmetric a b =>
a -> b -> OrderCompareType a b
leq

instance HasOrderAsymmetric Dyadic MPBall where
  type OrderCompareType Dyadic MPBall = Kleenean
  lessThan :: Dyadic -> MPBall -> OrderCompareType Dyadic MPBall
lessThan = (MPBall -> MPBall -> Kleenean) -> Dyadic -> MPBall -> Kleenean
forall a b c.
ConvertibleExactly a b =>
(b -> b -> c) -> a -> b -> c
convertFirst MPBall -> MPBall -> Kleenean
forall a b.
HasOrderAsymmetric a b =>
a -> b -> OrderCompareType a b
lessThan
  leq :: Dyadic -> MPBall -> OrderCompareType Dyadic MPBall
leq = (MPBall -> MPBall -> Kleenean) -> Dyadic -> MPBall -> Kleenean
forall a b c.
ConvertibleExactly a b =>
(b -> b -> c) -> a -> b -> c
convertFirst MPBall -> MPBall -> Kleenean
forall a b.
HasOrderAsymmetric a b =>
a -> b -> OrderCompareType a b
leq
instance HasOrderAsymmetric MPBall Dyadic where
  type OrderCompareType MPBall Dyadic = Kleenean
  lessThan :: MPBall -> Dyadic -> OrderCompareType MPBall Dyadic
lessThan = (MPBall -> MPBall -> Kleenean) -> MPBall -> Dyadic -> Kleenean
forall b a c.
ConvertibleExactly b a =>
(a -> a -> c) -> a -> b -> c
convertSecond MPBall -> MPBall -> Kleenean
forall a b.
HasOrderAsymmetric a b =>
a -> b -> OrderCompareType a b
lessThan
  leq :: MPBall -> Dyadic -> OrderCompareType MPBall Dyadic
leq = (MPBall -> MPBall -> Kleenean) -> MPBall -> Dyadic -> Kleenean
forall b a c.
ConvertibleExactly b a =>
(a -> a -> c) -> a -> b -> c
convertSecond MPBall -> MPBall -> Kleenean
forall a b.
HasOrderAsymmetric a b =>
a -> b -> OrderCompareType a b
leq

instance HasOrderAsymmetric MPBall Rational where
  type OrderCompareType MPBall Rational = Kleenean
  lessThan :: MPBall -> Rational -> OrderCompareType MPBall Rational
lessThan MPBall
b1 Rational
q2
    | MPFloat
r1 MPFloat -> Rational -> OrderCompareType MPFloat Rational
forall a b.
HasOrderAsymmetric a b =>
a -> b -> OrderCompareType a b
< Rational
l2 = OrderCompareType MPBall Rational
Kleenean
CertainTrue
    | Rational
r2 Rational -> MPFloat -> OrderCompareType Rational MPFloat
forall a b.
HasOrderAsymmetric a b =>
a -> b -> OrderCompareType a b
<= MPFloat
l1 = OrderCompareType MPBall Rational
Kleenean
CertainFalse
    | Bool
otherwise = OrderCompareType MPBall Rational
Kleenean
TrueOrFalse
    where
    (MPFloat
l1, MPFloat
r1) = MPBall -> (IntervalEndpoint MPBall, IntervalEndpoint MPBall)
forall i.
IsInterval i =>
i -> (IntervalEndpoint i, IntervalEndpoint i)
endpoints MPBall
b1
    l2 :: Rational
l2 = Rational
q2
    r2 :: Rational
r2 = Rational
q2
  leq :: MPBall -> Rational -> OrderCompareType MPBall Rational
leq MPBall
b1 Rational
q2
    | MPFloat
r1 MPFloat -> Rational -> OrderCompareType MPFloat Rational
forall a b.
HasOrderAsymmetric a b =>
a -> b -> OrderCompareType a b
<= Rational
l2 = OrderCompareType MPBall Rational
Kleenean
CertainTrue
    | Rational
r2 Rational -> MPFloat -> OrderCompareType Rational MPFloat
forall a b.
HasOrderAsymmetric a b =>
a -> b -> OrderCompareType a b
< MPFloat
l1 = OrderCompareType MPBall Rational
Kleenean
CertainFalse
    | Bool
otherwise = OrderCompareType MPBall Rational
Kleenean
TrueOrFalse
    where
    (MPFloat
l1, MPFloat
r1) = MPBall -> (IntervalEndpoint MPBall, IntervalEndpoint MPBall)
forall i.
IsInterval i =>
i -> (IntervalEndpoint i, IntervalEndpoint i)
endpoints MPBall
b1
    l2 :: Rational
l2 = Rational
q2
    r2 :: Rational
r2 = Rational
q2

instance HasOrderAsymmetric Rational MPBall where
  type OrderCompareType Rational MPBall = Kleenean
  lessThan :: Rational -> MPBall -> OrderCompareType Rational MPBall
lessThan Rational
q1 MPBall
b2
    | Rational
r1 Rational -> MPFloat -> OrderCompareType Rational MPFloat
forall a b.
HasOrderAsymmetric a b =>
a -> b -> OrderCompareType a b
< MPFloat
l2 = OrderCompareType Rational MPBall
Kleenean
CertainTrue
    | MPFloat
r2 MPFloat -> Rational -> OrderCompareType MPFloat Rational
forall a b.
HasOrderAsymmetric a b =>
a -> b -> OrderCompareType a b
<= Rational
l1 = OrderCompareType Rational MPBall
Kleenean
CertainFalse
    | Bool
otherwise = OrderCompareType Rational MPBall
Kleenean
TrueOrFalse
    where
    (MPFloat
l2, MPFloat
r2) = MPBall -> (IntervalEndpoint MPBall, IntervalEndpoint MPBall)
forall i.
IsInterval i =>
i -> (IntervalEndpoint i, IntervalEndpoint i)
endpoints MPBall
b2
    l1 :: Rational
l1 = Rational
q1
    r1 :: Rational
r1 = Rational
q1
  leq :: Rational -> MPBall -> OrderCompareType Rational MPBall
leq Rational
q1 MPBall
b2
    | Rational
r1 Rational -> MPFloat -> OrderCompareType Rational MPFloat
forall a b.
HasOrderAsymmetric a b =>
a -> b -> OrderCompareType a b
<= MPFloat
l2 = OrderCompareType Rational MPBall
Kleenean
CertainTrue
    | MPFloat
r2 MPFloat -> Rational -> OrderCompareType MPFloat Rational
forall a b.
HasOrderAsymmetric a b =>
a -> b -> OrderCompareType a b
< Rational
l1 = OrderCompareType Rational MPBall
Kleenean
CertainFalse
    | Bool
otherwise = OrderCompareType Rational MPBall
Kleenean
TrueOrFalse
    where
    (MPFloat
l2, MPFloat
r2) = MPBall -> (IntervalEndpoint MPBall, IntervalEndpoint MPBall)
forall i.
IsInterval i =>
i -> (IntervalEndpoint i, IntervalEndpoint i)
endpoints MPBall
b2
    l1 :: Rational
l1 = Rational
q1
    r1 :: Rational
r1 = Rational
q1

instance
  (HasOrderAsymmetric MPBall b
  , IsBool (OrderCompareType MPBall b)
  , CanBeErrors es)
  =>
  HasOrderAsymmetric MPBall (CollectErrors es  b)
  where
  type OrderCompareType MPBall (CollectErrors es  b) =
    CollectErrors es (OrderCompareType MPBall b)
  lessThan :: MPBall
-> CollectErrors es b
-> OrderCompareType MPBall (CollectErrors es b)
lessThan = (MPBall -> b -> OrderCompareType MPBall b)
-> MPBall
-> CollectErrors es b
-> CollectErrors es (OrderCompareType MPBall b)
forall es a b c.
Monoid es =>
(a -> b -> c) -> a -> CollectErrors es b -> CollectErrors es c
CE.liftT1 MPBall -> b -> OrderCompareType MPBall b
forall a b.
HasOrderAsymmetric a b =>
a -> b -> OrderCompareType a b
lessThan
  leq :: MPBall
-> CollectErrors es b
-> OrderCompareType MPBall (CollectErrors es b)
leq = (MPBall -> b -> OrderCompareType MPBall b)
-> MPBall
-> CollectErrors es b
-> CollectErrors es (OrderCompareType MPBall b)
forall es a b c.
Monoid es =>
(a -> b -> c) -> a -> CollectErrors es b -> CollectErrors es c
CE.liftT1 MPBall -> b -> OrderCompareType MPBall b
forall a b.
HasOrderAsymmetric a b =>
a -> b -> OrderCompareType a b
leq
  greaterThan :: MPBall
-> CollectErrors es b
-> OrderCompareType MPBall (CollectErrors es b)
greaterThan = (MPBall -> b -> OrderCompareType MPBall b)
-> MPBall
-> CollectErrors es b
-> CollectErrors es (OrderCompareType MPBall b)
forall es a b c.
Monoid es =>
(a -> b -> c) -> a -> CollectErrors es b -> CollectErrors es c
CE.liftT1 MPBall -> b -> OrderCompareType MPBall b
forall a b.
HasOrderAsymmetric a b =>
a -> b -> OrderCompareType a b
greaterThan
  geq :: MPBall
-> CollectErrors es b
-> OrderCompareType MPBall (CollectErrors es b)
geq = (MPBall -> b -> OrderCompareType MPBall b)
-> MPBall
-> CollectErrors es b
-> CollectErrors es (OrderCompareType MPBall b)
forall es a b c.
Monoid es =>
(a -> b -> c) -> a -> CollectErrors es b -> CollectErrors es c
CE.liftT1 MPBall -> b -> OrderCompareType MPBall b
forall a b.
HasOrderAsymmetric a b =>
a -> b -> OrderCompareType a b
geq

instance
  (HasOrderAsymmetric a MPBall
  , IsBool (OrderCompareType a MPBall)
  , CanBeErrors es)
  =>
  HasOrderAsymmetric (CollectErrors es a) MPBall
  where
  type OrderCompareType (CollectErrors es  a) MPBall =
    CollectErrors es (OrderCompareType a MPBall)
  lessThan :: CollectErrors es a
-> MPBall -> OrderCompareType (CollectErrors es a) MPBall
lessThan = (a -> MPBall -> OrderCompareType a MPBall)
-> CollectErrors es a
-> MPBall
-> CollectErrors es (OrderCompareType a MPBall)
forall es a b c.
Monoid es =>
(a -> b -> c) -> CollectErrors es a -> b -> CollectErrors es c
CE.lift1T a -> MPBall -> OrderCompareType a MPBall
forall a b.
HasOrderAsymmetric a b =>
a -> b -> OrderCompareType a b
lessThan
  leq :: CollectErrors es a
-> MPBall -> OrderCompareType (CollectErrors es a) MPBall
leq = (a -> MPBall -> OrderCompareType a MPBall)
-> CollectErrors es a
-> MPBall
-> CollectErrors es (OrderCompareType a MPBall)
forall es a b c.
Monoid es =>
(a -> b -> c) -> CollectErrors es a -> b -> CollectErrors es c
CE.lift1T a -> MPBall -> OrderCompareType a MPBall
forall a b.
HasOrderAsymmetric a b =>
a -> b -> OrderCompareType a b
leq
  greaterThan :: CollectErrors es a
-> MPBall -> OrderCompareType (CollectErrors es a) MPBall
greaterThan = (a -> MPBall -> OrderCompareType a MPBall)
-> CollectErrors es a
-> MPBall
-> CollectErrors es (OrderCompareType a MPBall)
forall es a b c.
Monoid es =>
(a -> b -> c) -> CollectErrors es a -> b -> CollectErrors es c
CE.lift1T a -> MPBall -> OrderCompareType a MPBall
forall a b.
HasOrderAsymmetric a b =>
a -> b -> OrderCompareType a b
greaterThan
  geq :: CollectErrors es a
-> MPBall -> OrderCompareType (CollectErrors es a) MPBall
geq = (a -> MPBall -> OrderCompareType a MPBall)
-> CollectErrors es a
-> MPBall
-> CollectErrors es (OrderCompareType a MPBall)
forall es a b c.
Monoid es =>
(a -> b -> c) -> CollectErrors es a -> b -> CollectErrors es c
CE.lift1T a -> MPBall -> OrderCompareType a MPBall
forall a b.
HasOrderAsymmetric a b =>
a -> b -> OrderCompareType a b
geq

instance CanTestZero MPBall
instance CanTestPosNeg MPBall

instance CanTestInteger MPBall where
  certainlyNotInteger :: MPBall -> Bool
certainlyNotInteger MPBall
b =
    (Integer
rN Integer -> Integer -> SubType Integer Integer
forall t1 t2. CanSub t1 t2 => t1 -> t2 -> SubType t1 t2
- Integer
lN) Integer -> Integer -> EqCompareType Integer Integer
forall a b. HasEqAsymmetric a b => a -> b -> EqCompareType a b
== Integer
1 Bool -> Bool -> AndOrType Bool Bool
forall a b. CanAndOrAsymmetric a b => a -> b -> AndOrType a b
&& Integer
lN Integer -> MPBall -> Bool
forall a b. HasOrderCertainlyAsymmetric a b => a -> b -> Bool
!<! MPBall
b Bool -> Bool -> AndOrType Bool Bool
forall a b. CanAndOrAsymmetric a b => a -> b -> AndOrType a b
&& MPBall
b MPBall -> Integer -> Bool
forall a b. HasOrderCertainlyAsymmetric a b => a -> b -> Bool
!<! Integer
rN
    where
      (Integer
lN, Integer
rN) = MPBall -> (Integer, Integer)
forall t. HasIntegerBounds t => t -> (Integer, Integer)
integerBounds MPBall
b
  certainlyIntegerGetIt :: MPBall -> Maybe Integer
certainlyIntegerGetIt MPBall
b
    | Integer
rN Integer -> Integer -> EqCompareType Integer Integer
forall a b. HasEqAsymmetric a b => a -> b -> EqCompareType a b
== Integer
lN = Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
lN
    | Bool
otherwise = Maybe Integer
forall a. Maybe a
Nothing
    where
      (Integer
lN, Integer
rN) = MPBall -> (Integer, Integer)
forall t. HasIntegerBounds t => t -> (Integer, Integer)
integerBounds MPBall
b

instance CanMinMaxAsymmetric MPBall MPBall where
  min :: MPBall -> MPBall -> MinMaxType MPBall MPBall
min = (MPFloat -> MPFloat -> MPFloat) -> MPBall -> MPBall -> MPBall
byEndpointsMP MPFloat -> MPFloat -> MPFloat
forall t1 t2.
CanMinMaxAsymmetric t1 t2 =>
t1 -> t2 -> MinMaxType t1 t2
min
  max :: MPBall -> MPBall -> MinMaxType MPBall MPBall
max = (MPFloat -> MPFloat -> MPFloat) -> MPBall -> MPBall -> MPBall
byEndpointsMP MPFloat -> MPFloat -> MPFloat
forall t1 t2.
CanMinMaxAsymmetric t1 t2 =>
t1 -> t2 -> MinMaxType t1 t2
max

instance CanMinMaxAsymmetric MPBall Integer where
  type MinMaxType MPBall Integer = MPBall
  min :: MPBall -> Integer -> MinMaxType MPBall Integer
min = (MPBall -> MPBall -> MPBall) -> MPBall -> Integer -> MPBall
forall b a c.
ConvertibleExactly b a =>
(a -> a -> c) -> a -> b -> c
convertSecond MPBall -> MPBall -> MPBall
forall t1 t2.
CanMinMaxAsymmetric t1 t2 =>
t1 -> t2 -> MinMaxType t1 t2
min
  max :: MPBall -> Integer -> MinMaxType MPBall Integer
max = (MPBall -> MPBall -> MPBall) -> MPBall -> Integer -> MPBall
forall b a c.
ConvertibleExactly b a =>
(a -> a -> c) -> a -> b -> c
convertSecond MPBall -> MPBall -> MPBall
forall t1 t2.
CanMinMaxAsymmetric t1 t2 =>
t1 -> t2 -> MinMaxType t1 t2
max
instance CanMinMaxAsymmetric Integer MPBall where
  type MinMaxType Integer MPBall = MPBall
  min :: Integer -> MPBall -> MinMaxType Integer MPBall
min = (MPBall -> MPBall -> MPBall) -> Integer -> MPBall -> MPBall
forall a b c.
ConvertibleExactly a b =>
(b -> b -> c) -> a -> b -> c
convertFirst MPBall -> MPBall -> MPBall
forall t1 t2.
CanMinMaxAsymmetric t1 t2 =>
t1 -> t2 -> MinMaxType t1 t2
min
  max :: Integer -> MPBall -> MinMaxType Integer MPBall
max = (MPBall -> MPBall -> MPBall) -> Integer -> MPBall -> MPBall
forall a b c.
ConvertibleExactly a b =>
(b -> b -> c) -> a -> b -> c
convertFirst MPBall -> MPBall -> MPBall
forall t1 t2.
CanMinMaxAsymmetric t1 t2 =>
t1 -> t2 -> MinMaxType t1 t2
max

instance CanMinMaxAsymmetric MPBall Int where
  type MinMaxType MPBall Int = MPBall
  min :: MPBall -> Int -> MinMaxType MPBall Int
min = (MPBall -> MPBall -> MPBall) -> MPBall -> Int -> MPBall
forall b a c.
ConvertibleExactly b a =>
(a -> a -> c) -> a -> b -> c
convertSecond MPBall -> MPBall -> MPBall
forall t1 t2.
CanMinMaxAsymmetric t1 t2 =>
t1 -> t2 -> MinMaxType t1 t2
min
  max :: MPBall -> Int -> MinMaxType MPBall Int
max = (MPBall -> MPBall -> MPBall) -> MPBall -> Int -> MPBall
forall b a c.
ConvertibleExactly b a =>
(a -> a -> c) -> a -> b -> c
convertSecond MPBall -> MPBall -> MPBall
forall t1 t2.
CanMinMaxAsymmetric t1 t2 =>
t1 -> t2 -> MinMaxType t1 t2
max
instance CanMinMaxAsymmetric Int MPBall where
  type MinMaxType Int MPBall = MPBall
  min :: Int -> MPBall -> MinMaxType Int MPBall
min = (MPBall -> MPBall -> MPBall) -> Int -> MPBall -> MPBall
forall a b c.
ConvertibleExactly a b =>
(b -> b -> c) -> a -> b -> c
convertFirst MPBall -> MPBall -> MPBall
forall t1 t2.
CanMinMaxAsymmetric t1 t2 =>
t1 -> t2 -> MinMaxType t1 t2
min
  max :: Int -> MPBall -> MinMaxType Int MPBall
max = (MPBall -> MPBall -> MPBall) -> Int -> MPBall -> MPBall
forall a b c.
ConvertibleExactly a b =>
(b -> b -> c) -> a -> b -> c
convertFirst MPBall -> MPBall -> MPBall
forall t1 t2.
CanMinMaxAsymmetric t1 t2 =>
t1 -> t2 -> MinMaxType t1 t2
max

instance CanMinMaxAsymmetric MPBall Dyadic where
  type MinMaxType MPBall Dyadic = MPBall
  min :: MPBall -> Dyadic -> MinMaxType MPBall Dyadic
min = (MPBall -> MPBall -> MPBall) -> MPBall -> Dyadic -> MPBall
forall b a c.
ConvertibleExactly b a =>
(a -> a -> c) -> a -> b -> c
convertSecond MPBall -> MPBall -> MPBall
forall t1 t2.
CanMinMaxAsymmetric t1 t2 =>
t1 -> t2 -> MinMaxType t1 t2
min
  max :: MPBall -> Dyadic -> MinMaxType MPBall Dyadic
max = (MPBall -> MPBall -> MPBall) -> MPBall -> Dyadic -> MPBall
forall b a c.
ConvertibleExactly b a =>
(a -> a -> c) -> a -> b -> c
convertSecond MPBall -> MPBall -> MPBall
forall t1 t2.
CanMinMaxAsymmetric t1 t2 =>
t1 -> t2 -> MinMaxType t1 t2
max
instance CanMinMaxAsymmetric Dyadic MPBall where
  type MinMaxType Dyadic MPBall = MPBall
  min :: Dyadic -> MPBall -> MinMaxType Dyadic MPBall
min = (MPBall -> MPBall -> MPBall) -> Dyadic -> MPBall -> MPBall
forall a b c.
ConvertibleExactly a b =>
(b -> b -> c) -> a -> b -> c
convertFirst MPBall -> MPBall -> MPBall
forall t1 t2.
CanMinMaxAsymmetric t1 t2 =>
t1 -> t2 -> MinMaxType t1 t2
min
  max :: Dyadic -> MPBall -> MinMaxType Dyadic MPBall
max = (MPBall -> MPBall -> MPBall) -> Dyadic -> MPBall -> MPBall
forall a b c.
ConvertibleExactly a b =>
(b -> b -> c) -> a -> b -> c
convertFirst MPBall -> MPBall -> MPBall
forall t1 t2.
CanMinMaxAsymmetric t1 t2 =>
t1 -> t2 -> MinMaxType t1 t2
max

instance CanMinMaxAsymmetric MPBall Rational where
  type MinMaxType MPBall Rational = MPBall
  min :: MPBall -> Rational -> MinMaxType MPBall Rational
min = (MPBall -> MPBall -> MPBall) -> MPBall -> Rational -> MPBall
forall t2 t1 c.
(ConvertibleWithPrecision t2 t1, HasPrecision t1) =>
(t1 -> t1 -> c) -> t1 -> t2 -> c
convertPSecond MPBall -> MPBall -> MPBall
forall t1 t2.
CanMinMaxAsymmetric t1 t2 =>
t1 -> t2 -> MinMaxType t1 t2
min
  max :: MPBall -> Rational -> MinMaxType MPBall Rational
max = (MPBall -> MPBall -> MPBall) -> MPBall -> Rational -> MPBall
forall t2 t1 c.
(ConvertibleWithPrecision t2 t1, HasPrecision t1) =>
(t1 -> t1 -> c) -> t1 -> t2 -> c
convertPSecond MPBall -> MPBall -> MPBall
forall t1 t2.
CanMinMaxAsymmetric t1 t2 =>
t1 -> t2 -> MinMaxType t1 t2
max
instance CanMinMaxAsymmetric Rational MPBall where
  type MinMaxType Rational MPBall = MPBall
  min :: Rational -> MPBall -> MinMaxType Rational MPBall
min = (MPBall -> MPBall -> MPBall) -> Rational -> MPBall -> MPBall
forall t1 t2 c.
(ConvertibleWithPrecision t1 t2, HasPrecision t2) =>
(t2 -> t2 -> c) -> t1 -> t2 -> c
convertPFirst MPBall -> MPBall -> MPBall
forall t1 t2.
CanMinMaxAsymmetric t1 t2 =>
t1 -> t2 -> MinMaxType t1 t2
min
  max :: Rational -> MPBall -> MinMaxType Rational MPBall
max = (MPBall -> MPBall -> MPBall) -> Rational -> MPBall -> MPBall
forall t1 t2 c.
(ConvertibleWithPrecision t1 t2, HasPrecision t2) =>
(t2 -> t2 -> c) -> t1 -> t2 -> c
convertPFirst MPBall -> MPBall -> MPBall
forall t1 t2.
CanMinMaxAsymmetric t1 t2 =>
t1 -> t2 -> MinMaxType t1 t2
max

instance
  (CanMinMaxAsymmetric MPBall b
  , CanBeErrors es)
  =>
  CanMinMaxAsymmetric MPBall (CollectErrors es  b)
  where
  type MinMaxType MPBall (CollectErrors es  b) =
    CollectErrors es (MinMaxType MPBall b)
  min :: MPBall
-> CollectErrors es b -> MinMaxType MPBall (CollectErrors es b)
min = (MPBall -> b -> MinMaxType MPBall b)
-> MPBall
-> CollectErrors es b
-> CollectErrors es (MinMaxType MPBall b)
forall es a b c.
Monoid es =>
(a -> b -> c) -> a -> CollectErrors es b -> CollectErrors es c
CE.liftT1 MPBall -> b -> MinMaxType MPBall b
forall t1 t2.
CanMinMaxAsymmetric t1 t2 =>
t1 -> t2 -> MinMaxType t1 t2
min
  max :: MPBall
-> CollectErrors es b -> MinMaxType MPBall (CollectErrors es b)
max = (MPBall -> b -> MinMaxType MPBall b)
-> MPBall
-> CollectErrors es b
-> CollectErrors es (MinMaxType MPBall b)
forall es a b c.
Monoid es =>
(a -> b -> c) -> a -> CollectErrors es b -> CollectErrors es c
CE.liftT1 MPBall -> b -> MinMaxType MPBall b
forall t1 t2.
CanMinMaxAsymmetric t1 t2 =>
t1 -> t2 -> MinMaxType t1 t2
max

instance
  (CanMinMaxAsymmetric a MPBall
  , CanBeErrors es)
  =>
  CanMinMaxAsymmetric (CollectErrors es a) MPBall
  where
  type MinMaxType (CollectErrors es  a) MPBall =
    CollectErrors es (MinMaxType a MPBall)
  min :: CollectErrors es a
-> MPBall -> MinMaxType (CollectErrors es a) MPBall
min = (a -> MPBall -> MinMaxType a MPBall)
-> CollectErrors es a
-> MPBall
-> CollectErrors es (MinMaxType a MPBall)
forall es a b c.
Monoid es =>
(a -> b -> c) -> CollectErrors es a -> b -> CollectErrors es c
CE.lift1T a -> MPBall -> MinMaxType a MPBall
forall t1 t2.
CanMinMaxAsymmetric t1 t2 =>
t1 -> t2 -> MinMaxType t1 t2
min
  max :: CollectErrors es a
-> MPBall -> MinMaxType (CollectErrors es a) MPBall
max = (a -> MPBall -> MinMaxType a MPBall)
-> CollectErrors es a
-> MPBall
-> CollectErrors es (MinMaxType a MPBall)
forall es a b c.
Monoid es =>
(a -> b -> c) -> CollectErrors es a -> b -> CollectErrors es c
CE.lift1T a -> MPBall -> MinMaxType a MPBall
forall t1 t2.
CanMinMaxAsymmetric t1 t2 =>
t1 -> t2 -> MinMaxType t1 t2
max

{- intersection -}

instance CanIntersectAsymmetric MPBall MPBall where
  intersect :: MPBall -> MPBall -> IntersectionType MPBall MPBall
intersect MPBall
a MPBall
b
    | MPFloat
MinMaxType MPFloat MPFloat
l MPFloat -> MPFloat -> OrderCompareType MPFloat MPFloat
forall a b.
HasOrderAsymmetric a b =>
a -> b -> OrderCompareType a b
> MPFloat
MinMaxType MPFloat MPFloat
r =
        NumError -> CN MPBall
forall v. NumError -> CN v
CN.noValueNumErrorCertain (NumError -> CN MPBall) -> NumError -> CN MPBall
forall a b. (a -> b) -> a -> b
$ String -> NumError
CN.NumError (String -> NumError) -> String -> NumError
forall a b. (a -> b) -> a -> b
$ String
"intersect: empty intersection: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ MPBall -> String
forall a. Show a => a -> String
show MPBall
a String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"; " String -> String -> String
forall a. [a] -> [a] -> [a]
++ MPBall -> String
forall a. Show a => a -> String
show MPBall
b
    | Bool
otherwise = MPBall -> CN MPBall
forall v. v -> CN v
cn (MPBall -> CN MPBall) -> MPBall -> CN MPBall
forall a b. (a -> b) -> a -> b
$ Precision -> MPBall -> MPBall
forall t. CanSetPrecision t => Precision -> t -> t
setPrecision Precision
p (MPBall -> MPBall) -> MPBall -> MPBall
forall a b. (a -> b) -> a -> b
$ MPFloat -> MPFloat -> MPBall
fromMPFloatEndpoints MPFloat
MinMaxType MPFloat MPFloat
l MPFloat
MinMaxType MPFloat MPFloat
r
    where
    p :: Precision
p  = MPBall -> Precision
forall t. HasPrecision t => t -> Precision
getPrecision MPBall
a
    l :: MinMaxType MPFloat MPFloat
l = MPFloat -> MPFloat -> MinMaxType MPFloat MPFloat
forall t1 t2.
CanMinMaxAsymmetric t1 t2 =>
t1 -> t2 -> MinMaxType t1 t2
max MPFloat
aL MPFloat
bL
    r :: MinMaxType MPFloat MPFloat
r = MPFloat -> MPFloat -> MinMaxType MPFloat MPFloat
forall t1 t2.
CanMinMaxAsymmetric t1 t2 =>
t1 -> t2 -> MinMaxType t1 t2
min MPFloat
aR MPFloat
bR
    (MPFloat
aL,MPFloat
aR) = MPBall -> (IntervalEndpoint MPBall, IntervalEndpoint MPBall)
forall i.
IsInterval i =>
i -> (IntervalEndpoint i, IntervalEndpoint i)
endpoints MPBall
a
    (MPFloat
bL,MPFloat
bR) = MPBall -> (IntervalEndpoint MPBall, IntervalEndpoint MPBall)
forall i.
IsInterval i =>
i -> (IntervalEndpoint i, IntervalEndpoint i)
endpoints MPBall
b

intersectCNMPBall :: CN MPBall -> CN MPBall -> CN MPBall
intersectCNMPBall :: CN MPBall -> CN MPBall -> CN MPBall
intersectCNMPBall = CN MPBall -> CN MPBall -> CN MPBall
forall e1 e2.
CanIntersectAsymmetric e1 e2 =>
e1 -> e2 -> IntersectionType e1 e2
intersect
  -- case (fst $ ensureNoCN x, fst $ ensureNoCN y) of 
  --   (Nothing, Nothing) -> x
  --   (Just _ , Nothing) -> x
  --   (Nothing, Just _ ) -> y
  --   (Just _ , Just _ ) -> lift2CE intersect x y

instance
  (CanIntersectAsymmetric MPBall b
  , CanBeErrors es)
  =>
  CanIntersectAsymmetric MPBall (CollectErrors es b)
  where
  type IntersectionType MPBall (CollectErrors es b) =
    CollectErrors es (IntersectionType MPBall b)
  intersect :: MPBall
-> CollectErrors es b
-> IntersectionType MPBall (CollectErrors es b)
intersect = (MPBall -> b -> IntersectionType MPBall b)
-> MPBall
-> CollectErrors es b
-> CollectErrors es (IntersectionType MPBall b)
forall es a b c.
Monoid es =>
(a -> b -> c) -> a -> CollectErrors es b -> CollectErrors es c
CE.liftT1 MPBall -> b -> IntersectionType MPBall b
forall e1 e2.
CanIntersectAsymmetric e1 e2 =>
e1 -> e2 -> IntersectionType e1 e2
intersect

instance
  (CanIntersectAsymmetric a MPBall
  , CanBeErrors es)
  =>
  CanIntersectAsymmetric (CollectErrors es a) MPBall
  where
  type IntersectionType (CollectErrors es  a) MPBall =
    CollectErrors es (IntersectionType a MPBall)
  intersect :: CollectErrors es a
-> MPBall -> IntersectionType (CollectErrors es a) MPBall
intersect = (a -> MPBall -> IntersectionType a MPBall)
-> CollectErrors es a
-> MPBall
-> CollectErrors es (IntersectionType a MPBall)
forall es a b c.
Monoid es =>
(a -> b -> c) -> CollectErrors es a -> b -> CollectErrors es c
CE.lift1T a -> MPBall -> IntersectionType a MPBall
forall e1 e2.
CanIntersectAsymmetric e1 e2 =>
e1 -> e2 -> IntersectionType e1 e2
intersect

{- hull -}

hullMPBall :: MPBall -> MPBall -> MPBall
hullMPBall :: MPBall -> MPBall -> MPBall
hullMPBall MPBall
a MPBall
b = 
  IntervalEndpoint MPBall -> IntervalEndpoint MPBall -> MPBall
forall i.
IsInterval i =>
IntervalEndpoint i -> IntervalEndpoint i -> i
fromEndpoints MinMaxType MPFloat MPFloat
IntervalEndpoint MPBall
rL MinMaxType MPFloat MPFloat
IntervalEndpoint MPBall
rR
  where
  rL :: MinMaxType MPFloat MPFloat
rL = MPFloat -> MPFloat -> MinMaxType MPFloat MPFloat
forall t1 t2.
CanMinMaxAsymmetric t1 t2 =>
t1 -> t2 -> MinMaxType t1 t2
min MPFloat
aL MPFloat
bL
  rR :: MinMaxType MPFloat MPFloat
rR = MPFloat -> MPFloat -> MinMaxType MPFloat MPFloat
forall t1 t2.
CanMinMaxAsymmetric t1 t2 =>
t1 -> t2 -> MinMaxType t1 t2
max MPFloat
aR MPFloat
bR
  (MPFloat
aL,MPFloat
aR) = MPBall -> (IntervalEndpoint MPBall, IntervalEndpoint MPBall)
forall i.
IsInterval i =>
i -> (IntervalEndpoint i, IntervalEndpoint i)
endpoints MPBall
a
  (MPFloat
bL,MPFloat
bR) = MPBall -> (IntervalEndpoint MPBall, IntervalEndpoint MPBall)
forall i.
IsInterval i =>
i -> (IntervalEndpoint i, IntervalEndpoint i)
endpoints MPBall
b

{- union -}

instance CanUnionAsymmetric MPBall MPBall where
  union :: MPBall -> MPBall -> UnionType MPBall MPBall
union MPBall
a MPBall
b =
    case CN MPBall -> Maybe MPBall
forall es v. CollectErrors es v -> Maybe v
getMaybeValue (MPBall
a MPBall -> MPBall -> IntersectionType MPBall MPBall
forall e1 e2.
CanIntersectAsymmetric e1 e2 =>
e1 -> e2 -> IntersectionType e1 e2
`intersect` MPBall
b) of
      Just MPBall
_ -> CN MPBall
UnionType MPBall MPBall
r
      Maybe MPBall
_ -> NumError -> CN MPBall -> CN MPBall
forall t. NumError -> CN t -> CN t
CN.prependErrorCertain NumError
err CN MPBall
r
    where
    err :: NumError
err = String -> NumError
CN.NumError (String -> NumError) -> String -> NumError
forall a b. (a -> b) -> a -> b
$ String
"union of enclosures: not enclosing the same value"
    r :: CN MPBall
r = MPBall -> CN MPBall
forall v. v -> CN v
cn (MPBall -> CN MPBall) -> MPBall -> CN MPBall
forall a b. (a -> b) -> a -> b
$ MPBall -> MPBall -> MPBall
hullMPBall MPBall
a MPBall
b

instance
  (CanUnionAsymmetric MPBall b
  , CanBeErrors es)
  =>
  CanUnionAsymmetric MPBall (CollectErrors es b)
  where
  type UnionType MPBall (CollectErrors es b) =
    CollectErrors es (UnionType MPBall b)
  union :: MPBall
-> CollectErrors es b -> UnionType MPBall (CollectErrors es b)
union = (MPBall -> b -> UnionType MPBall b)
-> MPBall
-> CollectErrors es b
-> CollectErrors es (UnionType MPBall b)
forall es a b c.
Monoid es =>
(a -> b -> c) -> a -> CollectErrors es b -> CollectErrors es c
CE.liftT1 MPBall -> b -> UnionType MPBall b
forall e1 e2.
CanUnionAsymmetric e1 e2 =>
e1 -> e2 -> UnionType e1 e2
union

instance
  (CanUnionAsymmetric a MPBall
  , CanBeErrors es)
  =>
  CanUnionAsymmetric (CollectErrors es a) MPBall
  where
  type UnionType (CollectErrors es  a) MPBall =
    CollectErrors es (UnionType a MPBall)
  union :: CollectErrors es a
-> MPBall -> UnionType (CollectErrors es a) MPBall
union = (a -> MPBall -> UnionType a MPBall)
-> CollectErrors es a
-> MPBall
-> CollectErrors es (UnionType a MPBall)
forall es a b c.
Monoid es =>
(a -> b -> c) -> CollectErrors es a -> b -> CollectErrors es c
CE.lift1T a -> MPBall -> UnionType a MPBall
forall e1 e2.
CanUnionAsymmetric e1 e2 =>
e1 -> e2 -> UnionType e1 e2
union

{-|
  Compute an MPBall function from *exact* MPFloat operations on interval endpoints.
  This works only for *non-decreasing* operations, eg addition, min, max.
-}
byEndpointsMP ::
    (MPFloat -> MPFloat -> MPFloat) ->
    (MPBall -> MPBall -> MPBall)
byEndpointsMP :: (MPFloat -> MPFloat -> MPFloat) -> MPBall -> MPBall -> MPBall
byEndpointsMP MPFloat -> MPFloat -> MPFloat
op MPBall
b1 MPBall
b2 =
    IntervalEndpoint MPBall -> IntervalEndpoint MPBall -> MPBall
forall i.
IsInterval i =>
IntervalEndpoint i -> IntervalEndpoint i -> i
fromEndpoints (MPFloat
l1 MPFloat -> MPFloat -> MPFloat
`op` MPFloat
l2) (MPFloat
r1 MPFloat -> MPFloat -> MPFloat
`op` MPFloat
r2)
    where
    (MPFloat
l1,MPFloat
r1) = MPBall -> (IntervalEndpoint MPBall, IntervalEndpoint MPBall)
forall i.
IsInterval i =>
i -> (IntervalEndpoint i, IntervalEndpoint i)
endpoints MPBall
b1
    (MPFloat
l2,MPFloat
r2) = MPBall -> (IntervalEndpoint MPBall, IntervalEndpoint MPBall)
forall i.
IsInterval i =>
i -> (IntervalEndpoint i, IntervalEndpoint i)
endpoints MPBall
b2