Changeset 17193 for branches/2974_Constants_Optimization/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionCSharpFormatter.cs
- Timestamp:
- 08/07/19 13:32:09 (5 years ago)
- Location:
- branches/2974_Constants_Optimization/HeuristicLab.Problems.DataAnalysis.Symbolic
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2974_Constants_Optimization/HeuristicLab.Problems.DataAnalysis.Symbolic
- Property svn:mergeinfo changed
-
branches/2974_Constants_Optimization/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionCSharpFormatter.cs
r16676 r17193 1 1 #region License Information 2 2 /* HeuristicLab 3 * Copyright (C) 2002-2019Heuristic and Evolutionary Algorithms Laboratory (HEAL)3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 4 * 5 5 * This file is part of HeuristicLab. … … 26 26 using System.Text; 27 27 using System.Text.RegularExpressions; 28 using HEAL.Attic; 28 29 using HeuristicLab.Common; 29 30 using HeuristicLab.Core; 30 31 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 31 using HEAL.Attic;32 32 33 33 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { … … 56 56 } 57 57 58 private string VariableName2Identifier(string name) { 58 private string VariableName2Identifier(string name) { 59 59 /* 60 60 * identifier-start-character: … … 131 131 } else if (node.Symbol is Tangent) { 132 132 FormatFunction(node, "Math.Tan", strBuilder); 133 } else if (node.Symbol is HyperbolicTangent) { 134 FormatFunction(node, "Math.Tanh", strBuilder); 133 135 } else if (node.Symbol is Square) { 134 136 FormatSquare(node, strBuilder); 135 137 } else if (node.Symbol is SquareRoot) { 136 138 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(")"); 137 145 } else if (node.Symbol is Power) { 138 146 FormatFunction(node, "Math.Pow", strBuilder); 139 147 } else if (node.Symbol is Root) { 140 148 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) )"); 141 157 } else { 142 158 throw new NotSupportedException("Formatting of symbol: " + node.Symbol + " not supported for C# symbolic expression tree formatter."); … … 171 187 172 188 private void FormatSquare(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) { 189 FormatPower(node, strBuilder, "2"); 190 } 191 private void FormatPower(ISymbolicExpressionTreeNode node, StringBuilder strBuilder, string exponent) { 173 192 strBuilder.Append("Math.Pow("); 174 193 FormatRecursively(node.GetSubtree(0), strBuilder); 175 strBuilder.Append( ", 2)");194 strBuilder.Append($", {exponent})"); 176 195 } 177 196 … … 235 254 strBuilder.AppendLine("public static class Model {"); 236 255 GenerateAverageSource(strBuilder); 256 GenerateCbrtSource(strBuilder); 237 257 GenerateIfThenElseSource(strBuilder); 238 258 GenerateFactorSource(strBuilder); … … 276 296 strBuilder.AppendLine("private static double Average(params double[] values) {"); 277 297 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);"); 278 303 strBuilder.AppendLine("}"); 279 304 }
Note: See TracChangeset
for help on using the changeset viewer.