Free cookie consent management tool by TermsFeed Policy Generator

Changeset 14339


Ignore:
Timestamp:
10/18/16 22:09:39 (8 years ago)
Author:
gkronber
Message:

#2650: fixed bug in simplification of factor symbols

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisExpressionTreeSimplifier.cs

    r14259 r14339  
    651651      } else if (IsFactor(node)) {
    652652        var factNode = node as FactorVariableTreeNode;
    653         for (int i = 0; i < factNode.Weights.Length; i++) factNode.Weights[i] = Math.Exp(factNode.Weights[i]);
    654         return factNode;
     653        return MakeFactor(factNode.Symbol, factNode.VariableName, factNode.Weights.Select(w => Math.Exp(w)));
    655654      } else if (IsLog(node)) {
    656655        return node.GetSubtree(0);
     
    672671      } else if (IsFactor(node)) {
    673672        var factNode = node as FactorVariableTreeNode;
    674         for (int i = 0; i < factNode.Weights.Length; i++) factNode.Weights[i] = factNode.Weights[i] * factNode.Weights[i];
    675         return factNode;
     673        return MakeFactor(factNode.Symbol, factNode.VariableName, factNode.Weights.Select(w => w * w));
    676674      } else if (IsSquareRoot(node)) {
    677675        return node.GetSubtree(0);
     
    689687      } else if (IsFactor(node)) {
    690688        var factNode = node as FactorVariableTreeNode;
    691         for (int i = 0; i < factNode.Weights.Length; i++) factNode.Weights[i] = Math.Sqrt(factNode.Weights[i]);
    692         return factNode;
     689        return MakeFactor(factNode.Symbol, factNode.VariableName, factNode.Weights.Select(w => Math.Sqrt(w)));
    693690      } else if (IsSquare(node)) {
    694691        return node.GetSubtree(0);
     
    706703      } else if (IsFactor(node)) {
    707704        var factNode = node as FactorVariableTreeNode;
    708         for (int i = 0; i < factNode.Weights.Length; i++) factNode.Weights[i] = Math.Log(factNode.Weights[i]);
    709         return factNode;
     705        return MakeFactor(factNode.Symbol, factNode.VariableName, factNode.Weights.Select(w => Math.Log(w)));
    710706      } else if (IsExp(node)) {
    711707        return node.GetSubtree(0);
     
    727723        var factNode = a as FactorVariableTreeNode;
    728724        var constNode = b as ConstantTreeNode;
    729         for (int i = 0; i < factNode.Weights.Length; i++)
    730           factNode.Weights[i] = Math.Pow(factNode.Weights[i], 1.0 / Math.Round(constNode.Value));
    731         return factNode;
     725        return MakeFactor(factNode.Symbol, factNode.VariableName,
     726          factNode.Weights.Select(w => Math.Pow(w, 1.0 / Math.Round(constNode.Value))));
    732727      } else if (IsConstant(a) && IsFactor(b)) {
    733728        var constNode = a as ConstantTreeNode;
    734729        var factNode = b as FactorVariableTreeNode;
    735         for (int i = 0; i < factNode.Weights.Length; i++)
    736           factNode.Weights[i] = Math.Pow(constNode.Value, 1.0 / Math.Round(factNode.Weights[i]));
    737         return factNode;
     730        return MakeFactor(factNode.Symbol, factNode.VariableName, factNode.Weights.Select(w => Math.Pow(constNode.Value, 1.0 / Math.Round(w))));
    738731      } else if (IsFactor(a) && IsFactor(b) && AreSameVariable(a, b)) {
    739732        var node0 = a as FactorVariableTreeNode;
    740733        var node1 = b as FactorVariableTreeNode;
    741         for (int i = 0; i < node0.Weights.Length; i++)
    742           node0.Weights[i] = Math.Pow(node0.Weights[i], 1.0 / Math.Round(node1.Weights[i]));
    743         return node0;
     734        return MakeFactor(node0.Symbol, node0.VariableName, node0.Weights.Zip(node1.Weights, (u, v) => Math.Pow(u, 1.0 / Math.Round(v))));
    744735      } else if (IsConstant(b)) {
    745736        var constB = b as ConstantTreeNode;
     
    770761    }
    771762
     763
    772764    private ISymbolicExpressionTreeNode MakePower(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
    773765      if (IsConstant(a) && IsConstant(b)) {
     
    778770        var factNode = a as FactorVariableTreeNode;
    779771        var constNode = b as ConstantTreeNode;
    780         for (int i = 0; i < factNode.Weights.Length; i++)
    781           factNode.Weights[i] = Math.Pow(factNode.Weights[i], Math.Round(constNode.Value));
    782         return factNode;
     772        return MakeFactor(factNode.Symbol, factNode.VariableName, factNode.Weights.Select(w => Math.Pow(w, Math.Round(constNode.Value))));
    783773      } else if (IsConstant(a) && IsFactor(b)) {
    784774        var constNode = a as ConstantTreeNode;
    785775        var factNode = b as FactorVariableTreeNode;
    786         for (int i = 0; i < factNode.Weights.Length; i++)
    787           factNode.Weights[i] = Math.Pow(constNode.Value, Math.Round(factNode.Weights[i]));
    788         return factNode;
     776        return MakeFactor(factNode.Symbol, factNode.VariableName, factNode.Weights.Select(w => Math.Pow(constNode.Value, Math.Round(w))));
    789777      } else if (IsFactor(a) && IsFactor(b) && AreSameVariable(a, b)) {
    790778        var node0 = a as FactorVariableTreeNode;
    791779        var node1 = b as FactorVariableTreeNode;
    792         for (int i = 0; i < node0.Weights.Length; i++)
    793           node0.Weights[i] = Math.Pow(node0.Weights[i], Math.Round(node1.Weights[i]));
    794         return node0;
     780        return MakeFactor(node0.Symbol, node0.VariableName, node0.Weights.Zip(node1.Weights, (u, v) => Math.Pow(u, Math.Round(v))));
    795781      } else if (IsConstant(b)) {
    796782        var constB = b as ConstantTreeNode;
     
    837823        var factNode = a as FactorVariableTreeNode;
    838824        var constNode = b as ConstantTreeNode;
    839         for (int i = 0; i < factNode.Weights.Length; i++) factNode.Weights[i] = factNode.Weights[i] / constNode.Value;
    840         return factNode;
     825        return MakeFactor(factNode.Symbol, factNode.VariableName, factNode.Weights.Select(w => w / constNode.Value));
    841826      } else if (IsFactor(a) && IsFactor(b) && AreSameVariable(a, b)) {
    842827        var node0 = a as FactorVariableTreeNode;
    843828        var node1 = b as FactorVariableTreeNode;
    844         for (int i = 0; i < node0.Weights.Length; i++) node0.Weights[i] = node0.Weights[i] / node1.Weights[i];
    845         return node0;
     829        return MakeFactor(node0.Symbol, node0.VariableName, node0.Weights.Zip(node1.Weights, (u, v) => u / v));
    846830      } else if (IsFactor(a)) {
    847831        return MakeFraction(MakeConstant(1.0), MakeProduct(b, Invert(a)));
     
    893877        var factNode = a as FactorVariableTreeNode;
    894878        var constNode = b as ConstantTreeNode;
    895         for (int i = 0; i < factNode.Weights.Length; i++) factNode.Weights[i] = factNode.Weights[i] + constNode.Value;
    896         return factNode;
     879        return MakeFactor(factNode.Symbol, factNode.VariableName, factNode.Weights.Select((w) => w + constNode.Value));
    897880      } else if (IsFactor(a) && IsFactor(b) && AreSameVariable(a, b)) {
    898881        var node0 = a as FactorVariableTreeNode;
    899882        var node1 = b as FactorVariableTreeNode;
    900         for (int i = 0; i < node0.Weights.Length; i++) node0.Weights[i] = node0.Weights[i] + node1.Weights[i];
    901         return node0;
     883        return MakeFactor(node0.Symbol, node0.VariableName, node0.Weights.Zip(node1.Weights, (u, v) => u + v));
    902884      } else if (IsAddition(a) && IsAddition(b)) {
    903885        // merge additions
     
    10181000        var node0 = a as FactorVariableTreeNode;
    10191001        var node1 = b as FactorVariableTreeNode;
    1020         for (int i = 0; i < node0.Weights.Length; i++) node0.Weights[i] = node0.Weights[i] * node1.Weights[i];
    1021         return node0;
     1002        return MakeFactor(node0.Symbol, node0.VariableName, node0.Weights.Zip(node1.Weights, (u, v) => u * v));
    10221003      } else if (IsFactor(a) && IsConstant(b)) {
    10231004        var node0 = a as FactorVariableTreeNode;
    10241005        var node1 = b as ConstantTreeNode;
    1025         for (int i = 0; i < node0.Weights.Length; i++) node0.Weights[i] = node0.Weights[i] * node1.Value;
    1026         return node0;
     1006        return MakeFactor(node0.Symbol, node0.VariableName, node0.Weights.Select(w => w * node1.Value));
    10271007      } else if (IsConstant(b) && ((ConstantTreeNode)b).Value.IsAlmost(1.0)) {
    10281008        // $ * 1.0 => $
     
    12571237      return tree;
    12581238    }
     1239    private ISymbolicExpressionTreeNode MakeFactor(FactorVariable sy, string variableName, IEnumerable<double> weights) {
     1240      var tree = (FactorVariableTreeNode)sy.CreateTreeNode();
     1241      tree.VariableName = variableName;
     1242      tree.Weights = weights.ToArray();
     1243      return tree;
     1244    }
     1245
    12591246
    12601247    #endregion
Note: See TracChangeset for help on using the changeset viewer.