Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 16773 was 16702, checked in by bburlacu, 6 years ago

#2866: Add tanh to all formatters except the TSQLExpressionFormatter, since TSQL does not have a TANH function (could be approximated but ugly).

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