Changeset 16700
- Timestamp:
- 03/20/19 10:17:30 (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/InfixExpressionFormatter.cs
r16565 r16700 51 51 } 52 52 53 public string Format(ISymbolicExpressionTree symbolicExpressionTree) { 53 /// <summary> 54 /// Produces an infix expression for a given expression tree. 55 /// </summary> 56 /// <param name="symbolicExpressionTree">The tree representation of the expression.</param> 57 /// <param name="numberFormat">Number format that should be used for numeric parameters (e.g. NumberFormatInfo.InvariantInfo (default)).</param> 58 /// <param name="formatString">The format string for numeric parameters (e.g. \"G4\" to limit to 4 digits, default is \"G\")</param> 59 /// <returns>Infix expression</returns> 60 public string Format(ISymbolicExpressionTree symbolicExpressionTree, NumberFormatInfo numberFormat, string formatString="G") { 54 61 // skip root and start symbols 55 62 StringBuilder strBuilder = new StringBuilder(); 56 FormatRecursively(symbolicExpressionTree.Root.GetSubtree(0).GetSubtree(0), strBuilder );63 FormatRecursively(symbolicExpressionTree.Root.GetSubtree(0).GetSubtree(0), strBuilder, numberFormat, formatString); 57 64 return strBuilder.ToString(); 58 65 } 59 66 60 private void FormatRecursively(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) { 67 public string Format(ISymbolicExpressionTree symbolicExpressionTree) { 68 return Format(symbolicExpressionTree, NumberFormatInfo.InvariantInfo); 69 } 70 71 private static void FormatRecursively(ISymbolicExpressionTreeNode node, StringBuilder strBuilder, NumberFormatInfo numberFormat, string formatString) { 61 72 if (node.SubtreeCount > 1) { 62 73 var token = GetToken(node.Symbol); … … 66 77 token == "^") { 67 78 strBuilder.Append("("); 68 FormatRecursively(node.Subtrees.First(), strBuilder );79 FormatRecursively(node.Subtrees.First(), strBuilder, numberFormat, formatString); 69 80 70 81 foreach (var subtree in node.Subtrees.Skip(1)) { 71 82 strBuilder.Append(" ").Append(token).Append(" "); 72 FormatRecursively(subtree, strBuilder );83 FormatRecursively(subtree, strBuilder, numberFormat, formatString); 73 84 } 74 85 strBuilder.Append(")"); … … 76 87 // function with multiple arguments 77 88 strBuilder.Append(token).Append("("); 78 FormatRecursively(node.Subtrees.First(), strBuilder );89 FormatRecursively(node.Subtrees.First(), strBuilder, numberFormat, formatString); 79 90 foreach (var subtree in node.Subtrees.Skip(1)) { 80 91 strBuilder.Append(", "); 81 FormatRecursively(subtree, strBuilder );92 FormatRecursively(subtree, strBuilder, numberFormat, formatString); 82 93 } 83 94 strBuilder.Append(")"); … … 87 98 if (token == "-" || token == "NOT") { 88 99 strBuilder.Append("(").Append(token).Append("("); 89 FormatRecursively(node.GetSubtree(0), strBuilder );100 FormatRecursively(node.GetSubtree(0), strBuilder, numberFormat, formatString); 90 101 strBuilder.Append("))"); 91 102 } else if (token == "/") { 92 103 strBuilder.Append("1/"); 93 FormatRecursively(node.GetSubtree(0), strBuilder );104 FormatRecursively(node.GetSubtree(0), strBuilder, numberFormat, formatString); 94 105 } else if (token == "+" || token == "*") { 95 FormatRecursively(node.GetSubtree(0), strBuilder );106 FormatRecursively(node.GetSubtree(0), strBuilder, numberFormat, formatString); 96 107 } else { 97 108 // function with only one argument 98 109 strBuilder.Append(token).Append("("); 99 FormatRecursively(node.GetSubtree(0), strBuilder );110 FormatRecursively(node.GetSubtree(0), strBuilder, numberFormat, formatString); 100 111 strBuilder.Append(")"); 101 112 } … … 106 117 if (!varNode.Weight.IsAlmost(1.0)) { 107 118 strBuilder.Append("("); 108 strBuilder.Append Format(CultureInfo.InvariantCulture, "{0}", varNode.Weight);119 strBuilder.Append(varNode.Weight.ToString(formatString, numberFormat)); 109 120 strBuilder.Append("*"); 110 121 } … … 116 127 } 117 128 strBuilder.Append(", ") 118 .AppendFormat( CultureInfo.InvariantCulture, "{0}", varNode.Lag)129 .AppendFormat(numberFormat, "{0}", varNode.Lag) 119 130 .Append(")"); 120 131 } else if (node.Symbol is Variable) { … … 122 133 if (!varNode.Weight.IsAlmost(1.0)) { 123 134 strBuilder.Append("("); 124 strBuilder.Append Format(CultureInfo.InvariantCulture, "{0}", varNode.Weight);135 strBuilder.Append(varNode.Weight.ToString(formatString, numberFormat)); 125 136 strBuilder.Append("*"); 126 137 } … … 141 152 } 142 153 strBuilder.AppendFormat("[{0}]", 143 string.Join(", ", factorNode.Weights.Select(w => w.ToString( CultureInfo.InvariantCulture))));154 string.Join(", ", factorNode.Weights.Select(w => w.ToString(formatString, numberFormat)))); 144 155 } else if (node.Symbol is BinaryFactorVariable) { 145 156 var factorNode = node as BinaryFactorVariableTreeNode; 146 157 if (!factorNode.Weight.IsAlmost(1.0)) { 147 158 strBuilder.Append("("); 148 strBuilder.Append Format(CultureInfo.InvariantCulture, "{0}", factorNode.Weight);159 strBuilder.Append(factorNode.Weight.ToString(formatString, numberFormat)); 149 160 strBuilder.Append("*"); 150 161 } … … 168 179 var constNode = node as ConstantTreeNode; 169 180 if (constNode.Value >= 0.0) 170 strBuilder.Append Format(CultureInfo.InvariantCulture, "{0}", constNode.Value);181 strBuilder.Append(constNode.Value.ToString(formatString, numberFormat)); 171 182 else 172 strBuilder.Append Format(CultureInfo.InvariantCulture, "({0})", constNode.Value); // (-1183 strBuilder.Append("(").Append(constNode.Value.ToString(formatString, numberFormat)).Append(")"); // (-1 173 184 } 174 185 } 175 186 } 176 187 177 private st ring GetToken(ISymbol symbol) {188 private static string GetToken(ISymbol symbol) { 178 189 var tok = InfixExpressionParser.knownSymbols.GetBySecond(symbol).FirstOrDefault(); 179 190 if (tok == null)
Note: See TracChangeset
for help on using the changeset viewer.