Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/04/11 11:34:59 (14 years ago)
Author:
gkronber
Message:

#1152 Merged LaTeX formatter from trunk and implemented formatting rules for new symbols (related to dynamic models).

Location:
trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Symbolic/Formatters
Files:
1 added
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Symbolic/Formatters/SymbolicExpressionTreeLatexFormatter.cs

    r5188 r5428  
    3535  public sealed class SymbolicExpressionTreeLatexFormatter : NamedItem, ISymbolicExpressionTreeStringFormatter {
    3636    private List<double> constants;
     37    private int currentLag;
    3738
    3839    public SymbolicExpressionTreeLatexFormatter()
     
    6667    private string FormatRecursively(SymbolicExpressionTreeNode node) {
    6768      StringBuilder strBuilder = new StringBuilder();
    68 
     69      currentLag = 0;
    6970      FormatBegin(node, strBuilder);
    7071
     
    8788        strBuilder.Append(@" \left( ");
    8889      } 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        }
    9095      } else if (node.Symbol is Multiplication) {
    9196      } 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        }
    94102      } 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(");
    96108      } else if (node.Symbol is Logarithm) {
    97109        strBuilder.Append(@"\log \left(");
     
    113125        strBuilder.Append(@" \left( \left( ");
    114126      } else if (node.Symbol is Not) {
    115         strBuilder.Append(@" -1.0 \times \left( ");
     127        strBuilder.Append(@" -1.0 \cdot \left( ");
    116128      } else if (node.Symbol is IfThenElse) {
    117129        strBuilder.Append(@"\left( \operatorname{if} \left( 0 < ");
    118130      } else if (node.Symbol is Constant) {
    119         strBuilder.Append("c_{" + constants.Count+"} ");
     131        strBuilder.Append("c_{" + constants.Count + "} ");
    120132        var constNode = node as ConstantTreeNode;
    121133        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);
    122139      } else if (node.Symbol is HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.Variable) {
    123140        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));
    125143        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);
    130144      } else if (node.Symbol is ProgramRootSymbol) {
    131145      } else if (node.Symbol is Defun) {
     
    140154        var argSym = node.Symbol as Argument;
    141155        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(");
    142169      } else {
    143170        throw new NotImplementedException("Export of " + node.Symbol + " is not implemented.");
     
    151178        strBuilder.Append(" - ");
    152179      } else if (node.Symbol is Multiplication) {
    153         strBuilder.Append(@" \times ");
     180        strBuilder.Append(@" \cdot ");
    154181      } else if (node.Symbol is Division) {
    155         strBuilder.Append(" }{ ");
     182        strBuilder.Append(@" }{ \cfrac{ ");
    156183      } else if (node.Symbol is Average) {
    157184        strBuilder.Append(@" + ");
     
    185212      } else if (node.Symbol is StartSymbol) {
    186213        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(");
    187218      } else {
    188219        throw new NotImplementedException("Export of " + node.Symbol + " is not implemented.");
     
    197228      } else if (node.Symbol is Multiplication) {
    198229      } 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        }
    200236      } else if (node.Symbol is Average) {
    201237        strBuilder.Append(@" \right)");
     
    223259        strBuilder.Append(@" \right) \right) ");
    224260      } else if (node.Symbol is Constant) {
     261      } else if (node.Symbol is LaggedVariable) {
    225262      } else if (node.Symbol is HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.Variable) {
    226       } else if (node.Symbol is LaggedVariable) {
    227263      } else if (node.Symbol is ProgramRootSymbol) {
    228264        // output all constant values
     
    240276      } else if (node.Symbol is StartSymbol) {
    241277      } 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) ");
    242290      } else {
    243291        throw new NotImplementedException("Export of " + node.Symbol + " is not implemented.");
    244292      }
    245293    }
     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    }
    246301  }
    247302}
Note: See TracChangeset for help on using the changeset viewer.