Changeset 16294 for trunk/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4/DeriveTest.cs
- Timestamp:
- 11/12/18 20:45:01 (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4/DeriveTest.cs
r16213 r16294 45 45 Assert.AreEqual("6", Derive("2*3*x", "x")); 46 46 Assert.AreEqual("(10*'y')", Derive("10*x*y+20*y", "x")); 47 Assert.AreEqual("(1 / (SQR('x') * (-1)))", 47 Assert.AreEqual("(1 / (SQR('x') * (-1)))", Derive("1/x", "x")); 48 48 Assert.AreEqual("('y' / (SQR('x') * (-1)))", Derive("y/x", "x")); 49 49 Assert.AreEqual("((((-2*'x') + (-1)) * ('a' + 'b')) / SQR(('x' + ('x' * 'x'))))", … … 58 58 Assert.AreEqual("(COS((3*'x')) * 3)", Derive("sin(3*x)", "x")); 59 59 Assert.AreEqual("(SIN((3*'x')) * (-3))", Derive("cos(3*x)", "x")); 60 Assert.AreEqual("(1 / (SQR(COS((3*'x'))) * 0.333333333333333))", Derive("tan(3*x)", "x")); // diff(tan(f(x)), x) = 1.0 / cos²(f(x)), simplifier puts constant factor into the denominator 60 61 62 { 63 // special case: Inv(x) using only one argument to the division symbol 64 // f(x) = 1/x 65 var root = new ProgramRootSymbol().CreateTreeNode(); 66 var start = new StartSymbol().CreateTreeNode(); 67 var div = new Division().CreateTreeNode(); 68 var varNode = (VariableTreeNode)(new Variable().CreateTreeNode()); 69 varNode.Weight = 1.0; 70 varNode.VariableName = "x"; 71 div.AddSubtree(varNode); 72 start.AddSubtree(div); 73 root.AddSubtree(start); 74 var t = new SymbolicExpressionTree(root); 75 Assert.AreEqual("(1 / (SQR('x') * (-1)))", 76 formatter.Format(DerivativeCalculator.Derive(t, "x"))); 77 } 61 78 62 // special case: Inv(x) using only one argument to the division symbol 63 // f(x) = 1/x 64 var root = new ProgramRootSymbol().CreateTreeNode(); 65 var start = new StartSymbol().CreateTreeNode(); 66 var div = new Division().CreateTreeNode(); 67 var varNode = (VariableTreeNode)(new Variable().CreateTreeNode()); 68 varNode.Weight = 1.0; 69 varNode.VariableName = "x"; 70 div.AddSubtree(varNode); 71 start.AddSubtree(div); 72 root.AddSubtree(start); 73 var t = new SymbolicExpressionTree(root); 74 Assert.AreEqual("(1 / (SQR('x') * (-1)))", 75 formatter.Format(DerivativeCalculator.Derive(t, "x"))); 79 { 80 // special case: multiplication with only one argument 81 var root = new ProgramRootSymbol().CreateTreeNode(); 82 var start = new StartSymbol().CreateTreeNode(); 83 var mul = new Multiplication().CreateTreeNode(); 84 var varNode = (VariableTreeNode)(new Variable().CreateTreeNode()); 85 varNode.Weight = 3.0; 86 varNode.VariableName = "x"; 87 mul.AddSubtree(varNode); 88 start.AddSubtree(mul); 89 root.AddSubtree(start); 90 var t = new SymbolicExpressionTree(root); 91 Assert.AreEqual("3", 92 formatter.Format(DerivativeCalculator.Derive(t, "x"))); 93 } 94 95 { 96 // division with multiple arguments 97 // div(x, y, z) is interpreted as (x / y) / z 98 var root = new ProgramRootSymbol().CreateTreeNode(); 99 var start = new StartSymbol().CreateTreeNode(); 100 var div = new Division().CreateTreeNode(); 101 var varNode1 = (VariableTreeNode)(new Variable().CreateTreeNode()); 102 varNode1.Weight = 3.0; 103 varNode1.VariableName = "x"; 104 var varNode2 = (VariableTreeNode)(new Variable().CreateTreeNode()); 105 varNode2.Weight = 4.0; 106 varNode2.VariableName = "y"; 107 var varNode3 = (VariableTreeNode)(new Variable().CreateTreeNode()); 108 varNode3.Weight = 5.0; 109 varNode3.VariableName = "z"; 110 div.AddSubtree(varNode1); div.AddSubtree(varNode2); div.AddSubtree(varNode3); 111 start.AddSubtree(div); 112 root.AddSubtree(start); 113 var t = new SymbolicExpressionTree(root); 114 115 Assert.AreEqual("(('y' * 'z' * 60) / SQR(('y' * 'z' * 20)))", // actually 3 / (4y 5z) but simplifier is not smart enough to cancel numerator and denominator 116 // 60 y z / y² z² 20² == 6 / y z 40 == 3 / y z 20 117 formatter.Format(DerivativeCalculator.Derive(t, "x"))); 118 Assert.AreEqual("(('x' * 'z' * (-60)) / SQR(('y' * 'z' * 20)))", // actually 3x * -(4 5 z) / (4y 5z)² = -3x / (20 y² z) 119 // -3 4 5 x z / 4² y² 5² z² = -60 x z / 20² z² y² == -60 x z / y² z² 20² 120 formatter.Format(DerivativeCalculator.Derive(t, "y"))); 121 Assert.AreEqual("(('x' * 'y' * (-60)) / SQR(('y' * 'z' * 20)))", 122 formatter.Format(DerivativeCalculator.Derive(t, "z"))); 123 } 76 124 } 77 125
Note: See TracChangeset
for help on using the changeset viewer.