Changeset 17860 for branches/3105_PythonFormatter
- Timestamp:
- 03/08/21 16:20:24 (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3105_PythonFormatter/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionPythonFormatter.cs
r17855 r17860 36 36 37 37 private int VariableCounter { get; set; } = 0; 38 private IDictionary<string, string> VariableMap = new Dictionary<string, string>(); 38 private IDictionary<string, string> VariableMap { get; } = new Dictionary<string, string>(); 39 private int MathLibCounter { get; set; } = 0; 40 private int StatisticLibCounter { get; set; } = 0; 41 private int EvaluateIfCounter { get; set; } = 0; 39 42 40 43 [StorableConstructor] … … 65 68 66 69 private void GenerateImports(StringBuilder strBuilder) { 67 strBuilder.AppendLine("# imports"); 68 strBuilder.AppendLine("import math"); 69 strBuilder.AppendLine("import statistics"); 70 if(MathLibCounter > 0 || StatisticLibCounter > 0) 71 strBuilder.AppendLine("# imports"); 72 if(MathLibCounter > 0) 73 strBuilder.AppendLine("import math"); 74 if(StatisticLibCounter > 0) 75 strBuilder.AppendLine("import statistics"); 70 76 } 71 77 72 78 private void GenerateIfThenElseSource(StringBuilder strBuilder) { 73 strBuilder.AppendLine("# condition helper function"); 74 strBuilder.AppendLine("def evaluate_if(condition, then_path, else_path): "); 75 strBuilder.AppendLine("\tif condition:"); 76 strBuilder.AppendLine("\t\treturn then_path"); 77 strBuilder.AppendLine("\telse:"); 78 strBuilder.AppendLine("\t\treturn else_path"); 79 if(EvaluateIfCounter > 0) { 80 strBuilder.AppendLine("# condition helper function"); 81 strBuilder.AppendLine("def evaluate_if(condition, then_path, else_path): "); 82 strBuilder.AppendLine("\tif condition:"); 83 strBuilder.AppendLine("\t\treturn then_path"); 84 strBuilder.AppendLine("\telse:"); 85 strBuilder.AppendLine("\t\treturn else_path"); 86 } 79 87 } 80 88 … … 110 118 FormatNode(node, strBuilder, infixSymbol: " and "); 111 119 } else if (symbol is Average) { 120 StatisticLibCounter++; 112 121 FormatNode(node, strBuilder, prefixSymbol: "statistics.mean", openingSymbol: "([", closingSymbol: "])"); 113 122 } else if (symbol is Cosine) { 123 MathLibCounter++; 114 124 FormatNode(node, strBuilder, "math.cos"); 115 125 } else if (symbol is Division) { 116 126 FormatDivision(node, strBuilder); 117 127 } else if (symbol is Exponential) { 128 MathLibCounter++; 118 129 FormatNode(node, strBuilder, "math.exp"); 119 130 } else if (symbol is GreaterThan) { 120 131 FormatNode(node, strBuilder, infixSymbol: " > "); 121 132 } else if (symbol is IfThenElse) { 133 EvaluateIfCounter++; 122 134 FormatNode(node, strBuilder, "evaluate_if"); 123 135 } else if (symbol is LessThan) { 124 136 FormatNode(node, strBuilder, infixSymbol: " < "); 125 137 } else if (symbol is Logarithm) { 138 MathLibCounter++; 126 139 FormatNode(node, strBuilder, "math.log"); 127 140 } else if (symbol is Multiplication) { … … 134 147 FormatNode(node, strBuilder, infixSymbol: " ^ "); 135 148 } else if (symbol is Sine) { 149 MathLibCounter++; 136 150 FormatNode(node, strBuilder, "math.sin"); 137 151 } else if (symbol is Subtraction) { 138 152 FormatSubtraction(node, strBuilder); 139 153 } else if (symbol is Tangent) { 154 MathLibCounter++; 140 155 FormatNode(node, strBuilder, "math.tan"); 141 156 } else if (symbol is HyperbolicTangent) { 157 MathLibCounter++; 142 158 FormatNode(node, strBuilder, "math.tanh"); 143 159 } else if (symbol is Square) { 144 160 FormatPower(node, strBuilder, "2"); 145 161 } else if (symbol is SquareRoot) { 162 MathLibCounter++; 146 163 FormatNode(node, strBuilder, "math.sqrt"); 147 164 } else if (symbol is Cube) { … … 150 167 FormatNode(node, strBuilder, closingSymbol: " ** (1. / 3))"); 151 168 } else if (symbol is Power) { 169 MathLibCounter++; 152 170 FormatNode(node, strBuilder, "math.pow"); 153 171 } else if (symbol is Root) { … … 156 174 FormatNode(node, strBuilder, "abs"); 157 175 } else if (symbol is AnalyticQuotient) { 176 MathLibCounter++; 158 177 strBuilder.Append("("); 159 178 FormatRecursively(node.GetSubtree(0), strBuilder); … … 188 207 189 208 private void FormatPower(ISymbolicExpressionTreeNode node, StringBuilder strBuilder, string exponent) { 209 MathLibCounter++; 190 210 strBuilder.Append("math.pow("); 191 211 FormatRecursively(node.GetSubtree(0), strBuilder); … … 194 214 195 215 private void FormatRoot(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) { 216 MathLibCounter++; 196 217 strBuilder.Append("math.pow("); 197 218 FormatRecursively(node.GetSubtree(0), strBuilder);
Note: See TracChangeset
for help on using the changeset viewer.