Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceSpeedUp/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionLatexFormatter.cs

Last change on this file was 5817, checked in by gkronber, 14 years ago

#1418 fixed bug in LaTeX formatter.

File size: 13.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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 System;
28using HeuristicLab.Core;
29using HeuristicLab.Common;
30
31namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
32  [Item("LaTeX String Formatter", "Formatter for symbolic expression trees for import into LaTeX documents.")]
33  [StorableClass]
34  public sealed class SymbolicDataAnalysisExpressionLatexFormatter : NamedItem, ISymbolicExpressionTreeStringFormatter {
35    private List<double> constants;
36    private int currentLag;
37
38    [StorableConstructor]
39    private SymbolicDataAnalysisExpressionLatexFormatter(bool deserializing) : base(deserializing) { }
40    private SymbolicDataAnalysisExpressionLatexFormatter(SymbolicDataAnalysisExpressionLatexFormatter original, Cloner cloner)
41      : base(original, cloner) {
42      constants = new List<double>(original.constants);
43    }
44    public SymbolicDataAnalysisExpressionLatexFormatter()
45      : base() {
46      Name = ItemName;
47      Description = ItemDescription;
48      constants = new List<double>();
49    }
50
51    public override IDeepCloneable Clone(Cloner cloner) {
52      return new SymbolicDataAnalysisExpressionLatexFormatter(this, cloner);
53    }
54
55    public string Format(ISymbolicExpressionTree symbolicExpressionTree) {
56      try {
57        StringBuilder strBuilder = new StringBuilder();
58        constants.Clear();
59        strBuilder.AppendLine("% needs \\usepackage{amsmath}");
60        strBuilder.AppendLine("\\begin{align}");
61        strBuilder.AppendLine(FormatRecursively(symbolicExpressionTree.Root));
62        strBuilder.AppendLine("\\end{align}");
63        return strBuilder.ToString();
64      }
65      catch (NotImplementedException ex) {
66        return ex.Message + Environment.NewLine + ex.StackTrace;
67      }
68    }
69
70    private string FormatRecursively(ISymbolicExpressionTreeNode node) {
71      StringBuilder strBuilder = new StringBuilder();
72      currentLag = 0;
73      FormatBegin(node, strBuilder);
74
75      if (node.SubtreesCount > 0) {
76        strBuilder.Append(FormatRecursively(node.GetSubtree(0)));
77      }
78      foreach (SymbolicExpressionTreeNode subTree in node.Subtrees.Skip(1)) {
79        FormatSep(node, strBuilder);
80        // format the whole subtree
81        strBuilder.Append(FormatRecursively(subTree));
82      }
83
84      FormatEnd(node, strBuilder);
85
86      return strBuilder.ToString();
87    }
88
89    private void FormatBegin(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) {
90      if (node.Symbol is Addition) {
91        strBuilder.Append(@" \left( ");
92      } else if (node.Symbol is Subtraction) {
93        if (node.SubtreesCount == 1) {
94          strBuilder.Append(@"- \left(");
95        } else {
96          strBuilder.Append(@" \left( ");
97        }
98      } else if (node.Symbol is Multiplication) {
99      } else if (node.Symbol is Division) {
100        if (node.SubtreesCount == 1) {
101          strBuilder.Append(@" \cfrac{1}{");
102        } else {
103          strBuilder.Append(@" \cfrac{ ");
104        }
105      } else if (node.Symbol is Average) {
106        // skip output of (1/1) if only one subtree
107        if (node.SubtreesCount > 1) {
108          strBuilder.Append(@" \cfrac{1}{" + node.SubtreesCount + @"}");
109        }
110        strBuilder.Append(@" \left(");
111      } else if (node.Symbol is Logarithm) {
112        strBuilder.Append(@"\log \left(");
113      } else if (node.Symbol is Exponential) {
114        strBuilder.Append(@"\exp \left(");
115      } else if (node.Symbol is Sine) {
116        strBuilder.Append(@"\sin \left(");
117      } else if (node.Symbol is Cosine) {
118        strBuilder.Append(@"\cos \left(");
119      } else if (node.Symbol is Tangent) {
120        strBuilder.Append(@"\tan \left(");
121      } else if (node.Symbol is GreaterThan) {
122        strBuilder.Append(@" \left( ");
123      } else if (node.Symbol is LessThan) {
124        strBuilder.Append(@" \left( ");
125      } else if (node.Symbol is And) {
126        strBuilder.Append(@" \left( \left( ");
127      } else if (node.Symbol is Or) {
128        strBuilder.Append(@" \left( \left( ");
129      } else if (node.Symbol is Not) {
130        strBuilder.Append(@" \neg \left( ");
131      } else if (node.Symbol is IfThenElse) {
132        strBuilder.Append(@"\left( \operatorname{if} \left( 0 < ");
133      } else if (node.Symbol is Constant) {
134        strBuilder.Append("c_{" + constants.Count + "} ");
135        var constNode = node as ConstantTreeNode;
136        constants.Add(constNode.Value);
137      } else if (node.Symbol is LaggedVariable) {
138        var laggedVarNode = node as LaggedVariableTreeNode;
139        strBuilder.Append("c_{" + constants.Count + "} " + laggedVarNode.VariableName);
140        strBuilder.Append(LagToString(currentLag + laggedVarNode.Lag));
141        constants.Add(laggedVarNode.Weight);
142      } else if (node.Symbol is HeuristicLab.Problems.DataAnalysis.Symbolic.Variable) {
143        var varNode = node as VariableTreeNode;
144        strBuilder.Append("c_{" + constants.Count + "} " + varNode.VariableName);
145        strBuilder.Append(LagToString(currentLag));
146        constants.Add(varNode.Weight);
147      } else if (node.Symbol is ProgramRootSymbol) {
148      } else if (node.Symbol is Defun) {
149        var defunNode = node as DefunTreeNode;
150        strBuilder.Append(defunNode.FunctionName + " & = ");
151      } else if (node.Symbol is InvokeFunction) {
152        var invokeNode = node as InvokeFunctionTreeNode;
153        strBuilder.Append(invokeNode.Symbol.FunctionName + @" \left( ");
154      } else if (node.Symbol is StartSymbol) {
155        strBuilder.Append("Result & = ");
156      } else if (node.Symbol is Argument) {
157        var argSym = node.Symbol as Argument;
158        strBuilder.Append(" ARG+" + argSym.ArgumentIndex + " ");
159      } else if (node.Symbol is Derivative) {
160        strBuilder.Append(@" \cfrac{d \left(");
161      } else if (node.Symbol is TimeLag) {
162        var laggedNode = node as ILaggedTreeNode;
163        currentLag += laggedNode.Lag;
164      } else if (node.Symbol is Power) {
165        strBuilder.Append(@"\left(");
166      } else if (node.Symbol is Root) {
167        strBuilder.Append(@"\left(");
168      } else if (node.Symbol is Integral) {
169        // actually a new variable for t is needed in all subtrees (TODO)
170        var laggedTreeNode = node as ILaggedTreeNode;
171        strBuilder.Append(@"\sum_{t=" + (laggedTreeNode.Lag + currentLag) + @"}^0 \left(");
172      } else if (node.Symbol is VariableCondition) {
173        var conditionTreeNode = node as VariableConditionTreeNode;
174        string p = @"1 / \left( 1 + \exp \left( - c_{" + constants.Count + "} ";
175        constants.Add(conditionTreeNode.Slope);
176        p += @" \cdot \left(" + conditionTreeNode.VariableName + LagToString(currentLag) + " - c_{" + constants.Count + @"} \right) \right) \right)";
177        constants.Add(conditionTreeNode.Threshold);
178        strBuilder.Append(@"\left( " + p + @"\cdot ");
179      } else {
180        throw new NotImplementedException("Export of " + node.Symbol + " is not implemented.");
181      }
182    }
183
184    private void FormatSep(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) {
185      if (node.Symbol is Addition) {
186        strBuilder.Append(" + ");
187      } else if (node.Symbol is Subtraction) {
188        strBuilder.Append(" - ");
189      } else if (node.Symbol is Multiplication) {
190        strBuilder.Append(@" \cdot ");
191      } else if (node.Symbol is Division) {
192        strBuilder.Append(@" }{ \cfrac{ ");
193      } else if (node.Symbol is Average) {
194        strBuilder.Append(@" + ");
195      } else if (node.Symbol is Logarithm) {
196        throw new InvalidOperationException();
197      } else if (node.Symbol is Exponential) {
198        throw new InvalidOperationException();
199      } else if (node.Symbol is Sine) {
200        throw new InvalidOperationException();
201      } else if (node.Symbol is Cosine) {
202        throw new InvalidOperationException();
203      } else if (node.Symbol is Tangent) {
204        throw new InvalidOperationException();
205      } else if (node.Symbol is GreaterThan) {
206        strBuilder.Append(@" > ");
207      } else if (node.Symbol is LessThan) {
208        strBuilder.Append(@" < ");
209      } else if (node.Symbol is And) {
210        strBuilder.Append(@" > 0 \right) \land \left(");
211      } else if (node.Symbol is Or) {
212        strBuilder.Append(@" > 0 \right) \lor \left(");
213      } else if (node.Symbol is Not) {
214        throw new InvalidOperationException();
215      } else if (node.Symbol is IfThenElse) {
216        strBuilder.Append(@" \right) , \left(");
217      } else if (node.Symbol is ProgramRootSymbol) {
218        strBuilder.Append(@"\\" + Environment.NewLine);
219      } else if (node.Symbol is Defun) {
220      } else if (node.Symbol is InvokeFunction) {
221        strBuilder.Append(" , ");
222      } else if (node.Symbol is StartSymbol) {
223        strBuilder.Append(@"\\" + Environment.NewLine + " & ");
224      } else if (node.Symbol is Power) {
225        strBuilder.Append(@"\right) ^ { \operatorname{round} \left(");
226      } else if (node.Symbol is Root) {
227        strBuilder.Append(@"\right) ^ { \left( \cfrac{1}{ \operatorname{round} \left(");
228      } else if (node.Symbol is VariableCondition) {
229        var conditionTreeNode = node as VariableConditionTreeNode;
230        string p = @"1 / \left( 1 + \exp \left( - c_{" + constants.Count + "} ";
231        constants.Add(conditionTreeNode.Slope);
232        p += @" \cdot \left(" + conditionTreeNode.VariableName + LagToString(currentLag) + " - c_{" + constants.Count + @"} \right) \right) \right)";
233        constants.Add(conditionTreeNode.Threshold);
234        strBuilder.Append(@" + \left( 1 - " + p + @" \right) \cdot ");
235      } else {
236        throw new NotImplementedException("Export of " + node.Symbol + " is not implemented.");
237      }
238    }
239
240    private void FormatEnd(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) {
241      if (node.Symbol is Addition) {
242        strBuilder.Append(@" \right) ");
243      } else if (node.Symbol is Subtraction) {
244        strBuilder.Append(@" \right) ");
245      } else if (node.Symbol is Multiplication) {
246      } else if (node.Symbol is Division) {
247        strBuilder.Append("} ");
248        if (node.SubtreesCount > 1)
249          strBuilder.Append("{1} ");
250        for (int i = 1; i < node.SubtreesCount; i++) {
251          strBuilder.Append(" } ");
252        }
253      } else if (node.Symbol is Average) {
254        strBuilder.Append(@" \right)");
255      } else if (node.Symbol is Logarithm) {
256        strBuilder.Append(@" \right) ");
257      } else if (node.Symbol is Exponential) {
258        strBuilder.Append(@" \right) ");
259      } else if (node.Symbol is Sine) {
260        strBuilder.Append(@" \right) ");
261      } else if (node.Symbol is Cosine) {
262        strBuilder.Append(@" \right) ");
263      } else if (node.Symbol is Tangent) {
264        strBuilder.Append(@" \right) ");
265      } else if (node.Symbol is GreaterThan) {
266        strBuilder.Append(@" \right) ");
267      } else if (node.Symbol is LessThan) {
268        strBuilder.Append(@" \right) ");
269      } else if (node.Symbol is And) {
270        strBuilder.Append(@" > 0 \right) \right) ");
271      } else if (node.Symbol is Or) {
272        strBuilder.Append(@" > 0 \right) \right) ");
273      } else if (node.Symbol is Not) {
274        strBuilder.Append(@" \right) ");
275      } else if (node.Symbol is IfThenElse) {
276        strBuilder.Append(@" \right) \right) ");
277      } else if (node.Symbol is Constant) {
278      } else if (node.Symbol is LaggedVariable) {
279      } else if (node.Symbol is HeuristicLab.Problems.DataAnalysis.Symbolic.Variable) {
280      } else if (node.Symbol is ProgramRootSymbol) {
281        // output all constant values
282        if (constants.Count > 0) {
283          int i = 0;
284          foreach (var constant in constants) {
285            strBuilder.AppendLine(@"\\");
286            strBuilder.Append("c_{" + i + "} & = " + constant);
287            i++;
288          }
289        }
290      } else if (node.Symbol is Defun) {
291      } else if (node.Symbol is InvokeFunction) {
292        strBuilder.Append(@" \right) ");
293      } else if (node.Symbol is StartSymbol) {
294      } else if (node.Symbol is Argument) {
295      } else if (node.Symbol is Derivative) {
296        strBuilder.Append(@" \right) }{dt} ");
297      } else if (node.Symbol is TimeLag) {
298        var laggedNode = node as ILaggedTreeNode;
299        currentLag -= laggedNode.Lag;
300      } else if (node.Symbol is Power) {
301        strBuilder.Append(@"\right) } ");
302      } else if (node.Symbol is Root) {
303        strBuilder.Append(@"\right) } \right) } ");
304      } else if (node.Symbol is Integral) {
305        var laggedTreeNode = node as ILaggedTreeNode;
306        strBuilder.Append(@"\right) ");
307      } else if (node.Symbol is VariableCondition) {
308        strBuilder.Append(@"\left) ");
309      } else {
310        throw new NotImplementedException("Export of " + node.Symbol + " is not implemented.");
311      }
312    }
313    private string LagToString(int lag) {
314      if (lag < 0) {
315        return "(t" + lag + ")";
316      } else if (lag > 0) {
317        return "(t+" + lag + ")";
318      } else return "(t)";
319    }
320  }
321}
Note: See TracBrowser for help on using the repository browser.