{-# OPTIONS_GHC -Wno-orphans #-}
module AERN2.Real.Tests
(
specCReal, tCReal
)
where
import MixedTypesNumPrelude
import qualified Numeric.CollectErrors as CN
import Test.Hspec
import Test.QuickCheck
import AERN2.MP.Accuracy
import AERN2.MP
import AERN2.MP.Dyadic
import AERN2.Limit
import AERN2.Select
import AERN2.Real.Type
import AERN2.Real.Field ()
import AERN2.Real.Elementary ()
import AERN2.Real.Limit ()
import AERN2.MP.Ball (CentreRadius(..))
instance Arbitrary CReal where
arbitrary :: Gen CReal
arbitrary =
[(Int, Gen CReal)] -> Gen CReal
forall a. HasCallStack => [(Int, Gen a)] -> Gen a
frequency
[(Integer -> Int
forall t. CanBeInt t => t -> Int
int Integer
1, Integer -> CReal
forall t. CanBeCReal t => t -> CReal
creal (Integer -> CReal) -> Gen Integer -> Gen CReal
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Integer -> Gen Integer
forall a.
(Arbitrary a, HasOrderCertainly a Integer) =>
Integer -> Gen a
arbitrarySmall Integer
1000000 :: Gen Integer)),
(Integer -> Int
forall t. CanBeInt t => t -> Int
int Integer
1, Rational -> CReal
forall t. CanBeCReal t => t -> CReal
creal (Rational -> CReal) -> Gen Rational -> Gen CReal
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Integer -> Gen Rational
forall a.
(Arbitrary a, HasOrderCertainly a Integer) =>
Integer -> Gen a
arbitrarySmall Integer
1000000 :: Gen Rational)),
(Integer -> Int
forall t. CanBeInt t => t -> Int
int Integer
2, Integer -> CReal -> MulType Integer CReal
Integer -> CReal -> CReal
forall t1 t2. CanMulAsymmetric t1 t2 => t1 -> t2 -> MulType t1 t2
(*) (Integer -> CReal -> CReal) -> Gen Integer -> Gen (CReal -> CReal)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Integer -> Gen Integer
forall a.
(Arbitrary a, HasOrderCertainly a Integer) =>
Integer -> Gen a
arbitrarySmall Integer
1000000 :: Gen Integer) Gen (CReal -> CReal) -> Gen CReal -> Gen CReal
forall a b. Gen (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen CReal
arbitrarySignedBinary)
]
where
arbitrarySignedBinary :: Gen CReal
arbitrarySignedBinary =
[Integer] -> CReal
forall {a}.
(EqCompareType a Integer ~ Bool, HasEqAsymmetric a Integer) =>
[a] -> CReal
signedBinary2Real ([Integer] -> CReal) -> Gen [Integer] -> Gen CReal
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen Integer -> Gen [Integer]
forall a. Gen a -> Gen [a]
infiniteListOf ([Integer] -> Gen Integer
forall a. HasCallStack => [a] -> Gen a
elements [-Integer
1,Integer
0,Integer
1])
signedBinary2Real :: [a] -> CReal
signedBinary2Real [a]
sbits =
(Precision -> CN MPBall) -> CReal
crealFromPrecFunction ((Precision -> CN MPBall) -> CReal)
-> (Precision -> CN MPBall) -> CReal
forall a b. (a -> b) -> a -> b
$ \ Precision
p -> MPBall -> CN MPBall
forall v. v -> CN v
cn (MPBall -> CN MPBall) -> MPBall -> CN MPBall
forall a b. (a -> b) -> a -> b
$ [MPBall]
balls [MPBall] -> Precision -> MPBall
forall n a. CanBeInteger n => [a] -> n -> a
!! Precision
p
where
balls :: [MPBall]
balls = MPBall -> [(a, Precision)] -> [MPBall]
forall {a} {t}.
(EqCompareType a Integer ~ Bool, MulType (AddType t t) Dyadic ~ t,
MinMaxType (IntervalEndpoint t) (IntervalEndpoint t)
~ IntervalEndpoint t,
IsInterval t, HasEqAsymmetric a Integer, CanAddAsymmetric t t,
CanMulAsymmetric (AddType t t) Dyadic, CanSetPrecision t,
CanMinMaxAsymmetric (IntervalEndpoint t) (IntervalEndpoint t)) =>
t -> [(a, Precision)] -> [t]
nextBit (CentreRadius Integer Integer -> MPBall
forall t. CanBeMPBall t => t -> MPBall
mpBall (Integer -> Integer -> CentreRadius Integer Integer
forall c e. c -> e -> CentreRadius c e
CentreRadius Integer
0 Integer
1)) ([(a, Precision)] -> [MPBall]) -> [(a, Precision)] -> [MPBall]
forall a b. (a -> b) -> a -> b
$ [a] -> [Precision] -> [(a, Precision)]
forall a b. [a] -> [b] -> [(a, b)]
zip [a]
sbits ((Integer -> Precision) -> [Integer] -> [Precision]
forall a b. (a -> b) -> [a] -> [b]
map Integer -> Precision
prec [Integer
10..])
nextBit :: t -> [(a, Precision)] -> [t]
nextBit t
ball ((a
sbit, Precision
p):[(a, Precision)]
rest) =
t
ball t -> [t] -> [t]
forall a. a -> [a] -> [a]
: t -> [(a, Precision)] -> [t]
nextBit t
newBall [(a, Precision)]
rest
where
newBall :: t
newBall =
case a
sbit of
(-1) -> t -> t -> t
forall i.
(IsInterval i, CanMinMaxSameType (IntervalEndpoint i)) =>
i -> i -> i
fromEndpointsAsIntervals t
l t
MulType (AddType t t) Dyadic
m
a
0 -> t -> t -> t
forall i.
(IsInterval i, CanMinMaxSameType (IntervalEndpoint i)) =>
i -> i -> i
fromEndpointsAsIntervals t
MulType (AddType t (MulType (AddType t t) Dyadic)) Dyadic
l2 t
MulType (AddType t (MulType (AddType t t) Dyadic)) Dyadic
r2
a
1 -> t -> t -> t
forall i.
(IsInterval i, CanMinMaxSameType (IntervalEndpoint i)) =>
i -> i -> i
fromEndpointsAsIntervals t
MulType (AddType t t) Dyadic
m t
r
a
_ -> [Char] -> t
forall a. HasCallStack => [Char] -> a
error [Char]
"in Arbitrary CReal"
(t
l_,t
r_) = t -> (t, t)
forall i. IsInterval i => i -> (i, i)
endpointsAsIntervals t
ball
l :: t
l = Precision -> t -> t
forall t. CanSetPrecision t => Precision -> t -> t
setPrecision Precision
p t
l_
r :: t
r = Precision -> t -> t
forall t. CanSetPrecision t => Precision -> t -> t
setPrecision Precision
p t
r_
m :: MulType (AddType t t) Dyadic
m = (t
l t -> t -> AddType t t
forall t1 t2. CanAddAsymmetric t1 t2 => t1 -> t2 -> AddType t1 t2
+ t
r) AddType t t -> Dyadic -> MulType (AddType t t) Dyadic
forall t1 t2. CanMulAsymmetric t1 t2 => t1 -> t2 -> MulType t1 t2
* (Rational -> Dyadic
forall t. CanBeDyadic t => t -> Dyadic
dyadic Rational
0.5)
l2 :: MulType (AddType t (MulType (AddType t t) Dyadic)) Dyadic
l2 = (t
l t
-> MulType (AddType t t) Dyadic
-> AddType t (MulType (AddType t t) Dyadic)
forall t1 t2. CanAddAsymmetric t1 t2 => t1 -> t2 -> AddType t1 t2
+ MulType (AddType t t) Dyadic
m) AddType t (MulType (AddType t t) Dyadic)
-> Dyadic
-> MulType (AddType t (MulType (AddType t t) Dyadic)) Dyadic
forall t1 t2. CanMulAsymmetric t1 t2 => t1 -> t2 -> MulType t1 t2
* (Rational -> Dyadic
forall t. CanBeDyadic t => t -> Dyadic
dyadic Rational
0.5)
r2 :: MulType (AddType t (MulType (AddType t t) Dyadic)) Dyadic
r2 = (t
r t
-> MulType (AddType t t) Dyadic
-> AddType t (MulType (AddType t t) Dyadic)
forall t1 t2. CanAddAsymmetric t1 t2 => t1 -> t2 -> AddType t1 t2
+ MulType (AddType t t) Dyadic
m) AddType t (MulType (AddType t t) Dyadic)
-> Dyadic
-> MulType (AddType t (MulType (AddType t t) Dyadic)) Dyadic
forall t1 t2. CanMulAsymmetric t1 t2 => t1 -> t2 -> MulType t1 t2
* (Rational -> Dyadic
forall t. CanBeDyadic t => t -> Dyadic
dyadic Rational
0.5)
nextBit t
_ [(a, Precision)]
_ = [Char] -> [t]
forall a. HasCallStack => [Char] -> a
error [Char]
"in Arbitrary CReal"
arbitrarySmall :: (Arbitrary a, HasOrderCertainly a Integer) => Integer -> Gen a
arbitrarySmall :: forall a.
(Arbitrary a, HasOrderCertainly a Integer) =>
Integer -> Gen a
arbitrarySmall Integer
bound = Gen a
aux
where
aux :: Gen a
aux =
do
a
x <- Gen a
forall a. Arbitrary a => Gen a
arbitrary
if -Integer
bound Integer -> a -> Bool
forall a b. HasOrderCertainlyAsymmetric a b => a -> b -> Bool
!<=! a
x Bool -> Bool -> AndOrType Bool Bool
forall a b. CanAndOrAsymmetric a b => a -> b -> AndOrType a b
&& a
x a -> Integer -> Bool
forall a b. HasOrderCertainlyAsymmetric a b => a -> b -> Bool
!<=! Integer
bound
then a -> Gen a
forall a. a -> Gen a
forall (m :: * -> *) a. Monad m => a -> m a
return a
x
else Gen a
aux
tCReal :: T CReal
tCReal :: T CReal
tCReal = [Char] -> T CReal
forall t. [Char] -> T t
T [Char]
"CReal"
specCRrespectsAccuracy1 ::
String ->
(CReal -> CReal) ->
(CReal -> Accuracy -> Bool) ->
Spec
specCRrespectsAccuracy1 :: [Char] -> (CReal -> CReal) -> (CReal -> Accuracy -> Bool) -> Spec
specCRrespectsAccuracy1 [Char]
opName CReal -> CReal
op CReal -> Accuracy -> Bool
precond =
[Char] -> Property -> SpecWith (Arg Property)
forall a.
(HasCallStack, Example a) =>
[Char] -> a -> SpecWith (Arg a)
it ([Char]
opName [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
" respects accuracy requests") (Property -> SpecWith (Arg Property))
-> Property -> SpecWith (Arg Property)
forall a b. (a -> b) -> a -> b
$ do
(CReal -> Accuracy -> Property) -> Property
forall prop. Testable prop => prop -> Property
property ((CReal -> Accuracy -> Property) -> Property)
-> (CReal -> Accuracy -> Property) -> Property
forall a b. (a -> b) -> a -> b
$
\ (CReal
x :: CReal) (Accuracy
ac :: Accuracy) ->
Accuracy
ac Accuracy -> Accuracy -> OrderCompareType Accuracy Accuracy
forall a b.
HasOrderAsymmetric a b =>
a -> b -> OrderCompareType a b
< (Integer -> Accuracy
forall t. ConvertibleExactly t Accuracy => t -> Accuracy
bits Integer
1000) Bool -> Bool -> AndOrType Bool Bool
forall a b. CanAndOrAsymmetric a b => a -> b -> AndOrType a b
&& CReal -> Accuracy -> Bool
precond CReal
x Accuracy
ac Bool -> Property -> Property
forall prop. Testable prop => Bool -> prop -> Property
==>
case CN MPBall -> Either NumErrors MPBall
forall es v. CanBeErrors es => CollectErrors es v -> Either es v
CN.toEither ((CReal -> CReal
op CReal
x) CReal -> Accuracy -> ExtractedApproximation CReal Accuracy
forall e q.
CanExtractApproximation e q =>
e -> q -> ExtractedApproximation e q
? Accuracy
ac) of
Right MPBall
v -> MPBall -> Accuracy
forall a. HasAccuracy a => a -> Accuracy
getAccuracy MPBall
v Accuracy -> Accuracy -> Property
>=$ Accuracy
ac
Either NumErrors MPBall
_ -> Bool -> Property
forall prop. Testable prop => prop -> Property
property Bool
True
specCRrespectsAccuracy2 ::
String ->
(CReal -> CReal -> CReal) ->
(CReal -> Accuracy -> Bool) ->
(CReal -> Accuracy -> Bool) ->
Spec
specCRrespectsAccuracy2 :: [Char]
-> (CReal -> CReal -> CReal)
-> (CReal -> Accuracy -> Bool)
-> (CReal -> Accuracy -> Bool)
-> Spec
specCRrespectsAccuracy2 [Char]
opName CReal -> CReal -> CReal
op CReal -> Accuracy -> Bool
precond1 CReal -> Accuracy -> Bool
precond2 =
[Char] -> Property -> SpecWith (Arg Property)
forall a.
(HasCallStack, Example a) =>
[Char] -> a -> SpecWith (Arg a)
it ([Char]
opName [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
" respects accuracy requests") (Property -> SpecWith (Arg Property))
-> Property -> SpecWith (Arg Property)
forall a b. (a -> b) -> a -> b
$ do
(CReal -> CReal -> Accuracy -> Property) -> Property
forall prop. Testable prop => prop -> Property
property ((CReal -> CReal -> Accuracy -> Property) -> Property)
-> (CReal -> CReal -> Accuracy -> Property) -> Property
forall a b. (a -> b) -> a -> b
$
\ (CReal
x :: CReal) (CReal
y :: CReal) (Accuracy
ac :: Accuracy) ->
Accuracy
ac Accuracy -> Accuracy -> OrderCompareType Accuracy Accuracy
forall a b.
HasOrderAsymmetric a b =>
a -> b -> OrderCompareType a b
< (Integer -> Accuracy
forall t. ConvertibleExactly t Accuracy => t -> Accuracy
bits Integer
1000) Bool -> Bool -> AndOrType Bool Bool
forall a b. CanAndOrAsymmetric a b => a -> b -> AndOrType a b
&& CReal -> Accuracy -> Bool
precond1 CReal
x Accuracy
ac Bool -> Bool -> AndOrType Bool Bool
forall a b. CanAndOrAsymmetric a b => a -> b -> AndOrType a b
&& CReal -> Accuracy -> Bool
precond2 CReal
y Accuracy
ac Bool -> Property -> Property
forall prop. Testable prop => Bool -> prop -> Property
==>
case CN MPBall -> Either NumErrors MPBall
forall es v. CanBeErrors es => CollectErrors es v -> Either es v
CN.toEither ((CReal -> CReal -> CReal
op CReal
x CReal
y) CReal -> Accuracy -> ExtractedApproximation CReal Accuracy
forall e q.
CanExtractApproximation e q =>
e -> q -> ExtractedApproximation e q
? Accuracy
ac) of
Right MPBall
v -> MPBall -> Accuracy
forall a. HasAccuracy a => a -> Accuracy
getAccuracy MPBall
v Accuracy -> Accuracy -> Property
>=$ Accuracy
ac
Either NumErrors MPBall
_ -> Bool -> Property
forall prop. Testable prop => prop -> Property
property Bool
True
(>=$) :: Accuracy -> Accuracy -> Property
>=$ :: Accuracy -> Accuracy -> Property
(>=$) = [Char]
-> (Accuracy -> Accuracy -> Bool)
-> Accuracy
-> Accuracy
-> Property
forall prop a b.
(Testable prop, Show a, Show b) =>
[Char] -> (a -> b -> prop) -> a -> b -> Property
printArgsIfFails2 [Char]
">=" Accuracy -> Accuracy -> Bool
Accuracy -> Accuracy -> OrderCompareType Accuracy Accuracy
forall a b.
HasOrderAsymmetric a b =>
a -> b -> OrderCompareType a b
(>=)
precondAnyReal :: CReal -> Accuracy -> Bool
precondAnyReal :: CReal -> Accuracy -> Bool
precondAnyReal CReal
_x Accuracy
_ac = Bool
True
precondPositiveReal :: CReal -> Accuracy -> Bool
precondPositiveReal :: CReal -> Accuracy -> Bool
precondPositiveReal CReal
x Accuracy
ac = (CReal
x CReal -> Accuracy -> ExtractedApproximation CReal Accuracy
forall e q.
CanExtractApproximation e q =>
e -> q -> ExtractedApproximation e q
? Accuracy
ac) CN MPBall -> Integer -> Bool
forall a b. HasOrderCertainlyAsymmetric a b => a -> b -> Bool
!>! Integer
0
precondNonZeroReal :: CReal -> Accuracy -> Bool
precondNonZeroReal :: CReal -> Accuracy -> Bool
precondNonZeroReal CReal
x Accuracy
ac = (CReal
x CReal -> Accuracy -> ExtractedApproximation CReal Accuracy
forall e q.
CanExtractApproximation e q =>
e -> q -> ExtractedApproximation e q
? Accuracy
ac) CN MPBall -> Integer -> Bool
forall a b. HasEqCertainlyAsymmetric a b => a -> b -> Bool
!/=! Integer
0
precondSmallReal :: CReal -> Accuracy -> Bool
precondSmallReal :: CReal -> Accuracy -> Bool
precondSmallReal CReal
x Accuracy
ac = CN MPBall -> AbsType (CN MPBall)
forall t. CanAbs t => t -> AbsType t
abs (CReal
x CReal -> Accuracy -> ExtractedApproximation CReal Accuracy
forall e q.
CanExtractApproximation e q =>
e -> q -> ExtractedApproximation e q
? Accuracy
ac) AbsType (CN MPBall) -> Integer -> Bool
forall a b. HasOrderCertainlyAsymmetric a b => a -> b -> Bool
!<! Integer
1000
precondPositiveSmallReal :: CReal -> Accuracy -> Bool
precondPositiveSmallReal :: CReal -> Accuracy -> Bool
precondPositiveSmallReal CReal
x Accuracy
ac = Integer
0 Integer -> CN MPBall -> Bool
forall a b. HasOrderCertainlyAsymmetric a b => a -> b -> Bool
!<! CN MPBall
ExtractedApproximation CReal Accuracy
b Bool -> Bool -> AndOrType Bool Bool
forall a b. CanAndOrAsymmetric a b => a -> b -> AndOrType a b
&& CN MPBall
ExtractedApproximation CReal Accuracy
b CN MPBall -> Integer -> Bool
forall a b. HasOrderCertainlyAsymmetric a b => a -> b -> Bool
!<! Integer
1000
where b :: ExtractedApproximation CReal Accuracy
b = CReal
x CReal -> Accuracy -> ExtractedApproximation CReal Accuracy
forall e q.
CanExtractApproximation e q =>
e -> q -> ExtractedApproximation e q
? Accuracy
ac
specCRrespectsAccuracy2T ::
(Arbitrary t, Show t) =>
T t ->
String ->
(CReal -> t -> CReal) ->
(CReal -> Accuracy -> Bool) ->
(t -> Bool) ->
Spec
specCRrespectsAccuracy2T :: forall t.
(Arbitrary t, Show t) =>
T t
-> [Char]
-> (CReal -> t -> CReal)
-> (CReal -> Accuracy -> Bool)
-> (t -> Bool)
-> Spec
specCRrespectsAccuracy2T (T [Char]
tName :: T t) [Char]
opName CReal -> t -> CReal
op CReal -> Accuracy -> Bool
precond1 t -> Bool
precond2 =
[Char] -> Property -> SpecWith (Arg Property)
forall a.
(HasCallStack, Example a) =>
[Char] -> a -> SpecWith (Arg a)
it ([Char]
opName [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
" with " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
tName [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
" respects accuracy requests") (Property -> SpecWith (Arg Property))
-> Property -> SpecWith (Arg Property)
forall a b. (a -> b) -> a -> b
$ do
(CReal -> t -> Accuracy -> Property) -> Property
forall prop. Testable prop => prop -> Property
property ((CReal -> t -> Accuracy -> Property) -> Property)
-> (CReal -> t -> Accuracy -> Property) -> Property
forall a b. (a -> b) -> a -> b
$
\ (CReal
x :: CReal) (t
t :: t) (Accuracy
ac :: Accuracy) ->
Accuracy
ac Accuracy -> Accuracy -> OrderCompareType Accuracy Accuracy
forall a b.
HasOrderAsymmetric a b =>
a -> b -> OrderCompareType a b
< (Integer -> Accuracy
forall t. ConvertibleExactly t Accuracy => t -> Accuracy
bits Integer
1000) Bool -> Bool -> AndOrType Bool Bool
forall a b. CanAndOrAsymmetric a b => a -> b -> AndOrType a b
&& CReal -> Accuracy -> Bool
precond1 CReal
x Accuracy
ac Bool -> Bool -> AndOrType Bool Bool
forall a b. CanAndOrAsymmetric a b => a -> b -> AndOrType a b
&& t -> Bool
precond2 t
t Bool -> Property -> Property
forall prop. Testable prop => Bool -> prop -> Property
==>
case CN MPBall -> Either NumErrors MPBall
forall es v. CanBeErrors es => CollectErrors es v -> Either es v
CN.toEither ((CReal -> t -> CReal
op CReal
x t
t) CReal -> Accuracy -> ExtractedApproximation CReal Accuracy
forall e q.
CanExtractApproximation e q =>
e -> q -> ExtractedApproximation e q
? Accuracy
ac) of
Right MPBall
v -> MPBall -> Accuracy
forall a. HasAccuracy a => a -> Accuracy
getAccuracy MPBall
v Accuracy -> Accuracy -> Property
>=$ Accuracy
ac
Either NumErrors MPBall
_ -> Bool -> Property
forall prop. Testable prop => prop -> Property
property Bool
True
precondAnyT :: t -> Bool
precondAnyT :: forall t. t -> Bool
precondAnyT t
_t = Bool
True
precondNonZeroT :: (HasEqCertainly t Integer) => t -> Bool
precondNonZeroT :: forall t. HasEqCertainly t Integer => t -> Bool
precondNonZeroT t
t = t
t t -> Integer -> Bool
forall a b. HasEqCertainlyAsymmetric a b => a -> b -> Bool
!/=! Integer
0
precondSmallT :: (HasOrderCertainly t Integer) => t -> Bool
precondSmallT :: forall t. HasOrderCertainly t Integer => t -> Bool
precondSmallT t
t = -Integer
1000 Integer -> t -> Bool
forall a b. HasOrderCertainlyAsymmetric a b => a -> b -> Bool
!<=! t
t Bool -> Bool -> AndOrType Bool Bool
forall a b. CanAndOrAsymmetric a b => a -> b -> AndOrType a b
&& t
t t -> Integer -> Bool
forall a b. HasOrderCertainlyAsymmetric a b => a -> b -> Bool
!<=! Integer
1000
specCReal :: Spec
specCReal :: Spec
specCReal =
[Char] -> Spec -> Spec
forall a. HasCallStack => [Char] -> SpecWith a -> SpecWith a
describe ([Char]
"CReal") (Spec -> Spec) -> Spec -> Spec
forall a b. (a -> b) -> a -> b
$ do
[Char] -> Spec -> Spec
forall a. HasCallStack => [Char] -> SpecWith a -> SpecWith a
describe [Char]
"min/max/abs" (Spec -> Spec) -> Spec -> Spec
forall a b. (a -> b) -> a -> b
$ do
[Char] -> (CReal -> CReal) -> (CReal -> Accuracy -> Bool) -> Spec
specCRrespectsAccuracy1 [Char]
"abs" CReal -> AbsType CReal
CReal -> CReal
forall t. CanAbs t => t -> AbsType t
abs CReal -> Accuracy -> Bool
precondAnyReal
[Char]
-> (CReal -> CReal -> CReal)
-> (CReal -> Accuracy -> Bool)
-> (CReal -> Accuracy -> Bool)
-> Spec
specCRrespectsAccuracy2 [Char]
"max" CReal -> CReal -> MinMaxType CReal CReal
CReal -> CReal -> CReal
forall t1 t2.
CanMinMaxAsymmetric t1 t2 =>
t1 -> t2 -> MinMaxType t1 t2
max CReal -> Accuracy -> Bool
precondAnyReal CReal -> Accuracy -> Bool
precondAnyReal
[Char]
-> (CReal -> CReal -> CReal)
-> (CReal -> Accuracy -> Bool)
-> (CReal -> Accuracy -> Bool)
-> Spec
specCRrespectsAccuracy2 [Char]
"min" CReal -> CReal -> MinMaxType CReal CReal
CReal -> CReal -> CReal
forall t1 t2.
CanMinMaxAsymmetric t1 t2 =>
t1 -> t2 -> MinMaxType t1 t2
min CReal -> Accuracy -> Bool
precondAnyReal CReal -> Accuracy -> Bool
precondAnyReal
[Char] -> Spec -> Spec
forall a. HasCallStack => [Char] -> SpecWith a -> SpecWith a
describe [Char]
"ring" (Spec -> Spec) -> Spec -> Spec
forall a b. (a -> b) -> a -> b
$ do
[Char] -> (CReal -> CReal) -> (CReal -> Accuracy -> Bool) -> Spec
specCRrespectsAccuracy1 [Char]
"negate" CReal -> NegType CReal
CReal -> CReal
forall t. CanNeg t => t -> NegType t
negate CReal -> Accuracy -> Bool
precondAnyReal
[Char]
-> (CReal -> CReal -> CReal)
-> (CReal -> Accuracy -> Bool)
-> (CReal -> Accuracy -> Bool)
-> Spec
specCRrespectsAccuracy2 [Char]
"+" CReal -> CReal -> AddType CReal CReal
CReal -> CReal -> CReal
forall t1 t2. CanAddAsymmetric t1 t2 => t1 -> t2 -> AddType t1 t2
add CReal -> Accuracy -> Bool
precondAnyReal CReal -> Accuracy -> Bool
precondAnyReal
T Integer
-> [Char]
-> (CReal -> Integer -> CReal)
-> (CReal -> Accuracy -> Bool)
-> (Integer -> Bool)
-> Spec
forall t.
(Arbitrary t, Show t) =>
T t
-> [Char]
-> (CReal -> t -> CReal)
-> (CReal -> Accuracy -> Bool)
-> (t -> Bool)
-> Spec
specCRrespectsAccuracy2T T Integer
tInteger [Char]
"+" CReal -> Integer -> AddType CReal Integer
CReal -> Integer -> CReal
forall t1 t2. CanAddAsymmetric t1 t2 => t1 -> t2 -> AddType t1 t2
add CReal -> Accuracy -> Bool
precondAnyReal Integer -> Bool
forall t. t -> Bool
precondAnyT
T Rational
-> [Char]
-> (CReal -> Rational -> CReal)
-> (CReal -> Accuracy -> Bool)
-> (Rational -> Bool)
-> Spec
forall t.
(Arbitrary t, Show t) =>
T t
-> [Char]
-> (CReal -> t -> CReal)
-> (CReal -> Accuracy -> Bool)
-> (t -> Bool)
-> Spec
specCRrespectsAccuracy2T T Rational
tRational [Char]
"+" CReal -> Rational -> AddType CReal Rational
CReal -> Rational -> CReal
forall t1 t2. CanAddAsymmetric t1 t2 => t1 -> t2 -> AddType t1 t2
add CReal -> Accuracy -> Bool
precondAnyReal Rational -> Bool
forall t. t -> Bool
precondAnyT
T Dyadic
-> [Char]
-> (CReal -> Dyadic -> CReal)
-> (CReal -> Accuracy -> Bool)
-> (Dyadic -> Bool)
-> Spec
forall t.
(Arbitrary t, Show t) =>
T t
-> [Char]
-> (CReal -> t -> CReal)
-> (CReal -> Accuracy -> Bool)
-> (t -> Bool)
-> Spec
specCRrespectsAccuracy2T T Dyadic
tDyadic [Char]
"+" CReal -> Dyadic -> AddType CReal Dyadic
CReal -> Dyadic -> CReal
forall t1 t2. CanAddAsymmetric t1 t2 => t1 -> t2 -> AddType t1 t2
add CReal -> Accuracy -> Bool
precondAnyReal Dyadic -> Bool
forall t. t -> Bool
precondAnyT
[Char]
-> (CReal -> CReal -> CReal)
-> (CReal -> Accuracy -> Bool)
-> (CReal -> Accuracy -> Bool)
-> Spec
specCRrespectsAccuracy2 [Char]
"a-b" CReal -> CReal -> SubType CReal CReal
CReal -> CReal -> CReal
forall t1 t2. CanSub t1 t2 => t1 -> t2 -> SubType t1 t2
sub CReal -> Accuracy -> Bool
precondAnyReal CReal -> Accuracy -> Bool
precondAnyReal
T Integer
-> [Char]
-> (CReal -> Integer -> CReal)
-> (CReal -> Accuracy -> Bool)
-> (Integer -> Bool)
-> Spec
forall t.
(Arbitrary t, Show t) =>
T t
-> [Char]
-> (CReal -> t -> CReal)
-> (CReal -> Accuracy -> Bool)
-> (t -> Bool)
-> Spec
specCRrespectsAccuracy2T T Integer
tInteger [Char]
"a-b" CReal -> Integer -> SubType CReal Integer
CReal -> Integer -> CReal
forall t1 t2. CanSub t1 t2 => t1 -> t2 -> SubType t1 t2
sub CReal -> Accuracy -> Bool
precondAnyReal Integer -> Bool
forall t. t -> Bool
precondAnyT
T Rational
-> [Char]
-> (CReal -> Rational -> CReal)
-> (CReal -> Accuracy -> Bool)
-> (Rational -> Bool)
-> Spec
forall t.
(Arbitrary t, Show t) =>
T t
-> [Char]
-> (CReal -> t -> CReal)
-> (CReal -> Accuracy -> Bool)
-> (t -> Bool)
-> Spec
specCRrespectsAccuracy2T T Rational
tRational [Char]
"a-b" CReal -> Rational -> SubType CReal Rational
CReal -> Rational -> CReal
forall t1 t2. CanSub t1 t2 => t1 -> t2 -> SubType t1 t2
sub CReal -> Accuracy -> Bool
precondAnyReal Rational -> Bool
forall t. t -> Bool
precondAnyT
T Dyadic
-> [Char]
-> (CReal -> Dyadic -> CReal)
-> (CReal -> Accuracy -> Bool)
-> (Dyadic -> Bool)
-> Spec
forall t.
(Arbitrary t, Show t) =>
T t
-> [Char]
-> (CReal -> t -> CReal)
-> (CReal -> Accuracy -> Bool)
-> (t -> Bool)
-> Spec
specCRrespectsAccuracy2T T Dyadic
tDyadic [Char]
"a-b" CReal -> Dyadic -> SubType CReal Dyadic
CReal -> Dyadic -> CReal
forall t1 t2. CanSub t1 t2 => t1 -> t2 -> SubType t1 t2
sub CReal -> Accuracy -> Bool
precondAnyReal Dyadic -> Bool
forall t. t -> Bool
precondAnyT
[Char]
-> (CReal -> CReal -> CReal)
-> (CReal -> Accuracy -> Bool)
-> (CReal -> Accuracy -> Bool)
-> Spec
specCRrespectsAccuracy2 [Char]
"*" CReal -> CReal -> MulType CReal CReal
CReal -> CReal -> CReal
forall t1 t2. CanMulAsymmetric t1 t2 => t1 -> t2 -> MulType t1 t2
mul CReal -> Accuracy -> Bool
precondAnyReal CReal -> Accuracy -> Bool
precondAnyReal
T Integer
-> [Char]
-> (CReal -> Integer -> CReal)
-> (CReal -> Accuracy -> Bool)
-> (Integer -> Bool)
-> Spec
forall t.
(Arbitrary t, Show t) =>
T t
-> [Char]
-> (CReal -> t -> CReal)
-> (CReal -> Accuracy -> Bool)
-> (t -> Bool)
-> Spec
specCRrespectsAccuracy2T T Integer
tInteger [Char]
"*" CReal -> Integer -> MulType CReal Integer
CReal -> Integer -> CReal
forall t1 t2. CanMulAsymmetric t1 t2 => t1 -> t2 -> MulType t1 t2
mul CReal -> Accuracy -> Bool
precondAnyReal Integer -> Bool
forall t. t -> Bool
precondAnyT
T Rational
-> [Char]
-> (CReal -> Rational -> CReal)
-> (CReal -> Accuracy -> Bool)
-> (Rational -> Bool)
-> Spec
forall t.
(Arbitrary t, Show t) =>
T t
-> [Char]
-> (CReal -> t -> CReal)
-> (CReal -> Accuracy -> Bool)
-> (t -> Bool)
-> Spec
specCRrespectsAccuracy2T T Rational
tRational [Char]
"*" CReal -> Rational -> MulType CReal Rational
CReal -> Rational -> CReal
forall t1 t2. CanMulAsymmetric t1 t2 => t1 -> t2 -> MulType t1 t2
mul CReal -> Accuracy -> Bool
precondAnyReal Rational -> Bool
forall t. t -> Bool
precondAnyT
T Dyadic
-> [Char]
-> (CReal -> Dyadic -> CReal)
-> (CReal -> Accuracy -> Bool)
-> (Dyadic -> Bool)
-> Spec
forall t.
(Arbitrary t, Show t) =>
T t
-> [Char]
-> (CReal -> t -> CReal)
-> (CReal -> Accuracy -> Bool)
-> (t -> Bool)
-> Spec
specCRrespectsAccuracy2T T Dyadic
tDyadic [Char]
"*" CReal -> Dyadic -> MulType CReal Dyadic
CReal -> Dyadic -> CReal
forall t1 t2. CanMulAsymmetric t1 t2 => t1 -> t2 -> MulType t1 t2
mul CReal -> Accuracy -> Bool
precondAnyReal Dyadic -> Bool
forall t. t -> Bool
precondAnyT
[Char] -> Spec -> Spec
forall a. HasCallStack => [Char] -> SpecWith a -> SpecWith a
describe [Char]
"field" (Spec -> Spec) -> Spec -> Spec
forall a b. (a -> b) -> a -> b
$ do
[Char]
-> (CReal -> CReal -> CReal)
-> (CReal -> Accuracy -> Bool)
-> (CReal -> Accuracy -> Bool)
-> Spec
specCRrespectsAccuracy2 [Char]
"/" CReal -> CReal -> DivType CReal CReal
CReal -> CReal -> CReal
forall t1 t2. CanDiv t1 t2 => t1 -> t2 -> DivType t1 t2
divide CReal -> Accuracy -> Bool
precondAnyReal CReal -> Accuracy -> Bool
precondNonZeroReal
T Integer
-> [Char]
-> (CReal -> Integer -> CReal)
-> (CReal -> Accuracy -> Bool)
-> (Integer -> Bool)
-> Spec
forall t.
(Arbitrary t, Show t) =>
T t
-> [Char]
-> (CReal -> t -> CReal)
-> (CReal -> Accuracy -> Bool)
-> (t -> Bool)
-> Spec
specCRrespectsAccuracy2T T Integer
tInteger [Char]
"/" CReal -> Integer -> DivType CReal Integer
CReal -> Integer -> CReal
forall t1 t2. CanDiv t1 t2 => t1 -> t2 -> DivType t1 t2
divide CReal -> Accuracy -> Bool
precondAnyReal Integer -> Bool
forall t. HasEqCertainly t Integer => t -> Bool
precondNonZeroT
T Rational
-> [Char]
-> (CReal -> Rational -> CReal)
-> (CReal -> Accuracy -> Bool)
-> (Rational -> Bool)
-> Spec
forall t.
(Arbitrary t, Show t) =>
T t
-> [Char]
-> (CReal -> t -> CReal)
-> (CReal -> Accuracy -> Bool)
-> (t -> Bool)
-> Spec
specCRrespectsAccuracy2T T Rational
tRational [Char]
"/" CReal -> Rational -> DivType CReal Rational
CReal -> Rational -> CReal
forall t1 t2. CanDiv t1 t2 => t1 -> t2 -> DivType t1 t2
divide CReal -> Accuracy -> Bool
precondAnyReal Rational -> Bool
forall t. HasEqCertainly t Integer => t -> Bool
precondNonZeroT
T Dyadic
-> [Char]
-> (CReal -> Dyadic -> CReal)
-> (CReal -> Accuracy -> Bool)
-> (Dyadic -> Bool)
-> Spec
forall t.
(Arbitrary t, Show t) =>
T t
-> [Char]
-> (CReal -> t -> CReal)
-> (CReal -> Accuracy -> Bool)
-> (t -> Bool)
-> Spec
specCRrespectsAccuracy2T T Dyadic
tDyadic [Char]
"/" CReal -> Dyadic -> DivType CReal Dyadic
CReal -> Dyadic -> CReal
forall t1 t2. CanDiv t1 t2 => t1 -> t2 -> DivType t1 t2
divide CReal -> Accuracy -> Bool
precondAnyReal Dyadic -> Bool
forall t. HasEqCertainly t Integer => t -> Bool
precondNonZeroT
[Char] -> Spec -> Spec
forall a. HasCallStack => [Char] -> SpecWith a -> SpecWith a
describe [Char]
"elementary" (Spec -> Spec) -> Spec -> Spec
forall a b. (a -> b) -> a -> b
$ do
[Char] -> (CReal -> CReal) -> (CReal -> Accuracy -> Bool) -> Spec
specCRrespectsAccuracy1 [Char]
"sqrt" CReal -> SqrtType CReal
CReal -> CReal
forall t. CanSqrt t => t -> SqrtType t
sqrt CReal -> Accuracy -> Bool
precondPositiveReal
[Char] -> (CReal -> CReal) -> (CReal -> Accuracy -> Bool) -> Spec
specCRrespectsAccuracy1 [Char]
"exp" CReal -> ExpType CReal
CReal -> CReal
forall t. CanExp t => t -> ExpType t
exp CReal -> Accuracy -> Bool
precondSmallReal
[Char] -> (CReal -> CReal) -> (CReal -> Accuracy -> Bool) -> Spec
specCRrespectsAccuracy1 [Char]
"log" CReal -> LogType CReal
CReal -> CReal
forall t. CanLog t => t -> LogType t
log CReal -> Accuracy -> Bool
precondPositiveSmallReal
[Char]
-> (CReal -> CReal -> CReal)
-> (CReal -> Accuracy -> Bool)
-> (CReal -> Accuracy -> Bool)
-> Spec
specCRrespectsAccuracy2 [Char]
"pow" CReal -> CReal -> PowType CReal CReal
CReal -> CReal -> CReal
forall b e. CanPow b e => b -> e -> PowType b e
pow CReal -> Accuracy -> Bool
precondPositiveSmallReal CReal -> Accuracy -> Bool
precondSmallReal
T Integer
-> [Char]
-> (CReal -> Integer -> CReal)
-> (CReal -> Accuracy -> Bool)
-> (Integer -> Bool)
-> Spec
forall t.
(Arbitrary t, Show t) =>
T t
-> [Char]
-> (CReal -> t -> CReal)
-> (CReal -> Accuracy -> Bool)
-> (t -> Bool)
-> Spec
specCRrespectsAccuracy2T T Integer
tInteger [Char]
"pow" CReal -> Integer -> PowType CReal Integer
CReal -> Integer -> CReal
forall b e. CanPow b e => b -> e -> PowType b e
pow CReal -> Accuracy -> Bool
precondNonZeroReal Integer -> Bool
forall t. HasOrderCertainly t Integer => t -> Bool
precondSmallT
T Rational
-> [Char]
-> (CReal -> Rational -> CReal)
-> (CReal -> Accuracy -> Bool)
-> (Rational -> Bool)
-> Spec
forall t.
(Arbitrary t, Show t) =>
T t
-> [Char]
-> (CReal -> t -> CReal)
-> (CReal -> Accuracy -> Bool)
-> (t -> Bool)
-> Spec
specCRrespectsAccuracy2T T Rational
tRational [Char]
"pow" CReal -> Rational -> PowType CReal Rational
CReal -> Rational -> CReal
forall b e. CanPow b e => b -> e -> PowType b e
pow CReal -> Accuracy -> Bool
precondPositiveSmallReal Rational -> Bool
forall t. HasOrderCertainly t Integer => t -> Bool
precondSmallT
[Char] -> (CReal -> CReal) -> (CReal -> Accuracy -> Bool) -> Spec
specCRrespectsAccuracy1 [Char]
"cos" CReal -> SinCosType CReal
CReal -> CReal
forall t. CanSinCos t => t -> SinCosType t
cos CReal -> Accuracy -> Bool
precondAnyReal
[Char] -> (CReal -> CReal) -> (CReal -> Accuracy -> Bool) -> Spec
specCRrespectsAccuracy1 [Char]
"sine" CReal -> SinCosType CReal
CReal -> CReal
forall t. CanSinCos t => t -> SinCosType t
sin CReal -> Accuracy -> Bool
precondAnyReal
[Char] -> Spec -> Spec
forall a. HasCallStack => [Char] -> SpecWith a -> SpecWith a
describe [Char]
"select" (Spec -> Spec) -> Spec -> Spec
forall a b. (a -> b) -> a -> b
$ do
[Char] -> Property -> SpecWith (Arg Property)
forall a.
(HasCallStack, Example a) =>
[Char] -> a -> SpecWith (Arg a)
it [Char]
"soft abs via select" (Property -> SpecWith (Arg Property))
-> Property -> SpecWith (Arg Property)
forall a b. (a -> b) -> a -> b
$ do
(CReal -> Precision -> Rational -> Property) -> Property
forall prop. Testable prop => prop -> Property
property ((CReal -> Precision -> Rational -> Property) -> Property)
-> (CReal -> Precision -> Rational -> Property) -> Property
forall a b. (a -> b) -> a -> b
$ \ (CReal
x :: CReal) (Precision
p :: Precision) (Rational
q :: Rational) ->
(Integer
1 Integer -> Rational -> OrderCompareType Integer Rational
forall a b.
HasOrderAsymmetric a b =>
a -> b -> OrderCompareType a b
< Rational
q) Bool -> Bool -> Property
forall prop. Testable prop => Bool -> prop -> Property
==>
let eps :: DivType Integer Rational
eps = Integer
1Integer -> Rational -> DivType Integer Rational
forall t1 t2. CanDiv t1 t2 => t1 -> t2 -> DivType t1 t2
/Rational
q in
(SubType
CReal
(IfThenElseType
(SelectType (OrderCompareType CReal (NegType Rational))) CReal)
-> AbsType
(SubType
CReal
(IfThenElseType
(SelectType (OrderCompareType CReal (NegType Rational))) CReal))
forall t. CanAbs t => t -> AbsType t
abs (CReal -> AbsType CReal
forall t. CanAbs t => t -> AbsType t
abs CReal
x CReal
-> IfThenElseType
(SelectType (OrderCompareType CReal (NegType Rational))) CReal
-> SubType
CReal
(IfThenElseType
(SelectType (OrderCompareType CReal (NegType Rational))) CReal)
forall t1 t2. CanSub t1 t2 => t1 -> t2 -> SubType t1 t2
- (if OrderCompareType CReal (NegType Rational)
-> OrderCompareType CReal (NegType Rational)
-> SelectType (OrderCompareType CReal (NegType Rational))
forall k. CanSelect k => k -> k -> SelectType k
select (CReal
x CReal
-> NegType Rational -> OrderCompareType CReal (NegType Rational)
forall a b.
HasOrderAsymmetric a b =>
a -> b -> OrderCompareType a b
> -Rational
DivType Integer Rational
eps) (CReal
x CReal -> Rational -> OrderCompareType CReal Rational
forall a b.
HasOrderAsymmetric a b =>
a -> b -> OrderCompareType a b
< Rational
DivType Integer Rational
eps) then CReal
x else -CReal
x)) AbsType
(SubType
CReal
(IfThenElseType
(SelectType (OrderCompareType CReal (NegType Rational))) CReal))
-> Precision
-> ExtractedApproximation
(AbsType
(SubType
CReal
(IfThenElseType
(SelectType (OrderCompareType CReal (NegType Rational))) CReal)))
Precision
forall e q.
CanExtractApproximation e q =>
e -> q -> ExtractedApproximation e q
? Precision
p) ExtractedApproximation
(AbsType
(SubType
CReal
(IfThenElseType
(SelectType (OrderCompareType CReal (NegType Rational))) CReal)))
Precision
-> MulType Integer Rational -> Bool
forall a b. HasOrderCertainlyAsymmetric a b => a -> b -> Bool
?<? Integer
2Integer -> Rational -> MulType Integer Rational
forall t1 t2. CanMulAsymmetric t1 t2 => t1 -> t2 -> MulType t1 t2
*Rational
DivType Integer Rational
eps
[Char] -> Spec -> Spec
forall a. HasCallStack => [Char] -> SpecWith a -> SpecWith a
describe [Char]
"limit" (Spec -> Spec) -> Spec -> Spec
forall a b. (a -> b) -> a -> b
$ do
[Char] -> Property -> SpecWith (Arg Property)
forall a.
(HasCallStack, Example a) =>
[Char] -> a -> SpecWith (Arg a)
it [Char]
"computing e as a limit of Taylor series" (Property -> SpecWith (Arg Property))
-> Property -> SpecWith (Arg Property)
forall a b. (a -> b) -> a -> b
$ do
(Precision -> Bool) -> Property
forall prop. Testable prop => prop -> Property
property ((Precision -> Bool) -> Property)
-> (Precision -> Bool) -> Property
forall a b. (a -> b) -> a -> b
$ \ (Precision
p :: Precision) ->
((MPBall -> ExpType MPBall
forall t. CanExp t => t -> ExpType t
exp (Precision -> Rational -> MPBall
forall t. CanBeMPBallP t => Precision -> t -> MPBall
mpBallP Precision
p Rational
1.0)) MPBall
-> ExtractedApproximation (LimitType Integer CReal) Precision
-> Bool
forall a b. HasEqCertainlyAsymmetric a b => a -> b -> Bool
?==?) (ExtractedApproximation (LimitType Integer CReal) Precision
-> Bool)
-> ExtractedApproximation (LimitType Integer CReal) Precision
-> Bool
forall a b. (a -> b) -> a -> b
$
((Integer -> CReal) -> LimitType Integer CReal
forall ix s. HasLimits ix s => (ix -> s) -> LimitType ix s
limit ((Integer -> CReal) -> LimitType Integer CReal)
-> (Integer -> CReal) -> LimitType Integer CReal
forall a b. (a -> b) -> a -> b
$ \(Integer
n :: Integer) -> [CReal] -> CReal
forall t.
(CanAddSameType t, ConvertibleExactly Integer t) =>
[t] -> t
sum ([CReal] -> CReal) -> [CReal] -> CReal
forall a b. (a -> b) -> a -> b
$ (Integer -> CReal) -> [Integer] -> [CReal]
forall a b. (a -> b) -> [a] -> [b]
map (CReal -> DivType Integer CReal
CReal -> CReal
forall t. CanRecip t => t -> DivType Integer t
recip (CReal -> CReal) -> (Integer -> CReal) -> Integer -> CReal
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> CReal
forall t. CanBeCReal t => t -> CReal
creal) ([Integer] -> [CReal]) -> [Integer] -> [CReal]
forall a b. (a -> b) -> a -> b
$ Integer -> [Integer] -> [Integer]
forall n a. CanBeInteger n => n -> [a] -> [a]
take (Integer
nInteger -> Integer -> AddType Integer Integer
forall t1 t2. CanAddAsymmetric t1 t2 => t1 -> t2 -> AddType t1 t2
+Integer
3) ([Integer] -> [Integer]) -> [Integer] -> [Integer]
forall a b. (a -> b) -> a -> b
$ (Integer -> Integer -> Integer)
-> Integer -> [Integer] -> [Integer]
forall b a. (b -> a -> b) -> b -> [a] -> [b]
scanl Integer -> Integer -> Integer
Integer -> Integer -> MulType Integer Integer
forall t1 t2. CanMulAsymmetric t1 t2 => t1 -> t2 -> MulType t1 t2
(*) Integer
1 [Integer
1..(Integer
n)]) LimitType Integer CReal
-> Precision
-> ExtractedApproximation (LimitType Integer CReal) Precision
forall e q.
CanExtractApproximation e q =>
e -> q -> ExtractedApproximation e q
? Precision
p