Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/09/21 08:11:24 (4 years ago)
Author:
gkronber
Message:

#2985: merged r17797,r17796,r17820 from trunk to stable

Location:
stable
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • stable

  • stable/HeuristicLab.Problems.DataAnalysis.Symbolic

  • stable/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Converters/TreeSimplifier.cs

    r17181 r17862  
    933933        var constB = b as ConstantTreeNode;
    934934        var constBValue = Math.Round(constB.Value);
    935         if (constBValue.IsAlmost(1.0)) {
     935        if (constBValue == 1.0) {
     936          // root(a, 1) => a
    936937          return a;
    937         } else if (constBValue.IsAlmost(0.0)) {
    938           return MakeConstant(1.0);
    939         } else if (constBValue.IsAlmost(-1.0)) {
     938        } else if (constBValue == 0.0) {
     939          // root(a, 0) is not defined
     940          //return MakeConstant(1.0);
     941          return MakeConstant(double.NaN);
     942        } else if (constBValue == -1.0) {
     943          // root(a, -1) => a^(-1/1) => 1/a
    940944          return MakeFraction(MakeConstant(1.0), a);
    941945        } else if (constBValue < 0) {
     946          // root(a, -b) => a^(-1/b) => (1/a)^(1/b) => root(1, b) / root(a, b) => 1 / root(a, b)
    942947          var rootNode = rootSymbol.CreateTreeNode();
    943948          rootNode.AddSubtree(a);
     
    987992        var constB = b as ConstantTreeNode;
    988993        double exponent = Math.Round(constB.Value);
    989         if (exponent.IsAlmost(0.0)) {
     994        if (exponent == 0.0) {
     995          // a^0 => 1
    990996          return MakeConstant(1.0);
    991         } else if (exponent.IsAlmost(1.0)) {
     997        } else if (exponent == 1.0) {
     998          // a^1 => a
    992999          return a;
    993         } else if (exponent.IsAlmost(-1.0)) {
     1000        } else if (exponent == -1.0) {
     1001          // a^-1 => 1/a
    9941002          return MakeFraction(MakeConstant(1.0), a);
    9951003        } else if (exponent < 0) {
     1004          // a^-b => (1/a)^b => 1/(a^b)
    9961005          var powNode = powSymbol.CreateTreeNode();
    9971006          powNode.AddSubtree(a);
     
    10181027        // fold constants
    10191028        return MakeConstant(((ConstantTreeNode)a).Value / ((ConstantTreeNode)b).Value);
    1020       } else if ((IsConstant(a) && !((ConstantTreeNode)a).Value.IsAlmost(1.0))) {
     1029      } else if ((IsConstant(a) && ((ConstantTreeNode)a).Value != 1.0)) {
     1030        // a / x => (a * 1/a) / (x * 1/a) => 1 / (x * 1/a)
    10211031        return MakeFraction(MakeConstant(1.0), MakeProduct(b, Invert(a)));
    10221032      } else if (IsVariableBase(a) && IsConstant(b)) {
     
    10941104        // b is not constant => make sure constant is on the right
    10951105        return MakeSum(b, a);
    1096       } else if (IsConstant(b) && ((ConstantTreeNode)b).Value.IsAlmost(0.0)) {
     1106      } else if (IsConstant(b) && ((ConstantTreeNode)b).Value == 0.0) {
    10971107        // x + 0 => x
    10981108        return a;
     
    12101220      foreach (var unchangedSubtree in unchangedSubtrees)
    12111221        sum.AddSubtree(unchangedSubtree);
    1212       if (!constant.IsAlmost(0.0)) {
     1222      if (constant != 0.0) {
    12131223        sum.AddSubtree(MakeConstant(constant));
    12141224      }
     
    12681278        if (wi < 0) throw new ArgumentException();
    12691279        return MakeBinFactor(node1.Symbol, node1.VariableName, node1.VariableValue, node1.Weight * node0.Weights[wi]);
    1270       } else if (IsConstant(b) && ((ConstantTreeNode)b).Value.IsAlmost(1.0)) {
     1280      } else if (IsConstant(b) && ((ConstantTreeNode)b).Value == 1.0) {
    12711281        // $ * 1.0 => $
    12721282        return a;
    1273       } else if (IsConstant(b) && ((ConstantTreeNode)b).Value.IsAlmost(0.0)) {
     1283      } else if (IsConstant(b) && ((ConstantTreeNode)b).Value == 0.0) {
    12741284        return MakeConstant(0);
    12751285      } else if (IsConstant(b) && IsVariableBase(a)) {
     
    14191429        prod.AddSubtree(unchangedSubtree);
    14201430
    1421       if (!constantProduct.IsAlmost(1.0)) {
     1431      if (constantProduct != 1.0) {
    14221432        prod.AddSubtree(MakeConstant(constantProduct));
    14231433      }
Note: See TracChangeset for help on using the changeset viewer.