- Timestamp:
- 12/15/21 11:50:57 (3 years ago)
- Location:
- trunk
- Files:
-
- 11 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk
- Property svn:mergeinfo changed
/branches/3140_NumberSymbol (added) merged: 18091,18093,18100,18112-18121,18123-18131
- Property svn:mergeinfo changed
-
trunk/HeuristicLab.Problems.DataAnalysis.Symbolic
- Property svn:mergeinfo changed
/branches/3140_NumberSymbol/HeuristicLab.Problems.DataAnalysis.Symbolic (added) merged: 18093,18100,18112-18116,18118,18121,18123-18124,18129-18130
- Property svn:mergeinfo changed
-
trunk/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/InfixExpressionFormatter.cs
r17811 r18132 33 33 public static class BaseInfixExpressionFormatter { 34 34 public static void FormatRecursively(ISymbolicExpressionTreeNode node, StringBuilder strBuilder, 35 NumberFormatInfo numberFormat, string formatString, List<KeyValuePair<string, double>> constants = null) {35 NumberFormatInfo numberFormat, string formatString, List<KeyValuePair<string, double>> parameters = null) { 36 36 if (node.SubtreeCount > 1) { 37 37 var token = GetToken(node.Symbol); … … 40 40 token == "*" || token == "/" || token == "AND") { 41 41 strBuilder.Append("("); 42 FormatRecursively(node.Subtrees.First(), strBuilder, numberFormat, formatString, constants);42 FormatRecursively(node.Subtrees.First(), strBuilder, numberFormat, formatString, parameters); 43 43 44 44 foreach (var subtree in node.Subtrees.Skip(1)) { 45 45 strBuilder.Append(" ").Append(token).Append(" "); 46 FormatRecursively(subtree, strBuilder, numberFormat, formatString, constants);46 FormatRecursively(subtree, strBuilder, numberFormat, formatString, parameters); 47 47 } 48 48 … … 51 51 // handle integer powers directly 52 52 strBuilder.Append("("); 53 FormatRecursively(node.Subtrees.First(), strBuilder, numberFormat, formatString, constants);53 FormatRecursively(node.Subtrees.First(), strBuilder, numberFormat, formatString, parameters); 54 54 55 55 var power = node.GetSubtree(1); 56 if(power is ConstantTreeNode constNode && Math.Truncate(constNode.Value) == constNode.Value) {57 strBuilder.Append(" ").Append(token).Append(" ").Append( constNode.Value.ToString(formatString, numberFormat));56 if(power is INumericTreeNode numNode && Math.Truncate(numNode.Value) == numNode.Value) { 57 strBuilder.Append(" ").Append(token).Append(" ").Append(numNode.Value.ToString(formatString, numberFormat)); 58 58 } else { 59 59 strBuilder.Append(" ").Append(token).Append(" "); 60 FormatRecursively(power, strBuilder, numberFormat, formatString, constants);60 FormatRecursively(power, strBuilder, numberFormat, formatString, parameters); 61 61 } 62 62 … … 65 65 // function with multiple arguments 66 66 strBuilder.Append(token).Append("("); 67 FormatRecursively(node.Subtrees.First(), strBuilder, numberFormat, formatString, constants);67 FormatRecursively(node.Subtrees.First(), strBuilder, numberFormat, formatString, parameters); 68 68 foreach (var subtree in node.Subtrees.Skip(1)) { 69 69 strBuilder.Append(", "); 70 FormatRecursively(subtree, strBuilder, numberFormat, formatString, constants);70 FormatRecursively(subtree, strBuilder, numberFormat, formatString, parameters); 71 71 } 72 72 … … 77 77 if (token == "-" || token == "NOT") { 78 78 strBuilder.Append("(").Append(token).Append("("); 79 FormatRecursively(node.GetSubtree(0), strBuilder, numberFormat, formatString, constants);79 FormatRecursively(node.GetSubtree(0), strBuilder, numberFormat, formatString, parameters); 80 80 strBuilder.Append("))"); 81 81 } else if (token == "/") { 82 82 strBuilder.Append("1/"); 83 FormatRecursively(node.GetSubtree(0), strBuilder, numberFormat, formatString, constants);83 FormatRecursively(node.GetSubtree(0), strBuilder, numberFormat, formatString, parameters); 84 84 } else if (token == "+" || token == "*") { 85 FormatRecursively(node.GetSubtree(0), strBuilder, numberFormat, formatString, constants);85 FormatRecursively(node.GetSubtree(0), strBuilder, numberFormat, formatString, parameters); 86 86 } else { 87 87 // function with only one argument 88 88 strBuilder.Append(token).Append("("); 89 FormatRecursively(node.GetSubtree(0), strBuilder, numberFormat, formatString, constants);89 FormatRecursively(node.GetSubtree(0), strBuilder, numberFormat, formatString, parameters); 90 90 strBuilder.Append(")"); 91 91 } … … 96 96 if (!varNode.Weight.IsAlmost(1.0)) { 97 97 strBuilder.Append("("); 98 Append Constant(strBuilder, constants, varNode.Weight, formatString, numberFormat);98 AppendNumber(strBuilder, parameters, varNode.Weight, formatString, numberFormat); 99 99 strBuilder.Append("*"); 100 100 } … … 110 110 if (!varNode.Weight.IsAlmost(1.0)) { 111 111 strBuilder.Append("("); 112 Append Constant(strBuilder, constants, varNode.Weight, formatString, numberFormat);112 AppendNumber(strBuilder, parameters, varNode.Weight, formatString, numberFormat); 113 113 strBuilder.Append("*"); 114 114 } … … 124 124 for (int i = 0; i < factorNode.Weights.Length; i++) { 125 125 if (i > 0) strBuilder.Append(", "); 126 Append Constant(strBuilder, constants, factorNode.Weights[i], formatString, numberFormat);126 AppendNumber(strBuilder, parameters, factorNode.Weights[i], formatString, numberFormat); 127 127 } 128 128 strBuilder.Append("]"); … … 131 131 if (!factorNode.Weight.IsAlmost(1.0)) { 132 132 strBuilder.Append("("); 133 Append Constant(strBuilder, constants, factorNode.Weight, formatString, numberFormat);133 AppendNumber(strBuilder, parameters, factorNode.Weight, formatString, numberFormat); 134 134 135 135 strBuilder.Append("*"); … … 141 141 142 142 if (!factorNode.Weight.IsAlmost(1.0)) strBuilder.Append(")"); 143 } else if (node .Symbol is Constant) {144 var constNode = node as ConstantTreeNode;145 if (constants == null && constNode.Value < 0) {146 strBuilder.Append("(").Append( constNode.Value.ToString(formatString, numberFormat))147 .Append(")"); // (-1143 } else if (node is INumericTreeNode numNode) { 144 if (parameters == null && numNode.Value < 0) { 145 // negative value 146 strBuilder.Append("(").Append(numNode.Value.ToString(formatString, numberFormat)) 147 .Append(")"); 148 148 } else { 149 Append Constant(strBuilder, constants, constNode.Value, formatString, numberFormat);149 AppendNumber(strBuilder, parameters, numNode.Value, formatString, numberFormat); 150 150 } 151 151 } … … 153 153 } 154 154 155 private static void Append Constant(StringBuilder strBuilder, List<KeyValuePair<string, double>> constants, double value, string formatString, NumberFormatInfo numberFormat) {156 if ( constants != null) {157 string constantKey = $"c_{constants.Count}";158 strBuilder.AppendFormat(CultureInfo.InvariantCulture, "{0}", constantKey);159 constants.Add(new KeyValuePair<string, double>(constantKey, value));155 private static void AppendNumber(StringBuilder strBuilder, List<KeyValuePair<string, double>> parameters, double value, string formatString, NumberFormatInfo numberFormat) { 156 if (parameters != null) { 157 string paramKey = $"c_{parameters.Count}"; 158 strBuilder.AppendFormat(CultureInfo.InvariantCulture, "{0}", paramKey); 159 parameters.Add(new KeyValuePair<string, double>(paramKey, value)); 160 160 } else { 161 161 strBuilder.Append(value.ToString(formatString, numberFormat)); … … 204 204 /// </summary> 205 205 /// <param name="symbolicExpressionTree">The tree representation of the expression.</param> 206 /// <param name="numberFormat">Number format that should be used for numericparameters (e.g. NumberFormatInfo.InvariantInfo (default)).</param>207 /// <param name="formatString">The format string for numericparameters (e.g. \"G4\" to limit to 4 digits, default is \"G\")</param>206 /// <param name="numberFormat">Number format that should be used for parameters (e.g. NumberFormatInfo.InvariantInfo (default)).</param> 207 /// <param name="formatString">The format string for parameters (e.g. \"G4\" to limit to 4 digits, default is \"G\")</param> 208 208 /// <returns>Infix expression</returns> 209 209 public string Format(ISymbolicExpressionTree symbolicExpressionTree, NumberFormatInfo numberFormat, … … 241 241 public string Format(ISymbolicExpressionTree symbolicExpressionTree) { 242 242 StringBuilder strBuilder = new StringBuilder(); 243 var constants = new List<KeyValuePair<string, double>>();243 var parameters = new List<KeyValuePair<string, double>>(); 244 244 BaseInfixExpressionFormatter.FormatRecursively(symbolicExpressionTree.Root.GetSubtree(0).GetSubtree(0), 245 strBuilder, NumberFormatInfo.InvariantInfo, "G", constants);245 strBuilder, NumberFormatInfo.InvariantInfo, "G", parameters); 246 246 strBuilder.Append($"{Environment.NewLine}{Environment.NewLine}"); 247 247 248 int maxDigits = GetDigits( constants.Count);249 int padding = constants.Max(x => x.Value.ToString("F12", CultureInfo.InvariantCulture).Length);250 foreach (var constant in constants) {251 int digits = GetDigits( Int32.Parse(constant.Key.Substring(2)));252 strBuilder.Append($"{ constant.Key}{new String(' ', maxDigits - digits)} = " +253 string.Format($"{{0,{padding}:F12}}", constant.Value, CultureInfo.InvariantCulture) +248 int maxDigits = GetDigits(parameters.Count); 249 int padding = parameters.Max(x => x.Value.ToString("F12", CultureInfo.InvariantCulture).Length); 250 foreach (var param in parameters) { 251 int digits = GetDigits(int.Parse(param.Key.Substring(2))); 252 strBuilder.Append($"{param.Key}{new string(' ', maxDigits - digits)} = " + 253 string.Format($"{{0,{padding}:F12}}", param.Value, CultureInfo.InvariantCulture) + 254 254 Environment.NewLine); 255 255 } -
trunk/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionCSharpFormatter.cs
r17648 r18132 162 162 var varNode = node as VariableTreeNode; 163 163 strBuilder.AppendFormat("{0} * {1}", VariableName2Identifier(varNode.VariableName), varNode.Weight.ToString("g17", CultureInfo.InvariantCulture)); 164 } else if (node is ConstantTreeNode) { 165 var constNode = node as ConstantTreeNode; 166 strBuilder.Append(constNode.Value.ToString("g17", CultureInfo.InvariantCulture)); 164 } else if (node is INumericTreeNode numNode) { 165 strBuilder.Append(numNode.Value.ToString("g17", CultureInfo.InvariantCulture)); 167 166 } else if (node.Symbol is FactorVariable) { 168 167 var factorNode = node as FactorVariableTreeNode; … … 311 310 312 311 private void GenerateFactorSource(StringBuilder strBuilder) { 313 strBuilder.AppendLine("private static double EvaluateFactor(string factorValue, string[] factorValues, double[] constants) {");312 strBuilder.AppendLine("private static double EvaluateFactor(string factorValue, string[] factorValues, double[] parameters) {"); 314 313 strBuilder.AppendLine(" for(int i=0;i<factorValues.Length;i++) " + 315 " if(factorValues[i] == factorValue) return constants[i];" +314 " if(factorValues[i] == factorValue) return parameters[i];" + 316 315 " throw new ArgumentException();"); 317 316 strBuilder.AppendLine("}"); -
trunk/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionExcelFormatter.cs
r17180 r18132 47 47 private string GetExcelColumnName(int columnNumber) { 48 48 int dividend = columnNumber; 49 string columnName = String.Empty;49 string columnName = string.Empty; 50 50 51 51 while (dividend > 0) { 52 52 int modulo = (dividend - 1) % 26; 53 53 columnName = System.Convert.ToChar(65 + modulo) + columnName; 54 dividend = ( int)((dividend - modulo) / 26);54 dividend = (dividend - modulo) / 26; 55 55 } 56 56 … … 134 134 } 135 135 stringBuilder.Append("))"); 136 } else if (symbol is Constant) {137 ConstantTreeNode constantTreeNode = node as ConstantTreeNode;138 stringBuilder.Append( constantTreeNode.Value.ToString(CultureInfo.InvariantCulture));136 } else if (symbol is INumericSymbol numSy) { 137 var numTreeNode = node as INumericTreeNode; 138 stringBuilder.Append(numTreeNode.Value.ToString(CultureInfo.InvariantCulture)); 139 139 } else if (symbol is Cosine) { 140 140 stringBuilder.Append("COS("); -
trunk/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionLatexFormatter.cs
r17826 r18132 33 33 [StorableType("D7186DFF-1596-4A58-B27D-974DF0D93E4F")] 34 34 public sealed class SymbolicDataAnalysisExpressionLatexFormatter : NamedItem, ISymbolicExpressionTreeStringFormatter { 35 private readonly List<KeyValuePair<string, double>> constants;36 private int constIndex;35 private readonly List<KeyValuePair<string, double>> parameters; 36 private int paramIdx; 37 37 private int targetCount; 38 38 private int currentLag; … … 44 44 private SymbolicDataAnalysisExpressionLatexFormatter(SymbolicDataAnalysisExpressionLatexFormatter original, Cloner cloner) 45 45 : base(original, cloner) { 46 constants = new List<KeyValuePair<string, double>>(original.constants);47 constIndex = original.constIndex;46 parameters = new List<KeyValuePair<string, double>>(original.parameters); 47 paramIdx = original.paramIdx; 48 48 currentLag = original.currentLag; 49 49 targetCount = original.targetCount; … … 53 53 Name = ItemName; 54 54 Description = ItemDescription; 55 constants = new List<KeyValuePair<string, double>>();55 parameters = new List<KeyValuePair<string, double>>(); 56 56 } 57 57 … … 66 66 try { 67 67 StringBuilder strBuilder = new StringBuilder(); 68 constants.Clear();69 constIndex = 0;68 parameters.Clear(); 69 paramIdx = 0; 70 70 this.targetVariable = targetVariable; 71 71 containsTimeSeriesSymbol = symbolicExpressionTree.IterateNodesBreadth().Any(n => IsTimeSeriesSymbol(n.Symbol)); … … 189 189 } else if (node.Symbol is IfThenElse) { 190 190 strBuilder.Append(@" \operatorname{if} \left( "); 191 } else if (node.Symbol is Constant) {192 var constName = "c_{" + constIndex + "}";193 constIndex++;194 var constNode = node as ConstantTreeNode;195 if ( constNode.Value.IsAlmost(1.0)) {191 } else if (node.Symbol is INumericSymbol numSy) { 192 var numName = "c_{" + paramIdx + "}"; 193 paramIdx++; 194 var numericNode = node as INumericTreeNode; 195 if (numericNode.Value.IsAlmost(1.0)) { 196 196 strBuilder.Append("1 "); 197 197 } else { 198 strBuilder.Append(constName); 199 constants.Add(new KeyValuePair<string, double>(constName, constNode.Value)); 200 } 201 198 strBuilder.Append(numName); 199 parameters.Add(new KeyValuePair<string, double>(numName, numericNode.Value)); 200 } 202 201 } else if (node.Symbol is FactorVariable) { 203 202 var factorNode = node as FactorVariableTreeNode; 204 var constName = "c_{" + constIndex + "}";205 strBuilder.Append( constName + " ");203 var paramName = "c_{" + paramIdx + "}"; 204 strBuilder.Append(paramName + " "); 206 205 foreach (var e in factorNode.Symbol.GetVariableValues(factorNode.VariableName) 207 206 .Zip(factorNode.Weights, Tuple.Create)) { 208 constants.Add(new KeyValuePair<string, double>("c_{" + constIndex + ", " + EscapeLatexString(factorNode.VariableName) + "=" + EscapeLatexString(e.Item1) + "}", e.Item2));209 } 210 constIndex++;207 parameters.Add(new KeyValuePair<string, double>("c_{" + paramIdx + ", " + EscapeLatexString(factorNode.VariableName) + "=" + EscapeLatexString(e.Item1) + "}", e.Item2)); 208 } 209 paramIdx++; 211 210 } else if (node.Symbol is BinaryFactorVariable) { 212 211 var binFactorNode = node as BinaryFactorVariableTreeNode; 213 212 if (!binFactorNode.Weight.IsAlmost((1.0))) { 214 var constName = "c_{" + constIndex + "}";215 strBuilder.Append( constName + " \\cdot");216 constants.Add(new KeyValuePair<string, double>(constName, binFactorNode.Weight));217 constIndex++;213 var paramName = "c_{" + paramIdx + "}"; 214 strBuilder.Append(paramName + " \\cdot"); 215 parameters.Add(new KeyValuePair<string, double>(paramName, binFactorNode.Weight)); 216 paramIdx++; 218 217 } 219 218 strBuilder.Append("(" + EscapeLatexString(binFactorNode.VariableName)); … … 223 222 var laggedVarNode = node as LaggedVariableTreeNode; 224 223 if (!laggedVarNode.Weight.IsAlmost(1.0)) { 225 var constName = "c_{" + constIndex + "}";226 strBuilder.Append( constName + " \\cdot");227 constants.Add(new KeyValuePair<string, double>(constName, laggedVarNode.Weight));228 constIndex++;224 var paramName = "c_{" + paramIdx + "}"; 225 strBuilder.Append(paramName + " \\cdot"); 226 parameters.Add(new KeyValuePair<string, double>(paramName, laggedVarNode.Weight)); 227 paramIdx++; 229 228 } 230 229 strBuilder.Append(EscapeLatexString(laggedVarNode.VariableName)); … … 234 233 var varNode = node as VariableTreeNode; 235 234 if (!varNode.Weight.IsAlmost((1.0))) { 236 var constName = "c_{" + constIndex + "}";237 strBuilder.Append( constName + " \\cdot");238 constants.Add(new KeyValuePair<string, double>(constName, varNode.Weight));239 constIndex++;235 var paramName = "c_{" + paramIdx + "}"; 236 strBuilder.Append(paramName + " \\cdot"); 237 parameters.Add(new KeyValuePair<string, double>(paramName, varNode.Weight)); 238 paramIdx++; 240 239 } 241 240 strBuilder.Append(EscapeLatexString(varNode.VariableName)); … … 271 270 } else if (node.Symbol is VariableCondition) { 272 271 var conditionTreeNode = node as VariableConditionTreeNode; 273 var constName = "c_{" + constants.Count + "}";274 string p = @"1 / 1 + \exp - " + constName + " ";275 constants.Add(new KeyValuePair<string, double>(constName, conditionTreeNode.Slope));276 constIndex++;277 var const2Name = "c_{" + constants.Count + @"}";272 var paramName = "c_{" + parameters.Count + "}"; 273 string p = @"1 / 1 + \exp - " + paramName + " "; 274 parameters.Add(new KeyValuePair<string, double>(paramName, conditionTreeNode.Slope)); 275 paramIdx++; 276 var const2Name = "c_{" + parameters.Count + @"}"; 278 277 p += @" \cdot " + EscapeLatexString(conditionTreeNode.VariableName) + LagToString(currentLag) + " - " + const2Name + " "; 279 constants.Add(new KeyValuePair<string, double>(const2Name, conditionTreeNode.Threshold));280 constIndex++;278 parameters.Add(new KeyValuePair<string, double>(const2Name, conditionTreeNode.Threshold)); 279 paramIdx++; 281 280 strBuilder.Append(@" \left( " + p + @"\cdot "); 282 281 } else { … … 379 378 } else if (node.Symbol is VariableCondition) { 380 379 var conditionTreeNode = node as VariableConditionTreeNode; 381 var const1Name = "c_{" + constants.Count + "}";380 var const1Name = "c_{" + parameters.Count + "}"; 382 381 string p = @"1 / \left( 1 + \exp \left( - " + const1Name + " "; 383 constants.Add(new KeyValuePair<string, double>(const1Name, conditionTreeNode.Slope));384 constIndex++;385 var const2Name = "c_{" + constants.Count + "}";382 parameters.Add(new KeyValuePair<string, double>(const1Name, conditionTreeNode.Slope)); 383 paramIdx++; 384 var const2Name = "c_{" + parameters.Count + "}"; 386 385 p += @" \cdot " + EscapeLatexString(conditionTreeNode.VariableName) + LagToString(currentLag) + " - " + const2Name + " \right) \right) \right) "; 387 constants.Add(new KeyValuePair<string, double>(const2Name, conditionTreeNode.Threshold));388 constIndex++;386 parameters.Add(new KeyValuePair<string, double>(const2Name, conditionTreeNode.Threshold)); 387 paramIdx++; 389 388 strBuilder.Append(@" + \left( 1 - " + p + @" \right) \cdot "); 390 389 } else { … … 471 470 } else if (node.Symbol is IfThenElse) { 472 471 strBuilder.Append(@" \right) "); 472 } else if (node.Symbol is Number) { 473 473 } else if (node.Symbol is Constant) { 474 474 } else if (node.Symbol is LaggedVariable) { … … 481 481 .AppendLine("\\begin{align*}") 482 482 .AppendLine("\\nonumber"); 483 // output all constantvalues484 if ( constants.Count > 0) {485 foreach (var constant in constants) {483 // output all parameter values 484 if (parameters.Count > 0) { 485 foreach (var param in parameters) { 486 486 // replace "." with ".&" to align decimal points 487 var constStr = string.Format(System.Globalization.NumberFormatInfo.InvariantInfo, "{0:G5}", constant.Value);488 if (! constStr.Contains(".")) constStr = constStr + ".0";489 constStr = constStr.Replace(".", "&."); // fix problem in rendering of aligned expressions490 strBuilder.Append( constant.Key + "& = & " + constStr);487 var paramStr = string.Format(System.Globalization.NumberFormatInfo.InvariantInfo, "{0:G5}", param.Value); 488 if (!paramStr.Contains(".")) paramStr = paramStr + ".0"; 489 paramStr = paramStr.Replace(".", "&."); // fix problem in rendering of aligned expressions 490 strBuilder.Append(param.Key + "& = & " + paramStr); 491 491 strBuilder.Append(@"\\"); 492 492 } -
trunk/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionMATLABFormatter.cs
r17180 r18132 151 151 } 152 152 stringBuilder.Append(")"); 153 } else if (symbol is Constant) {154 ConstantTreeNode constantTreeNode = node as ConstantTreeNode;155 stringBuilder.Append( constantTreeNode.Value.ToString(CultureInfo.InvariantCulture));153 } else if (symbol is INumericSymbol) { 154 var numberTreeNode = node as INumericTreeNode; 155 stringBuilder.Append(numberTreeNode.Value.ToString(CultureInfo.InvariantCulture)); 156 156 } else if (symbol is Cosine) { 157 157 stringBuilder.Append("cos("); -
trunk/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionMathematicaFormatter.cs
r17180 r18132 130 130 var varNode = node as VariableTreeNode; 131 131 strBuilder.AppendFormat("Times[{0}, {1}]", varNode.VariableName, varNode.Weight.ToString("G17", CultureInfo.InvariantCulture)); 132 } else if (node.Symbol is Constant) {133 var constNode = node as ConstantTreeNode;134 strBuilder.Append( constNode.Value.ToString("G17", CultureInfo.InvariantCulture));132 } else if (node.Symbol is INumericSymbol) { 133 var numNode = node as INumericTreeNode; 134 strBuilder.Append(numNode.Value.ToString("G17", CultureInfo.InvariantCulture)); 135 135 } else if (node.Symbol is FactorVariable) { 136 136 var factorNode = node as FactorVariableTreeNode; -
trunk/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionPythonFormatter.cs
r17929 r18132 202 202 else if (node is VariableTreeNode) 203 203 FormatVariableTreeNode(node, strBuilder); 204 else if (node is ConstantTreeNode)205 Format ConstantTreeNode(node, strBuilder);204 else if (node is INumericTreeNode) 205 FormatNumericTreeNode(node, strBuilder); 206 206 else 207 207 throw new NotSupportedException("Formatting of symbol: " + symbol + " not supported for Python symbolic expression tree formatter."); … … 227 227 } 228 228 229 private static void FormatConstantTreeNode(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) { 230 var constNode = node as ConstantTreeNode; 231 strBuilder.Append(constNode.Value.ToString("g17", CultureInfo.InvariantCulture)); 229 private static void FormatNumericTreeNode(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) { 230 var symbol = node.Symbol; 231 if (node is INumericTreeNode numNode) { 232 strBuilder.Append(numNode.Value.ToString("g17", CultureInfo.InvariantCulture)); 233 } else { 234 throw new NotSupportedException("Formatting of symbol: " + symbol + " not supported for Python symbolic expression tree formatter."); 235 } 232 236 } 233 237 -
trunk/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionSmalltalkFormatter.cs
r17180 r18132 83 83 } 84 84 stringBuilder.Append(")"); 85 } else if (symbol is Constant) {86 ConstantTreeNode constantTreeNode = node as ConstantTreeNode;87 stringBuilder.Append( constantTreeNode.Value.ToString(CultureInfo.InvariantCulture));85 } else if (symbol is INumericSymbol) { 86 var numberTreeNode = node as INumericTreeNode; 87 stringBuilder.Append(numberTreeNode.Value.ToString(CultureInfo.InvariantCulture)); 88 88 } else if (symbol is Cosine) { 89 89 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); -
trunk/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/TSQLExpressionFormatter.cs
r17180 r18132 93 93 //Generate function header 94 94 strBuilder.Append("CREATE FUNCTION dbo.REGRESSIONMODEL("); 95 strBuilder.Append(string.Join(", ", sortedVarcharIdentifiers.Select(n => string.Format("{0} NVARCHAR(max)", n))));95 strBuilder.Append(string.Join(", ", sortedVarcharIdentifiers.Select(n => string.Format("{0} NVARCHAR(max)", n)))); 96 96 if (varcharVarNames.Any() && floatVarNames.Any()) 97 97 strBuilder.Append(","); 98 strBuilder.Append(string.Join(", ", sortedFloatIdentifiers.Select(n => string.Format("{0} FLOAT", n))));98 strBuilder.Append(string.Join(", ", sortedFloatIdentifiers.Select(n => string.Format("{0} FLOAT", n)))); 99 99 strBuilder.AppendLine(")"); 100 100 … … 164 164 var varNode = node as VariableTreeNode; 165 165 strBuilder.AppendFormat("{0} * {1}", VariableName2Identifier(varNode.VariableName), varNode.Weight.ToString("g17", CultureInfo.InvariantCulture)); 166 } else if (node is ConstantTreeNode) { 167 var constNode = node as ConstantTreeNode; 168 strBuilder.Append(constNode.Value.ToString("g17", CultureInfo.InvariantCulture)); 166 } else if (node is INumericTreeNode numNode) { 167 strBuilder.Append(numNode.Value.ToString("g17", CultureInfo.InvariantCulture)); 169 168 } else if (node.Symbol is FactorVariable) { 170 169 var factorNode = node as FactorVariableTreeNode; … … 181 180 private void FormatIfThenElse(int level, ISymbolicExpressionTreeNode node, StringBuilder strBuilder) { 182 181 strBuilder.Append("CASE ISNULL((SELECT 1 WHERE"); 183 FormatRecursively(level, node.GetSubtree(0), strBuilder);182 FormatRecursively(level, node.GetSubtree(0), strBuilder); 184 183 strBuilder.AppendLine("),0)"); 185 strBuilder.AppendIndented(level, "WHEN 1 THEN ");184 strBuilder.AppendIndented(level, "WHEN 1 THEN "); 186 185 FormatRecursively(level, node.GetSubtree(1), strBuilder); 187 186 strBuilder.AppendLine(); … … 203 202 204 203 private string VariableName2Identifier(string variableName) { 205 return "@" +variableName.Replace(' ', '_');204 return "@" + variableName.Replace(' ', '_'); 206 205 } 207 206
Note: See TracChangeset
for help on using the changeset viewer.