Free cookie consent management tool by TermsFeed Policy Generator

Changeset 16213


Ignore:
Timestamp:
10/06/18 08:56:14 (6 years ago)
Author:
gkronber
Message:

#2948 changed unit test cases to assert results of derivative calculations. Fixed bug in deriving sqrt(x)

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Converters/DerivativeCalculator.cs

    r16206 r16213  
    120120        var f = (ISymbolicExpressionTreeNode)branch.Clone();
    121121        var u = (ISymbolicExpressionTreeNode)branch.GetSubtree(0).Clone();
    122         return Div(CreateConstant(1.0), Product(Product(CreateConstant(2.0), f), Derive(u, variableName)));
     122        return Product(Div(CreateConstant(1.0), Product(CreateConstant(2.0), f)), Derive(u, variableName));
    123123      }
    124124      if (branch.Symbol is Sine) {
  • trunk/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4/DeriveTest.cs

    r16206 r16213  
    3535      var formatter = new InfixExpressionFormatter();
    3636      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"));
     37      Assert.AreEqual("0", Derive("3", "x"));
     38      Assert.AreEqual("1", Derive("x", "x"));
     39      Assert.AreEqual("10", Derive("10*x", "x"));
     40      Assert.AreEqual("10", Derive("x*10", "x"));
     41      Assert.AreEqual("(2*'x')", Derive("x*x", "x"));
     42      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
     43      Assert.AreEqual("0", Derive("10*x", "y"));
     44      Assert.AreEqual("20", Derive("10*x+20*y", "y"));
     45      Assert.AreEqual("6", Derive("2*3*x", "x"));
     46      Assert.AreEqual("(10*'y')", Derive("10*x*y+20*y", "x"));
     47      Assert.AreEqual("(1 / (SQR('x') * (-1)))",  Derive("1/x", "x"));
     48      Assert.AreEqual("('y' / (SQR('x') * (-1)))", Derive("y/x", "x"));
     49      Assert.AreEqual("((((-2*'x') + (-1)) * ('a' + 'b')) / SQR(('x' + ('x' * 'x'))))",
     50        Derive("(a+b)/(x+x*x)", "x"));
     51      Assert.AreEqual("((((-2*'x') + (-1)) * ('a' + 'b')) / SQR(('x' + SQR('x'))))", Derive("(a+b)/(x+SQR(x))", "x"));
     52      Assert.AreEqual("EXP('x')", Derive("exp(x)", "x"));
     53      Assert.AreEqual("(EXP((3*'x')) * 3)", Derive("exp(3*x)", "x"));
     54      Assert.AreEqual("(1 / 'x')", Derive("log(x)", "x"));
     55      Assert.AreEqual("(1 / 'x')", Derive("log(3*x)", "x"));   // 3 * 1/(3*x)
     56      Assert.AreEqual("(1 / ('x' + (0.333333333333333*'y')))", Derive("log(3*x+y)", "x"));  // simplifier does not try to keep fractions
     57      Assert.AreEqual("(1 / (SQRT(((3*'x') + 'y')) * 0.666666666666667))", Derive("sqrt(3*x+y)", "x"));   // 3 / (2 * sqrt(3*x+y)) = 1 / ((2/3) * sqrt(3*x+y))
     58      Assert.AreEqual("(COS((3*'x')) * 3)", Derive("sin(3*x)", "x"));
     59      Assert.AreEqual("(SIN((3*'x')) * (-3))", Derive("cos(3*x)", "x"));
     60
    5961
    6062      // special case: Inv(x) using only one argument to the division symbol
     63      // f(x) = 1/x
    6164      var root = new ProgramRootSymbol().CreateTreeNode();
    6265      var start = new StartSymbol().CreateTreeNode();
     
    6972      root.AddSubtree(start);
    7073      var t = new SymbolicExpressionTree(root);
    71       Console.WriteLine(formatter.Format(DerivativeCalculator.Derive(t, "x")));
     74      Assert.AreEqual("(1 / (SQR('x') * (-1)))",
     75        formatter.Format(DerivativeCalculator.Derive(t, "x")));
    7276    }
    7377
Note: See TracChangeset for help on using the changeset viewer.