{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# OPTIONS_HADDOCK not-home #-}

-----------------------------------------------------------------------------
-- |
-- Copyright   :  (c) Edward Kmett 2010-2021
-- License     :  BSD3
-- Maintainer  :  ekmett@gmail.com
-- Stability   :  experimental
-- Portability :  GHC only
--
-----------------------------------------------------------------------------

module Numeric.AD.Internal.On
  ( On(..)
  ) where

import Data.Number.Erf
import Data.Data
import Numeric.AD.Mode

------------------------------------------------------------------------------
-- On
------------------------------------------------------------------------------

-- | The composition of two AD modes is an AD mode in its own right
newtype On t = On { On t -> t
off :: t } deriving
  ( On t -> On t -> Bool
(On t -> On t -> Bool) -> (On t -> On t -> Bool) -> Eq (On t)
forall t. Eq t => On t -> On t -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: On t -> On t -> Bool
$c/= :: forall t. Eq t => On t -> On t -> Bool
== :: On t -> On t -> Bool
$c== :: forall t. Eq t => On t -> On t -> Bool
Eq, Int -> On t
On t -> Int
On t -> [On t]
On t -> On t
On t -> On t -> [On t]
On t -> On t -> On t -> [On t]
(On t -> On t)
-> (On t -> On t)
-> (Int -> On t)
-> (On t -> Int)
-> (On t -> [On t])
-> (On t -> On t -> [On t])
-> (On t -> On t -> [On t])
-> (On t -> On t -> On t -> [On t])
-> Enum (On t)
forall t. Enum t => Int -> On t
forall t. Enum t => On t -> Int
forall t. Enum t => On t -> [On t]
forall t. Enum t => On t -> On t
forall t. Enum t => On t -> On t -> [On t]
forall t. Enum t => On t -> On t -> On t -> [On t]
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: On t -> On t -> On t -> [On t]
$cenumFromThenTo :: forall t. Enum t => On t -> On t -> On t -> [On t]
enumFromTo :: On t -> On t -> [On t]
$cenumFromTo :: forall t. Enum t => On t -> On t -> [On t]
enumFromThen :: On t -> On t -> [On t]
$cenumFromThen :: forall t. Enum t => On t -> On t -> [On t]
enumFrom :: On t -> [On t]
$cenumFrom :: forall t. Enum t => On t -> [On t]
fromEnum :: On t -> Int
$cfromEnum :: forall t. Enum t => On t -> Int
toEnum :: Int -> On t
$ctoEnum :: forall t. Enum t => Int -> On t
pred :: On t -> On t
$cpred :: forall t. Enum t => On t -> On t
succ :: On t -> On t
$csucc :: forall t. Enum t => On t -> On t
Enum, Eq (On t)
Eq (On t)
-> (On t -> On t -> Ordering)
-> (On t -> On t -> Bool)
-> (On t -> On t -> Bool)
-> (On t -> On t -> Bool)
-> (On t -> On t -> Bool)
-> (On t -> On t -> On t)
-> (On t -> On t -> On t)
-> Ord (On t)
On t -> On t -> Bool
On t -> On t -> Ordering
On t -> On t -> On t
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall t. Ord t => Eq (On t)
forall t. Ord t => On t -> On t -> Bool
forall t. Ord t => On t -> On t -> Ordering
forall t. Ord t => On t -> On t -> On t
min :: On t -> On t -> On t
$cmin :: forall t. Ord t => On t -> On t -> On t
max :: On t -> On t -> On t
$cmax :: forall t. Ord t => On t -> On t -> On t
>= :: On t -> On t -> Bool
$c>= :: forall t. Ord t => On t -> On t -> Bool
> :: On t -> On t -> Bool
$c> :: forall t. Ord t => On t -> On t -> Bool
<= :: On t -> On t -> Bool
$c<= :: forall t. Ord t => On t -> On t -> Bool
< :: On t -> On t -> Bool
$c< :: forall t. Ord t => On t -> On t -> Bool
compare :: On t -> On t -> Ordering
$ccompare :: forall t. Ord t => On t -> On t -> Ordering
$cp1Ord :: forall t. Ord t => Eq (On t)
Ord, On t
On t -> On t -> Bounded (On t)
forall a. a -> a -> Bounded a
forall t. Bounded t => On t
maxBound :: On t
$cmaxBound :: forall t. Bounded t => On t
minBound :: On t
$cminBound :: forall t. Bounded t => On t
Bounded
  , Integer -> On t
On t -> On t
On t -> On t -> On t
(On t -> On t -> On t)
-> (On t -> On t -> On t)
-> (On t -> On t -> On t)
-> (On t -> On t)
-> (On t -> On t)
-> (On t -> On t)
-> (Integer -> On t)
-> Num (On t)
forall t. Num t => Integer -> On t
forall t. Num t => On t -> On t
forall t. Num t => On t -> On t -> On t
forall a.
(a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (Integer -> a)
-> Num a
fromInteger :: Integer -> On t
$cfromInteger :: forall t. Num t => Integer -> On t
signum :: On t -> On t
$csignum :: forall t. Num t => On t -> On t
abs :: On t -> On t
$cabs :: forall t. Num t => On t -> On t
negate :: On t -> On t
$cnegate :: forall t. Num t => On t -> On t
* :: On t -> On t -> On t
$c* :: forall t. Num t => On t -> On t -> On t
- :: On t -> On t -> On t
$c- :: forall t. Num t => On t -> On t -> On t
+ :: On t -> On t -> On t
$c+ :: forall t. Num t => On t -> On t -> On t
Num, Num (On t)
Ord (On t)
Num (On t) -> Ord (On t) -> (On t -> Rational) -> Real (On t)
On t -> Rational
forall a. Num a -> Ord a -> (a -> Rational) -> Real a
forall t. Real t => Num (On t)
forall t. Real t => Ord (On t)
forall t. Real t => On t -> Rational
toRational :: On t -> Rational
$ctoRational :: forall t. Real t => On t -> Rational
$cp2Real :: forall t. Real t => Ord (On t)
$cp1Real :: forall t. Real t => Num (On t)
Real, Num (On t)
Num (On t)
-> (On t -> On t -> On t)
-> (On t -> On t)
-> (Rational -> On t)
-> Fractional (On t)
Rational -> On t
On t -> On t
On t -> On t -> On t
forall t. Fractional t => Num (On t)
forall t. Fractional t => Rational -> On t
forall t. Fractional t => On t -> On t
forall t. Fractional t => On t -> On t -> On t
forall a.
Num a
-> (a -> a -> a) -> (a -> a) -> (Rational -> a) -> Fractional a
fromRational :: Rational -> On t
$cfromRational :: forall t. Fractional t => Rational -> On t
recip :: On t -> On t
$crecip :: forall t. Fractional t => On t -> On t
/ :: On t -> On t -> On t
$c/ :: forall t. Fractional t => On t -> On t -> On t
$cp1Fractional :: forall t. Fractional t => Num (On t)
Fractional
  , Fractional (On t)
Real (On t)
Real (On t)
-> Fractional (On t)
-> (forall b. Integral b => On t -> (b, On t))
-> (forall b. Integral b => On t -> b)
-> (forall b. Integral b => On t -> b)
-> (forall b. Integral b => On t -> b)
-> (forall b. Integral b => On t -> b)
-> RealFrac (On t)
On t -> b
On t -> b
On t -> b
On t -> b
On t -> (b, On t)
forall b. Integral b => On t -> b
forall b. Integral b => On t -> (b, On t)
forall a.
Real a
-> Fractional a
-> (forall b. Integral b => a -> (b, a))
-> (forall b. Integral b => a -> b)
-> (forall b. Integral b => a -> b)
-> (forall b. Integral b => a -> b)
-> (forall b. Integral b => a -> b)
-> RealFrac a
forall t. RealFrac t => Fractional (On t)
forall t. RealFrac t => Real (On t)
forall t b. (RealFrac t, Integral b) => On t -> b
forall t b. (RealFrac t, Integral b) => On t -> (b, On t)
floor :: On t -> b
$cfloor :: forall t b. (RealFrac t, Integral b) => On t -> b
ceiling :: On t -> b
$cceiling :: forall t b. (RealFrac t, Integral b) => On t -> b
round :: On t -> b
$cround :: forall t b. (RealFrac t, Integral b) => On t -> b
truncate :: On t -> b
$ctruncate :: forall t b. (RealFrac t, Integral b) => On t -> b
properFraction :: On t -> (b, On t)
$cproperFraction :: forall t b. (RealFrac t, Integral b) => On t -> (b, On t)
$cp2RealFrac :: forall t. RealFrac t => Fractional (On t)
$cp1RealFrac :: forall t. RealFrac t => Real (On t)
RealFrac, Fractional (On t)
On t
Fractional (On t)
-> On t
-> (On t -> On t)
-> (On t -> On t)
-> (On t -> On t)
-> (On t -> On t -> On t)
-> (On t -> On t -> On t)
-> (On t -> On t)
-> (On t -> On t)
-> (On t -> On t)
-> (On t -> On t)
-> (On t -> On t)
-> (On t -> On t)
-> (On t -> On t)
-> (On t -> On t)
-> (On t -> On t)
-> (On t -> On t)
-> (On t -> On t)
-> (On t -> On t)
-> (On t -> On t)
-> (On t -> On t)
-> (On t -> On t)
-> (On t -> On t)
-> Floating (On t)
On t -> On t
On t -> On t -> On t
forall t. Floating t => Fractional (On t)
forall t. Floating t => On t
forall t. Floating t => On t -> On t
forall t. Floating t => On t -> On t -> On t
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
log1mexp :: On t -> On t
$clog1mexp :: forall t. Floating t => On t -> On t
log1pexp :: On t -> On t
$clog1pexp :: forall t. Floating t => On t -> On t
expm1 :: On t -> On t
$cexpm1 :: forall t. Floating t => On t -> On t
log1p :: On t -> On t
$clog1p :: forall t. Floating t => On t -> On t
atanh :: On t -> On t
$catanh :: forall t. Floating t => On t -> On t
acosh :: On t -> On t
$cacosh :: forall t. Floating t => On t -> On t
asinh :: On t -> On t
$casinh :: forall t. Floating t => On t -> On t
tanh :: On t -> On t
$ctanh :: forall t. Floating t => On t -> On t
cosh :: On t -> On t
$ccosh :: forall t. Floating t => On t -> On t
sinh :: On t -> On t
$csinh :: forall t. Floating t => On t -> On t
atan :: On t -> On t
$catan :: forall t. Floating t => On t -> On t
acos :: On t -> On t
$cacos :: forall t. Floating t => On t -> On t
asin :: On t -> On t
$casin :: forall t. Floating t => On t -> On t
tan :: On t -> On t
$ctan :: forall t. Floating t => On t -> On t
cos :: On t -> On t
$ccos :: forall t. Floating t => On t -> On t
sin :: On t -> On t
$csin :: forall t. Floating t => On t -> On t
logBase :: On t -> On t -> On t
$clogBase :: forall t. Floating t => On t -> On t -> On t
** :: On t -> On t -> On t
$c** :: forall t. Floating t => On t -> On t -> On t
sqrt :: On t -> On t
$csqrt :: forall t. Floating t => On t -> On t
log :: On t -> On t
$clog :: forall t. Floating t => On t -> On t
exp :: On t -> On t
$cexp :: forall t. Floating t => On t -> On t
pi :: On t
$cpi :: forall t. Floating t => On t
$cp1Floating :: forall t. Floating t => Fractional (On t)
Floating, Floating (On t)
Floating (On t)
-> (On t -> On t)
-> (On t -> On t)
-> (On t -> On t)
-> (On t -> On t)
-> Erf (On t)
On t -> On t
forall a.
Floating a -> (a -> a) -> (a -> a) -> (a -> a) -> (a -> a) -> Erf a
forall t. Erf t => Floating (On t)
forall t. Erf t => On t -> On t
normcdf :: On t -> On t
$cnormcdf :: forall t. Erf t => On t -> On t
erfcx :: On t -> On t
$cerfcx :: forall t. Erf t => On t -> On t
erfc :: On t -> On t
$cerfc :: forall t. Erf t => On t -> On t
erf :: On t -> On t
$cerf :: forall t. Erf t => On t -> On t
$cp1Erf :: forall t. Erf t => Floating (On t)
Erf
  , Floating (On t)
Floating (On t)
-> (On t -> On t)
-> (On t -> On t)
-> (On t -> On t)
-> InvErf (On t)
On t -> On t
forall a.
Floating a -> (a -> a) -> (a -> a) -> (a -> a) -> InvErf a
forall t. InvErf t => Floating (On t)
forall t. InvErf t => On t -> On t
invnormcdf :: On t -> On t
$cinvnormcdf :: forall t. InvErf t => On t -> On t
inverfc :: On t -> On t
$cinverfc :: forall t. InvErf t => On t -> On t
inverf :: On t -> On t
$cinverf :: forall t. InvErf t => On t -> On t
$cp1InvErf :: forall t. InvErf t => Floating (On t)
InvErf, Floating (On t)
RealFrac (On t)
RealFrac (On t)
-> Floating (On t)
-> (On t -> Integer)
-> (On t -> Int)
-> (On t -> (Int, Int))
-> (On t -> (Integer, Int))
-> (Integer -> Int -> On t)
-> (On t -> Int)
-> (On t -> On t)
-> (Int -> On t -> On t)
-> (On t -> Bool)
-> (On t -> Bool)
-> (On t -> Bool)
-> (On t -> Bool)
-> (On t -> Bool)
-> (On t -> On t -> On t)
-> RealFloat (On t)
Int -> On t -> On t
Integer -> Int -> On t
On t -> Bool
On t -> Int
On t -> Integer
On t -> (Int, Int)
On t -> (Integer, Int)
On t -> On t
On t -> On t -> On t
forall t. RealFloat t => Floating (On t)
forall t. RealFloat t => RealFrac (On t)
forall t. RealFloat t => Int -> On t -> On t
forall t. RealFloat t => Integer -> Int -> On t
forall t. RealFloat t => On t -> Bool
forall t. RealFloat t => On t -> Int
forall t. RealFloat t => On t -> Integer
forall t. RealFloat t => On t -> (Int, Int)
forall t. RealFloat t => On t -> (Integer, Int)
forall t. RealFloat t => On t -> On t
forall t. RealFloat t => On t -> On t -> On t
forall a.
RealFrac a
-> Floating a
-> (a -> Integer)
-> (a -> Int)
-> (a -> (Int, Int))
-> (a -> (Integer, Int))
-> (Integer -> Int -> a)
-> (a -> Int)
-> (a -> a)
-> (Int -> a -> a)
-> (a -> Bool)
-> (a -> Bool)
-> (a -> Bool)
-> (a -> Bool)
-> (a -> Bool)
-> (a -> a -> a)
-> RealFloat a
atan2 :: On t -> On t -> On t
$catan2 :: forall t. RealFloat t => On t -> On t -> On t
isIEEE :: On t -> Bool
$cisIEEE :: forall t. RealFloat t => On t -> Bool
isNegativeZero :: On t -> Bool
$cisNegativeZero :: forall t. RealFloat t => On t -> Bool
isDenormalized :: On t -> Bool
$cisDenormalized :: forall t. RealFloat t => On t -> Bool
isInfinite :: On t -> Bool
$cisInfinite :: forall t. RealFloat t => On t -> Bool
isNaN :: On t -> Bool
$cisNaN :: forall t. RealFloat t => On t -> Bool
scaleFloat :: Int -> On t -> On t
$cscaleFloat :: forall t. RealFloat t => Int -> On t -> On t
significand :: On t -> On t
$csignificand :: forall t. RealFloat t => On t -> On t
exponent :: On t -> Int
$cexponent :: forall t. RealFloat t => On t -> Int
encodeFloat :: Integer -> Int -> On t
$cencodeFloat :: forall t. RealFloat t => Integer -> Int -> On t
decodeFloat :: On t -> (Integer, Int)
$cdecodeFloat :: forall t. RealFloat t => On t -> (Integer, Int)
floatRange :: On t -> (Int, Int)
$cfloatRange :: forall t. RealFloat t => On t -> (Int, Int)
floatDigits :: On t -> Int
$cfloatDigits :: forall t. RealFloat t => On t -> Int
floatRadix :: On t -> Integer
$cfloatRadix :: forall t. RealFloat t => On t -> Integer
$cp2RealFloat :: forall t. RealFloat t => Floating (On t)
$cp1RealFloat :: forall t. RealFloat t => RealFrac (On t)
RealFloat, Typeable
  )

instance (Mode t, Mode (Scalar t)) => Mode (On t) where
  type Scalar (On t) = Scalar (Scalar t)
  auto :: Scalar (On t) -> On t
auto = t -> On t
forall t. t -> On t
On (t -> On t)
-> (Scalar (Scalar t) -> t) -> Scalar (Scalar t) -> On t
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Scalar t -> t
forall t. Mode t => Scalar t -> t
auto (Scalar t -> t)
-> (Scalar (Scalar t) -> Scalar t) -> Scalar (Scalar t) -> t
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Scalar (Scalar t) -> Scalar t
forall t. Mode t => Scalar t -> t
auto
  isKnownZero :: On t -> Bool
isKnownZero (On t
n) = t -> Bool
forall t. Mode t => t -> Bool
isKnownZero t
n
  asKnownConstant :: On t -> Maybe (Scalar (On t))
asKnownConstant (On t
n) = t -> Maybe (Scalar t)
forall t. Mode t => t -> Maybe (Scalar t)
asKnownConstant t
n Maybe (Scalar t)
-> (Scalar t -> Maybe (Scalar (Scalar t)))
-> Maybe (Scalar (Scalar t))
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Scalar t -> Maybe (Scalar (Scalar t))
forall t. Mode t => t -> Maybe (Scalar t)
asKnownConstant
  isKnownConstant :: On t -> Bool
isKnownConstant (On t
n) = Bool -> (Scalar t -> Bool) -> Maybe (Scalar t) -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
False Scalar t -> Bool
forall t. Mode t => t -> Bool
isKnownConstant (t -> Maybe (Scalar t)
forall t. Mode t => t -> Maybe (Scalar t)
asKnownConstant t
n)
  Scalar (On t)
a *^ :: Scalar (On t) -> On t -> On t
*^ On t
b = t -> On t
forall t. t -> On t
On (Scalar (Scalar t) -> Scalar t
forall t. Mode t => Scalar t -> t
auto Scalar (Scalar t)
Scalar (On t)
a Scalar t -> t -> t
forall t. Mode t => Scalar t -> t -> t
*^ t
b)
  On t
a ^* :: On t -> Scalar (On t) -> On t
^* Scalar (On t)
b = t -> On t
forall t. t -> On t
On (t
a t -> Scalar t -> t
forall t. Mode t => t -> Scalar t -> t
^* Scalar (Scalar t) -> Scalar t
forall t. Mode t => Scalar t -> t
auto Scalar (Scalar t)
Scalar (On t)
b)