# source:trunk/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis-3.4/IntervalTest.cs@16631

Last change on this file since 16631 was 16631, checked in by gkronber, 12 months ago

#2966: fixed calculation for intervals of Sqr, Sqrt, Cube, CubeRoot

File size: 7.6 KB
Line
1using System;
2using Microsoft.VisualStudio.TestTools.UnitTesting;
3
4namespace HeuristicLab.Problems.DataAnalysis.Tests {
5  [TestClass]
6  public class IntervalTest {
7    private readonly Interval a = new Interval(-1, 1);
8    private readonly Interval b = new Interval(-2, 2);
9    private readonly Interval c = new Interval(0, 3);
10    private readonly Interval d = new Interval(1, 3);
11    private readonly Interval e = new Interval(4, 6);
12
13    [TestMethod]
14    [TestCategory("Problems.DataAnalysis")]
15    [TestProperty("Time", "short")]
17      //add        [x1,x2] + [y1,y2] = [x1 + y1,x2 + y2]
18
19      // [-1,1] + [-2,2] = [-3,3]
20      Assert.AreEqual(Interval.Add(a, b), new Interval(-3, 3));
21      //([-1, 1] + [-2, 2]) + [0, 3] = [-3, 6]
23      //([-1, 1] + [0, 3]) + [-2, 2] = [-3, 6]
25    }
26
27    [TestMethod]
28    [TestCategory("Problems.DataAnalysis")]
29    [TestProperty("Time", "short")]
30    public void TestIntervalSubOperation() {
31      //subtract   [x1,x2] − [y1,y2] = [x1 − y2,x2 − y1]
32
33      //[-1, 1] - [-2, 2] = [-3, 3]
34      Assert.AreEqual<Interval>(Interval.Subtract(a, b), new Interval(-3, 3));
35      //([-1, 1] - [-2, 2]) - [0, 3] = [-6, 3]
36      Assert.AreEqual<Interval>(Interval.Subtract(Interval.Subtract(a, b), c), new Interval(-6, 3));
37      //([-1, 1] - [0, 3]) - [-2, 2] = [-6, 3]
38      Assert.AreEqual<Interval>(Interval.Subtract(Interval.Subtract(a, c), b), new Interval(-6, 3));
39    }
40
41    [TestMethod]
42    [TestCategory("Problems.DataAnalysis")]
43    [TestProperty("Time", "short")]
44    public void TestIntervalMutlipyOperation() {
45      //multiply   [x1,x2] * [y1,y2] = [min(x1*y1,x1*y2,x2*y1,x2*y2),max(x1*y1,x1*y2,x2*y1,x2*y2)]
46
47      //[-1, 1] * [-2, 2] = [-2, 2]
48      Assert.AreEqual<Interval>(Interval.Multiply(a, b), new Interval(-2, 2));
49      //([-1, 1] * [-2, 2]) * [0, 3] = [-6, 6]
50      Assert.AreEqual<Interval>(Interval.Multiply(Interval.Multiply(a, b), c), new Interval(-6, 6));
51      //([-1, 1] * [0, 3]) * [-2, 2] = [-6, 6]
52      Assert.AreEqual<Interval>(Interval.Multiply(Interval.Multiply(a, c), b), new Interval(-6, 6));
53    }
54
55    [TestMethod]
56    [TestCategory("Problems.DataAnalysis")]
57    [TestProperty("Time", "short")]
58    public void TestIntervalDivideOperation() {
59      //divide  [x1, x2] / [y1, y2] = [x1, x2] * (1/[y1, y2]), where 1 / [y1,y2] = [1 / y2,1 / y1] if 0 not in [y_1, y_2].
60
61      //[4, 6] / [1, 3] = [4/3, 6]
62      Assert.AreEqual<Interval>(Interval.Divide(e, d), new Interval(4.0 / 3.0, 6));
63      //([4, 6] / [1, 3]) / [1, 3] = [4/9, 6]
64      Assert.AreEqual<Interval>(Interval.Divide(Interval.Divide(e, d), d), new Interval(4.0 / 9.0, 6));
65      //[4, 6] / [0, 3] = [4/3, +Inf]
66      Assert.AreEqual<Interval>(Interval.Divide(e, c), new Interval(4.0 / 3.0, double.PositiveInfinity));
67      //[-1, 1] / [0, 3] = [+Inf, -Inf]
68      Assert.AreEqual<Interval>(Interval.Divide(a, c), new Interval(double.NegativeInfinity, double.PositiveInfinity));
69      //Devision by 0 ==> IsInfiniteOrUndefined == true
70      Assert.IsTrue(Interval.Divide(e, c).IsInfiniteOrUndefined);
71      //Devision by 0 ==> IsInfiniteOrUndefined == true
72      Assert.IsTrue(Interval.Divide(a, c).IsInfiniteOrUndefined);
73      Assert.AreEqual<Interval>(Interval.Divide(d, b), new Interval(double.NegativeInfinity, double.PositiveInfinity));
74    }
75
76    [TestMethod]
77    [TestCategory("Problems.DataAnalysis")]
78    [TestProperty("Time", "short")]
79    public void TestIntervalSineOperator() {
80      //sine depends on interval
81      //sin([0, 2*pi]) = [-1, 1]
82      Assert.AreEqual<Interval>(Interval.Sine(new Interval(0, 2 * Math.PI)), new Interval(-1, 1));
83      //sin([-pi/2, pi/2]) = [sin(-pi/2), sin(pi/2)]
84      Assert.AreEqual<Interval>(Interval.Sine(new Interval(-1 * Math.PI / 2, Math.PI / 2)), new Interval(-1, 1));
85      //sin([0, pi/2]) = [sin(0), sin(pi/2)]
86      Assert.AreEqual<Interval>(Interval.Sine(new Interval(0, Math.PI / 2)), new Interval(0, 1));
87      //sin([pi, 3*pi/2]) = [sin(pi), sin(3*pi/2)]
88      Assert.AreEqual<Interval>(Interval.Sine(new Interval(Math.PI, 3 * Math.PI / 2)), new Interval(-1, 0));
89      Assert.AreEqual<Interval>(Interval.Sine(new Interval(1, 2)), new Interval(Math.Min(Math.Sin(1), Math.Sin(2)), 1));
90      Assert.AreEqual<Interval>(Interval.Sine(new Interval(1, 3)), new Interval(Math.Min(Math.Sin(1), Math.Sin(3)), 1));
91      Assert.AreEqual<Interval>(Interval.Sine(new Interval(Math.PI, 5 * Math.PI / 2)), new Interval(-1, 1));
92    }
93
94    [TestMethod]
95    [TestCategory("Problems.DataAnalysis")]
96    [TestProperty("Time", "short")]
97    public void TestIntervalCosineOperator() {
98      //Cosine uses sine Interval.Sine(Interval.Subtract(a, new Interval(Math.PI / 2, Math.PI / 2)));
99      Assert.AreEqual<Interval>(Interval.Cosine(new Interval(0, 2 * Math.PI)), new Interval(-1, 1));
100      Assert.AreEqual<Interval>(new Interval(-1, 1), Interval.Cosine(new Interval(Math.PI, 4 * Math.PI / 2)));
101    }
102
103    [TestMethod]
104    [TestCategory("Problems.DataAnalysis")]
105    [TestProperty("Time", "short")]
106    public void TestIntervalLogOperator() {
107      //Log([3, 5]) = [log(3), log(5)]
108      Assert.AreEqual<Interval>(new Interval(Math.Log(3), Math.Log(5)), Interval.Logarithm(new Interval(3, 5)));
109      //Log([0.5, 1]) = [log(0.5), log(1)]
110      Assert.AreEqual<Interval>(new Interval(Math.Log(0.5), 0), Interval.Logarithm(new Interval(0.5, 1)));
111      //Log([-1, 5]) = [NaN, log(5)]
112      var result = Interval.Logarithm(new Interval(-1, 5));
113      Assert.AreEqual<Interval>(new Interval(double.NaN, Math.Log(5)),result);
114      Assert.IsTrue(result.IsInfiniteOrUndefined);
115    }
116
117
118    [TestMethod]
119    [TestCategory("Problems.DataAnalysis")]
120    [TestProperty("Time", "short")]
121    public void TestIntervalExpOperator() {
122      //Exp([0, 1]) = [exp(0), exp(1)]
123      Assert.AreEqual<Interval>(new Interval(1, Math.Exp(1)), Interval.Exponential(new Interval(0, 1)));
124    }
125
126
127    [TestMethod]
128    [TestCategory("Problems.DataAnalysis")]
129    [TestProperty("Time", "short")]
130    public void TestIntervalSqrOperator() {
131      Assert.AreEqual<Interval>(new Interval(1, 4), Interval.Square(new Interval(1, 2)));
132      Assert.AreEqual<Interval>(new Interval(1, 4), Interval.Square(new Interval(-2, -1)));
133      Assert.AreEqual<Interval>(new Interval(0, 4), Interval.Square(new Interval(-2, 2)));
134    }
135
136    [TestMethod]
137    [TestCategory("Problems.DataAnalysis")]
138    [TestProperty("Time", "short")]
139    public void TestIntervalSqrtOperator() {
140      Assert.AreEqual<Interval>(new Interval(1, 2), Interval.SquareRoot(new Interval(1, 4)));
141      Assert.AreEqual<Interval>(new Interval(double.NaN, double.NaN), Interval.SquareRoot(new Interval(-4, -1)));
142    }
143
144    [TestMethod]
145    [TestCategory("Problems.DataAnalysis")]
146    [TestProperty("Time", "short")]
147    public void TestIntervalCubeOperator() {
148      Assert.AreEqual<Interval>(new Interval(1, 8), Interval.Cube(new Interval(1, 2)));
149      Assert.AreEqual<Interval>(new Interval(-8, -1), Interval.Cube(new Interval(-2, -1)));
150      Assert.AreEqual<Interval>(new Interval(-8, 8), Interval.Cube(new Interval(-2, 2)));
151    }
152
153    [TestMethod]
154    [TestCategory("Problems.DataAnalysis")]
155    [TestProperty("Time", "short")]
156    public void TestIntervalCbrtOperator() {
157      Assert.AreEqual<Interval>(new Interval(1, 2), Interval.CubicRoot(new Interval(1, 8)));
158      Assert.AreEqual<Interval>(new Interval(double.NaN, double.NaN), Interval.CubicRoot(new Interval(-8, -1)));
159    }
160  }
161}
Note: See TracBrowser for help on using the repository browser.