Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/22/11 14:23:31 (13 years ago)
Author:
mkommend
Message:

#1686: Improved LatexFormatter for SymbolicExpressions.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionLatexFormatter.cs

    r6975 r7038  
    5858        constants.Clear();
    5959        strBuilder.AppendLine("% needs \\usepackage{amsmath}");
    60         strBuilder.AppendLine("\\begin{align}");
     60        strBuilder.AppendLine("\\begin{align*}");
     61        strBuilder.AppendLine("\\nonumber");
    6162        strBuilder.AppendLine(FormatRecursively(symbolicExpressionTree.Root));
    62         strBuilder.AppendLine("\\end{align}");
     63        strBuilder.AppendLine("\\end{align*}");
    6364        return strBuilder.ToString();
    6465      }
     
    8990    private void FormatBegin(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) {
    9091      if (node.Symbol is Addition) {
    91         strBuilder.Append(@" \left( ");
     92        strBuilder.Append(@" ");
    9293      } else if (node.Symbol is Subtraction) {
    9394        if (node.SubtreeCount == 1) {
    94           strBuilder.Append(@"- \left(");
     95          strBuilder.Append(@"- ");
    9596        } else {
    96           strBuilder.Append(@" \left( ");
     97          strBuilder.Append(@" ");
    9798        }
    9899      } else if (node.Symbol is Multiplication) {
     
    108109          strBuilder.Append(@" \cfrac{1}{" + node.SubtreeCount + @"}");
    109110        }
    110         strBuilder.Append(@" \left(");
     111        strBuilder.Append(@" ");
    111112      } else if (node.Symbol is Logarithm) {
    112         strBuilder.Append(@"\log \left(");
     113        strBuilder.Append(@"\log ");
    113114      } else if (node.Symbol is Exponential) {
    114         strBuilder.Append(@"\exp \left(");
     115        strBuilder.Append(@"\exp ");
    115116      } else if (node.Symbol is Sine) {
    116         strBuilder.Append(@"\sin \left(");
     117        strBuilder.Append(@"\sin ");
    117118      } else if (node.Symbol is Cosine) {
    118         strBuilder.Append(@"\cos \left(");
     119        strBuilder.Append(@"\cos ");
    119120      } else if (node.Symbol is Tangent) {
    120         strBuilder.Append(@"\tan \left(");
     121        strBuilder.Append(@"\tan ");
    121122      } else if (node.Symbol is GreaterThan) {
    122         strBuilder.Append(@" \left( ");
     123        strBuilder.Append(@" ");
    123124      } else if (node.Symbol is LessThan) {
    124         strBuilder.Append(@" \left( ");
     125        strBuilder.Append(@" ");
    125126      } else if (node.Symbol is And) {
    126         strBuilder.Append(@" \left( \left( ");
     127        strBuilder.Append(@"   ");
    127128      } else if (node.Symbol is Or) {
    128         strBuilder.Append(@" \left( \left( ");
     129        strBuilder.Append(@"   ");
    129130      } else if (node.Symbol is Not) {
    130         strBuilder.Append(@" \neg \left( ");
     131        strBuilder.Append(@" \neg ");
    131132      } else if (node.Symbol is IfThenElse) {
    132         strBuilder.Append(@"\left( \operatorname{if} \left( 0 < ");
     133        strBuilder.Append(@" \operatorname{if} 0 < ");
    133134      } else if (node.Symbol is Constant) {
    134135        strBuilder.Append("c_{" + constants.Count + "} ");
     
    137138      } else if (node.Symbol is LaggedVariable) {
    138139        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));
    140145        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) {
    143148        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));
    145154        strBuilder.Append(LagToString(currentLag));
    146         constants.Add(varNode.Weight);
    147155      } else if (node.Symbol is ProgramRootSymbol) {
    148156      } else if (node.Symbol is Defun) {
     
    151159      } else if (node.Symbol is InvokeFunction) {
    152160        var invokeNode = node as InvokeFunctionTreeNode;
    153         strBuilder.Append(invokeNode.Symbol.FunctionName + @" \left( ");
     161        strBuilder.Append(invokeNode.Symbol.FunctionName + @" ");
    154162      } else if (node.Symbol is StartSymbol) {
    155163        strBuilder.Append("Result & = ");
     
    158166        strBuilder.Append(" ARG+" + argSym.ArgumentIndex + " ");
    159167      } else if (node.Symbol is Derivative) {
    160         strBuilder.Append(@" \cfrac{d \left(");
     168        strBuilder.Append(@" \cfrac{d ");
    161169      } else if (node.Symbol is TimeLag) {
    162170        var laggedNode = node as ILaggedTreeNode;
    163171        currentLag += laggedNode.Lag;
    164172      } else if (node.Symbol is Power) {
    165         strBuilder.Append(@"\left(");
     173        strBuilder.Append(@"");
    166174      } else if (node.Symbol is Root) {
    167         strBuilder.Append(@"\left(");
     175        strBuilder.Append(@"");
    168176      } else if (node.Symbol is Integral) {
    169177        // actually a new variable for t is needed in all subtrees (TODO)
    170178        var laggedTreeNode = node as ILaggedTreeNode;
    171         strBuilder.Append(@"\sum_{t=" + (laggedTreeNode.Lag + currentLag) + @"}^0 \left(");
     179        strBuilder.Append(@"\sum_{t=" + (laggedTreeNode.Lag + currentLag) + @"}^0 ");
    172180      } else if (node.Symbol is VariableCondition) {
    173181        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 + "} ";
    175183        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 + @"}   ";
    177185        constants.Add(conditionTreeNode.Threshold);
    178         strBuilder.Append(@"\left( " + p + @"\cdot ");
     186        strBuilder.Append(@" " + p + @"\cdot ");
    179187      } else {
    180188        throw new NotImplementedException("Export of " + node.Symbol + " is not implemented.");
     
    208216        strBuilder.Append(@" < ");
    209217      } else if (node.Symbol is And) {
    210         strBuilder.Append(@" > 0 \right) \land \left(");
     218        strBuilder.Append(@" > 0  \land ");
    211219      } else if (node.Symbol is Or) {
    212         strBuilder.Append(@" > 0 \right) \lor \left(");
     220        strBuilder.Append(@" > 0  \lor ");
    213221      } else if (node.Symbol is Not) {
    214222        throw new InvalidOperationException();
    215223      } else if (node.Symbol is IfThenElse) {
    216         strBuilder.Append(@" \right) , \left(");
     224        strBuilder.Append(@" , ");
    217225      } else if (node.Symbol is ProgramRootSymbol) {
    218226        strBuilder.Append(@"\\" + Environment.NewLine);
     
    223231        strBuilder.Append(@"\\" + Environment.NewLine + " & ");
    224232      } else if (node.Symbol is Power) {
    225         strBuilder.Append(@"\right) ^ { \operatorname{round} \left(");
     233        strBuilder.Append(@" ^ { \operatorname{round} ");
    226234      } else if (node.Symbol is Root) {
    227         strBuilder.Append(@"\right) ^ { \left( \cfrac{1}{ \operatorname{round} \left(");
     235        strBuilder.Append(@" ^ {  \cfrac{1}{ \operatorname{round} ");
    228236      } else if (node.Symbol is VariableCondition) {
    229237        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 + "} ";
    231239        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 + @"}   ";
    233241        constants.Add(conditionTreeNode.Threshold);
    234         strBuilder.Append(@" + \left( 1 - " + p + @" \right) \cdot ");
     242        strBuilder.Append(@" +  1 - " + p + @" \cdot ");
    235243      } else {
    236244        throw new NotImplementedException("Export of " + node.Symbol + " is not implemented.");
     
    240248    private void FormatEnd(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) {
    241249      if (node.Symbol is Addition) {
    242         strBuilder.Append(@" \right) ");
     250        strBuilder.Append(@" ");
    243251      } else if (node.Symbol is Subtraction) {
    244         strBuilder.Append(@" \right) ");
     252        strBuilder.Append(@" ");
    245253      } else if (node.Symbol is Multiplication) {
    246254      } else if (node.Symbol is Division) {
     
    252260        }
    253261      } else if (node.Symbol is Average) {
    254         strBuilder.Append(@" \right)");
     262        strBuilder.Append(@" ");
    255263      } else if (node.Symbol is Logarithm) {
    256         strBuilder.Append(@" \right) ");
     264        strBuilder.Append(@" ");
    257265      } else if (node.Symbol is Exponential) {
    258         strBuilder.Append(@" \right) ");
     266        strBuilder.Append(@" ");
    259267      } else if (node.Symbol is Sine) {
    260         strBuilder.Append(@" \right) ");
     268        strBuilder.Append(@" ");
    261269      } else if (node.Symbol is Cosine) {
    262         strBuilder.Append(@" \right) ");
     270        strBuilder.Append(@" ");
    263271      } else if (node.Symbol is Tangent) {
    264         strBuilder.Append(@" \right) ");
     272        strBuilder.Append(@" ");
    265273      } else if (node.Symbol is GreaterThan) {
    266         strBuilder.Append(@" \right) ");
     274        strBuilder.Append(@" ");
    267275      } else if (node.Symbol is LessThan) {
    268         strBuilder.Append(@" \right) ");
     276        strBuilder.Append(@" ");
    269277      } else if (node.Symbol is And) {
    270         strBuilder.Append(@" > 0 \right) \right) ");
     278        strBuilder.Append(@" > 0   ");
    271279      } else if (node.Symbol is Or) {
    272         strBuilder.Append(@" > 0 \right) \right) ");
     280        strBuilder.Append(@" > 0   ");
    273281      } else if (node.Symbol is Not) {
    274         strBuilder.Append(@" \right) ");
     282        strBuilder.Append(@" ");
    275283      } else if (node.Symbol is IfThenElse) {
    276         strBuilder.Append(@" \right) \right) ");
     284        strBuilder.Append(@"   ");
    277285      } else if (node.Symbol is Constant) {
    278286      } else if (node.Symbol is LaggedVariable) {
    279       } else if (node.Symbol is HeuristicLab.Problems.DataAnalysis.Symbolic.Variable) {
     287      } else if (node.Symbol is Variable) {
    280288      } else if (node.Symbol is ProgramRootSymbol) {
    281289        // output all constant values
     
    290298      } else if (node.Symbol is Defun) {
    291299      } else if (node.Symbol is InvokeFunction) {
    292         strBuilder.Append(@" \right) ");
     300        strBuilder.Append(@" ");
    293301      } else if (node.Symbol is StartSymbol) {
    294302      } else if (node.Symbol is Argument) {
    295303      } else if (node.Symbol is Derivative) {
    296         strBuilder.Append(@" \right) }{dt} ");
     304        strBuilder.Append(@" }{dt} ");
    297305      } else if (node.Symbol is TimeLag) {
    298306        var laggedNode = node as ILaggedTreeNode;
    299307        currentLag -= laggedNode.Lag;
    300308      } else if (node.Symbol is Power) {
    301         strBuilder.Append(@"\right) } ");
     309        strBuilder.Append(@" } ");
    302310      } else if (node.Symbol is Root) {
    303         strBuilder.Append(@"\right) } \right) } ");
     311        strBuilder.Append(@" } } ");
    304312      } else if (node.Symbol is Integral) {
    305313        var laggedTreeNode = node as ILaggedTreeNode;
    306         strBuilder.Append(@"\right) ");
     314        strBuilder.Append(@" ");
    307315      } else if (node.Symbol is VariableCondition) {
    308316        strBuilder.Append(@"\left) ");
     
    317325      } else if (lag > 0) {
    318326        return "(t+" + lag + ")";
    319       } else return "(t)";
     327      } else return "";
    320328    }
    321329
Note: See TracChangeset for help on using the changeset viewer.