{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE TypeFamilies #-}

module Data.EPS where

import qualified Data.Vector.Generic as G
import qualified Data.Vector.Generic.Mutable as GM
import qualified Data.Vector.Unboxed as U

eps :: (Fractional a) => a
eps :: forall a. Fractional a => a
eps = a
1e-8
{-# INLINE eps #-}

absErr :: Double -> Double -> Double
absErr :: Double -> Double -> Double
absErr Double
ans Double
x = Double -> Double
forall a. Num a => a -> a
abs (Double
x Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
ans)

relErr :: Double -> Double -> Double
relErr :: Double -> Double -> Double
relErr Double
ans Double
x = Double -> Double
forall a. Num a => a -> a
abs (Double -> Double) -> Double -> Double
forall a b. (a -> b) -> a -> b
$ (Double
x Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
ans) Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
ans

newtype EPS a = EPS {forall a. EPS a -> a
getEPS :: a}
  deriving newtype (Int -> EPS a -> ShowS
[EPS a] -> ShowS
EPS a -> String
(Int -> EPS a -> ShowS)
-> (EPS a -> String) -> ([EPS a] -> ShowS) -> Show (EPS a)
forall a. Show a => Int -> EPS a -> ShowS
forall a. Show a => [EPS a] -> ShowS
forall a. Show a => EPS a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall a. Show a => Int -> EPS a -> ShowS
showsPrec :: Int -> EPS a -> ShowS
$cshow :: forall a. Show a => EPS a -> String
show :: EPS a -> String
$cshowList :: forall a. Show a => [EPS a] -> ShowS
showList :: [EPS a] -> ShowS
Show, ReadPrec [EPS a]
ReadPrec (EPS a)
Int -> ReadS (EPS a)
ReadS [EPS a]
(Int -> ReadS (EPS a))
-> ReadS [EPS a]
-> ReadPrec (EPS a)
-> ReadPrec [EPS a]
-> Read (EPS a)
forall a. Read a => ReadPrec [EPS a]
forall a. Read a => ReadPrec (EPS a)
forall a. Read a => Int -> ReadS (EPS a)
forall a. Read a => ReadS [EPS a]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: forall a. Read a => Int -> ReadS (EPS a)
readsPrec :: Int -> ReadS (EPS a)
$creadList :: forall a. Read a => ReadS [EPS a]
readList :: ReadS [EPS a]
$creadPrec :: forall a. Read a => ReadPrec (EPS a)
readPrec :: ReadPrec (EPS a)
$creadListPrec :: forall a. Read a => ReadPrec [EPS a]
readListPrec :: ReadPrec [EPS a]
Read, Integer -> EPS a
EPS a -> EPS a
EPS a -> EPS a -> EPS a
(EPS a -> EPS a -> EPS a)
-> (EPS a -> EPS a -> EPS a)
-> (EPS a -> EPS a -> EPS a)
-> (EPS a -> EPS a)
-> (EPS a -> EPS a)
-> (EPS a -> EPS a)
-> (Integer -> EPS a)
-> Num (EPS a)
forall a. Num a => Integer -> EPS a
forall a. Num a => EPS a -> EPS a
forall a. Num a => EPS a -> EPS a -> EPS a
forall a.
(a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (Integer -> a)
-> Num a
$c+ :: forall a. Num a => EPS a -> EPS a -> EPS a
+ :: EPS a -> EPS a -> EPS a
$c- :: forall a. Num a => EPS a -> EPS a -> EPS a
- :: EPS a -> EPS a -> EPS a
$c* :: forall a. Num a => EPS a -> EPS a -> EPS a
* :: EPS a -> EPS a -> EPS a
$cnegate :: forall a. Num a => EPS a -> EPS a
negate :: EPS a -> EPS a
$cabs :: forall a. Num a => EPS a -> EPS a
abs :: EPS a -> EPS a
$csignum :: forall a. Num a => EPS a -> EPS a
signum :: EPS a -> EPS a
$cfromInteger :: forall a. Num a => Integer -> EPS a
fromInteger :: Integer -> EPS a
Num, Num (EPS a)
Num (EPS a) =>
(EPS a -> EPS a -> EPS a)
-> (EPS a -> EPS a) -> (Rational -> EPS a) -> Fractional (EPS a)
Rational -> EPS a
EPS a -> EPS a
EPS a -> EPS a -> EPS a
forall a. Fractional a => Num (EPS a)
forall a. Fractional a => Rational -> EPS a
forall a. Fractional a => EPS a -> EPS a
forall a. Fractional a => EPS a -> EPS a -> EPS a
forall a.
Num a =>
(a -> a -> a) -> (a -> a) -> (Rational -> a) -> Fractional a
$c/ :: forall a. Fractional a => EPS a -> EPS a -> EPS a
/ :: EPS a -> EPS a -> EPS a
$crecip :: forall a. Fractional a => EPS a -> EPS a
recip :: EPS a -> EPS a
$cfromRational :: forall a. Fractional a => Rational -> EPS a
fromRational :: Rational -> EPS a
Fractional, Fractional (EPS a)
EPS a
Fractional (EPS a) =>
EPS a
-> (EPS a -> EPS a)
-> (EPS a -> EPS a)
-> (EPS a -> EPS a)
-> (EPS a -> EPS a -> EPS a)
-> (EPS a -> EPS a -> EPS a)
-> (EPS a -> EPS a)
-> (EPS a -> EPS a)
-> (EPS a -> EPS a)
-> (EPS a -> EPS a)
-> (EPS a -> EPS a)
-> (EPS a -> EPS a)
-> (EPS a -> EPS a)
-> (EPS a -> EPS a)
-> (EPS a -> EPS a)
-> (EPS a -> EPS a)
-> (EPS a -> EPS a)
-> (EPS a -> EPS a)
-> (EPS a -> EPS a)
-> (EPS a -> EPS a)
-> (EPS a -> EPS a)
-> (EPS a -> EPS a)
-> Floating (EPS a)
EPS a -> EPS a
EPS a -> EPS a -> EPS a
forall a. Floating a => Fractional (EPS a)
forall a. Floating a => EPS a
forall a. Floating a => EPS a -> EPS a
forall a. Floating a => EPS a -> EPS a -> EPS a
forall a.
Fractional a =>
a
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> Floating a
$cpi :: forall a. Floating a => EPS a
pi :: EPS a
$cexp :: forall a. Floating a => EPS a -> EPS a
exp :: EPS a -> EPS a
$clog :: forall a. Floating a => EPS a -> EPS a
log :: EPS a -> EPS a
$csqrt :: forall a. Floating a => EPS a -> EPS a
sqrt :: EPS a -> EPS a
$c** :: forall a. Floating a => EPS a -> EPS a -> EPS a
** :: EPS a -> EPS a -> EPS a
$clogBase :: forall a. Floating a => EPS a -> EPS a -> EPS a
logBase :: EPS a -> EPS a -> EPS a
$csin :: forall a. Floating a => EPS a -> EPS a
sin :: EPS a -> EPS a
$ccos :: forall a. Floating a => EPS a -> EPS a
cos :: EPS a -> EPS a
$ctan :: forall a. Floating a => EPS a -> EPS a
tan :: EPS a -> EPS a
$casin :: forall a. Floating a => EPS a -> EPS a
asin :: EPS a -> EPS a
$cacos :: forall a. Floating a => EPS a -> EPS a
acos :: EPS a -> EPS a
$catan :: forall a. Floating a => EPS a -> EPS a
atan :: EPS a -> EPS a
$csinh :: forall a. Floating a => EPS a -> EPS a
sinh :: EPS a -> EPS a
$ccosh :: forall a. Floating a => EPS a -> EPS a
cosh :: EPS a -> EPS a
$ctanh :: forall a. Floating a => EPS a -> EPS a
tanh :: EPS a -> EPS a
$casinh :: forall a. Floating a => EPS a -> EPS a
asinh :: EPS a -> EPS a
$cacosh :: forall a. Floating a => EPS a -> EPS a
acosh :: EPS a -> EPS a
$catanh :: forall a. Floating a => EPS a -> EPS a
atanh :: EPS a -> EPS a
$clog1p :: forall a. Floating a => EPS a -> EPS a
log1p :: EPS a -> EPS a
$cexpm1 :: forall a. Floating a => EPS a -> EPS a
expm1 :: EPS a -> EPS a
$clog1pexp :: forall a. Floating a => EPS a -> EPS a
log1pexp :: EPS a -> EPS a
$clog1mexp :: forall a. Floating a => EPS a -> EPS a
log1mexp :: EPS a -> EPS a
Floating)

instance (Num a, Ord a, Fractional a) => Eq (EPS a) where
  {-# SPECIALIZE instance Eq (EPS Double) #-}
  (EPS a
x) == :: EPS a -> EPS a -> Bool
== (EPS a
y) = a -> a
forall a. Num a => a -> a
abs (a
y a -> a -> a
forall a. Num a => a -> a -> a
- a
x) a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
forall a. Fractional a => a
eps

instance (Num a, Ord a, Fractional a) => Ord (EPS a) where
  {-# SPECIALIZE instance Ord (EPS Double) #-}
  compare :: EPS a -> EPS a -> Ordering
compare (EPS a
x) (EPS a
y)
    | a -> a
forall a. Num a => a -> a
abs (a
x a -> a -> a
forall a. Num a => a -> a -> a
- a
y) a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
forall a. Fractional a => a
eps = Ordering
EQ
    | Bool
otherwise = a -> a -> Ordering
forall a. Ord a => a -> a -> Ordering
compare a
x a
y
  (EPS a
x) < :: EPS a -> EPS a -> Bool
< (EPS a
y) = a
x a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
y a -> a -> a
forall a. Num a => a -> a -> a
- a
forall a. Fractional a => a
eps
  (EPS a
x) <= :: EPS a -> EPS a -> Bool
<= (EPS a
y) = a
x a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
y a -> a -> a
forall a. Num a => a -> a -> a
+ a
forall a. Fractional a => a
eps
  (EPS a
x) > :: EPS a -> EPS a -> Bool
> (EPS a
y) = a
x a -> a -> Bool
forall a. Ord a => a -> a -> Bool
> a
y a -> a -> a
forall a. Num a => a -> a -> a
+ a
forall a. Fractional a => a
eps
  (EPS a
x) >= :: EPS a -> EPS a -> Bool
>= (EPS a
y) = a
x a -> a -> Bool
forall a. Ord a => a -> a -> Bool
> a
y a -> a -> a
forall a. Num a => a -> a -> a
- a
forall a. Fractional a => a
eps

newtype instance U.MVector s (EPS a) = MV_EPS (U.MVector s a)
newtype instance U.Vector (EPS a) = V_EPS (U.Vector a)
deriving newtype instance (U.Unbox a) => GM.MVector U.MVector (EPS a)
deriving newtype instance (U.Unbox a) => G.Vector U.Vector (EPS a)
instance (U.Unbox a) => U.Unbox (EPS a)