Changeset 17825 for branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Converters/TreeSimplifier.cs
- Timestamp:
- 01/27/21 14:10:56 (4 years ago)
- Location:
- branches/3040_VectorBasedGP
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3040_VectorBasedGP
- Property svn:mergeinfo changed
-
branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic
- Property svn:mergeinfo changed
-
branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Converters/TreeSimplifier.cs
r17180 r17825 933 933 var constB = b as ConstantTreeNode; 934 934 var constBValue = Math.Round(constB.Value); 935 if (constBValue.IsAlmost(1.0)) { 935 if (constBValue == 1.0) { 936 // root(a, 1) => a 936 937 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 940 944 return MakeFraction(MakeConstant(1.0), a); 941 945 } 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) 942 947 var rootNode = rootSymbol.CreateTreeNode(); 943 948 rootNode.AddSubtree(a); … … 987 992 var constB = b as ConstantTreeNode; 988 993 double exponent = Math.Round(constB.Value); 989 if (exponent.IsAlmost(0.0)) { 994 if (exponent == 0.0) { 995 // a^0 => 1 990 996 return MakeConstant(1.0); 991 } else if (exponent.IsAlmost(1.0)) { 997 } else if (exponent == 1.0) { 998 // a^1 => a 992 999 return a; 993 } else if (exponent.IsAlmost(-1.0)) { 1000 } else if (exponent == -1.0) { 1001 // a^-1 => 1/a 994 1002 return MakeFraction(MakeConstant(1.0), a); 995 1003 } else if (exponent < 0) { 1004 // a^-b => (1/a)^b => 1/(a^b) 996 1005 var powNode = powSymbol.CreateTreeNode(); 997 1006 powNode.AddSubtree(a); … … 1018 1027 // fold constants 1019 1028 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) 1021 1031 return MakeFraction(MakeConstant(1.0), MakeProduct(b, Invert(a))); 1022 1032 } else if (IsVariableBase(a) && IsConstant(b)) { … … 1094 1104 // b is not constant => make sure constant is on the right 1095 1105 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) { 1097 1107 // x + 0 => x 1098 1108 return a; … … 1210 1220 foreach (var unchangedSubtree in unchangedSubtrees) 1211 1221 sum.AddSubtree(unchangedSubtree); 1212 if ( !constant.IsAlmost(0.0)) {1222 if (constant != 0.0) { 1213 1223 sum.AddSubtree(MakeConstant(constant)); 1214 1224 } … … 1268 1278 if (wi < 0) throw new ArgumentException(); 1269 1279 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) { 1271 1281 // $ * 1.0 => $ 1272 1282 return a; 1273 } else if (IsConstant(b) && ((ConstantTreeNode)b).Value .IsAlmost(0.0)) {1283 } else if (IsConstant(b) && ((ConstantTreeNode)b).Value == 0.0) { 1274 1284 return MakeConstant(0); 1275 1285 } else if (IsConstant(b) && IsVariableBase(a)) { … … 1419 1429 prod.AddSubtree(unchangedSubtree); 1420 1430 1421 if ( !constantProduct.IsAlmost(1.0)) {1431 if (constantProduct != 1.0) { 1422 1432 prod.AddSubtree(MakeConstant(constantProduct)); 1423 1433 }
Note: See TracChangeset
for help on using the changeset viewer.