Changeset 7038
- Timestamp:
- 11/22/11 14:23:31 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionLatexFormatter.cs
r6975 r7038 58 58 constants.Clear(); 59 59 strBuilder.AppendLine("% needs \\usepackage{amsmath}"); 60 strBuilder.AppendLine("\\begin{align}"); 60 strBuilder.AppendLine("\\begin{align*}"); 61 strBuilder.AppendLine("\\nonumber"); 61 62 strBuilder.AppendLine(FormatRecursively(symbolicExpressionTree.Root)); 62 strBuilder.AppendLine("\\end{align }");63 strBuilder.AppendLine("\\end{align*}"); 63 64 return strBuilder.ToString(); 64 65 } … … 89 90 private void FormatBegin(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) { 90 91 if (node.Symbol is Addition) { 91 strBuilder.Append(@" \left(");92 strBuilder.Append(@" "); 92 93 } else if (node.Symbol is Subtraction) { 93 94 if (node.SubtreeCount == 1) { 94 strBuilder.Append(@"- \left(");95 strBuilder.Append(@"- "); 95 96 } else { 96 strBuilder.Append(@" \left(");97 strBuilder.Append(@" "); 97 98 } 98 99 } else if (node.Symbol is Multiplication) { … … 108 109 strBuilder.Append(@" \cfrac{1}{" + node.SubtreeCount + @"}"); 109 110 } 110 strBuilder.Append(@" \left(");111 strBuilder.Append(@" "); 111 112 } else if (node.Symbol is Logarithm) { 112 strBuilder.Append(@"\log \left(");113 strBuilder.Append(@"\log "); 113 114 } else if (node.Symbol is Exponential) { 114 strBuilder.Append(@"\exp \left(");115 strBuilder.Append(@"\exp "); 115 116 } else if (node.Symbol is Sine) { 116 strBuilder.Append(@"\sin \left(");117 strBuilder.Append(@"\sin "); 117 118 } else if (node.Symbol is Cosine) { 118 strBuilder.Append(@"\cos \left(");119 strBuilder.Append(@"\cos "); 119 120 } else if (node.Symbol is Tangent) { 120 strBuilder.Append(@"\tan \left(");121 strBuilder.Append(@"\tan "); 121 122 } else if (node.Symbol is GreaterThan) { 122 strBuilder.Append(@" \left(");123 strBuilder.Append(@" "); 123 124 } else if (node.Symbol is LessThan) { 124 strBuilder.Append(@" \left(");125 strBuilder.Append(@" "); 125 126 } else if (node.Symbol is And) { 126 strBuilder.Append(@" \left( \left(");127 strBuilder.Append(@" "); 127 128 } else if (node.Symbol is Or) { 128 strBuilder.Append(@" \left( \left(");129 strBuilder.Append(@" "); 129 130 } else if (node.Symbol is Not) { 130 strBuilder.Append(@" \neg \left(");131 strBuilder.Append(@" \neg "); 131 132 } else if (node.Symbol is IfThenElse) { 132 strBuilder.Append(@" \left( \operatorname{if} \left(0 < ");133 strBuilder.Append(@" \operatorname{if} 0 < "); 133 134 } else if (node.Symbol is Constant) { 134 135 strBuilder.Append("c_{" + constants.Count + "} "); … … 137 138 } else if (node.Symbol is LaggedVariable) { 138 139 var laggedVarNode = node as LaggedVariableTreeNode; 139 strBuilder.Append("c_{" + constants.Count + "} " + EscapeLatexString(laggedVarNode.VariableName)); 140 if (!laggedVarNode.Weight.IsAlmost(1.0)) { 141 strBuilder.Append("c_{" + constants.Count + "} \\cdot "); 142 constants.Add(laggedVarNode.Weight); 143 } 144 strBuilder.Append(EscapeLatexString(laggedVarNode.VariableName)); 140 145 strBuilder.Append(LagToString(currentLag + laggedVarNode.Lag)); 141 constants.Add(laggedVarNode.Weight); 142 } else if (node.Symbol is HeuristicLab.Problems.DataAnalysis.Symbolic.Variable) {146 147 } else if (node.Symbol is Variable) { 143 148 var varNode = node as VariableTreeNode; 144 strBuilder.Append("c_{" + constants.Count + "} " + EscapeLatexString(varNode.VariableName)); 149 if (!varNode.Weight.IsAlmost((1.0))) { 150 strBuilder.Append("c_{" + constants.Count + "} \\cdot "); 151 constants.Add(varNode.Weight); 152 } 153 strBuilder.Append(EscapeLatexString(varNode.VariableName)); 145 154 strBuilder.Append(LagToString(currentLag)); 146 constants.Add(varNode.Weight);147 155 } else if (node.Symbol is ProgramRootSymbol) { 148 156 } else if (node.Symbol is Defun) { … … 151 159 } else if (node.Symbol is InvokeFunction) { 152 160 var invokeNode = node as InvokeFunctionTreeNode; 153 strBuilder.Append(invokeNode.Symbol.FunctionName + @" \left(");161 strBuilder.Append(invokeNode.Symbol.FunctionName + @" "); 154 162 } else if (node.Symbol is StartSymbol) { 155 163 strBuilder.Append("Result & = "); … … 158 166 strBuilder.Append(" ARG+" + argSym.ArgumentIndex + " "); 159 167 } else if (node.Symbol is Derivative) { 160 strBuilder.Append(@" \cfrac{d \left(");168 strBuilder.Append(@" \cfrac{d "); 161 169 } else if (node.Symbol is TimeLag) { 162 170 var laggedNode = node as ILaggedTreeNode; 163 171 currentLag += laggedNode.Lag; 164 172 } else if (node.Symbol is Power) { 165 strBuilder.Append(@" \left(");173 strBuilder.Append(@""); 166 174 } else if (node.Symbol is Root) { 167 strBuilder.Append(@" \left(");175 strBuilder.Append(@""); 168 176 } else if (node.Symbol is Integral) { 169 177 // actually a new variable for t is needed in all subtrees (TODO) 170 178 var laggedTreeNode = node as ILaggedTreeNode; 171 strBuilder.Append(@"\sum_{t=" + (laggedTreeNode.Lag + currentLag) + @"}^0 \left(");179 strBuilder.Append(@"\sum_{t=" + (laggedTreeNode.Lag + currentLag) + @"}^0 "); 172 180 } else if (node.Symbol is VariableCondition) { 173 181 var conditionTreeNode = node as VariableConditionTreeNode; 174 string p = @"1 / \left( 1 + \exp \left(- c_{" + constants.Count + "} ";182 string p = @"1 / 1 + \exp - c_{" + constants.Count + "} "; 175 183 constants.Add(conditionTreeNode.Slope); 176 p += @" \cdot \left(" + EscapeLatexString(conditionTreeNode.VariableName) + LagToString(currentLag) + " - c_{" + constants.Count + @"} \right) \right) \right)";184 p += @" \cdot " + EscapeLatexString(conditionTreeNode.VariableName) + LagToString(currentLag) + " - c_{" + constants.Count + @"} "; 177 185 constants.Add(conditionTreeNode.Threshold); 178 strBuilder.Append(@" \left(" + p + @"\cdot ");186 strBuilder.Append(@" " + p + @"\cdot "); 179 187 } else { 180 188 throw new NotImplementedException("Export of " + node.Symbol + " is not implemented."); … … 208 216 strBuilder.Append(@" < "); 209 217 } else if (node.Symbol is And) { 210 strBuilder.Append(@" > 0 \right) \land \left(");218 strBuilder.Append(@" > 0 \land "); 211 219 } else if (node.Symbol is Or) { 212 strBuilder.Append(@" > 0 \right) \lor \left(");220 strBuilder.Append(@" > 0 \lor "); 213 221 } else if (node.Symbol is Not) { 214 222 throw new InvalidOperationException(); 215 223 } else if (node.Symbol is IfThenElse) { 216 strBuilder.Append(@" \right) , \left(");224 strBuilder.Append(@" , "); 217 225 } else if (node.Symbol is ProgramRootSymbol) { 218 226 strBuilder.Append(@"\\" + Environment.NewLine); … … 223 231 strBuilder.Append(@"\\" + Environment.NewLine + " & "); 224 232 } else if (node.Symbol is Power) { 225 strBuilder.Append(@" \right) ^ { \operatorname{round} \left(");233 strBuilder.Append(@" ^ { \operatorname{round} "); 226 234 } else if (node.Symbol is Root) { 227 strBuilder.Append(@" \right) ^ { \left( \cfrac{1}{ \operatorname{round} \left(");235 strBuilder.Append(@" ^ { \cfrac{1}{ \operatorname{round} "); 228 236 } else if (node.Symbol is VariableCondition) { 229 237 var conditionTreeNode = node as VariableConditionTreeNode; 230 string p = @"1 / \left( 1 + \exp \left(- c_{" + constants.Count + "} ";238 string p = @"1 / 1 + \exp - c_{" + constants.Count + "} "; 231 239 constants.Add(conditionTreeNode.Slope); 232 p += @" \cdot \left(" + EscapeLatexString(conditionTreeNode.VariableName) + LagToString(currentLag) + " - c_{" + constants.Count + @"} \right) \right) \right)";240 p += @" \cdot " + EscapeLatexString(conditionTreeNode.VariableName) + LagToString(currentLag) + " - c_{" + constants.Count + @"} "; 233 241 constants.Add(conditionTreeNode.Threshold); 234 strBuilder.Append(@" + \left( 1 - " + p + @" \right)\cdot ");242 strBuilder.Append(@" + 1 - " + p + @" \cdot "); 235 243 } else { 236 244 throw new NotImplementedException("Export of " + node.Symbol + " is not implemented."); … … 240 248 private void FormatEnd(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) { 241 249 if (node.Symbol is Addition) { 242 strBuilder.Append(@" \right)");250 strBuilder.Append(@" "); 243 251 } else if (node.Symbol is Subtraction) { 244 strBuilder.Append(@" \right)");252 strBuilder.Append(@" "); 245 253 } else if (node.Symbol is Multiplication) { 246 254 } else if (node.Symbol is Division) { … … 252 260 } 253 261 } else if (node.Symbol is Average) { 254 strBuilder.Append(@" \right)");262 strBuilder.Append(@" "); 255 263 } else if (node.Symbol is Logarithm) { 256 strBuilder.Append(@" \right)");264 strBuilder.Append(@" "); 257 265 } else if (node.Symbol is Exponential) { 258 strBuilder.Append(@" \right)");266 strBuilder.Append(@" "); 259 267 } else if (node.Symbol is Sine) { 260 strBuilder.Append(@" \right)");268 strBuilder.Append(@" "); 261 269 } else if (node.Symbol is Cosine) { 262 strBuilder.Append(@" \right)");270 strBuilder.Append(@" "); 263 271 } else if (node.Symbol is Tangent) { 264 strBuilder.Append(@" \right)");272 strBuilder.Append(@" "); 265 273 } else if (node.Symbol is GreaterThan) { 266 strBuilder.Append(@" \right)");274 strBuilder.Append(@" "); 267 275 } else if (node.Symbol is LessThan) { 268 strBuilder.Append(@" \right)");276 strBuilder.Append(@" "); 269 277 } else if (node.Symbol is And) { 270 strBuilder.Append(@" > 0 \right) \right)");278 strBuilder.Append(@" > 0 "); 271 279 } else if (node.Symbol is Or) { 272 strBuilder.Append(@" > 0 \right) \right)");280 strBuilder.Append(@" > 0 "); 273 281 } else if (node.Symbol is Not) { 274 strBuilder.Append(@" \right)");282 strBuilder.Append(@" "); 275 283 } else if (node.Symbol is IfThenElse) { 276 strBuilder.Append(@" \right) \right)");284 strBuilder.Append(@" "); 277 285 } else if (node.Symbol is Constant) { 278 286 } else if (node.Symbol is LaggedVariable) { 279 } else if (node.Symbol is HeuristicLab.Problems.DataAnalysis.Symbolic.Variable) {287 } else if (node.Symbol is Variable) { 280 288 } else if (node.Symbol is ProgramRootSymbol) { 281 289 // output all constant values … … 290 298 } else if (node.Symbol is Defun) { 291 299 } else if (node.Symbol is InvokeFunction) { 292 strBuilder.Append(@" \right)");300 strBuilder.Append(@" "); 293 301 } else if (node.Symbol is StartSymbol) { 294 302 } else if (node.Symbol is Argument) { 295 303 } else if (node.Symbol is Derivative) { 296 strBuilder.Append(@" \right)}{dt} ");304 strBuilder.Append(@" }{dt} "); 297 305 } else if (node.Symbol is TimeLag) { 298 306 var laggedNode = node as ILaggedTreeNode; 299 307 currentLag -= laggedNode.Lag; 300 308 } else if (node.Symbol is Power) { 301 strBuilder.Append(@" \right)} ");309 strBuilder.Append(@" } "); 302 310 } else if (node.Symbol is Root) { 303 strBuilder.Append(@" \right) } \right)} ");311 strBuilder.Append(@" } } "); 304 312 } else if (node.Symbol is Integral) { 305 313 var laggedTreeNode = node as ILaggedTreeNode; 306 strBuilder.Append(@" \right)");314 strBuilder.Append(@" "); 307 315 } else if (node.Symbol is VariableCondition) { 308 316 strBuilder.Append(@"\left) "); … … 317 325 } else if (lag > 0) { 318 326 return "(t+" + lag + ")"; 319 } else return " (t)";327 } else return ""; 320 328 } 321 329
Note: See TracChangeset
for help on using the changeset viewer.