Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
08/07/19 13:32:09 (5 years ago)
Author:
mkommend
Message:

#2974: Merged trunk changes into branch.

Location:
branches/2974_Constants_Optimization/HeuristicLab.Problems.DataAnalysis.Symbolic
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/2974_Constants_Optimization/HeuristicLab.Problems.DataAnalysis.Symbolic

  • branches/2974_Constants_Optimization/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionCSharpFormatter.cs

    r16676 r17193  
    11#region License Information
    22/* HeuristicLab
    3  * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
    44 *
    55 * This file is part of HeuristicLab.
     
    2626using System.Text;
    2727using System.Text.RegularExpressions;
     28using HEAL.Attic;
    2829using HeuristicLab.Common;
    2930using HeuristicLab.Core;
    3031using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    31 using HEAL.Attic;
    3232
    3333namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
     
    5656    }
    5757
    58     private string VariableName2Identifier(string name) {     
     58    private string VariableName2Identifier(string name) {
    5959      /*
    6060       * identifier-start-character:
     
    131131        } else if (node.Symbol is Tangent) {
    132132          FormatFunction(node, "Math.Tan", strBuilder);
     133        } else if (node.Symbol is HyperbolicTangent) {
     134          FormatFunction(node, "Math.Tanh", strBuilder);
    133135        } else if (node.Symbol is Square) {
    134136          FormatSquare(node, strBuilder);
    135137        } else if (node.Symbol is SquareRoot) {
    136138          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(")");
    137145        } else if (node.Symbol is Power) {
    138146          FormatFunction(node, "Math.Pow", strBuilder);
    139147        } else if (node.Symbol is Root) {
    140148          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) )");
    141157        } else {
    142158          throw new NotSupportedException("Formatting of symbol: " + node.Symbol + " not supported for C# symbolic expression tree formatter.");
     
    171187
    172188    private void FormatSquare(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) {
     189      FormatPower(node, strBuilder, "2");
     190    }
     191    private void FormatPower(ISymbolicExpressionTreeNode node, StringBuilder strBuilder, string exponent) {
    173192      strBuilder.Append("Math.Pow(");
    174193      FormatRecursively(node.GetSubtree(0), strBuilder);
    175       strBuilder.Append(", 2)");
     194      strBuilder.Append($", {exponent})");
    176195    }
    177196
     
    235254      strBuilder.AppendLine("public static class Model {");
    236255      GenerateAverageSource(strBuilder);
     256      GenerateCbrtSource(strBuilder);
    237257      GenerateIfThenElseSource(strBuilder);
    238258      GenerateFactorSource(strBuilder);
     
    276296      strBuilder.AppendLine("private static double Average(params double[] values) {");
    277297      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);");
    278303      strBuilder.AppendLine("}");
    279304    }
Note: See TracChangeset for help on using the changeset viewer.