Changeset 17066 for stable/HeuristicLab.Problems.DataAnalysis.Symbolic
- Timestamp:
- 07/04/19 15:15:58 (5 years ago)
- Location:
- stable/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4
- Files:
-
- 2 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
stable/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Converters/DerivativeCalculator.cs
r16207 r17066 21 21 22 22 using System; 23 using System.Collections.Generic; 23 24 using System.Linq; 24 25 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; … … 27 28 public static class DerivativeCalculator { 28 29 public static ISymbolicExpressionTree Derive(ISymbolicExpressionTree tree, string variableName) { 30 if (tree.Root.SubtreeCount != 1) 31 throw new NotImplementedException("Derive is not implemented for symbolic expressions with automatically defined functions (ADF)"); 32 if (tree.Root.GetSubtree(0).SubtreeCount != 1) 33 throw new NotImplementedException("Derive is not implemented for multi-variate symbolic expressions"); 29 34 var mainBranch = tree.Root.GetSubtree(0).GetSubtree(0); 30 35 var root = new ProgramRootSymbol().CreateTreeNode(); 31 36 root.AddSubtree(new StartSymbol().CreateTreeNode()); 32 37 var dTree = TreeSimplifier.GetSimplifiedTree(Derive(mainBranch, variableName)); 33 // 38 //var dTree = Derive(mainBranch, variableName); 34 39 root.GetSubtree(0).AddSubtree(dTree); 35 40 return new SymbolicExpressionTree(root); 36 41 } 37 42 38 private static Constant constantSy = new Constant(); 39 private static Addition addSy = new Addition(); 40 private static Subtraction subSy = new Subtraction(); 41 private static Multiplication mulSy = new Multiplication(); 42 private static Division divSy = new Division(); 43 private static readonly Constant constantSy = new Constant(); 44 private static readonly Addition addSy = new Addition(); 45 private static readonly Subtraction subSy = new Subtraction(); 46 private static readonly Multiplication mulSy = new Multiplication(); 47 private static readonly Division divSy = new Division(); 48 private static readonly Cosine cosSy = new Cosine(); 49 private static readonly Square sqrSy = new Square(); 43 50 44 51 public static ISymbolicExpressionTreeNode Derive(ISymbolicExpressionTreeNode branch, string variableName) { … … 85 92 } 86 93 return fgPrime; 87 } else throw new ArgumentException(); 94 } else 95 // multiplication with only one argument has no effect -> derive the argument 96 return Derive(branch.GetSubtree(0), variableName); 88 97 } 89 98 if (branch.Symbol is Division) { … … 95 104 sqrNode.AddSubtree(g); 96 105 return Div(gPrime, sqrNode); 97 } else if (branch.SubtreeCount == 2) { 106 } else { 107 // for two subtrees: 108 // (f/g)' = (f'g - fg')/g² 109 110 // if there are more than 2 subtrees 111 // div(x,y,z) is interpretered as (x/y)/z 112 // which is the same as x / (y*z) 113 114 // --> make a product of all but the first subtree and differentiate as for the 2-argument case above 98 115 var f = (ISymbolicExpressionTreeNode)branch.GetSubtree(0).Clone(); 99 var g = (ISymbolicExpressionTreeNode)branch.GetSubtree(1).Clone();116 var g = Product(branch.Subtrees.Skip(1).Select(n => (ISymbolicExpressionTreeNode)n.Clone())); 100 117 var fprime = Derive(f, variableName); 101 118 var gprime = Derive(g, variableName); 102 var sqrNode = new Square().CreateTreeNode(); 103 sqrNode.AddSubtree((ISymbolicExpressionTreeNode)branch.GetSubtree(1).Clone()); 104 return Div(Subtract(Product(fprime, g), Product(f, gprime)), sqrNode); 105 } else throw new NotSupportedException(); 119 var gSqr = Square(g); 120 return Div(Subtract(Product(fprime, g), Product(f, gprime)), gSqr); 121 } 106 122 } 107 123 if (branch.Symbol is Logarithm) { … … 113 129 return Product(f, Derive(branch.GetSubtree(0), variableName)); 114 130 } 115 if (branch.Symbol is Square) {131 if (branch.Symbol is Square) { 116 132 var f = (ISymbolicExpressionTreeNode)branch.GetSubtree(0).Clone(); 117 133 return Product(Product(CreateConstant(2.0), f), Derive(f, variableName)); 118 } 119 if (branch.Symbol is SquareRoot) {134 } 135 if (branch.Symbol is SquareRoot) { 120 136 var f = (ISymbolicExpressionTreeNode)branch.Clone(); 121 137 var u = (ISymbolicExpressionTreeNode)branch.GetSubtree(0).Clone(); 122 return Div(CreateConstant(1.0), Product(Product(CreateConstant(2.0), f), Derive(u, variableName)));138 return Product(Div(CreateConstant(1.0), Product(CreateConstant(2.0), f)), Derive(u, variableName)); 123 139 } 124 140 if (branch.Symbol is Sine) { … … 134 150 return Product(CreateConstant(-1.0), Product(sin, Derive(u, variableName))); 135 151 } 136 throw new NotSupportedException($"Symbol {branch.Symbol} is not supported."); 152 if (branch.Symbol is Tangent) { 153 // tan(x)' = 1 / cos²(x) 154 var fxp = Derive(branch.GetSubtree(0), variableName); 155 var u = (ISymbolicExpressionTreeNode)branch.GetSubtree(0).Clone(); 156 return Div(fxp, Square(Cosine(u))); 157 } 158 throw new NotSupportedException(string.Format("Symbol {0} is not supported.", branch.Symbol)); 137 159 } 138 160 … … 144 166 return product; 145 167 } 168 private static ISymbolicExpressionTreeNode Product(IEnumerable<ISymbolicExpressionTreeNode> fs) { 169 var product = mulSy.CreateTreeNode(); 170 foreach (var f in fs) product.AddSubtree(f); 171 return product; 172 } 146 173 private static ISymbolicExpressionTreeNode Div(ISymbolicExpressionTreeNode f, ISymbolicExpressionTreeNode g) { 147 174 var div = divSy.CreateTreeNode(); … … 163 190 return sum; 164 191 } 165 192 private static ISymbolicExpressionTreeNode Cosine(ISymbolicExpressionTreeNode f) { 193 var cos = cosSy.CreateTreeNode(); 194 cos.AddSubtree(f); 195 return cos; 196 } 197 private static ISymbolicExpressionTreeNode Square(ISymbolicExpressionTreeNode f) { 198 var sqr = sqrSy.CreateTreeNode(); 199 sqr.AddSubtree(f); 200 return sqr; 201 } 202 166 203 private static ISymbolicExpressionTreeNode CreateConstant(double v) { 167 204 var constNode = (ConstantTreeNode)constantSy.CreateTreeNode(); … … 186 223 !(n.Symbol is Sine) && 187 224 !(n.Symbol is Cosine) && 225 !(n.Symbol is Tangent) && 188 226 !(n.Symbol is StartSymbol) 189 227 select n).Any(); -
stable/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Converters/TreeSimplifier.cs
r15584 r17066 1121 1121 // $ * 1.0 => $ 1122 1122 return a; 1123 } else if (IsConstant(b) && ((ConstantTreeNode)b).Value.IsAlmost(0.0)) { 1124 return MakeConstant(0); 1123 1125 } else if (IsConstant(b) && IsVariableBase(a)) { 1124 1126 // multiply constants into variables weights -
stable/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4.csproj
r16436 r17066 139 139 <Compile Include="Converters\LinearModelToTreeConverter.cs" /> 140 140 <Compile Include="Converters\TreeSimplifier.cs" /> 141 <Compile Include="Converters\DerivativeCalculator.cs" /> 141 142 <Compile Include="Converters\TreeToAutoDiffTermConverter.cs" /> 142 143 <Compile Include="Formatters\InfixExpressionFormatter.cs" />
Note: See TracChangeset
for help on using the changeset viewer.