Changeset 16631
Legend:
 Unmodified
 Added
 Removed

trunk/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval.cs
r16571 r16631 201 201 202 202 public static Interval Square(Interval a) { 203 return Power(a, new Interval(2, 2)); 204 } 205 206 public static Interval Cubic(Interval a) { 207 return Power(a, new Interval(3, 3)); 203 if (a.UpperBound <= 0) return new Interval(a.UpperBound * a.UpperBound, a.LowerBound * a.LowerBound); // interval is negative 204 else if (a.LowerBound >= 0) return new Interval(a.LowerBound * a.LowerBound, a.UpperBound * a.UpperBound); // interval is positive 205 else return new Interval(0, Math.Max(a.LowerBound*a.LowerBound, a.UpperBound*a.UpperBound)); // interval goes over zero 206 } 207 208 public static Interval Cube(Interval a) { 209 return new Interval(Math.Pow(a.LowerBound, 3), Math.Pow(a.UpperBound, 3)); 208 210 } 209 211 … … 216 218 217 219 public static Interval SquareRoot(Interval a) { 218 return Root(a, new Interval(2, 2)); 220 if (a.LowerBound < 0) return new Interval(double.NaN, double.NaN); 221 return new Interval(Math.Sqrt(a.LowerBound), Math.Sqrt(a.UpperBound)); 219 222 } 220 223 221 224 public static Interval CubicRoot(Interval a) { 222 return Root(a, new Interval(3, 3)); 225 if (a.LowerBound < 0) return new Interval(double.NaN, double.NaN); 226 return new Interval(Math.Pow(a.LowerBound, 1.0/3), Math.Pow(a.UpperBound, 1.0/3)); 223 227 } 224 228 #endregion 
trunk/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis3.4/IntervalTest.cs
r16407 r16631 1 1 using System; 2 using HeuristicLab.Problems.DataAnalysis;3 2 using Microsoft.VisualStudio.TestTools.UnitTesting; 4 3 … … 124 123 Assert.AreEqual<Interval>(new Interval(1, Math.Exp(1)), Interval.Exponential(new Interval(0, 1))); 125 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 } 126 160 } 127 161 }
Note: See TracChangeset
for help on using the changeset viewer.