Changeset 17862
- Timestamp:
- 03/09/21 08:11:24 (4 years ago)
- Location:
- stable
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
stable
- Property svn:mergeinfo changed
/trunk merged: 17796-17797,17820
- Property svn:mergeinfo changed
-
stable/HeuristicLab.Problems.DataAnalysis.Symbolic
- Property svn:mergeinfo changed
/trunk/HeuristicLab.Problems.DataAnalysis.Symbolic merged: 17797,17820
- Property svn:mergeinfo changed
-
stable/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Converters/TreeSimplifier.cs
r17181 r17862 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 } -
stable/HeuristicLab.Tests
- Property svn:mergeinfo changed
/trunk/HeuristicLab.Tests merged: 17796-17797,17820
- Property svn:mergeinfo changed
-
stable/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4/SymbolicDataAnalysisExpressionTreeSimplifierTest.cs
r17181 r17862 99 99 #region root rules 100 100 // cancellation 101 AssertEqualAfterSimplification("(root (variable 2.0 a) 0.0)", " 1.0");101 AssertEqualAfterSimplification("(root (variable 2.0 a) 0.0)", "NaN"); 102 102 // fixed point 103 103 AssertEqualAfterSimplification("(root (variable 2.0 a) 1.0)", "(variable 2.0 a)"); … … 269 269 270 270 #endregion 271 272 #region do not drop subtrees with small weights 273 AssertEqualAfterSimplification("(* 1e-14 (variable 1.0 a))", "(variable 1e-14 a)"); 274 AssertEqualAfterSimplification("(+ (variable 1.0 a) 1e-14)", 275 "(+ (variable 1.0 a) 1e-14)"); 276 // a scenario where a term with small weight can have large effect 277 AssertEqualAfterSimplification("(+ (* (pow (variable 1.0 a) 10) 1e-14) 1.0)", 278 "(+ (* (pow (variable 1.0 a) 10) 1e-14) 1.0)"); 279 // a test case (from ticket #2985) 280 AssertEqualAfterSimplification("(+ (* (exp (variable 3.5861E+001 a)) 5.5606E-016) 5.9323E-002)", 281 "(+ (* (exp (variable 3.5861E+001 a)) 5.5606E-016) 5.9323E-002)"); 282 #endregion 271 283 } 272 284
Note: See TracChangeset
for help on using the changeset viewer.