Changeset 14339
 Timestamp:
 10/18/16 22:09:39 (3 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

branches/symbregfactors2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisExpressionTreeSimplifier.cs
r14259 r14339 651 651 } else if (IsFactor(node)) { 652 652 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))); 655 654 } else if (IsLog(node)) { 656 655 return node.GetSubtree(0); … … 672 671 } else if (IsFactor(node)) { 673 672 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)); 676 674 } else if (IsSquareRoot(node)) { 677 675 return node.GetSubtree(0); … … 689 687 } else if (IsFactor(node)) { 690 688 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))); 693 690 } else if (IsSquare(node)) { 694 691 return node.GetSubtree(0); … … 706 703 } else if (IsFactor(node)) { 707 704 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))); 710 706 } else if (IsExp(node)) { 711 707 return node.GetSubtree(0); … … 727 723 var factNode = a as FactorVariableTreeNode; 728 724 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)))); 732 727 } else if (IsConstant(a) && IsFactor(b)) { 733 728 var constNode = a as ConstantTreeNode; 734 729 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)))); 738 731 } else if (IsFactor(a) && IsFactor(b) && AreSameVariable(a, b)) { 739 732 var node0 = a as FactorVariableTreeNode; 740 733 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)))); 744 735 } else if (IsConstant(b)) { 745 736 var constB = b as ConstantTreeNode; … … 770 761 } 771 762 763 772 764 private ISymbolicExpressionTreeNode MakePower(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) { 773 765 if (IsConstant(a) && IsConstant(b)) { … … 778 770 var factNode = a as FactorVariableTreeNode; 779 771 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)))); 783 773 } else if (IsConstant(a) && IsFactor(b)) { 784 774 var constNode = a as ConstantTreeNode; 785 775 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)))); 789 777 } else if (IsFactor(a) && IsFactor(b) && AreSameVariable(a, b)) { 790 778 var node0 = a as FactorVariableTreeNode; 791 779 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)))); 795 781 } else if (IsConstant(b)) { 796 782 var constB = b as ConstantTreeNode; … … 837 823 var factNode = a as FactorVariableTreeNode; 838 824 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)); 841 826 } else if (IsFactor(a) && IsFactor(b) && AreSameVariable(a, b)) { 842 827 var node0 = a as FactorVariableTreeNode; 843 828 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)); 846 830 } else if (IsFactor(a)) { 847 831 return MakeFraction(MakeConstant(1.0), MakeProduct(b, Invert(a))); … … 893 877 var factNode = a as FactorVariableTreeNode; 894 878 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)); 897 880 } else if (IsFactor(a) && IsFactor(b) && AreSameVariable(a, b)) { 898 881 var node0 = a as FactorVariableTreeNode; 899 882 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)); 902 884 } else if (IsAddition(a) && IsAddition(b)) { 903 885 // merge additions … … 1018 1000 var node0 = a as FactorVariableTreeNode; 1019 1001 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)); 1022 1003 } else if (IsFactor(a) && IsConstant(b)) { 1023 1004 var node0 = a as FactorVariableTreeNode; 1024 1005 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)); 1027 1007 } else if (IsConstant(b) && ((ConstantTreeNode)b).Value.IsAlmost(1.0)) { 1028 1008 // $ * 1.0 => $ … … 1257 1237 return tree; 1258 1238 } 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 1259 1246 1260 1247 #endregion
Note: See TracChangeset
for help on using the changeset viewer.