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

Last change on this file since 16206 was 16206, checked in by gkronber, 12 months ago

#2948 added first implementation of symbolic differentiation as well as a test case (to be checked manually)

File size: 3.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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      var parser = new InfixExpressionParser();
37      Console.WriteLine(Derive("3", "x"));
38      Console.WriteLine(Derive("x", "x"));
39      Console.WriteLine(Derive("10*x", "x"));
40      Console.WriteLine(Derive("x*10", "x"));
41      Console.WriteLine(Derive("x*x", "x"));
42      Console.WriteLine(Derive("x*x*x", "x"));
43      Console.WriteLine(Derive("10*x", "y"));
44      Console.WriteLine(Derive("10*x+20*y", "y"));
45      Console.WriteLine(Derive("2*3*x", "x"));
46      Console.WriteLine(Derive("10*x*y+20*y", "x"));
47      Console.WriteLine(Derive("1/x", "x"));
48      Console.WriteLine(Derive("y/x", "x"));
49      Console.WriteLine(Derive("(a+b)/(x+x*x)", "x"));
50      Console.WriteLine(Derive("(a+b)/(x+SQR(x))", "x"));
51      Console.WriteLine(Derive("exp(x)", "x"));
52      Console.WriteLine(Derive("exp(3*x)", "x"));
53      Console.WriteLine(Derive("log(x)", "x"));
54      Console.WriteLine(Derive("log(3*x)", "x"));
55      Console.WriteLine(Derive("log(3*x+y)", "x"));
56      Console.WriteLine(Derive("sqrt(3*x+y)", "x"));
57      Console.WriteLine(Derive("sin(3*x)", "x"));
58      Console.WriteLine(Derive("cos(3*x)", "x"));
59
60      // special case: Inv(x) using only one argument to the division symbol
61      var root = new ProgramRootSymbol().CreateTreeNode();
62      var start = new StartSymbol().CreateTreeNode();
63      var div = new Division().CreateTreeNode();
64      var varNode = (VariableTreeNode)(new Variable().CreateTreeNode());
65      varNode.Weight = 1.0;
66      varNode.VariableName = "x";
67      div.AddSubtree(varNode);
68      start.AddSubtree(div);
69      root.AddSubtree(start);
70      var t = new SymbolicExpressionTree(root);
71      Console.WriteLine(formatter.Format(DerivativeCalculator.Derive(t, "x")));
72    }
73
74    private string Derive(string expr, string variable) {
75      var parser = new InfixExpressionParser();
76      var formatter = new InfixExpressionFormatter();
77
78      var t = parser.Parse(expr);
79      var tPrime = DerivativeCalculator.Derive(t, variable);
80
81      return formatter.Format(tPrime);
82    }
83  }
84}
Note: See TracBrowser for help on using the repository browser.