Changeset 16631


Ignore:
Timestamp:
02/28/19 14:34:17 (5 months ago)
Author:
gkronber
Message:

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

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval.cs

    r16571 r16631  
    201201
    202202    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));
    208210    }
    209211
     
    216218
    217219    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));
    219222    }
    220223
    221224    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));
    223227    }
    224228    #endregion
  • trunk/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis-3.4/IntervalTest.cs

    r16407 r16631  
    11using System;
    2 using HeuristicLab.Problems.DataAnalysis;
    32using Microsoft.VisualStudio.TestTools.UnitTesting;
    43
     
    124123      Assert.AreEqual<Interval>(new Interval(1, Math.Exp(1)), Interval.Exponential(new Interval(0, 1)));
    125124    }
     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    }
    126160  }
    127161}
Note: See TracChangeset for help on using the changeset viewer.