Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/02/19 13:55:49 (5 years ago)
Author:
gkronber
Message:

#2994: slightly changed calculation of integer powers for intervals and added unit tests.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2994-AutoDiffForIntervals/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/Interpreter.cs

    r16738 r16744  
    285285        var g = code[0].value.Gradient;
    286286        for (int j = 0; j < nParams; ++j) {
    287           if(g.Elements.TryGetValue(j, out AlgebraicDoubleVector v)) {
     287          if (g.Elements.TryGetValue(j, out AlgebraicDoubleVector v)) {
    288288            v.CopyColumnTo(jac, j, rowIndex, BATCHSIZE);
    289289          } else {
     
    490490    public AlgebraicDouble AssignSin(AlgebraicDouble a) { Value = Math.Sin(a.Value); return this; }
    491491    public AlgebraicDouble AssignCos(AlgebraicDouble a) { Value = Math.Cos(a.Value); return this; }
    492     public AlgebraicDouble AssignLog(AlgebraicDouble a) { Value = a.Value <= 0?double.NegativeInfinity : Math.Log(a.Value); return this; } // alternative definiton of log to prevent NaN
     492    public AlgebraicDouble AssignLog(AlgebraicDouble a) { Value = a.Value <= 0 ? double.NegativeInfinity : Math.Log(a.Value); return this; } // alternative definiton of log to prevent NaN
    493493    public AlgebraicDouble AssignExp(AlgebraicDouble a) { Value = Math.Exp(a.Value); return this; }
    494494    public AlgebraicDouble AssignIntPower(AlgebraicDouble a, int p) { Value = Math.Pow(a.Value, p); return this; }
    495495    public AlgebraicDouble AssignIntRoot(AlgebraicDouble a, int r) { Value = Math.Pow(a.Value, 1.0 / r); return this; }
    496496    public AlgebraicDouble AssignAbs(AlgebraicDouble a) { Value = Math.Abs(a.Value); return this; }
    497     public AlgebraicDouble AssignSgn(AlgebraicDouble a) { Value = double.IsNaN(a.Value)? double.NaN : Math.Sign(a.Value); return this; }
     497    public AlgebraicDouble AssignSgn(AlgebraicDouble a) { Value = double.IsNaN(a.Value) ? double.NaN : Math.Sign(a.Value); return this; }
    498498    public AlgebraicDouble Clone() { return new AlgebraicDouble(Value); }
    499499
     
    828828    }
    829829
    830 //     Exponentiation by a natural power[a, b] k:
    831 //
    832 // if 0∈[a, b], then
    833 //   [a, b]^0  =  [0, 1]
    834 //   [a, b]^2n   =  [0, max(a2n, b2n)]
    835 //   [a, b]^2n+1   =  [a2n+1, b2n+1]
    836 // if [a, b]>0, then
    837 //   [a, b]^0  =  [1, 1]
    838 //   [a, b]^k>0  =  [ak, bk]
    839 // if [a, b]<0, then
    840 //   [a, b]^0  =  [1, 1]
    841 //   [a, b]^2n   =  [b2n, a2n]
    842 //   [a, b]^2n+1   =  [a2n+1, b2n+1]
    843 
    844830    public AlgebraicInterval AssignIntPower(AlgebraicInterval a, int p) {
    845       if (p == 0) {
    846         // => 1
    847         low = new MultivariateDual<AlgebraicDouble>(1.0);
    848         high = new MultivariateDual<AlgebraicDouble>(1.0);
    849         return this;
    850       }
    851       if (p == 1) return this;
    852 
    853831      if (p < 0) {  // x^-3 == 1/(x^3)
    854832        AssignIntPower(a, -p);
    855833        return AssignInv(this);
    856       } else {
     834      } else if (p == 0) {
     835        if (a.Contains(0.0)) {
     836          // => 0^0 = 0 ; might not be relevant
     837          low = new MultivariateDual<AlgebraicDouble>(0.0);
     838          high = new MultivariateDual<AlgebraicDouble>(1.0);
     839          return this;
     840        } else {
     841          // => 1
     842          low = new MultivariateDual<AlgebraicDouble>(1.0);
     843          high = new MultivariateDual<AlgebraicDouble>(1.0);
     844          return this;
     845        }
     846      } else if (p == 1) return this;
     847      else if (p % 2 == 0) {
    857848        // p is even => interval must be positive
    858         if (p % 2 == 0) {
    859           if (a.Contains(0.0)) {
    860             low = new MultivariateDual<AlgebraicDouble>(0.0);
    861             high = Algebraic.Max(a.low.IntPower(p), a.high.IntPower(p));
    862           } else {
    863             var lowPower = a.low.IntPower(p);
    864             var highPower = a.high.IntPower(p);
    865             low = Algebraic.Min(lowPower, highPower);
    866             high = Algebraic.Max(lowPower, highPower);
    867           }
     849        if (a.Contains(0.0)) {
     850          low = new MultivariateDual<AlgebraicDouble>(0.0);
     851          high = Algebraic.Max(a.low.IntPower(p), a.high.IntPower(p));
    868852        } else {
    869           // p is uneven
    870853          var lowPower = a.low.IntPower(p);
    871854          var highPower = a.high.IntPower(p);
     
    873856          high = Algebraic.Max(lowPower, highPower);
    874857        }
    875         return this;
    876       }
     858      } else {
     859        // p is uneven
     860        if (a.Contains(0.0)) {
     861          low.AssignIntPower(a.low, p);
     862          high.AssignIntPower(a.high, p);
     863        } else {
     864          var lowPower = a.low.IntPower(p);
     865          var highPower = a.high.IntPower(p);
     866          low = Algebraic.Min(lowPower, highPower);
     867          high = Algebraic.Max(lowPower, highPower);
     868        }
     869      }
     870      return this;
    877871    }
    878872
Note: See TracChangeset for help on using the changeset viewer.