Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionLatexFormatter.cs @ 18082

Last change on this file since 18082 was 17826, checked in by gkronber, 4 years ago

#3083: fixed problem in LaTeX formatter when targetVariable = null

File size: 24.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HEAL.Attic;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
30
31namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
32  [Item("LaTeX String Formatter", "Formatter for symbolic expression trees for import into LaTeX documents.")]
33  [StorableType("D7186DFF-1596-4A58-B27D-974DF0D93E4F")]
34  public sealed class SymbolicDataAnalysisExpressionLatexFormatter : NamedItem, ISymbolicExpressionTreeStringFormatter {
35    private readonly List<KeyValuePair<string, double>> constants;
36    private int constIndex;
37    private int targetCount;
38    private int currentLag;
39    private string targetVariable;
40    private bool containsTimeSeriesSymbol;
41
42    [StorableConstructor]
43    private SymbolicDataAnalysisExpressionLatexFormatter(StorableConstructorFlag _) : base(_) { }
44    private SymbolicDataAnalysisExpressionLatexFormatter(SymbolicDataAnalysisExpressionLatexFormatter original, Cloner cloner)
45      : base(original, cloner) {
46      constants = new List<KeyValuePair<string, double>>(original.constants);
47      constIndex = original.constIndex;
48      currentLag = original.currentLag;
49      targetCount = original.targetCount;
50    }
51    public SymbolicDataAnalysisExpressionLatexFormatter()
52      : base() {
53      Name = ItemName;
54      Description = ItemDescription;
55      constants = new List<KeyValuePair<string, double>>();
56    }
57
58    public override IDeepCloneable Clone(Cloner cloner) {
59      return new SymbolicDataAnalysisExpressionLatexFormatter(this, cloner);
60    }
61
62    public string Format(ISymbolicExpressionTree symbolicExpressionTree) {
63      return Format(symbolicExpressionTree, null);
64    }
65    public string Format(ISymbolicExpressionTree symbolicExpressionTree, string targetVariable) {
66      try {
67        StringBuilder strBuilder = new StringBuilder();
68        constants.Clear();
69        constIndex = 0;
70        this.targetVariable = targetVariable;
71        containsTimeSeriesSymbol = symbolicExpressionTree.IterateNodesBreadth().Any(n => IsTimeSeriesSymbol(n.Symbol));
72        strBuilder.AppendLine(FormatRecursively(symbolicExpressionTree.Root));
73        return strBuilder.ToString();
74      } catch (NotImplementedException ex) {
75        return ex.Message + Environment.NewLine + ex.StackTrace;
76      }
77    }
78    static bool IsTimeSeriesSymbol(ISymbol s) {
79      return s is TimeLag || s is Integral || s is Derivative || s is LaggedVariable;
80    }
81
82    private string FormatRecursively(ISymbolicExpressionTreeNode node) {
83      StringBuilder strBuilder = new StringBuilder();
84      currentLag = 0;
85      FormatBegin(node, strBuilder);
86
87      if (node.SubtreeCount > 0) {
88        strBuilder.Append(FormatRecursively(node.GetSubtree(0)));
89      }
90      int i = 1;
91      foreach (var subTree in node.Subtrees.Skip(1)) {
92        FormatSep(node, strBuilder, i);
93        // format the whole subtree
94        strBuilder.Append(FormatRecursively(subTree));
95        i++;
96      }
97
98      FormatEnd(node, strBuilder);
99
100      return strBuilder.ToString();
101    }
102
103    private void FormatBegin(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) {
104      if (node.Symbol is Addition) {
105        strBuilder.Append(@" \left( ");
106      } else if (node.Symbol is Subtraction) {
107        if (node.SubtreeCount == 1) {
108          strBuilder.Append(@"- \left( ");
109        } else {
110          strBuilder.Append(@" \left( ");
111        }
112      } else if (node.Symbol is Multiplication) {
113      } else if (node.Symbol is Division) {
114        if (node.SubtreeCount == 1) {
115          strBuilder.Append(@" \cfrac{1}{");
116        } else {
117          strBuilder.Append(@" \cfrac{ ");
118        }
119      } else if (node.Symbol is Absolute) {
120        strBuilder.Append(@"\operatorname{abs} \left( ");
121      } else if (node.Symbol is AnalyticQuotient) {
122        strBuilder.Append(@" \frac { ");
123      } else if (node.Symbol is Average) {
124        // skip output of (1/1) if only one subtree
125        if (node.SubtreeCount > 1) {
126          strBuilder.Append(@" \cfrac{1}{" + node.SubtreeCount + @"}");
127        }
128        strBuilder.Append(@" \left( ");
129      } else if (node.Symbol is Logarithm) {
130        strBuilder.Append(@"\log \left( ");
131      } else if (node.Symbol is Exponential) {
132        strBuilder.Append(@"\exp \left( ");
133      } else if (node.Symbol is Square) {
134        strBuilder.Append(@"\left(");
135      } else if (node.Symbol is SquareRoot) {
136        strBuilder.Append(@"\sqrt{");
137      } else if (node.Symbol is Cube) {
138        strBuilder.Append(@"\left(");
139      } else if (node.Symbol is CubeRoot) {
140        strBuilder.Append(@"\operatorname{cbrt}\left(");
141      } else if (node.Symbol is Sine) {
142        strBuilder.Append(@"\sin \left( ");
143      } else if (node.Symbol is Cosine) {
144        strBuilder.Append(@"\cos \left( ");
145      } else if (node.Symbol is Tangent) {
146        strBuilder.Append(@"\tan \left( ");
147      } else if (node.Symbol is HyperbolicTangent) {
148        strBuilder.Append(@"\tanh \left( ");
149      } else if (node.Symbol is AiryA) {
150        strBuilder.Append(@"\operatorname{airy}_a \left( ");
151      } else if (node.Symbol is AiryB) {
152        strBuilder.Append(@"\operatorname{airy}_b \left( ");
153      } else if (node.Symbol is Bessel) {
154        strBuilder.Append(@"\operatorname{bessel}_1 \left( ");
155      } else if (node.Symbol is CosineIntegral) {
156        strBuilder.Append(@"\operatorname{cosInt} \left( ");
157      } else if (node.Symbol is Dawson) {
158        strBuilder.Append(@"\operatorname{dawson} \left( ");
159      } else if (node.Symbol is Erf) {
160        strBuilder.Append(@"\operatorname{erf} \left( ");
161      } else if (node.Symbol is ExponentialIntegralEi) {
162        strBuilder.Append(@"\operatorname{expInt}_i \left( ");
163      } else if (node.Symbol is FresnelCosineIntegral) {
164        strBuilder.Append(@"\operatorname{fresnel}_\operatorname{cosInt} \left( ");
165      } else if (node.Symbol is FresnelSineIntegral) {
166        strBuilder.Append(@"\operatorname{fresnel}_\operatorname{sinInt} \left( ");
167      } else if (node.Symbol is Gamma) {
168        strBuilder.Append(@"\Gamma \left( ");
169      } else if (node.Symbol is HyperbolicCosineIntegral) {
170        strBuilder.Append(@"\operatorname{hypCosInt} \left( ");
171      } else if (node.Symbol is HyperbolicSineIntegral) {
172        strBuilder.Append(@"\operatorname{hypSinInt} \left( ");
173      } else if (node.Symbol is Norm) {
174        strBuilder.Append(@"\operatorname{norm} \left( ");
175      } else if (node.Symbol is Psi) {
176        strBuilder.Append(@"\operatorname{digamma} \left( ");
177      } else if (node.Symbol is SineIntegral) {
178        strBuilder.Append(@"\operatorname{sinInt} \left( ");
179      } else if (node.Symbol is GreaterThan) {
180        strBuilder.Append(@"  \left( ");
181      } else if (node.Symbol is LessThan) {
182        strBuilder.Append(@"  \left( ");
183      } else if (node.Symbol is And) {
184        strBuilder.Append(@"  \left( \left( ");
185      } else if (node.Symbol is Or) {
186        strBuilder.Append(@"   \left( \left( ");
187      } else if (node.Symbol is Not) {
188        strBuilder.Append(@" \neg \left( ");
189      } else if (node.Symbol is IfThenElse) {
190        strBuilder.Append(@" \operatorname{if}  \left( ");
191      } else if (node.Symbol is Constant) {
192        var constName = "c_{" + constIndex + "}";
193        constIndex++;
194        var constNode = node as ConstantTreeNode;
195        if (constNode.Value.IsAlmost(1.0)) {
196          strBuilder.Append("1 ");
197        } else {
198          strBuilder.Append(constName);
199          constants.Add(new KeyValuePair<string, double>(constName, constNode.Value));
200        }
201
202      } else if (node.Symbol is FactorVariable) {
203        var factorNode = node as FactorVariableTreeNode;
204        var constName = "c_{" + constIndex + "}";
205        strBuilder.Append(constName + " ");
206        foreach (var e in factorNode.Symbol.GetVariableValues(factorNode.VariableName)
207          .Zip(factorNode.Weights, Tuple.Create)) {
208          constants.Add(new KeyValuePair<string, double>("c_{" + constIndex + ", " + EscapeLatexString(factorNode.VariableName) + "=" + EscapeLatexString(e.Item1) + "}", e.Item2));
209        }
210        constIndex++;
211      } else if (node.Symbol is BinaryFactorVariable) {
212        var binFactorNode = node as BinaryFactorVariableTreeNode;
213        if (!binFactorNode.Weight.IsAlmost((1.0))) {
214          var constName = "c_{" + constIndex + "}";
215          strBuilder.Append(constName + "  \\cdot");
216          constants.Add(new KeyValuePair<string, double>(constName, binFactorNode.Weight));
217          constIndex++;
218        }
219        strBuilder.Append("(" + EscapeLatexString(binFactorNode.VariableName));
220        strBuilder.Append(LagToString(currentLag));
221        strBuilder.Append(" = " + EscapeLatexString(binFactorNode.VariableValue) + " )");
222      } else if (node.Symbol is LaggedVariable) {
223        var laggedVarNode = node as LaggedVariableTreeNode;
224        if (!laggedVarNode.Weight.IsAlmost(1.0)) {
225          var constName = "c_{" + constIndex + "}";
226          strBuilder.Append(constName + "  \\cdot");
227          constants.Add(new KeyValuePair<string, double>(constName, laggedVarNode.Weight));
228          constIndex++;
229        }
230        strBuilder.Append(EscapeLatexString(laggedVarNode.VariableName));
231        strBuilder.Append(LagToString(currentLag + laggedVarNode.Lag));
232
233      } else if (node.Symbol is Variable) {
234        var varNode = node as VariableTreeNode;
235        if (!varNode.Weight.IsAlmost((1.0))) {
236          var constName = "c_{" + constIndex + "}";
237          strBuilder.Append(constName + "  \\cdot");
238          constants.Add(new KeyValuePair<string, double>(constName, varNode.Weight));
239          constIndex++;
240        }
241        strBuilder.Append(EscapeLatexString(varNode.VariableName));
242        strBuilder.Append(LagToString(currentLag));
243      } else if (node.Symbol is ProgramRootSymbol) {
244        strBuilder
245          .AppendLine("\\begin{align*}")
246          .AppendLine("\\nonumber");
247      } else if (node.Symbol is Defun) {
248        var defunNode = node as DefunTreeNode;
249        strBuilder.Append(defunNode.FunctionName + " & = ");
250      } else if (node.Symbol is InvokeFunction) {
251        var invokeNode = node as InvokeFunctionTreeNode;
252        strBuilder.Append(invokeNode.Symbol.FunctionName + @" \left( ");
253      } else if (node.Symbol is StartSymbol) {
254        FormatStartSymbol(strBuilder);
255      } else if (node.Symbol is Argument) {
256        var argSym = node.Symbol as Argument;
257        strBuilder.Append(" ARG+" + argSym.ArgumentIndex + " ");
258      } else if (node.Symbol is Derivative) {
259        strBuilder.Append(@" \cfrac{d \left( ");
260      } else if (node.Symbol is TimeLag) {
261        var laggedNode = node as ILaggedTreeNode;
262        currentLag += laggedNode.Lag;
263      } else if (node.Symbol is Power) {
264        strBuilder.Append(@" \left( ");
265      } else if (node.Symbol is Root) {
266        strBuilder.Append(@" \left( ");
267      } else if (node.Symbol is Integral) {
268        // actually a new variable for t is needed in all subtrees (TODO)
269        var laggedTreeNode = node as ILaggedTreeNode;
270        strBuilder.Append(@"\sum_{t=" + (laggedTreeNode.Lag + currentLag) + @"}^0 \left( ");
271      } else if (node.Symbol is VariableCondition) {
272        var conditionTreeNode = node as VariableConditionTreeNode;
273        var constName = "c_{" + constants.Count + "}";
274        string p = @"1 /  1 + \exp  - " + constName + " ";
275        constants.Add(new KeyValuePair<string, double>(constName, conditionTreeNode.Slope));
276        constIndex++;
277        var const2Name = "c_{" + constants.Count + @"}";
278        p += @" \cdot " + EscapeLatexString(conditionTreeNode.VariableName) + LagToString(currentLag) + " - " + const2Name + "   ";
279        constants.Add(new KeyValuePair<string, double>(const2Name, conditionTreeNode.Threshold));
280        constIndex++;
281        strBuilder.Append(@" \left( " + p + @"\cdot ");
282      } else {
283        throw new NotImplementedException("Export of " + node.Symbol + " is not implemented.");
284      }
285    }
286
287    private void FormatSep(ISymbolicExpressionTreeNode node, StringBuilder strBuilder, int step) {
288      if (node.Symbol is Addition) {
289        strBuilder.Append(" + ");
290      } else if (node.Symbol is Subtraction) {
291        strBuilder.Append(" - ");
292      } else if (node.Symbol is Multiplication) {
293        strBuilder.Append(@" \cdot ");
294      } else if (node.Symbol is Division) {
295        if (step + 1 == node.SubtreeCount)
296          strBuilder.Append(@"}{");
297        else
298          strBuilder.Append(@" }{ \cfrac{ ");
299      } else if (node.Symbol is Absolute) {
300        throw new InvalidOperationException();
301      } else if (node.Symbol is AnalyticQuotient) {
302        strBuilder.Append(@"}{\sqrt{1 + \left( ");
303      } else if (node.Symbol is Average) {
304        strBuilder.Append(@" + ");
305      } else if (node.Symbol is Logarithm) {
306        throw new InvalidOperationException();
307      } else if (node.Symbol is Exponential) {
308        throw new InvalidOperationException();
309      } else if (node.Symbol is Square) {
310        throw new InvalidOperationException();
311      } else if (node.Symbol is SquareRoot) {
312        throw new InvalidOperationException();
313      } else if (node.Symbol is Cube) {
314        throw new InvalidOperationException();
315      } else if (node.Symbol is CubeRoot) {
316        throw new InvalidOperationException();
317      } else if (node.Symbol is Sine) {
318        throw new InvalidOperationException();
319      } else if (node.Symbol is Cosine) {
320        throw new InvalidOperationException();
321      } else if (node.Symbol is Tangent) {
322        throw new InvalidOperationException();
323      } else if (node.Symbol is HyperbolicTangent) {
324        throw new InvalidOperationException();
325      } else if (node.Symbol is AiryA) {
326        throw new InvalidOperationException();
327      } else if (node.Symbol is AiryB) {
328        throw new InvalidOperationException();
329      } else if (node.Symbol is Bessel) {
330        throw new InvalidOperationException();
331      } else if (node.Symbol is CosineIntegral) {
332        throw new InvalidOperationException();
333      } else if (node.Symbol is Dawson) {
334        throw new InvalidOperationException();
335      } else if (node.Symbol is Erf) {
336        throw new InvalidOperationException();
337      } else if (node.Symbol is ExponentialIntegralEi) {
338        throw new InvalidOperationException();
339      } else if (node.Symbol is FresnelCosineIntegral) {
340        throw new InvalidOperationException();
341      } else if (node.Symbol is FresnelSineIntegral) {
342        throw new InvalidOperationException();
343      } else if (node.Symbol is Gamma) {
344        throw new InvalidOperationException();
345      } else if (node.Symbol is HyperbolicCosineIntegral) {
346        throw new InvalidOperationException();
347      } else if (node.Symbol is HyperbolicSineIntegral) {
348        throw new InvalidOperationException();
349      } else if (node.Symbol is Norm) {
350        throw new InvalidOperationException();
351      } else if (node.Symbol is Psi) {
352        throw new InvalidOperationException();
353      } else if (node.Symbol is SineIntegral) {
354        throw new InvalidOperationException();
355      } else if (node.Symbol is GreaterThan) {
356        strBuilder.Append(@" > ");
357      } else if (node.Symbol is LessThan) {
358        strBuilder.Append(@" < ");
359      } else if (node.Symbol is And) {
360        strBuilder.Append(@" > 0  \right) \land \left(");
361      } else if (node.Symbol is Or) {
362        strBuilder.Append(@" > 0  \right) \lor \left(");
363      } else if (node.Symbol is Not) {
364        throw new InvalidOperationException();
365      } else if (node.Symbol is IfThenElse) {
366        strBuilder.Append(@" , ");
367      } else if (node.Symbol is ProgramRootSymbol) {
368        strBuilder.Append(@"\\" + Environment.NewLine);
369      } else if (node.Symbol is Defun) {
370      } else if (node.Symbol is InvokeFunction) {
371        strBuilder.Append(" , ");
372      } else if (node.Symbol is StartSymbol) {
373        strBuilder.Append(@"\\" + Environment.NewLine);
374        FormatStartSymbol(strBuilder);
375      } else if (node.Symbol is Power) {
376        strBuilder.Append(@"\right) ^ { \operatorname{round} \left(");
377      } else if (node.Symbol is Root) {
378        strBuilder.Append(@"\right) ^ {  \cfrac{1}{ \operatorname{round} \left(");
379      } else if (node.Symbol is VariableCondition) {
380        var conditionTreeNode = node as VariableConditionTreeNode;
381        var const1Name = "c_{" + constants.Count + "}";
382        string p = @"1 / \left( 1 + \exp \left( - " + const1Name + " ";
383        constants.Add(new KeyValuePair<string, double>(const1Name, conditionTreeNode.Slope));
384        constIndex++;
385        var const2Name = "c_{" + constants.Count + "}";
386        p += @" \cdot " + EscapeLatexString(conditionTreeNode.VariableName) + LagToString(currentLag) + " - " + const2Name + " \right) \right) \right)   ";
387        constants.Add(new KeyValuePair<string, double>(const2Name, conditionTreeNode.Threshold));
388        constIndex++;
389        strBuilder.Append(@" +  \left( 1 - " + p + @" \right) \cdot ");
390      } else {
391        throw new NotImplementedException("Export of " + node.Symbol + " is not implemented.");
392      }
393    }
394
395    private void FormatEnd(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) {
396      if (node.Symbol is Addition) {
397        strBuilder.Append(@" \right) ");
398      } else if (node.Symbol is Subtraction) {
399        strBuilder.Append(@" \right) ");
400      } else if (node.Symbol is Multiplication) {
401      } else if (node.Symbol is Division) {
402        strBuilder.Append(" } ");
403        for (int i = 2; i < node.SubtreeCount; i++)
404          strBuilder.Append(" } ");
405      } else if (node.Symbol is Absolute) {
406        strBuilder.Append(@" \right)");
407      } else if (node.Symbol is AnalyticQuotient) {
408        strBuilder.Append(@" \right)^2}}");
409      } else if (node.Symbol is Average) {
410        strBuilder.Append(@" \right) ");
411      } else if (node.Symbol is Logarithm) {
412        strBuilder.Append(@" \right) ");
413      } else if (node.Symbol is Exponential) {
414        strBuilder.Append(@" \right) ");
415      } else if (node.Symbol is Square) {
416        strBuilder.Append(@"\right)^2");
417      } else if (node.Symbol is SquareRoot) {
418        strBuilder.Append(@"}");
419      } else if (node.Symbol is Cube) {
420        strBuilder.Append(@"\right)^3");
421      } else if (node.Symbol is CubeRoot) {
422        strBuilder.Append(@"\right)");
423      } else if (node.Symbol is Sine) {
424        strBuilder.Append(@" \right) ");
425      } else if (node.Symbol is Cosine) {
426        strBuilder.Append(@" \right) ");
427      } else if (node.Symbol is Tangent) {
428        strBuilder.Append(@" \right) ");
429      } else if (node.Symbol is HyperbolicTangent) {
430        strBuilder.Append(@" \right) ");
431      } else if (node.Symbol is AiryA) {
432        strBuilder.Append(@" \right) ");
433      } else if (node.Symbol is AiryB) {
434        strBuilder.Append(@" \right) ");
435      } else if (node.Symbol is Bessel) {
436        strBuilder.Append(@" \right) ");
437      } else if (node.Symbol is CosineIntegral) {
438        strBuilder.Append(@" \right) ");
439      } else if (node.Symbol is Dawson) {
440        strBuilder.Append(@" \right) ");
441      } else if (node.Symbol is Erf) {
442        strBuilder.Append(@" \right) ");
443      } else if (node.Symbol is ExponentialIntegralEi) {
444        strBuilder.Append(@" \right) ");
445      } else if (node.Symbol is FresnelCosineIntegral) {
446        strBuilder.Append(@" \right) ");
447      } else if (node.Symbol is FresnelSineIntegral) {
448        strBuilder.Append(@" \right) ");
449      } else if (node.Symbol is Gamma) {
450        strBuilder.Append(@" \right) ");
451      } else if (node.Symbol is HyperbolicCosineIntegral) {
452        strBuilder.Append(@" \right) ");
453      } else if (node.Symbol is HyperbolicSineIntegral) {
454        strBuilder.Append(@" \right) ");
455      } else if (node.Symbol is Norm) {
456        strBuilder.Append(@" \right) ");
457      } else if (node.Symbol is Psi) {
458        strBuilder.Append(@" \right) ");
459      } else if (node.Symbol is SineIntegral) {
460        strBuilder.Append(@" \right) ");
461      } else if (node.Symbol is GreaterThan) {
462        strBuilder.Append(@" \right) ");
463      } else if (node.Symbol is LessThan) {
464        strBuilder.Append(@" \right) ");
465      } else if (node.Symbol is And) {
466        strBuilder.Append(@" > 0 \right) \right) ");
467      } else if (node.Symbol is Or) {
468        strBuilder.Append(@" > 0 \right) \right) ");
469      } else if (node.Symbol is Not) {
470        strBuilder.Append(@" \right) ");
471      } else if (node.Symbol is IfThenElse) {
472        strBuilder.Append(@" \right) ");
473      } else if (node.Symbol is Constant) {
474      } else if (node.Symbol is LaggedVariable) {
475      } else if (node.Symbol is Variable) {
476      } else if (node.Symbol is FactorVariable) {
477      } else if (node.Symbol is BinaryFactorVariable) {
478      } else if (node.Symbol is ProgramRootSymbol) {
479        strBuilder
480          .AppendLine("\\end{align*}")
481          .AppendLine("\\begin{align*}")
482          .AppendLine("\\nonumber");
483        // output all constant values
484        if (constants.Count > 0) {
485          foreach (var constant in constants) {
486            // replace "." with ".&" to align decimal points
487            var constStr = string.Format(System.Globalization.NumberFormatInfo.InvariantInfo, "{0:G5}", constant.Value);
488            if (!constStr.Contains(".")) constStr = constStr + ".0";
489            constStr = constStr.Replace(".", "&.");  // fix problem in rendering of aligned expressions
490            strBuilder.Append(constant.Key + "& = & " + constStr);
491            strBuilder.Append(@"\\");
492          }
493        }
494        strBuilder.AppendLine("\\end{align*}");
495      } else if (node.Symbol is Defun) {
496      } else if (node.Symbol is InvokeFunction) {
497        strBuilder.Append(@" \right) ");
498      } else if (node.Symbol is StartSymbol) {
499      } else if (node.Symbol is Argument) {
500      } else if (node.Symbol is Derivative) {
501        strBuilder.Append(@" \right) }{dt} ");
502      } else if (node.Symbol is TimeLag) {
503        var laggedNode = node as ILaggedTreeNode;
504        currentLag -= laggedNode.Lag;
505      } else if (node.Symbol is Power) {
506        strBuilder.Append(@" \right) } ");
507      } else if (node.Symbol is Root) {
508        strBuilder.Append(@" \right) } } ");
509      } else if (node.Symbol is Integral) {
510        strBuilder.Append(@" \right) ");
511      } else if (node.Symbol is VariableCondition) {
512        strBuilder.Append(@"\right) ");
513      } else {
514        throw new NotImplementedException("Export of " + node.Symbol + " is not implemented.");
515      }
516    }
517
518    private void FormatStartSymbol(StringBuilder strBuilder) {
519      strBuilder.Append(targetVariable != null ? EscapeLatexString(targetVariable) : "\\text{target}_{" + targetCount++ + "}");
520      if (containsTimeSeriesSymbol)
521        strBuilder.Append("(t)");
522      strBuilder.Append(" & = ");
523    }
524
525    private string LagToString(int lag) {
526      if (lag < 0) {
527        return "(t" + lag + ")";
528      } else if (lag > 0) {
529        return "(t+" + lag + ")";
530      } else return "";
531    }
532
533    private string EscapeLatexString(string s) {
534      return "\\text{" +
535        s
536         .Replace("\\", "\\\\")
537         .Replace("{", "\\{")
538         .Replace("}", "\\}")
539        + "}";
540    }
541  }
542}
Note: See TracBrowser for help on using the repository browser.