Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.Views/3.3/Symbolic/SymbolicExpressionTreeLatexFormatter.cs @ 7624

Last change on this file since 7624 was 4556, checked in by gkronber, 14 years ago

Added classes and views for analysis of symbolic time series prognosis results. #1142

File size: 5.6 KB
RevLine 
[4327]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System.Text;
23using System.Linq;
24using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
25using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
26using System.Collections.Generic;
27using HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols;
28using System;
29using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
30
31namespace HeuristicLab.Problems.DataAnalysis.Views.Symbolic {
32  public class SymbolicExpressionTreeLatexFormatter {
33    public SymbolicExpressionTreeLatexFormatter()
34      : base() { }
35
36    public string Format(SymbolicExpressionTree symbolicExpressionTree) {
37      try {
38        StringBuilder strBuilder = new StringBuilder();
39        strBuilder.AppendLine("\\begin{eqnarray}");
40        strBuilder.AppendLine(FormatRecursively(symbolicExpressionTree.Root));
41        strBuilder.AppendLine("\\end{eqnarray}");
42        return strBuilder.ToString();
43      }
44      catch (NotImplementedException ex) {
45        return ex.Message + Environment.NewLine + ex.StackTrace;
46      }
47    }
48
49    private string FormatRecursively(SymbolicExpressionTreeNode node) {
50      StringBuilder strBuilder = new StringBuilder();
51
52      FormatBegin(node, strBuilder);
53
54      if (node.SubTrees.Count > 0) {
55        strBuilder.Append(FormatRecursively(node.SubTrees[0]));
56      }
57      foreach (SymbolicExpressionTreeNode subTree in node.SubTrees.Skip(1)) {
58        FormatSep(node, strBuilder);
59        // format the whole subtree
60        strBuilder.Append(FormatRecursively(subTree));
61      }
62
63      FormatEnd(node, strBuilder);
64
65      return strBuilder.ToString();
66    }
67
68    private void FormatBegin(SymbolicExpressionTreeNode node, StringBuilder strBuilder) {
69      if (node.Symbol is Addition) {
70        strBuilder.Append(@" \left( ");
71      } else if (node.Symbol is Subtraction) {
72        strBuilder.Append(@" \left( ");
73      } else if (node.Symbol is Multiplication) {
74      } else if (node.Symbol is Division) {
75        if (node.SubTrees.Count != 2) throw new NotImplementedException("Division with more than 2 arguments is not implemented.");
76        strBuilder.Append(@" \frac{ ");
77      } else if (node.Symbol is Constant) {
78        strBuilder.Append(" c ");
79      } else if (node.Symbol is Variable) {
80        var varNode = node as VariableTreeNode;
[4556]81        strBuilder.Append(" " + varNode.ToString()+ " ");
[4327]82      } else if (node.Symbol is ProgramRootSymbol) {
83      } else if (node.Symbol is Defun) {
84        var defunNode = node as DefunTreeNode;
85        strBuilder.Append(defunNode.FunctionName + " & = & ");
86      } else if (node.Symbol is InvokeFunction) {
87        var invokeNode = node as InvokeFunctionTreeNode;
88        strBuilder.Append(invokeNode.Symbol.FunctionName + @" \left( ");
89      } else if (node.Symbol is StartSymbol) {
90        strBuilder.Append("Result & = & ");
91      } else if(node.Symbol is Argument) {
92        var argSym = node.Symbol as Argument;
93        strBuilder.Append(" ARG+"+argSym.ArgumentIndex+" ");
94      } else {
95        throw new NotImplementedException("Export of " + node.Symbol + " is not implemented.");
96      }
97    }
98
99    private void FormatSep(SymbolicExpressionTreeNode node, StringBuilder strBuilder) {
100      if (node.Symbol is Addition) {
101        strBuilder.Append(" + ");
102      } else if (node.Symbol is Subtraction) {
103        strBuilder.Append(" - ");
104      } else if (node.Symbol is Multiplication) {
105      } else if (node.Symbol is Division) {
106        strBuilder.Append(" }{ ");
107      } else if (node.Symbol is ProgramRootSymbol) {
108        strBuilder.Append(@"\\" + Environment.NewLine);
109      } else if (node.Symbol is Defun) {
110      } else if (node.Symbol is InvokeFunction) {
111        strBuilder.Append(" , ");
112      } else if (node.Symbol is StartSymbol) {
113        strBuilder.Append(@"\\" + Environment.NewLine + " & & ");
114      } else {
115        throw new NotImplementedException("Export of " + node.Symbol + " is not implemented.");
116      }
117    }
118
119    private void FormatEnd(SymbolicExpressionTreeNode node, StringBuilder strBuilder) {
120      if (node.Symbol is Addition) {
121        strBuilder.Append(@" \right) ");
122      } else if (node.Symbol is Subtraction) {
123        strBuilder.Append(@" \right) ");
124      } else if (node.Symbol is Multiplication) {
125      } else if (node.Symbol is Division) {
126        strBuilder.Append(" } ");
127      } else if (node.Symbol is Constant) {
128      } else if (node.Symbol is Variable) {
129      } else if (node.Symbol is ProgramRootSymbol) {
130      } else if (node.Symbol is Defun) {
131      } else if (node.Symbol is InvokeFunction) {
132        strBuilder.Append(@" \right) ");
133      } else if (node.Symbol is StartSymbol) {
134      } else if (node.Symbol is Argument) {
135      } else {
136        throw new NotImplementedException("Export of " + node.Symbol + " is not implemented.");
137      }
138    }
139  }
140}
Note: See TracBrowser for help on using the repository browser.