Changeset 18172
Legend:
- Unmodified
- Added
- Removed
-
trunk/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/InfixExpressionFormatter.cs
r18132 r18172 37 37 var token = GetToken(node.Symbol); 38 38 // operators 39 if (token == "+" || token == "-" || token == "OR" || token == "XOR" || 40 token == "*" || token == "/" || token == "AND") { 41 strBuilder.Append("("); 39 if (token == "+" || token == "-" || token == "OR" || token == "XOR") { 40 var parenthesisRequired = false; 41 if (node.Parent != null && node.Parent.SubtreeCount > 1) { 42 var parentOp = GetToken(node.Parent.Symbol); 43 if (parentOp != "+" && parentOp != "-" && parentOp != "OR" && parentOp != "XOR") 44 parenthesisRequired = true; 45 } 46 if (parenthesisRequired) strBuilder.Append("("); 42 47 FormatRecursively(node.Subtrees.First(), strBuilder, numberFormat, formatString, parameters); 43 48 … … 47 52 } 48 53 49 strBuilder.Append(")"); 54 if (parenthesisRequired) strBuilder.Append(")"); 55 } else if (token == "*" || token == "/" || token == "AND") { 56 FormatRecursively(node.Subtrees.First(), strBuilder, numberFormat, formatString, parameters); 57 58 foreach (var subtree in node.Subtrees.Skip(1)) { 59 strBuilder.Append(" ").Append(token).Append(" "); 60 // a / b * c => a / (b * C) 61 if (subtree.SubtreeCount > 1 && token == "/" && GetToken(subtree.Symbol) == "*") { 62 strBuilder.Append("("); 63 FormatRecursively(subtree, strBuilder, numberFormat, formatString, parameters); 64 strBuilder.Append(")"); 65 } else { 66 FormatRecursively(subtree, strBuilder, numberFormat, formatString, parameters); 67 } 68 } 50 69 } else if (token == "^") { 51 70 // handle integer powers directly 52 strBuilder.Append("(");53 71 FormatRecursively(node.Subtrees.First(), strBuilder, numberFormat, formatString, parameters); 54 72 55 73 var power = node.GetSubtree(1); 56 if (power is INumericTreeNode numNode && Math.Truncate(numNode.Value) == numNode.Value) {74 if (power is INumericTreeNode numNode && Math.Truncate(numNode.Value) == numNode.Value) { 57 75 strBuilder.Append(" ").Append(token).Append(" ").Append(numNode.Value.ToString(formatString, numberFormat)); 58 76 } else { … … 60 78 FormatRecursively(power, strBuilder, numberFormat, formatString, parameters); 61 79 } 62 63 strBuilder.Append(")");64 80 } else { 65 81 // function with multiple arguments … … 76 92 var token = GetToken(node.Symbol); 77 93 if (token == "-" || token == "NOT") { 78 strBuilder.Append("(").Append(token).Append("("); 79 FormatRecursively(node.GetSubtree(0), strBuilder, numberFormat, formatString, parameters); 80 strBuilder.Append("))"); 94 strBuilder.Append(token); 95 FormatRecursively(node.GetSubtree(0), strBuilder, numberFormat, formatString, parameters); 81 96 } else if (token == "/") { 82 97 strBuilder.Append("1/"); … … 95 110 var varNode = node as LaggedVariableTreeNode; 96 111 if (!varNode.Weight.IsAlmost(1.0)) { 97 strBuilder.Append("(");98 112 AppendNumber(strBuilder, parameters, varNode.Weight, formatString, numberFormat); 99 113 strBuilder.Append("*"); … … 105 119 .AppendFormat(numberFormat, "{0}", varNode.Lag) 106 120 .Append(")"); 107 if (!varNode.Weight.IsAlmost(1.0)) strBuilder.Append(")");108 121 } else if (node.Symbol is Variable) { 109 122 var varNode = node as VariableTreeNode; 110 123 if (!varNode.Weight.IsAlmost(1.0)) { 111 strBuilder.Append("(");112 124 AppendNumber(strBuilder, parameters, varNode.Weight, formatString, numberFormat); 113 125 strBuilder.Append("*"); 114 126 } 115 116 127 AppendVariableName(strBuilder, varNode.VariableName); 117 118 if (!varNode.Weight.IsAlmost(1.0)) strBuilder.Append(")");119 128 } else if (node.Symbol is FactorVariable) { 120 129 var factorNode = node as FactorVariableTreeNode; … … 130 139 var factorNode = node as BinaryFactorVariableTreeNode; 131 140 if (!factorNode.Weight.IsAlmost(1.0)) { 132 strBuilder.Append("(");133 141 AppendNumber(strBuilder, parameters, factorNode.Weight, formatString, numberFormat); 134 135 142 strBuilder.Append("*"); 136 143 } 137 138 144 AppendVariableName(strBuilder, factorNode.VariableName); 139 145 strBuilder.Append(" = "); 140 146 AppendVariableName(strBuilder, factorNode.VariableValue); 141 142 if (!factorNode.Weight.IsAlmost(1.0)) strBuilder.Append(")");143 147 } else if (node is INumericTreeNode numNode) { 144 148 if (parameters == null && numNode.Value < 0) { 145 149 // negative value 146 strBuilder.Append("(").Append(numNode.Value.ToString(formatString, numberFormat)) 147 .Append(")"); 150 strBuilder.Append(numNode.Value.ToString(formatString, numberFormat)); 148 151 } else { 149 152 AppendNumber(strBuilder, parameters, numNode.Value, formatString, numberFormat); -
trunk/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4/DeriveTest.cs
r18170 r18172 43 43 Assert.AreEqual("20", Derive("<num=10>*x+<num=20>*y", "y")); 44 44 Assert.AreEqual("6", Derive("<num=2>*<num=3>*x", "x")); 45 Assert.AreEqual("10 *'y'", Derive("<num=10>*x*y+<num=20>*y", "x"));46 Assert.AreEqual(" (1 / (SQR('x') * (-1)))", Derive("1/x", "x"));47 Assert.AreEqual(" 'y' / (SQR('x') * -1)", Derive("y/x", "x"));48 Assert.AreEqual("( -2*'x' + -1) * ('a' + 'b') / SQR('x' + 'x' * 'x')",45 Assert.AreEqual("10*'y'", Derive("<num=10>*x*y+<num=20>*y", "x")); 46 Assert.AreEqual("1 * -1 / SQR('x')", Derive("1/x", "x")); 47 Assert.AreEqual("-1*'y' / SQR('x')", Derive("y/x", "x")); 48 Assert.AreEqual("('a' + 'b') * (-2*'x' + -1) / SQR('x' + 'x' * 'x')", 49 49 Derive("(a+b)/(x+x*x)", "x")); 50 Assert.AreEqual("( -2*'x' + -1) * ('a' + 'b') / SQR('x' + SQR('x'))", Derive("(a+b)/(x+SQR(x))", "x"));50 Assert.AreEqual("('a' + 'b') * (-2*'x' + -1) / SQR('x' + SQR('x'))", Derive("(a+b)/(x+SQR(x))", "x")); 51 51 Assert.AreEqual("EXP('x')", Derive("exp(x)", "x")); 52 52 Assert.AreEqual("EXP(3*'x') * 3", Derive("exp(<num=3>*x)", "x")); … … 67 67 68 68 Assert.AreEqual("'a' * 'b' * 'c'", Derive("a*b*c*d", "d")); 69 Assert.AreEqual("' a' / ('b' * 'c' * SQR('d') * -1)", Derive("a/b/c/d", "d"));69 Assert.AreEqual("'b' * 'c' * -1*'a' / (SQR('b') * SQR('c') * SQR('d'))", Derive("a/b/c/d", "d")); // TODO simplifier should be able to simplify this 70 70 71 71 Assert.AreEqual("'x' * (SQR(TANH(SQR('x'))) * -1 + 1) * 2", Derive("tanh(sqr(x))", "x")); // (2*'x'*(1 - SQR(TANH(SQR('x')))) … … 84 84 root.AddSubtree(start); 85 85 var t = new SymbolicExpressionTree(root); 86 Assert.AreEqual(" (1 / (SQR('x') * (-1)))",86 Assert.AreEqual("1 / (SQR('x') * -1)", 87 87 formatter.Format(DerivativeCalculator.Derive(t, "x"))); 88 88 }
Note: See TracChangeset
for help on using the changeset viewer.