Changeset 18006 for branches/3087_Ceres_Integration/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Converters/TreeSimplifier.cs
- Timestamp:
- 07/13/21 10:55:09 (3 years ago)
- Location:
- branches/3087_Ceres_Integration
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3087_Ceres_Integration
- Property svn:mergeinfo changed
-
branches/3087_Ceres_Integration/HeuristicLab.Problems.DataAnalysis.Symbolic
- Property svn:mergeinfo changed
-
branches/3087_Ceres_Integration/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Converters/TreeSimplifier.cs
r17180 r18006 784 784 } else if (IsExp(node)) { 785 785 return MakeExp(MakeProduct(node.GetSubtree(0), MakeConstant(2.0))); // sqr(exp(x)) = exp(2x) 786 } else if (IsSquare(node)) { 787 return MakePower(node.GetSubtree(0), MakeConstant(4)); 786 788 } else if (IsCube(node)) { 787 789 return MakePower(node.GetSubtree(0), MakeConstant(6)); … … 809 811 } else if (IsSquare(node)) { 810 812 return MakePower(node.GetSubtree(0), MakeConstant(6)); 813 } else if (IsCube(node)) { 814 return MakePower(node.GetSubtree(0), MakeConstant(9)); 811 815 } else { 812 816 var cubeNode = cubeSymbol.CreateTreeNode(); … … 933 937 var constB = b as ConstantTreeNode; 934 938 var constBValue = Math.Round(constB.Value); 935 if (constBValue.IsAlmost(1.0)) { 939 if (constBValue == 1.0) { 940 // root(a, 1) => a 936 941 return a; 937 } else if (constBValue.IsAlmost(0.0)) { 938 return MakeConstant(1.0); 939 } else if (constBValue.IsAlmost(-1.0)) { 942 } else if (constBValue == 0.0) { 943 // root(a, 0) is not defined 944 //return MakeConstant(1.0); 945 return MakeConstant(double.NaN); 946 } else if (constBValue == -1.0) { 947 // root(a, -1) => a^(-1/1) => 1/a 940 948 return MakeFraction(MakeConstant(1.0), a); 941 949 } else if (constBValue < 0) { 950 // root(a, -b) => a^(-1/b) => (1/a)^(1/b) => root(1, b) / root(a, b) => 1 / root(a, b) 942 951 var rootNode = rootSymbol.CreateTreeNode(); 943 952 rootNode.AddSubtree(a); … … 987 996 var constB = b as ConstantTreeNode; 988 997 double exponent = Math.Round(constB.Value); 989 if (exponent.IsAlmost(0.0)) { 998 if (exponent == 0.0) { 999 // a^0 => 1 990 1000 return MakeConstant(1.0); 991 } else if (exponent.IsAlmost(1.0)) { 1001 } else if (exponent == 1.0) { 1002 // a^1 => a 992 1003 return a; 993 } else if (exponent.IsAlmost(-1.0)) { 1004 } else if (exponent == -1.0) { 1005 // a^-1 => 1/a 994 1006 return MakeFraction(MakeConstant(1.0), a); 995 1007 } else if (exponent < 0) { 1008 // a^-b => (1/a)^b => 1/(a^b) 996 1009 var powNode = powSymbol.CreateTreeNode(); 997 1010 powNode.AddSubtree(a); … … 1018 1031 // fold constants 1019 1032 return MakeConstant(((ConstantTreeNode)a).Value / ((ConstantTreeNode)b).Value); 1020 } else if ((IsConstant(a) && !((ConstantTreeNode)a).Value.IsAlmost(1.0))) { 1033 } else if ((IsConstant(a) && ((ConstantTreeNode)a).Value != 1.0)) { 1034 // a / x => (a * 1/a) / (x * 1/a) => 1 / (x * 1/a) 1021 1035 return MakeFraction(MakeConstant(1.0), MakeProduct(b, Invert(a))); 1022 1036 } else if (IsVariableBase(a) && IsConstant(b)) { … … 1094 1108 // b is not constant => make sure constant is on the right 1095 1109 return MakeSum(b, a); 1096 } else if (IsConstant(b) && ((ConstantTreeNode)b).Value .IsAlmost(0.0)) {1110 } else if (IsConstant(b) && ((ConstantTreeNode)b).Value == 0.0) { 1097 1111 // x + 0 => x 1098 1112 return a; … … 1210 1224 foreach (var unchangedSubtree in unchangedSubtrees) 1211 1225 sum.AddSubtree(unchangedSubtree); 1212 if ( !constant.IsAlmost(0.0)) {1226 if (constant != 0.0) { 1213 1227 sum.AddSubtree(MakeConstant(constant)); 1214 1228 } … … 1268 1282 if (wi < 0) throw new ArgumentException(); 1269 1283 return MakeBinFactor(node1.Symbol, node1.VariableName, node1.VariableValue, node1.Weight * node0.Weights[wi]); 1270 } else if (IsConstant(b) && ((ConstantTreeNode)b).Value .IsAlmost(1.0)) {1284 } else if (IsConstant(b) && ((ConstantTreeNode)b).Value == 1.0) { 1271 1285 // $ * 1.0 => $ 1272 1286 return a; 1273 } else if (IsConstant(b) && ((ConstantTreeNode)b).Value .IsAlmost(0.0)) {1287 } else if (IsConstant(b) && ((ConstantTreeNode)b).Value == 0.0) { 1274 1288 return MakeConstant(0); 1275 1289 } else if (IsConstant(b) && IsVariableBase(a)) { … … 1419 1433 prod.AddSubtree(unchangedSubtree); 1420 1434 1421 if ( !constantProduct.IsAlmost(1.0)) {1435 if (constantProduct != 1.0) { 1422 1436 prod.AddSubtree(MakeConstant(constantProduct)); 1423 1437 }
Note: See TracChangeset
for help on using the changeset viewer.