Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/18/17 14:22:46 (8 years ago)
Author:
gkronber
Message:

#2650 adapted formatters to handle factor symbols

Location:
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionLatexFormatter.cs

    r14399 r14764  
    7272        strBuilder.AppendLine(FormatRecursively(symbolicExpressionTree.Root));
    7373        return strBuilder.ToString();
    74       } catch (NotImplementedException ex) {
     74      } catch(NotImplementedException ex) {
    7575        return ex.Message + Environment.NewLine + ex.StackTrace;
    7676      }
     
    8585      FormatBegin(node, strBuilder);
    8686
    87       if (node.SubtreeCount > 0) {
     87      if(node.SubtreeCount > 0) {
    8888        strBuilder.Append(FormatRecursively(node.GetSubtree(0)));
    8989      }
    9090      int i = 1;
    91       foreach (var subTree in node.Subtrees.Skip(1)) {
     91      foreach(var subTree in node.Subtrees.Skip(1)) {
    9292        FormatSep(node, strBuilder, i);
    9393        // format the whole subtree
     
    102102
    103103    private void FormatBegin(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) {
    104       if (node.Symbol is Addition) {
     104      if(node.Symbol is Addition) {
    105105        strBuilder.Append(@" \left( ");
    106       } else if (node.Symbol is Subtraction) {
    107         if (node.SubtreeCount == 1) {
     106      } else if(node.Symbol is Subtraction) {
     107        if(node.SubtreeCount == 1) {
    108108          strBuilder.Append(@"- \left( ");
    109109        } else {
    110110          strBuilder.Append(@" \left( ");
    111111        }
    112       } else if (node.Symbol is Multiplication) {
    113       } else if (node.Symbol is Division) {
    114         if (node.SubtreeCount == 1) {
     112      } else if(node.Symbol is Multiplication) {
     113      } else if(node.Symbol is Division) {
     114        if(node.SubtreeCount == 1) {
    115115          strBuilder.Append(@" \cfrac{1}{");
    116116        } else {
    117117          strBuilder.Append(@" \cfrac{ ");
    118118        }
    119       } else if (node.Symbol is Average) {
     119      } else if(node.Symbol is Average) {
    120120        // skip output of (1/1) if only one subtree
    121         if (node.SubtreeCount > 1) {
     121        if(node.SubtreeCount > 1) {
    122122          strBuilder.Append(@" \cfrac{1}{" + node.SubtreeCount + @"}");
    123123        }
    124124        strBuilder.Append(@" \left( ");
    125       } else if (node.Symbol is Logarithm) {
     125      } else if(node.Symbol is Logarithm) {
    126126        strBuilder.Append(@"\log \left( ");
    127       } else if (node.Symbol is Exponential) {
     127      } else if(node.Symbol is Exponential) {
    128128        strBuilder.Append(@"\exp \left( ");
    129       } else if (node.Symbol is Square) {
     129      } else if(node.Symbol is Square) {
    130130        strBuilder.Append(@"\left(");
    131       } else if (node.Symbol is SquareRoot) {
     131      } else if(node.Symbol is SquareRoot) {
    132132        strBuilder.Append(@"\sqrt{");
    133       } else if (node.Symbol is Sine) {
     133      } else if(node.Symbol is Sine) {
    134134        strBuilder.Append(@"\sin \left( ");
    135       } else if (node.Symbol is Cosine) {
     135      } else if(node.Symbol is Cosine) {
    136136        strBuilder.Append(@"\cos \left( ");
    137       } else if (node.Symbol is Tangent) {
     137      } else if(node.Symbol is Tangent) {
    138138        strBuilder.Append(@"\tan \left( ");
    139       } else if (node.Symbol is AiryA) {
     139      } else if(node.Symbol is AiryA) {
    140140        strBuilder.Append(@"\operatorname{airy}_a \left( ");
    141       } else if (node.Symbol is AiryB) {
     141      } else if(node.Symbol is AiryB) {
    142142        strBuilder.Append(@"\operatorname{airy}_b \left( ");
    143       } else if (node.Symbol is Bessel) {
     143      } else if(node.Symbol is Bessel) {
    144144        strBuilder.Append(@"\operatorname{bessel}_1 \left( ");
    145       } else if (node.Symbol is CosineIntegral) {
     145      } else if(node.Symbol is CosineIntegral) {
    146146        strBuilder.Append(@"\operatorname{cosInt} \left( ");
    147       } else if (node.Symbol is Dawson) {
     147      } else if(node.Symbol is Dawson) {
    148148        strBuilder.Append(@"\operatorname{dawson} \left( ");
    149       } else if (node.Symbol is Erf) {
     149      } else if(node.Symbol is Erf) {
    150150        strBuilder.Append(@"\operatorname{erf} \left( ");
    151       } else if (node.Symbol is ExponentialIntegralEi) {
     151      } else if(node.Symbol is ExponentialIntegralEi) {
    152152        strBuilder.Append(@"\operatorname{expInt}_i \left( ");
    153       } else if (node.Symbol is FresnelCosineIntegral) {
     153      } else if(node.Symbol is FresnelCosineIntegral) {
    154154        strBuilder.Append(@"\operatorname{fresnel}_\operatorname{cosInt} \left( ");
    155       } else if (node.Symbol is FresnelSineIntegral) {
     155      } else if(node.Symbol is FresnelSineIntegral) {
    156156        strBuilder.Append(@"\operatorname{fresnel}_\operatorname{sinInt} \left( ");
    157       } else if (node.Symbol is Gamma) {
     157      } else if(node.Symbol is Gamma) {
    158158        strBuilder.Append(@"\Gamma \left( ");
    159       } else if (node.Symbol is HyperbolicCosineIntegral) {
     159      } else if(node.Symbol is HyperbolicCosineIntegral) {
    160160        strBuilder.Append(@"\operatorname{hypCosInt} \left( ");
    161       } else if (node.Symbol is HyperbolicSineIntegral) {
     161      } else if(node.Symbol is HyperbolicSineIntegral) {
    162162        strBuilder.Append(@"\operatorname{hypSinInt} \left( ");
    163       } else if (node.Symbol is Norm) {
     163      } else if(node.Symbol is Norm) {
    164164        strBuilder.Append(@"\operatorname{norm} \left( ");
    165       } else if (node.Symbol is Psi) {
     165      } else if(node.Symbol is Psi) {
    166166        strBuilder.Append(@"\operatorname{digamma} \left( ");
    167       } else if (node.Symbol is SineIntegral) {
     167      } else if(node.Symbol is SineIntegral) {
    168168        strBuilder.Append(@"\operatorname{sinInt} \left( ");
    169       } else if (node.Symbol is GreaterThan) {
     169      } else if(node.Symbol is GreaterThan) {
    170170        strBuilder.Append(@"  \left( ");
    171       } else if (node.Symbol is LessThan) {
     171      } else if(node.Symbol is LessThan) {
    172172        strBuilder.Append(@"  \left( ");
    173       } else if (node.Symbol is And) {
     173      } else if(node.Symbol is And) {
    174174        strBuilder.Append(@"  \left( \left( ");
    175       } else if (node.Symbol is Or) {
     175      } else if(node.Symbol is Or) {
    176176        strBuilder.Append(@"   \left( \left( ");
    177       } else if (node.Symbol is Not) {
     177      } else if(node.Symbol is Not) {
    178178        strBuilder.Append(@" \neg \left( ");
    179       } else if (node.Symbol is IfThenElse) {
     179      } else if(node.Symbol is IfThenElse) {
    180180        strBuilder.Append(@" \operatorname{if}  \left( ");
    181       } else if (node.Symbol is Constant) {
     181      } else if(node.Symbol is Constant) {
    182182        var constName = "c_{" + constIndex + "}";
    183183        constIndex++;
    184184        var constNode = node as ConstantTreeNode;
    185         if (constNode.Value.IsAlmost(1.0)) {
     185        if(constNode.Value.IsAlmost(1.0)) {
    186186          strBuilder.Append("1 ");
    187187        } else {
     
    190190        }
    191191
    192       } else if (node.Symbol is FactorVariable) {
     192      } else if(node.Symbol is FactorVariable) {
    193193        var factorNode = node as FactorVariableTreeNode;
    194194        var constName = "c_{" + constIndex + "}";
    195195        strBuilder.Append(constName + " ");
    196         foreach (var e in factorNode.Symbol.GetVariableValues(factorNode.VariableName)
     196        foreach(var e in factorNode.Symbol.GetVariableValues(factorNode.VariableName)
    197197          .Zip(factorNode.Weights, Tuple.Create)) {
    198198          constants.Add(new KeyValuePair<string, double>("c_{" + constIndex + ", " + EscapeLatexString(factorNode.VariableName) + "=" + EscapeLatexString(e.Item1) + "}", e.Item2));
    199199        }
    200200        constIndex++;
    201       } else if (node.Symbol is BinaryFactorVariable) {
     201      } else if(node.Symbol is BinaryFactorVariable) {
    202202        var binFactorNode = node as BinaryFactorVariableTreeNode;
    203         if (!binFactorNode.Weight.IsAlmost((1.0))) {
     203        if(!binFactorNode.Weight.IsAlmost((1.0))) {
    204204          var constName = "c_{" + constIndex + "}";
    205205          strBuilder.Append(constName + "  \\cdot");
     
    207207          constIndex++;
    208208        }
    209         strBuilder.Append(EscapeLatexString(binFactorNode.VariableName));
     209        strBuilder.Append("(" + EscapeLatexString(binFactorNode.VariableName));
    210210        strBuilder.Append(LagToString(currentLag));
    211         strBuilder.Append(" = " + EscapeLatexString(binFactorNode.VariableValue));
    212       } else if (node.Symbol is LaggedVariable) {
     211        strBuilder.Append(" = " + EscapeLatexString(binFactorNode.VariableValue) + " )");
     212      } else if(node.Symbol is LaggedVariable) {
    213213        var laggedVarNode = node as LaggedVariableTreeNode;
    214         if (!laggedVarNode.Weight.IsAlmost(1.0)) {
     214        if(!laggedVarNode.Weight.IsAlmost(1.0)) {
    215215          var constName = "c_{" + constIndex + "}";
    216216          strBuilder.Append(constName + "  \\cdot");
     
    221221        strBuilder.Append(LagToString(currentLag + laggedVarNode.Lag));
    222222
    223       } else if (node.Symbol is Variable) {
     223      } else if(node.Symbol is Variable) {
    224224        var varNode = node as VariableTreeNode;
    225         if (!varNode.Weight.IsAlmost((1.0))) {
     225        if(!varNode.Weight.IsAlmost((1.0))) {
    226226          var constName = "c_{" + constIndex + "}";
    227227          strBuilder.Append(constName + "  \\cdot");
     
    231231        strBuilder.Append(EscapeLatexString(varNode.VariableName));
    232232        strBuilder.Append(LagToString(currentLag));
    233       } else if (node.Symbol is ProgramRootSymbol) {
     233      } else if(node.Symbol is ProgramRootSymbol) {
    234234        strBuilder
    235235          .AppendLine("\\begin{align*}")
    236236          .AppendLine("\\nonumber");
    237       } else if (node.Symbol is Defun) {
     237      } else if(node.Symbol is Defun) {
    238238        var defunNode = node as DefunTreeNode;
    239239        strBuilder.Append(defunNode.FunctionName + " & = ");
    240       } else if (node.Symbol is InvokeFunction) {
     240      } else if(node.Symbol is InvokeFunction) {
    241241        var invokeNode = node as InvokeFunctionTreeNode;
    242242        strBuilder.Append(invokeNode.Symbol.FunctionName + @" \left( ");
    243       } else if (node.Symbol is StartSymbol) {
     243      } else if(node.Symbol is StartSymbol) {
    244244        FormatStartSymbol(strBuilder);
    245       } else if (node.Symbol is Argument) {
     245      } else if(node.Symbol is Argument) {
    246246        var argSym = node.Symbol as Argument;
    247247        strBuilder.Append(" ARG+" + argSym.ArgumentIndex + " ");
    248       } else if (node.Symbol is Derivative) {
     248      } else if(node.Symbol is Derivative) {
    249249        strBuilder.Append(@" \cfrac{d \left( ");
    250       } else if (node.Symbol is TimeLag) {
     250      } else if(node.Symbol is TimeLag) {
    251251        var laggedNode = node as ILaggedTreeNode;
    252252        currentLag += laggedNode.Lag;
    253       } else if (node.Symbol is Power) {
     253      } else if(node.Symbol is Power) {
    254254        strBuilder.Append(@" \left( ");
    255       } else if (node.Symbol is Root) {
     255      } else if(node.Symbol is Root) {
    256256        strBuilder.Append(@" \left( ");
    257       } else if (node.Symbol is Integral) {
     257      } else if(node.Symbol is Integral) {
    258258        // actually a new variable for t is needed in all subtrees (TODO)
    259259        var laggedTreeNode = node as ILaggedTreeNode;
    260260        strBuilder.Append(@"\sum_{t=" + (laggedTreeNode.Lag + currentLag) + @"}^0 \left( ");
    261       } else if (node.Symbol is VariableCondition) {
     261      } else if(node.Symbol is VariableCondition) {
    262262        var conditionTreeNode = node as VariableConditionTreeNode;
    263263        var constName = "c_{" + constants.Count + "}";
     
    276276
    277277    private void FormatSep(ISymbolicExpressionTreeNode node, StringBuilder strBuilder, int step) {
    278       if (node.Symbol is Addition) {
     278      if(node.Symbol is Addition) {
    279279        strBuilder.Append(" + ");
    280       } else if (node.Symbol is Subtraction) {
     280      } else if(node.Symbol is Subtraction) {
    281281        strBuilder.Append(" - ");
    282       } else if (node.Symbol is Multiplication) {
     282      } else if(node.Symbol is Multiplication) {
    283283        strBuilder.Append(@" \cdot ");
    284       } else if (node.Symbol is Division) {
    285         if (step + 1 == node.SubtreeCount)
     284      } else if(node.Symbol is Division) {
     285        if(step + 1 == node.SubtreeCount)
    286286          strBuilder.Append(@"}{");
    287287        else
    288288          strBuilder.Append(@" }{ \cfrac{ ");
    289       } else if (node.Symbol is Average) {
     289      } else if(node.Symbol is Average) {
    290290        strBuilder.Append(@" + ");
    291       } else if (node.Symbol is Logarithm) {
    292         throw new InvalidOperationException();
    293       } else if (node.Symbol is Exponential) {
    294         throw new InvalidOperationException();
    295       } else if (node.Symbol is Square) {
    296         throw new InvalidOperationException();
    297       } else if (node.Symbol is SquareRoot) {
    298         throw new InvalidOperationException();
    299       } else if (node.Symbol is Sine) {
    300         throw new InvalidOperationException();
    301       } else if (node.Symbol is Cosine) {
    302         throw new InvalidOperationException();
    303       } else if (node.Symbol is Tangent) {
    304         throw new InvalidOperationException();
    305       } else if (node.Symbol is AiryA) {
    306         throw new InvalidOperationException();
    307       } else if (node.Symbol is AiryB) {
    308         throw new InvalidOperationException();
    309       } else if (node.Symbol is Bessel) {
    310         throw new InvalidOperationException();
    311       } else if (node.Symbol is CosineIntegral) {
    312         throw new InvalidOperationException();
    313       } else if (node.Symbol is Dawson) {
    314         throw new InvalidOperationException();
    315       } else if (node.Symbol is Erf) {
    316         throw new InvalidOperationException();
    317       } else if (node.Symbol is ExponentialIntegralEi) {
    318         throw new InvalidOperationException();
    319       } else if (node.Symbol is FresnelCosineIntegral) {
    320         throw new InvalidOperationException();
    321       } else if (node.Symbol is FresnelSineIntegral) {
    322         throw new InvalidOperationException();
    323       } else if (node.Symbol is Gamma) {
    324         throw new InvalidOperationException();
    325       } else if (node.Symbol is HyperbolicCosineIntegral) {
    326         throw new InvalidOperationException();
    327       } else if (node.Symbol is HyperbolicSineIntegral) {
    328         throw new InvalidOperationException();
    329       } else if (node.Symbol is Norm) {
    330         throw new InvalidOperationException();
    331       } else if (node.Symbol is Psi) {
    332         throw new InvalidOperationException();
    333       } else if (node.Symbol is SineIntegral) {
    334         throw new InvalidOperationException();
    335       } else if (node.Symbol is GreaterThan) {
     291      } else if(node.Symbol is Logarithm) {
     292        throw new InvalidOperationException();
     293      } else if(node.Symbol is Exponential) {
     294        throw new InvalidOperationException();
     295      } else if(node.Symbol is Square) {
     296        throw new InvalidOperationException();
     297      } else if(node.Symbol is SquareRoot) {
     298        throw new InvalidOperationException();
     299      } else if(node.Symbol is Sine) {
     300        throw new InvalidOperationException();
     301      } else if(node.Symbol is Cosine) {
     302        throw new InvalidOperationException();
     303      } else if(node.Symbol is Tangent) {
     304        throw new InvalidOperationException();
     305      } else if(node.Symbol is AiryA) {
     306        throw new InvalidOperationException();
     307      } else if(node.Symbol is AiryB) {
     308        throw new InvalidOperationException();
     309      } else if(node.Symbol is Bessel) {
     310        throw new InvalidOperationException();
     311      } else if(node.Symbol is CosineIntegral) {
     312        throw new InvalidOperationException();
     313      } else if(node.Symbol is Dawson) {
     314        throw new InvalidOperationException();
     315      } else if(node.Symbol is Erf) {
     316        throw new InvalidOperationException();
     317      } else if(node.Symbol is ExponentialIntegralEi) {
     318        throw new InvalidOperationException();
     319      } else if(node.Symbol is FresnelCosineIntegral) {
     320        throw new InvalidOperationException();
     321      } else if(node.Symbol is FresnelSineIntegral) {
     322        throw new InvalidOperationException();
     323      } else if(node.Symbol is Gamma) {
     324        throw new InvalidOperationException();
     325      } else if(node.Symbol is HyperbolicCosineIntegral) {
     326        throw new InvalidOperationException();
     327      } else if(node.Symbol is HyperbolicSineIntegral) {
     328        throw new InvalidOperationException();
     329      } else if(node.Symbol is Norm) {
     330        throw new InvalidOperationException();
     331      } else if(node.Symbol is Psi) {
     332        throw new InvalidOperationException();
     333      } else if(node.Symbol is SineIntegral) {
     334        throw new InvalidOperationException();
     335      } else if(node.Symbol is GreaterThan) {
    336336        strBuilder.Append(@" > ");
    337       } else if (node.Symbol is LessThan) {
     337      } else if(node.Symbol is LessThan) {
    338338        strBuilder.Append(@" < ");
    339       } else if (node.Symbol is And) {
     339      } else if(node.Symbol is And) {
    340340        strBuilder.Append(@" > 0  \right) \land \left(");
    341       } else if (node.Symbol is Or) {
     341      } else if(node.Symbol is Or) {
    342342        strBuilder.Append(@" > 0  \right) \lor \left(");
    343       } else if (node.Symbol is Not) {
    344         throw new InvalidOperationException();
    345       } else if (node.Symbol is IfThenElse) {
     343      } else if(node.Symbol is Not) {
     344        throw new InvalidOperationException();
     345      } else if(node.Symbol is IfThenElse) {
    346346        strBuilder.Append(@" , ");
    347       } else if (node.Symbol is ProgramRootSymbol) {
     347      } else if(node.Symbol is ProgramRootSymbol) {
    348348        strBuilder.Append(@"\\" + Environment.NewLine);
    349       } else if (node.Symbol is Defun) {
    350       } else if (node.Symbol is InvokeFunction) {
     349      } else if(node.Symbol is Defun) {
     350      } else if(node.Symbol is InvokeFunction) {
    351351        strBuilder.Append(" , ");
    352       } else if (node.Symbol is StartSymbol) {
     352      } else if(node.Symbol is StartSymbol) {
    353353        strBuilder.Append(@"\\" + Environment.NewLine);
    354354        FormatStartSymbol(strBuilder);
    355       } else if (node.Symbol is Power) {
     355      } else if(node.Symbol is Power) {
    356356        strBuilder.Append(@"\right) ^ { \operatorname{round} \left(");
    357       } else if (node.Symbol is Root) {
     357      } else if(node.Symbol is Root) {
    358358        strBuilder.Append(@"\right) ^ {  \cfrac{1}{ \operatorname{round} \left(");
    359       } else if (node.Symbol is VariableCondition) {
     359      } else if(node.Symbol is VariableCondition) {
    360360        var conditionTreeNode = node as VariableConditionTreeNode;
    361361        var const1Name = "c_{" + constants.Count + "}";
     
    374374
    375375    private void FormatEnd(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) {
    376       if (node.Symbol is Addition) {
    377         strBuilder.Append(@" \right) ");
    378       } else if (node.Symbol is Subtraction) {
    379         strBuilder.Append(@" \right) ");
    380       } else if (node.Symbol is Multiplication) {
    381       } else if (node.Symbol is Division) {
     376      if(node.Symbol is Addition) {
     377        strBuilder.Append(@" \right) ");
     378      } else if(node.Symbol is Subtraction) {
     379        strBuilder.Append(@" \right) ");
     380      } else if(node.Symbol is Multiplication) {
     381      } else if(node.Symbol is Division) {
    382382        strBuilder.Append(" } ");
    383         for (int i = 2; i < node.SubtreeCount; i++)
     383        for(int i = 2; i < node.SubtreeCount; i++)
    384384          strBuilder.Append(" } ");
    385       } else if (node.Symbol is Average) {
    386         strBuilder.Append(@" \right) ");
    387       } else if (node.Symbol is Logarithm) {
    388         strBuilder.Append(@" \right) ");
    389       } else if (node.Symbol is Exponential) {
    390         strBuilder.Append(@" \right) ");
    391       } else if (node.Symbol is Square) {
     385      } else if(node.Symbol is Average) {
     386        strBuilder.Append(@" \right) ");
     387      } else if(node.Symbol is Logarithm) {
     388        strBuilder.Append(@" \right) ");
     389      } else if(node.Symbol is Exponential) {
     390        strBuilder.Append(@" \right) ");
     391      } else if(node.Symbol is Square) {
    392392        strBuilder.Append(@"\right)^2");
    393       } else if (node.Symbol is SquareRoot) {
     393      } else if(node.Symbol is SquareRoot) {
    394394        strBuilder.Append(@"}");
    395       } else if (node.Symbol is Sine) {
    396         strBuilder.Append(@" \right) ");
    397       } else if (node.Symbol is Cosine) {
    398         strBuilder.Append(@" \right) ");
    399       } else if (node.Symbol is Tangent) {
    400         strBuilder.Append(@" \right) ");
    401       } else if (node.Symbol is AiryA) {
    402         strBuilder.Append(@" \right) ");
    403       } else if (node.Symbol is AiryB) {
    404         strBuilder.Append(@" \right) ");
    405       } else if (node.Symbol is Bessel) {
    406         strBuilder.Append(@" \right) ");
    407       } else if (node.Symbol is CosineIntegral) {
    408         strBuilder.Append(@" \right) ");
    409       } else if (node.Symbol is Dawson) {
    410         strBuilder.Append(@" \right) ");
    411       } else if (node.Symbol is Erf) {
    412         strBuilder.Append(@" \right) ");
    413       } else if (node.Symbol is ExponentialIntegralEi) {
    414         strBuilder.Append(@" \right) ");
    415       } else if (node.Symbol is FresnelCosineIntegral) {
    416         strBuilder.Append(@" \right) ");
    417       } else if (node.Symbol is FresnelSineIntegral) {
    418         strBuilder.Append(@" \right) ");
    419       } else if (node.Symbol is Gamma) {
    420         strBuilder.Append(@" \right) ");
    421       } else if (node.Symbol is HyperbolicCosineIntegral) {
    422         strBuilder.Append(@" \right) ");
    423       } else if (node.Symbol is HyperbolicSineIntegral) {
    424         strBuilder.Append(@" \right) ");
    425       } else if (node.Symbol is Norm) {
    426         strBuilder.Append(@" \right) ");
    427       } else if (node.Symbol is Psi) {
    428         strBuilder.Append(@" \right) ");
    429       } else if (node.Symbol is SineIntegral) {
    430         strBuilder.Append(@" \right) ");
    431       } else if (node.Symbol is GreaterThan) {
    432         strBuilder.Append(@" \right) ");
    433       } else if (node.Symbol is LessThan) {
    434         strBuilder.Append(@" \right) ");
    435       } else if (node.Symbol is And) {
     395      } else if(node.Symbol is Sine) {
     396        strBuilder.Append(@" \right) ");
     397      } else if(node.Symbol is Cosine) {
     398        strBuilder.Append(@" \right) ");
     399      } else if(node.Symbol is Tangent) {
     400        strBuilder.Append(@" \right) ");
     401      } else if(node.Symbol is AiryA) {
     402        strBuilder.Append(@" \right) ");
     403      } else if(node.Symbol is AiryB) {
     404        strBuilder.Append(@" \right) ");
     405      } else if(node.Symbol is Bessel) {
     406        strBuilder.Append(@" \right) ");
     407      } else if(node.Symbol is CosineIntegral) {
     408        strBuilder.Append(@" \right) ");
     409      } else if(node.Symbol is Dawson) {
     410        strBuilder.Append(@" \right) ");
     411      } else if(node.Symbol is Erf) {
     412        strBuilder.Append(@" \right) ");
     413      } else if(node.Symbol is ExponentialIntegralEi) {
     414        strBuilder.Append(@" \right) ");
     415      } else if(node.Symbol is FresnelCosineIntegral) {
     416        strBuilder.Append(@" \right) ");
     417      } else if(node.Symbol is FresnelSineIntegral) {
     418        strBuilder.Append(@" \right) ");
     419      } else if(node.Symbol is Gamma) {
     420        strBuilder.Append(@" \right) ");
     421      } else if(node.Symbol is HyperbolicCosineIntegral) {
     422        strBuilder.Append(@" \right) ");
     423      } else if(node.Symbol is HyperbolicSineIntegral) {
     424        strBuilder.Append(@" \right) ");
     425      } else if(node.Symbol is Norm) {
     426        strBuilder.Append(@" \right) ");
     427      } else if(node.Symbol is Psi) {
     428        strBuilder.Append(@" \right) ");
     429      } else if(node.Symbol is SineIntegral) {
     430        strBuilder.Append(@" \right) ");
     431      } else if(node.Symbol is GreaterThan) {
     432        strBuilder.Append(@" \right) ");
     433      } else if(node.Symbol is LessThan) {
     434        strBuilder.Append(@" \right) ");
     435      } else if(node.Symbol is And) {
    436436        strBuilder.Append(@" > 0 \right) \right) ");
    437       } else if (node.Symbol is Or) {
     437      } else if(node.Symbol is Or) {
    438438        strBuilder.Append(@" > 0 \right) \right) ");
    439       } else if (node.Symbol is Not) {
    440         strBuilder.Append(@" \right) ");
    441       } else if (node.Symbol is IfThenElse) {
    442         strBuilder.Append(@" \right) ");
    443       } else if (node.Symbol is Constant) {
    444       } else if (node.Symbol is LaggedVariable) {
    445       } else if (node.Symbol is Variable) {
    446       } else if (node.Symbol is FactorVariable) {
    447       } else if (node.Symbol is BinaryFactorVariable) {
    448       } else if (node.Symbol is ProgramRootSymbol) {
     439      } else if(node.Symbol is Not) {
     440        strBuilder.Append(@" \right) ");
     441      } else if(node.Symbol is IfThenElse) {
     442        strBuilder.Append(@" \right) ");
     443      } else if(node.Symbol is Constant) {
     444      } else if(node.Symbol is LaggedVariable) {
     445      } else if(node.Symbol is Variable) {
     446      } else if(node.Symbol is FactorVariable) {
     447      } else if(node.Symbol is BinaryFactorVariable) {
     448      } else if(node.Symbol is ProgramRootSymbol) {
    449449        strBuilder
    450450          .AppendLine("\\end{align*}")
     
    452452          .AppendLine("\\nonumber");
    453453        // output all constant values
    454         if (constants.Count > 0) {
    455           foreach (var constant in constants) {
     454        if(constants.Count > 0) {
     455          foreach(var constant in constants) {
    456456            // replace "." with ".&" to align decimal points
    457457            var constStr = string.Format(System.Globalization.NumberFormatInfo.InvariantInfo, "{0:G5}", constant.Value);
    458             if (!constStr.Contains(".")) constStr = constStr + ".0";
     458            if(!constStr.Contains(".")) constStr = constStr + ".0";
    459459            constStr = constStr.Replace(".", "&.");  // fix problem in rendering of aligned expressions
    460460            strBuilder.Append(constant.Key + "& = & " + constStr);
     
    463463        }
    464464        strBuilder.AppendLine("\\end{align*}");
    465       } else if (node.Symbol is Defun) {
    466       } else if (node.Symbol is InvokeFunction) {
    467         strBuilder.Append(@" \right) ");
    468       } else if (node.Symbol is StartSymbol) {
    469       } else if (node.Symbol is Argument) {
    470       } else if (node.Symbol is Derivative) {
     465      } else if(node.Symbol is Defun) {
     466      } else if(node.Symbol is InvokeFunction) {
     467        strBuilder.Append(@" \right) ");
     468      } else if(node.Symbol is StartSymbol) {
     469      } else if(node.Symbol is Argument) {
     470      } else if(node.Symbol is Derivative) {
    471471        strBuilder.Append(@" \right) }{dt} ");
    472       } else if (node.Symbol is TimeLag) {
     472      } else if(node.Symbol is TimeLag) {
    473473        var laggedNode = node as ILaggedTreeNode;
    474474        currentLag -= laggedNode.Lag;
    475       } else if (node.Symbol is Power) {
     475      } else if(node.Symbol is Power) {
    476476        strBuilder.Append(@" \right) } ");
    477       } else if (node.Symbol is Root) {
     477      } else if(node.Symbol is Root) {
    478478        strBuilder.Append(@" \right) } } ");
    479       } else if (node.Symbol is Integral) {
    480         strBuilder.Append(@" \right) ");
    481       } else if (node.Symbol is VariableCondition) {
     479      } else if(node.Symbol is Integral) {
     480        strBuilder.Append(@" \right) ");
     481      } else if(node.Symbol is VariableCondition) {
    482482        strBuilder.Append(@"\right) ");
    483483      } else {
     
    488488    private void FormatStartSymbol(StringBuilder strBuilder) {
    489489      strBuilder.Append(targetVariable ?? "target_" + (targetCount++));
    490       if (containsTimeSeriesSymbol)
     490      if(containsTimeSeriesSymbol)
    491491        strBuilder.Append("(t)");
    492492      strBuilder.Append(" & = ");
     
    494494
    495495    private string LagToString(int lag) {
    496       if (lag < 0) {
     496      if(lag < 0) {
    497497        return "(t" + lag + ")";
    498       } else if (lag > 0) {
     498      } else if(lag > 0) {
    499499        return "(t+" + lag + ")";
    500500      } else return "";
  • branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionMATLABFormatter.cs

    r14185 r14764  
    2121
    2222using System.Globalization;
     23using System.Linq;
    2324using System.Text;
    2425using HeuristicLab.Common;
     
    7778      stringBuilder.AppendLine("  y = (f0 + 2*f1 - 2*f3 - f4) / 8;");
    7879      stringBuilder.AppendLine("end");
     80
     81      var factorVariableNames =
     82        symbolicExpressionTree.IterateNodesPostfix()
     83          .OfType<FactorVariableTreeNode>()
     84          .Select(n => n.VariableName)
     85          .Distinct();
     86
     87      foreach(var factorVarName in factorVariableNames)
     88      {
     89        var factorSymb = symbolicExpressionTree.IterateNodesPostfix()
     90          .OfType<FactorVariableTreeNode>()
     91          .First(n => n.VariableName == factorVarName)
     92          .Symbol;
     93        stringBuilder.AppendFormat("function y = switch_{0}(val, v)", factorVarName).AppendLine();
     94        var values = factorSymb.GetVariableValues(factorVarName).ToArray();
     95        stringBuilder.AppendLine("switch val");
     96        for(int i = 0; i < values.Length; i++) {
     97          stringBuilder.AppendFormat(CultureInfo.InvariantCulture, "  case \"{0}\" y = v({1})", values[i], i).AppendLine();
     98        }
     99        stringBuilder.AppendLine("end");
     100        stringBuilder.AppendLine();
     101      }
     102
    79103      return stringBuilder.ToString();
    80104    }
     
    92116      StringBuilder stringBuilder = new StringBuilder();
    93117
    94       if (symbol is ProgramRootSymbol) {
     118      if(symbol is ProgramRootSymbol) {
    95119        stringBuilder.AppendLine(FormatRecursively(node.GetSubtree(0)));
    96       } else if (symbol is StartSymbol)
     120      } else if(symbol is StartSymbol)
    97121        return FormatRecursively(node.GetSubtree(0));
    98       else if (symbol is Addition) {
    99         stringBuilder.Append("(");
    100         for (int i = 0; i < node.SubtreeCount; i++) {
    101           if (i > 0) stringBuilder.Append("+");
    102           stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
    103         }
    104         stringBuilder.Append(")");
    105       } else if (symbol is And) {
     122      else if(symbol is Addition) {
     123        stringBuilder.Append("(");
     124        for(int i = 0; i < node.SubtreeCount; i++) {
     125          if(i > 0) stringBuilder.Append("+");
     126          stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
     127        }
     128        stringBuilder.Append(")");
     129      } else if(symbol is And) {
    106130        stringBuilder.Append("((");
    107         for (int i = 0; i < node.SubtreeCount; i++) {
    108           if (i > 0) stringBuilder.Append("&");
     131        for(int i = 0; i < node.SubtreeCount; i++) {
     132          if(i > 0) stringBuilder.Append("&");
    109133          stringBuilder.Append("((");
    110134          stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
     
    113137        stringBuilder.Append(")-0.5)*2");
    114138        // MATLAB maps false and true to 0 and 1, resp., we map this result to -1.0 and +1.0, resp.
    115       } else if (symbol is Average) {
     139      } else if(symbol is Average) {
    116140        stringBuilder.Append("(1/");
    117141        stringBuilder.Append(node.SubtreeCount);
    118142        stringBuilder.Append(")*(");
    119         for (int i = 0; i < node.SubtreeCount; i++) {
    120           if (i > 0) stringBuilder.Append("+");
     143        for(int i = 0; i < node.SubtreeCount; i++) {
     144          if(i > 0) stringBuilder.Append("+");
    121145          stringBuilder.Append("(");
    122146          stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
     
    124148        }
    125149        stringBuilder.Append(")");
    126       } else if (symbol is Constant) {
     150      } else if(symbol is Constant) {
    127151        ConstantTreeNode constantTreeNode = node as ConstantTreeNode;
    128152        stringBuilder.Append(constantTreeNode.Value.ToString(CultureInfo.InvariantCulture));
    129       } else if (symbol is Cosine) {
     153      } else if(symbol is Cosine) {
    130154        stringBuilder.Append("cos(");
    131155        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
    132156        stringBuilder.Append(")");
    133       } else if (symbol is Division) {
    134         if (node.SubtreeCount == 1) {
     157      } else if(symbol is Division) {
     158        if(node.SubtreeCount == 1) {
    135159          stringBuilder.Append("1/");
    136160          stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
     
    138162          stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
    139163          stringBuilder.Append("/(");
    140           for (int i = 1; i < node.SubtreeCount; i++) {
    141             if (i > 1) stringBuilder.Append("*");
     164          for(int i = 1; i < node.SubtreeCount; i++) {
     165            if(i > 1) stringBuilder.Append("*");
    142166            stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
    143167          }
    144168          stringBuilder.Append(")");
    145169        }
    146       } else if (symbol is Exponential) {
     170      } else if(symbol is Exponential) {
    147171        stringBuilder.Append("exp(");
    148172        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
    149173        stringBuilder.Append(")");
    150       } else if (symbol is Square) {
     174      } else if(symbol is Square) {
    151175        stringBuilder.Append("(");
    152176        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
    153177        stringBuilder.Append(").^2");
    154       } else if (symbol is SquareRoot) {
     178      } else if(symbol is SquareRoot) {
    155179        stringBuilder.Append("sqrt(");
    156180        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
    157181        stringBuilder.Append(")");
    158       } else if (symbol is GreaterThan) {
     182      } else if(symbol is GreaterThan) {
    159183        stringBuilder.Append("((");
    160184        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
     
    163187        stringBuilder.Append(")-0.5)*2");
    164188        // MATLAB maps false and true to 0 and 1, resp., we map this result to -1.0 and +1.0, resp.
    165       } else if (symbol is IfThenElse) {
     189      } else if(symbol is IfThenElse) {
    166190        stringBuilder.Append("(");
    167191        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
     
    173197        stringBuilder.Append("<=0)*");
    174198        stringBuilder.Append(FormatRecursively(node.GetSubtree(2)));
    175       } else if (symbol is LaggedVariable) {
     199      } else if(symbol is LaggedVariable) {
    176200        // this if must be checked before if(symbol is LaggedVariable)
    177201        LaggedVariableTreeNode laggedVariableTreeNode = node as LaggedVariableTreeNode;
     
    180204        stringBuilder.Append(laggedVariableTreeNode.VariableName +
    181205                             LagToString(currentLag + laggedVariableTreeNode.Lag));
    182       } else if (symbol is LessThan) {
     206      } else if(symbol is LessThan) {
    183207        stringBuilder.Append("((");
    184208        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
     
    187211        stringBuilder.Append(")-0.5)*2");
    188212        // MATLAB maps false and true to 0 and 1, resp., we map this result to -1.0 and +1.0, resp.
    189       } else if (symbol is Logarithm) {
     213      } else if(symbol is Logarithm) {
    190214        stringBuilder.Append("log_(");
    191215        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
    192216        stringBuilder.Append(")");
    193       } else if (symbol is Multiplication) {
    194         for (int i = 0; i < node.SubtreeCount; i++) {
    195           if (i > 0) stringBuilder.Append("*");
    196           stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
    197         }
    198       } else if (symbol is Not) {
     217      } else if(symbol is Multiplication) {
     218        for(int i = 0; i < node.SubtreeCount; i++) {
     219          if(i > 0) stringBuilder.Append("*");
     220          stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
     221        }
     222      } else if(symbol is Not) {
    199223        stringBuilder.Append("~(");
    200224        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
    201225        stringBuilder.Append(" > 0 )");
    202       } else if (symbol is Or) {
     226      } else if(symbol is Or) {
    203227        stringBuilder.Append("((");
    204         for (int i = 0; i < node.SubtreeCount; i++) {
    205           if (i > 0) stringBuilder.Append("|");
     228        for(int i = 0; i < node.SubtreeCount; i++) {
     229          if(i > 0) stringBuilder.Append("|");
    206230          stringBuilder.Append("((");
    207231          stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
     
    210234        stringBuilder.Append(")-0.5)*2");
    211235        // MATLAB maps false and true to 0 and 1, resp., we map this result to -1.0 and +1.0, resp.
    212       } else if (symbol is Sine) {
     236      } else if(symbol is Sine) {
    213237        stringBuilder.Append("sin(");
    214238        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
    215239        stringBuilder.Append(")");
    216       } else if (symbol is Subtraction) {
    217         stringBuilder.Append("(");
    218         if (node.SubtreeCount == 1) {
     240      } else if(symbol is Subtraction) {
     241        stringBuilder.Append("(");
     242        if(node.SubtreeCount == 1) {
    219243          stringBuilder.Append("-");
    220244          stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
    221245        } else {
    222246          stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
    223           for (int i = 1; i < node.SubtreeCount; i++) {
     247          for(int i = 1; i < node.SubtreeCount; i++) {
    224248            stringBuilder.Append("-");
    225249            stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
     
    227251        }
    228252        stringBuilder.Append(")");
    229       } else if (symbol is Tangent) {
     253      } else if(symbol is Tangent) {
    230254        stringBuilder.Append("tan(");
    231255        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
    232256        stringBuilder.Append(")");
    233       } else if (node.Symbol is AiryA) {
     257      } else if(node.Symbol is AiryA) {
    234258        stringBuilder.Append("airy(");
    235259        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
    236260        stringBuilder.Append(")");
    237       } else if (node.Symbol is AiryB) {
     261      } else if(node.Symbol is AiryB) {
    238262        stringBuilder.Append("airy(2, ");
    239263        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
    240264        stringBuilder.Append(")");
    241       } else if (node.Symbol is Bessel) {
     265      } else if(node.Symbol is Bessel) {
    242266        stringBuilder.Append("besseli(0.0,");
    243267        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
    244268        stringBuilder.Append(")");
    245       } else if (node.Symbol is CosineIntegral) {
     269      } else if(node.Symbol is CosineIntegral) {
    246270        stringBuilder.Append("cosint(");
    247271        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
    248272        stringBuilder.Append(")");
    249       } else if (node.Symbol is Dawson) {
     273      } else if(node.Symbol is Dawson) {
    250274        stringBuilder.Append("dawson(");
    251275        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
    252276        stringBuilder.Append(")");
    253       } else if (node.Symbol is Erf) {
     277      } else if(node.Symbol is Erf) {
    254278        stringBuilder.Append("erf(");
    255279        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
    256280        stringBuilder.Append(")");
    257       } else if (node.Symbol is ExponentialIntegralEi) {
     281      } else if(node.Symbol is ExponentialIntegralEi) {
    258282        stringBuilder.Append("expint(");
    259283        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
    260284        stringBuilder.Append(")");
    261       } else if (node.Symbol is FresnelCosineIntegral) {
     285      } else if(node.Symbol is FresnelCosineIntegral) {
    262286        stringBuilder.Append("FresnelC(");
    263287        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
    264288        stringBuilder.Append(")");
    265       } else if (node.Symbol is FresnelSineIntegral) {
     289      } else if(node.Symbol is FresnelSineIntegral) {
    266290        stringBuilder.Append("FresnelS(");
    267291        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
    268292        stringBuilder.Append(")");
    269       } else if (node.Symbol is Gamma) {
     293      } else if(node.Symbol is Gamma) {
    270294        stringBuilder.Append("gamma(");
    271295        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
    272296        stringBuilder.Append(")");
    273       } else if (node.Symbol is HyperbolicCosineIntegral) {
     297      } else if(node.Symbol is HyperbolicCosineIntegral) {
    274298        stringBuilder.Append("Chi(");
    275299        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
    276300        stringBuilder.Append(")");
    277       } else if (node.Symbol is HyperbolicSineIntegral) {
     301      } else if(node.Symbol is HyperbolicSineIntegral) {
    278302        stringBuilder.Append("Shi(");
    279303        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
    280304        stringBuilder.Append(")");
    281       } else if (node.Symbol is Norm) {
     305      } else if(node.Symbol is Norm) {
    282306        stringBuilder.Append("normpdf(");
    283307        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
    284308        stringBuilder.Append(")");
    285       } else if (node.Symbol is Psi) {
     309      } else if(node.Symbol is Psi) {
    286310        stringBuilder.Append("psi(");
    287311        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
    288312        stringBuilder.Append(")");
    289       } else if (node.Symbol is SineIntegral) {
     313      } else if(node.Symbol is SineIntegral) {
    290314        stringBuilder.Append("sinint(");
    291315        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
    292316        stringBuilder.Append(")");
    293       } else if (symbol is HeuristicLab.Problems.DataAnalysis.Symbolic.Variable) {
     317      } else if(symbol is HeuristicLab.Problems.DataAnalysis.Symbolic.Variable) {
    294318        VariableTreeNode variableTreeNode = node as VariableTreeNode;
    295319        stringBuilder.Append(variableTreeNode.Weight.ToString(CultureInfo.InvariantCulture));
    296320        stringBuilder.Append("*");
    297321        stringBuilder.Append(variableTreeNode.VariableName + LagToString(currentLag));
    298       } else if (symbol is Power) {
     322      } else if(symbol is HeuristicLab.Problems.DataAnalysis.Symbolic.FactorVariable) {
     323        var factorNode = node as FactorVariableTreeNode;
     324        var weights = string.Join(" ", factorNode.Weights.Select(w => w.ToString("G17", CultureInfo.InvariantCulture)));
     325        stringBuilder.AppendFormat("switch_{0}(\"{1}\",[{2}])",
     326          factorNode.VariableName, factorNode.VariableName, weights)
     327          .AppendLine();
     328      } else if(symbol is HeuristicLab.Problems.DataAnalysis.Symbolic.BinaryFactorVariable) {
     329        var factorNode = node as BinaryFactorVariableTreeNode;
     330        stringBuilder.AppendFormat(CultureInfo.InvariantCulture,
     331          "((strcmp({0},\"{1}\")==1) * {2:G17})", factorNode.VariableName, factorNode.VariableValue, factorNode.Weight);
     332      } else if(symbol is Power) {
    299333        stringBuilder.Append("(");
    300334        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
     
    302336        stringBuilder.Append(FormatRecursively(node.GetSubtree(1)));
    303337        stringBuilder.Append(")");
    304       } else if (symbol is Root) {
     338      } else if(symbol is Root) {
    305339        stringBuilder.Append("(");
    306340        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
     
    308342        stringBuilder.Append(FormatRecursively(node.GetSubtree(1)));
    309343        stringBuilder.Append("))");
    310       } else if (symbol is Derivative) {
     344      } else if(symbol is Derivative) {
    311345        stringBuilder.Append("fivePoint(");
    312346        // f0
     
    326360        stringBuilder.Append(")");
    327361        currentLag += 4;
    328       } else if (symbol is Integral) {
     362      } else if(symbol is Integral) {
    329363        var laggedNode = node as LaggedTreeNode;
    330364        string prevCounterVariable = CurrentIndexVariable;
     
    334368                                 "))");
    335369        ReleaseIndexVariable();
    336       } else if (symbol is TimeLag) {
     370      } else if(symbol is TimeLag) {
    337371        var laggedNode = node as LaggedTreeNode;
    338372        currentLag += laggedNode.Lag;
     
    347381
    348382    private string LagToString(int lag) {
    349       if (lag < 0) {
     383      if(lag < 0) {
    350384        return "(" + CurrentIndexVariable + "" + lag + ")";
    351       } else if (lag > 0) {
     385      } else if(lag > 0) {
    352386        return "(" + CurrentIndexVariable + "+" + lag + ")";
    353387      } else return "(" + CurrentIndexVariable + ")";
  • branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionMathematicaFormatter.cs

    r14185 r14764  
    5353
    5454    private void FormatRecursively(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) {
    55       if (node.Subtrees.Any()) {
    56         if (node.Symbol is Addition) {
     55      if(node.Subtrees.Any()) {
     56        if(node.Symbol is Addition) {
    5757          FormatFunction(node, "Plus", strBuilder);
    58         } else if (node.Symbol is Average) {
     58        } else if(node.Symbol is Average) {
    5959          FormatAverage(node, strBuilder);
    60         } else if (node.Symbol is Multiplication) {
     60        } else if(node.Symbol is Multiplication) {
    6161          FormatFunction(node, "Times", strBuilder);
    62         } else if (node.Symbol is Subtraction) {
     62        } else if(node.Symbol is Subtraction) {
    6363          FormatSubtraction(node, strBuilder);
    64         } else if (node.Symbol is Division) {
     64        } else if(node.Symbol is Division) {
    6565          FormatDivision(node, strBuilder);
    66         } else if (node.Symbol is Sine) {
     66        } else if(node.Symbol is Sine) {
    6767          FormatFunction(node, "Sin", strBuilder);
    68         } else if (node.Symbol is Cosine) {
     68        } else if(node.Symbol is Cosine) {
    6969          FormatFunction(node, "Cos", strBuilder);
    70         } else if (node.Symbol is Tangent) {
     70        } else if(node.Symbol is Tangent) {
    7171          FormatFunction(node, "Tan", strBuilder);
    72         } else if (node.Symbol is Exponential) {
     72        } else if(node.Symbol is Exponential) {
    7373          FormatFunction(node, "Exp", strBuilder);
    74         } else if (node.Symbol is Logarithm) {
     74        } else if(node.Symbol is Logarithm) {
    7575          FormatFunction(node, "Log", strBuilder);
    76         } else if (node.Symbol is IfThenElse) {
     76        } else if(node.Symbol is IfThenElse) {
    7777          FormatIf(node, strBuilder);
    78         } else if (node.Symbol is GreaterThan) {
     78        } else if(node.Symbol is GreaterThan) {
    7979          strBuilder.Append("If[Greater[");
    8080          FormatRecursively(node.GetSubtree(0), strBuilder);
     
    8282          FormatRecursively(node.GetSubtree(1), strBuilder);
    8383          strBuilder.Append("], 1, -1]");
    84         } else if (node.Symbol is LessThan) {
     84        } else if(node.Symbol is LessThan) {
    8585          strBuilder.Append("If[Less[");
    8686          FormatRecursively(node.GetSubtree(0), strBuilder);
     
    8888          FormatRecursively(node.GetSubtree(1), strBuilder);
    8989          strBuilder.Append("], 1, -1]");
    90         } else if (node.Symbol is And) {
     90        } else if(node.Symbol is And) {
    9191          FormatAnd(node, strBuilder);
    92         } else if (node.Symbol is Not) {
     92        } else if(node.Symbol is Not) {
    9393          strBuilder.Append("If[Greater[");
    9494          FormatRecursively(node.GetSubtree(0), strBuilder);
    9595          strBuilder.Append(", 0], -1, 1]");
    96         } else if (node.Symbol is Or) {
     96        } else if(node.Symbol is Or) {
    9797          FormatOr(node, strBuilder);
    98         } else if (node.Symbol is Xor) {
     98        } else if(node.Symbol is Xor) {
    9999          FormatXor(node, strBuilder);
    100         } else if (node.Symbol is Square) {
     100        } else if(node.Symbol is Square) {
    101101          FormatSquare(node, strBuilder);
    102         } else if (node.Symbol is SquareRoot) {
     102        } else if(node.Symbol is SquareRoot) {
    103103          FormatFunction(node, "Sqrt", strBuilder);
    104         } else if (node.Symbol is Power) {
     104        } else if(node.Symbol is Power) {
    105105          FormatFunction(node, "Power", strBuilder);
    106         } else if (node.Symbol is Root) {
     106        } else if(node.Symbol is Root) {
    107107          FormatRoot(node, strBuilder);
    108108        } else {
     
    110110        }
    111111      } else {
    112         if (node is VariableTreeNode) {
     112        // terminals
     113        if(node.Symbol is Variable) {
    113114          var varNode = node as VariableTreeNode;
    114115          strBuilder.AppendFormat("Times[{0}, {1}]", varNode.VariableName, varNode.Weight.ToString("G17", CultureInfo.InvariantCulture));
    115         } else if (node is ConstantTreeNode) {
     116        } else if(node.Symbol is Constant) {
    116117          var constNode = node as ConstantTreeNode;
    117118          strBuilder.Append(constNode.Value.ToString("G17", CultureInfo.InvariantCulture));
     119        } else if(node.Symbol is FactorVariable) {
     120          var factorNode = node as FactorVariableTreeNode;
     121          strBuilder.AppendFormat("Switch[{0},", factorNode.VariableName);
     122          var varValues = factorNode.Symbol.GetVariableValues(factorNode.VariableName).ToArray();
     123          var weights = varValues.Select(factorNode.GetValue).ToArray();
     124
     125          var weightStr = string.Join(", ",
     126            varValues.Zip(weights, (s, d) => string.Format(CultureInfo.InvariantCulture, "\"{0}\", {1:G17}", s, d)));
     127          strBuilder.Append(weightStr);
     128          strBuilder.Append("]");
     129        } else if(node.Symbol is BinaryFactorVariable) {
     130          var factorNode = node as BinaryFactorVariableTreeNode;
     131          strBuilder.AppendFormat(CultureInfo.InvariantCulture, "If[{0}==\"{1}\",{2:G17},0.0]",
     132            factorNode.VariableName, factorNode.VariableValue, factorNode.Weight);
    118133        } else {
    119134          throw new NotSupportedException("Formatting of symbol: " + node.Symbol + " is not supported.");
     
    124139    private void FormatXor(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) {
    125140      strBuilder.Append("If[Xor[");
    126       foreach (var t in node.Subtrees) {
     141      foreach(var t in node.Subtrees) {
    127142        strBuilder.Append("Greater[");
    128143        FormatRecursively(t, strBuilder);
    129144        strBuilder.Append(", 0]");
    130         if (t != node.Subtrees.Last()) strBuilder.Append(",");
     145        if(t != node.Subtrees.Last()) strBuilder.Append(",");
    131146      }
    132147      strBuilder.Append("], 1, -1]");
     
    135150    private void FormatOr(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) {
    136151      strBuilder.Append("If[Or[");
    137       foreach (var t in node.Subtrees) {
     152      foreach(var t in node.Subtrees) {
    138153        strBuilder.Append("Greater[");
    139154        FormatRecursively(t, strBuilder);
    140155        strBuilder.Append(", 0]");
    141         if (t != node.Subtrees.Last()) strBuilder.Append(",");
     156        if(t != node.Subtrees.Last()) strBuilder.Append(",");
    142157      }
    143158      strBuilder.Append("], 1, -1]");
     
    146161    private void FormatAnd(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) {
    147162      strBuilder.Append("If[And[");
    148       foreach (var t in node.Subtrees) {
     163      foreach(var t in node.Subtrees) {
    149164        strBuilder.Append("Greater[");
    150165        FormatRecursively(t, strBuilder);
    151166        strBuilder.Append(", 0]");
    152         if (t != node.Subtrees.Last()) strBuilder.Append(",");
     167        if(t != node.Subtrees.Last()) strBuilder.Append(",");
    153168      }
    154169      strBuilder.Append("], 1, -1]");
     
    169184      strBuilder.Append("Mean[{");
    170185      FormatRecursively(node.GetSubtree(0), strBuilder);
    171       for (int i = 1; i < node.SubtreeCount; i++) {
     186      for(int i = 1; i < node.SubtreeCount; i++) {
    172187        strBuilder.Append(",");
    173188        FormatRecursively(node.GetSubtree(i), strBuilder);
     
    180195      FormatRecursively(node.GetSubtree(0), strBuilder);
    181196      strBuilder.Append(", Times[-1");
    182       foreach (var t in node.Subtrees) {
     197      foreach(var t in node.Subtrees) {
    183198        strBuilder.Append(",");
    184199        FormatRecursively(t, strBuilder);
     
    202217
    203218    private void FormatDivision(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) {
    204       if (node.SubtreeCount == 1) {
     219      if(node.SubtreeCount == 1) {
    205220        strBuilder.Append("Divide[1, ");
    206221        FormatRecursively(node.GetSubtree(0), strBuilder);
     
    211226        strBuilder.Append(", Times[");
    212227        FormatRecursively(node.GetSubtree(1), strBuilder);
    213         for (int i = 2; i < node.SubtreeCount; i++) {
     228        for(int i = 2; i < node.SubtreeCount; i++) {
    214229          strBuilder.Append(",");
    215230          FormatRecursively(node.GetSubtree(i), strBuilder);
     
    221236    private void FormatFunction(ISymbolicExpressionTreeNode node, string function, StringBuilder strBuilder) {
    222237      strBuilder.Append(function + "[");
    223       foreach (var child in node.Subtrees) {
     238      foreach(var child in node.Subtrees) {
    224239        FormatRecursively(child, strBuilder);
    225         if (child != node.Subtrees.Last())
     240        if(child != node.Subtrees.Last())
    226241          strBuilder.Append(", ");
    227242      }
  • branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionSmalltalkFormatter.cs

    r14185 r14764  
    4848      ISymbol symbol = node.Symbol;
    4949
    50       if (symbol is ProgramRootSymbol || symbol is StartSymbol)
     50      if(symbol is ProgramRootSymbol || symbol is StartSymbol)
    5151        return FormatRecursively(node.GetSubtree(0));
    5252
     
    5555      stringBuilder.Append("(");
    5656
    57       if (symbol is Addition) {
    58         for (int i = 0; i < node.SubtreeCount; i++) {
    59           if (i > 0) stringBuilder.Append("+");
     57      if(symbol is Addition) {
     58        for(int i = 0; i < node.SubtreeCount; i++) {
     59          if(i > 0) stringBuilder.Append("+");
    6060          stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
    6161        }
    62       } else if (symbol is And) {
     62      } else if(symbol is And) {
    6363        stringBuilder.Append("(");
    64         for (int i = 0; i < node.SubtreeCount; i++) {
    65           if (i > 0) stringBuilder.Append("&");
     64        for(int i = 0; i < node.SubtreeCount; i++) {
     65          if(i > 0) stringBuilder.Append("&");
    6666          stringBuilder.Append("(");
    6767          stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
     
    6969        }
    7070        stringBuilder.Append(") ifTrue:[1] ifFalse:[-1]");
    71       } else if (symbol is Average) {
     71      } else if(symbol is Average) {
    7272        stringBuilder.Append("(1/");
    7373        stringBuilder.Append(node.SubtreeCount);
    7474        stringBuilder.Append(")*(");
    75         for (int i = 0; i < node.SubtreeCount; i++) {
    76           if (i > 0) stringBuilder.Append("+");
     75        for(int i = 0; i < node.SubtreeCount; i++) {
     76          if(i > 0) stringBuilder.Append("+");
    7777          stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
    7878        }
    7979        stringBuilder.Append(")");
    80       } else if (symbol is Constant) {
     80      } else if(symbol is Constant) {
    8181        ConstantTreeNode constantTreeNode = node as ConstantTreeNode;
    8282        stringBuilder.Append(constantTreeNode.Value.ToString(CultureInfo.InvariantCulture));
    83       } else if (symbol is Cosine) {
     83      } else if(symbol is Cosine) {
    8484        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
    8585        stringBuilder.Append(" cos");
    86       } else if (symbol is Division) {
    87         if (node.SubtreeCount == 1) {
     86      } else if(symbol is Division) {
     87        if(node.SubtreeCount == 1) {
    8888          stringBuilder.Append("1/");
    8989          stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
     
    9191          stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
    9292          stringBuilder.Append("/(");
    93           for (int i = 1; i < node.SubtreeCount; i++) {
    94             if (i > 1) stringBuilder.Append("*");
     93          for(int i = 1; i < node.SubtreeCount; i++) {
     94            if(i > 1) stringBuilder.Append("*");
    9595            stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
    9696          }
    9797          stringBuilder.Append(")");
    9898        }
    99       } else if (symbol is Exponential) {
     99      } else if(symbol is Exponential) {
    100100        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
    101101        stringBuilder.Append(" exp");
    102       } else if (symbol is GreaterThan) {
     102      } else if(symbol is GreaterThan) {
    103103        stringBuilder.Append("(");
    104104        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
     
    106106        stringBuilder.Append(FormatRecursively(node.GetSubtree(1)));
    107107        stringBuilder.Append(") ifTrue: [1] ifFalse: [-1]");
    108       } else if (symbol is IfThenElse) {
     108      } else if(symbol is IfThenElse) {
    109109        stringBuilder.Append("(");
    110110        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
     
    114114        stringBuilder.Append(FormatRecursively(node.GetSubtree(2)));
    115115        stringBuilder.Append("]");
    116       } else if (symbol is LaggedVariable) {
    117         stringBuilder.Append("lagged variable not implemented");
    118       } else if (symbol is LessThan) {
     116      } else if(symbol is LaggedVariable) {
     117        stringBuilder.Append("lagged variables are not supported");
     118      } else if(symbol is LessThan) {
    119119        stringBuilder.Append("(");
    120120        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
     
    122122        stringBuilder.Append(FormatRecursively(node.GetSubtree(1)));
    123123        stringBuilder.Append(") ifTrue: [1] ifFalse: [-1]");
    124       } else if (symbol is Logarithm) {
     124      } else if(symbol is Logarithm) {
    125125        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
    126126        stringBuilder.Append("ln");
    127       } else if (symbol is Multiplication) {
    128         for (int i = 0; i < node.SubtreeCount; i++) {
    129           if (i > 0) stringBuilder.Append("*");
     127      } else if(symbol is Multiplication) {
     128        for(int i = 0; i < node.SubtreeCount; i++) {
     129          if(i > 0) stringBuilder.Append("*");
    130130          stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
    131131        }
    132       } else if (symbol is Not) {
     132      } else if(symbol is Not) {
    133133        stringBuilder.Append("(");
    134134        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
    135135        stringBuilder.Append(">0) ifTrue: [-1] ifFalse: [1.0]");
    136       } else if (symbol is Or) {
     136      } else if(symbol is Or) {
    137137        stringBuilder.Append("(");
    138         for (int i = 0; i < node.SubtreeCount; i++) {
    139           if (i > 0) stringBuilder.Append("|");
     138        for(int i = 0; i < node.SubtreeCount; i++) {
     139          if(i > 0) stringBuilder.Append("|");
    140140          stringBuilder.Append("(");
    141141          stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
     
    143143        }
    144144        stringBuilder.Append(") ifTrue:[1] ifFalse:[-1]");
    145       } else if (symbol is Sine) {
     145      } else if(symbol is Sine) {
    146146        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
    147147        stringBuilder.Append(" sin");
    148       } else if (symbol is Subtraction) {
    149         if (node.SubtreeCount == 1) {
     148      } else if(symbol is Subtraction) {
     149        if(node.SubtreeCount == 1) {
    150150          stringBuilder.Append("-1*");
    151151          stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
    152152        } else {
    153153          stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
    154           for (int i = 1; i < node.SubtreeCount; i++) {
     154          for(int i = 1; i < node.SubtreeCount; i++) {
    155155            stringBuilder.Append(" - ");
    156156            stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
    157157          }
    158158        }
    159       } else if (symbol is Tangent) {
     159      } else if(symbol is Tangent) {
    160160        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
    161161        stringBuilder.Append(" tan");
    162       } else if (symbol is Variable) {
     162      } else if(symbol is Variable) {
    163163        VariableTreeNode variableTreeNode = node as VariableTreeNode;
    164164        stringBuilder.Append(variableTreeNode.Weight.ToString(CultureInfo.InvariantCulture));
    165165        stringBuilder.Append("*");
    166166        stringBuilder.Append(variableTreeNode.VariableName);
     167      } else if(symbol is BinaryFactorVariable || symbol is FactorVariable) {
     168        stringBuilder.Append("factor variables are not supported");
    167169      } else {
    168170        stringBuilder.Append("(");
    169         for (int i = 0; i < node.SubtreeCount; i++) {
    170           if (i > 0) stringBuilder.Append(", ");
     171        for(int i = 0; i < node.SubtreeCount; i++) {
     172          if(i > 0) stringBuilder.Append(", ");
    171173          stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
    172174        }
Note: See TracChangeset for help on using the changeset viewer.