intervals 0.7 → 0.7.0.1
raw patch · 7 files changed
+183/−3 lines, 7 files
Files
- CHANGELOG.markdown +4/−0
- intervals.cabal +1/−1
- src/Numeric/Interval.hs +3/−0
- src/Numeric/Interval/Internal.hs +71/−1
- src/Numeric/Interval/Kaucher.hs +49/−0
- src/Numeric/Interval/NonEmpty.hs +2/−0
- src/Numeric/Interval/NonEmpty/Internal.hs +53/−1
CHANGELOG.markdown view
@@ -1,3 +1,7 @@+0.7.0.1+-------+* Removed a couple of unnecessary `Fractional` constraints.+ 0.7 --- * Corrected the definition of `mignitude`.
intervals.cabal view
@@ -1,5 +1,5 @@ name: intervals-version: 0.7+version: 0.7.0.1 synopsis: Interval Arithmetic description: A 'Numeric.Interval.Interval' is a closed, convex set of floating point values.
src/Numeric/Interval.hs view
@@ -12,6 +12,7 @@ module Numeric.Interval ( Interval , (...)+ , (+/-) , interval , whole , empty@@ -31,6 +32,8 @@ , magnitude , mignitude , distance+ , inflate, deflate+ , scale, symmetric , contains , isSubsetOf , certainly, (<!), (<=!), (==!), (>=!), (>!)
src/Numeric/Interval/Internal.hs view
@@ -20,6 +20,7 @@ module Numeric.Interval.Internal ( Interval(..) , (...)+ , (+/-) , interval , whole , empty@@ -39,6 +40,8 @@ , magnitude , mignitude , distance+ , inflate, deflate+ , scale, symmetric , contains , isSubsetOf , certainly, (<!), (<=!), (==!), (>=!), (>!)@@ -77,7 +80,11 @@ {-# INLINE foldMap #-} infix 3 ...+infixl 6 +/- +(+/-) :: (Num a, Ord a) => a -> a -> Interval a+a +/- b = a - b ... a + b+ negInfinity :: Fractional a => a negInfinity = (-1)/0 {-# INLINE negInfinity #-}@@ -261,6 +268,69 @@ distance :: (Num a, Ord a) => Interval a -> Interval a -> a distance i1 i2 = mignitude (i1 - i2) +-- | Inflate an interval by enlarging it at both ends.+--+-- >>> inflate 3 (-1 ... 7)+-- -4 ... 10+--+-- >>> inflate (-2) (0 ... 4)+-- -2 ... 6+--+-- >>> inflate 1 empty+-- Empty+inflate :: (Num a, Ord a) => a -> Interval a -> Interval a+inflate x y = symmetric x + y++-- | Deflate an interval by shrinking it from both ends.+--+-- >>> deflate 3.0 (-4.0 ... 10.0)+-- -1.0 ... 7.0+--+-- >>> deflate 2.0 (-1.0 ... 1.0)+-- Empty+--+-- >>> deflate 1.0 empty+-- Empty+deflate :: (Fractional a, Ord a) => a -> Interval a -> Interval a+deflate _ Empty = Empty+deflate x (I a b) | a' <= b' = I a' b'+ | otherwise = Empty+ where+ a' = a + x+ b' = b - x++-- | Scale an interval about its midpoint.+--+-- >>> scale 1.1 (-6.0 ... 4.0)+-- -6.5 ... 4.5+--+-- >>> scale (-2.0) (-1.0 ... 1.0)+-- Empty+--+-- >>> scale 3.0 empty+-- Empty+scale :: (Fractional a, Ord a) => a -> Interval a -> Interval a+scale _ Empty = Empty+scale x i = a ... b where+ h = x * width i / 2+ mid = midpoint i+ a = mid - h+ b = mid + h++-- | Construct a symmetric interval.+--+-- >>> symmetric 3+-- -3 ... 3+--+-- >>> symmetric (-2)+-- -2 ... 2+symmetric :: (Num a, Ord a) => a -> Interval a+symmetric x | a <= b = I a b+ | otherwise = I b a+ where+ a = negate x+ b = x+ instance (Num a, Ord a) => Num (Interval a) where I a b + I a' b' = (a + a') ... (b + b') _ + _ = Empty@@ -592,7 +662,7 @@ -- -- >>> intersection (1 ... 10 :: Interval Double) (5 ... 15 :: Interval Double) -- 5.0 ... 10.0-intersection :: (Fractional a, Ord a) => Interval a -> Interval a -> Interval a+intersection :: Ord a => Interval a -> Interval a -> Interval a intersection x@(I a b) y@(I a' b') | x /=! y = Empty | otherwise = I (max a a') (min b b')
src/Numeric/Interval/Kaucher.hs view
@@ -38,6 +38,8 @@ , magnitude , mignitude , distance+ , inflate, deflate+ , scale, symmetric , contains , isSubsetOf , certainly, (<!), (<=!), (==!), (>=!), (>!)@@ -272,6 +274,53 @@ -- NaN distance :: (Num a, Ord a) => Interval a -> Interval a -> a distance i1 i2 = mignitude (i1 - i2)++-- | Inflate an interval by enlarging it at both ends.+--+-- >>> inflate 3 (-1 ... 7)+-- -4 ... 10+--+-- >>> inflate (-2) (0 ... 4)+-- 2 ... 2+inflate :: (Num a, Ord a) => a -> Interval a -> Interval a+inflate x y = symmetric x + y++-- | Deflate an interval by shrinking it from both ends.+--+-- >>> deflate 3.0 (-4.0 ... 10.0)+-- -1.0 ... 7.0+--+-- >>> deflate 2.0 (-1.0 ... 1.0)+-- 1.0 ... -1.0+deflate :: (Fractional a, Ord a) => a -> Interval a -> Interval a+deflate x (I a b) = I a' b'+ where+ a' = a + x+ b' = b - x++-- | Scale an interval about its midpoint.+--+-- >>> scale 1.1 (-6.0 ... 4.0)+-- -6.5 ... 4.5+--+-- >>> scale (-2.0) (-1.0 ... 1.0)+-- 2.0 ... -2.0+scale :: (Fractional a, Ord a) => a -> Interval a -> Interval a+scale x i = I a b where+ h = x * width i / 2+ mid = midpoint i+ a = mid - h+ b = mid + h++-- | Construct a symmetric interval.+--+-- >>> symmetric 3+-- -3 ... 3+--+-- >>> symmetric (-2)+-- 2 ... -2+symmetric :: (Num a, Ord a) => a -> Interval a+symmetric x = negate x ... x instance (Num a, Ord a) => Num (Interval a) where I a b + I a' b' = (a + a') ... (b + b')
src/Numeric/Interval/NonEmpty.hs view
@@ -42,6 +42,8 @@ , certainly, (<!), (<=!), (==!), (>=!), (>!) , possibly, (<?), (<=?), (==?), (>=?), (>?) , clamp+ , inflate, deflate+ , scale, symmetric , idouble , ifloat ) where
src/Numeric/Interval/NonEmpty/Internal.hs view
@@ -41,6 +41,8 @@ , certainly, (<!), (<=!), (==!), (>=!), (>!) , possibly, (<?), (<=?), (==?), (>=?), (>?) , clamp+ , inflate, deflate+ , scale, symmetric , idouble , ifloat ) where@@ -471,7 +473,7 @@ -- -- >>> intersection (1 ... 10 :: Interval Double) (5 ... 15 :: Interval Double) -- Just (5.0 ... 10.0)-intersection :: (Fractional a, Ord a) => Interval a -> Interval a -> Maybe (Interval a)+intersection :: Ord a => Interval a -> Interval a -> Maybe (Interval a) intersection x@(I a b) y@(I a' b') | x /=! y = Nothing | otherwise = Just $ I (max a a') (min b b')@@ -654,6 +656,56 @@ | x < a = a | x > b = b | otherwise = x++-- | Inflate an interval by enlarging it at both ends.+--+-- >>> inflate 3 (-1 ... 7)+-- -4 ... 10+--+-- >>> inflate (-2) (0 ... 4)+-- -2 ... 6+inflate :: (Num a, Ord a) => a -> Interval a -> Interval a+inflate x y = symmetric x + y++-- | Deflate an interval by shrinking it from both ends.+-- Note that in cases that would result in an empty interval, the result is a singleton interval at the midpoint.+--+-- >>> deflate 3.0 (-4.0 ... 10.0)+-- -1.0 ... 7.0+--+-- >>> deflate 2.0 (-1.0 ... 1.0)+-- 0.0 ... 0.0+deflate :: (Fractional a, Ord a) => a -> Interval a -> Interval a+deflate x i@(I a b) | a' <= b' = I a' b'+ | otherwise = singleton m+ where+ a' = a + x+ b' = b - x+ m = midpoint i++-- | Scale an interval about its midpoint.+--+-- >>> scale 1.1 (-6.0 ... 4.0)+-- -6.5 ... 4.5+--+-- >>> scale (-2.0) (-1.0 ... 1.0)+-- -2.0 ... 2.0+scale :: (Fractional a, Ord a) => a -> Interval a -> Interval a+scale x i = a ... b where+ h = x * width i / 2+ mid = midpoint i+ a = mid - h+ b = mid + h++-- | Construct a symmetric interval.+--+-- >>> symmetric 3+-- -3 ... 3+--+-- >>> symmetric (-2)+-- -2 ... 2+symmetric :: (Num a, Ord a) => a -> Interval a+symmetric x = negate x ... x -- | id function. Useful for type specification --