1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
4 |
|
---|
5 | namespace HeuristicLab.Problems.DataAnalysis.Tests {
|
---|
6 | [TestClass]
|
---|
7 | public class IntervalTest {
|
---|
8 | private readonly Interval a = new Interval(-1, 1);
|
---|
9 | private readonly Interval b = new Interval(-2, 2);
|
---|
10 | private readonly Interval c = new Interval(0, 3);
|
---|
11 | private readonly Interval d = new Interval(1, 3);
|
---|
12 | private readonly Interval e = new Interval(4, 6);
|
---|
13 |
|
---|
14 | private void CheckLowerAndUpperBoundOfInterval(Interval expected, Interval calculated) {
|
---|
15 | var lowerBoundExpected = expected.LowerBound;
|
---|
16 | var upperBoundExpected = expected.UpperBound;
|
---|
17 | var lowerBoundCalculated = calculated.LowerBound;
|
---|
18 | var upperBoundCalculated = calculated.UpperBound;
|
---|
19 |
|
---|
20 | if(double.IsNaN(lowerBoundExpected) && double.IsNaN(lowerBoundCalculated)) {
|
---|
21 | Assert.IsTrue(double.IsNaN(lowerBoundExpected) && double.IsNaN(lowerBoundCalculated));
|
---|
22 | } else if (double.IsNaN(upperBoundExpected) && double.IsNaN(upperBoundCalculated)) {
|
---|
23 | Assert.IsTrue(double.IsNaN(upperBoundExpected) && double.IsNaN(upperBoundCalculated));
|
---|
24 | } else {
|
---|
25 | Assert.AreEqual(lowerBoundExpected, lowerBoundCalculated, 1e-9);
|
---|
26 | Assert.AreEqual(upperBoundExpected, upperBoundCalculated, 1e-9);
|
---|
27 | }
|
---|
28 | }
|
---|
29 |
|
---|
30 | [TestMethod]
|
---|
31 | [TestCategory("Problems.DataAnalysis")]
|
---|
32 | [TestProperty("Time", "short")]
|
---|
33 | public void AddIntervalTest() {
|
---|
34 | //add [x1,x2] + [y1,y2] = [x1 + y1,x2 + y2]
|
---|
35 |
|
---|
36 | // [-1,1] + [-2,2] = [-3,3]
|
---|
37 | Assert.AreEqual(Interval.Add(a, b), new Interval(-3, 3));
|
---|
38 | //([-1, 1] + [-2, 2]) + [0, 3] = [-3, 6]
|
---|
39 | Assert.AreEqual(Interval.Add(Interval.Add(a, b), c), new Interval(-3, 6));
|
---|
40 | //([-1, 1] + [0, 3]) + [-2, 2] = [-3, 6]
|
---|
41 | Assert.AreEqual(Interval.Add(Interval.Add(a, c), b), new Interval(-3, 6));
|
---|
42 | }
|
---|
43 |
|
---|
44 | [TestMethod]
|
---|
45 | [TestCategory("Problems.DataAnalysis")]
|
---|
46 | [TestProperty("Time", "short")]
|
---|
47 | public void SubtractIntervalTest() {
|
---|
48 | //subtract [x1,x2] − [y1,y2] = [x1 − y2,x2 − y1]
|
---|
49 |
|
---|
50 | //[-1, 1] - [-2, 2] = [-3, 3]
|
---|
51 | CheckLowerAndUpperBoundOfInterval(Interval.Subtract(a, b), new Interval(-3, 3));
|
---|
52 | //([-1, 1] - [-2, 2]) - [0, 3] = [-6, 3]
|
---|
53 | CheckLowerAndUpperBoundOfInterval(Interval.Subtract(Interval.Subtract(a, b), c), new Interval(-6, 3));
|
---|
54 | //([-1, 1] - [0, 3]) - [-2, 2] = [-6, 3]
|
---|
55 | CheckLowerAndUpperBoundOfInterval(Interval.Subtract(Interval.Subtract(a, c), b), new Interval(-6, 3));
|
---|
56 | }
|
---|
57 |
|
---|
58 | [TestMethod]
|
---|
59 | [TestCategory("Problems.DataAnalysis")]
|
---|
60 | [TestProperty("Time", "short")]
|
---|
61 | public void MultiplyIntervalTest() {
|
---|
62 | //multiply [x1,x2] * [y1,y2] = [min(x1*y1,x1*y2,x2*y1,x2*y2),max(x1*y1,x1*y2,x2*y1,x2*y2)]
|
---|
63 |
|
---|
64 | //[-1, 1] * [-2, 2] = [-2, 2]
|
---|
65 | CheckLowerAndUpperBoundOfInterval(Interval.Multiply(a, b), new Interval(-2, 2));
|
---|
66 | //([-1, 1] * [-2, 2]) * [0, 3] = [-6, 6]
|
---|
67 | CheckLowerAndUpperBoundOfInterval(Interval.Multiply(Interval.Multiply(a, b), c), new Interval(-6, 6));
|
---|
68 | //([-1, 1] * [0, 3]) * [-2, 2] = [-6, 6]
|
---|
69 | CheckLowerAndUpperBoundOfInterval(Interval.Multiply(Interval.Multiply(a, c), b), new Interval(-6, 6));
|
---|
70 |
|
---|
71 | // [-2, 0] * [-2, 0] = [0, 4]
|
---|
72 | CheckLowerAndUpperBoundOfInterval(new Interval(0, 4), Interval.Multiply(new Interval(-2, 0), new Interval(-2, 0)));
|
---|
73 | }
|
---|
74 |
|
---|
75 | [TestMethod]
|
---|
76 | [TestCategory("Problems.DataAnalysis")]
|
---|
77 | [TestProperty("Time", "short")]
|
---|
78 | public void DivideIntervalTest() {
|
---|
79 | //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].
|
---|
80 |
|
---|
81 | //[4, 6] / [1, 3] = [4/3, 6]
|
---|
82 | CheckLowerAndUpperBoundOfInterval(Interval.Divide(e, d), new Interval(4.0 / 3.0, 6));
|
---|
83 | //([4, 6] / [1, 3]) / [1, 3] = [4/9, 6]
|
---|
84 | CheckLowerAndUpperBoundOfInterval(Interval.Divide(Interval.Divide(e, d), d), new Interval(4.0 / 9.0, 6));
|
---|
85 | //[4, 6] / [0, 3] = [4/3, +Inf]
|
---|
86 | CheckLowerAndUpperBoundOfInterval(Interval.Divide(e, c), new Interval(4.0 / 3.0, double.PositiveInfinity));
|
---|
87 | //[-1, 1] / [0, 3] = [+Inf, -Inf]
|
---|
88 | CheckLowerAndUpperBoundOfInterval(Interval.Divide(a, c), new Interval(double.NegativeInfinity, double.PositiveInfinity));
|
---|
89 | //Devision by 0 ==> IsInfiniteOrUndefined == true
|
---|
90 | Assert.IsTrue(Interval.Divide(e, c).IsInfiniteOrUndefined);
|
---|
91 | //Devision by 0 ==> IsInfiniteOrUndefined == true
|
---|
92 | Assert.IsTrue(Interval.Divide(a, c).IsInfiniteOrUndefined);
|
---|
93 | CheckLowerAndUpperBoundOfInterval(Interval.Divide(d, b), new Interval(double.NegativeInfinity, double.PositiveInfinity));
|
---|
94 | }
|
---|
95 |
|
---|
96 | [TestMethod]
|
---|
97 | [TestCategory("Problems.DataAnalysis")]
|
---|
98 | [TestProperty("Time", "short")]
|
---|
99 | public void SineIntervalTest() {
|
---|
100 | //sine depends on interval
|
---|
101 | //sin([0, 2*pi]) = [-1, 1]
|
---|
102 | CheckLowerAndUpperBoundOfInterval(Interval.Sine(new Interval(0, 2 * Math.PI)), new Interval(-1, 1));
|
---|
103 | //sin([-pi/2, pi/2]) = [sin(-pi/2), sin(pi/2)]
|
---|
104 | CheckLowerAndUpperBoundOfInterval(Interval.Sine(new Interval(-1 * Math.PI / 2, Math.PI / 2)), new Interval(-1, 1));
|
---|
105 | //sin([0, pi/2]) = [sin(0), sin(pi/2)]
|
---|
106 | CheckLowerAndUpperBoundOfInterval(Interval.Sine(new Interval(0, Math.PI / 2)), new Interval(0, 1));
|
---|
107 | //sin([pi, 3*pi/2]) = [sin(pi), sin(3*pi/2)]
|
---|
108 | CheckLowerAndUpperBoundOfInterval(Interval.Sine(new Interval(Math.PI, 3 * Math.PI / 2)), new Interval(-1, 0));
|
---|
109 | CheckLowerAndUpperBoundOfInterval(Interval.Sine(new Interval(1, 2)), new Interval(Math.Min(Math.Sin(1), Math.Sin(2)), 1));
|
---|
110 | CheckLowerAndUpperBoundOfInterval(Interval.Sine(new Interval(1, 3)), new Interval(Math.Min(Math.Sin(1), Math.Sin(3)), 1));
|
---|
111 | CheckLowerAndUpperBoundOfInterval(Interval.Sine(new Interval(Math.PI, 5 * Math.PI / 2)), new Interval(-1, 1));
|
---|
112 | }
|
---|
113 |
|
---|
114 | [TestMethod]
|
---|
115 | [TestCategory("Problems.DataAnalysis")]
|
---|
116 | [TestProperty("Time", "short")]
|
---|
117 | public void CosineIntervalTest() {
|
---|
118 | //Cosine uses sine Interval.Sine(Interval.Subtract(a, new Interval(Math.PI / 2, Math.PI / 2)));
|
---|
119 | CheckLowerAndUpperBoundOfInterval(Interval.Cosine(new Interval(0, 2 * Math.PI)), new Interval(-1, 1));
|
---|
120 | CheckLowerAndUpperBoundOfInterval(new Interval(-1, 1), Interval.Cosine(new Interval(Math.PI, 4 * Math.PI / 2)));
|
---|
121 | }
|
---|
122 |
|
---|
123 | [TestMethod]
|
---|
124 | [TestCategory("Problems.DataAnalysis")]
|
---|
125 | [TestProperty("Time", "short")]
|
---|
126 | public void LogIntervalTest() {
|
---|
127 | //Log([3, 5]) = [log(3), log(5)]
|
---|
128 | CheckLowerAndUpperBoundOfInterval(new Interval(Math.Log(3), Math.Log(5)), Interval.Logarithm(new Interval(3, 5)));
|
---|
129 | //Log([0.5, 1]) = [log(0.5), log(1)]
|
---|
130 | CheckLowerAndUpperBoundOfInterval(new Interval(Math.Log(0.5), 0), Interval.Logarithm(new Interval(0.5, 1)));
|
---|
131 | //Log([-1, 5]) = [NaN, log(5)]
|
---|
132 | var result = Interval.Logarithm(new Interval(-1, 5));
|
---|
133 | CheckLowerAndUpperBoundOfInterval(new Interval(double.NaN, Math.Log(5)),result);
|
---|
134 | Assert.IsTrue(result.IsInfiniteOrUndefined);
|
---|
135 | }
|
---|
136 |
|
---|
137 |
|
---|
138 | [TestMethod]
|
---|
139 | [TestCategory("Problems.DataAnalysis")]
|
---|
140 | [TestProperty("Time", "short")]
|
---|
141 | public void ExponentialIntervalTest() {
|
---|
142 | //Exp([0, 1]) = [exp(0), exp(1)]
|
---|
143 | CheckLowerAndUpperBoundOfInterval(new Interval(1, Math.Exp(1)), Interval.Exponential(new Interval(0, 1)));
|
---|
144 | }
|
---|
145 |
|
---|
146 |
|
---|
147 | [TestMethod]
|
---|
148 | [TestCategory("Problems.DataAnalysis")]
|
---|
149 | [TestProperty("Time", "short")]
|
---|
150 | public void SquareIntervalTest() {
|
---|
151 | CheckLowerAndUpperBoundOfInterval(new Interval(1, 4), Interval.Square(new Interval(1, 2)));
|
---|
152 | CheckLowerAndUpperBoundOfInterval(new Interval(1, 4), Interval.Square(new Interval(-2, -1)));
|
---|
153 | CheckLowerAndUpperBoundOfInterval(new Interval(0, 4), Interval.Square(new Interval(-2, 2)));
|
---|
154 | }
|
---|
155 |
|
---|
156 | [TestMethod]
|
---|
157 | [TestCategory("Problems.DataAnalysis")]
|
---|
158 | [TestProperty("Time", "short")]
|
---|
159 | public void SquarerootIntervalTest() {
|
---|
160 | CheckLowerAndUpperBoundOfInterval(new Interval(-2, 2), Interval.SquareRoot(new Interval(1, 4)));
|
---|
161 | CheckLowerAndUpperBoundOfInterval(new Interval(double.NaN, double.NaN), Interval.SquareRoot(new Interval(-4, -1)));
|
---|
162 | }
|
---|
163 |
|
---|
164 | [TestMethod]
|
---|
165 | [TestCategory("Problems.DataAnalysis")]
|
---|
166 | [TestProperty("Time", "short")]
|
---|
167 | public void CubeIntervalTest() {
|
---|
168 | CheckLowerAndUpperBoundOfInterval(new Interval(1, 8), Interval.Cube(new Interval(1, 2)));
|
---|
169 | CheckLowerAndUpperBoundOfInterval(new Interval(-8, -1), Interval.Cube(new Interval(-2, -1)));
|
---|
170 | CheckLowerAndUpperBoundOfInterval(new Interval(-8, 8), Interval.Cube(new Interval(-2, 2)));
|
---|
171 | }
|
---|
172 |
|
---|
173 | [TestMethod]
|
---|
174 | [TestCategory("Problems.DataAnalysis")]
|
---|
175 | [TestProperty("Time", "short")]
|
---|
176 | public void CubeRootIntervalTest() {
|
---|
177 | CheckLowerAndUpperBoundOfInterval(new Interval(1, 2), Interval.CubicRoot(new Interval(1, 8)));
|
---|
178 | CheckLowerAndUpperBoundOfInterval(new Interval(-2, -2), Interval.CubicRoot(new Interval(-8, -8)));
|
---|
179 | CheckLowerAndUpperBoundOfInterval(new Interval(-2, 2), Interval.CubicRoot(new Interval(-8, 8)));
|
---|
180 | Assert.AreEqual(new Interval(2, 2), Interval.CubicRoot(new Interval(8, 8)));
|
---|
181 | Assert.AreEqual(new Interval(-Math.Pow(6, 1.0 / 3), 2), Interval.CubicRoot(new Interval(-6, 8)));
|
---|
182 | Assert.AreEqual(new Interval(2, 2), Interval.CubicRoot(new Interval(8, 8)));
|
---|
183 | Assert.AreEqual(new Interval(-2, 0), Interval.CubicRoot(new Interval(-8, 0)));
|
---|
184 | }
|
---|
185 |
|
---|
186 | [TestMethod]
|
---|
187 | [TestCategory("Problems.DataAnalysis")]
|
---|
188 | [TestProperty("Time", "short")]
|
---|
189 | public void AbsoluteIntervalTest() {
|
---|
190 | Assert.AreEqual(new Interval(2, 5), Interval.Absolute(new Interval(-5, -2)));
|
---|
191 | Assert.AreEqual(new Interval(2, 5), Interval.Absolute(new Interval(2, 5)));
|
---|
192 | Assert.AreEqual(new Interval(0, 3), Interval.Absolute(new Interval(-3, 0)));
|
---|
193 | Assert.AreEqual(new Interval(0, 5), Interval.Absolute(new Interval(0, 5)));
|
---|
194 | Assert.AreEqual(new Interval(0, 5), Interval.Absolute(new Interval(-2, 5)));
|
---|
195 | }
|
---|
196 |
|
---|
197 | [TestMethod]
|
---|
198 | [TestCategory("Problems.DataAnalysis")]
|
---|
199 | [TestProperty("Time", "short")]
|
---|
200 | public void AnalyticalQuotientIntervalTest() {
|
---|
201 | //Analytical Quotient ==> a / sqrt(b^2 + 1)
|
---|
202 | var aPos = new Interval(3, 5);
|
---|
203 | var aZero = new Interval(-3, 5);
|
---|
204 | var aNeg = new Interval(-5, -3);
|
---|
205 |
|
---|
206 | var bPos = new Interval(2, 4);
|
---|
207 | var bZero = new Interval(-2, 4);
|
---|
208 | var bNeg = new Interval(-4, -2);
|
---|
209 |
|
---|
210 | //Second interval goes over zero
|
---|
211 | //Assert.AreEqual(new Interval(aPos.LowerBound/Math.Sqrt(17), aPos.UpperBound), Interval.AnalyticalQuotient(aPos, bZero));
|
---|
212 | //Assert.AreEqual(new Interval(aZero.LowerBound, aZero.UpperBound), Interval.AnalyticalQuotient(aZero, bZero));
|
---|
213 | //Assert.AreEqual(new Interval(aNeg.LowerBound, aNeg.UpperBound/Math.Sqrt(17)), Interval.AnalyticalQuotient(aNeg, bZero));
|
---|
214 | ////Second interval is positive
|
---|
215 | //Assert.AreEqual(new Interval(aPos.LowerBound/Math.Sqrt(17), aPos.UpperBound/Math.Sqrt(5)), Interval.AnalyticalQuotient(aPos, bPos));
|
---|
216 | //Assert.AreEqual(new Interval(aZero.LowerBound/Math.Sqrt(5), aZero.UpperBound/Math.Sqrt(5)), Interval.AnalyticalQuotient(aZero, bPos));
|
---|
217 | //Assert.AreEqual(new Interval(aNeg.LowerBound/Math.Sqrt(5), aNeg.UpperBound/Math.Sqrt(17)), Interval.AnalyticalQuotient(aNeg, bPos));
|
---|
218 | ////Second interval is negative
|
---|
219 | //Assert.AreEqual(new Interval(aPos.LowerBound/Math.Sqrt(17), aPos.UpperBound/Math.Sqrt(5)), Interval.AnalyticalQuotient(aPos, bNeg));
|
---|
220 | //Assert.AreEqual(new Interval(aZero.LowerBound/Math.Sqrt(5), aZero.UpperBound/Math.Sqrt(5)), Interval.AnalyticalQuotient(aZero, bNeg));
|
---|
221 | //Assert.AreEqual(new Interval(aNeg.LowerBound/Math.Sqrt(5), aNeg.UpperBound/Math.Sqrt(17)), Interval.AnalyticalQuotient(aNeg, bNeg));
|
---|
222 | Assert.AreEqual(new Interval(double.NegativeInfinity, double.PositiveInfinity), Interval.AnalyticQuotient(aPos, bZero));
|
---|
223 | Assert.AreEqual(new Interval(double.NegativeInfinity, double.PositiveInfinity), Interval.AnalyticQuotient(aPos, bPos));
|
---|
224 | Assert.AreEqual(new Interval(double.NegativeInfinity, double.PositiveInfinity), Interval.AnalyticQuotient(aZero, bNeg));
|
---|
225 | }
|
---|
226 |
|
---|
227 | [TestMethod]
|
---|
228 | [TestCategory("Problems.DataAnalysis")]
|
---|
229 | [TestProperty("Time", "short")]
|
---|
230 | public void IsNegativeIntervalTest() {
|
---|
231 | Assert.IsTrue(new Interval(-2, -1).IsNegative);
|
---|
232 | Assert.IsFalse(new Interval(-2, 0).IsNegative);
|
---|
233 | Assert.IsFalse(new Interval(-2, 2).IsNegative);
|
---|
234 | Assert.IsFalse(new Interval(2, 4).IsNegative);
|
---|
235 | }
|
---|
236 |
|
---|
237 | [TestMethod]
|
---|
238 | [TestCategory("Problems.DataAnalysis")]
|
---|
239 | [TestProperty("Time", "short")]
|
---|
240 | public void IsPositiveIntervalTest() {
|
---|
241 | Assert.IsTrue(new Interval(3, 5).IsPositive);
|
---|
242 | Assert.IsFalse(new Interval(0, 5).IsPositive);
|
---|
243 | Assert.IsFalse(new Interval(-1, 5).IsPositive);
|
---|
244 | Assert.IsFalse(new Interval(-5, -2).IsPositive);
|
---|
245 | }
|
---|
246 |
|
---|
247 | [TestMethod]
|
---|
248 | [TestCategory("Problems.DataAnalysis")]
|
---|
249 | [TestProperty("Time", "short")]
|
---|
250 | public void IsAlmostIntervalTest() {
|
---|
251 | var negativeLowerBound = -2E-13;
|
---|
252 | var negativeUpperBound = -1E-13;
|
---|
253 | var positiveLowerBound = 3E-13;
|
---|
254 | var positiveUpperBound = 5E-13;
|
---|
255 |
|
---|
256 | var negativeInterval = new Interval(negativeLowerBound, negativeUpperBound);
|
---|
257 | var positiveInterval = new Interval(positiveLowerBound, positiveUpperBound);
|
---|
258 | var zeroInterval = new Interval(negativeUpperBound, positiveLowerBound);
|
---|
259 |
|
---|
260 | //Check for right-shift of negative interval
|
---|
261 | Assert.AreEqual(negativeUpperBound, negativeInterval.LowerBound);
|
---|
262 | Assert.AreEqual(negativeUpperBound, negativeInterval.UpperBound);
|
---|
263 | //Check for left-shift of positive interval
|
---|
264 | Assert.AreEqual(positiveLowerBound, positiveInterval.LowerBound);
|
---|
265 | Assert.AreEqual(positiveLowerBound, positiveInterval.UpperBound);
|
---|
266 | //Check for setting interval to zero
|
---|
267 | Assert.AreEqual(0, zeroInterval.LowerBound);
|
---|
268 | Assert.AreEqual(0, zeroInterval.UpperBound);
|
---|
269 | }
|
---|
270 |
|
---|
271 | [TestMethod]
|
---|
272 | [TestCategory("Problems.DataAnalysis")]
|
---|
273 | [TestProperty("Time", "short")]
|
---|
274 | public void ContaintsTest() {
|
---|
275 | var negativeInterval = new Interval(-10, -5);
|
---|
276 | var positiveInterval = new Interval(5, 10);
|
---|
277 | var overZeroInterval = new Interval(-5, 5);
|
---|
278 |
|
---|
279 | //Tests for negative intervals
|
---|
280 | Assert.AreEqual(true, negativeInterval.Contains(new Interval(-9, -7)));
|
---|
281 | Assert.AreEqual(false, negativeInterval.Contains(new Interval(-11, -3)));
|
---|
282 | Assert.AreEqual(false, negativeInterval.Contains(positiveInterval));
|
---|
283 | Assert.AreEqual(false, negativeInterval.Contains(overZeroInterval));
|
---|
284 | Assert.AreEqual(true, negativeInterval.Contains(-8));
|
---|
285 | Assert.AreEqual(false, negativeInterval.Contains(-12));
|
---|
286 | Assert.AreEqual(false, negativeInterval.Contains(0));
|
---|
287 | //Testes for positive intervals
|
---|
288 | Assert.AreEqual(true, positiveInterval.Contains(new Interval(6, 10)));
|
---|
289 | Assert.AreEqual(false, positiveInterval.Contains(new Interval(6, 12)));
|
---|
290 | Assert.AreEqual(false, positiveInterval.Contains(negativeInterval));
|
---|
291 | Assert.AreEqual(false, positiveInterval.Contains(overZeroInterval));
|
---|
292 | Assert.AreEqual(true, positiveInterval.Contains(7));
|
---|
293 | Assert.AreEqual(false, positiveInterval.Contains(11));
|
---|
294 | Assert.AreEqual(false, positiveInterval.Contains(0));
|
---|
295 | //Tests for over zero intervals
|
---|
296 | Assert.AreEqual(true, overZeroInterval.Contains(new Interval(-3, 3)));
|
---|
297 | Assert.AreEqual(true, overZeroInterval.Contains(new Interval(-4, -1)));
|
---|
298 | Assert.AreEqual(true, overZeroInterval.Contains(new Interval(1, 5)));
|
---|
299 | Assert.AreEqual(false, overZeroInterval.Contains(new Interval(-6, 0)));
|
---|
300 | Assert.AreEqual(false, overZeroInterval.Contains(new Interval(0, 6)));
|
---|
301 | Assert.AreEqual(false, overZeroInterval.Contains(new Interval(-7, 7)));
|
---|
302 | Assert.AreEqual(true, overZeroInterval.Contains(-3));
|
---|
303 | Assert.AreEqual(true, overZeroInterval.Contains(0));
|
---|
304 | Assert.AreEqual(true, overZeroInterval.Contains(3));
|
---|
305 | Assert.AreEqual(false, overZeroInterval.Contains(12));
|
---|
306 | Assert.AreEqual(false, overZeroInterval.Contains(-7));
|
---|
307 | }
|
---|
308 |
|
---|
309 | [TestMethod]
|
---|
310 | [TestCategory("Problems.DataAnalysis")]
|
---|
311 | [TestProperty("Time", "short")]
|
---|
312 | public void GetIntervalTest() {
|
---|
313 | var values = new List<double>() { -2.5, -9, 2, 7, 0 ,12, 12.4, 12.4, 9.29, 1002, -29.9};
|
---|
314 | var valuesNan = new List<double>() { double.NaN, 2, 4, 19, -2, -12.2};
|
---|
315 | var valuesInf = new List<double>() {double.NegativeInfinity, double.PositiveInfinity, 12, 2, -2, -12.2};
|
---|
316 |
|
---|
317 | var valuesInterval = new Interval(-29.9, 1002);
|
---|
318 | var valuesNanInterval = new Interval(double.NaN, double.NaN);
|
---|
319 | var valuesInfInterval = new Interval(double.NegativeInfinity, double.PositiveInfinity);
|
---|
320 |
|
---|
321 |
|
---|
322 | Assert.AreEqual(valuesInterval, Interval.GetInterval(values));
|
---|
323 | Assert.AreEqual(valuesNanInterval, Interval.GetInterval(valuesNan));
|
---|
324 | Assert.AreEqual(valuesInfInterval, Interval.GetInterval(valuesInf));
|
---|
325 | }
|
---|
326 |
|
---|
327 | [TestMethod]
|
---|
328 | [TestCategory("Problems.DataAnalysis")]
|
---|
329 | [TestProperty("Time", "short")]
|
---|
330 | public void GeometricTest() {
|
---|
331 | CheckLowerAndUpperBoundOfInterval(new Interval(-1, -0.936456687290796), Interval.Cosine(new Interval(3, 3.5)));
|
---|
332 | CheckLowerAndUpperBoundOfInterval(new Interval(-1, -0.936456687290796), Interval.Cosine(new Interval(-3.5, -3)));
|
---|
333 | CheckLowerAndUpperBoundOfInterval(new Interval(-1, 1), Interval.Cosine(new Interval(-3.5, 3)));
|
---|
334 | CheckLowerAndUpperBoundOfInterval(new Interval(-0.839071529076452, 0.843853958732493), Interval.Cosine(new Interval(10, 12)));
|
---|
335 | CheckLowerAndUpperBoundOfInterval(new Interval(0.136737218207833, 0.907446781450197), Interval.Cosine(new Interval(13, 14)));
|
---|
336 | CheckLowerAndUpperBoundOfInterval(new Interval(-0.839071529076452, 1), Interval.Cosine(new Interval(10, 14)));
|
---|
337 | CheckLowerAndUpperBoundOfInterval(new Interval(-1, 0.136737218207833), Interval.Cosine(new Interval(14, 16)));
|
---|
338 | CheckLowerAndUpperBoundOfInterval(new Interval(-0.839071529076452, 0.004425697988051), Interval.Cosine(new Interval(-11, -10)));
|
---|
339 | CheckLowerAndUpperBoundOfInterval(new Interval(0.136737218207833, 0.907446781450197), Interval.Cosine(new Interval(-14, -13)));
|
---|
340 | CheckLowerAndUpperBoundOfInterval(new Interval(-1, 0.136737218207833), Interval.Cosine(new Interval(-16, -14)));
|
---|
341 | CheckLowerAndUpperBoundOfInterval(new Interval(0.101585703696621, 1), Interval.Cosine(new Interval(-102, -100)));
|
---|
342 | CheckLowerAndUpperBoundOfInterval(new Interval(-1, 1), Interval.Cosine(new Interval(4.6e15, 4.7e15)));
|
---|
343 | CheckLowerAndUpperBoundOfInterval(new Interval(0.87758256189037265, 0.87758256189037276), Interval.Cosine(new Interval(0.5, 0.5)));
|
---|
344 | CheckLowerAndUpperBoundOfInterval(new Interval(-0.09904103659872825, 0.8775825618903728), Interval.Cosine(new Interval(0.5, 1.67)));
|
---|
345 | CheckLowerAndUpperBoundOfInterval(new Interval(-1.0, 0.77556587851025016), Interval.Cosine(new Interval(2.1, 5.6)));
|
---|
346 | CheckLowerAndUpperBoundOfInterval(new Interval(-1.0, 1.0), Interval.Cosine(new Interval(0.5, 8.5)));
|
---|
347 | CheckLowerAndUpperBoundOfInterval(new Interval(-1.0, -0.09904103659872801), Interval.Cosine(new Interval(1.67, 3.2)));
|
---|
348 |
|
---|
349 | CheckLowerAndUpperBoundOfInterval(new Interval(double.NegativeInfinity, double.PositiveInfinity), Interval.Tangens(new Interval(double.NegativeInfinity, double.PositiveInfinity)));
|
---|
350 | CheckLowerAndUpperBoundOfInterval(new Interval(0, 1.55740772465490223051), Interval.Tangens(new Interval(0, 1)));
|
---|
351 | CheckLowerAndUpperBoundOfInterval(new Interval(-1.55740772465490223051, 0), Interval.Tangens(new Interval(-1, 0)));
|
---|
352 | CheckLowerAndUpperBoundOfInterval(new Interval(double.NegativeInfinity, double.PositiveInfinity), Interval.Tangens(new Interval(-2, -1)));
|
---|
353 | CheckLowerAndUpperBoundOfInterval(new Interval(double.NegativeInfinity, double.PositiveInfinity), Interval.Tangens(new Interval(202, 203)));
|
---|
354 | CheckLowerAndUpperBoundOfInterval(new Interval(0.54630248984379048, 0.5463024898437906), Interval.Tangens(new Interval(0.5, 0.5)));
|
---|
355 | CheckLowerAndUpperBoundOfInterval(new Interval(double.NegativeInfinity, double.PositiveInfinity), Interval.Tangens(new Interval(0.5,
|
---|
356 | 1.67)));
|
---|
357 |
|
---|
358 | CheckLowerAndUpperBoundOfInterval(new Interval(double.NegativeInfinity, double.PositiveInfinity), Interval.Tangens(new Interval(
|
---|
359 | 6.638314112824137, 8.38263151220128)));
|
---|
360 |
|
---|
361 | CheckLowerAndUpperBoundOfInterval(new Interval(0.47942553860420295, 0.47942553860420301), Interval.Sine(new Interval(0.5, 0.5)));
|
---|
362 | CheckLowerAndUpperBoundOfInterval(new Interval(4.7942553860420295e-01, 1.0), Interval.Sine(new Interval(0.5, 1.67)));
|
---|
363 | CheckLowerAndUpperBoundOfInterval(new Interval(-5.8374143427580093e-02, 9.9508334981018021e-01), Interval.Sine(new Interval(1.67,
|
---|
364 | 3.2)));
|
---|
365 | CheckLowerAndUpperBoundOfInterval(new Interval(-1.0, 0.863209366648874), Interval.Sine(new Interval(2.1, 5.6)));
|
---|
366 | CheckLowerAndUpperBoundOfInterval(new Interval(-1.0, 1.0), Interval.Sine(new Interval(0.5, 8.5)));
|
---|
367 | CheckLowerAndUpperBoundOfInterval(new Interval(-1.0, 0.9775301176650971), Interval.Sine(new Interval(-4.5, 0.1)));
|
---|
368 | CheckLowerAndUpperBoundOfInterval(new Interval(-1.0, 1.0), Interval.Sine(new Interval(1.3, 6.3)));
|
---|
369 | }
|
---|
370 |
|
---|
371 | [TestMethod]
|
---|
372 | [TestCategory("Problems.DataAnalysis")]
|
---|
373 | [TestProperty("Time", "short")]
|
---|
374 | public void EqualsTest() {
|
---|
375 | var interval1 = new Interval(0, 12);
|
---|
376 | var interval2 = new Interval(-12, 8);
|
---|
377 | var interval3 = new Interval(double.NegativeInfinity, 0);
|
---|
378 |
|
---|
379 | Assert.AreEqual(true, interval1.Equals(new Interval(0, 12)));
|
---|
380 | Assert.AreEqual(false, interval1.Equals(interval2));
|
---|
381 | Assert.AreEqual(true, interval3 == new Interval(double.NegativeInfinity, 0));
|
---|
382 | Assert.AreEqual(true, interval1 != interval2);
|
---|
383 | Assert.AreEqual(false, interval2 == interval3);
|
---|
384 | Assert.AreEqual(false, interval1 != new Interval(0, 12));
|
---|
385 | }
|
---|
386 | //[TestMethod]
|
---|
387 | //[TestCategory("Problems.DataAnalysis")]
|
---|
388 | //[TestProperty("Time", "short")]
|
---|
389 | //public void RootTest() {
|
---|
390 | // var posInterval = new Interval(3, 5);
|
---|
391 | // var negInterval = new Interval(-6, -2);
|
---|
392 | // var posIntervalConst = new Interval(5, 5);
|
---|
393 | // var negIntervalConst = new Interval(-3, -3);
|
---|
394 | // var zeroIntervalConst = new Interval(0, 0);
|
---|
395 | // var zeroPosInterval = new Interval(0, 2);
|
---|
396 | // var zeroNegInterval = new Interval(-2, 0);
|
---|
397 |
|
---|
398 | // var posRoot = new Interval(4, 4);
|
---|
399 | // var negRoot = new Interval(-4, -4);
|
---|
400 | // var zeroRoot = new Interval(0, 0);
|
---|
401 | // var oneRoot = new Interval(1, 1);
|
---|
402 |
|
---|
403 | // Assert.AreEqual(Interval.Root(posInterval, posRoot), new Interval(Math.Pow(3, (1.0/4)), Math.Pow(5, (1.0/4))));
|
---|
404 | // Assert.AreEqual(Interval.Root(posInterval, negRoot), new Interval(Math.Pow(5, -(1.0/4)), Math.Pow(3, -(1.0/4))));
|
---|
405 | // Assert.AreEqual(Interval.Root(posInterval, zeroRoot), new Interval(double.NaN, double.NaN));
|
---|
406 | // Assert.AreEqual(Interval.Root(posInterval, oneRoot), new Interval(3, 5));
|
---|
407 |
|
---|
408 | // Assert.AreEqual(Interval.Root(negInterval, posRoot), new Interval(Math.Pow(-6, (1.0 / 4)), Math.Pow(-2, (1.0 / 4))));
|
---|
409 | // Assert.AreEqual(Interval.Root(negInterval, negRoot), new Interval(Math.Pow(-2, -(1.0 / 4)), Math.Pow(-6, -(1.0 / 4))));
|
---|
410 | // Assert.AreEqual(Interval.Root(negInterval, zeroRoot), new Interval(double.NaN, double.NaN));
|
---|
411 | // Assert.AreEqual(Interval.Root(negInterval, oneRoot), new Interval(-6, -2));
|
---|
412 |
|
---|
413 | // Assert.AreEqual(Interval.Root(posIntervalConst, posRoot), new Interval(Math.Pow(5, (1.0 / 4)), Math.Pow(5, (1.0 / 4))));
|
---|
414 | // Assert.AreEqual(Interval.Root(posIntervalConst, negRoot), new Interval(Math.Pow(5, -(1.0 / 4)), Math.Pow(5, -(1.0 / 4))));
|
---|
415 | // Assert.AreEqual(Interval.Root(posIntervalConst, zeroRoot), new Interval(double.NaN, double.NaN));
|
---|
416 | // Assert.AreEqual(Interval.Root(posIntervalConst, oneRoot), new Interval(5, 5));
|
---|
417 |
|
---|
418 | // Assert.AreEqual(Interval.Root(negIntervalConst, posRoot), new Interval(Math.Pow(-3, (1.0 / 4)), Math.Pow(-3, (1.0 / 4))));
|
---|
419 | // Assert.AreEqual(Interval.Root(negIntervalConst, negRoot), new Interval(Math.Pow(-3, -(1.0 / 4)), Math.Pow(-3, -(1.0 / 4))));
|
---|
420 | // Assert.AreEqual(Interval.Root(negIntervalConst, zeroRoot), new Interval(double.NaN, double.NaN));
|
---|
421 | // Assert.AreEqual(Interval.Root(negIntervalConst, oneRoot), new Interval(-3, -3));
|
---|
422 |
|
---|
423 | // Assert.AreEqual(Interval.Root(zeroIntervalConst, posRoot), new Interval(0, 0));
|
---|
424 | // //Compley Infinity https://www.wolframalpha.com/input/?i=root%28-4%2C+0%29
|
---|
425 | // Assert.AreEqual(Interval.Root(zeroIntervalConst, negRoot), new Interval(double.PositiveInfinity, double.PositiveInfinity));
|
---|
426 | // Assert.AreEqual(Interval.Root(zeroIntervalConst, zeroRoot), new Interval(0, 0));
|
---|
427 | // Assert.AreEqual(Interval.Root(zeroIntervalConst, oneRoot), new Interval(0, 0));
|
---|
428 |
|
---|
429 | // Assert.AreEqual(Interval.Root(zeroPosInterval, posRoot), new Interval(0, Math.Pow(2, (1.0 / 4))));
|
---|
430 | // //Check for complex infinity
|
---|
431 | // Assert.AreEqual(Interval.Root(zeroPosInterval, negRoot), new Interval(Math.Pow(2, -(1.0 / 4)), double.PositiveInfinity));
|
---|
432 | // Assert.AreEqual(Interval.Root(zeroPosInterval, zeroRoot), new Interval(0, 0));
|
---|
433 | // Assert.AreEqual(Interval.Root(zeroPosInterval, oneRoot), new Interval(0, 2));
|
---|
434 |
|
---|
435 | // Assert.AreEqual(Interval.Root(zeroNegInterval, posRoot), new Interval(Math.Pow(-2, (1.0 / 4)),0));
|
---|
436 | // //Check for complex infinity
|
---|
437 | // Assert.AreEqual(Interval.Root(zeroNegInterval, negRoot), new Interval(Math.Pow(-2, -(1.0 / 4)), double.PositiveInfinity));
|
---|
438 | // Assert.AreEqual(Interval.Root(zeroNegInterval, zeroRoot), new Interval(double.NaN, double.NaN));
|
---|
439 | // Assert.AreEqual(Interval.Root(zeroNegInterval, oneRoot), new Interval(-2, 0));
|
---|
440 | //}
|
---|
441 | }
|
---|
442 | }
|
---|