[4327] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17180] | 3 | * Copyright (C) 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] | 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
[4327] | 25 | using System.Text;
|
---|
[16702] | 26 | using HEAL.Attic;
|
---|
[6975] | 27 | using HeuristicLab.Common;
|
---|
| 28 | using HeuristicLab.Core;
|
---|
| 29 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
[4327] | 30 |
|
---|
[5745] | 31 | namespace 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 | }
|
---|
[16802] | 119 | } else if (node.Symbol is Absolute) {
|
---|
| 120 | strBuilder.Append(@"\operatorname{abs} \left( ");
|
---|
| 121 | } else if (node.Symbol is AnalyticQuotient) {
|
---|
| 122 | strBuilder.Append(@" \frac { ");
|
---|
[4969] | 123 | } else if (node.Symbol is Average) {
|
---|
[5428] | 124 | // skip output of (1/1) if only one subtree
|
---|
[6803] | 125 | if (node.SubtreeCount > 1) {
|
---|
| 126 | strBuilder.Append(@" \cfrac{1}{" + node.SubtreeCount + @"}");
|
---|
[5428] | 127 | }
|
---|
[7446] | 128 | strBuilder.Append(@" \left( ");
|
---|
[4969] | 129 | } else if (node.Symbol is Logarithm) {
|
---|
[7446] | 130 | strBuilder.Append(@"\log \left( ");
|
---|
[4969] | 131 | } else if (node.Symbol is Exponential) {
|
---|
[7446] | 132 | strBuilder.Append(@"\exp \left( ");
|
---|
[7695] | 133 | } else if (node.Symbol is Square) {
|
---|
| 134 | strBuilder.Append(@"\left(");
|
---|
| 135 | } else if (node.Symbol is SquareRoot) {
|
---|
| 136 | strBuilder.Append(@"\sqrt{");
|
---|
[16802] | 137 | } else if (node.Symbol is Cube) {
|
---|
| 138 | strBuilder.Append(@"\left(");
|
---|
| 139 | } else if (node.Symbol is CubeRoot) {
|
---|
[16905] | 140 | strBuilder.Append(@"\operatorname{cbrt}\left(");
|
---|
[4969] | 141 | } else if (node.Symbol is Sine) {
|
---|
[7446] | 142 | strBuilder.Append(@"\sin \left( ");
|
---|
[4969] | 143 | } else if (node.Symbol is Cosine) {
|
---|
[7446] | 144 | strBuilder.Append(@"\cos \left( ");
|
---|
[4969] | 145 | } else if (node.Symbol is Tangent) {
|
---|
[7446] | 146 | strBuilder.Append(@"\tan \left( ");
|
---|
[16702] | 147 | } else if (node.Symbol is HyperbolicTangent) {
|
---|
| 148 | strBuilder.Append(@"\tanh \left( ");
|
---|
[7696] | 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) {
|
---|
[7708] | 164 | strBuilder.Append(@"\operatorname{fresnel}_\operatorname{cosInt} \left( ");
|
---|
[7696] | 165 | } else if (node.Symbol is FresnelSineIntegral) {
|
---|
[7708] | 166 | strBuilder.Append(@"\operatorname{fresnel}_\operatorname{sinInt} \left( ");
|
---|
[7696] | 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( ");
|
---|
[7697] | 173 | } else if (node.Symbol is Norm) {
|
---|
| 174 | strBuilder.Append(@"\operatorname{norm} \left( ");
|
---|
[7696] | 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( ");
|
---|
[4969] | 179 | } else if (node.Symbol is GreaterThan) {
|
---|
[7446] | 180 | strBuilder.Append(@" \left( ");
|
---|
[4969] | 181 | } else if (node.Symbol is LessThan) {
|
---|
[7446] | 182 | strBuilder.Append(@" \left( ");
|
---|
[4969] | 183 | } else if (node.Symbol is And) {
|
---|
[7451] | 184 | strBuilder.Append(@" \left( \left( ");
|
---|
[4969] | 185 | } else if (node.Symbol is Or) {
|
---|
[7451] | 186 | strBuilder.Append(@" \left( \left( ");
|
---|
[4969] | 187 | } else if (node.Symbol is Not) {
|
---|
[7446] | 188 | strBuilder.Append(@" \neg \left( ");
|
---|
[4969] | 189 | } else if (node.Symbol is IfThenElse) {
|
---|
[7446] | 190 | strBuilder.Append(@" \operatorname{if} \left( ");
|
---|
[4327] | 191 | } else if (node.Symbol is Constant) {
|
---|
[14826] | 192 | var constName = "c_{" + constIndex + "}";
|
---|
| 193 | constIndex++;
|
---|
[4969] | 194 | var constNode = node as ConstantTreeNode;
|
---|
[14367] | 195 | if (constNode.Value.IsAlmost(1.0)) {
|
---|
| 196 | strBuilder.Append("1 ");
|
---|
| 197 | } else {
|
---|
[14826] | 198 | strBuilder.Append(constName);
|
---|
| 199 | constants.Add(new KeyValuePair<string, double>(constName, constNode.Value));
|
---|
[14367] | 200 | }
|
---|
[14826] | 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) + " )");
|
---|
[5428] | 222 | } else if (node.Symbol is LaggedVariable) {
|
---|
| 223 | var laggedVarNode = node as LaggedVariableTreeNode;
|
---|
[7038] | 224 | if (!laggedVarNode.Weight.IsAlmost(1.0)) {
|
---|
[14826] | 225 | var constName = "c_{" + constIndex + "}";
|
---|
| 226 | strBuilder.Append(constName + " \\cdot");
|
---|
| 227 | constants.Add(new KeyValuePair<string, double>(constName, laggedVarNode.Weight));
|
---|
| 228 | constIndex++;
|
---|
[7038] | 229 | }
|
---|
| 230 | strBuilder.Append(EscapeLatexString(laggedVarNode.VariableName));
|
---|
[5428] | 231 | strBuilder.Append(LagToString(currentLag + laggedVarNode.Lag));
|
---|
[7038] | 232 |
|
---|
| 233 | } else if (node.Symbol is Variable) {
|
---|
[4327] | 234 | var varNode = node as VariableTreeNode;
|
---|
[7038] | 235 | if (!varNode.Weight.IsAlmost((1.0))) {
|
---|
[14826] | 236 | var constName = "c_{" + constIndex + "}";
|
---|
| 237 | strBuilder.Append(constName + " \\cdot");
|
---|
| 238 | constants.Add(new KeyValuePair<string, double>(constName, varNode.Weight));
|
---|
| 239 | constIndex++;
|
---|
[7038] | 240 | }
|
---|
| 241 | strBuilder.Append(EscapeLatexString(varNode.VariableName));
|
---|
[5428] | 242 | strBuilder.Append(LagToString(currentLag));
|
---|
[4327] | 243 | } else if (node.Symbol is ProgramRootSymbol) {
|
---|
[7446] | 244 | strBuilder
|
---|
| 245 | .AppendLine("\\begin{align*}")
|
---|
| 246 | .AppendLine("\\nonumber");
|
---|
[4327] | 247 | } else if (node.Symbol is Defun) {
|
---|
| 248 | var defunNode = node as DefunTreeNode;
|
---|
[4969] | 249 | strBuilder.Append(defunNode.FunctionName + " & = ");
|
---|
[4327] | 250 | } else if (node.Symbol is InvokeFunction) {
|
---|
| 251 | var invokeNode = node as InvokeFunctionTreeNode;
|
---|
[7446] | 252 | strBuilder.Append(invokeNode.Symbol.FunctionName + @" \left( ");
|
---|
[4327] | 253 | } else if (node.Symbol is StartSymbol) {
|
---|
[14255] | 254 | FormatStartSymbol(strBuilder);
|
---|
[4900] | 255 | } else if (node.Symbol is Argument) {
|
---|
[4327] | 256 | var argSym = node.Symbol as Argument;
|
---|
[4900] | 257 | strBuilder.Append(" ARG+" + argSym.ArgumentIndex + " ");
|
---|
[5428] | 258 | } else if (node.Symbol is Derivative) {
|
---|
[7446] | 259 | strBuilder.Append(@" \cfrac{d \left( ");
|
---|
[5428] | 260 | } else if (node.Symbol is TimeLag) {
|
---|
| 261 | var laggedNode = node as ILaggedTreeNode;
|
---|
| 262 | currentLag += laggedNode.Lag;
|
---|
| 263 | } else if (node.Symbol is Power) {
|
---|
[7446] | 264 | strBuilder.Append(@" \left( ");
|
---|
[5428] | 265 | } else if (node.Symbol is Root) {
|
---|
[7446] | 266 | strBuilder.Append(@" \left( ");
|
---|
[5428] | 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;
|
---|
[7446] | 270 | strBuilder.Append(@"\sum_{t=" + (laggedTreeNode.Lag + currentLag) + @"}^0 \left( ");
|
---|
[5468] | 271 | } else if (node.Symbol is VariableCondition) {
|
---|
| 272 | var conditionTreeNode = node as VariableConditionTreeNode;
|
---|
[14826] | 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++;
|
---|
[7446] | 281 | strBuilder.Append(@" \left( " + p + @"\cdot ");
|
---|
[4327] | 282 | } else {
|
---|
| 283 | throw new NotImplementedException("Export of " + node.Symbol + " is not implemented.");
|
---|
| 284 | }
|
---|
| 285 | }
|
---|
| 286 |
|
---|
[7140] | 287 | private void FormatSep(ISymbolicExpressionTreeNode node, StringBuilder strBuilder, int step) {
|
---|
[4327] | 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) {
|
---|
[5428] | 293 | strBuilder.Append(@" \cdot ");
|
---|
[4327] | 294 | } else if (node.Symbol is Division) {
|
---|
[7140] | 295 | if (step + 1 == node.SubtreeCount)
|
---|
| 296 | strBuilder.Append(@"}{");
|
---|
| 297 | else
|
---|
| 298 | strBuilder.Append(@" }{ \cfrac{ ");
|
---|
[16802] | 299 | } else if (node.Symbol is Absolute) {
|
---|
| 300 | throw new InvalidOperationException();
|
---|
| 301 | } else if (node.Symbol is AnalyticQuotient) {
|
---|
| 302 | strBuilder.Append(@"}{\sqrt{1 + \left( ");
|
---|
[4969] | 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();
|
---|
[7695] | 309 | } else if (node.Symbol is Square) {
|
---|
| 310 | throw new InvalidOperationException();
|
---|
| 311 | } else if (node.Symbol is SquareRoot) {
|
---|
| 312 | throw new InvalidOperationException();
|
---|
[16802] | 313 | } else if (node.Symbol is Cube) {
|
---|
| 314 | throw new InvalidOperationException();
|
---|
| 315 | } else if (node.Symbol is CubeRoot) {
|
---|
| 316 | throw new InvalidOperationException();
|
---|
[4969] | 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();
|
---|
[16702] | 323 | } else if (node.Symbol is HyperbolicTangent) {
|
---|
| 324 | throw new InvalidOperationException();
|
---|
[7696] | 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();
|
---|
[7697] | 349 | } else if (node.Symbol is Norm) {
|
---|
| 350 | throw new InvalidOperationException();
|
---|
[7696] | 351 | } else if (node.Symbol is Psi) {
|
---|
| 352 | throw new InvalidOperationException();
|
---|
| 353 | } else if (node.Symbol is SineIntegral) {
|
---|
| 354 | throw new InvalidOperationException();
|
---|
[4969] | 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) {
|
---|
[7446] | 360 | strBuilder.Append(@" > 0 \right) \land \left(");
|
---|
[4969] | 361 | } else if (node.Symbol is Or) {
|
---|
[7446] | 362 | strBuilder.Append(@" > 0 \right) \lor \left(");
|
---|
[4969] | 363 | } else if (node.Symbol is Not) {
|
---|
| 364 | throw new InvalidOperationException();
|
---|
| 365 | } else if (node.Symbol is IfThenElse) {
|
---|
[7446] | 366 | strBuilder.Append(@" , ");
|
---|
[4327] | 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) {
|
---|
[8798] | 373 | strBuilder.Append(@"\\" + Environment.NewLine);
|
---|
[14255] | 374 | FormatStartSymbol(strBuilder);
|
---|
[5428] | 375 | } else if (node.Symbol is Power) {
|
---|
[7446] | 376 | strBuilder.Append(@"\right) ^ { \operatorname{round} \left(");
|
---|
[5428] | 377 | } else if (node.Symbol is Root) {
|
---|
[7446] | 378 | strBuilder.Append(@"\right) ^ { \cfrac{1}{ \operatorname{round} \left(");
|
---|
[5468] | 379 | } else if (node.Symbol is VariableCondition) {
|
---|
| 380 | var conditionTreeNode = node as VariableConditionTreeNode;
|
---|
[14826] | 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++;
|
---|
[7446] | 389 | strBuilder.Append(@" + \left( 1 - " + p + @" \right) \cdot ");
|
---|
[4327] | 390 | } else {
|
---|
| 391 | throw new NotImplementedException("Export of " + node.Symbol + " is not implemented.");
|
---|
| 392 | }
|
---|
| 393 | }
|
---|
| 394 |
|
---|
[5745] | 395 | private void FormatEnd(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) {
|
---|
[4327] | 396 | if (node.Symbol is Addition) {
|
---|
[7446] | 397 | strBuilder.Append(@" \right) ");
|
---|
[4327] | 398 | } else if (node.Symbol is Subtraction) {
|
---|
[7446] | 399 | strBuilder.Append(@" \right) ");
|
---|
[4327] | 400 | } else if (node.Symbol is Multiplication) {
|
---|
| 401 | } else if (node.Symbol is Division) {
|
---|
[7140] | 402 | strBuilder.Append(" } ");
|
---|
| 403 | for (int i = 2; i < node.SubtreeCount; i++)
|
---|
[5428] | 404 | strBuilder.Append(" } ");
|
---|
[16802] | 405 | } else if (node.Symbol is Absolute) {
|
---|
| 406 | strBuilder.Append(@" \right)");
|
---|
| 407 | } else if (node.Symbol is AnalyticQuotient) {
|
---|
| 408 | strBuilder.Append(@" \right)^2}}");
|
---|
[4969] | 409 | } else if (node.Symbol is Average) {
|
---|
[7446] | 410 | strBuilder.Append(@" \right) ");
|
---|
[4969] | 411 | } else if (node.Symbol is Logarithm) {
|
---|
[7446] | 412 | strBuilder.Append(@" \right) ");
|
---|
[4969] | 413 | } else if (node.Symbol is Exponential) {
|
---|
[7446] | 414 | strBuilder.Append(@" \right) ");
|
---|
[7695] | 415 | } else if (node.Symbol is Square) {
|
---|
| 416 | strBuilder.Append(@"\right)^2");
|
---|
| 417 | } else if (node.Symbol is SquareRoot) {
|
---|
| 418 | strBuilder.Append(@"}");
|
---|
[16802] | 419 | } else if (node.Symbol is Cube) {
|
---|
| 420 | strBuilder.Append(@"\right)^3");
|
---|
| 421 | } else if (node.Symbol is CubeRoot) {
|
---|
[16905] | 422 | strBuilder.Append(@"\right)");
|
---|
[4969] | 423 | } else if (node.Symbol is Sine) {
|
---|
[7446] | 424 | strBuilder.Append(@" \right) ");
|
---|
[4969] | 425 | } else if (node.Symbol is Cosine) {
|
---|
[7446] | 426 | strBuilder.Append(@" \right) ");
|
---|
[4969] | 427 | } else if (node.Symbol is Tangent) {
|
---|
[7446] | 428 | strBuilder.Append(@" \right) ");
|
---|
[16702] | 429 | } else if (node.Symbol is HyperbolicTangent) {
|
---|
| 430 | strBuilder.Append(@" \right) ");
|
---|
[7696] | 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) ");
|
---|
[7697] | 455 | } else if (node.Symbol is Norm) {
|
---|
| 456 | strBuilder.Append(@" \right) ");
|
---|
[7696] | 457 | } else if (node.Symbol is Psi) {
|
---|
| 458 | strBuilder.Append(@" \right) ");
|
---|
| 459 | } else if (node.Symbol is SineIntegral) {
|
---|
| 460 | strBuilder.Append(@" \right) ");
|
---|
[4969] | 461 | } else if (node.Symbol is GreaterThan) {
|
---|
[7446] | 462 | strBuilder.Append(@" \right) ");
|
---|
[4969] | 463 | } else if (node.Symbol is LessThan) {
|
---|
[7446] | 464 | strBuilder.Append(@" \right) ");
|
---|
[4969] | 465 | } else if (node.Symbol is And) {
|
---|
[7446] | 466 | strBuilder.Append(@" > 0 \right) \right) ");
|
---|
[4969] | 467 | } else if (node.Symbol is Or) {
|
---|
[7446] | 468 | strBuilder.Append(@" > 0 \right) \right) ");
|
---|
[4969] | 469 | } else if (node.Symbol is Not) {
|
---|
[7446] | 470 | strBuilder.Append(@" \right) ");
|
---|
[4969] | 471 | } else if (node.Symbol is IfThenElse) {
|
---|
[7446] | 472 | strBuilder.Append(@" \right) ");
|
---|
[4327] | 473 | } else if (node.Symbol is Constant) {
|
---|
[5428] | 474 | } else if (node.Symbol is LaggedVariable) {
|
---|
[7038] | 475 | } else if (node.Symbol is Variable) {
|
---|
[14826] | 476 | } else if (node.Symbol is FactorVariable) {
|
---|
| 477 | } else if (node.Symbol is BinaryFactorVariable) {
|
---|
[4327] | 478 | } else if (node.Symbol is ProgramRootSymbol) {
|
---|
[7446] | 479 | strBuilder
|
---|
| 480 | .AppendLine("\\end{align*}")
|
---|
| 481 | .AppendLine("\\begin{align*}")
|
---|
| 482 | .AppendLine("\\nonumber");
|
---|
[4969] | 483 | // output all constant values
|
---|
| 484 | if (constants.Count > 0) {
|
---|
| 485 | foreach (var constant in constants) {
|
---|
[7446] | 486 | // replace "." with ".&" to align decimal points
|
---|
[14826] | 487 | var constStr = string.Format(System.Globalization.NumberFormatInfo.InvariantInfo, "{0:G5}", constant.Value);
|
---|
[7451] | 488 | if (!constStr.Contains(".")) constStr = constStr + ".0";
|
---|
[13933] | 489 | constStr = constStr.Replace(".", "&."); // fix problem in rendering of aligned expressions
|
---|
[14826] | 490 | strBuilder.Append(constant.Key + "& = & " + constStr);
|
---|
[7446] | 491 | strBuilder.Append(@"\\");
|
---|
[4969] | 492 | }
|
---|
| 493 | }
|
---|
[7446] | 494 | strBuilder.AppendLine("\\end{align*}");
|
---|
[4327] | 495 | } else if (node.Symbol is Defun) {
|
---|
| 496 | } else if (node.Symbol is InvokeFunction) {
|
---|
[7446] | 497 | strBuilder.Append(@" \right) ");
|
---|
[4327] | 498 | } else if (node.Symbol is StartSymbol) {
|
---|
| 499 | } else if (node.Symbol is Argument) {
|
---|
[5428] | 500 | } else if (node.Symbol is Derivative) {
|
---|
[7446] | 501 | strBuilder.Append(@" \right) }{dt} ");
|
---|
[5428] | 502 | } else if (node.Symbol is TimeLag) {
|
---|
| 503 | var laggedNode = node as ILaggedTreeNode;
|
---|
| 504 | currentLag -= laggedNode.Lag;
|
---|
| 505 | } else if (node.Symbol is Power) {
|
---|
[7446] | 506 | strBuilder.Append(@" \right) } ");
|
---|
[5428] | 507 | } else if (node.Symbol is Root) {
|
---|
[7446] | 508 | strBuilder.Append(@" \right) } } ");
|
---|
[5428] | 509 | } else if (node.Symbol is Integral) {
|
---|
[7446] | 510 | strBuilder.Append(@" \right) ");
|
---|
[5468] | 511 | } else if (node.Symbol is VariableCondition) {
|
---|
[7446] | 512 | strBuilder.Append(@"\right) ");
|
---|
[4327] | 513 | } else {
|
---|
| 514 | throw new NotImplementedException("Export of " + node.Symbol + " is not implemented.");
|
---|
| 515 | }
|
---|
| 516 | }
|
---|
[6975] | 517 |
|
---|
[14255] | 518 | private void FormatStartSymbol(StringBuilder strBuilder) {
|
---|
| 519 | strBuilder.Append(targetVariable ?? "target_" + (targetCount++));
|
---|
| 520 | if (containsTimeSeriesSymbol)
|
---|
| 521 | strBuilder.Append("(t)");
|
---|
| 522 | strBuilder.Append(" & = ");
|
---|
| 523 | }
|
---|
| 524 |
|
---|
[5428] | 525 | private string LagToString(int lag) {
|
---|
| 526 | if (lag < 0) {
|
---|
| 527 | return "(t" + lag + ")";
|
---|
| 528 | } else if (lag > 0) {
|
---|
| 529 | return "(t+" + lag + ")";
|
---|
[7038] | 530 | } else return "";
|
---|
[5428] | 531 | }
|
---|
[6975] | 532 |
|
---|
| 533 | private string EscapeLatexString(string s) {
|
---|
[7446] | 534 | return "\\text{" +
|
---|
| 535 | s
|
---|
| 536 | .Replace("\\", "\\\\")
|
---|
| 537 | .Replace("{", "\\{")
|
---|
| 538 | .Replace("}", "\\}")
|
---|
| 539 | + "}";
|
---|
[6975] | 540 | }
|
---|
[4327] | 541 | }
|
---|
| 542 | }
|
---|