Free cookie consent management tool by TermsFeed Policy Generator

Changeset 4969 for branches


Ignore:
Timestamp:
11/28/10 11:08:22 (14 years ago)
Author:
gkronber
Message:

Improved LaTeX string formatter to support all available function symbols and output all constant values. #1152

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/DataAnalysis.Extensions/HeuristicLab.Problems.DataAnalysis/3.3/Symbolic/Formatters/SymbolicExpressionTreeLatexFormatter.cs

    r4906 r4969  
    3232
    3333namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Formatters {
    34   [Item("SymbolicExpressionTreeLatexFormatter", "Formatter for symbolic expression trees for import into LaTeX documents.")]
     34  [Item("LaTeX String Formatter", "Formatter for symbolic expression trees for import into LaTeX documents.")]
    3535  public sealed class SymbolicExpressionTreeLatexFormatter : NamedItem, ISymbolicExpressionTreeStringFormatter {
     36    private List<double> constants;
     37
    3638    public SymbolicExpressionTreeLatexFormatter()
    37       : base("SymbolicExpressionTreeLatexFormatter", "Formatter for symbolic expression trees for import into LaTeX documents.") { }
    38     private SymbolicExpressionTreeLatexFormatter(SymbolicExpressionTreeLatexFormatter original, Cloner cloner) : base(original, cloner) { }
     39      : base("LaTeX String Formatter", "Formatter for symbolic expression trees for import into LaTeX documents.") {
     40      constants = new List<double>();
     41    }
     42    private SymbolicExpressionTreeLatexFormatter(SymbolicExpressionTreeLatexFormatter original, Cloner cloner)
     43      : base(original, cloner) {
     44      constants = new List<double>(original.constants);
     45    }
    3946
    4047    public override IDeepCloneable Clone(Cloner cloner) {
     
    4552      try {
    4653        StringBuilder strBuilder = new StringBuilder();
    47         strBuilder.AppendLine("\\begin{eqnarray}");
     54        constants.Clear();
     55        strBuilder.AppendLine("% needs \\usepackage{amsmath}");
     56        strBuilder.AppendLine("\\begin{align}");
    4857        strBuilder.AppendLine(FormatRecursively(symbolicExpressionTree.Root));
    49         strBuilder.AppendLine("\\end{eqnarray}");
     58        strBuilder.AppendLine("\\end{align}");
    5059        return strBuilder.ToString();
    5160      }
     
    8392        if (node.SubTrees.Count != 2) throw new NotImplementedException("Division with more than 2 arguments is not implemented.");
    8493        strBuilder.Append(@" \frac{ ");
     94      } else if (node.Symbol is Average) {
     95        strBuilder.Append(@" \frac{1}{" + node.SubTrees.Count + @"} \left(");
     96      } else if (node.Symbol is Logarithm) {
     97        strBuilder.Append(@"\log \left(");
     98      } else if (node.Symbol is Exponential) {
     99        strBuilder.Append(@"\exp \left(");
     100      } else if (node.Symbol is Sine) {
     101        strBuilder.Append(@"\sin \left(");
     102      } else if (node.Symbol is Cosine) {
     103        strBuilder.Append(@"\cos \left(");
     104      } else if (node.Symbol is Tangent) {
     105        strBuilder.Append(@"\tan \left(");
     106      } else if (node.Symbol is GreaterThan) {
     107        strBuilder.Append(@" \left( ");
     108      } else if (node.Symbol is LessThan) {
     109        strBuilder.Append(@" \left( ");
     110      } else if (node.Symbol is And) {
     111        strBuilder.Append(@" \left( \left( ");
     112      } else if (node.Symbol is Or) {
     113        strBuilder.Append(@" \left( \left( ");
     114      } else if (node.Symbol is Not) {
     115        strBuilder.Append(@" -1.0 \times \left( ");
     116      } else if (node.Symbol is IfThenElse) {
     117        strBuilder.Append(@"\left( \operatorname{if} \left( 0 < ");
    85118      } else if (node.Symbol is Constant) {
    86         strBuilder.Append(" c ");
     119        strBuilder.Append("c_{" + constants.Count+"} ");
     120        var constNode = node as ConstantTreeNode;
     121        constants.Add(constNode.Value);
    87122      } else if (node.Symbol is HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.Variable) {
    88123        var varNode = node as VariableTreeNode;
    89         strBuilder.Append(" " + varNode.ToString() + " ");
     124        strBuilder.Append("c_{" + constants.Count + "} " + varNode.VariableName + " ");
     125        constants.Add(varNode.Weight);
     126      } else if (node.Symbol is LaggedVariable) {
     127        var laggedVarNode = node as LaggedVariableTreeNode;
     128        strBuilder.Append("c_{" + constants.Count + "} " + laggedVarNode.VariableName + "(t" + laggedVarNode.Lag + ")");
     129        constants.Add(laggedVarNode.Weight);
    90130      } else if (node.Symbol is ProgramRootSymbol) {
    91131      } else if (node.Symbol is Defun) {
    92132        var defunNode = node as DefunTreeNode;
    93         strBuilder.Append(defunNode.FunctionName + " & = & ");
     133        strBuilder.Append(defunNode.FunctionName + " & = ");
    94134      } else if (node.Symbol is InvokeFunction) {
    95135        var invokeNode = node as InvokeFunctionTreeNode;
    96136        strBuilder.Append(invokeNode.Symbol.FunctionName + @" \left( ");
    97137      } else if (node.Symbol is StartSymbol) {
    98         strBuilder.Append("Result & = & ");
     138        strBuilder.Append("Result & = ");
    99139      } else if (node.Symbol is Argument) {
    100140        var argSym = node.Symbol as Argument;
     
    111151        strBuilder.Append(" - ");
    112152      } else if (node.Symbol is Multiplication) {
     153        strBuilder.Append(@" \times ");
    113154      } else if (node.Symbol is Division) {
    114155        strBuilder.Append(" }{ ");
     156      } else if (node.Symbol is Average) {
     157        strBuilder.Append(@" + ");
     158      } else if (node.Symbol is Logarithm) {
     159        throw new InvalidOperationException();
     160      } else if (node.Symbol is Exponential) {
     161        throw new InvalidOperationException();
     162      } else if (node.Symbol is Sine) {
     163        throw new InvalidOperationException();
     164      } else if (node.Symbol is Cosine) {
     165        throw new InvalidOperationException();
     166      } else if (node.Symbol is Tangent) {
     167        throw new InvalidOperationException();
     168      } else if (node.Symbol is GreaterThan) {
     169        strBuilder.Append(@" > ");
     170      } else if (node.Symbol is LessThan) {
     171        strBuilder.Append(@" < ");
     172      } else if (node.Symbol is And) {
     173        strBuilder.Append(@" > 0 \right) \land \left(");
     174      } else if (node.Symbol is Or) {
     175        strBuilder.Append(@" > 0 \right) \lor \left(");
     176      } else if (node.Symbol is Not) {
     177        throw new InvalidOperationException();
     178      } else if (node.Symbol is IfThenElse) {
     179        strBuilder.Append(@" \right) , \left(");
    115180      } else if (node.Symbol is ProgramRootSymbol) {
    116181        strBuilder.Append(@"\\" + Environment.NewLine);
     
    119184        strBuilder.Append(" , ");
    120185      } else if (node.Symbol is StartSymbol) {
    121         strBuilder.Append(@"\\" + Environment.NewLine + " & & ");
     186        strBuilder.Append(@"\\" + Environment.NewLine + " & ");
    122187      } else {
    123188        throw new NotImplementedException("Export of " + node.Symbol + " is not implemented.");
     
    133198      } else if (node.Symbol is Division) {
    134199        strBuilder.Append(" } ");
     200      } else if (node.Symbol is Average) {
     201        strBuilder.Append(@" \right)");
     202      } else if (node.Symbol is Logarithm) {
     203        strBuilder.Append(@" \right) ");
     204      } else if (node.Symbol is Exponential) {
     205        strBuilder.Append(@" \right) ");
     206      } else if (node.Symbol is Sine) {
     207        strBuilder.Append(@" \right) ");
     208      } else if (node.Symbol is Cosine) {
     209        strBuilder.Append(@" \right) ");
     210      } else if (node.Symbol is Tangent) {
     211        strBuilder.Append(@" \right) ");
     212      } else if (node.Symbol is GreaterThan) {
     213        strBuilder.Append(@" \right) ");
     214      } else if (node.Symbol is LessThan) {
     215        strBuilder.Append(@" \right) ");
     216      } else if (node.Symbol is And) {
     217        strBuilder.Append(@" > 0 \right) \right) ");
     218      } else if (node.Symbol is Or) {
     219        strBuilder.Append(@" > 0 \right) \right) ");
     220      } else if (node.Symbol is Not) {
     221        strBuilder.Append(@" \right) ");
     222      } else if (node.Symbol is IfThenElse) {
     223        strBuilder.Append(@" \right) \right) ");
    135224      } else if (node.Symbol is Constant) {
    136225      } else if (node.Symbol is HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.Variable) {
     226      } else if (node.Symbol is LaggedVariable) {
    137227      } else if (node.Symbol is ProgramRootSymbol) {
     228        // output all constant values
     229        if (constants.Count > 0) {
     230          int i = 0;
     231          foreach (var constant in constants) {
     232            strBuilder.AppendLine(@"\\");
     233            strBuilder.Append("c_{" + i + "} & = " + constant);
     234            i++;
     235          }
     236        }
    138237      } else if (node.Symbol is Defun) {
    139238      } else if (node.Symbol is InvokeFunction) {
Note: See TracChangeset for help on using the changeset viewer.