Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2994-AutoDiffForIntervals/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis-3.4/AlgebraicIntervalTest.cs @ 17877

Last change on this file since 17877 was 17318, checked in by gkronber, 5 years ago

#2994: refactor / cleanup of unit tests related to intervals and new interpreters

File size: 11.5 KB
Line 
1using System;
2using HeuristicLab.Problems.DataAnalysis.Symbolic;
3using Microsoft.VisualStudio.TestTools.UnitTesting;
4
5namespace HeuristicLab.Problems.DataAnalysis.Tests {
6  [TestClass]
7  public class AlgebraicIntervalTest {
8    private readonly AlgebraicInterval a = new AlgebraicInterval(-1, 1);
9    private readonly AlgebraicInterval b = new AlgebraicInterval(-2, 2);
10    private readonly AlgebraicInterval c = new AlgebraicInterval(0, 3);
11    private readonly AlgebraicInterval d = new AlgebraicInterval(1, 3);
12    private readonly AlgebraicInterval e = new AlgebraicInterval(4, 6);
13
14    [TestMethod]
15    [TestCategory("Problems.DataAnalysis")]
16    [TestProperty("Time", "short")]
17    public void AlgebraicIntervalAdd() {
18      //add        [x1,x2] + [y1,y2] = [x1 + y1,x2 + y2]
19
20      // [-1,1] + [-2,2] = [-3,3]
21      AssertAreEqualInterval(Add(a, b), new AlgebraicInterval(-3, 3));
22      //([-1, 1] + [-2, 2]) + [0, 3] = [-3, 6]
23      AssertAreEqualInterval(Add(Add(a, b), c), new AlgebraicInterval(-3, 6));
24      //([-1, 1] + [0, 3]) + [-2, 2] = [-3, 6]
25      AssertAreEqualInterval(Add(Add(a, c), b), new AlgebraicInterval(-3, 6));
26    }
27
28    private void AssertAreEqualInterval(AlgebraicInterval a, AlgebraicInterval b) {
29      if (double.IsNaN(a.LowerBound.Value)) {
30        Assert.IsTrue(double.IsNaN(b.LowerBound.Value));
31      } else {
32        Assert.AreEqual(a.LowerBound.Value.Value, b.LowerBound.Value.Value, Math.Abs(a.LowerBound.Value.Value) * 1e-4); // relative error < 0.1%
33      }
34
35      if (double.IsNaN(a.UpperBound.Value)) {
36        Assert.IsTrue(double.IsNaN(b.UpperBound.Value));
37      } else {
38        Assert.AreEqual(a.UpperBound.Value.Value, b.UpperBound.Value.Value, Math.Abs(a.UpperBound.Value.Value) * 1e-4);
39      }
40    }
41
42    private static AlgebraicInterval Add(AlgebraicInterval a, AlgebraicInterval b) {
43      return a.Clone().Add(b);
44    }
45
46    [TestMethod]
47    [TestCategory("Problems.DataAnalysis")]
48    [TestProperty("Time", "short")]
49    public void AlgebraicIntervalSub() {
50      //subtract   [x1,x2] − [y1,y2] = [x1 − y2,x2 − y1]
51
52      //[-1, 1] - [-2, 2] = [-3, 3]
53      AssertAreEqualInterval(Subtract(a, b), new AlgebraicInterval(-3, 3));
54      //([-1, 1] - [-2, 2]) - [0, 3] = [-6, 3]
55      AssertAreEqualInterval(Subtract(Subtract(a, b), c), new AlgebraicInterval(-6, 3));
56      //([-1, 1] - [0, 3]) - [-2, 2] = [-6, 3]
57      AssertAreEqualInterval(Subtract(Subtract(a, c), b), new AlgebraicInterval(-6, 3));
58    }
59
60    private AlgebraicInterval Subtract(AlgebraicInterval a, AlgebraicInterval b) {
61      return a.Clone().Sub(b);
62    }
63
64    [TestMethod]
65    [TestCategory("Problems.DataAnalysis")]
66    [TestProperty("Time", "short")]
67    public void AlgebraicIntervalMutlipy() {
68      //multiply   [x1,x2] * [y1,y2] = [min(x1*y1,x1*y2,x2*y1,x2*y2),max(x1*y1,x1*y2,x2*y1,x2*y2)]
69
70      //[-1, 1] * [-2, 2] = [-2, 2]
71      AssertAreEqualInterval(Multiply(a, b), new AlgebraicInterval(-2, 2));
72      //([-1, 1] * [-2, 2]) * [0, 3] = [-6, 6]
73      AssertAreEqualInterval(Multiply(Multiply(a, b), c), new AlgebraicInterval(-6, 6));
74      //([-1, 1] * [0, 3]) * [-2, 2] = [-6, 6]
75      AssertAreEqualInterval(Multiply(Multiply(a, c), b), new AlgebraicInterval(-6, 6));
76
77      // [-2, 0] * [-2, 0]  = [0, 4]
78      AssertAreEqualInterval(new AlgebraicInterval(0, 4), Multiply(new AlgebraicInterval(-2, 0), new AlgebraicInterval(-2, 0)));
79    }
80
81    private AlgebraicInterval Multiply(AlgebraicInterval a, AlgebraicInterval b) {
82      return a.Clone().Mul(b);
83    }
84
85    [TestMethod]
86    [TestCategory("Problems.DataAnalysis")]
87    [TestProperty("Time", "short")]
88    public void AlgebraicIntervalDivide() {
89      //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].
90
91      //[4, 6] / [1, 3] = [4/3, 6]
92      AssertAreEqualInterval(Divide(e, d), new AlgebraicInterval(4.0 / 3.0, 6));
93      //([4, 6] / [1, 3]) / [1, 3] = [4/9, 6]
94      AssertAreEqualInterval(Divide(Divide(e, d), d), new AlgebraicInterval(4.0 / 9.0, 6));
95      //[4, 6] / [0, 3] = [4/3, +Inf]
96      AssertAreEqualInterval(Divide(e, c), new AlgebraicInterval(4.0 / 3.0, double.PositiveInfinity));
97      //[-1, 1] / [0, 3] = [+Inf, -Inf]
98      AssertAreEqualInterval(Divide(a, c), new AlgebraicInterval(double.NegativeInfinity, double.PositiveInfinity));
99
100      //Division by 0 ==> IsInfiniteOrUndefined == true
101      AssertAreEqualInterval(Divide(d, b), new AlgebraicInterval(double.NegativeInfinity, double.PositiveInfinity));
102
103      // [0, 1] / [0, 1]
104      AssertAreEqualInterval(new AlgebraicInterval(0, 1).Div(new AlgebraicInterval(0, 1)), new AlgebraicInterval(1, double.PositiveInfinity)); // returns [NaN, PositiveInfinity]
105
106    }
107
108    private AlgebraicInterval Divide(AlgebraicInterval e, AlgebraicInterval d) {
109      return e.Clone().Div(d);
110    }
111
112    [TestMethod]
113    [TestCategory("Problems.DataAnalysis")]
114    [TestProperty("Time", "short")]
115    public void AlgebraicIntervalSine() {
116      //sine depends on interval
117      //sin([0, 2*pi]) = [-1, 1]
118      AssertAreEqualInterval(Sine(new AlgebraicInterval(0, 2 * Math.PI)), new AlgebraicInterval(-1, 1));
119      //sin([-pi/2, pi/2]) = [sin(-pi/2), sin(pi/2)]
120      AssertAreEqualInterval(Sine(new AlgebraicInterval(-1 * Math.PI / 2, Math.PI / 2)), new AlgebraicInterval(-1, 1));
121      //sin([0, pi/2]) = [sin(0), sin(pi/2)]
122      AssertAreEqualInterval(Sine(new AlgebraicInterval(0, Math.PI / 2)), new AlgebraicInterval(0, 1));
123      //sin([pi, 3*pi/2]) = [sin(pi), sin(3*pi/2)]
124      AssertAreEqualInterval(Sine(new AlgebraicInterval(Math.PI, 3 * Math.PI / 2)), new AlgebraicInterval(-1, 0));
125      AssertAreEqualInterval(Sine(new AlgebraicInterval(1, 2)), new AlgebraicInterval(Math.Min(Math.Sin(1), Math.Sin(2)), 1));
126      AssertAreEqualInterval(Sine(new AlgebraicInterval(1, 3)), new AlgebraicInterval(Math.Min(Math.Sin(1), Math.Sin(3)), 1));
127      AssertAreEqualInterval(Sine(new AlgebraicInterval(Math.PI, 5 * Math.PI / 2)), new AlgebraicInterval(-1, 1));
128    }
129
130    private AlgebraicInterval Sine(AlgebraicInterval algebraicInterval) {
131      return algebraicInterval.Sin();
132    }
133
134    [TestMethod]
135    [TestCategory("Problems.DataAnalysis")]
136    [TestProperty("Time", "short")]
137    public void AlgebraicIntervalCosine() {
138      //Cosine uses sine Sine(Subtract(a, new AlgebraicInterval(Math.PI / 2, Math.PI / 2)));
139      AssertAreEqualInterval(Cosine(new AlgebraicInterval(0, 2 * Math.PI)), new AlgebraicInterval(-1, 1));
140      AssertAreEqualInterval(new AlgebraicInterval(-1, 1), Cosine(new AlgebraicInterval(Math.PI, 4 * Math.PI / 2)));
141
142      // some random expression that were problematic
143      AssertAreEqualInterval(new AlgebraicInterval(-0.0326026961803847, 1), new AlgebraicInterval(1, 2).Scale(1.73269159006613).Add(9.23027422279119).Cos());
144      AssertAreEqualInterval(new AlgebraicInterval(0.54030230586814, 1), new AlgebraicInterval(1, 2).Scale(3.39318455754522).Cos().IntPower(2).Cos());
145      AssertAreEqualInterval(new AlgebraicInterval(-0.825841274068331, 1), new AlgebraicInterval(1, 2).Scale(-16.7140105366707 * -0.223806041028346).Cos());
146      AssertAreEqualInterval(new AlgebraicInterval(0, 1), new AlgebraicInterval(1, 2).Scale(1.03513653909418).Exp().Cos().IntPower(2));
147      AssertAreEqualInterval(new AlgebraicInterval(-1, 1), new AlgebraicInterval(1, 2).Scale(0.535386838272755).Exp().IntPower(2).Cos());
148    }
149
150    private AlgebraicInterval Cosine(AlgebraicInterval algebraicInterval) {
151      return algebraicInterval.Cos();
152    }
153
154    [TestMethod]
155    [TestCategory("Problems.DataAnalysis")]
156    [TestProperty("Time", "short")]
157    public void AlgebraicIntervalLog() {
158      //Log([3, 5]) = [log(3), log(5)]
159      AssertAreEqualInterval(new AlgebraicInterval(Math.Log(3), Math.Log(5)), Logarithm(new AlgebraicInterval(3, 5)));
160      //Log([0.5, 1]) = [log(0.5), log(1)]
161      AssertAreEqualInterval(new AlgebraicInterval(Math.Log(0.5), 0), Logarithm(new AlgebraicInterval(0.5, 1)));
162      //Log([-1, 5]) = [NaN, log(5)]
163      var result = Logarithm(new AlgebraicInterval(-1, 5));
164      AssertAreEqualInterval(new AlgebraicInterval(double.NaN, Math.Log(5)), result);
165
166    }
167
168    private AlgebraicInterval Logarithm(AlgebraicInterval algebraicInterval) {
169      return algebraicInterval.Log();
170    }
171
172    [TestMethod]
173    [TestCategory("Problems.DataAnalysis")]
174    [TestProperty("Time", "short")]
175    public void AlgebraicIntervalExp() {
176      //Exp([0, 1]) = [exp(0), exp(1)]
177      AssertAreEqualInterval(new AlgebraicInterval(1, Math.Exp(1)), Exponential(new AlgebraicInterval(0, 1)));
178    }
179
180    private AlgebraicInterval Exponential(AlgebraicInterval algebraicInterval) {
181      return algebraicInterval.Exp();
182    }
183
184    [TestMethod]
185    [TestCategory("Problems.DataAnalysis")]
186    [TestProperty("Time", "short")]
187    public void AlgebraicIntervalSqr() {
188      AssertAreEqualInterval(new AlgebraicInterval(1, 4), new AlgebraicInterval(1, 2).IntPower(2));
189      AssertAreEqualInterval(new AlgebraicInterval(1, 4), new AlgebraicInterval(-2, -1).IntPower(2));
190      AssertAreEqualInterval(new AlgebraicInterval(0, 4), new AlgebraicInterval(-2, 2).IntPower(2));
191
192      AssertAreEqualInterval(new AlgebraicInterval(0, 16), new AlgebraicInterval(-2, 2).Mul(new AlgebraicInterval(2, 2)).IntPower(2));
193    }
194
195    [TestMethod]
196    [TestCategory("Problems.DataAnalysis")]
197    [TestProperty("Time", "short")]
198    public void AlgebraicIntervalSqrt() {
199      AssertAreEqualInterval(new AlgebraicInterval(1, 2), new AlgebraicInterval(1, 4).IntRoot(2));
200      AssertAreEqualInterval(new AlgebraicInterval(double.NaN, double.NaN), new AlgebraicInterval(-4, -1).IntRoot(2));
201      AssertAreEqualInterval(new AlgebraicInterval(0, 1), new AlgebraicInterval(0, 1).IntRoot(2));
202    }
203
204    [TestMethod]
205    [TestCategory("Problems.DataAnalysis")]
206    [TestProperty("Time", "short")]
207    public void AlgebraicIntervalCube() {
208      AssertAreEqualInterval(new AlgebraicInterval(1, 8), new AlgebraicInterval(1, 2).IntPower(3));
209      AssertAreEqualInterval(new AlgebraicInterval(-8, -1), new AlgebraicInterval(-2, -1).IntPower(3));
210      AssertAreEqualInterval(new AlgebraicInterval(-8, 8), new AlgebraicInterval(-2, 2).IntPower(3));
211    }
212
213
214    [TestMethod]
215    [TestCategory("Problems.DataAnalysis")]
216    [TestProperty("Time", "short")]
217    public void AlgebraicIntervalAbs() {
218      AssertAreEqualInterval(new AlgebraicInterval(1, 2), new AlgebraicInterval(1, 2).Abs());
219      AssertAreEqualInterval(new AlgebraicInterval(1, 2), new AlgebraicInterval(-2, -1).Abs());
220      AssertAreEqualInterval(new AlgebraicInterval(0, 2), new AlgebraicInterval(-2, 2).Abs());
221      AssertAreEqualInterval(new AlgebraicInterval(0, double.PositiveInfinity), new AlgebraicInterval(double.NegativeInfinity, double.PositiveInfinity).Abs());
222      AssertAreEqualInterval(new AlgebraicInterval(double.NaN, 1), new AlgebraicInterval(-1, double.NaN).Abs()); // XXX unclear how we should handle NaN
223      AssertAreEqualInterval(new AlgebraicInterval(double.NaN, 1), new AlgebraicInterval(double.NaN, -1).Abs());
224    }
225
226
227    [TestMethod]
228    [TestCategory("Problems.DataAnalysis")]
229    [TestProperty("Time", "short")]
230    public void AlgebraicIntervalCbrt() {
231      AssertAreEqualInterval(new AlgebraicInterval(1, 2), new AlgebraicInterval(1, 8).IntRoot(3));
232      AssertAreEqualInterval(new AlgebraicInterval(-2, -1), new AlgebraicInterval(-8, -1).IntRoot(3));
233      AssertAreEqualInterval(new AlgebraicInterval(0, 1), new AlgebraicInterval(0, 1).IntRoot(3));
234    }
235  }
236}
Note: See TracBrowser for help on using the repository browser.