Changeset 4969
 Timestamp:
 11/28/10 11:08:22 (13 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

branches/DataAnalysis.Extensions/HeuristicLab.Problems.DataAnalysis/3.3/Symbolic/Formatters/SymbolicExpressionTreeLatexFormatter.cs
r4906 r4969 32 32 33 33 namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Formatters { 34 [Item(" SymbolicExpressionTreeLatexFormatter", "Formatter for symbolic expression trees for import into LaTeX documents.")]34 [Item("LaTeX String Formatter", "Formatter for symbolic expression trees for import into LaTeX documents.")] 35 35 public sealed class SymbolicExpressionTreeLatexFormatter : NamedItem, ISymbolicExpressionTreeStringFormatter { 36 private List<double> constants; 37 36 38 public SymbolicExpressionTreeLatexFormatter() 37 : base("SymbolicExpressionTreeLatexFormatter", "Formatter for symbolic expression trees for import into LaTeX documents.") { } 38 private SymbolicExpressionTreeLatexFormatter(SymbolicExpressionTreeLatexFormatter original, Cloner cloner) : base(original, cloner) { } 39 : base("LaTeX String Formatter", "Formatter for symbolic expression trees for import into LaTeX documents.") { 40 constants = new List<double>(); 41 } 42 private SymbolicExpressionTreeLatexFormatter(SymbolicExpressionTreeLatexFormatter original, Cloner cloner) 43 : base(original, cloner) { 44 constants = new List<double>(original.constants); 45 } 39 46 40 47 public override IDeepCloneable Clone(Cloner cloner) { … … 45 52 try { 46 53 StringBuilder strBuilder = new StringBuilder(); 47 strBuilder.AppendLine("\\begin{eqnarray}"); 54 constants.Clear(); 55 strBuilder.AppendLine("% needs \\usepackage{amsmath}"); 56 strBuilder.AppendLine("\\begin{align}"); 48 57 strBuilder.AppendLine(FormatRecursively(symbolicExpressionTree.Root)); 49 strBuilder.AppendLine("\\end{ eqnarray}");58 strBuilder.AppendLine("\\end{align}"); 50 59 return strBuilder.ToString(); 51 60 } … … 83 92 if (node.SubTrees.Count != 2) throw new NotImplementedException("Division with more than 2 arguments is not implemented."); 84 93 strBuilder.Append(@" \frac{ "); 94 } else if (node.Symbol is Average) { 95 strBuilder.Append(@" \frac{1}{" + node.SubTrees.Count + @"} \left("); 96 } else if (node.Symbol is Logarithm) { 97 strBuilder.Append(@"\log \left("); 98 } else if (node.Symbol is Exponential) { 99 strBuilder.Append(@"\exp \left("); 100 } else if (node.Symbol is Sine) { 101 strBuilder.Append(@"\sin \left("); 102 } else if (node.Symbol is Cosine) { 103 strBuilder.Append(@"\cos \left("); 104 } else if (node.Symbol is Tangent) { 105 strBuilder.Append(@"\tan \left("); 106 } else if (node.Symbol is GreaterThan) { 107 strBuilder.Append(@" \left( "); 108 } else if (node.Symbol is LessThan) { 109 strBuilder.Append(@" \left( "); 110 } else if (node.Symbol is And) { 111 strBuilder.Append(@" \left( \left( "); 112 } else if (node.Symbol is Or) { 113 strBuilder.Append(@" \left( \left( "); 114 } else if (node.Symbol is Not) { 115 strBuilder.Append(@" 1.0 \times \left( "); 116 } else if (node.Symbol is IfThenElse) { 117 strBuilder.Append(@"\left( \operatorname{if} \left( 0 < "); 85 118 } else if (node.Symbol is Constant) { 86 strBuilder.Append(" c "); 119 strBuilder.Append("c_{" + constants.Count+"} "); 120 var constNode = node as ConstantTreeNode; 121 constants.Add(constNode.Value); 87 122 } else if (node.Symbol is HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.Variable) { 88 123 var varNode = node as VariableTreeNode; 89 strBuilder.Append(" " + varNode.ToString() + " "); 124 strBuilder.Append("c_{" + constants.Count + "} " + varNode.VariableName + " "); 125 constants.Add(varNode.Weight); 126 } else if (node.Symbol is LaggedVariable) { 127 var laggedVarNode = node as LaggedVariableTreeNode; 128 strBuilder.Append("c_{" + constants.Count + "} " + laggedVarNode.VariableName + "(t" + laggedVarNode.Lag + ")"); 129 constants.Add(laggedVarNode.Weight); 90 130 } else if (node.Symbol is ProgramRootSymbol) { 91 131 } else if (node.Symbol is Defun) { 92 132 var defunNode = node as DefunTreeNode; 93 strBuilder.Append(defunNode.FunctionName + " & = &");133 strBuilder.Append(defunNode.FunctionName + " & = "); 94 134 } else if (node.Symbol is InvokeFunction) { 95 135 var invokeNode = node as InvokeFunctionTreeNode; 96 136 strBuilder.Append(invokeNode.Symbol.FunctionName + @" \left( "); 97 137 } else if (node.Symbol is StartSymbol) { 98 strBuilder.Append("Result & = &");138 strBuilder.Append("Result & = "); 99 139 } else if (node.Symbol is Argument) { 100 140 var argSym = node.Symbol as Argument; … … 111 151 strBuilder.Append("  "); 112 152 } else if (node.Symbol is Multiplication) { 153 strBuilder.Append(@" \times "); 113 154 } else if (node.Symbol is Division) { 114 155 strBuilder.Append(" }{ "); 156 } else if (node.Symbol is Average) { 157 strBuilder.Append(@" + "); 158 } else if (node.Symbol is Logarithm) { 159 throw new InvalidOperationException(); 160 } else if (node.Symbol is Exponential) { 161 throw new InvalidOperationException(); 162 } else if (node.Symbol is Sine) { 163 throw new InvalidOperationException(); 164 } else if (node.Symbol is Cosine) { 165 throw new InvalidOperationException(); 166 } else if (node.Symbol is Tangent) { 167 throw new InvalidOperationException(); 168 } else if (node.Symbol is GreaterThan) { 169 strBuilder.Append(@" > "); 170 } else if (node.Symbol is LessThan) { 171 strBuilder.Append(@" < "); 172 } else if (node.Symbol is And) { 173 strBuilder.Append(@" > 0 \right) \land \left("); 174 } else if (node.Symbol is Or) { 175 strBuilder.Append(@" > 0 \right) \lor \left("); 176 } else if (node.Symbol is Not) { 177 throw new InvalidOperationException(); 178 } else if (node.Symbol is IfThenElse) { 179 strBuilder.Append(@" \right) , \left("); 115 180 } else if (node.Symbol is ProgramRootSymbol) { 116 181 strBuilder.Append(@"\\" + Environment.NewLine); … … 119 184 strBuilder.Append(" , "); 120 185 } else if (node.Symbol is StartSymbol) { 121 strBuilder.Append(@"\\" + Environment.NewLine + " & &");186 strBuilder.Append(@"\\" + Environment.NewLine + " & "); 122 187 } else { 123 188 throw new NotImplementedException("Export of " + node.Symbol + " is not implemented."); … … 133 198 } else if (node.Symbol is Division) { 134 199 strBuilder.Append(" } "); 200 } else if (node.Symbol is Average) { 201 strBuilder.Append(@" \right)"); 202 } else if (node.Symbol is Logarithm) { 203 strBuilder.Append(@" \right) "); 204 } else if (node.Symbol is Exponential) { 205 strBuilder.Append(@" \right) "); 206 } else if (node.Symbol is Sine) { 207 strBuilder.Append(@" \right) "); 208 } else if (node.Symbol is Cosine) { 209 strBuilder.Append(@" \right) "); 210 } else if (node.Symbol is Tangent) { 211 strBuilder.Append(@" \right) "); 212 } else if (node.Symbol is GreaterThan) { 213 strBuilder.Append(@" \right) "); 214 } else if (node.Symbol is LessThan) { 215 strBuilder.Append(@" \right) "); 216 } else if (node.Symbol is And) { 217 strBuilder.Append(@" > 0 \right) \right) "); 218 } else if (node.Symbol is Or) { 219 strBuilder.Append(@" > 0 \right) \right) "); 220 } else if (node.Symbol is Not) { 221 strBuilder.Append(@" \right) "); 222 } else if (node.Symbol is IfThenElse) { 223 strBuilder.Append(@" \right) \right) "); 135 224 } else if (node.Symbol is Constant) { 136 225 } else if (node.Symbol is HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.Variable) { 226 } else if (node.Symbol is LaggedVariable) { 137 227 } else if (node.Symbol is ProgramRootSymbol) { 228 // output all constant values 229 if (constants.Count > 0) { 230 int i = 0; 231 foreach (var constant in constants) { 232 strBuilder.AppendLine(@"\\"); 233 strBuilder.Append("c_{" + i + "} & = " + constant); 234 i++; 235 } 236 } 138 237 } else if (node.Symbol is Defun) { 139 238 } else if (node.Symbol is InvokeFunction) {
Note: See TracChangeset
for help on using the changeset viewer.