Free cookie consent management tool by TermsFeed Policy Generator

Changeset 18170


Ignore:
Timestamp:
12/27/21 09:57:49 (2 years ago)
Author:
gkronber
Message:

#3145 changed unit tests to check new expected output (unnecessary parenthesis removed) --> unit tests fail

Location:
trunk/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4/DeriveTest.cs

    r18132 r18170  
    3434    public void DeriveExpressions() {
    3535      var formatter = new InfixExpressionFormatter();
    36       var parser = new InfixExpressionParser();
    3736      Assert.AreEqual("0", Derive("<num=3>", "x"));
    3837      Assert.AreEqual("1", Derive("x", "x"));
    3938      Assert.AreEqual("10", Derive("<num=10>*x", "x"));
    4039      Assert.AreEqual("10", Derive("x*<num=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
     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
    4342      Assert.AreEqual("0", Derive("<num=10>*x", "y"));
    4443      Assert.AreEqual("20", Derive("<num=10>*x+<num=20>*y", "y"));
    4544      Assert.AreEqual("6", Derive("<num=2>*<num=3>*x", "x"));
    46       Assert.AreEqual("(10*'y')", Derive("<num=10>*x*y+<num=20>*y", "x"));
     45      Assert.AreEqual("10 * 'y'", Derive("<num=10>*x*y+<num=20>*y", "x"));
    4746      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'))))",
     47      Assert.AreEqual("'y' / (SQR('x') * -1)", Derive("y/x", "x"));
     48      Assert.AreEqual("(-2*'x' + -1) * ('a' + 'b') / SQR('x' + 'x' * 'x')",
    5049        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"));
     50      Assert.AreEqual("(-2*'x' + -1) * ('a' + 'b') / SQR('x' + SQR('x'))", Derive("(a+b)/(x+SQR(x))", "x"));
    5251      Assert.AreEqual("EXP('x')", Derive("exp(x)", "x"));
    53       Assert.AreEqual("(EXP((3*'x')) * 3)", Derive("exp(<num=3>*x)", "x"));
    54       Assert.AreEqual("(1 / 'x')", Derive("log(x)", "x"));
    55       Assert.AreEqual("(1 / 'x')", Derive("log(<num=3>*x)", "x"));   // 3 * 1/(3*x)
    56       Assert.AreEqual("(1 / ('x' + (0.333333333333333*'y')))", Derive("log(<num=3>*x+y)", "x"));  // simplifier does not try to keep fractions
    57       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))
    58       Assert.AreEqual("(COS((3*'x')) * 3)", Derive("sin(<num=3>*x)", "x"));
    59       Assert.AreEqual("(SIN((3*'x')) * (-3))", Derive("cos(<num=3>*x)", "x"));
    60       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
     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
    6160
    62       Assert.AreEqual("((9*'x') / ABS((3*'x')))", Derive("abs(<num=3>*x)", "x"));
    63       Assert.AreEqual("(SQR('x') * 3)", Derive("cube(x)", "x"));
    64       Assert.AreEqual("(1 / (SQR(CUBEROOT('x')) * 3))", Derive("cuberoot(x)", "x"));
     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"));
    6564
    6665      Assert.AreEqual("0", Derive("(a+b)/(x+SQR(x))", "y")); // df(a,b,x) / dy = 0
    6766
    6867
    69       Assert.AreEqual("('a' * 'b' * 'c')", Derive("a*b*c*d", "d"));
    70       Assert.AreEqual("('a' / ('b' * 'c' * SQR('d') * (-1)))", Derive("a/b/c/d", "d"));
     68      Assert.AreEqual("'a' * 'b' * 'c'", Derive("a*b*c*d", "d"));
     69      Assert.AreEqual("'a' / ('b' * 'c' * SQR('d') * -1)", Derive("a/b/c/d", "d"));
    7170
    72       Assert.AreEqual("('x' * ((SQR(TANH(SQR('x'))) * (-1)) + 1) * 2)", Derive("tanh(sqr(x))", "x")); // (2*'x'*(1 - SQR(TANH(SQR('x'))))
     71      Assert.AreEqual("'x' * (SQR(TANH(SQR('x'))) * -1 + 1) * 2", Derive("tanh(sqr(x))", "x")); // (2*'x'*(1 - SQR(TANH(SQR('x'))))
    7372
    7473      {
     
    125124        var t = new SymbolicExpressionTree(root);
    126125
    127         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
     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
    128127                                                                            // 60 y z / y² z² 20² == 6 / y z 40 == 3 / y z 20
    129128          formatter.Format(DerivativeCalculator.Derive(t, "x")));
    130         Assert.AreEqual("(('x' * 'z' * (-60)) / (SQR('y') * SQR('z') * 400))", // actually 3x * -(4 5 z) / (4y 5z)² = -3x / (20 y² z)
     129        Assert.AreEqual("'x' * 'z' * -60 / (SQR('y') * SQR('z') * 400)", // actually 3x * -(4 5 z) / (4y 5z)² = -3x / (20 y² z)
    131130                                                                               // -3 4 5 x z / 4² y² 5² z² = -60 x z / 20² z² y² ==    -60 x z / y² z² 20²
    132131          formatter.Format(DerivativeCalculator.Derive(t, "y")));
    133         Assert.AreEqual("(('x' * 'y' * (-60)) / (SQR('y') * SQR('z') * 400))",
     132        Assert.AreEqual("'x' * 'y' * -60 / (SQR('y') * SQR('z') * 400)",
    134133          formatter.Format(DerivativeCalculator.Derive(t, "z")));
    135134      }
  • trunk/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4/InfixExpressionParserTest.cs

    r18169 r18170  
    3535      var parser = new InfixExpressionParser();
    3636      Assert.AreEqual("3", formatter.Format(parser.Parse("3")));
    37       Assert.AreEqual("(3 * 3)", formatter.Format(parser.Parse("3*3")));
    38       Assert.AreEqual("(3 * 4)",formatter.Format(parser.Parse("3 * 4")));
     37      Assert.AreEqual("3 * 3", formatter.Format(parser.Parse("3*3")));
     38      Assert.AreEqual("3 * 4",formatter.Format(parser.Parse("3 * 4")));
    3939      Assert.AreEqual("0.123",formatter.Format(parser.Parse("123E-03")));
    4040      Assert.AreEqual("0.123",formatter.Format(parser.Parse("123e-03")));
     
    5050      Assert.AreEqual("123000",formatter.Format(parser.Parse("123.0E+3")));
    5151
    52       Assert.AreEqual("(3.1415 + 2)",formatter.Format(parser.Parse("3.1415+2.0")));
    53       Assert.AreEqual("(3.1415 / 2)",formatter.Format(parser.Parse("3.1415/2.0")));
    54       Assert.AreEqual("(3.1415 * 2)",formatter.Format(parser.Parse("3.1415*2.0")));
    55       Assert.AreEqual("(3.1415 - 2)", formatter.Format(parser.Parse("3.1415-2.0")));
     52      Assert.AreEqual("3.1415 + 2",formatter.Format(parser.Parse("3.1415+2.0")));
     53      Assert.AreEqual("3.1415 / 2",formatter.Format(parser.Parse("3.1415/2.0")));
     54      Assert.AreEqual("3.1415 * 2",formatter.Format(parser.Parse("3.1415*2.0")));
     55      Assert.AreEqual("3.1415 - 2", formatter.Format(parser.Parse("3.1415-2.0")));
    5656      // round-trip
    57       Assert.AreEqual("(3.1415 - 2)",formatter.Format(parser.Parse(formatter.Format(parser.Parse("3.1415-2.0")))));
    58       Assert.AreEqual("(3.1415 + 2)",formatter.Format(parser.Parse("3.1415+(2.0)")));
    59       Assert.AreEqual("(3.1415 + 2)", formatter.Format(parser.Parse("(3.1415+(2.0))")));
     57      Assert.AreEqual("3.1415 - 2",formatter.Format(parser.Parse(formatter.Format(parser.Parse("3.1415-2.0")))));
     58      Assert.AreEqual("3.1415 + 2",formatter.Format(parser.Parse("3.1415+(2.0)")));
     59      Assert.AreEqual("3.1415 + 2", formatter.Format(parser.Parse("(3.1415+(2.0))")));
    6060
    6161
    6262      Assert.AreEqual("LOG(3)",formatter.Format(parser.Parse("log(3)")));
    63       Assert.AreEqual("LOG((-3))",formatter.Format(parser.Parse("log(-3)")));
     63      Assert.AreEqual("LOG(-3)",formatter.Format(parser.Parse("log(-3)")));
    6464      Assert.AreEqual("EXP(3)",formatter.Format(parser.Parse("exp(3)")));
    65       Assert.AreEqual("EXP((-3))",formatter.Format(parser.Parse("exp(-3)")));
     65      Assert.AreEqual("EXP(-3)",formatter.Format(parser.Parse("exp(-3)")));
    6666      Assert.AreEqual("SQRT(3)", formatter.Format(parser.Parse("sqrt(3)")));
    6767
    68       Assert.AreEqual("SQR((-3))", formatter.Format(parser.Parse("sqr((-3))")));
     68      Assert.AreEqual("SQR(-3)", formatter.Format(parser.Parse("sqr((-3))")));
    6969
    70       Assert.AreEqual("((3 / 3) + (2 / 2) + (1 / 1))",formatter.Format(parser.Parse("3/3+2/2+1/1")));
    71       Assert.AreEqual("((((((-3) + 30) - 2) + 20) - 1) + 10)", formatter.Format(parser.Parse("-3+30-2+20-1+10")));
     70      Assert.AreEqual("3 / 3 + 2 / 2 + 1 / 1",formatter.Format(parser.Parse("3/3+2/2+1/1")));
     71      Assert.AreEqual("-3 + 30 - 2 + 20 - 1 + 10", formatter.Format(parser.Parse("-3+30-2+20-1+10")));
    7272
    7373      // 'flattening' of nested addition, subtraction, multiplication, or division
    74       Assert.AreEqual("(1 + 2 + 3 + 4)", formatter.Format(parser.Parse("1 + 2 + 3 + 4")));
    75       Assert.AreEqual("(1 - 2 - 3 - 4)", formatter.Format(parser.Parse("1 - 2 - 3 - 4")));
    76       Assert.AreEqual("(1 * 2 * 3 * 4)", formatter.Format(parser.Parse("1 * 2 * 3 * 4")));
    77       Assert.AreEqual("(1 / 2 / 3 / 4)", formatter.Format(parser.Parse("1 / 2 / 3 / 4")));
     74      Assert.AreEqual("1 + 2 + 3 + 4", formatter.Format(parser.Parse("1 + 2 + 3 + 4")));
     75      Assert.AreEqual("1 - 2 - 3 - 4", formatter.Format(parser.Parse("1 - 2 - 3 - 4")));
     76      Assert.AreEqual("1 * 2 * 3 * 4", formatter.Format(parser.Parse("1 * 2 * 3 * 4")));
     77      Assert.AreEqual("1 / 2 / 3 / 4", formatter.Format(parser.Parse("1 / 2 / 3 / 4")));
    7878
    7979      // signed variables / constants
    80       Assert.AreEqual("(-1*'x1')", formatter.Format(parser.Parse("-x1")));
     80      Assert.AreEqual("-1*'x1'", formatter.Format(parser.Parse("-x1")));
    8181      Assert.AreEqual("1", formatter.Format(parser.Parse("--1.0")));
    8282      Assert.AreEqual("1", formatter.Format(parser.Parse("----1.0")));
    8383      Assert.AreEqual("1", formatter.Format(parser.Parse("-+-1.0")));
    8484      Assert.AreEqual("1", formatter.Format(parser.Parse("+-+-1.0")));
    85       Assert.AreEqual("((-3) + (-1))", formatter.Format(parser.Parse("-3 + -1.0")));
     85      Assert.AreEqual("-3 + -1", formatter.Format(parser.Parse("-3 + -1.0")));
    8686
    8787
     
    9595
    9696
    97       Assert.AreEqual("('x1' * 'x2')",formatter.Format(parser.Parse("\"x1\"*\"x2\"")));
    98       Assert.AreEqual("(('x1' * 'x2') + ('x3' * 'x4'))",formatter.Format(parser.Parse("\"x1\"*\"x2\"+\"x3\"*\"x4\"")));
    99       Assert.AreEqual("(('x1' * 'x2') + ('x3' * 'x4'))", formatter.Format(parser.Parse("x1*x2+x3*x4")));
     97      Assert.AreEqual("'x1' * 'x2'",formatter.Format(parser.Parse("\"x1\"*\"x2\"")));
     98      Assert.AreEqual("'x1' * 'x2' + 'x3' * 'x4'",formatter.Format(parser.Parse("\"x1\"*\"x2\"+\"x3\"*\"x4\"")));
     99      Assert.AreEqual("'x1' * 'x2' + 'x3' * 'x4'", formatter.Format(parser.Parse("x1*x2+x3*x4")));
    100100
    101101
    102       Assert.AreEqual("(3 ^ 2)",formatter.Format(parser.Parse("POW(3, 2)")));
    103       Assert.AreEqual("(3.1 ^ 2.1)",formatter.Format(parser.Parse("POW(3.1, 2.1)")));
    104       Assert.AreEqual("(3.1 ^ 2.1)",formatter.Format(parser.Parse("POW(3.1 , 2.1)")));
    105       Assert.AreEqual("(3.1 ^ 2.1)",formatter.Format(parser.Parse("POW(3.1 ,2.1)")));
    106       Assert.AreEqual("((-3.1) ^ (-2.1))",formatter.Format(parser.Parse("POW(-3.1 , - 2.1)")));
     102      Assert.AreEqual("3 ^ 2",formatter.Format(parser.Parse("POW(3, 2)")));
     103      Assert.AreEqual("3.1 ^ 2.1",formatter.Format(parser.Parse("POW(3.1, 2.1)")));
     104      Assert.AreEqual("3.1 ^ 2.1",formatter.Format(parser.Parse("POW(3.1 , 2.1)")));
     105      Assert.AreEqual("3.1 ^ 2.1",formatter.Format(parser.Parse("POW(3.1 ,2.1)")));
     106      Assert.AreEqual("-3.1 ^ -2.1",formatter.Format(parser.Parse("POW(-3.1 , - 2.1)")));
    107107      Assert.AreEqual("ROOT(3, 2)",formatter.Format(parser.Parse("ROOT(3, 2)")));
    108108      Assert.AreEqual("ROOT(3.1, 2.1)",formatter.Format(parser.Parse("ROOT(3.1, 2.1)")));
    109109      Assert.AreEqual("ROOT(3.1, 2.1)",formatter.Format(parser.Parse("ROOT(3.1 , 2.1)")));
    110110      Assert.AreEqual("ROOT(3.1, 2.1)",formatter.Format(parser.Parse("ROOT(3.1 ,2.1)")));
    111       Assert.AreEqual("ROOT((-3.1), (-2.1))", formatter.Format(parser.Parse("ROOT(-3.1 , -2.1)")));
     111      Assert.AreEqual("ROOT(-3.1, -2.1)", formatter.Format(parser.Parse("ROOT(-3.1 , -2.1)")));
    112112
    113113      Assert.AreEqual("IF(GT(0, 1), 1, 0)",formatter.Format(parser.Parse("IF(GT( 0, 1), 1, 0)")));
     
    116116      Assert.AreEqual("LAG('x', -1)",formatter.Format(parser.Parse("LAG(x, -1)")));
    117117      Assert.AreEqual("LAG('x', 1)",formatter.Format(parser.Parse("LAG(x, +1)")));
    118       Assert.AreEqual("('x' * LAG('x', 1))", formatter.Format(parser.Parse("x * LAG('x', +1)")));
     118      Assert.AreEqual("'x' * LAG('x', 1)", formatter.Format(parser.Parse("x * LAG('x', +1)")));
    119119
    120120      // factor variables
    121       Assert.AreEqual("('x'[1] * 'y')",formatter.Format(parser.Parse("x [1.0] * y")));
    122       Assert.AreEqual("('x'[1, 2] * 'y'[1, 2])",formatter.Format(parser.Parse("x [1.0, 2.0] * y [1.0, 2.0]")));
    123       Assert.AreEqual("('x'[1] * 'y')",formatter.Format(parser.Parse("x[1] * y")));
    124       Assert.AreEqual("('x'[1, 2] * 'y'[1, 2])",formatter.Format(parser.Parse("x[1, 2] * y [1, 2]")));
    125       Assert.AreEqual("('x'[1] * 'y')",formatter.Format(parser.Parse("x [+1.0] * y")));
    126       Assert.AreEqual("('x'[-1] * 'y')",formatter.Format(parser.Parse("x [-1.0] * y")));
    127       Assert.AreEqual("('x'[-1, -2] * 'y'[1, 2])", formatter.Format(parser.Parse("x [-1.0, -2.0] * y [+1.0, +2.0]")));
     121      Assert.AreEqual("'x'[1] * 'y'",formatter.Format(parser.Parse("x [1.0] * y")));
     122      Assert.AreEqual("'x'[1, 2] * 'y'[1, 2]",formatter.Format(parser.Parse("x [1.0, 2.0] * y [1.0, 2.0]")));
     123      Assert.AreEqual("'x'[1] * 'y'",formatter.Format(parser.Parse("x[1] * y")));
     124      Assert.AreEqual("'x'[1, 2] * 'y'[1, 2]",formatter.Format(parser.Parse("x[1, 2] * y [1, 2]")));
     125      Assert.AreEqual("'x'[1] * 'y'",formatter.Format(parser.Parse("x [+1.0] * y")));
     126      Assert.AreEqual("'x'[-1] * 'y'",formatter.Format(parser.Parse("x [-1.0] * y")));
     127      Assert.AreEqual("'x'[-1, -2] * 'y'[1, 2]", formatter.Format(parser.Parse("x [-1.0, -2.0] * y [+1.0, +2.0]")));
    128128
    129129      // one-hot for factor
    130       Assert.AreEqual("('x' = 'val' * 'y')",formatter.Format(parser.Parse("x='val' * y")));
     130      Assert.AreEqual("'x' = 'val' * 'y'",formatter.Format(parser.Parse("x='val' * y")));
    131131      Assert.AreEqual("'x' = 'val'",formatter.Format(parser.Parse("x = 'val'")));
    132132      Assert.AreEqual("'x' = 'val'",formatter.Format(parser.Parse("x = \"val\"")));
    133       Assert.AreEqual("(1 * 'x' = 'val')",formatter.Format(parser.Parse("1.0 * x = val")));
    134       Assert.AreEqual("((-1) * 'x' = 'val')",formatter.Format(parser.Parse("-1.0 * x = val")));
    135       Assert.AreEqual("((1 * 'x' = 'val1') + 'y' = 'val2')", formatter.Format(parser.Parse("+1.0 * \"x\" = val1 + y = \"val2\"")));
     133      Assert.AreEqual("1 * 'x' = 'val'",formatter.Format(parser.Parse("1.0 * x = val")));
     134      Assert.AreEqual("-1 * 'x' = 'val'",formatter.Format(parser.Parse("-1.0 * x = val")));
     135      Assert.AreEqual("1 * 'x' = 'val1' + 'y' = 'val2'", formatter.Format(parser.Parse("+1.0 * \"x\" = val1 + y = \"val2\"")));
    136136
    137137      // numeric parameters
     
    140140      Assert.AreEqual("1", formatter.Format(parser.Parse("< num=1.0>")));
    141141      Assert.AreEqual("1", formatter.Format(parser.Parse("< num = 1.0>")));
    142       Assert.AreEqual("(-1)", formatter.Format(parser.Parse("< num =-1.0>")));
    143       Assert.AreEqual("(-1)", formatter.Format(parser.Parse("< num = - 1.0>")));
     142      Assert.AreEqual("-1", formatter.Format(parser.Parse("< num =-1.0>")));
     143      Assert.AreEqual("-1", formatter.Format(parser.Parse("< num = - 1.0>")));
    144144     
    145145      // numeric parameter with sign
Note: See TracChangeset for help on using the changeset viewer.