- Timestamp:
- 02/03/12 17:59:51 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionLatexFormatter.cs
r7259 r7446 57 57 StringBuilder strBuilder = new StringBuilder(); 58 58 constants.Clear(); 59 strBuilder.AppendLine("% needs \\usepackage{amsmath}");60 strBuilder.AppendLine("\\begin{align*}");61 strBuilder.AppendLine("\\nonumber");62 59 strBuilder.AppendLine(FormatRecursively(symbolicExpressionTree.Root)); 63 strBuilder.AppendLine("\\end{align*}");64 60 return strBuilder.ToString(); 65 61 } … … 92 88 private void FormatBegin(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) { 93 89 if (node.Symbol is Addition) { 94 strBuilder.Append(@" ( ");90 strBuilder.Append(@" \left( "); 95 91 } else if (node.Symbol is Subtraction) { 96 92 if (node.SubtreeCount == 1) { 97 strBuilder.Append(@"- ( ");93 strBuilder.Append(@"- \left( "); 98 94 } else { 99 strBuilder.Append(@" ( ");95 strBuilder.Append(@" \left( "); 100 96 } 101 97 } else if (node.Symbol is Multiplication) { … … 111 107 strBuilder.Append(@" \cfrac{1}{" + node.SubtreeCount + @"}"); 112 108 } 113 strBuilder.Append(@" ( ");109 strBuilder.Append(@" \left( "); 114 110 } else if (node.Symbol is Logarithm) { 115 strBuilder.Append(@"\log ( ");111 strBuilder.Append(@"\log \left( "); 116 112 } else if (node.Symbol is Exponential) { 117 strBuilder.Append(@"\exp ( ");113 strBuilder.Append(@"\exp \left( "); 118 114 } else if (node.Symbol is Sine) { 119 strBuilder.Append(@"\sin ( ");115 strBuilder.Append(@"\sin \left( "); 120 116 } else if (node.Symbol is Cosine) { 121 strBuilder.Append(@"\cos ( ");117 strBuilder.Append(@"\cos \left( "); 122 118 } else if (node.Symbol is Tangent) { 123 strBuilder.Append(@"\tan ( ");119 strBuilder.Append(@"\tan \left( "); 124 120 } else if (node.Symbol is GreaterThan) { 125 strBuilder.Append(@" ( ");121 strBuilder.Append(@" \left( "); 126 122 } else if (node.Symbol is LessThan) { 127 strBuilder.Append(@" ( ");123 strBuilder.Append(@" \left( "); 128 124 } else if (node.Symbol is And) { 129 strBuilder.Append(@" ( ");125 strBuilder.Append(@" \left( "); 130 126 } else if (node.Symbol is Or) { 131 strBuilder.Append(@" ( ");127 strBuilder.Append(@" \left( "); 132 128 } else if (node.Symbol is Not) { 133 strBuilder.Append(@" \neg ( ");129 strBuilder.Append(@" \neg \left( "); 134 130 } else if (node.Symbol is IfThenElse) { 135 strBuilder.Append(@" \operatorname{if} ( 0 <");131 strBuilder.Append(@" \operatorname{if} \left( "); 136 132 } else if (node.Symbol is Constant) { 137 133 strBuilder.Append("c_{" + constants.Count + "} "); … … 156 152 strBuilder.Append(LagToString(currentLag)); 157 153 } else if (node.Symbol is ProgramRootSymbol) { 154 strBuilder 155 .AppendLine("\\begin{align*}") 156 .AppendLine("\\nonumber"); 158 157 } else if (node.Symbol is Defun) { 159 158 var defunNode = node as DefunTreeNode; … … 161 160 } else if (node.Symbol is InvokeFunction) { 162 161 var invokeNode = node as InvokeFunctionTreeNode; 163 strBuilder.Append(invokeNode.Symbol.FunctionName + @" ( ");162 strBuilder.Append(invokeNode.Symbol.FunctionName + @" \left( "); 164 163 } else if (node.Symbol is StartSymbol) { 165 164 strBuilder.Append("Result & = "); … … 168 167 strBuilder.Append(" ARG+" + argSym.ArgumentIndex + " "); 169 168 } else if (node.Symbol is Derivative) { 170 strBuilder.Append(@" \cfrac{d ( ");169 strBuilder.Append(@" \cfrac{d \left( "); 171 170 } else if (node.Symbol is TimeLag) { 172 171 var laggedNode = node as ILaggedTreeNode; 173 172 currentLag += laggedNode.Lag; 174 173 } else if (node.Symbol is Power) { 175 strBuilder.Append(@" ( ");174 strBuilder.Append(@" \left( "); 176 175 } else if (node.Symbol is Root) { 177 strBuilder.Append(@" ( ");176 strBuilder.Append(@" \left( "); 178 177 } else if (node.Symbol is Integral) { 179 178 // actually a new variable for t is needed in all subtrees (TODO) 180 179 var laggedTreeNode = node as ILaggedTreeNode; 181 strBuilder.Append(@"\sum_{t=" + (laggedTreeNode.Lag + currentLag) + @"}^0 ( ");180 strBuilder.Append(@"\sum_{t=" + (laggedTreeNode.Lag + currentLag) + @"}^0 \left( "); 182 181 } else if (node.Symbol is VariableCondition) { 183 182 var conditionTreeNode = node as VariableConditionTreeNode; … … 186 185 p += @" \cdot " + EscapeLatexString(conditionTreeNode.VariableName) + LagToString(currentLag) + " - c_{" + constants.Count + @"} "; 187 186 constants.Add(conditionTreeNode.Threshold); 188 strBuilder.Append(@" ( " + p + @"\cdot ");187 strBuilder.Append(@" \left( " + p + @"\cdot "); 189 188 } else { 190 189 throw new NotImplementedException("Export of " + node.Symbol + " is not implemented."); … … 221 220 strBuilder.Append(@" < "); 222 221 } else if (node.Symbol is And) { 223 strBuilder.Append(@" > 0 ) \land(");222 strBuilder.Append(@" > 0 \right) \land \left("); 224 223 } else if (node.Symbol is Or) { 225 strBuilder.Append(@" > 0 ) \lor(");224 strBuilder.Append(@" > 0 \right) \lor \left("); 226 225 } else if (node.Symbol is Not) { 227 226 throw new InvalidOperationException(); 228 227 } else if (node.Symbol is IfThenElse) { 229 strBuilder.Append(@" ) , (");228 strBuilder.Append(@" , "); 230 229 } else if (node.Symbol is ProgramRootSymbol) { 231 230 strBuilder.Append(@"\\" + Environment.NewLine); … … 236 235 strBuilder.Append(@"\\" + Environment.NewLine + " & "); 237 236 } else if (node.Symbol is Power) { 238 strBuilder.Append(@" ) ^ { \operatorname{round}(");237 strBuilder.Append(@"\right) ^ { \operatorname{round} \left("); 239 238 } else if (node.Symbol is Root) { 240 strBuilder.Append(@" ) ^ { \cfrac{1}{ \operatorname{round}(");239 strBuilder.Append(@"\right) ^ { \cfrac{1}{ \operatorname{round} \left("); 241 240 } else if (node.Symbol is VariableCondition) { 242 241 var conditionTreeNode = node as VariableConditionTreeNode; 243 string p = @"1 / ( 1 + \exp( - c_{" + constants.Count + "} ";242 string p = @"1 / \left( 1 + \exp \left( - c_{" + constants.Count + "} "; 244 243 constants.Add(conditionTreeNode.Slope); 245 p += @" \cdot " + EscapeLatexString(conditionTreeNode.VariableName) + LagToString(currentLag) + " - c_{" + constants.Count + @"} ) )) ";244 p += @" \cdot " + EscapeLatexString(conditionTreeNode.VariableName) + LagToString(currentLag) + " - c_{" + constants.Count + @"} \right) \right) \right) "; 246 245 constants.Add(conditionTreeNode.Threshold); 247 strBuilder.Append(@" + ( 1 - " + p + @") \cdot ");246 strBuilder.Append(@" + \left( 1 - " + p + @" \right) \cdot "); 248 247 } else { 249 248 throw new NotImplementedException("Export of " + node.Symbol + " is not implemented."); … … 253 252 private void FormatEnd(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) { 254 253 if (node.Symbol is Addition) { 255 strBuilder.Append(@" ) ");254 strBuilder.Append(@" \right) "); 256 255 } else if (node.Symbol is Subtraction) { 257 strBuilder.Append(@" ) ");256 strBuilder.Append(@" \right) "); 258 257 } else if (node.Symbol is Multiplication) { 259 258 } else if (node.Symbol is Division) { … … 262 261 strBuilder.Append(" } "); 263 262 } else if (node.Symbol is Average) { 264 strBuilder.Append(@" ) ");263 strBuilder.Append(@" \right) "); 265 264 } else if (node.Symbol is Logarithm) { 266 strBuilder.Append(@" ) ");265 strBuilder.Append(@" \right) "); 267 266 } else if (node.Symbol is Exponential) { 268 strBuilder.Append(@" ) ");267 strBuilder.Append(@" \right) "); 269 268 } else if (node.Symbol is Sine) { 270 strBuilder.Append(@" ) ");269 strBuilder.Append(@" \right) "); 271 270 } else if (node.Symbol is Cosine) { 272 strBuilder.Append(@" ) ");271 strBuilder.Append(@" \right) "); 273 272 } else if (node.Symbol is Tangent) { 274 strBuilder.Append(@" ) ");273 strBuilder.Append(@" \right) "); 275 274 } else if (node.Symbol is GreaterThan) { 276 strBuilder.Append(@" ) ");275 strBuilder.Append(@" \right) "); 277 276 } else if (node.Symbol is LessThan) { 278 strBuilder.Append(@" ) ");277 strBuilder.Append(@" \right) "); 279 278 } else if (node.Symbol is And) { 280 strBuilder.Append(@" > 0 )) ");279 strBuilder.Append(@" > 0 \right) \right) "); 281 280 } else if (node.Symbol is Or) { 282 strBuilder.Append(@" > 0 )) ");281 strBuilder.Append(@" > 0 \right) \right) "); 283 282 } else if (node.Symbol is Not) { 284 strBuilder.Append(@" ) ");283 strBuilder.Append(@" \right) "); 285 284 } else if (node.Symbol is IfThenElse) { 286 strBuilder.Append(@" )) ");285 strBuilder.Append(@" \right) "); 287 286 } else if (node.Symbol is Constant) { 288 287 } else if (node.Symbol is LaggedVariable) { 289 288 } else if (node.Symbol is Variable) { 290 289 } else if (node.Symbol is ProgramRootSymbol) { 290 strBuilder 291 .AppendLine("\\end{align*}") 292 .AppendLine("\\begin{align*}") 293 .AppendLine("\\nonumber"); 291 294 // output all constant values 292 295 if (constants.Count > 0) { 293 296 int i = 0; 294 297 foreach (var constant in constants) { 295 strBuilder.AppendLine(@"\\"); 296 strBuilder.Append("c_{" + i + "} & = " + constant); 298 // replace "." with ".&" to align decimal points 299 var constStr = string.Format(System.Globalization.NumberFormatInfo.InvariantInfo, "{0:G5}", constant); 300 if(!constStr.Contains(".")) constStr = constStr + ".0"; 301 constStr = constStr.Replace(".", "\\negthickspace&."); // fix problem in rendering of aligned expressions 302 strBuilder.Append("c_{" + i + "}& = & " + constStr); 303 strBuilder.Append(@"\\"); 297 304 i++; 298 305 } 299 306 } 307 strBuilder.AppendLine("\\end{align*}"); 300 308 } else if (node.Symbol is Defun) { 301 309 } else if (node.Symbol is InvokeFunction) { 302 strBuilder.Append(@" ) ");310 strBuilder.Append(@" \right) "); 303 311 } else if (node.Symbol is StartSymbol) { 304 312 } else if (node.Symbol is Argument) { 305 313 } else if (node.Symbol is Derivative) { 306 strBuilder.Append(@" ) }{dt} ");314 strBuilder.Append(@" \right) }{dt} "); 307 315 } else if (node.Symbol is TimeLag) { 308 316 var laggedNode = node as ILaggedTreeNode; 309 317 currentLag -= laggedNode.Lag; 310 318 } else if (node.Symbol is Power) { 311 strBuilder.Append(@" ) } ");319 strBuilder.Append(@" \right) } "); 312 320 } else if (node.Symbol is Root) { 313 strBuilder.Append(@" ) } )} ");321 strBuilder.Append(@" \right) } } "); 314 322 } else if (node.Symbol is Integral) { 315 var laggedTreeNode = node as ILaggedTreeNode; 316 strBuilder.Append(@" ) "); 323 strBuilder.Append(@" \right) "); 317 324 } else if (node.Symbol is VariableCondition) { 318 strBuilder.Append(@"\ left) ");325 strBuilder.Append(@"\right) "); 319 326 } else { 320 327 throw new NotImplementedException("Export of " + node.Symbol + " is not implemented."); … … 331 338 332 339 private string EscapeLatexString(string s) { 333 return s.Replace(@"_", @"\_"); 340 return "\\text{" + 341 s 342 .Replace("\\", "\\\\") 343 .Replace("{", "\\{") 344 .Replace("}", "\\}") 345 .Replace("%", "\\%") 346 + "}"; 334 347 } 335 348 }
Note: See TracChangeset
for help on using the changeset viewer.