#region License Information /* HeuristicLab * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System.Text; using System.Linq; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; using System.Collections.Generic; using HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols; using System; using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols; namespace HeuristicLab.Problems.DataAnalysis.Views.Symbolic { public class SymbolicExpressionTreeLatexFormatter { public SymbolicExpressionTreeLatexFormatter() : base() { } public string Format(SymbolicExpressionTree symbolicExpressionTree) { try { StringBuilder strBuilder = new StringBuilder(); strBuilder.AppendLine("\\begin{eqnarray}"); strBuilder.AppendLine(FormatRecursively(symbolicExpressionTree.Root)); strBuilder.AppendLine("\\end{eqnarray}"); return strBuilder.ToString(); } catch (NotImplementedException ex) { return ex.Message + Environment.NewLine + ex.StackTrace; } } private string FormatRecursively(SymbolicExpressionTreeNode node) { StringBuilder strBuilder = new StringBuilder(); FormatBegin(node, strBuilder); if (node.SubTrees.Count > 0) { strBuilder.Append(FormatRecursively(node.SubTrees[0])); } foreach (SymbolicExpressionTreeNode subTree in node.SubTrees.Skip(1)) { FormatSep(node, strBuilder); // format the whole subtree strBuilder.Append(FormatRecursively(subTree)); } FormatEnd(node, strBuilder); return strBuilder.ToString(); } private void FormatBegin(SymbolicExpressionTreeNode node, StringBuilder strBuilder) { if (node.Symbol is Addition) { strBuilder.Append(@" \left( "); } else if (node.Symbol is Subtraction) { strBuilder.Append(@" \left( "); } else if (node.Symbol is Multiplication) { } else if (node.Symbol is Division) { if (node.SubTrees.Count != 2) throw new NotImplementedException("Division with more than 2 arguments is not implemented."); strBuilder.Append(@" \frac{ "); } else if (node.Symbol is Constant) { strBuilder.Append(" c "); } else if (node.Symbol is Variable) { var varNode = node as VariableTreeNode; strBuilder.Append(" " + varNode.ToString()+ " "); } else if (node.Symbol is ProgramRootSymbol) { } else if (node.Symbol is Defun) { var defunNode = node as DefunTreeNode; strBuilder.Append(defunNode.FunctionName + " & = & "); } else if (node.Symbol is InvokeFunction) { var invokeNode = node as InvokeFunctionTreeNode; strBuilder.Append(invokeNode.Symbol.FunctionName + @" \left( "); } else if (node.Symbol is StartSymbol) { strBuilder.Append("Result & = & "); } else if(node.Symbol is Argument) { var argSym = node.Symbol as Argument; strBuilder.Append(" ARG+"+argSym.ArgumentIndex+" "); } else { throw new NotImplementedException("Export of " + node.Symbol + " is not implemented."); } } private void FormatSep(SymbolicExpressionTreeNode node, StringBuilder strBuilder) { if (node.Symbol is Addition) { strBuilder.Append(" + "); } else if (node.Symbol is Subtraction) { strBuilder.Append(" - "); } else if (node.Symbol is Multiplication) { } else if (node.Symbol is Division) { strBuilder.Append(" }{ "); } else if (node.Symbol is ProgramRootSymbol) { strBuilder.Append(@"\\" + Environment.NewLine); } else if (node.Symbol is Defun) { } else if (node.Symbol is InvokeFunction) { strBuilder.Append(" , "); } else if (node.Symbol is StartSymbol) { strBuilder.Append(@"\\" + Environment.NewLine + " & & "); } else { throw new NotImplementedException("Export of " + node.Symbol + " is not implemented."); } } private void FormatEnd(SymbolicExpressionTreeNode node, StringBuilder strBuilder) { if (node.Symbol is Addition) { strBuilder.Append(@" \right) "); } else if (node.Symbol is Subtraction) { strBuilder.Append(@" \right) "); } else if (node.Symbol is Multiplication) { } else if (node.Symbol is Division) { strBuilder.Append(" } "); } else if (node.Symbol is Constant) { } else if (node.Symbol is Variable) { } else if (node.Symbol is ProgramRootSymbol) { } else if (node.Symbol is Defun) { } else if (node.Symbol is InvokeFunction) { strBuilder.Append(@" \right) "); } else if (node.Symbol is StartSymbol) { } else if (node.Symbol is Argument) { } else { throw new NotImplementedException("Export of " + node.Symbol + " is not implemented."); } } } }