- Timestamp:
- 04/01/19 19:21:36 (6 years ago)
- Location:
- branches/2994-AutoDiffForIntervals
- Files:
-
- 10 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2994-AutoDiffForIntervals
- Property svn:mergeinfo changed
/trunk merged: 16681,16700-16704,16706-16707,16716,16729,16737
- Property svn:mergeinfo changed
-
branches/2994-AutoDiffForIntervals/HeuristicLab.Problems.DataAnalysis.Symbolic
- Property svn:mergeinfo changed
/trunk/HeuristicLab.Problems.DataAnalysis.Symbolic merged: 16700,16702,16737
- Property svn:mergeinfo changed
-
branches/2994-AutoDiffForIntervals/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Converters/DerivativeCalculator.cs
r16565 r16739 83 83 if (branch.SubtreeCount >= 2) { 84 84 var f = (ISymbolicExpressionTreeNode)branch.GetSubtree(0).Clone(); 85 var g = (ISymbolicExpressionTreeNode)branch.GetSubtree(1).Clone();86 85 var fprime = Derive(f, variableName); 87 var gprime = Derive(g, variableName); 88 var fgPrime = Sum(Product(f, gprime), Product(fprime, g)); 89 for (int i = 2; i < branch.SubtreeCount; i++) { 86 for (int i = 1; i < branch.SubtreeCount; i++) { 87 var g = (ISymbolicExpressionTreeNode)branch.GetSubtree(i).Clone(); 90 88 var fg = Product((ISymbolicExpressionTreeNode)f.Clone(), (ISymbolicExpressionTreeNode)g.Clone()); 91 var h = (ISymbolicExpressionTreeNode)branch.GetSubtree(i).Clone(); 92 var hPrime = Derive(h, variableName); 93 fgPrime = Sum(Product(fgPrime, h), Product(fg, hPrime)); 89 var gPrime = Derive(g, variableName); 90 var fgPrime = Sum(Product(fprime, g), Product(gPrime, f)); 91 // prepare for next iteration 92 f = fg; 93 fprime = fgPrime; 94 94 } 95 return f gPrime;95 return fprime; 96 96 } else 97 97 // multiplication with only one argument has no effect -> derive the argument -
branches/2994-AutoDiffForIntervals/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/InfixExpressionFormatter.cs
r16565 r16739 51 51 } 52 52 53 public string Format(ISymbolicExpressionTree symbolicExpressionTree) { 53 /// <summary> 54 /// Produces an infix expression for a given expression tree. 55 /// </summary> 56 /// <param name="symbolicExpressionTree">The tree representation of the expression.</param> 57 /// <param name="numberFormat">Number format that should be used for numeric parameters (e.g. NumberFormatInfo.InvariantInfo (default)).</param> 58 /// <param name="formatString">The format string for numeric parameters (e.g. \"G4\" to limit to 4 digits, default is \"G\")</param> 59 /// <returns>Infix expression</returns> 60 public string Format(ISymbolicExpressionTree symbolicExpressionTree, NumberFormatInfo numberFormat, string formatString="G") { 54 61 // skip root and start symbols 55 62 StringBuilder strBuilder = new StringBuilder(); 56 FormatRecursively(symbolicExpressionTree.Root.GetSubtree(0).GetSubtree(0), strBuilder );63 FormatRecursively(symbolicExpressionTree.Root.GetSubtree(0).GetSubtree(0), strBuilder, numberFormat, formatString); 57 64 return strBuilder.ToString(); 58 65 } 59 66 60 private void FormatRecursively(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) { 67 public string Format(ISymbolicExpressionTree symbolicExpressionTree) { 68 return Format(symbolicExpressionTree, NumberFormatInfo.InvariantInfo); 69 } 70 71 private static void FormatRecursively(ISymbolicExpressionTreeNode node, StringBuilder strBuilder, NumberFormatInfo numberFormat, string formatString) { 61 72 if (node.SubtreeCount > 1) { 62 73 var token = GetToken(node.Symbol); … … 66 77 token == "^") { 67 78 strBuilder.Append("("); 68 FormatRecursively(node.Subtrees.First(), strBuilder );79 FormatRecursively(node.Subtrees.First(), strBuilder, numberFormat, formatString); 69 80 70 81 foreach (var subtree in node.Subtrees.Skip(1)) { 71 82 strBuilder.Append(" ").Append(token).Append(" "); 72 FormatRecursively(subtree, strBuilder );83 FormatRecursively(subtree, strBuilder, numberFormat, formatString); 73 84 } 74 85 strBuilder.Append(")"); … … 76 87 // function with multiple arguments 77 88 strBuilder.Append(token).Append("("); 78 FormatRecursively(node.Subtrees.First(), strBuilder );89 FormatRecursively(node.Subtrees.First(), strBuilder, numberFormat, formatString); 79 90 foreach (var subtree in node.Subtrees.Skip(1)) { 80 91 strBuilder.Append(", "); 81 FormatRecursively(subtree, strBuilder );92 FormatRecursively(subtree, strBuilder, numberFormat, formatString); 82 93 } 83 94 strBuilder.Append(")"); … … 87 98 if (token == "-" || token == "NOT") { 88 99 strBuilder.Append("(").Append(token).Append("("); 89 FormatRecursively(node.GetSubtree(0), strBuilder );100 FormatRecursively(node.GetSubtree(0), strBuilder, numberFormat, formatString); 90 101 strBuilder.Append("))"); 91 102 } else if (token == "/") { 92 103 strBuilder.Append("1/"); 93 FormatRecursively(node.GetSubtree(0), strBuilder );104 FormatRecursively(node.GetSubtree(0), strBuilder, numberFormat, formatString); 94 105 } else if (token == "+" || token == "*") { 95 FormatRecursively(node.GetSubtree(0), strBuilder );106 FormatRecursively(node.GetSubtree(0), strBuilder, numberFormat, formatString); 96 107 } else { 97 108 // function with only one argument 98 109 strBuilder.Append(token).Append("("); 99 FormatRecursively(node.GetSubtree(0), strBuilder );110 FormatRecursively(node.GetSubtree(0), strBuilder, numberFormat, formatString); 100 111 strBuilder.Append(")"); 101 112 } … … 106 117 if (!varNode.Weight.IsAlmost(1.0)) { 107 118 strBuilder.Append("("); 108 strBuilder.Append Format(CultureInfo.InvariantCulture, "{0}", varNode.Weight);119 strBuilder.Append(varNode.Weight.ToString(formatString, numberFormat)); 109 120 strBuilder.Append("*"); 110 121 } … … 116 127 } 117 128 strBuilder.Append(", ") 118 .AppendFormat( CultureInfo.InvariantCulture, "{0}", varNode.Lag)129 .AppendFormat(numberFormat, "{0}", varNode.Lag) 119 130 .Append(")"); 120 131 } else if (node.Symbol is Variable) { … … 122 133 if (!varNode.Weight.IsAlmost(1.0)) { 123 134 strBuilder.Append("("); 124 strBuilder.Append Format(CultureInfo.InvariantCulture, "{0}", varNode.Weight);135 strBuilder.Append(varNode.Weight.ToString(formatString, numberFormat)); 125 136 strBuilder.Append("*"); 126 137 } … … 141 152 } 142 153 strBuilder.AppendFormat("[{0}]", 143 string.Join(", ", factorNode.Weights.Select(w => w.ToString( CultureInfo.InvariantCulture))));154 string.Join(", ", factorNode.Weights.Select(w => w.ToString(formatString, numberFormat)))); 144 155 } else if (node.Symbol is BinaryFactorVariable) { 145 156 var factorNode = node as BinaryFactorVariableTreeNode; 146 157 if (!factorNode.Weight.IsAlmost(1.0)) { 147 158 strBuilder.Append("("); 148 strBuilder.Append Format(CultureInfo.InvariantCulture, "{0}", factorNode.Weight);159 strBuilder.Append(factorNode.Weight.ToString(formatString, numberFormat)); 149 160 strBuilder.Append("*"); 150 161 } … … 168 179 var constNode = node as ConstantTreeNode; 169 180 if (constNode.Value >= 0.0) 170 strBuilder.Append Format(CultureInfo.InvariantCulture, "{0}", constNode.Value);181 strBuilder.Append(constNode.Value.ToString(formatString, numberFormat)); 171 182 else 172 strBuilder.Append Format(CultureInfo.InvariantCulture, "({0})", constNode.Value); // (-1183 strBuilder.Append("(").Append(constNode.Value.ToString(formatString, numberFormat)).Append(")"); // (-1 173 184 } 174 185 } 175 186 } 176 187 177 private st ring GetToken(ISymbol symbol) {188 private static string GetToken(ISymbol symbol) { 178 189 var tok = InfixExpressionParser.knownSymbols.GetBySecond(symbol).FirstOrDefault(); 179 190 if (tok == null) -
branches/2994-AutoDiffForIntervals/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionCSharpFormatter.cs
r16565 r16739 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); -
branches/2994-AutoDiffForIntervals/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionExcelFormatter.cs
r16565 r16739 25 25 using System.Linq; 26 26 using System.Text; 27 using HEAL.Attic; 27 28 using HeuristicLab.Common; 28 29 using HeuristicLab.Core; 29 30 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 30 using HEAL.Attic;31 31 32 32 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { … … 192 192 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 193 193 stringBuilder.Append(")"); 194 194 } else if (symbol is HyperbolicTangent) { 195 stringBuilder.Append("TANH("); 196 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 197 stringBuilder.Append(")"); 195 198 } else if (symbol is Variable) { 196 199 VariableTreeNode variableTreeNode = node as VariableTreeNode; -
branches/2994-AutoDiffForIntervals/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionLatexFormatter.cs
r16565 r16739 24 24 using System.Linq; 25 25 using System.Text; 26 using HEAL.Attic; 26 27 using HeuristicLab.Common; 27 28 using HeuristicLab.Core; 28 29 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 29 using HEAL.Attic;30 30 31 31 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { … … 137 137 } else if (node.Symbol is Tangent) { 138 138 strBuilder.Append(@"\tan \left( "); 139 } else if (node.Symbol is HyperbolicTangent) { 140 strBuilder.Append(@"\tanh \left( "); 139 141 } else if (node.Symbol is AiryA) { 140 142 strBuilder.Append(@"\operatorname{airy}_a \left( "); … … 303 305 } else if (node.Symbol is Tangent) { 304 306 throw new InvalidOperationException(); 307 } else if (node.Symbol is HyperbolicTangent) { 308 throw new InvalidOperationException(); 305 309 } else if (node.Symbol is AiryA) { 306 310 throw new InvalidOperationException(); … … 399 403 } else if (node.Symbol is Tangent) { 400 404 strBuilder.Append(@" \right) "); 405 } else if (node.Symbol is HyperbolicTangent) { 406 strBuilder.Append(@" \right) "); 401 407 } else if (node.Symbol is AiryA) { 402 408 strBuilder.Append(@" \right) "); -
branches/2994-AutoDiffForIntervals/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionMATLABFormatter.cs
r16565 r16739 23 23 using System.Linq; 24 24 using System.Text; 25 using HEAL.Attic; 25 26 using HeuristicLab.Common; 26 27 using HeuristicLab.Core; 27 28 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 28 using HEAL.Attic;29 29 30 30 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { … … 252 252 } else if (symbol is Tangent) { 253 253 stringBuilder.Append("tan("); 254 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 255 stringBuilder.Append(")"); 256 } else if (symbol is HyperbolicTangent) { 257 stringBuilder.Append("tanh("); 254 258 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 255 259 stringBuilder.Append(")"); -
branches/2994-AutoDiffForIntervals/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionMathematicaFormatter.cs
r16565 r16739 24 24 using System.Linq; 25 25 using System.Text; 26 using HEAL.Attic; 26 27 using HeuristicLab.Common; 27 28 using HeuristicLab.Core; 28 29 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 29 using HEAL.Attic;30 30 31 31 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { … … 70 70 } else if (node.Symbol is Tangent) { 71 71 FormatFunction(node, "Tan", strBuilder); 72 } else if (node.Symbol is HyperbolicTangent) { 73 FormatFunction(node, "Tanh", strBuilder); 72 74 } else if (node.Symbol is Exponential) { 73 75 FormatFunction(node, "Exp", strBuilder); -
branches/2994-AutoDiffForIntervals/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionSmalltalkFormatter.cs
r16565 r16739 160 160 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 161 161 stringBuilder.Append(" tan"); 162 } else if (symbol is HyperbolicTangent) { 163 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 164 stringBuilder.Append(" tanh"); 162 165 } else if (symbol is Variable) { 163 166 VariableTreeNode variableTreeNode = node as VariableTreeNode;
Note: See TracChangeset
for help on using the changeset viewer.