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