Search in sources :

Example 21 with ExpressionParser

use of edu.cmu.tetrad.calculator.parser.ExpressionParser in project tetrad by cmu-phil.

the class TestParser method testCommutativeOperators.

/**
 * Tests commutative operators
 */
@Test
public void testCommutativeOperators() {
    ExpressionParser parser = new ExpressionParser();
    Expression expression = parse(parser, "1 + 2 + 3");
    assertTrue(expression.evaluate(new TestingContext()) == 6.0);
    expression = parse(parser, "1 + 1 + 1 + (3 * 4)");
    assertTrue(expression.evaluate(new TestingContext()) == 15.0);
    expression = parse(parser, "1 * 1 * 2 * 3 * (1 + 1)");
    assertTrue(expression.evaluate(new TestingContext()) == 12.0);
}
Also used : Expression(edu.cmu.tetrad.calculator.expression.Expression) ConstantExpression(edu.cmu.tetrad.calculator.expression.ConstantExpression) ExpressionParser(edu.cmu.tetrad.calculator.parser.ExpressionParser) Test(org.junit.Test)

Example 22 with ExpressionParser

use of edu.cmu.tetrad.calculator.parser.ExpressionParser in project tetrad by cmu-phil.

the class TestParser method testBasicExpressions.

/**
 * Tests expressions without variables (mainly used while writing the parser)
 */
@Test
public void testBasicExpressions() {
    ExpressionParser parser = new ExpressionParser();
    Expression expression = parse(parser, "+(1,1)");
    assertTrue(expression.evaluate(new TestingContext()) == 2.0);
    expression = parse(parser, "*(+(1,2), 5)");
    assertTrue(expression.evaluate(new TestingContext()) == 15.0);
    expression = parse(parser, "1 + 2.5");
    assertTrue(expression.evaluate(new TestingContext()) == 3.5);
    expression = parse(parser, "(2 + 3)");
    assertTrue(expression.evaluate(new TestingContext()) == 5.0);
    expression = parse(parser, "1 + (3 + 4)");
    assertTrue(expression.evaluate(new TestingContext()) == 8.0);
    expression = parse(parser, "1 + 2 + 5");
    assertTrue(expression.evaluate(new TestingContext()) == 8.0);
    expression = parse(parser, "1 + (2 * 3)");
    assertTrue(expression.evaluate(new TestingContext()) == 7.0);
    expression = parse(parser, "1 + (2 + (3 * 4))");
    assertTrue(expression.evaluate(new TestingContext()) == 15.0);
    expression = parse(parser, "(2 * 3) + (4 * 5)");
    assertTrue(expression.evaluate(new TestingContext()) == 26.0);
    expression = parse(parser, "((2 * 3) + (1 + (2 + (3 * 4))))");
    assertTrue(expression.evaluate(new TestingContext()) == 21.0);
    expression = parse(parser, "pow(2,3)");
    assertTrue(expression.evaluate(new TestingContext()) == 8.0);
    expression = parse(parser, "sqrt(pow(2,2))");
    assertTrue(expression.evaluate(new TestingContext()) == 2.0);
    expression = parse(parser, ConstantExpression.E.getName());
    assertTrue(expression.evaluate(new TestingContext()) == ConstantExpression.E.evaluate(null));
    expression = parse(parser, ConstantExpression.PI.getName());
    assertTrue(expression.evaluate(new TestingContext()) == ConstantExpression.PI.evaluate(null));
    expression = parse(parser, ConstantExpression.PI.getName() + "+ 2");
    assertTrue(expression.evaluate(new TestingContext()) == Math.PI + 2);
}
Also used : Expression(edu.cmu.tetrad.calculator.expression.Expression) ConstantExpression(edu.cmu.tetrad.calculator.expression.ConstantExpression) ExpressionParser(edu.cmu.tetrad.calculator.parser.ExpressionParser) Test(org.junit.Test)

Aggregations

ExpressionParser (edu.cmu.tetrad.calculator.parser.ExpressionParser)22 Expression (edu.cmu.tetrad.calculator.expression.Expression)10 Test (org.junit.Test)8 ParseException (java.text.ParseException)7 ConstantExpression (edu.cmu.tetrad.calculator.expression.ConstantExpression)3 Context (edu.cmu.tetrad.calculator.expression.Context)3 Node (edu.cmu.tetrad.graph.Node)2