1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2018 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Text;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 |
|
---|
31 | namespace 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 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(bool deserializing) : base(deserializing) { }
|
---|
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 Average) {
|
---|
120 | // skip output of (1/1) if only one subtree
|
---|
121 | if (node.SubtreeCount > 1) {
|
---|
122 | strBuilder.Append(@" \cfrac{1}{" + node.SubtreeCount + @"}");
|
---|
123 | }
|
---|
124 | strBuilder.Append(@" \left( ");
|
---|
125 | } else if (node.Symbol is Logarithm) {
|
---|
126 | strBuilder.Append(@"\log \left( ");
|
---|
127 | } else if (node.Symbol is Exponential) {
|
---|
128 | strBuilder.Append(@"\exp \left( ");
|
---|
129 | } else if (node.Symbol is Square) {
|
---|
130 | strBuilder.Append(@"\left(");
|
---|
131 | } else if (node.Symbol is SquareRoot) {
|
---|
132 | strBuilder.Append(@"\sqrt{");
|
---|
133 | } else if (node.Symbol is Sine) {
|
---|
134 | strBuilder.Append(@"\sin \left( ");
|
---|
135 | } else if (node.Symbol is Cosine) {
|
---|
136 | strBuilder.Append(@"\cos \left( ");
|
---|
137 | } else if (node.Symbol is Tangent) {
|
---|
138 | strBuilder.Append(@"\tan \left( ");
|
---|
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) {
|
---|
154 | strBuilder.Append(@"\operatorname{fresnel}_\operatorname{cosInt} \left( ");
|
---|
155 | } else if (node.Symbol is FresnelSineIntegral) {
|
---|
156 | strBuilder.Append(@"\operatorname{fresnel}_\operatorname{sinInt} \left( ");
|
---|
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( ");
|
---|
163 | } else if (node.Symbol is Norm) {
|
---|
164 | strBuilder.Append(@"\operatorname{norm} \left( ");
|
---|
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( ");
|
---|
169 | } else if (node.Symbol is GreaterThan) {
|
---|
170 | strBuilder.Append(@" \left( ");
|
---|
171 | } else if (node.Symbol is LessThan) {
|
---|
172 | strBuilder.Append(@" \left( ");
|
---|
173 | } else if (node.Symbol is And) {
|
---|
174 | strBuilder.Append(@" \left( \left( ");
|
---|
175 | } else if (node.Symbol is Or) {
|
---|
176 | strBuilder.Append(@" \left( \left( ");
|
---|
177 | } else if (node.Symbol is Not) {
|
---|
178 | strBuilder.Append(@" \neg \left( ");
|
---|
179 | } else if (node.Symbol is IfThenElse) {
|
---|
180 | strBuilder.Append(@" \operatorname{if} \left( ");
|
---|
181 | } else if (node.Symbol is Constant) {
|
---|
182 | var constName = "c_{" + constIndex + "}";
|
---|
183 | constIndex++;
|
---|
184 | var constNode = node as ConstantTreeNode;
|
---|
185 | if (constNode.Value.IsAlmost(1.0)) {
|
---|
186 | strBuilder.Append("1 ");
|
---|
187 | } else {
|
---|
188 | strBuilder.Append(constName);
|
---|
189 | constants.Add(new KeyValuePair<string, double>(constName, constNode.Value));
|
---|
190 | }
|
---|
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) + " )");
|
---|
212 | } else if (node.Symbol is LaggedVariable) {
|
---|
213 | var laggedVarNode = node as LaggedVariableTreeNode;
|
---|
214 | if (!laggedVarNode.Weight.IsAlmost(1.0)) {
|
---|
215 | var constName = "c_{" + constIndex + "}";
|
---|
216 | strBuilder.Append(constName + " \\cdot");
|
---|
217 | constants.Add(new KeyValuePair<string, double>(constName, laggedVarNode.Weight));
|
---|
218 | constIndex++;
|
---|
219 | }
|
---|
220 | strBuilder.Append(EscapeLatexString(laggedVarNode.VariableName));
|
---|
221 | strBuilder.Append(LagToString(currentLag + laggedVarNode.Lag));
|
---|
222 |
|
---|
223 | } else if (node.Symbol is Variable) {
|
---|
224 | var varNode = node as VariableTreeNode;
|
---|
225 | if (!varNode.Weight.IsAlmost((1.0))) {
|
---|
226 | var constName = "c_{" + constIndex + "}";
|
---|
227 | strBuilder.Append(constName + " \\cdot");
|
---|
228 | constants.Add(new KeyValuePair<string, double>(constName, varNode.Weight));
|
---|
229 | constIndex++;
|
---|
230 | }
|
---|
231 | strBuilder.Append(EscapeLatexString(varNode.VariableName));
|
---|
232 | strBuilder.Append(LagToString(currentLag));
|
---|
233 | } else if (node.Symbol is ProgramRootSymbol) {
|
---|
234 | strBuilder
|
---|
235 | .AppendLine("\\begin{align*}")
|
---|
236 | .AppendLine("\\nonumber");
|
---|
237 | } else if (node.Symbol is Defun) {
|
---|
238 | var defunNode = node as DefunTreeNode;
|
---|
239 | strBuilder.Append(defunNode.FunctionName + " & = ");
|
---|
240 | } else if (node.Symbol is InvokeFunction) {
|
---|
241 | var invokeNode = node as InvokeFunctionTreeNode;
|
---|
242 | strBuilder.Append(invokeNode.Symbol.FunctionName + @" \left( ");
|
---|
243 | } else if (node.Symbol is StartSymbol) {
|
---|
244 | FormatStartSymbol(strBuilder);
|
---|
245 | } else if (node.Symbol is Argument) {
|
---|
246 | var argSym = node.Symbol as Argument;
|
---|
247 | strBuilder.Append(" ARG+" + argSym.ArgumentIndex + " ");
|
---|
248 | } else if (node.Symbol is Derivative) {
|
---|
249 | strBuilder.Append(@" \cfrac{d \left( ");
|
---|
250 | } else if (node.Symbol is TimeLag) {
|
---|
251 | var laggedNode = node as ILaggedTreeNode;
|
---|
252 | currentLag += laggedNode.Lag;
|
---|
253 | } else if (node.Symbol is Power) {
|
---|
254 | strBuilder.Append(@" \left( ");
|
---|
255 | } else if (node.Symbol is Root) {
|
---|
256 | strBuilder.Append(@" \left( ");
|
---|
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;
|
---|
260 | strBuilder.Append(@"\sum_{t=" + (laggedTreeNode.Lag + currentLag) + @"}^0 \left( ");
|
---|
261 | } else if (node.Symbol is VariableCondition) {
|
---|
262 | var conditionTreeNode = node as VariableConditionTreeNode;
|
---|
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++;
|
---|
271 | strBuilder.Append(@" \left( " + p + @"\cdot ");
|
---|
272 | } else {
|
---|
273 | throw new NotImplementedException("Export of " + node.Symbol + " is not implemented.");
|
---|
274 | }
|
---|
275 | }
|
---|
276 |
|
---|
277 | private void FormatSep(ISymbolicExpressionTreeNode node, StringBuilder strBuilder, int step) {
|
---|
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) {
|
---|
283 | strBuilder.Append(@" \cdot ");
|
---|
284 | } else if (node.Symbol is Division) {
|
---|
285 | if (step + 1 == node.SubtreeCount)
|
---|
286 | strBuilder.Append(@"}{");
|
---|
287 | else
|
---|
288 | strBuilder.Append(@" }{ \cfrac{ ");
|
---|
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();
|
---|
295 | } else if (node.Symbol is Square) {
|
---|
296 | throw new InvalidOperationException();
|
---|
297 | } else if (node.Symbol is SquareRoot) {
|
---|
298 | throw new InvalidOperationException();
|
---|
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();
|
---|
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();
|
---|
329 | } else if (node.Symbol is Norm) {
|
---|
330 | throw new InvalidOperationException();
|
---|
331 | } else if (node.Symbol is Psi) {
|
---|
332 | throw new InvalidOperationException();
|
---|
333 | } else if (node.Symbol is SineIntegral) {
|
---|
334 | throw new InvalidOperationException();
|
---|
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) {
|
---|
340 | strBuilder.Append(@" > 0 \right) \land \left(");
|
---|
341 | } else if (node.Symbol is Or) {
|
---|
342 | strBuilder.Append(@" > 0 \right) \lor \left(");
|
---|
343 | } else if (node.Symbol is Not) {
|
---|
344 | throw new InvalidOperationException();
|
---|
345 | } else if (node.Symbol is IfThenElse) {
|
---|
346 | strBuilder.Append(@" , ");
|
---|
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) {
|
---|
353 | strBuilder.Append(@"\\" + Environment.NewLine);
|
---|
354 | FormatStartSymbol(strBuilder);
|
---|
355 | } else if (node.Symbol is Power) {
|
---|
356 | strBuilder.Append(@"\right) ^ { \operatorname{round} \left(");
|
---|
357 | } else if (node.Symbol is Root) {
|
---|
358 | strBuilder.Append(@"\right) ^ { \cfrac{1}{ \operatorname{round} \left(");
|
---|
359 | } else if (node.Symbol is VariableCondition) {
|
---|
360 | var conditionTreeNode = node as VariableConditionTreeNode;
|
---|
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++;
|
---|
369 | strBuilder.Append(@" + \left( 1 - " + p + @" \right) \cdot ");
|
---|
370 | } else {
|
---|
371 | throw new NotImplementedException("Export of " + node.Symbol + " is not implemented.");
|
---|
372 | }
|
---|
373 | }
|
---|
374 |
|
---|
375 | private void FormatEnd(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) {
|
---|
376 | if (node.Symbol is Addition) {
|
---|
377 | strBuilder.Append(@" \right) ");
|
---|
378 | } else if (node.Symbol is Subtraction) {
|
---|
379 | strBuilder.Append(@" \right) ");
|
---|
380 | } else if (node.Symbol is Multiplication) {
|
---|
381 | } else if (node.Symbol is Division) {
|
---|
382 | strBuilder.Append(" } ");
|
---|
383 | for (int i = 2; i < node.SubtreeCount; i++)
|
---|
384 | strBuilder.Append(" } ");
|
---|
385 | } else if (node.Symbol is Average) {
|
---|
386 | strBuilder.Append(@" \right) ");
|
---|
387 | } else if (node.Symbol is Logarithm) {
|
---|
388 | strBuilder.Append(@" \right) ");
|
---|
389 | } else if (node.Symbol is Exponential) {
|
---|
390 | strBuilder.Append(@" \right) ");
|
---|
391 | } else if (node.Symbol is Square) {
|
---|
392 | strBuilder.Append(@"\right)^2");
|
---|
393 | } else if (node.Symbol is SquareRoot) {
|
---|
394 | strBuilder.Append(@"}");
|
---|
395 | } else if (node.Symbol is Sine) {
|
---|
396 | strBuilder.Append(@" \right) ");
|
---|
397 | } else if (node.Symbol is Cosine) {
|
---|
398 | strBuilder.Append(@" \right) ");
|
---|
399 | } else if (node.Symbol is Tangent) {
|
---|
400 | strBuilder.Append(@" \right) ");
|
---|
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) ");
|
---|
425 | } else if (node.Symbol is Norm) {
|
---|
426 | strBuilder.Append(@" \right) ");
|
---|
427 | } else if (node.Symbol is Psi) {
|
---|
428 | strBuilder.Append(@" \right) ");
|
---|
429 | } else if (node.Symbol is SineIntegral) {
|
---|
430 | strBuilder.Append(@" \right) ");
|
---|
431 | } else if (node.Symbol is GreaterThan) {
|
---|
432 | strBuilder.Append(@" \right) ");
|
---|
433 | } else if (node.Symbol is LessThan) {
|
---|
434 | strBuilder.Append(@" \right) ");
|
---|
435 | } else if (node.Symbol is And) {
|
---|
436 | strBuilder.Append(@" > 0 \right) \right) ");
|
---|
437 | } else if (node.Symbol is Or) {
|
---|
438 | strBuilder.Append(@" > 0 \right) \right) ");
|
---|
439 | } else if (node.Symbol is Not) {
|
---|
440 | strBuilder.Append(@" \right) ");
|
---|
441 | } else if (node.Symbol is IfThenElse) {
|
---|
442 | strBuilder.Append(@" \right) ");
|
---|
443 | } else if (node.Symbol is Constant) {
|
---|
444 | } else if (node.Symbol is LaggedVariable) {
|
---|
445 | } else if (node.Symbol is Variable) {
|
---|
446 | } else if (node.Symbol is FactorVariable) {
|
---|
447 | } else if (node.Symbol is BinaryFactorVariable) {
|
---|
448 | } else if (node.Symbol is ProgramRootSymbol) {
|
---|
449 | strBuilder
|
---|
450 | .AppendLine("\\end{align*}")
|
---|
451 | .AppendLine("\\begin{align*}")
|
---|
452 | .AppendLine("\\nonumber");
|
---|
453 | // output all constant values
|
---|
454 | if (constants.Count > 0) {
|
---|
455 | foreach (var constant in constants) {
|
---|
456 | // replace "." with ".&" to align decimal points
|
---|
457 | var constStr = string.Format(System.Globalization.NumberFormatInfo.InvariantInfo, "{0:G5}", constant.Value);
|
---|
458 | if (!constStr.Contains(".")) constStr = constStr + ".0";
|
---|
459 | constStr = constStr.Replace(".", "&."); // fix problem in rendering of aligned expressions
|
---|
460 | strBuilder.Append(constant.Key + "& = & " + constStr);
|
---|
461 | strBuilder.Append(@"\\");
|
---|
462 | }
|
---|
463 | }
|
---|
464 | strBuilder.AppendLine("\\end{align*}");
|
---|
465 | } else if (node.Symbol is Defun) {
|
---|
466 | } else if (node.Symbol is InvokeFunction) {
|
---|
467 | strBuilder.Append(@" \right) ");
|
---|
468 | } else if (node.Symbol is StartSymbol) {
|
---|
469 | } else if (node.Symbol is Argument) {
|
---|
470 | } else if (node.Symbol is Derivative) {
|
---|
471 | strBuilder.Append(@" \right) }{dt} ");
|
---|
472 | } else if (node.Symbol is TimeLag) {
|
---|
473 | var laggedNode = node as ILaggedTreeNode;
|
---|
474 | currentLag -= laggedNode.Lag;
|
---|
475 | } else if (node.Symbol is Power) {
|
---|
476 | strBuilder.Append(@" \right) } ");
|
---|
477 | } else if (node.Symbol is Root) {
|
---|
478 | strBuilder.Append(@" \right) } } ");
|
---|
479 | } else if (node.Symbol is Integral) {
|
---|
480 | strBuilder.Append(@" \right) ");
|
---|
481 | } else if (node.Symbol is VariableCondition) {
|
---|
482 | strBuilder.Append(@"\right) ");
|
---|
483 | } else {
|
---|
484 | throw new NotImplementedException("Export of " + node.Symbol + " is not implemented.");
|
---|
485 | }
|
---|
486 | }
|
---|
487 |
|
---|
488 | private void FormatStartSymbol(StringBuilder strBuilder) {
|
---|
489 | strBuilder.Append(EscapeLatexString(targetVariable) ?? "target_{" + (targetCount++) + "}");
|
---|
490 | if (containsTimeSeriesSymbol)
|
---|
491 | strBuilder.Append("(t)");
|
---|
492 | strBuilder.Append(" & = ");
|
---|
493 | }
|
---|
494 |
|
---|
495 | private string LagToString(int lag) {
|
---|
496 | if (lag < 0) {
|
---|
497 | return "(t" + lag + ")";
|
---|
498 | } else if (lag > 0) {
|
---|
499 | return "(t+" + lag + ")";
|
---|
500 | } else return "";
|
---|
501 | }
|
---|
502 |
|
---|
503 | private string EscapeLatexString(string s) {
|
---|
504 | return "\\text{" +
|
---|
505 | s
|
---|
506 | .Replace("\\", "\\\\")
|
---|
507 | .Replace("{", "\\{")
|
---|
508 | .Replace("}", "\\}")
|
---|
509 | + "}";
|
---|
510 | }
|
---|
511 | }
|
---|
512 | }
|
---|