Adaptive (empty) → 0.1
raw patch · 4 files changed
+515/−0 lines, 4 filesdep +basesetup-changed
Dependencies added: base
Files
- Adaptive.cabal +61/−0
- Data/Adaptive.hs +287/−0
- LICENSE +165/−0
- Setup.hs +2/−0
+ Adaptive.cabal view
@@ -0,0 +1,61 @@+-- Adaptive.cabal auto-generated by cabal init. For additional+-- options, see+-- http://www.haskell.org/cabal/release/cabal-latest/doc/users-guide/authors.html#pkg-descr.+-- The name of the package.+Name: Adaptive++-- The package version. See the Haskell package versioning policy+-- (http://www.haskell.org/haskellwiki/Package_versioning_policy) for+-- standards guiding when and how versions should be incremented.+Version: 0.1++-- A short (one-line) description of the package.+Synopsis: Adaptive precision floating-point arithmetic++-- A longer description of the package.+Description: Lazy arithmetic computed with as much precision as demanded ++-- URL for the project homepage or repository.+Homepage: http://github.com/HackerFoo/Adaptive++-- The license under which the package is released.+License: LGPL-3++-- The file containing the license text.+License-file: LICENSE++-- The package author(s).+Author: Dustin DeWeese++-- An email address to which users can send suggestions, bug reports,+-- and patches.+Maintainer: dustin.deweese@gmail.com++-- A copyright notice.+-- Copyright: ++Category: Data++Build-type: Simple++-- Extra files to be distributed with the package, such as examples or+-- a README.+-- Extra-source-files: ++-- Constraint on the version of Cabal needed to build this package.+Cabal-version: >=1.4+++Library+ -- Modules exported by the library.+ Exposed-modules: Data.Adaptive+ + -- Packages needed in order to build this package.+ Build-depends: base >= 4 && < 5+ + -- Modules not exported by this package.+ -- Other-modules: + + -- Extra tools (e.g. alex, hsc2hs, ...) needed to build the source.+ -- Build-tools: +
+ Data/Adaptive.hs view
@@ -0,0 +1,287 @@+{- + This file is part of Adaptive.++ Adaptive is free software: you can redistribute it and/or modify+ it under the terms of the GNU Lesser General Public License as published by+ the Free Software Foundation, either version 3 of the License, or+ (at your option) any later version.++ Adaptive is distributed in the hope that it will be useful,+ but WITHOUT ANY WARRANTY; without even the implied warranty of+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ GNU Lesser General Public License for more details.++ You should have received a copy of the GNU Lesser General Public License+ along with Adaptive. If not, see <http://www.gnu.org/licenses/>.+-}++{-# LANGUAGE FlexibleInstances, BangPatterns, UnboxedTuples #-}+{-# OPTIONS_GHC -fno-excess-precision -fno-spec-constr #-}+-- {-# OPTIONS_GHC -fglasgow-exts -fno-excess-precision -fno-spec-constr #-}+-- use SSE to avoid the excess precision of the 387 FPU+-- {-# OPTIONS_GHC -fvia-C -optc-O -optc-ffast-math -optc-mfpmath=sse -optc-msse #-}+-- {-# OPTIONS_GHC -fllvm -optlc-mattr=+sse4a -optlc--disable-excess-fp-precision #-}++-- | Based on Adaptive Precision Floating-Point Arithmetic and Fast Robust Geometric Predicates, Jonathan Richard Shewchuk, 1997+module Data.Adaptive (Adaptive(..), fromFloatingPoint, approx, approx', approxFast, splitter, epsilon) where++import Data.List+import Data.Bits+import Data.Ratio++type FloatT = Double++--mergeBy :: (a -> a -> Ordering) -> [a] -> [a] -> [a]+mergeBy _ xs [] = xs+mergeBy _ [] ys = ys+mergeBy cmp (x:xs) (y:ys) = case cmp x y of+ GT -> y : mergeBy cmp (x:xs) ys+ _ -> x : mergeBy cmp xs (y:ys)++newtype Adaptive a = Adaptive [a]++instance (Show a, RealFloat a) => Show (Adaptive a) where+ showsPrec n x = showParen (n > 6 && x < 0) $ showsPrec 0 (approx x) . ('~':)++------------------------- approximate instances -------------------------++liftAdaptive1 f = fromFloatingPoint . f . approxFast+liftAdaptive2 f x y = fromFloatingPoint $ approxFast x `f` approxFast y++instance (RealFloat a, Real a) => Real (Adaptive a) where+ toRational = toRational . approxFast++instance (RealFloat a, Floating a) => Floating (Adaptive a) where+ pi = fromFloatingPoint pi+ exp = liftAdaptive1 exp+ sqrt = sqrtA+ log = liftAdaptive1 log+ (**) = liftAdaptive2 (**)+ logBase = liftAdaptive2 logBase+ sin = liftAdaptive1 sin+ tan = liftAdaptive1 tan+ cos = liftAdaptive1 cos+ asin = liftAdaptive1 asin+ atan = liftAdaptive1 atan+ acos = liftAdaptive1 acos+ sinh = liftAdaptive1 sinh+ tanh = liftAdaptive1 tanh+ cosh = liftAdaptive1 cosh+ asinh = liftAdaptive1 asinh+ atanh = liftAdaptive1 atanh+ acosh = liftAdaptive1 acosh++instance (RealFloat a) => RealFloat (Adaptive a) where+ floatRadix = floatRadix . approxFast+ floatDigits = floatDigits . approxFast+ floatRange = floatRange . approxFast+ decodeFloat = decodeFloat . approxFast+ encodeFloat s r = fromFloatingPoint (encodeFloat s r)+ exponent = exponent . approxFast+ significand = liftAdaptive1 significand+ scaleFloat x = liftAdaptive1 (scaleFloat x)+ isNaN = isNaN . approxFast+ isInfinite = isInfinite . approxFast+ isDenormalized = isDenormalized . approxFast+ isNegativeZero = isNegativeZero . approxFast+ isIEEE = isIEEE . approxFast+ atan2 = liftAdaptive2 atan2++instance (RealFloat a, RealFrac a) => RealFrac (Adaptive a) where+ properFraction x = (y, fromFloatingPoint x')+ where (y, x') = properFraction (approxFast x)++-------------------------------------------------------------------------++instance (Num a, RealFloat a) => Eq (Adaptive a) where+ {-# SPECIALIZE instance Eq (Adaptive FloatT) #-}+ a == b | null x = True+ | otherwise = False+ where Adaptive x = a - b++instance (Num a, RealFloat a) => Ord (Adaptive a) where+ {-# SPECIALIZE instance Ord (Adaptive FloatT) #-}+ compare a b | null x = EQ+ | head x > 0 = GT+ | otherwise = LT+ where Adaptive x = a - b++instance (Num a, RealFloat a) => Num (Adaptive a) where+ {-# SPECIALIZE instance Num (Adaptive FloatT) #-}+ Adaptive x + Adaptive y = Adaptive . compress $ fastExpSum x y+ Adaptive x - Adaptive y = Adaptive x + Adaptive (map negate y)+ Adaptive x * Adaptive y = Adaptive $ expProd x y+ negate (Adaptive x) = Adaptive (map negate x)+ abs (Adaptive []) = Adaptive []+ abs (Adaptive x) | head x < 0 = Adaptive (map negate x)+ | otherwise = Adaptive x+ signum (Adaptive []) = Adaptive []+ signum (Adaptive (x:_)) = Adaptive [signum x]+ fromInteger 0 = Adaptive []+ fromInteger x = Adaptive [fromInteger x]++instance (RealFloat a, Fractional a) => Fractional (Adaptive a) where+ {-# SPECIALIZE instance Fractional (Adaptive FloatT) #-}+ fromRational r = fromIntegral (numerator r) / fromIntegral (denominator r)+ a / b = Adaptive $ a `divA` b++{-# SPECIALIZE divA :: Adaptive FloatT -> Adaptive FloatT -> [FloatT] #-}+divA x y | d == 0 = []+ | e == 0 || abs e >= abs x = [d]+ | otherwise = d : divA e y+ where d = approxFast x / approxFast y+ e = x - fromFloatingPoint d * y++-- | Use Babylonian method to calculate corrections to built in sqrt+{-# SPECIALIZE sqrtA :: Adaptive FloatT -> Adaptive FloatT #-}+sqrtA x = r + bab r+ where r = liftAdaptive1 sqrt x+ bab e | c == 0 = 0+ | otherwise = c + bab (e + c)+ where c = (x / e - e) / 2++{-# SPECIALIZE epsilon :: FloatT #-}+epsilon :: (RealFloat a) => a+epsilon = x+ where x = scaleFloat (- floatDigits x) 1++{-# SPECIALIZE splitter :: FloatT #-}+splitter :: (RealFloat a) => a+splitter = s+ where s = scaleFloat ((floatDigits s + 1) `shiftR` 1) 1 + 1++-- {-# INLINE fromFloatingPoint #-}+{-# SPECIALIZE fromFloatingPoint :: FloatT -> Adaptive FloatT #-}+fromFloatingPoint :: (RealFloat a) => a -> Adaptive a+fromFloatingPoint 0 = Adaptive []+fromFloatingPoint x = Adaptive [x]++{-# INLINE approx #-}+-- {-# SPECIALIZE approx :: Adaptive FloatT -> FloatT #-}+approx :: (RealFloat a, Ord a) => Adaptive a -> a+approx (Adaptive []) = 0+approx (Adaptive (x:xs)) = foldr (+) x . takeWhile ((> x*epsilon).abs) $ xs++{-# INLINE approx' #-}+-- {-# SPECIALIZE approx' :: Adaptive FloatT -> FloatT #-}+approx' :: (Real a, RealFloat b) => Adaptive a -> b+approx' (Adaptive []) = 0+approx' (Adaptive (x:xs)) = foldr (+) x' .+ takeWhile ((> x' * epsilon) . abs) .+ map realToFrac $ xs+ where x' = realToFrac x++-- {-# INLINE approxFast #-}+{-# SPECIALIZE approxFast :: Adaptive FloatT -> FloatT #-}+approxFast :: (Num a) => Adaptive a -> a+approxFast (Adaptive []) = 0+approxFast (Adaptive (x:_)) = x++-- |a| >= [b]+{-# INLINE fastTwoSum #-}+--{-# SPECIALIZE fastTwoSum :: FloatT -> FloatT -> (# FloatT, FloatT #) #-}+fastTwoSum :: (Num a) => a -> a -> (# a, a #)+fastTwoSum a b = (# x, y #)+ where x = a + b+ b' = x - a+ y = b - b'++{-# INLINE twoSum #-}+--{-# SPECIALIZE twoSum :: FloatT -> FloatT -> (# FloatT, FloatT #) #-}+twoSum :: (Num a) => a -> a -> (# a, a #)+twoSum !a !b = (# x, y #)+ where !x = a + b+ !b' = x - a+ !a' = x - b'+ !br = b - b'+ !ar = a - a'+ !y = ar + br++{-+{-# INLINE twoSum' #-}+--{-# SPECIALIZE twoSum' :: FloatT -> FloatT -> (FloatT, FloatT) #-}+twoSum' :: (Num a) => a -> a -> (a, a)+twoSum' a b = (x, y)+ where x = a + b+ b' = x - a+ a' = x - b'+ br = b - b'+ ar = a - a'+ y = ar + br++--{-# SPECIALIZE growExp :: FloatT -> [FloatT] -> [FloatT] #-}+growExp :: (Num a) => a -> [a] -> [a]+growExp b es = filter (/= 0) . uncurry (:) . mapAccumR twoSum' b $ es++--{-# SPECIALIZE expSum :: [FloatT] -> [FloatT] -> [FloatT] #-}+expSum :: (Num a) => [a] -> [a] -> [a]+expSum = foldr growExp+-}++{-# SPECIALIZE fastExpSum :: [FloatT] -> [FloatT] -> [FloatT] #-}+fastExpSum :: (RealFloat a) => [a] -> [a] -> [a]+fastExpSum [] x = x+fastExpSum x [] = x+fastExpSum e f = filter (/= 0) (q:hs)+ where g = mergeBy cmp e f+ cmp x y = compare (exponent y) (exponent x)+ (# q, hs #) = mapAccumR1 twoSum g++--mapAccumR1 :: (t -> t -> (# t, a #)) -> [t] -> (# t, [a] #)+mapAccumR1 f [x] = (# x, [] #)+mapAccumR1 f (x:xs) = (# a', y:xs' #)+ where (# a, xs' #) = mapAccumR1 f xs+ (# a', y #) = f a x++--{-# INLINE split #-}+--{-# SPECIALIZE split :: FloatT -> (# FloatT, FloatT #) #-}+--split :: (RealFloat a) => a -> (# a, a #)+split !a = (# ah, al #)+ where !c = splitter * a+ !ab = c - a+ !ah = c - ab+ !al = a - ah++--{-# INLINE twoProd #-}+--{-# SPECIALIZE twoProd :: FloatT -> FloatT -> (# FloatT, FloatT #) #-}+--twoProd :: (RealFloat a) => a -> a -> (# a, a #)+twoProd !a !b = (# x, y #)+ where !x = a * b+ (# !ah, !al #) = split a+ (# !bh, !bl #) = split b+ !err = x - ah * bh - al * bh - ah * bl+ !y = al * bl - err++--{-# INLINE scaleExp #-}+--{-# SPECIALIZE scaleExp :: FloatT -> [FloatT] -> [FloatT] #-}+--scaleExp :: (RealFloat a) => a -> [a] -> [a]+scaleExp _ [] = []+scaleExp b es = filter (/= 0) . (uncurry (:)) . foldr f (q0, [h0]) . init $ es+ where (# q0, h0 #) = twoProd (last es) b+ f e (q, h) = (q'', h2 : h1 : h)+ where (# th, tl #) = twoProd e b+ (# q', h1 #) = twoSum q tl+ (# q'', h2 #) = fastTwoSum th q'++--{-# INLINE expProd #-}+--{-# SPECIALIZE expProd :: [FloatT] -> [FloatT] -> [FloatT] #-}+--expProd :: (RealFloat a) => [a] -> [a] -> [a]+expProd [] _ = []+expProd _ [] = []+expProd x [y] = scaleExp y x+expProd [x] y = scaleExp x y+expProd (x:xs) ys = foldl' f (scaleExp x ys) xs+ where f h e = fastExpSum h . scaleExp e $ ys++--{-# SPECIALIZE compress :: [FloatT] -> [FloatT] #-}+--compress :: (Num a) => [a] -> [a]+compress e = comp up . comp down $ e+ where comp _ [] = []+ comp _ [x] = [x]+ comp f (x:xs) = uncurry (:) . foldl' f (x, []) $ xs+ down (q, h) e | ql /= 0 = (ql, qu:h)+ | otherwise = (qu, h)+ where (# qu, ql #) = fastTwoSum q e+ up (q, h) e | ql /= 0 = (qu, ql:h) -- error on pg. 28, line 14: Q should be q+ | otherwise = (qu, h)+ where (# qu, ql #) = fastTwoSum e q
+ LICENSE view
@@ -0,0 +1,165 @@+ GNU LESSER GENERAL PUBLIC LICENSE+ Version 3, 29 June 2007++ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>+ Everyone is permitted to copy and distribute verbatim copies+ of this license document, but changing it is not allowed.+++ This version of the GNU Lesser General Public License incorporates+the terms and conditions of version 3 of the GNU General Public+License, supplemented by the additional permissions listed below.++ 0. Additional Definitions. ++ As used herein, "this License" refers to version 3 of the GNU Lesser+General Public License, and the "GNU GPL" refers to version 3 of the GNU+General Public License.++ "The Library" refers to a covered work governed by this License,+other than an Application or a Combined Work as defined below.++ An "Application" is any work that makes use of an interface provided+by the Library, but which is not otherwise based on the Library.+Defining a subclass of a class defined by the Library is deemed a mode+of using an interface provided by the Library.++ A "Combined Work" is a work produced by combining or linking an+Application with the Library. The particular version of the Library+with which the Combined Work was made is also called the "Linked+Version".++ The "Minimal Corresponding Source" for a Combined Work means the+Corresponding Source for the Combined Work, excluding any source code+for portions of the Combined Work that, considered in isolation, are+based on the Application, and not on the Linked Version.++ The "Corresponding Application Code" for a Combined Work means the+object code and/or source code for the Application, including any data+and utility programs needed for reproducing the Combined Work from the+Application, but excluding the System Libraries of the Combined Work.++ 1. Exception to Section 3 of the GNU GPL.++ You may convey a covered work under sections 3 and 4 of this License+without being bound by section 3 of the GNU GPL.++ 2. Conveying Modified Versions.++ If you modify a copy of the Library, and, in your modifications, a+facility refers to a function or data to be supplied by an Application+that uses the facility (other than as an argument passed when the+facility is invoked), then you may convey a copy of the modified+version:++ a) under this License, provided that you make a good faith effort to+ ensure that, in the event an Application does not supply the+ function or data, the facility still operates, and performs+ whatever part of its purpose remains meaningful, or++ b) under the GNU GPL, with none of the additional permissions of+ this License applicable to that copy.++ 3. Object Code Incorporating Material from Library Header Files.++ The object code form of an Application may incorporate material from+a header file that is part of the Library. You may convey such object+code under terms of your choice, provided that, if the incorporated+material is not limited to numerical parameters, data structure+layouts and accessors, or small macros, inline functions and templates+(ten or fewer lines in length), you do both of the following:++ a) Give prominent notice with each copy of the object code that the+ Library is used in it and that the Library and its use are+ covered by this License.++ b) Accompany the object code with a copy of the GNU GPL and this license+ document.++ 4. Combined Works.++ You may convey a Combined Work under terms of your choice that,+taken together, effectively do not restrict modification of the+portions of the Library contained in the Combined Work and reverse+engineering for debugging such modifications, if you also do each of+the following:++ a) Give prominent notice with each copy of the Combined Work that+ the Library is used in it and that the Library and its use are+ covered by this License.++ b) Accompany the Combined Work with a copy of the GNU GPL and this license+ document.++ c) For a Combined Work that displays copyright notices during+ execution, include the copyright notice for the Library among+ these notices, as well as a reference directing the user to the+ copies of the GNU GPL and this license document.++ d) Do one of the following:++ 0) Convey the Minimal Corresponding Source under the terms of this+ License, and the Corresponding Application Code in a form+ suitable for, and under terms that permit, the user to+ recombine or relink the Application with a modified version of+ the Linked Version to produce a modified Combined Work, in the+ manner specified by section 6 of the GNU GPL for conveying+ Corresponding Source.++ 1) Use a suitable shared library mechanism for linking with the+ Library. A suitable mechanism is one that (a) uses at run time+ a copy of the Library already present on the user's computer+ system, and (b) will operate properly with a modified version+ of the Library that is interface-compatible with the Linked+ Version. ++ e) Provide Installation Information, but only if you would otherwise+ be required to provide such information under section 6 of the+ GNU GPL, and only to the extent that such information is+ necessary to install and execute a modified version of the+ Combined Work produced by recombining or relinking the+ Application with a modified version of the Linked Version. (If+ you use option 4d0, the Installation Information must accompany+ the Minimal Corresponding Source and Corresponding Application+ Code. If you use option 4d1, you must provide the Installation+ Information in the manner specified by section 6 of the GNU GPL+ for conveying Corresponding Source.)++ 5. Combined Libraries.++ You may place library facilities that are a work based on the+Library side by side in a single library together with other library+facilities that are not Applications and are not covered by this+License, and convey such a combined library under terms of your+choice, if you do both of the following:++ a) Accompany the combined library with a copy of the same work based+ on the Library, uncombined with any other library facilities,+ conveyed under the terms of this License.++ b) Give prominent notice with the combined library that part of it+ is a work based on the Library, and explaining where to find the+ accompanying uncombined form of the same work.++ 6. Revised Versions of the GNU Lesser General Public License.++ The Free Software Foundation may publish revised and/or new versions+of the GNU Lesser General Public License from time to time. Such new+versions will be similar in spirit to the present version, but may+differ in detail to address new problems or concerns.++ Each version is given a distinguishing version number. If the+Library as you received it specifies that a certain numbered version+of the GNU Lesser General Public License "or any later version"+applies to it, you have the option of following the terms and+conditions either of that published version or of any later version+published by the Free Software Foundation. If the Library as you+received it does not specify a version number of the GNU Lesser+General Public License, you may choose any version of the GNU Lesser+General Public License ever published by the Free Software Foundation.++ If the Library as you received it specifies that a proxy can decide+whether future versions of the GNU Lesser General Public License shall+apply, that proxy's public statement of acceptance of any version is+permanent authorization for you to choose that version for the+Library.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain