module Data.Number.ER.RnToRm.UnitDom.ChebyshevBase.Polynom.Elementary
where
import Data.Number.ER.RnToRm.UnitDom.ChebyshevBase.Polynom.Basic
import Data.Number.ER.RnToRm.UnitDom.ChebyshevBase.Polynom.Eval
import Data.Number.ER.RnToRm.UnitDom.ChebyshevBase.Polynom.Field
import Data.Number.ER.RnToRm.UnitDom.ChebyshevBase.Polynom.Bounds
import qualified Data.Number.ER.Real.Approx as RA
import qualified Data.Number.ER.Real.Approx.Elementary as RAEL
import qualified Data.Number.ER.Real.Base as B
import Data.Number.ER.Real.Approx.Interval
import Data.Number.ER.Real.Arithmetic.Elementary
import qualified Data.Number.ER.Real.DomainBox as DBox
import Data.Number.ER.Real.DomainBox (VariableID(..), DomainBox, DomainIntBox)
import Data.Number.ER.BasicTypes
import Data.Number.ER.Misc
import qualified Data.Map as Map
chplSqrt ::
(B.ERRealBase b, RealFrac b, DomainBox box varid Int, Ord box) =>
Int ->
EffortIndex ->
ERChebPoly box b ->
(ERChebPoly box b, ERChebPoly box b)
chplSqrt maxDegree ix p =
error "ERChebPoly: chplSqrt: not implemented yet"
chplExp ::
(B.ERRealBase b, RealFrac b, DomainBox box varid Int, Ord box) =>
Int ->
EffortIndex ->
ERChebPoly box b ->
(ERChebPoly box b, ERChebPoly box b)
chplExp maxDegree ix p =
(expDownwards, expUpwards + valueAtRDnNeg + (chplConst expRUp))
where
expUpwards =
(chplConst expMUp) * (chplPow maxDegree (expNear0Up pNear0Up) a_int)
expDownwards =
(chplConst expMDn) * (chplPow maxDegree (expNear0Dn pNear0Dn) a_int)
upperB = chplUpperBoundAffine ix p
lowerB = (chplUpperBoundAffine ix ( p))
m = (upperB + lowerB) / 2
r = (upperB lowerB) / 2
expMUp = erintv_right expM
expMDn = erintv_left expM
expM = erExp_R ix (ERInterval m m)
pNear0Up = (p (chplConst m)) * (chplConst $ recip a_base)
pNear0Dn = (((p) + (chplConst m)) * (chplConst $ recip a_base))
a_base = fromInteger a_int
a_int = max 1 $ floor r
expNear0Up p0 =
expAux p0 1 (B.setGranularity coeffGr 1)
expNear0Dn p0 =
negate $ expAux p0 1 (B.setGranularity coeffGr (1))
expAux p0 nextDegree thisCoeff
| nextDegree > taylorDegree =
chplConst thisCoeff
| otherwise =
snd $ chplReduceDegree maxDegree $
(chplConst thisCoeff) + p0 * (expAux p0 (nextDegree + 1) nextCoeff)
where
nextCoeff =
thisCoeff / (fromInteger nextDegree)
taylorDegree = 1 + 2 * (ix `div` 6)
coeffGr = effIx2gran $ 10 + 3 * taylorDegree
expRUp = erintv_right expR
expR = erExp_R ix (ERInterval r r)
valueAtRDnNeg =
expAux (chplConst r) 1 (B.setGranularity coeffGr (1))
chplPow ::
(B.ERRealBase b, Integral i, DomainBox box varid Int, Ord box) =>
Int ->
ERChebPoly box b ->
i ->
ERChebPoly box b
chplPow maxDegree p n
| n == 0 =
chplConst 1
| n == 1 =
p
| even n =
snd $ chplReduceDegree maxDegree $ powHalfN * powHalfN
| odd n =
snd $ chplReduceDegree maxDegree $
p *
(snd $ chplReduceDegree maxDegree $
powHalfN * powHalfN)
where
powHalfN =
chplPow maxDegree p halfN
halfN = n `div` 2
chplLog ::
(B.ERRealBase b, RealFrac b, DomainBox box varid Int, Ord box) =>
Int ->
EffortIndex ->
ERChebPoly box b ->
(ERChebPoly box b, ERChebPoly box b)
chplLog maxDegree ix p =
error "ERChebPoly: chplLog: not implemented yet"
chplSineCosine ::
(B.ERRealBase b, RealFrac b, DomainBox box varid Int, Ord box) =>
Bool ->
Int ->
EffortIndex ->
ERChebPoly box b ->
(ERChebPoly box b, ERChebPoly box b)
chplSineCosine isSine maxDegree ix p
| ranfNear0 `RA.refines` plusMinusPiHalf =
case isSine of
True -> sineShifted ( k2pi)
False -> cosineShifted ( k2pi)
| (ranfNear0 piHalf) `RA.refines` plusMinusPiHalf =
case isSine of
True -> cosineShifted ( k2pi piHalf)
False -> sineShiftedNegated ( k2pi piHalf)
| (ranfNear0 + piHalf) `RA.refines` plusMinusPiHalf =
case isSine of
True -> cosineShiftedNegated (k2pi + piHalf)
False -> sineShifted (k2pi + piHalf)
| (ranfNear0 pi) `RA.refines` plusMinusPiHalf =
case isSine of
True -> sineShiftedNegated ( k2pi pi)
False -> cosineShiftedNegated ( k2pi pi)
| otherwise = (chplConst (1), chplConst 1)
where
ranfNear0 = ranf k2pi
k2pi = k * 2 * pi
plusMinusPiHalf = (piHalfLO) RA.\/ piHalfLO
pi = RAEL.pi ix
piHalf = pi / 2
(piHalfLO, piHalfHI) = RA.bounds piHalf
ranf =
ERInterval
(negate $ chplUpperBoundAffine 10 (p))
(chplUpperBoundAffine 10 p)
k =
fromInteger $ floor $
case (pi + ranf) / (2 * pi) of ERInterval lo hi -> lo
sineShiftedNegated shift =
boundsNegate $ sineShifted shift
cosineShiftedNegated shift =
boundsNegate $ cosineShifted shift
boundsNegate (pLO, pHI) = ( pHI, pLO)
sineShifted shift =
boundsAddErr shiftWidthB $ sineTaylor (p + shiftPoly) (ranf + shift)
where
shiftPoly = chplConst shiftLOB
ERInterval shiftLOB shiftHIB = shift
shiftWidthB = shiftHIB shiftLOB
cosineShifted shift =
boundsAddErr shiftWidthB $ cosineTaylor (p + shiftPoly) (ranf + shift)
where
shiftPoly = chplConst shiftLOB
ERInterval shiftLOB shiftHIB = shift
shiftWidthB = shiftHIB shiftLOB
boundsAddErr errB (pLO, pHI) =
(pLO `plusDown` ( errPoly), pHI + errPoly)
where
errPoly = chplConst errB
sineTaylor x xran =
(sineDown, sineUp)
where
sineUp =
chplReduceDegreeUp maxDegree $
x * sineUpTaylor + (chplConst sineUpErrorBound)
(sineUpTaylor, sineUpErrorTermDegree, sineUpErrorTermCoeff) =
taylorAux x 1 (B.setGranularity coeffGr 1)
sineUpErrorBound =
case sineUpErrorBoundRA of ERInterval lo hi -> hi
where
sineUpErrorBoundRA =
(xranLargerEndpoint ^ (1 + sineUpErrorTermDegree)) * sineUpErrorTermCoeffRA
sineUpErrorTermCoeffRA =
abs $
ERInterval sineUpErrorTermCoeff sineUpErrorTermCoeff
sineDown =
negate $ chplReduceDegreeUp maxDegree $
x * sineDownTaylorNeg + (chplConst $ sineDownErrorBound)
(sineDownTaylorNeg, sineDownErrorTermDegree, sineDownErrorTermCoeff) =
taylorAux x 1 (B.setGranularity coeffGr (1))
sineDownErrorBound =
case sineDownErrorBoundRA of ERInterval lo hi -> hi
where
sineDownErrorBoundRA =
(xranLargerEndpoint ^ (1 + sineDownErrorTermDegree)) * sineDownErrorTermCoeffRA
sineDownErrorTermCoeffRA =
abs $
ERInterval sineDownErrorTermCoeff sineDownErrorTermCoeff
xranLargerEndpoint =
max (abs xranLO) (abs xranHI)
(xranLO, xranHI) = RA.bounds xran
cosineTaylor x xran =
(cosineDown, cosineUp)
where
cosineUp =
chplReduceDegreeUp maxDegree $
cosineUpTaylor + (chplConst cosineUpErrorBound)
(cosineUpTaylor, cosineUpErrorTermDegree, cosineUpErrorTermCoeff) =
taylorAux x 0 (B.setGranularity coeffGr 1)
cosineUpErrorBound
| odd (cosineUpErrorTermDegree `div` 2) = 0
| otherwise =
case cosineUpErrorBoundRA of ERInterval lo hi -> hi
where
cosineUpErrorBoundRA =
(xranLargerEndpoint ^ (cosineUpErrorTermDegree)) * cosineUpErrorTermCoeffRA
cosineUpErrorTermCoeffRA =
abs $
ERInterval cosineUpErrorTermCoeff cosineUpErrorTermCoeff
cosineDown =
negate $ chplReduceDegreeUp maxDegree $
cosineDownTaylorNeg + (chplConst $ cosineDownErrorBound)
(cosineDownTaylorNeg, cosineDownErrorTermDegree, cosineDownErrorTermCoeff) =
taylorAux x 0 (B.setGranularity coeffGr (1))
cosineDownErrorBound
| even (cosineDownErrorTermDegree `div` 2) = 0
| otherwise =
case cosineDownErrorBoundRA of ERInterval lo hi -> hi
where
cosineDownErrorBoundRA =
(xranLargerEndpoint ^ (cosineDownErrorTermDegree)) * cosineDownErrorTermCoeffRA
cosineDownErrorTermCoeffRA =
abs $
ERInterval cosineDownErrorTermCoeff cosineDownErrorTermCoeff
xranLargerEndpoint =
max (abs xranLO) (abs xranHI)
(xranLO, xranHI) = RA.bounds xran
taylorAux p0 thisDegree thisCoeff
| nextDegree > taylorDegree =
(chplConst thisCoeff, nextDegree, nextCoeff)
| otherwise =
(chplReduceDegreeUp maxDegree $
(chplConst thisCoeff) + p0 * p0 * rest,
errorTermDegree, errorTermCoeff)
where
(rest, errorTermDegree, errorTermCoeff) =
taylorAux p0 nextDegree nextCoeff
nextDegree = thisDegree + 2
nextCoeff =
thisCoeff / (fromInteger $ negate $ nextDegree * (nextDegree 1))
taylorDegree = ix `div` 3
coeffGr = effIx2gran $ ix
chplRecip ::
(B.ERRealBase b, RealFrac b, DomainBox box varid Int, Ord box) =>
Int ->
EffortIndex ->
ERChebPoly box b ->
(ERChebPoly box b, ERChebPoly box b)
chplRecip maxDegree ix p
| upperB < 0 =
(\(lo, hi) -> (hi, lo)) $ chplRecip maxDegree ix (negate p)
| lowerB > 0 =
(resDn, resUp)
| otherwise =
error $
"ERChebPoly: chplRecip: "
++ "cannot deal with estimated range " ++ show ranp
++ "of polynomial: \n" ++ show p
where
ranp = ERInterval lowerB upperB
lowerB = (chplUpperBoundAffine ix ( p))
upperB = chplUpperBoundAffine ix p
tauDegree = effIx2int (ix `div` 3)
coeffGr = effIx2gran $ ix
k = intLogUp 2 $ ceiling (1/lowerB)
upperBtr = upperB * 2^k
(pAbove1Dn, pAbove1Up) =
chplScale (2^k) p
trT1Dn =
(chplScaleDown nuLOB (pAbove1Dn 1)) 1
trT1Up =
(chplScaleUp nuHIB (pAbove1Up 1)) 1
nu = recip nuInv
ERInterval nuLOB nuHIB = nu
nuInv = (RA.setMinGranularity coeffGr (ERInterval upperBtr upperBtr)) / 2
nuPlus1 = nu + 1
nuInvPlus1 = nuInv + 1
nuInvDiv2 = nuInv / 2
trTis =
map (mapPair (chplReduceDegreeDown maxDegree, chplReduceDegreeUp maxDegree)) $
chebyEvalTsRoundDownUp trT1Dn
resDn =
chplScaleDown (2^k) $
(tauAbsUpPoly) `plusDown`
(chplScaleUp tauAbsDnB $
sumDown $
( errPoly) : (zipWith scaleDn cis trTis))
resUp =
chplScaleUp (2^k) $
(tauAbsUpPoly) `plusUp`
(chplScaleUp tauAbsUpB $
sumUp $
(errPoly) : (zipWith scaleUp cis trTis))
scaleDn c (trTDn, trTUp)
| r >= 0 = chplScaleDown r trTDn
| otherwise = chplScaleDown r trTUp
where
r = c * tauSign
scaleUp c (trTDn, trTUp)
| r >= 0 = chplScaleUp r trTUp
| otherwise = chplScaleUp r trTDn
where
r = c * tauSign
tauAbsUpPoly = chplConst $ tauAbsUpB
tauSign =
case RA.compareReals tauInv 0 of
Just GT -> 1
Just LT -> 1
ERInterval tauAbsDnB tauAbsUpB = abs $ recip tauInv
cis =
map (\(ERInterval lo hi) -> hi) c0n
errPoly = chplConst err
err =
foldl1 plusUp $
map (\(ERInterval lo hi) -> hi lo) c0n
c0n = c0 : c1n
tauInv = c0 * nuInvPlus1 + c1 * nuInvDiv2
c0 = c1 * nuPlus1 c2/2
(c1 : c2 : _) = c1n
c1n = reverse $ take n $ csRev
n = tauDegree
csRev =
cn : cnM1 : (csAux cn cnM1)
where
cn = 1
cnM1 = 2 * nuPlus1
csAux cn cnM1 =
cnM2 : (csAux cnM1 cnM2)
where
cnM2 = cn 2 * nuPlus1 * cnM1