Commit 900dca13 authored by Anastasiia's avatar Anastasiia

assign one delta for each variable and reuse it in bodyDelta expression

parent 903428da
......@@ -98,7 +98,8 @@ object RelativeErrorPhase extends DaisyPhase with Taylor with ErrorFunctions{
}
val bodyReal = fnc.body.get
val bodyDeltaAbs = deltaAbstract(bodyReal)
val deltaVarMap = mapDeltasToVars(bodyReal)
val bodyDeltaAbs = deltaAbstract(bodyReal, deltaVarMap)
// Step 1: disregard initial errors for now
// (f(x) - fl(x))/ f(x)
......@@ -260,7 +261,7 @@ object RelativeErrorPhase extends DaisyPhase with Taylor with ErrorFunctions{
reporter.debug(mapEntry._1 + " -> " + mapEntry._2)
}
reporter.debug(s"We need to evaluate expression on "+newSet.size+" intervals")
reporter.debug("there are " + deltasOf(relErrorExpr).size+ " deltas")
val taylorFirst = getDerivative(relErrorExpr)
// reporter.warning(s"the taylor expression we got is ")
// taylorFirst.foreach(x=>{reporter.debug(s"term is $x")})
......@@ -272,9 +273,10 @@ object RelativeErrorPhase extends DaisyPhase with Taylor with ErrorFunctions{
val errForSum = taylorFirst.map(x => {
val (expr, wrt) = x
val tmpExpr = simplify(Times(replaceDeltasWithZeros(expr), Delta(wrt)))
reporter.debug(s"Evaluate the term $tmpExpr")
val tmpForMax = newSet.par.map(interval => {
val tmp = evaluateOpt(tmpExpr, interval, rangeMethod)
reporter.debug("err on "+removeDeltasFromMap(interval)+s" is $tmp")
if (tmp.isEmpty)
if (!listFailInterval.contains(interval)) listFailInterval = listFailInterval :+ interval
tmp
......
......@@ -65,8 +65,10 @@ object TaylorErrorPhase extends DaisyPhase with Taylor with ErrorFunctions {
reporter.info("analyzing fnc: " + fnc.id)
val startTime = System.currentTimeMillis
val bodyReal = fnc.body.get
val deltaVarMap = mapDeltasToVars(bodyReal)
// derive f~(x)
val bodyDelta = deltaAbstract(fnc.body.get)
val bodyDelta = deltaAbstract(bodyReal, deltaVarMap)
// get set of partial derivatives wrt deltas
val taylor = getDerivative(bodyDelta)
......
......@@ -599,12 +599,21 @@ trait Taylor{
tmp
}
def deltaAbstract(e: Expr): Expr = (e: @unchecked) match {
def mapDeltasToVars(e: Expr):Map[Variable, Delta] = {
variablesOf(e).map(x =>{
(Variable(x) -> getADelta)
}).toMap
}
def deltaAbstract(e: Expr, deltaToVarMap: Map[Variable, Delta]): Expr = (e: @unchecked) match {
// x -> x * (1 + delta)
case x: Variable if trackRoundoffErrs =>
// initial error
Times(x, Plus(one, getADelta))
if (deltaToVarMap.contains(x))
Times(x, Plus(one, deltaToVarMap(x)))
else
Times(x, Plus(one, getADelta))
case x: Variable =>
// no initial error
......@@ -618,48 +627,48 @@ trait Taylor{
// x - > x ## negation does not incur any error
case UMinus(x) =>
UMinus(deltaAbstract(x))
UMinus(deltaAbstract(x, deltaToVarMap))
// (x + y) -> (x + y)(1 + delta)
case z @ Plus(x, y) =>
val xDelta = deltaAbstract(x)
val yDelta = deltaAbstract(y)
val xDelta = deltaAbstract(x, deltaToVarMap)
val yDelta = deltaAbstract(y, deltaToVarMap)
Times(Plus(xDelta, yDelta),
Plus(one,
getADelta))
// (x - y) -> (x - y)(1 + delta)
case z @ Minus(x, y) =>
val xDelta = deltaAbstract(x)
val yDelta = deltaAbstract(y)
val xDelta = deltaAbstract(x, deltaToVarMap)
val yDelta = deltaAbstract(y, deltaToVarMap)
Times(Minus(xDelta, yDelta),
Plus(one,
getADelta))
// (x * y) -> (x * y)(1 + delta)
case z @ Times(x, y) =>
val xDelta = deltaAbstract(x)
val yDelta = deltaAbstract(y)
val xDelta = deltaAbstract(x, deltaToVarMap)
val yDelta = deltaAbstract(y, deltaToVarMap)
Times(Times(xDelta, yDelta),
Plus(one,
getADelta))
// (x / y) -> ( x / y)(1 + delta)
case z @ Division(x, y) =>
val xDelta = deltaAbstract(x)
val yDelta = deltaAbstract(y)
val xDelta = deltaAbstract(x, deltaToVarMap)
val yDelta = deltaAbstract(y, deltaToVarMap)
Times(Division(xDelta, yDelta),
Plus(one,
getADelta))
// sqrt(x) = sqrt(x)(1 + delta)
case z @ Sqrt(x) =>
Times(Sqrt(deltaAbstract(x)),
Times(Sqrt(deltaAbstract(x, deltaToVarMap)),
Plus(one, getADelta))
case z @ Let(x, value, body) =>
Let(x, deltaAbstract(value), deltaAbstract(body))
Let(x, deltaAbstract(value, deltaToVarMap), deltaAbstract(body, deltaToVarMap))
case z =>
throw new IllegalArgumentException(s"Unknown expression $z. Parsing failed")
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment