Changeset 18172 for trunk/HeuristicLab.Problems.DataAnalysis.Symbolic
- Timestamp:
- 12/27/21 10:59:13 (3 years ago)
- File:
-
- 1 edited
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);
Note: See TracChangeset
for help on using the changeset viewer.