{-# LANGUAGE PartialTypeSignatures #-}
{-# OPTIONS_GHC -Wno-missing-signatures #-}
{-# OPTIONS_GHC -Wno-partial-type-signatures #-}
module AERN2.Real.Examples.Introduction where
import MixedTypesNumPrelude
import qualified Numeric.CollectErrors as CN
import AERN2.MP
import AERN2.Real
sine1 :: SinCosType Integer
sine1 = Integer -> SinCosType Integer
forall t. CanSinCos t => t -> SinCosType t
sin Integer
1
sine1_run1 :: ExtractedApproximation CReal Precision
sine1_run1 = CReal
sine1 CReal -> Precision -> ExtractedApproximation CReal Precision
forall e q.
CanExtractApproximation e q =>
e -> q -> ExtractedApproximation e q
? (Integer -> Precision
prec Integer
120)
sine1_run2 :: ExtractedApproximation CReal Accuracy
sine1_run2 = CReal
sine1 CReal -> Accuracy -> ExtractedApproximation CReal Accuracy
forall e q.
CanExtractApproximation e q =>
e -> q -> ExtractedApproximation e q
? (Integer -> Accuracy
forall t. ConvertibleExactly t Accuracy => t -> Accuracy
bits Integer
120)
sumSines1 :: Integer -> CReal
sumSines1 :: Integer -> CReal
sumSines1 Integer
n = [CReal] -> CReal
forall t.
(CanAddSameType t, ConvertibleExactly Integer t) =>
[t] -> t
sum [Integer -> SinCosType Integer
forall t. CanSinCos t => t -> SinCosType t
sin Integer
i | Integer
i <- [Integer
1..Integer
n]]
sumSines1_run1 :: CN MPBall
sumSines1_run1 :: CN MPBall
sumSines1_run1 = (Integer -> CReal
sumSines1 Integer
100) CReal -> Precision -> ExtractedApproximation CReal Precision
forall e q.
CanExtractApproximation e q =>
e -> q -> ExtractedApproximation e q
? (Integer -> Precision
prec Integer
120)
sumSines1_run2 :: ExtractedApproximation CReal Accuracy
sumSines1_run2 = (Integer -> CReal
sumSines1 Integer
100) CReal -> Accuracy -> ExtractedApproximation CReal Accuracy
forall e q.
CanExtractApproximation e q =>
e -> q -> ExtractedApproximation e q
? (Integer -> Accuracy
forall t. ConvertibleExactly t Accuracy => t -> Accuracy
bits Integer
100)
pi100 :: CN MPBall
pi100 :: CN MPBall
pi100 = CReal
piCReal -> Accuracy -> ExtractedApproximation CReal Accuracy
forall e q.
CanExtractApproximation e q =>
e -> q -> ExtractedApproximation e q
?(Integer -> Accuracy
forall t. ConvertibleExactly t Accuracy => t -> Accuracy
bits Integer
100)
compare_run1 :: CN Kleenean
compare_run1 :: CN Kleenean
compare_run1 = CN MPBall
pi100 CN MPBall -> Integer -> OrderCompareType (CN MPBall) Integer
forall a b.
HasOrderAsymmetric a b =>
a -> b -> OrderCompareType a b
> Integer
0
compare_run2 :: CN Kleenean
compare_run2 :: CN Kleenean
compare_run2 = CN MPBall
pi100 CN MPBall -> CN MPBall -> EqCompareType (CN MPBall) (CN MPBall)
forall a b. HasEqAsymmetric a b => a -> b -> EqCompareType a b
== CN MPBall
pi100
compare_run3 :: CKleenean
compare_run3 :: CKleenean
compare_run3 = CReal
pi CReal -> Integer -> OrderCompareType CReal Integer
forall a b.
HasOrderAsymmetric a b =>
a -> b -> OrderCompareType a b
> Integer
0
compare_run4 :: EqCompareType CReal CReal
compare_run4 = CReal
pi CReal -> CReal -> EqCompareType CReal CReal
forall a b. HasEqAsymmetric a b => a -> b -> EqCompareType a b
== CReal
pi CReal -> Integer -> AddType CReal Integer
forall t1 t2. CanAddAsymmetric t1 t2 => t1 -> t2 -> AddType t1 t2
+ Integer
2Integer -> Integer -> PowType Integer Integer
forall t1 t2. CanPow t1 t2 => t1 -> t2 -> PowType t1 t2
^(-Integer
100)
compare_run5 :: ExtractedApproximation CKleenean Precision
compare_run5 = (CReal
pi CReal -> CReal -> EqCompareType CReal CReal
forall a b. HasEqAsymmetric a b => a -> b -> EqCompareType a b
== CReal
pi CReal -> Integer -> AddType CReal Integer
forall t1 t2. CanAddAsymmetric t1 t2 => t1 -> t2 -> AddType t1 t2
+ Integer
2Integer -> Integer -> PowType Integer Integer
forall t1 t2. CanPow t1 t2 => t1 -> t2 -> PowType t1 t2
^(-Integer
100)) CKleenean
-> Precision -> ExtractedApproximation CKleenean Precision
forall e q.
CanExtractApproximation e q =>
e -> q -> ExtractedApproximation e q
? (Integer -> Precision
prec Integer
1000)
compare_run6 :: EqCompareType CReal Integer
compare_run6 = (Integer -> CReal
forall t. CanBeCReal t => t -> CReal
creal Integer
0) CReal -> Integer -> EqCompareType CReal Integer
forall a b. HasEqAsymmetric a b => a -> b -> EqCompareType a b
== Integer
0
compare_run7 :: ExtractedApproximation CKleenean Precision
compare_run7 = CReal
pi CReal -> CReal -> EqCompareType CReal CReal
forall a b. HasEqAsymmetric a b => a -> b -> EqCompareType a b
== CReal
pi CKleenean
-> Precision -> ExtractedApproximation CKleenean Precision
forall e q.
CanExtractApproximation e q =>
e -> q -> ExtractedApproximation e q
? (Integer -> Precision
prec Integer
10000)
partialfn_bad1 :: SqrtType Integer
partialfn_bad1 = Integer -> SqrtType Integer
forall t. CanSqrt t => t -> SqrtType t
sqrt (-Integer
1)
a_third :: CReal
a_third = Rational -> CReal
forall t. CanBeCReal t => t -> CReal
creal (Integer
1Integer -> Integer -> DivType Integer Integer
forall t1 t2. CanDiv t1 t2 => t1 -> t2 -> DivType t1 t2
/Integer
3)
partialfn_bad2 :: DivType Integer CReal
partialfn_bad2 = Integer
1Integer -> CReal -> DivType Integer CReal
forall t1 t2. CanDiv t1 t2 => t1 -> t2 -> DivType t1 t2
/(CReal
a_thirdCReal -> CReal -> SubType CReal CReal
forall t1 t2. CanSub t1 t2 => t1 -> t2 -> SubType t1 t2
-CReal
a_third)
partialfn_bad3 :: DivType Integer CReal
partialfn_bad3 = Integer
1Integer -> CReal -> DivType Integer CReal
forall t1 t2. CanDiv t1 t2 => t1 -> t2 -> DivType t1 t2
/(CReal
piCReal -> CReal -> SubType CReal CReal
forall t1 t2. CanSub t1 t2 => t1 -> t2 -> SubType t1 t2
-CReal
pi)
partialfn_ok4 :: SqrtType CReal
partialfn_ok4 = CReal -> SqrtType CReal
forall t. CanSqrt t => t -> SqrtType t
sqrt (CReal
piCReal -> CReal -> SubType CReal CReal
forall t1 t2. CanSub t1 t2 => t1 -> t2 -> SubType t1 t2
-CReal
pi)
partialfn_ok5 :: CReal
partialfn_ok5 = CReal -> CReal
forall cnt. CanClearPotentialErrors cnt => cnt -> cnt
clearPotentialErrors (CReal -> SqrtType CReal
forall t. CanSqrt t => t -> SqrtType t
sqrt (CReal
piCReal -> CReal -> SubType CReal CReal
forall t1 t2. CanSub t1 t2 => t1 -> t2 -> SubType t1 t2
-CReal
pi))
partialfn_bad6 :: CReal
partialfn_bad6 = CReal -> CReal
forall cnt. CanClearPotentialErrors cnt => cnt -> cnt
clearPotentialErrors (CReal -> SqrtType CReal
forall t. CanSqrt t => t -> SqrtType t
sqrt (CReal
piCReal -> CReal -> SubType CReal CReal
forall t1 t2. CanSub t1 t2 => t1 -> t2 -> SubType t1 t2
-CReal
piCReal -> Integer -> SubType CReal Integer
forall t1 t2. CanSub t1 t2 => t1 -> t2 -> SubType t1 t2
-Integer
1))
partialfn_bad7 :: CReal
partialfn_bad7 = CReal -> CReal
forall cnt. CanClearPotentialErrors cnt => cnt -> cnt
clearPotentialErrors (CReal -> SqrtType CReal
forall t. CanSqrt t => t -> SqrtType t
sqrt (CReal
piCReal -> CReal -> SubType CReal CReal
forall t1 t2. CanSub t1 t2 => t1 -> t2 -> SubType t1 t2
-CReal
piCReal -> Integer -> SubType CReal Integer
forall t1 t2. CanSub t1 t2 => t1 -> t2 -> SubType t1 t2
-Integer
2Integer -> Integer -> PowType Integer Integer
forall t1 t2. CanPow t1 t2 => t1 -> t2 -> PowType t1 t2
^(-Integer
1000)))
detectCN :: CN.CanTestErrorsPresent a => a -> Maybe a
detectCN :: a -> Maybe a
detectCN a
r = if Bool -> NegType Bool
forall t. CanNeg t => t -> NegType t
not (a -> Bool
forall es. CanTestErrorsPresent es => es -> Bool
CN.hasError a
r) then a -> Maybe a
forall a. a -> Maybe a
Just a
r else Maybe a
forall a. Maybe a
Nothing
fact :: Integer -> CReal
fact :: Integer -> CReal
fact Integer
n = Integer -> CReal
forall t. CanBeCReal t => t -> CReal
creal (Integer -> CReal) -> Integer -> CReal
forall a b. (a -> b) -> a -> b
$ [Integer] -> Integer
forall t.
(CanMulSameType t, ConvertibleExactly Integer t) =>
[t] -> t
product [Integer
1..Integer
n]
e_sum :: Integer -> CReal
e_sum :: Integer -> CReal
e_sum Integer
n = [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 -> 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
fact) [Integer
0..Integer
n]
my_e :: CReal
my_e :: CReal
my_e = (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) -> Integer -> CReal
e_sum (Integer
nInteger -> Integer -> AddType Integer Integer
forall t1 t2. CanAddAsymmetric t1 t2 => t1 -> t2 -> AddType t1 t2
+Integer
2)
e_sum2 :: Integer -> CReal
e_sum2 :: Integer -> CReal
e_sum2 Integer
n = (CReal -> Integer -> CReal) -> CReal -> [Integer] -> CReal
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl CReal -> Integer -> CReal
forall t1 t2.
(CanAddAsymmetric Integer (DivType t1 t2), CanDiv t1 t2) =>
t1 -> t2 -> AddType Integer (DivType t1 t2)
aux (Integer -> CReal
forall t. CanBeCReal t => t -> CReal
creal Integer
1) ([Integer] -> CReal) -> [Integer] -> CReal
forall a b. (a -> b) -> a -> b
$ [Integer] -> [Integer]
forall a. [a] -> [a]
reverse [Integer
1..Integer
n]
where aux :: t1 -> t2 -> AddType Integer (DivType t1 t2)
aux t1
x t2
m = Integer
1 Integer -> DivType t1 t2 -> AddType Integer (DivType t1 t2)
forall t1 t2. CanAddAsymmetric t1 t2 => t1 -> t2 -> AddType t1 t2
+ t1
x t1 -> t2 -> DivType t1 t2
forall t1 t2. CanDiv t1 t2 => t1 -> t2 -> DivType t1 t2
/ t2
m
my_e2 :: CReal
my_e2 :: CReal
my_e2 = (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) -> Integer -> CReal
e_sum2 (Integer
nInteger -> Integer -> AddType Integer Integer
forall t1 t2. CanAddAsymmetric t1 t2 => t1 -> t2 -> AddType t1 t2
+Integer
2)
absQ :: Rational -> Rational
absQ :: Rational -> Rational
absQ Rational
x = if Rational
x Rational -> Integer -> OrderCompareType Rational Integer
forall a b.
HasOrderAsymmetric a b =>
a -> b -> OrderCompareType a b
< Integer
0 then -Rational
x else Rational
x
absR1 :: CReal -> CReal
absR1 :: CReal -> CReal
absR1 CReal
x = if CReal
x CReal -> Integer -> OrderCompareType CReal Integer
forall a b.
HasOrderAsymmetric a b =>
a -> b -> OrderCompareType a b
< Integer
0 then -CReal
x else CReal
x
pif_run1 :: CReal
pif_run1 = CReal -> CReal
absR1 (CReal
piCReal -> CReal -> SubType CReal CReal
forall t1 t2. CanSub t1 t2 => t1 -> t2 -> SubType t1 t2
-CReal
pi)
absR2_approx :: t
-> Rational
-> IfThenElseType (SelectType (OrderCompareType t Rational)) t
absR2_approx t
x (Rational
q :: Rational) = if OrderCompareType t Rational
-> OrderCompareType t Rational
-> SelectType (OrderCompareType t Rational)
forall k. CanSelect k => k -> k -> SelectType k
select (t
x t -> Rational -> OrderCompareType t Rational
forall a b.
HasOrderAsymmetric a b =>
a -> b -> OrderCompareType a b
> -Rational
q) (t
x t -> Rational -> OrderCompareType t Rational
forall a b.
HasOrderAsymmetric a b =>
a -> b -> OrderCompareType a b
< Rational
q) then t
x else -t
x
absR2 :: CReal -> CReal
absR2 :: CReal -> CReal
absR2 CReal
x = (Rational -> CReal) -> LimitType Rational CReal
forall ix s. HasLimits ix s => (ix -> s) -> LimitType ix s
limit ((Rational -> CReal) -> LimitType Rational CReal)
-> (Rational -> CReal) -> LimitType Rational CReal
forall a b. (a -> b) -> a -> b
$ CReal
-> Rational
-> IfThenElseType
(SelectType (OrderCompareType CReal Rational)) CReal
forall t.
(HasIfThenElse (SelectType (OrderCompareType t Rational)) t,
CanSelect (OrderCompareType t Rational),
HasOrderAsymmetric t Rational, CanNeg t, NegType t ~ t) =>
t
-> Rational
-> IfThenElseType (SelectType (OrderCompareType t Rational)) t
absR2_approx CReal
x
select_run1 :: CReal
select_run1 = CReal -> CReal
absR2 (CReal
piCReal -> CReal -> SubType CReal CReal
forall t1 t2. CanSub t1 t2 => t1 -> t2 -> SubType t1 t2
-CReal
pi)
logistic1 :: _ => Rational -> Integer -> t -> t
logistic1 :: Rational -> Integer -> t -> t
logistic1 Rational
c Integer
n t
x0 =
(((t -> t) -> (t -> t) -> t -> t) -> [t -> t] -> t -> t
forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
foldl1 (t -> t) -> (t -> t) -> t -> t
forall b c a. (b -> c) -> (a -> b) -> a -> c
(.) (Integer -> (t -> t) -> [t -> t]
forall n a. CanBeInteger n => n -> a -> [a]
replicate Integer
n t -> t
t -> MulType (MulType Rational t) (SubType Integer t)
lg)) t
x0
where
lg :: t -> MulType (MulType Rational t) (SubType Integer t)
lg t
x = Rational
c Rational -> t -> MulType Rational t
forall t1 t2. CanMulAsymmetric t1 t2 => t1 -> t2 -> MulType t1 t2
* t
x MulType Rational t
-> SubType Integer t
-> MulType (MulType Rational t) (SubType Integer t)
forall t1 t2. CanMulAsymmetric t1 t2 => t1 -> t2 -> MulType t1 t2
* (Integer
1Integer -> t -> SubType Integer t
forall t1 t2. CanSub t1 t2 => t1 -> t2 -> SubType t1 t2
-t
x)
logistic1_CReal_run :: Integer -> CReal
logistic1_CReal_run :: Integer -> CReal
logistic1_CReal_run Integer
n = Rational -> Integer -> CReal -> CReal
forall t.
(CanMulAsymmetric (MulType Rational t) (SubType Integer t),
CanMulAsymmetric Rational t, CanSub Integer t,
MulType (MulType Rational t) (SubType Integer t) ~ t) =>
Rational -> Integer -> t -> t
logistic1 Rational
3.82 Integer
n (Rational -> CReal
forall t. CanBeCReal t => t -> CReal
creal Rational
0.5)