Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionLatexFormatter.cs @ 16130

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

#1772: Merge trunk changes

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