{-# OPTIONS_GHC -Wno-orphans #-}
module AERN2.BoxFun.Box where

import qualified Prelude as Prelude
import MixedTypesNumPrelude
import AERN2.MP.Dyadic
import AERN2.MP.Ball
import AERN2.Linear.Vector.Type (Vector, (!))
import qualified AERN2.Linear.Vector.Type as V
import AERN2.Util.Util

type Box = Vector (CN MPBall)

-- instance HasEqAsymmetric Box Box where
--     type EqCompareType Box Box = (CN Kleenean)
--     equalTo box0 box1 = 
--         V.foldl' (&&) (cn CertainTrue) $ V.zipWith equalTo box0 box1

-- intersection :: Box -> Box -> Maybe Box
-- intersection = undefined

intersectionCertainlyEmpty :: Box -> Box -> Bool
intersectionCertainlyEmpty :: Box -> Box -> Bool
intersectionCertainlyEmpty Box
vx Box
vy =
    (Bool -> Bool -> Bool) -> Bool -> Vector Bool -> Bool
forall b a. (b -> a -> b) -> b -> Vector a -> b
V.foldl' Bool -> Bool -> Bool
forall a b. CanAndOrAsymmetric a b => a -> b -> AndOrType a b
(||) Bool
False (Vector Bool -> Bool) -> Vector Bool -> Bool
forall a b. (a -> b) -> a -> b
$ (CN MPBall -> CN MPBall -> Bool) -> Box -> Box -> Vector Bool
forall a b c. (a -> b -> c) -> Vector a -> Vector b -> Vector c
V.zipWith CN MPBall -> CN MPBall -> Bool
forall b a.
(IsInterval b, IsInterval a, HasOrderAsymmetric a b,
 CanTestCertainly (OrderCompareType a b)) =>
a -> b -> Bool
ballIntersectionCertainlyEmpty Box
vx Box
vy
    where 
    ballIntersectionCertainlyEmpty :: a -> b -> AndOrType Bool Bool
ballIntersectionCertainlyEmpty a
x b
y = 
        (a
lx a -> b -> Bool
forall a b. HasOrderCertainlyAsymmetric a b => a -> b -> Bool
!>! b
ry Bool -> Bool -> AndOrType Bool Bool
forall a b. CanAndOrAsymmetric a b => a -> b -> AndOrType a b
|| a
rx a -> b -> Bool
forall a b. HasOrderCertainlyAsymmetric a b => a -> b -> Bool
!<! b
ly)
        where
        (a
lx, a
rx) = a -> (a, a)
forall i. IsInterval i => i -> (i, i)
endpointsAsIntervals a
x
        (b
ly, b
ry) = b -> (b, b)
forall i. IsInterval i => i -> (i, i)
endpointsAsIntervals b
y

nonEmptyIntersection :: Box -> Box -> Box
nonEmptyIntersection :: Box -> Box -> Box
nonEmptyIntersection Box
vx Box
vy = 
    (CN MPBall -> CN MPBall -> CN MPBall) -> Box -> Box -> Box
forall a b c. (a -> b -> c) -> Vector a -> Vector b -> Vector c
V.zipWith CN MPBall -> CN MPBall -> CN MPBall
intersectCN Box
vx Box
vy

instance IsBall Box where
    type CentreType (Box) = Box
    centre :: Box -> CentreType Box
centre Box
v = (CN MPBall -> CN MPBall) -> Box -> Box
forall a b. (a -> b) -> Vector a -> Vector b
V.map (\CN MPBall
x -> (Precision -> CN Dyadic -> CN MPBall
forall a. CanBeMPBallP a => Precision -> CN a -> CN MPBall
cnMPBallP (CN MPBall -> Precision
forall t. HasPrecision t => t -> Precision
getPrecision CN MPBall
x) (CN Dyadic -> CN MPBall)
-> (CN MPBall -> CN Dyadic) -> CN MPBall -> CN MPBall
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CN MPBall -> CN Dyadic
forall t. IsBall t => t -> CentreType t
centre) CN MPBall
x) Box
v

inftyNorm :: Box -> CN MPBall
inftyNorm :: Box -> CN MPBall
inftyNorm Box
box = 
    (CN MPBall -> CN MPBall -> CN MPBall)
-> CN MPBall -> Box -> CN MPBall
forall b a. (b -> a -> b) -> b -> Vector a -> b
V.foldl' (\CN MPBall
n CN MPBall
x -> CN MPBall -> CN MPBall -> MinMaxType (CN MPBall) (CN MPBall)
forall t1 t2.
CanMinMaxAsymmetric t1 t2 =>
t1 -> t2 -> MinMaxType t1 t2
max CN MPBall
n (CN MPBall -> AbsType (CN MPBall)
forall t. CanAbs t => t -> AbsType t
abs CN MPBall
x)) (MPBall -> CN MPBall
forall v. v -> CN v
cn (MPBall -> CN MPBall) -> MPBall -> CN MPBall
forall a b. (a -> b) -> a -> b
$ Integer -> MPBall
forall t. CanBeMPBall t => t -> MPBall
mpBall Integer
0) Box
box

ellOneNorm :: Box -> CN MPBall
ellOneNorm :: Box -> CN MPBall
ellOneNorm Box
box = 
    (CN MPBall -> CN MPBall -> CN MPBall)
-> CN MPBall -> Box -> CN MPBall
forall b a. (b -> a -> b) -> b -> Vector a -> b
V.foldl' (\CN MPBall
n CN MPBall
x -> CN MPBall
n CN MPBall -> CN MPBall -> AddType (CN MPBall) (CN MPBall)
forall t1 t2. CanAddAsymmetric t1 t2 => t1 -> t2 -> AddType t1 t2
+ (CN MPBall -> AbsType (CN MPBall)
forall t. CanAbs t => t -> AbsType t
abs CN MPBall
x)) (MPBall -> CN MPBall
forall v. v -> CN v
cn (MPBall -> CN MPBall) -> MPBall -> CN MPBall
forall a b. (a -> b) -> a -> b
$ Integer -> MPBall
forall t. CanBeMPBall t => t -> MPBall
mpBall Integer
0) Box
box

width :: Box -> CN MPBall
width :: Box -> CN MPBall
width Box
box = 
    (CN MPBall -> CN MPBall -> CN MPBall)
-> CN MPBall -> Box -> CN MPBall
forall b a. (b -> a -> b) -> b -> Vector a -> b
V.foldl' (\CN MPBall
n CN MPBall
x -> CN MPBall -> CN MPBall -> MinMaxType (CN MPBall) (CN MPBall)
forall t1 t2.
CanMinMaxAsymmetric t1 t2 =>
t1 -> t2 -> MinMaxType t1 t2
max CN MPBall
n (Integer
2 Integer -> CN MPBall -> MulType Integer (CN MPBall)
forall t1 t2. CanMulAsymmetric t1 t2 => t1 -> t2 -> MulType t1 t2
* (((ErrorBound -> MPBall)
-> CollectErrors NumErrors ErrorBound -> CN MPBall
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ErrorBound -> MPBall
forall t. CanBeMPBall t => t -> MPBall
mpBall) (CollectErrors NumErrors ErrorBound -> CN MPBall)
-> CollectErrors NumErrors ErrorBound -> CN MPBall
forall a b. (a -> b) -> a -> b
$ ((MPBall -> ErrorBound)
-> CN MPBall -> CollectErrors NumErrors ErrorBound
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap MPBall -> ErrorBound
forall t. IsBall t => t -> ErrorBound
radius) CN MPBall
x) ) ) (MPBall -> CN MPBall
forall v. v -> CN v
cn (MPBall -> CN MPBall) -> MPBall -> CN MPBall
forall a b. (a -> b) -> a -> b
$ Integer -> MPBall
forall t. CanBeMPBall t => t -> MPBall
mpBall Integer
0) Box
box

widestDirection :: Box -> Integer
widestDirection :: Box -> Integer
widestDirection Box
box =
    Integer -> Integer -> Dyadic -> Integer
aux (Box -> Integer
forall a. Vector a -> Integer
V.length Box
box Integer -> Integer -> SubType Integer Integer
forall t1 t2. CanSub t1 t2 => t1 -> t2 -> SubType t1 t2
- Integer
1) (Box -> Integer
forall a. Vector a -> Integer
V.length Box
box Integer -> Integer -> SubType Integer Integer
forall t1 t2. CanSub t1 t2 => t1 -> t2 -> SubType t1 t2
- Integer
1) (Integer -> Dyadic
forall t. CanBeDyadic t => t -> Dyadic
dyadic Integer
0)
    where
    aux :: Integer -> Integer -> Dyadic -> Integer
aux Integer
k Integer
i Dyadic
lth =
        if Integer
k Integer -> Integer -> OrderCompareType Integer Integer
forall a b.
HasOrderAsymmetric a b =>
a -> b -> OrderCompareType a b
< Integer
0 then 
            Integer
i
        else
            let 
                x :: CN MPBall
x    = Box
box Box -> Integer -> CN MPBall
forall a. Vector a -> Integer -> a
! Integer
k
                lth' :: MulType Integer Dyadic
lth' = 
                    Integer
2 Integer -> Dyadic -> MulType Integer Dyadic
forall t1 t2. CanMulAsymmetric t1 t2 => t1 -> t2 -> MulType t1 t2
* (ErrorBound -> Dyadic
forall t. CanBeDyadic t => t -> Dyadic
dyadic (ErrorBound -> Dyadic) -> ErrorBound -> Dyadic
forall a b. (a -> b) -> a -> b
$ MPBall -> ErrorBound
forall t. IsBall t => t -> ErrorBound
radius (MPBall -> ErrorBound) -> MPBall -> ErrorBound
forall a b. (a -> b) -> a -> b
$ CN MPBall -> MPBall
forall p. CN p -> p
unCN CN MPBall
x) -- TODO: unsafe
            in
                if (Dyadic
MulType Integer Dyadic
lth' Dyadic -> Dyadic -> OrderCompareType Dyadic Dyadic
forall a b.
HasOrderAsymmetric a b =>
a -> b -> OrderCompareType a b
> Dyadic
lth) then 
                    Integer -> Integer -> Dyadic -> Integer
aux (Integer
k Integer -> Integer -> SubType Integer Integer
forall t1 t2. CanSub t1 t2 => t1 -> t2 -> SubType t1 t2
- Integer
1) Integer
k Dyadic
MulType Integer Dyadic
lth'
                else 
                    Integer -> Integer -> Dyadic -> Integer
aux (Integer
k Integer -> Integer -> SubType Integer Integer
forall t1 t2. CanSub t1 t2 => t1 -> t2 -> SubType t1 t2
- Integer
1) Integer
i Dyadic
lth


bisect :: Integer -> Box -> (Box, Box)
bisect :: Integer -> Box -> (Box, Box)
bisect Integer
k Box
box =
    if Integer
SubType Integer Integer
exponent_ Integer -> Integer -> OrderCompareType Integer Integer
forall a b.
HasOrderAsymmetric a b =>
a -> b -> OrderCompareType a b
> Integer
0
    Bool -> Bool -> AndOrType Bool Bool
forall a b. CanAndOrAsymmetric a b => a -> b -> AndOrType a b
&& (Dyadic -> AbsType Dyadic
forall t. CanAbs t => t -> AbsType t
abs (Dyadic -> AbsType Dyadic) -> Dyadic -> AbsType Dyadic
forall a b. (a -> b) -> a -> b
$ Dyadic
mc Dyadic -> Dyadic -> SubType Dyadic Dyadic
forall t1 t2. CanSub t1 t2 => t1 -> t2 -> SubType t1 t2
- Dyadic
lbc) Dyadic -> Dyadic -> OrderCompareType Dyadic Dyadic
forall a b.
HasOrderAsymmetric a b =>
a -> b -> OrderCompareType a b
< (Rational -> Dyadic
forall t. CanBeDyadic t => t -> Dyadic
dyadic Rational
0.5)Dyadic -> Integer -> PowType Dyadic Integer
forall t1 t2. CanPow t1 t2 => t1 -> t2 -> PowType t1 t2
^Integer
SubType Integer Integer
exponent_ then
        (Precision -> Box -> Box
forall t. CanSetPrecision t => Precision -> t -> t
setPrecision (Precision -> AddType Precision Precision
forall t.
(CanAddAsymmetric t Precision, ConvertibleExactly t Integer) =>
t -> AddType t Precision
increasePrecision Precision
p) Box
lb, Precision -> Box -> Box
forall t. CanSetPrecision t => Precision -> t -> t
setPrecision (Precision -> AddType Precision Precision
forall t.
(CanAddAsymmetric t Precision, ConvertibleExactly t Integer) =>
t -> AddType t Precision
increasePrecision Precision
p) Box
rb)
    else 
        (Box
lb, Box
rb)
    where
    exponent_ :: SubType Integer Integer
exponent_      = Precision -> Integer
forall t. CanBeInteger t => t -> Integer
integer Precision
p Integer -> Integer -> SubType Integer Integer
forall t1 t2. CanSub t1 t2 => t1 -> t2 -> SubType t1 t2
- Integer
ilog
    NormBits Integer
ilog = Integer
2 Integer -> NormLog -> AddType Integer NormLog
forall t1 t2. CanAddAsymmetric t1 t2 => t1 -> t2 -> AddType t1 t2
+ (MPBall -> NormLog
forall a. HasNorm a => a -> NormLog
getNormLog (MPBall -> NormLog) -> MPBall -> NormLog
forall a b. (a -> b) -> a -> b
$  Dyadic -> MPBall
forall t. CanBeMPBall t => t -> MPBall
mpBall (Dyadic -> MPBall) -> Dyadic -> MPBall
forall a b. (a -> b) -> a -> b
$ Integer
1 Integer -> Dyadic -> AddType Integer Dyadic
forall t1 t2. CanAddAsymmetric t1 t2 => t1 -> t2 -> AddType t1 t2
+ Dyadic -> AbsType Dyadic
forall t. CanAbs t => t -> AbsType t
abs Dyadic
lbc )
    lbc :: Dyadic
lbc = CN Dyadic -> Dyadic
forall p. CN p -> p
unCN (CN MPBall -> CentreType (CN MPBall)
forall t. IsBall t => t -> CentreType t
centre (CN MPBall -> CentreType (CN MPBall))
-> CN MPBall -> CentreType (CN MPBall)
forall a b. (a -> b) -> a -> b
$ Box
lb Box -> Integer -> CN MPBall
forall a. Vector a -> Integer -> a
! Integer
k)
    -- rbc = unCN (centre $ rb ! k)
    mc :: Dyadic
mc  = CN Dyadic -> Dyadic
forall p. CN p -> p
unCN (CN MPBall -> CentreType (CN MPBall)
forall t. IsBall t => t -> CentreType t
centre (CN MPBall -> CentreType (CN MPBall))
-> CN MPBall -> CentreType (CN MPBall)
forall a b. (a -> b) -> a -> b
$ CN MPBall
m)
    increasePrecision :: t -> AddType t Precision
increasePrecision t
p1 =
        t
p1 t -> Precision -> AddType t Precision
forall t1 t2. CanAddAsymmetric t1 t2 => t1 -> t2 -> AddType t1 t2
+ (Integer -> Precision
prec (Integer -> Precision) -> Integer -> Precision
forall a b. (a -> b) -> a -> b
$ (t -> Integer
forall t. CanBeInteger t => t -> Integer
integer t
p1) Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`Prelude.div` Integer
2)
    lb :: Box
lb = Precision -> Box -> Box
forall t. CanSetPrecision t => Precision -> t -> t
setPrecision Precision
p Box
leftBox
    rb :: Box
rb = Precision -> Box -> Box
forall t. CanSetPrecision t => Precision -> t -> t
setPrecision Precision
p Box
rightBox
    p :: Precision
p   = Box -> Precision
forall t. HasPrecision t => t -> Precision
getPrecision Box
box
    interval :: CN MPBall
interval = Box
box Box -> Integer -> CN MPBall
forall a. Vector a -> Integer -> a
! Integer
k
    m :: CN MPBall
m = ((Dyadic -> MPBall) -> CN Dyadic -> CN MPBall
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((Dyadic -> MPBall) -> CN Dyadic -> CN MPBall)
-> (Dyadic -> MPBall) -> CN Dyadic -> CN MPBall
forall a b. (a -> b) -> a -> b
$ Precision -> Dyadic -> MPBall
forall t. CanBeMPBallP t => Precision -> t -> MPBall
mpBallP Precision
p) (CN Dyadic -> CN MPBall) -> CN Dyadic -> CN MPBall
forall a b. (a -> b) -> a -> b
$ ((MPBall -> Dyadic) -> CN MPBall -> CN Dyadic
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap MPBall -> Dyadic
forall t. IsBall t => t -> CentreType t
centre) CN MPBall
interval
    (CN MPBall
l, CN MPBall
r) = CN MPBall -> (CN MPBall, CN MPBall)
forall i. IsInterval i => i -> (i, i)
endpointsAsIntervals CN MPBall
interval
    leftBox :: Box
leftBox  = (Integer -> CN MPBall -> CN MPBall) -> Box -> Box
forall a b. (Integer -> a -> b) -> Vector a -> Vector b
V.imap (\Integer
i CN MPBall
x -> if Integer
i Integer -> Integer -> EqCompareType Integer Integer
forall a b. HasEqAsymmetric a b => a -> b -> EqCompareType a b
== Integer
k then CN MPBall -> CN MPBall -> CN MPBall
forall i.
(IsInterval i, CanMinMaxSameType (IntervalEndpoint i)) =>
i -> i -> i
fromEndpointsAsIntervals CN MPBall
l CN MPBall
m else CN MPBall
x) Box
box
    rightBox :: Box
rightBox = (Integer -> CN MPBall -> CN MPBall) -> Box -> Box
forall a b. (Integer -> a -> b) -> Vector a -> Vector b
V.imap (\Integer
i CN MPBall
x -> if Integer
i Integer -> Integer -> EqCompareType Integer Integer
forall a b. HasEqAsymmetric a b => a -> b -> EqCompareType a b
== Integer
k then CN MPBall -> CN MPBall -> CN MPBall
forall i.
(IsInterval i, CanMinMaxSameType (IntervalEndpoint i)) =>
i -> i -> i
fromEndpointsAsIntervals CN MPBall
m CN MPBall
r else CN MPBall
x) Box
box

-- Bisects a into 2^d boxes of the same size, where d is the dimension of the given box
fullBisect :: Box -> [Box]
fullBisect :: Box -> [Box]
fullBisect Box
b =
    case Box -> Integer
forall a. Vector a -> Integer
V.length Box
b of
        Integer
0 -> [Box
b]
        Integer
l ->
            -- y is the dimension bisected in the current iteration
            -- x is a bisection of the previous dimension (tail recursion)
            (Box -> [Box]) -> [Box] -> [Box]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (\Box
x -> (CN MPBall -> Box) -> [CN MPBall] -> [Box]
forall a b. (a -> b) -> [a] -> [b]
map (\CN MPBall
y -> Box
x Box -> Box -> Box
forall a. Vector a -> Vector a -> Vector a
V.+++ CN MPBall -> Box
forall a. a -> Vector a
V.singleton CN MPBall
y) (Integer -> [CN MPBall]
bisectDimension (Integer
lInteger -> Integer -> SubType Integer Integer
forall t1 t2. CanSub t1 t2 => t1 -> t2 -> SubType t1 t2
-Integer
1))) (Box -> [Box]
fullBisect (Int -> Box -> Box
forall a. Int -> Vector a -> Vector a
V.take (Integer -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Integer
lInteger -> Integer -> SubType Integer Integer
forall t1 t2. CanSub t1 t2 => t1 -> t2 -> SubType t1 t2
-Integer
1)) Box
b))

            where
                bisectDimension :: Integer -> [CN MPBall]
bisectDimension Integer
n = [(Box, Box) -> Box
forall a b. (a, b) -> a
fst (Box, Box)
bn Box -> Integer -> CN MPBall
forall a. Vector a -> Integer -> a
! Integer
n, (Box, Box) -> Box
forall a b. (a, b) -> b
snd (Box, Box)
bn Box -> Integer -> CN MPBall
forall a. Vector a -> Integer -> a
! Integer
n]
                    where bn :: (Box, Box)
bn = Integer -> Box -> (Box, Box)
bisect Integer
n Box
b

-- Get the endpoints of a box as a list containing a pair of MPBalls for each dimension
getEndpoints :: Box -> [(MPBall, MPBall)]
getEndpoints :: Box -> [(MPBall, MPBall)]
getEndpoints Box
b  = 
    case Box -> Integer
forall a. Vector a -> Integer
V.length Box
b of
        Integer
0 -> []
        Integer
_ -> MPBall -> (MPBall, MPBall)
forall i. IsInterval i => i -> (i, i)
endpointsAsIntervals (CN MPBall -> MPBall
forall p. CN p -> p
unCN (Box
b Box -> Integer -> CN MPBall
forall a. Vector a -> Integer -> a
! Integer
0)) (MPBall, MPBall) -> [(MPBall, MPBall)] -> [(MPBall, MPBall)]
forall a. a -> [a] -> [a]
: Box -> [(MPBall, MPBall)]
getEndpoints (Int -> Box -> Box
forall a. Int -> Vector a -> Vector a
V.drop (Integer -> Int
forall t. CanBeInt t => t -> Int
int Integer
1) Box
b)

lowerBounds :: Box -> Box
lowerBounds :: Box -> Box
lowerBounds = (CN MPBall -> CN MPBall) -> Box -> Box
forall a b. (a -> b) -> Vector a -> Vector b
V.map CN MPBall -> CN MPBall
forall i. IsInterval i => i -> i
lowerBound

upperBounds :: Box -> Box
upperBounds :: Box -> Box
upperBounds = (CN MPBall -> CN MPBall) -> Box -> Box
forall a b. (a -> b) -> Vector a -> Vector b
V.map CN MPBall -> CN MPBall
forall i. IsInterval i => i -> i
upperBound

instance 
    CanNeg Box
    where
    type NegType Box = Box
    negate :: Box -> NegType Box
negate Box
box = (CN MPBall -> CN MPBall) -> Box -> Box
forall a b. (a -> b) -> Vector a -> Vector b
V.map (\CN MPBall
x -> -CN MPBall
x) Box
box
    
createEnclosingBox :: Box -> Box -> Box
createEnclosingBox :: Box -> Box -> Box
createEnclosingBox Box
box1 Box
box2 =
    Box
enclosingBox
    where
        indexedBox1 :: Vector (Integer, CN MPBall)
indexedBox1 = Vector Integer -> Box -> Vector (Integer, CN MPBall)
forall a b. Vector a -> Vector b -> Vector (a, b)
V.zip ([Integer] -> Vector Integer
forall a. [a] -> Vector a
V.fromList [Integer
0 .. Integer -> Integer
forall t. CanBeInteger t => t -> Integer
integer (Box -> Integer
forall a. Vector a -> Integer
V.length Box
box1) Integer -> Integer -> SubType Integer Integer
forall t1 t2. CanSub t1 t2 => t1 -> t2 -> SubType t1 t2
- Integer
1]) Box
box1
        enclosingBox :: Box
enclosingBox = 
            ((Integer, CN MPBall) -> CN MPBall)
-> Vector (Integer, CN MPBall) -> Box
forall a b. (a -> b) -> Vector a -> Vector b
V.map 
            (\(Integer
i, CN MPBall
x) -> 
                let
                    y :: CN MPBall
y = Box
box2 Box -> Integer -> CN MPBall
forall a. Vector a -> Integer -> a
V.! Integer
i
                    (CN MPBall
l, CN MPBall
r) = CN MPBall -> (CN MPBall, CN MPBall)
forall i. IsInterval i => i -> (i, i)
endpointsAsIntervals CN MPBall
x
                    (CN MPBall
l', CN MPBall
r') = CN MPBall -> (CN MPBall, CN MPBall)
forall i. IsInterval i => i -> (i, i)
endpointsAsIntervals CN MPBall
y
                    newL :: MinMaxType (CN MPBall) (CN MPBall)
newL = CN MPBall -> CN MPBall -> MinMaxType (CN MPBall) (CN MPBall)
forall t1 t2.
CanMinMaxAsymmetric t1 t2 =>
t1 -> t2 -> MinMaxType t1 t2
min CN MPBall
l CN MPBall
l'
                    newR :: MinMaxType (CN MPBall) (CN MPBall)
newR = CN MPBall -> CN MPBall -> MinMaxType (CN MPBall) (CN MPBall)
forall t1 t2.
CanMinMaxAsymmetric t1 t2 =>
t1 -> t2 -> MinMaxType t1 t2
max CN MPBall
r CN MPBall
r'
                in
                    CN MPBall -> CN MPBall -> CN MPBall
forall i.
(IsInterval i, CanMinMaxSameType (IntervalEndpoint i)) =>
i -> i -> i
fromEndpointsAsIntervals CN MPBall
MinMaxType (CN MPBall) (CN MPBall)
newL CN MPBall
MinMaxType (CN MPBall) (CN MPBall)
newR)
            Vector (Integer, CN MPBall)
indexedBox1

intersectList :: [Box] -> Box
intersectList :: [Box] -> Box
intersectList []            = Box
forall a. Vector a
V.empty
intersectList [Box
b]           = Box
b
intersectList [Box
b1,Box
b2]       = if Box -> Box -> Bool
intersectionCertainlyEmpty Box
b1 Box
b2 then Box
forall a. Vector a
V.empty else Box -> Box -> Box
nonEmptyIntersection Box
b1 Box
b2
intersectList (Box
b1:Box
b2:[Box]
bs)    = if Box -> Box -> Bool
intersectionCertainlyEmpty Box
b1 Box
b2 then Box
forall a. Vector a
V.empty else [Box] -> Box
intersectList ([Box] -> Box) -> [Box] -> Box
forall a b. (a -> b) -> a -> b
$ Box -> Box -> Box
nonEmptyIntersection Box
b1 Box
b2 Box -> [Box] -> [Box]
forall a. a -> [a] -> [a]
: [Box]
bs