Changeset 16723 for branches/2521_ProblemRefactoring/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters
- Timestamp:
- 03/28/19 16:54:20 (6 years ago)
- Location:
- branches/2521_ProblemRefactoring
- Files:
-
- 9 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
branches/2521_ProblemRefactoring
- Property svn:mergeinfo changed
-
branches/2521_ProblemRefactoring/HeuristicLab.Problems.DataAnalysis.Symbolic
- Property svn:mergeinfo changed
-
branches/2521_ProblemRefactoring/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/InfixExpressionFormatter.cs
r16692 r16723 1 1 #region License Information 2 2 /* HeuristicLab 3 * Copyright (C) 2002-201 8Heuristic and Evolutionary Algorithms Laboratory (HEAL)3 * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 4 * 5 5 * This file is part of HeuristicLab. … … 21 21 22 22 using System; 23 using System.Collections.Generic;24 23 using System.Globalization; 25 24 using System.Linq; … … 28 27 using HeuristicLab.Core; 29 28 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 30 using H euristicLab.Persistence.Default.CompositeSerializers.Storable;29 using HEAL.Attic; 31 30 32 31 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { … … 34 33 /// Formats mathematical expressions in infix form. E.g. x1 * (3.0 * x2 + x3) 35 34 /// </summary> 36 [Storable Class]35 [StorableType("6FE2C83D-A594-4ABF-B101-5AEAEA6D3E3D")] 37 36 [Item("Infix Symbolic Expression Tree Formatter", "A string formatter that converts symbolic expression trees to infix expressions.")] 38 37 … … 41 40 42 41 [StorableConstructor] 43 private InfixExpressionFormatter( bool deserializing) : base(deserializing) { }42 private InfixExpressionFormatter(StorableConstructorFlag _) : base(_) { } 44 43 private InfixExpressionFormatter(InfixExpressionFormatter original, Cloner cloner) : base(original, cloner) { } 45 44 public InfixExpressionFormatter() … … 52 51 } 53 52 54 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") { 55 61 // skip root and start symbols 56 62 StringBuilder strBuilder = new StringBuilder(); 57 FormatRecursively(symbolicExpressionTree.Root.GetSubtree(0).GetSubtree(0), strBuilder );63 FormatRecursively(symbolicExpressionTree.Root.GetSubtree(0).GetSubtree(0), strBuilder, numberFormat, formatString); 58 64 return strBuilder.ToString(); 59 65 } 60 66 61 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) { 62 72 if (node.SubtreeCount > 1) { 63 73 var token = GetToken(node.Symbol); 64 if (token == "+" || token == "-" || token == "OR" || token == "XOR") { 74 // operators 75 if (token == "+" || token == "-" || token == "OR" || token == "XOR" || 76 token == "*" || token == "/" || token == "AND" || 77 token == "^") { 65 78 strBuilder.Append("("); 66 FormatRecursively(node.Subtrees.First(), strBuilder );79 FormatRecursively(node.Subtrees.First(), strBuilder, numberFormat, formatString); 67 80 68 81 foreach (var subtree in node.Subtrees.Skip(1)) { 69 82 strBuilder.Append(" ").Append(token).Append(" "); 70 FormatRecursively(subtree, strBuilder); 71 } 72 strBuilder.Append(")"); 73 74 } else if (token == "*" || token == "/" || token == "AND") { 75 strBuilder.Append("("); 76 FormatRecursively(node.Subtrees.First(), strBuilder); 77 78 foreach (var subtree in node.Subtrees.Skip(1)) { 79 strBuilder.Append(" ").Append(token).Append(" "); 80 FormatRecursively(subtree, strBuilder); 83 FormatRecursively(subtree, strBuilder, numberFormat, formatString); 81 84 } 82 85 strBuilder.Append(")"); … … 84 87 // function with multiple arguments 85 88 strBuilder.Append(token).Append("("); 86 FormatRecursively(node.Subtrees.First(), strBuilder );89 FormatRecursively(node.Subtrees.First(), strBuilder, numberFormat, formatString); 87 90 foreach (var subtree in node.Subtrees.Skip(1)) { 88 91 strBuilder.Append(", "); 89 FormatRecursively(subtree, strBuilder );92 FormatRecursively(subtree, strBuilder, numberFormat, formatString); 90 93 } 91 94 strBuilder.Append(")"); … … 95 98 if (token == "-" || token == "NOT") { 96 99 strBuilder.Append("(").Append(token).Append("("); 97 FormatRecursively(node.GetSubtree(0), strBuilder );100 FormatRecursively(node.GetSubtree(0), strBuilder, numberFormat, formatString); 98 101 strBuilder.Append("))"); 99 102 } else if (token == "/") { 100 103 strBuilder.Append("1/"); 101 FormatRecursively(node.GetSubtree(0), strBuilder );104 FormatRecursively(node.GetSubtree(0), strBuilder, numberFormat, formatString); 102 105 } else if (token == "+" || token == "*") { 103 FormatRecursively(node.GetSubtree(0), strBuilder );106 FormatRecursively(node.GetSubtree(0), strBuilder, numberFormat, formatString); 104 107 } else { 105 108 // function with only one argument 106 109 strBuilder.Append(token).Append("("); 107 FormatRecursively(node.GetSubtree(0), strBuilder );110 FormatRecursively(node.GetSubtree(0), strBuilder, numberFormat, formatString); 108 111 strBuilder.Append(")"); 109 112 } … … 114 117 if (!varNode.Weight.IsAlmost(1.0)) { 115 118 strBuilder.Append("("); 116 strBuilder.Append Format(CultureInfo.InvariantCulture, "{0}", varNode.Weight);119 strBuilder.Append(varNode.Weight.ToString(formatString, numberFormat)); 117 120 strBuilder.Append("*"); 118 121 } … … 124 127 } 125 128 strBuilder.Append(", ") 126 .AppendFormat( CultureInfo.InvariantCulture, "{0}", varNode.Lag)129 .AppendFormat(numberFormat, "{0}", varNode.Lag) 127 130 .Append(")"); 128 131 } else if (node.Symbol is Variable) { … … 130 133 if (!varNode.Weight.IsAlmost(1.0)) { 131 134 strBuilder.Append("("); 132 strBuilder.Append Format(CultureInfo.InvariantCulture, "{0}", varNode.Weight);135 strBuilder.Append(varNode.Weight.ToString(formatString, numberFormat)); 133 136 strBuilder.Append("*"); 134 137 } … … 149 152 } 150 153 strBuilder.AppendFormat("[{0}]", 151 string.Join(", ", factorNode.Weights.Select(w => w.ToString( CultureInfo.InvariantCulture))));154 string.Join(", ", factorNode.Weights.Select(w => w.ToString(formatString, numberFormat)))); 152 155 } else if (node.Symbol is BinaryFactorVariable) { 153 156 var factorNode = node as BinaryFactorVariableTreeNode; 154 157 if (!factorNode.Weight.IsAlmost(1.0)) { 155 158 strBuilder.Append("("); 156 strBuilder.Append Format(CultureInfo.InvariantCulture, "{0}", factorNode.Weight);159 strBuilder.Append(factorNode.Weight.ToString(formatString, numberFormat)); 157 160 strBuilder.Append("*"); 158 161 } … … 176 179 var constNode = node as ConstantTreeNode; 177 180 if (constNode.Value >= 0.0) 178 strBuilder.Append Format(CultureInfo.InvariantCulture, "{0}", constNode.Value);181 strBuilder.Append(constNode.Value.ToString(formatString, numberFormat)); 179 182 else 180 strBuilder.Append Format(CultureInfo.InvariantCulture, "({0})", constNode.Value); // (-1183 strBuilder.Append("(").Append(constNode.Value.ToString(formatString, numberFormat)).Append(")"); // (-1 181 184 } 182 185 } 183 186 } 184 187 185 private st ring GetToken(ISymbol symbol) {186 var tok = InfixExpressionParser.knownSymbols.GetBySecond(symbol). SingleOrDefault();188 private static string GetToken(ISymbol symbol) { 189 var tok = InfixExpressionParser.knownSymbols.GetBySecond(symbol).FirstOrDefault(); 187 190 if (tok == null) 188 191 throw new ArgumentException(string.Format("Unknown symbol {0} found.", symbol.Name)); -
branches/2521_ProblemRefactoring/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionCSharpFormatter.cs
r16692 r16723 1 1 #region License Information 2 2 /* HeuristicLab 3 * Copyright (C) 2002-201 8Heuristic and Evolutionary Algorithms Laboratory (HEAL)3 * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 4 * 5 5 * This file is part of HeuristicLab. … … 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 HeuristicLab.Persistence.Default.CompositeSerializers.Storable;32 32 33 33 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { 34 34 [Item("C# Symbolic Expression Tree Formatter", "A string formatter that converts symbolic expression trees to C# code.")] 35 [Storable Class]35 [StorableType("88298836-6087-405A-9354-D4E6864887EB")] 36 36 public sealed class CSharpSymbolicExpressionTreeStringFormatter : NamedItem, ISymbolicExpressionTreeStringFormatter { 37 37 [StorableConstructor] 38 private CSharpSymbolicExpressionTreeStringFormatter( bool deserializing) : base(deserializing) { }38 private CSharpSymbolicExpressionTreeStringFormatter(StorableConstructorFlag _) : base(_) { } 39 39 private CSharpSymbolicExpressionTreeStringFormatter(CSharpSymbolicExpressionTreeStringFormatter original, Cloner cloner) : base(original, cloner) { } 40 40 public CSharpSymbolicExpressionTreeStringFormatter() … … 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/2521_ProblemRefactoring/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionExcelFormatter.cs
r16692 r16723 1 1 #region License Information 2 2 /* HeuristicLab 3 * Copyright (C) 2002-201 8Heuristic and Evolutionary Algorithms Laboratory (HEAL)3 * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 4 * 5 5 * This file is part of HeuristicLab. … … 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 HeuristicLab.Persistence.Default.CompositeSerializers.Storable;31 31 32 32 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { 33 33 [Item("Excel String Formatter", "String formatter for string representations of symbolic data analysis expressions in Excel syntax.")] 34 [Storable Class]34 [StorableType("46C46897-9C92-4CF1-81C9-700732700DD3")] 35 35 public sealed class SymbolicDataAnalysisExpressionExcelFormatter : NamedItem, ISymbolicExpressionTreeStringFormatter { 36 36 [StorableConstructor] 37 private SymbolicDataAnalysisExpressionExcelFormatter( bool deserializing) : base(deserializing) { }37 private SymbolicDataAnalysisExpressionExcelFormatter(StorableConstructorFlag _) : base(_) { } 38 38 private SymbolicDataAnalysisExpressionExcelFormatter(SymbolicDataAnalysisExpressionExcelFormatter original, Cloner cloner) : base(original, cloner) { } 39 39 public SymbolicDataAnalysisExpressionExcelFormatter() … … 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/2521_ProblemRefactoring/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionLatexFormatter.cs
r16692 r16723 1 1 #region License Information 2 2 /* HeuristicLab 3 * Copyright (C) 2002-201 8Heuristic and Evolutionary Algorithms Laboratory (HEAL)3 * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 4 * 5 5 * This file is part of HeuristicLab. … … 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 HeuristicLab.Persistence.Default.CompositeSerializers.Storable;30 30 31 31 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { 32 32 [Item("LaTeX String Formatter", "Formatter for symbolic expression trees for import into LaTeX documents.")] 33 [Storable Class]33 [StorableType("D7186DFF-1596-4A58-B27D-974DF0D93E4F")] 34 34 public sealed class SymbolicDataAnalysisExpressionLatexFormatter : NamedItem, ISymbolicExpressionTreeStringFormatter { 35 35 private readonly List<KeyValuePair<string, double>> constants; … … 41 41 42 42 [StorableConstructor] 43 private SymbolicDataAnalysisExpressionLatexFormatter( bool deserializing) : base(deserializing) { }43 private SymbolicDataAnalysisExpressionLatexFormatter(StorableConstructorFlag _) : base(_) { } 44 44 private SymbolicDataAnalysisExpressionLatexFormatter(SymbolicDataAnalysisExpressionLatexFormatter original, Cloner cloner) 45 45 : base(original, cloner) { … … 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/2521_ProblemRefactoring/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionMATLABFormatter.cs
r16692 r16723 1 1 #region License Information 2 2 /* HeuristicLab 3 * Copyright (C) 2002-201 8Heuristic and Evolutionary Algorithms Laboratory (HEAL)3 * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 4 * 5 5 * This file is part of HeuristicLab. … … 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 HeuristicLab.Persistence.Default.CompositeSerializers.Storable;29 29 30 30 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { 31 31 32 32 [Item("MATLAB String Formatter", "String formatter for string representations of symbolic data analysis expressions in MATLAB syntax.")] 33 [Storable Class]33 [StorableType("ADFB0A37-412D-4DD4-A174-F0103ADD7972")] 34 34 public sealed class SymbolicDataAnalysisExpressionMATLABFormatter : NamedItem, ISymbolicExpressionTreeStringFormatter { 35 35 private int currentLag; 36 36 37 37 [StorableConstructor] 38 private SymbolicDataAnalysisExpressionMATLABFormatter( bool deserializing) : base(deserializing) { }38 private SymbolicDataAnalysisExpressionMATLABFormatter(StorableConstructorFlag _) : base(_) { } 39 39 private SymbolicDataAnalysisExpressionMATLABFormatter(SymbolicDataAnalysisExpressionMATLABFormatter original, Cloner cloner) : base(original, cloner) { } 40 40 public SymbolicDataAnalysisExpressionMATLABFormatter() … … 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/2521_ProblemRefactoring/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionMathematicaFormatter.cs
r16692 r16723 1 1 #region License Information 2 2 /* HeuristicLab 3 * Copyright (C) 2002-201 8Heuristic and Evolutionary Algorithms Laboratory (HEAL)3 * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 4 * 5 5 * This file is part of HeuristicLab. … … 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 HeuristicLab.Persistence.Default.CompositeSerializers.Storable;30 30 31 31 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { 32 32 [Item("Mathematica Symbolic Expression Tree Formatter", "A string formatter that converts symbolic expression trees to Mathematica expressions.")] 33 [Storable Class]33 [StorableType("818A9294-FA95-41F6-A5F0-D7D050BDD076")] 34 34 public sealed class SymbolicDataAnalysisExpressionMathematicaFormatter : NamedItem, ISymbolicExpressionTreeStringFormatter { 35 35 [StorableConstructor] 36 private SymbolicDataAnalysisExpressionMathematicaFormatter( bool deserializing) : base(deserializing) { }36 private SymbolicDataAnalysisExpressionMathematicaFormatter(StorableConstructorFlag _) : base(_) { } 37 37 private SymbolicDataAnalysisExpressionMathematicaFormatter(SymbolicDataAnalysisExpressionMathematicaFormatter original, Cloner cloner) : base(original, cloner) { } 38 38 public SymbolicDataAnalysisExpressionMathematicaFormatter() … … 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/2521_ProblemRefactoring/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionSmalltalkFormatter.cs
r16692 r16723 1 1 #region License Information 2 2 /* HeuristicLab 3 * Copyright (C) 2002-201 8Heuristic and Evolutionary Algorithms Laboratory (HEAL)3 * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 4 * 5 5 * This file is part of HeuristicLab. … … 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.