Free cookie consent management tool by TermsFeed Policy Generator

Changeset 16758


Ignore:
Timestamp:
04/04/19 13:30:57 (6 years ago)
Author:
gkronber
Message:

#2966: added support for tanh to interval interpreter and added unit tests for tan and tanh

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/IntervalInterpreter.cs

    r16740 r16758  
    216216            var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals);
    217217            result = Interval.Tangens(argumentInterval);
     218            break;
     219          }
     220        case OpCodes.Tanh: {
     221            var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals);
     222            result = Interval.HyperbolicTangent(argumentInterval);
    218223            break;
    219224          }
  • trunk/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval.cs

    r16757 r16758  
    171171    public static Interval Tangens(Interval a) {
    172172      return Interval.Divide(Interval.Sine(a), Interval.Cosine(a));
     173    } 
     174    public static Interval HyperbolicTangent(Interval a) {
     175      return new Interval(Math.Tanh(a.LowerBound), Math.Tanh(a.UpperBound));
    173176    }
    174177
  • trunk/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4/IntervalInterpreterTest.cs

    r16757 r16758  
    3434    }
    3535
    36     private void EvaluateTest(string expression, Interval expectedResult, Dictionary<string, Interval> variableRanges = null) {
     36    private void EvaluateTest(string expression, Interval expectedResult, Dictionary<string, Interval> variableRanges = null, double lowerDelta =0, double upperDelta = 0) {
    3737      var parser = new InfixExpressionParser();
    3838      var tree = parser.Parse(expression);
     
    4444        result = interpreter.GetSymbolicExpressionTreeInterval(tree, variableRanges);
    4545
    46       Assert.AreEqual(expectedResult, result);
     46      Assert.AreEqual(expectedResult.LowerBound, result.LowerBound, lowerDelta);
     47      Assert.AreEqual(expectedResult.UpperBound, result.UpperBound, upperDelta);
    4748    }
    4849
     
    120121    [TestCategory("Problems.DataAnalysis.Symbolic")]
    121122    [TestProperty("Time", "short")]
     123    public void TestIntervalInterpreterTan() {
     124      // critical values:
     125      // lim tan(x) = -inf for x => -pi/2
     126      // lim tan(x) = +inf for x =>  pi/2
     127      var variableRanges = new Dictionary<string, Interval>();
     128      variableRanges.Add("x1", new Interval(-1, 1));
     129      variableRanges.Add("x2", new Interval(-(Math.PI / 2), 0));
     130      variableRanges.Add("x3", new Interval(0, Math.PI / 2));
     131      variableRanges.Add("x4", new Interval(-Math.PI, Math.PI));
     132
     133      EvaluateTest("tan(x1)", new Interval(Math.Tan(-1), Math.Tan(1)), variableRanges, 1E-8, 1E-8);
     134      EvaluateTest("tan(x2)", new Interval(double.NegativeInfinity, 0), variableRanges, 0, 1E-8);
     135      EvaluateTest("tan(x3)", new Interval(0, 8.16588936419192E+15), variableRanges, 0, 1E6); // actually upper bound should be infinity.
     136      EvaluateTest("tan(x4)", new Interval(double.NegativeInfinity, double.PositiveInfinity), variableRanges);
     137    }
     138
     139    [TestMethod]
     140    [TestCategory("Problems.DataAnalysis.Symbolic")]
     141    [TestProperty("Time", "short")]
     142    public void TestIntervalInterpreterTanh() {
     143      // critical values:
     144      // lim tanh(x) = -1 for x => -inf
     145      // lim tanh(x) =  1 for x =>  inf
     146      var variableRanges = new Dictionary<string, Interval>();
     147      variableRanges.Add("x1", new Interval(-1, 1));
     148      variableRanges.Add("x2", new Interval(double.NegativeInfinity, 0));
     149      variableRanges.Add("x3", new Interval(0, double.PositiveInfinity));
     150
     151      EvaluateTest("tanh(x1)", new Interval(Math.Tanh(-1), Math.Tanh(1)), variableRanges);
     152      EvaluateTest("tanh(x2)", new Interval(-1, 0), variableRanges);
     153      EvaluateTest("tanh(x3)", new Interval(0, 1), variableRanges);
     154    }
     155
     156
     157    [TestMethod]
     158    [TestCategory("Problems.DataAnalysis.Symbolic")]
     159    [TestProperty("Time", "short")]
    122160    public void TestIntervalInterpreterExp() {
    123161      EvaluateTest("exp(x1-x2)", new Interval(Math.Exp(-3), Math.Exp(6)));
Note: See TracChangeset for help on using the changeset viewer.