Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/25/19 09:19:26 (5 years ago)
Author:
chaider
Message:

#2971 Merged trunk into branch

Location:
branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis

  • branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis/3.4

  • branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval/Interval.cs

    r17313 r17368  
    3737    protected Interval(StorableConstructorFlag _) { }
    3838
     39    /// <summary>
     40    /// Creates an interval with given bounds, where lower bound must be smaller than
     41    /// the upper bound. Floating point precision errors trough calculations are fixed by,
     42    /// checking if the intervals are almost the same (E-12). If this is the case, the bounds
     43    /// will be set to the bound closer to zero.
     44    /// </summary>
     45    /// <param name="lowerBound">Lower bound of the interval</param>
     46    /// <param name="upperBound">Upper bound of the interval</param>
    3947    public Interval(double lowerBound, double upperBound) {
    40       //TODO Handle floating point issues where lowerbound is bigger than upperbound in the nth decimal place
     48      if (lowerBound.IsAlmost(upperBound)) {
     49        //If the bounds go over zero
     50        if (lowerBound <= 0 && upperBound >= 0) {
     51          lowerBound = 0.0;
     52          upperBound = 0.0;
     53          //Interval is negative
     54        } else if (upperBound < 0) {
     55          lowerBound = upperBound;
     56          //Interval is positive
     57        } else {
     58          upperBound = lowerBound;
     59        }
     60      }
     61
    4162      if (lowerBound > upperBound)
    4263        throw new ArgumentException("lowerBound must be smaller than or equal to upperBound.");
     
    98119    }
    99120
     121    /// <summary>
     122    /// True if the interval is positive without zero
     123    /// </summary>
     124    public bool IsPositive {
     125      get => LowerBound > 0.0;
     126    }
     127
     128    /// <summary>
     129    /// True if the interval is negative without zero
     130    /// </summary>
     131    public bool IsNegative {
     132      get => UpperBound < 0.0;
     133    }
     134
    100135    public static Interval GetInterval(IEnumerable<double> values) {
    101136      if (values == null) throw new ArgumentNullException("values");
     
    166201    }
    167202
    168     //mkommend: Division by intervals containing 0 is implemented as defined in
     203    //Division by intervals containing 0 is implemented as defined in
    169204    //http://en.wikipedia.org/wiki/Interval_arithmetic
    170205    public static Interval Divide(Interval a, Interval b) {
     
    209244    public static Interval Tangens(Interval a) {
    210245      return Interval.Divide(Interval.Sine(a), Interval.Cosine(a));
    211     } 
     246    }
    212247    public static Interval HyperbolicTangent(Interval a) {
    213248      return new Interval(Math.Tanh(a.LowerBound), Math.Tanh(a.UpperBound));
     
    244279      if (a.UpperBound <= 0) return new Interval(a.UpperBound * a.UpperBound, a.LowerBound * a.LowerBound);     // interval is negative
    245280      else if (a.LowerBound >= 0) return new Interval(a.LowerBound * a.LowerBound, a.UpperBound * a.UpperBound); // interval is positive
    246       else return new Interval(0, Math.Max(a.LowerBound*a.LowerBound, a.UpperBound*a.UpperBound)); // interval goes over zero
     281      else return new Interval(0, Math.Max(a.LowerBound * a.LowerBound, a.UpperBound * a.UpperBound)); // interval goes over zero
    247282    }
    248283
     
    273308      var absLower = Math.Abs(a.LowerBound);
    274309      var absUpper = Math.Abs(a.UpperBound);
    275       return new Interval(Math.Min(absLower, absUpper), Math.Max(absLower, absUpper));
     310      var min = Math.Min(absLower, absUpper);
     311      var max = Math.Max(absLower, absUpper);
     312
     313      if (a.Contains(0.0)) {
     314        min = 0.0;
     315    }
     316
     317      return new Interval(min, max);
     318    }
     319
     320    public static Interval AnalyticalQuotient(Interval a, Interval b) {
     321      var dividend = a;
     322      var divisor = Add(Square(b), new Interval(1.0, 1.0));
     323      divisor = SquareRoot(divisor);
     324
     325      var quotient = Divide(dividend, divisor);
     326      return quotient;
    276327    }
    277328    #endregion
Note: See TracChangeset for help on using the changeset viewer.