Free cookie consent management tool by TermsFeed Policy Generator

Changeset 17075


Ignore:
Timestamp:
07/04/19 16:36:42 (5 years ago)
Author:
mkommend
Message:

#2998: Merged r16700 into stable.

Location:
stable
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • stable

  • stable/HeuristicLab.Problems.DataAnalysis.Symbolic

  • stable/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/InfixExpressionFormatter.cs

    r17072 r17075  
    5252    }
    5353
    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") {
    5562      // skip root and start symbols
    5663      StringBuilder strBuilder = new StringBuilder();
    57       FormatRecursively(symbolicExpressionTree.Root.GetSubtree(0).GetSubtree(0), strBuilder);
     64      FormatRecursively(symbolicExpressionTree.Root.GetSubtree(0).GetSubtree(0), strBuilder, numberFormat, formatString);
    5865      return strBuilder.ToString();
    5966    }
    6067
    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) {
    6273      if (node.SubtreeCount > 1) {
    6374        var token = GetToken(node.Symbol);
     
    6778            token == "^") {
    6879          strBuilder.Append("(");
    69           FormatRecursively(node.Subtrees.First(), strBuilder);
     80          FormatRecursively(node.Subtrees.First(), strBuilder, numberFormat, formatString);
    7081
    7182          foreach (var subtree in node.Subtrees.Skip(1)) {
    7283            strBuilder.Append(" ").Append(token).Append(" ");
    73             FormatRecursively(subtree, strBuilder);
     84            FormatRecursively(subtree, strBuilder, numberFormat, formatString);
    7485          }
    7586          strBuilder.Append(")");
     
    7788          // function with multiple arguments
    7889          strBuilder.Append(token).Append("(");
    79           FormatRecursively(node.Subtrees.First(), strBuilder);
     90          FormatRecursively(node.Subtrees.First(), strBuilder, numberFormat, formatString);
    8091          foreach (var subtree in node.Subtrees.Skip(1)) {
    8192            strBuilder.Append(", ");
    82             FormatRecursively(subtree, strBuilder);
     93            FormatRecursively(subtree, strBuilder, numberFormat, formatString);
    8394          }
    8495          strBuilder.Append(")");
     
    8899        if (token == "-" || token == "NOT") {
    89100          strBuilder.Append("(").Append(token).Append("(");
    90           FormatRecursively(node.GetSubtree(0), strBuilder);
     101          FormatRecursively(node.GetSubtree(0), strBuilder, numberFormat, formatString);
    91102          strBuilder.Append("))");
    92103        } else if (token == "/") {
    93104          strBuilder.Append("1/");
    94           FormatRecursively(node.GetSubtree(0), strBuilder);
     105          FormatRecursively(node.GetSubtree(0), strBuilder, numberFormat, formatString);
    95106        } else if (token == "+" || token == "*") {
    96           FormatRecursively(node.GetSubtree(0), strBuilder);
     107          FormatRecursively(node.GetSubtree(0), strBuilder, numberFormat, formatString);
    97108        } else {
    98109          // function with only one argument
    99110          strBuilder.Append(token).Append("(");
    100           FormatRecursively(node.GetSubtree(0), strBuilder);
     111          FormatRecursively(node.GetSubtree(0), strBuilder, numberFormat, formatString);
    101112          strBuilder.Append(")");
    102113        }
     
    107118          if (!varNode.Weight.IsAlmost(1.0)) {
    108119            strBuilder.Append("(");
    109             strBuilder.AppendFormat(CultureInfo.InvariantCulture, "{0}", varNode.Weight);
     120            strBuilder.Append(varNode.Weight.ToString(formatString, numberFormat));
    110121            strBuilder.Append("*");
    111122          }
     
    117128          }
    118129          strBuilder.Append(", ")
    119             .AppendFormat(CultureInfo.InvariantCulture, "{0}", varNode.Lag)
     130            .AppendFormat(numberFormat, "{0}", varNode.Lag)
    120131            .Append(")");
    121132        } else if (node.Symbol is Variable) {
     
    123134          if (!varNode.Weight.IsAlmost(1.0)) {
    124135            strBuilder.Append("(");
    125             strBuilder.AppendFormat(CultureInfo.InvariantCulture, "{0}", varNode.Weight);
     136            strBuilder.Append(varNode.Weight.ToString(formatString, numberFormat));
    126137            strBuilder.Append("*");
    127138          }
     
    142153          }
    143154          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))));
    145156        } else if (node.Symbol is BinaryFactorVariable) {
    146157          var factorNode = node as BinaryFactorVariableTreeNode;
    147158          if (!factorNode.Weight.IsAlmost(1.0)) {
    148159            strBuilder.Append("(");
    149             strBuilder.AppendFormat(CultureInfo.InvariantCulture, "{0}", factorNode.Weight);
     160            strBuilder.Append(factorNode.Weight.ToString(formatString, numberFormat));
    150161            strBuilder.Append("*");
    151162          }
     
    169180          var constNode = node as ConstantTreeNode;
    170181          if (constNode.Value >= 0.0)
    171             strBuilder.AppendFormat(CultureInfo.InvariantCulture, "{0}", constNode.Value);
     182            strBuilder.Append(constNode.Value.ToString(formatString, numberFormat));
    172183          else
    173             strBuilder.AppendFormat(CultureInfo.InvariantCulture, "({0})", constNode.Value); // (-1
     184            strBuilder.Append("(").Append(constNode.Value.ToString(formatString, numberFormat)).Append(")"); // (-1
    174185        }
    175186      }
    176187    }
    177188
    178     private string GetToken(ISymbol symbol) {
     189    private static string GetToken(ISymbol symbol) {
    179190      var tok = InfixExpressionParser.knownSymbols.GetBySecond(symbol).FirstOrDefault();
    180191      if (tok == null)
Note: See TracChangeset for help on using the changeset viewer.