Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
05/13/19 13:59:30 (6 years ago)
Author:
mkommend
Message:

#2521: Merged trunk changes up to r16945 into branch.

Location:
branches/2521_ProblemRefactoring
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • branches/2521_ProblemRefactoring

  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.DataAnalysis.Symbolic

  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionCSharpFormatter.cs

    r16723 r16946  
    137137        } else if (node.Symbol is SquareRoot) {
    138138          FormatFunction(node, "Math.Sqrt", strBuilder);
     139        } else if (node.Symbol is Cube) {
     140          FormatPower(node, strBuilder, "3");
     141        } else if (node.Symbol is CubeRoot) {
     142          strBuilder.Append("Cbrt(");
     143          FormatRecursively(node.GetSubtree(0), strBuilder);
     144          strBuilder.Append(")");
    139145        } else if (node.Symbol is Power) {
    140146          FormatFunction(node, "Math.Pow", strBuilder);
    141147        } else if (node.Symbol is Root) {
    142148          FormatRoot(node, strBuilder);
     149        } else if (node.Symbol is Absolute) {
     150          FormatFunction(node, "Math.Abs", strBuilder);
     151        } else if (node.Symbol is AnalyticQuotient) {
     152          strBuilder.Append("(");
     153          FormatRecursively(node.GetSubtree(0), strBuilder);
     154          strBuilder.Append(" / Math.Sqrt(1 + Math.Pow(");
     155          FormatRecursively(node.GetSubtree(1), strBuilder);
     156          strBuilder.Append(" , 2) )");
    143157        } else {
    144158          throw new NotSupportedException("Formatting of symbol: " + node.Symbol + " not supported for C# symbolic expression tree formatter.");
     
    173187
    174188    private void FormatSquare(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) {
     189      FormatPower(node, strBuilder, "2");
     190    }
     191    private void FormatPower(ISymbolicExpressionTreeNode node, StringBuilder strBuilder, string exponent) {
    175192      strBuilder.Append("Math.Pow(");
    176193      FormatRecursively(node.GetSubtree(0), strBuilder);
    177       strBuilder.Append(", 2)");
     194      strBuilder.Append($", {exponent})");
    178195    }
    179196
     
    237254      strBuilder.AppendLine("public static class Model {");
    238255      GenerateAverageSource(strBuilder);
     256      GenerateCbrtSource(strBuilder);
    239257      GenerateIfThenElseSource(strBuilder);
    240258      GenerateFactorSource(strBuilder);
     
    278296      strBuilder.AppendLine("private static double Average(params double[] values) {");
    279297      strBuilder.AppendLine("  return values.Average();");
     298      strBuilder.AppendLine("}");
     299    }
     300    private void GenerateCbrtSource(StringBuilder strBuilder) {
     301      strBuilder.AppendLine("private static double Cbrt(double x) {");
     302      strBuilder.AppendLine("  return x < 0 ? -Math.Pow(-x, 1.0 / 3.0) : Math.Pow(x, 1.0 / 3.0);");
    280303      strBuilder.AppendLine("}");
    281304    }
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionExcelFormatter.cs

    r16801 r16946  
    119119        }
    120120        stringBuilder.Append(")");
     121      } else if (symbol is Absolute) {
     122        stringBuilder.Append($"ABS({FormatRecursively(node.GetSubtree(0))})");
     123      } else if (symbol is AnalyticQuotient) {
     124        stringBuilder.Append($"({FormatRecursively(node.GetSubtree(0))}) / SQRT(1 + POWER({FormatRecursively(node.GetSubtree(1))}, 2))");
    121125      } else if (symbol is Average) {
    122126        stringBuilder.Append("(1/(");
     
    129133          stringBuilder.Append(")");
    130134        }
    131         stringBuilder.Append(")");
     135        stringBuilder.Append("))");
    132136      } else if (symbol is Constant) {
    133137        ConstantTreeNode constantTreeNode = node as ConstantTreeNode;
     
    137141        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
    138142        stringBuilder.Append(")");
     143      } else if (symbol is Cube) {
     144        stringBuilder.Append($"POWER({FormatRecursively(node.GetSubtree(0))}, 3)");
     145      } else if (symbol is CubeRoot) {
     146        var arg_expr = FormatRecursively(node.GetSubtree(0));
     147        stringBuilder.Append($"IF({arg_expr} < 0, -POWER(-{arg_expr}, 1/3), POWER({arg_expr}, 1/3))");
    139148      } else if (symbol is Division) {
    140149        if (node.SubtreeCount == 1) {
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionLatexFormatter.cs

    r16723 r16946  
    117117          strBuilder.Append(@" \cfrac{ ");
    118118        }
     119      } else if (node.Symbol is Absolute) {
     120        strBuilder.Append(@"\operatorname{abs} \left( ");
     121      } else if (node.Symbol is AnalyticQuotient) {
     122        strBuilder.Append(@" \frac { ");
    119123      } else if (node.Symbol is Average) {
    120124        // skip output of (1/1) if only one subtree
     
    131135      } else if (node.Symbol is SquareRoot) {
    132136        strBuilder.Append(@"\sqrt{");
     137      } else if (node.Symbol is Cube) {
     138        strBuilder.Append(@"\left(");
     139      } else if (node.Symbol is CubeRoot) {
     140        strBuilder.Append(@"\operatorname{cbrt}\left(");
    133141      } else if (node.Symbol is Sine) {
    134142        strBuilder.Append(@"\sin \left( ");
     
    289297        else
    290298          strBuilder.Append(@" }{ \cfrac{ ");
     299      } else if (node.Symbol is Absolute) {
     300        throw new InvalidOperationException();
     301      } else if (node.Symbol is AnalyticQuotient) {
     302        strBuilder.Append(@"}{\sqrt{1 + \left( ");
    291303      } else if (node.Symbol is Average) {
    292304        strBuilder.Append(@" + ");
     
    298310        throw new InvalidOperationException();
    299311      } else if (node.Symbol is SquareRoot) {
     312        throw new InvalidOperationException();
     313      } else if (node.Symbol is Cube) {
     314        throw new InvalidOperationException();
     315      } else if (node.Symbol is CubeRoot) {
    300316        throw new InvalidOperationException();
    301317      } else if (node.Symbol is Sine) {
     
    387403        for (int i = 2; i < node.SubtreeCount; i++)
    388404          strBuilder.Append(" } ");
     405      } else if (node.Symbol is Absolute) {
     406        strBuilder.Append(@" \right)");
     407      } else if (node.Symbol is AnalyticQuotient) {
     408        strBuilder.Append(@" \right)^2}}");
    389409      } else if (node.Symbol is Average) {
    390410        strBuilder.Append(@" \right) ");
     
    397417      } else if (node.Symbol is SquareRoot) {
    398418        strBuilder.Append(@"}");
     419      } else if (node.Symbol is Cube) {
     420        strBuilder.Append(@"\right)^3");
     421      } else if (node.Symbol is CubeRoot) {
     422        strBuilder.Append(@"\right)");
    399423      } else if (node.Symbol is Sine) {
    400424        strBuilder.Append(@" \right) ");
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionMATLABFormatter.cs

    r16723 r16946  
    126126        }
    127127        stringBuilder.Append(")");
     128      } else if (symbol is Absolute) {
     129        stringBuilder.Append($"abs({FormatRecursively(node.GetSubtree(0))})");
     130      } else if (symbol is AnalyticQuotient) {
     131        stringBuilder.Append($"({FormatRecursively(node.GetSubtree(0))}) / sqrt(1 + ({FormatRecursively(node.GetSubtree(1))}).^2)");
    128132      } else if (symbol is And) {
    129133        stringBuilder.Append("((");
     
    179183        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
    180184        stringBuilder.Append(")");
     185      } else if (symbol is Cube) {
     186        stringBuilder.Append("(");
     187        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
     188        stringBuilder.Append(").^3");
     189      } else if (symbol is CubeRoot) {
     190        stringBuilder.Append("NTHROOT(");
     191        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
     192        stringBuilder.Append(", 3)");
    181193      } else if (symbol is GreaterThan) {
    182194        stringBuilder.Append("((");
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionMathematicaFormatter.cs

    r16723 r16946  
    5656        if (node.Symbol is Addition) {
    5757          FormatFunction(node, "Plus", strBuilder);
     58        } else if (node.Symbol is Absolute) {
     59          FormatFunction(node, "Abs", strBuilder);
     60        } else if (node.Symbol is AnalyticQuotient) {
     61          strBuilder.Append("[");
     62          FormatRecursively(node.GetSubtree(0), strBuilder);
     63          strBuilder.Append("]/Sqrt[ 1 + Power[");
     64          FormatRecursively(node.GetSubtree(1), strBuilder);
     65          strBuilder.Append(", 2]]");
    5866        } else if (node.Symbol is Average) {
    5967          FormatAverage(node, strBuilder);
     
    104112        } else if (node.Symbol is SquareRoot) {
    105113          FormatFunction(node, "Sqrt", strBuilder);
     114        } else if (node.Symbol is Cube) {
     115          FormatPower(node, strBuilder, "3");
     116        } else if (node.Symbol is CubeRoot) {
     117          strBuilder.Append("CubeRoot[");
     118          FormatRecursively(node.GetSubtree(0), strBuilder);
     119          strBuilder.Append("]");
    106120        } else if (node.Symbol is Power) {
    107121          FormatFunction(node, "Power", strBuilder);
     
    205219
    206220    private void FormatSquare(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) {
     221      FormatPower(node, strBuilder, "2");
     222    }
     223
     224    private void FormatPower(ISymbolicExpressionTreeNode node, StringBuilder strBuilder, string exponent) {
    207225      strBuilder.Append("Power[");
    208226      FormatRecursively(node.GetSubtree(0), strBuilder);
    209       strBuilder.Append(", 2]");
     227      strBuilder.Append($", {exponent}]");
    210228    }
    211229
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionSmalltalkFormatter.cs

    r16723 r16946  
    2020#endregion
    2121
     22using System;
    2223using System.Globalization;
    2324using System.Text;
     
    6970        }
    7071        stringBuilder.Append(") ifTrue:[1] ifFalse:[-1]");
     72      } else if (symbol is Absolute) {
     73        stringBuilder.Append($"({FormatRecursively(node.GetSubtree(0))}) abs");
     74      } else if (symbol is AnalyticQuotient) {
     75        stringBuilder.Append($"({FormatRecursively(node.GetSubtree(0))}) / (1 + ({FormatPower(node.GetSubtree(1), "2")})) sqrt");
    7176      } else if (symbol is Average) {
    7277        stringBuilder.Append("(1/");
     
    8489        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
    8590        stringBuilder.Append(" cos");
     91      } else if (symbol is Cube) {
     92        stringBuilder.Append(FormatPower(node.GetSubtree(0), "3"));
     93      } else if (symbol is CubeRoot) {
     94        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
     95        stringBuilder.Append(" cbrt");
    8696      } else if (symbol is Division) {
    8797        if (node.SubtreeCount == 1) {
     
    146156        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
    147157        stringBuilder.Append(" sin");
     158      } else if (symbol is Square) {
     159        stringBuilder.Append(FormatPower(node.GetSubtree(0), "2"));
     160      } else if (symbol is SquareRoot) {
     161        stringBuilder.Append(FormatPower(node.GetSubtree(0), "(1/2)"));
    148162      } else if (symbol is Subtraction) {
    149163        if (node.SubtreeCount == 1) {
     
    184198    }
    185199
     200    private string FormatPower(ISymbolicExpressionTreeNode node, string exponent) {
     201      return $"(({FormatRecursively(node)}) log * {exponent}) exp ";
     202    }
     203
    186204    public override IDeepCloneable Clone(Cloner cloner) {
    187205      return new SymbolicDataAnalysisExpressionSmalltalkFormatter(this, cloner);
Note: See TracChangeset for help on using the changeset viewer.