Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4/DeriveTest.cs @ 18183

Last change on this file since 18183 was 18172, checked in by gkronber, 3 years ago

#3145: removed unnecessary parenthesis in InfixExpressionFormatter (and adapted the unit tests slightly)

File size: 7.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22
23using System;
24using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
25using Microsoft.VisualStudio.TestTools.UnitTesting;
26namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Tests {
27
28
29  [TestClass]
30  public class DerivativeTest {
31    [TestMethod]
32    [TestCategory("Problems.DataAnalysis.Symbolic")]
33    [TestProperty("Time", "short")]
34    public void DeriveExpressions() {
35      var formatter = new InfixExpressionFormatter();
36      Assert.AreEqual("0", Derive("<num=3>", "x"));
37      Assert.AreEqual("1", Derive("x", "x"));
38      Assert.AreEqual("10", Derive("<num=10>*x", "x"));
39      Assert.AreEqual("10", Derive("x*<num=10>", "x"));
40      Assert.AreEqual("2*'x'", Derive("x*x", "x"));
41      Assert.AreEqual("'x' * 'x' * 2 + 'x' * 'x'", Derive("x*x*x", "x")); // simplifier does not merge (x*x)*2 + x*x  to 3*x*x
42      Assert.AreEqual("0", Derive("<num=10>*x", "y"));
43      Assert.AreEqual("20", Derive("<num=10>*x+<num=20>*y", "y"));
44      Assert.AreEqual("6", Derive("<num=2>*<num=3>*x", "x"));
45      Assert.AreEqual("10*'y'", Derive("<num=10>*x*y+<num=20>*y", "x"));
46      Assert.AreEqual("1 * -1 / SQR('x')", Derive("1/x", "x"));
47      Assert.AreEqual("-1*'y' / SQR('x')", Derive("y/x", "x"));
48      Assert.AreEqual("('a' + 'b') * (-2*'x' + -1) / SQR('x' + 'x' * 'x')",
49        Derive("(a+b)/(x+x*x)", "x"));
50      Assert.AreEqual("('a' + 'b') * (-2*'x' + -1) / SQR('x' + SQR('x'))", Derive("(a+b)/(x+SQR(x))", "x"));
51      Assert.AreEqual("EXP('x')", Derive("exp(x)", "x"));
52      Assert.AreEqual("EXP(3*'x') * 3", Derive("exp(<num=3>*x)", "x"));
53      Assert.AreEqual("1 / 'x'", Derive("log(x)", "x"));
54      Assert.AreEqual("1 / 'x'", Derive("log(<num=3>*x)", "x"));   // 3 * 1/(3*x)
55      Assert.AreEqual("1 / ('x' + 0.333333333333333*'y')", Derive("log(<num=3>*x+y)", "x"));  // simplifier does not try to keep fractions
56      Assert.AreEqual("1 / (SQRT(3*'x' + 'y') * 0.666666666666667)", Derive("sqrt(<num=3>*x+y)", "x"));   // 3 / (2 * sqrt(3*x+y)) = 1 / ((2/3) * sqrt(3*x+y))
57      Assert.AreEqual("COS(3*'x') * 3", Derive("sin(<num=3>*x)", "x"));
58      Assert.AreEqual("SIN(3*'x') * -3", Derive("cos(<num=3>*x)", "x"));
59      Assert.AreEqual("1 / (SQR(COS(3*'x')) * 0.333333333333333)", Derive("tan(<num=3>*x)", "x")); // diff(tan(f(x)), x) = 1.0 / cos²(f(x)), simplifier puts constant factor into the denominator
60
61      Assert.AreEqual("9*'x' / ABS(3*'x')", Derive("abs(<num=3>*x)", "x"));
62      Assert.AreEqual("SQR('x') * 3", Derive("cube(x)", "x"));
63      Assert.AreEqual("1 / (SQR(CUBEROOT('x')) * 3)", Derive("cuberoot(x)", "x"));
64
65      Assert.AreEqual("0", Derive("(a+b)/(x+SQR(x))", "y")); // df(a,b,x) / dy = 0
66
67
68      Assert.AreEqual("'a' * 'b' * 'c'", Derive("a*b*c*d", "d"));
69      Assert.AreEqual("'b' * 'c' * -1*'a' / (SQR('b') * SQR('c') * SQR('d'))", Derive("a/b/c/d", "d")); // TODO simplifier should be able to simplify this
70
71      Assert.AreEqual("'x' * (SQR(TANH(SQR('x'))) * -1 + 1) * 2", Derive("tanh(sqr(x))", "x")); // (2*'x'*(1 - SQR(TANH(SQR('x'))))
72
73      {
74        // special case: Inv(x) using only one argument to the division symbol
75        // f(x) = 1/x
76        var root = new ProgramRootSymbol().CreateTreeNode();
77        var start = new StartSymbol().CreateTreeNode();
78        var div = new Division().CreateTreeNode();
79        var varNode = (VariableTreeNode)(new Variable().CreateTreeNode());
80        varNode.Weight = 1.0;
81        varNode.VariableName = "x";
82        div.AddSubtree(varNode);
83        start.AddSubtree(div);
84        root.AddSubtree(start);
85        var t = new SymbolicExpressionTree(root);
86        Assert.AreEqual("1 / (SQR('x') * -1)",
87          formatter.Format(DerivativeCalculator.Derive(t, "x")));
88      }
89
90      {
91        // special case: multiplication with only one argument
92        var root = new ProgramRootSymbol().CreateTreeNode();
93        var start = new StartSymbol().CreateTreeNode();
94        var mul = new Multiplication().CreateTreeNode();
95        var varNode = (VariableTreeNode)(new Variable().CreateTreeNode());
96        varNode.Weight = 3.0;
97        varNode.VariableName = "x";
98        mul.AddSubtree(varNode);
99        start.AddSubtree(mul);
100        root.AddSubtree(start);
101        var t = new SymbolicExpressionTree(root);
102        Assert.AreEqual("3",
103          formatter.Format(DerivativeCalculator.Derive(t, "x")));
104      }
105
106      {
107        // division with multiple arguments
108        // div(x, y, z) is interpreted as (x / y) / z
109        var root = new ProgramRootSymbol().CreateTreeNode();
110        var start = new StartSymbol().CreateTreeNode();
111        var div = new Division().CreateTreeNode();
112        var varNode1 = (VariableTreeNode)(new Variable().CreateTreeNode());
113        varNode1.Weight = 3.0;
114        varNode1.VariableName = "x";
115        var varNode2 = (VariableTreeNode)(new Variable().CreateTreeNode());
116        varNode2.Weight = 4.0;
117        varNode2.VariableName = "y";
118        var varNode3 = (VariableTreeNode)(new Variable().CreateTreeNode());
119        varNode3.Weight = 5.0;
120        varNode3.VariableName = "z";
121        div.AddSubtree(varNode1); div.AddSubtree(varNode2); div.AddSubtree(varNode3);
122        start.AddSubtree(div);
123        root.AddSubtree(start);
124        var t = new SymbolicExpressionTree(root);
125
126        Assert.AreEqual("'y' * 'z' * 60 / (SQR('y') * SQR('z') * 400)", // actually 3 / (4y  5z) but simplifier is not smart enough to cancel numerator and denominator
127                                                                            // 60 y z / y² z² 20² == 6 / y z 40 == 3 / y z 20
128          formatter.Format(DerivativeCalculator.Derive(t, "x")));
129        Assert.AreEqual("'x' * 'z' * -60 / (SQR('y') * SQR('z') * 400)", // actually 3x * -(4 5 z) / (4y 5z)² = -3x / (20 y² z)
130                                                                               // -3 4 5 x z / 4² y² 5² z² = -60 x z / 20² z² y² ==    -60 x z / y² z² 20²
131          formatter.Format(DerivativeCalculator.Derive(t, "y")));
132        Assert.AreEqual("'x' * 'y' * -60 / (SQR('y') * SQR('z') * 400)",
133          formatter.Format(DerivativeCalculator.Derive(t, "z")));
134      }
135    }
136
137    private string Derive(string expr, string variable) {
138      var parser = new InfixExpressionParser();
139      var formatter = new InfixExpressionFormatter();
140
141      var t = parser.Parse(expr);
142      var tPrime = DerivativeCalculator.Derive(t, variable);
143
144      return formatter.Format(tPrime);
145    }
146  }
147}
Note: See TracBrowser for help on using the repository browser.