Changeset 16722 for branches/2988_ModelsOfModels2/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters
- Timestamp:
- 03/28/19 11:22:24 (6 years ago)
- Location:
- branches/2988_ModelsOfModels2
- Files:
-
- 1 added
- 7 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
branches/2988_ModelsOfModels2/HeuristicLab.Problems.DataAnalysis.Symbolic
- Property svn:mergeinfo changed
-
branches/2988_ModelsOfModels2/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/InfixExpressionFormatter.cs
r16565 r16722 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) -
branches/2988_ModelsOfModels2/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionCSharpFormatter.cs
r16565 r16722 26 26 using System.Text; 27 27 using System.Text.RegularExpressions; 28 using HEAL.Attic; 28 29 using HeuristicLab.Common; 29 30 using HeuristicLab.Core; 30 31 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 31 using HEAL.Attic;32 32 33 33 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { … … 56 56 } 57 57 58 private string VariableName2Identifier(string name) { 58 private string VariableName2Identifier(string name) { 59 59 /* 60 60 * identifier-start-character: … … 131 131 } else if (node.Symbol is Tangent) { 132 132 FormatFunction(node, "Math.Tan", strBuilder); 133 } else if (node.Symbol is HyperbolicTangent) { 134 FormatFunction(node, "Math.Tanh", strBuilder); 133 135 } else if (node.Symbol is Square) { 134 136 FormatSquare(node, strBuilder); -
branches/2988_ModelsOfModels2/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionExcelFormatter.cs
r16565 r16722 25 25 using System.Linq; 26 26 using System.Text; 27 using HEAL.Attic; 27 28 using HeuristicLab.Common; 28 29 using HeuristicLab.Core; 29 30 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 30 using HEAL.Attic;31 31 32 32 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { … … 192 192 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 193 193 stringBuilder.Append(")"); 194 194 } else if (symbol is HyperbolicTangent) { 195 stringBuilder.Append("TANH("); 196 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 197 stringBuilder.Append(")"); 195 198 } else if (symbol is Variable) { 196 199 VariableTreeNode variableTreeNode = node as VariableTreeNode; -
branches/2988_ModelsOfModels2/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionLatexFormatter.cs
r16565 r16722 24 24 using System.Linq; 25 25 using System.Text; 26 using HEAL.Attic; 26 27 using HeuristicLab.Common; 27 28 using HeuristicLab.Core; 28 29 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 29 using HEAL.Attic;30 30 31 31 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { … … 137 137 } else if (node.Symbol is Tangent) { 138 138 strBuilder.Append(@"\tan \left( "); 139 } else if (node.Symbol is HyperbolicTangent) { 140 strBuilder.Append(@"\tanh \left( "); 139 141 } else if (node.Symbol is AiryA) { 140 142 strBuilder.Append(@"\operatorname{airy}_a \left( "); … … 303 305 } else if (node.Symbol is Tangent) { 304 306 throw new InvalidOperationException(); 307 } else if (node.Symbol is HyperbolicTangent) { 308 throw new InvalidOperationException(); 305 309 } else if (node.Symbol is AiryA) { 306 310 throw new InvalidOperationException(); … … 399 403 } else if (node.Symbol is Tangent) { 400 404 strBuilder.Append(@" \right) "); 405 } else if (node.Symbol is HyperbolicTangent) { 406 strBuilder.Append(@" \right) "); 401 407 } else if (node.Symbol is AiryA) { 402 408 strBuilder.Append(@" \right) "); -
branches/2988_ModelsOfModels2/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionMATLABFormatter.cs
r16565 r16722 23 23 using System.Linq; 24 24 using System.Text; 25 using HEAL.Attic; 25 26 using HeuristicLab.Common; 26 27 using HeuristicLab.Core; 27 28 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 28 using HEAL.Attic;29 29 30 30 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { … … 252 252 } else if (symbol is Tangent) { 253 253 stringBuilder.Append("tan("); 254 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 255 stringBuilder.Append(")"); 256 } else if (symbol is HyperbolicTangent) { 257 stringBuilder.Append("tanh("); 254 258 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 255 259 stringBuilder.Append(")"); -
branches/2988_ModelsOfModels2/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionMathematicaFormatter.cs
r16565 r16722 24 24 using System.Linq; 25 25 using System.Text; 26 using HEAL.Attic; 26 27 using HeuristicLab.Common; 27 28 using HeuristicLab.Core; 28 29 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 29 using HEAL.Attic;30 30 31 31 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { … … 70 70 } else if (node.Symbol is Tangent) { 71 71 FormatFunction(node, "Tan", strBuilder); 72 } else if (node.Symbol is HyperbolicTangent) { 73 FormatFunction(node, "Tanh", strBuilder); 72 74 } else if (node.Symbol is Exponential) { 73 75 FormatFunction(node, "Exp", strBuilder); -
branches/2988_ModelsOfModels2/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionSmalltalkFormatter.cs
r16565 r16722 160 160 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 161 161 stringBuilder.Append(" tan"); 162 } else if (symbol is HyperbolicTangent) { 163 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 164 stringBuilder.Append(" tanh"); 162 165 } else if (symbol is Variable) { 163 166 VariableTreeNode variableTreeNode = node as VariableTreeNode;
Note: See TracChangeset
for help on using the changeset viewer.