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"
chplSine ::
(B.ERRealBase b, RealFrac b, DomainBox box varid Int, Ord box) =>
Int ->
EffortIndex ->
ERChebPoly box b ->
(ERChebPoly box b, ERChebPoly box b)
chplSine maxDegree ix p =
(sineDown, sineUp)
where
(sineDown, sineUp) =
boundsAddErr sineErrorBound $
chplThinTimesEncl maxDegree p (sineDownTaylor, sineUpTaylor)
((sineDownTaylor, sineUpTaylor),
sineErrorTermDegree,
(sineErrorTermCoeffDown, sineErrorTermCoeffUp)) =
sincosTaylorAux True (chplSquare maxDegree p) taylorDegree 1 (one, one)
one = B.setGranularity coeffGr 1
sineErrorBound =
case sineErrorBoundRA of ERInterval lo hi -> hi
where
sineErrorBoundRA =
(ranLargerEndpointRA ^ (sineErrorTermDegree)) * sineErrorTermCoeffRA
sineErrorTermCoeffRA =
ERInterval sineErrorTermCoeff sineErrorTermCoeff
sineErrorTermCoeff =
max (abs sineErrorTermCoeffDown) (abs sineErrorTermCoeffUp)
ranLargerEndpointRA =
ERInterval ranLargerEndpoint ranLargerEndpoint
ranLargerEndpoint =
max (abs ranLO) (abs ranHI)
ranLO = negate $ chplUpperBoundAffine ix (p)
ranHI = chplUpperBoundAffine ix p
taylorDegree = effIx2int $ ix `div` 3
coeffGr = effIx2gran $ ix
boundsAddErr errB (pLO, pHI) =
(pLO `plusDown` ( errPoly), pHI + errPoly)
where
errPoly = chplConst errB
chplCosine ::
(B.ERRealBase b, RealFrac b, DomainBox box varid Int, Ord box) =>
Int ->
EffortIndex ->
ERChebPoly box b ->
(ERChebPoly box b, ERChebPoly box b)
chplCosine maxDegree ix p =
(cosineDown, cosineUp)
where
(cosineDown, cosineUp) =
boundsAddErr cosineErrorBound $
(cosineDownTaylor, cosineUpTaylor)
((cosineDownTaylor, cosineUpTaylor),
cosineErrorTermDegree,
(cosineErrorTermCoeffDown, cosineErrorTermCoeffUp)) =
sincosTaylorAux True (chplSquare maxDegree p) taylorDegree 0 (one, one)
one = B.setGranularity coeffGr 1
cosineErrorBound =
case cosineErrorBoundRA of ERInterval lo hi -> hi
where
cosineErrorBoundRA =
(ranLargerEndpointRA ^ (cosineErrorTermDegree)) * cosineErrorTermCoeffRA
cosineErrorTermCoeffRA =
ERInterval cosineErrorTermCoeff cosineErrorTermCoeff
cosineErrorTermCoeff =
max (abs cosineErrorTermCoeffDown) (abs cosineErrorTermCoeffUp)
ranLargerEndpointRA =
ERInterval ranLargerEndpoint ranLargerEndpoint
ranLargerEndpoint =
max (abs ranLO) (abs ranHI)
ranLO = negate $ chplUpperBoundAffine ix (p)
ranHI = chplUpperBoundAffine ix p
taylorDegree = effIx2int $ ix `div` 3
coeffGr = effIx2gran $ ix
sincosTaylorAux ::
(B.ERRealBase b, RealFrac b, DomainBox box varid Int, Ord box) =>
Bool ->
(ERChebPoly box b, ERChebPoly box b) ->
Int ->
Int ->
(b,b) ->
((ERChebPoly box b, ERChebPoly box b),
Int,
(b,b))
sincosTaylorAux resultPositive pSquares@(pSquareDown, pSquareUp)
maxDegree thisDegree (thisCoeffDown, thisCoeffUp)
| nextDegree > maxDegree =
((thisCoeffDownP, thisCoeffUpP), nextDegree, (nextCoeffDown, nextCoeffUp))
| otherwise =
((resultDown, resultUp), errorTermDegree, errorTermCoeffs)
where
thisCoeffDownP = chplConst thisCoeffDown
thisCoeffUpP = chplConst thisCoeffUp
resultDown
| resultPositive =
chplReduceDegreeDown maxDegree $
thisCoeffDownP `plusDown` (pSquareUp `timesDown` restDown)
| otherwise =
chplReduceDegreeDown maxDegree $
thisCoeffDownP `plusDown` (pSquareDown `timesDown` restDown)
resultUp
| resultPositive =
chplReduceDegreeUp maxDegree $
thisCoeffUpP `plusUp` (pSquareDown `timesUp` restUp)
| otherwise =
chplReduceDegreeUp maxDegree $
thisCoeffUpP `plusUp` (pSquareUp `timesUp` restUp)
((restDown, restUp), errorTermDegree, errorTermCoeffs) =
sincosTaylorAux (not resultPositive) pSquares maxDegree nextDegree (nextCoeffDown, nextCoeffUp)
nextDegree = thisDegree + 2
nextCoeffUp
| resultPositive =
thisCoeffDown / nextCoeffDenominator
| otherwise =
thisCoeffUp / nextCoeffDenominator
nextCoeffDown
| resultPositive =
thisCoeffUp `divDown` nextCoeffDenominator
| otherwise =
thisCoeffDown `divDown` nextCoeffDenominator
nextCoeffDenominator =
fromInteger $ toInteger $ negate $ nextDegree * (nextDegree 1)
divDown a b = negate $ a / (negate b)
chplAtan ::
(B.ERRealBase b, RealFrac b, DomainBox box varid Int, Ord box) =>
Int ->
EffortIndex ->
ERChebPoly box b ->
(ERChebPoly box b, ERChebPoly box b)
chplAtan maxDegree ix p
| avoidingDivBy0 =
(resDn, resUp)
| otherwise =
(chplConst (2), chplConst 2)
where
avoidingDivBy0 =
(chplUpperBoundAffine ix ( pSquarePlus1Dn) < 0)
&&
(chplUpperBoundAffine ix ( pSquarePlus1Up) < 0)
resDn =
negate $
chplMaxUp maxDegree
(chplReduceDegreeUp maxDegree $
pOverPSquarePlus1DnNeg `timesUp` preresDn)
(chplReduceDegreeUp maxDegree $
pOverPSquarePlus1DnNeg `timesUp` preresUp)
where
pOverPSquarePlus1DnNeg = negate pOverPSquarePlus1Dn
resUp =
chplMaxUp maxDegree
(chplReduceDegreeUp maxDegree $
pOverPSquarePlus1Up `timesUp` preresDn)
(chplReduceDegreeUp maxDegree $
pOverPSquarePlus1Up `timesUp` preresUp)
(preresDn, preresUp) = seriesDnUp termsCount 2
termsCount = max 0 $ ix `div` 3
gran = effIx2gran ix
seriesDnUp termsCount coeffBase
| termsCount > 0 =
(
chplReduceDegreeDown maxDegree $
1 `plusDown`
(pSquareOverPSquarePlus1Dn
`timesDown` (chplConst coeffDn)
`timesDown` restDn
)
,
chplReduceDegreeUp maxDegree $
1 `plusUp`
(pSquareOverPSquarePlus1Up
`timesUp` (chplConst coeffUp)
`timesUp` restUp
)
)
| otherwise =
(
1 `plusDown` (pSquareDn `timesDown` (chplConst coeffDn))
,
1 `plusUp` pSquareUp
)
where
(restDn, restUp) = seriesDnUp (termsCount 1) (coeffBase + 2)
coeffUp = coeffBaseB / (coeffBaseB `plusDown` 1)
coeffDn = negate $ coeffBaseB / (negate $ coeffBaseB `plusUp` 1)
coeffBaseB = B.setMinGranularity gran $ fromInteger coeffBase
(pSquareDn, pSquareUp) = chplSquare maxDegree p
pSquarePlus1Dn = pSquareDn `plusDown` 1
pSquarePlus1Up = pSquareUp `plusUp` 1
recipPSquarePlus1Dn = chplRecipDn maxDegree ix pSquarePlus1Up
recipPSquarePlus1Up = chplRecipUp maxDegree ix pSquarePlus1Dn
pSquareOverPSquarePlus1Up =
pSquareUp `timesUp` recipPSquarePlus1Up
pSquareOverPSquarePlus1Dn =
pSquareDn `timesDown` recipPSquarePlus1Dn
pOverPSquarePlus1Up =
chplMaxUp maxDegree
(p `timesUp` recipPSquarePlus1Up)
(p `timesUp` recipPSquarePlus1Dn)
pOverPSquarePlus1Dn =
negate $
chplMaxUp maxDegree
(pn `timesUp` recipPSquarePlus1Up)
(pn `timesUp` recipPSquarePlus1Dn)
where
pn = negate p
chplRecipDn m i = fst . chplRecip m i
chplRecipUp m i = snd . chplRecip m i
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@(ERChebPoly pCoeffs)
| pIsConst =
(chplConst $ (recip ( pConst)), chplConst $ recip pConst)
| 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
(pIsConst, pConst) =
case chplGetConst p of
Nothing -> (False, error "ChebyshevBase.Polynom.Elementary.chplRecip")
Just coeff -> (True, coeff)
tauDegree = effIx2int (max 2 $ 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