Changeset 5428
- Timestamp:
- 02/04/11 11:34:59 (14 years ago)
- Location:
- trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3
- Files:
-
- 1 added
- 1 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/HeuristicLab.Problems.DataAnalysis-3.3.csproj
r5384 r5428 108 108 </ItemGroup> 109 109 <ItemGroup> 110 <Compile Include="Symbolic\Formatters\SymbolicExpressionTreeLatexFormatter.cs" /> 110 111 <Compile Include="Symbolic\Symbols\Root.cs" /> 111 112 <Compile Include="Symbolic\Symbols\Derivative.cs" /> -
trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Symbolic/Formatters/SymbolicExpressionTreeLatexFormatter.cs
r5188 r5428 35 35 public sealed class SymbolicExpressionTreeLatexFormatter : NamedItem, ISymbolicExpressionTreeStringFormatter { 36 36 private List<double> constants; 37 private int currentLag; 37 38 38 39 public SymbolicExpressionTreeLatexFormatter() … … 66 67 private string FormatRecursively(SymbolicExpressionTreeNode node) { 67 68 StringBuilder strBuilder = new StringBuilder(); 68 69 currentLag = 0; 69 70 FormatBegin(node, strBuilder); 70 71 … … 87 88 strBuilder.Append(@" \left( "); 88 89 } else if (node.Symbol is Subtraction) { 89 strBuilder.Append(@" \left( "); 90 if (node.SubTrees.Count == 1) { 91 strBuilder.Append(@"- \left("); 92 } else { 93 strBuilder.Append(@" \left( "); 94 } 90 95 } else if (node.Symbol is Multiplication) { 91 96 } else if (node.Symbol is Division) { 92 if (node.SubTrees.Count != 2) throw new NotImplementedException("Division with more than 2 arguments is not implemented."); 93 strBuilder.Append(@" \frac{ "); 97 if (node.SubTrees.Count == 1) { 98 strBuilder.Append(@" \cfrac{1}{"); 99 } else { 100 strBuilder.Append(@" \cfrac{ "); 101 } 94 102 } else if (node.Symbol is Average) { 95 strBuilder.Append(@" \frac{1}{" + node.SubTrees.Count + @"} \left("); 103 // skip output of (1/1) if only one subtree 104 if (node.SubTrees.Count > 1) { 105 strBuilder.Append(@" \cfrac{1}{" + node.SubTrees.Count + @"}"); 106 } 107 strBuilder.Append(@" \left("); 96 108 } else if (node.Symbol is Logarithm) { 97 109 strBuilder.Append(@"\log \left("); … … 113 125 strBuilder.Append(@" \left( \left( "); 114 126 } else if (node.Symbol is Not) { 115 strBuilder.Append(@" -1.0 \ times\left( ");127 strBuilder.Append(@" -1.0 \cdot \left( "); 116 128 } else if (node.Symbol is IfThenElse) { 117 129 strBuilder.Append(@"\left( \operatorname{if} \left( 0 < "); 118 130 } else if (node.Symbol is Constant) { 119 strBuilder.Append("c_{" + constants.Count +"} ");131 strBuilder.Append("c_{" + constants.Count + "} "); 120 132 var constNode = node as ConstantTreeNode; 121 133 constants.Add(constNode.Value); 134 } else if (node.Symbol is LaggedVariable) { 135 var laggedVarNode = node as LaggedVariableTreeNode; 136 strBuilder.Append("c_{" + constants.Count + "} " + laggedVarNode.VariableName); 137 strBuilder.Append(LagToString(currentLag + laggedVarNode.Lag)); 138 constants.Add(laggedVarNode.Weight); 122 139 } else if (node.Symbol is HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.Variable) { 123 140 var varNode = node as VariableTreeNode; 124 strBuilder.Append("c_{" + constants.Count + "} " + varNode.VariableName + " "); 141 strBuilder.Append("c_{" + constants.Count + "} " + varNode.VariableName); 142 strBuilder.Append(LagToString(currentLag)); 125 143 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);130 144 } else if (node.Symbol is ProgramRootSymbol) { 131 145 } else if (node.Symbol is Defun) { … … 140 154 var argSym = node.Symbol as Argument; 141 155 strBuilder.Append(" ARG+" + argSym.ArgumentIndex + " "); 156 } else if (node.Symbol is Derivative) { 157 strBuilder.Append(@" \cfrac{d \left("); 158 } else if (node.Symbol is TimeLag) { 159 var laggedNode = node as ILaggedTreeNode; 160 currentLag += laggedNode.Lag; 161 } else if (node.Symbol is Power) { 162 strBuilder.Append(@"\left("); 163 } else if (node.Symbol is Root) { 164 strBuilder.Append(@"\left("); 165 } else if (node.Symbol is Integral) { 166 // actually a new variable for t is needed in all subtrees (TODO) 167 var laggedTreeNode = node as ILaggedTreeNode; 168 strBuilder.Append(@"\sum_{t=" + (laggedTreeNode.Lag + currentLag) + @"}^0 \left("); 142 169 } else { 143 170 throw new NotImplementedException("Export of " + node.Symbol + " is not implemented."); … … 151 178 strBuilder.Append(" - "); 152 179 } else if (node.Symbol is Multiplication) { 153 strBuilder.Append(@" \ times");180 strBuilder.Append(@" \cdot "); 154 181 } else if (node.Symbol is Division) { 155 strBuilder.Append( " }{ ");182 strBuilder.Append(@" }{ \cfrac{ "); 156 183 } else if (node.Symbol is Average) { 157 184 strBuilder.Append(@" + "); … … 185 212 } else if (node.Symbol is StartSymbol) { 186 213 strBuilder.Append(@"\\" + Environment.NewLine + " & "); 214 } else if (node.Symbol is Power) { 215 strBuilder.Append(@"\right) ^ { \operatorname{round} \left("); 216 } else if (node.Symbol is Root) { 217 strBuilder.Append(@"\right) ^ { \left( \cfrac{1}{ \operatorname{round} \left("); 187 218 } else { 188 219 throw new NotImplementedException("Export of " + node.Symbol + " is not implemented."); … … 197 228 } else if (node.Symbol is Multiplication) { 198 229 } else if (node.Symbol is Division) { 199 strBuilder.Append(" } "); 230 strBuilder.Append("} "); 231 if (node.SubTrees.Count > 1) 232 strBuilder.Append("{1} "); 233 for (int i = 1; i < node.SubTrees.Count; i++) { 234 strBuilder.Append(" } "); 235 } 200 236 } else if (node.Symbol is Average) { 201 237 strBuilder.Append(@" \right)"); … … 223 259 strBuilder.Append(@" \right) \right) "); 224 260 } else if (node.Symbol is Constant) { 261 } else if (node.Symbol is LaggedVariable) { 225 262 } else if (node.Symbol is HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.Variable) { 226 } else if (node.Symbol is LaggedVariable) {227 263 } else if (node.Symbol is ProgramRootSymbol) { 228 264 // output all constant values … … 240 276 } else if (node.Symbol is StartSymbol) { 241 277 } else if (node.Symbol is Argument) { 278 } else if (node.Symbol is Derivative) { 279 strBuilder.Append(@" \right) }{dt} "); 280 } else if (node.Symbol is TimeLag) { 281 var laggedNode = node as ILaggedTreeNode; 282 currentLag -= laggedNode.Lag; 283 } else if (node.Symbol is Power) { 284 strBuilder.Append(@"\right) } "); 285 } else if (node.Symbol is Root) { 286 strBuilder.Append(@"\right) } \right) } "); 287 } else if (node.Symbol is Integral) { 288 var laggedTreeNode = node as ILaggedTreeNode; 289 strBuilder.Append(@"\right) "); 242 290 } else { 243 291 throw new NotImplementedException("Export of " + node.Symbol + " is not implemented."); 244 292 } 245 293 } 294 private string LagToString(int lag) { 295 if (lag < 0) { 296 return "(t" + lag + ")"; 297 } else if (lag > 0) { 298 return "(t+" + lag + ")"; 299 } else return "(t)"; 300 } 246 301 } 247 302 }
Note: See TracChangeset
for help on using the changeset viewer.